From 12efff23df88ae48cb5d7adce32a70b581814619 Mon Sep 17 00:00:00 2001 From: perceptualrobots <67976964+perceptualrobots@users.noreply.github.com> Date: Wed, 18 Dec 2024 12:29:43 +0000 Subject: [PATCH] Modify PCTExamples Related to #2 --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/perceptualrobots/pct/issues/2?shareId=XXXX-XXXX-XXXX-XXXX). --- nbs/19_pct_examples.ipynb | 32 +---- pct/pct_examples.py | 10 +- ...6abebd2c88246e9f77dd8623eac6e3e.properties | 118 ++++++++++++++++++ 3 files changed, 129 insertions(+), 31 deletions(-) create mode 100644 testfiles/MountainCar/ga-000.326-s066-2x3-6655-a6abebd2c88246e9f77dd8623eac6e3e.properties diff --git a/nbs/19_pct_examples.ipynb b/nbs/19_pct_examples.ipynb index 7baa1017..a379e8fb 100644 --- a/nbs/19_pct_examples.ipynb +++ b/nbs/19_pct_examples.ipynb @@ -86,20 +86,21 @@ " config = json.load(f)\n", " self.hierarchy = PCTHierarchy.from_config(config)\n", "\n", - " def display_summary(self):\n", + " def summary(self):\n", " self.hierarchy.summary()\n", "\n", - " def return_config(self):\n", + " def get_config(self):\n", " return self.hierarchy.get_config()\n", "\n", - " def draw_hierarchy(self, with_labels=True, with_edge_labels=False, font_size=12, font_weight='bold', font_color='black', \n", + " def draw(self, with_labels=True, with_edge_labels=False, font_size=12, font_weight='bold', font_color='black', \n", " color_mapping={'PL':'aqua','OL':'limegreen','CL':'goldenrod', 'RL':'red', 'I':'silver', 'A':'yellow'},\n", " node_size=500, arrowsize=25, align='horizontal', file=None, figsize=(8,8), move={}, draw_fig=True,\n", " node_color=None, layout={'r':2,'c':1,'p':2, 'o':0}, funcdata=False, interactive_mode=False, experiment=None):\n", " self.hierarchy.draw(with_labels, with_edge_labels, font_size, font_weight, font_color, color_mapping, node_size, \n", " arrowsize, align, file, figsize, move, draw_fig, node_color, layout, funcdata, interactive_mode, experiment)\n", "\n", - " def run_hierarchy(self, steps=1, verbose=False):\n", + " def run(self, steps=1, verbose=False, render=False):\n", + " self.hierarchy.get_preprocessor()[0].set_render(render)\n", " return self.hierarchy.run(steps, verbose)\n" ] }, @@ -113,26 +114,5 @@ "import nbdev; nbdev.nbdev_export()" ] } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.11" - } - }, - "nbformat": 4, - "nbformat_minor": 4 + ] } diff --git a/pct/pct_examples.py b/pct/pct_examples.py index 61d42e3b..45075d45 100644 --- a/pct/pct_examples.py +++ b/pct/pct_examples.py @@ -20,19 +20,19 @@ def __init__(self, config_file): config = json.load(f) self.hierarchy = PCTHierarchy.from_config(config) - def display_summary(self): + def summary(self): self.hierarchy.summary() - def return_config(self): + def get_config(self): return self.hierarchy.get_config() - def draw_hierarchy(self, with_labels=True, with_edge_labels=False, font_size=12, font_weight='bold', font_color='black', + def draw(self, with_labels=True, with_edge_labels=False, font_size=12, font_weight='bold', font_color='black', color_mapping={'PL':'aqua','OL':'limegreen','CL':'goldenrod', 'RL':'red', 'I':'silver', 'A':'yellow'}, node_size=500, arrowsize=25, align='horizontal', file=None, figsize=(8,8), move={}, draw_fig=True, node_color=None, layout={'r':2,'c':1,'p':2, 'o':0}, funcdata=False, interactive_mode=False, experiment=None): self.hierarchy.draw(with_labels, with_edge_labels, font_size, font_weight, font_color, color_mapping, node_size, arrowsize, align, file, figsize, move, draw_fig, node_color, layout, funcdata, interactive_mode, experiment) - def run_hierarchy(self, steps=1, verbose=False): + def run(self, steps=1, verbose=False, render=False): + self.hierarchy.get_preprocessor()[0].set_render(render) return self.hierarchy.run(steps, verbose) - diff --git a/testfiles/MountainCar/ga-000.326-s066-2x3-6655-a6abebd2c88246e9f77dd8623eac6e3e.properties b/testfiles/MountainCar/ga-000.326-s066-2x3-6655-a6abebd2c88246e9f77dd8623eac6e3e.properties new file mode 100644 index 00000000..dfc76a55 --- /dev/null +++ b/testfiles/MountainCar/ga-000.326-s066-2x3-6655-a6abebd2c88246e9f77dd8623eac6e3e.properties @@ -0,0 +1,118 @@ +# Date 2024-02-23 17:08:59.068611 +# Result +# Best individual +raw = grid: [1, 2, 3, 2] +env: [['WindTurbine'], [1], [2], [4]] act: [[0.324], 0.556, 3.113, 10.526] +level0 +col: 0 ref: [[0.536, -0.302], 0.376] per: [[-1.558, -0.12, 0.604], 0.448] out: [[-0.11], 0.443] +level1 +col: 0 ref: [[0.033, 0.872, 0.369], 0.698] per: [[0.715], 0.389] out: [[-0.141], 0.94] +col: 1 ref: [[-0.818, -0.179, -0.915], 0.78] per: [[-0.559], 0.34] out: [[0.16], 0.544] +level2 +col: 0 ref: [[0.779, 0.307], 0.351] per: [[-0.261, 0.032], 0.832] out: [[0.479], 0.814] +col: 1 ref: [[-0.463, 0.923], 0.494] per: [[-1.414, -0.447], 0.733] out: [[1.594], 0.517] +col: 2 ref: [[0.732, 0.457], 0.265] per: [[0.222, -0.028], 0.829] out: [[0.256], 0.081] +level3 +col: 0 ref: [[-0.263]] per: [[0.014, 0.259, 0.967], 0.977] out: [[0.941], 0.96]col: 1 ref: [[0.741]] per: [[-0.587, 0.133, -0.436], 0.969] out: [[1.582], 0.522] + +config = {'type': 'Individual', 'name': 'pcthierarchy', 'pre': {'pre0': {'type': 'WindTurbine', 'name': 'WindTurbine', 'value': [0.0, -0.0, 0.0, -0.0, 0.0, 0.0, 0.0], 'links': {0: 'Action1sgsm'}, 'env_name': 'YawEnv'}, 'pre1': {'type': 'IndexedParameter', 'name': 'IYE', 'value': -0.0, 'links': {0: 'WindTurbine'}, 'index': 1}, 'pre2': {'type': 'IndexedParameter', 'name': 'IWD', 'value': 0.0, 'links': {0: 'WindTurbine'}, 'index': 2}, 'pre3': {'type': 'IndexedParameter', 'name': 'IWS', 'value': 0.0, 'links': {0: 'WindTurbine'}, 'index': 4}}, 'levels': {'level0': {'level': 0, 'nodes': {'col0': {'col': 0, 'node': {'type': 'PCTNode', 'name': 'L0C0', 'refcoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'RL0C0sm', 'value': -0.0, 'links': {0: 'OL1C0sm', 1: 'OL1C1sm'}, 'weights': [0.5363283431225842, -0.3020876246108368], 'smooth_factor': 0.37626808573352905}}, 'percoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'PL0C0sm', 'value': 0.0, 'links': {0: 'IYE', 1: 'IWD', 2: 'IWS'}, 'weights': [-1.558107094645247, -0.12006569048610073, 0.6039925484415403], 'smooth_factor': 0.447644445087388}}, 'comcoll': {'0': {'type': 'Subtract', 'name': 'CL0C0', 'value': -0.0, 'links': {0: 'RL0C0sm', 1: 'PL0C0sm'}}}, 'outcoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'OL0C0sm', 'value': 0.0, 'links': {0: 'CL0C0'}, 'weights': [-0.11032634192252495], 'smooth_factor': 0.4432600555050679}}}}}}, 'level1': {'level': 1, 'nodes': {'col0': {'col': 0, 'node': {'type': 'PCTNode', 'name': 'L1C0', 'refcoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'RL1C0sm', 'value': 0.0, 'links': {0: 'OL3C0sm', 1: 'OL3C1sm', 2: 'OL2C2sm'}, 'weights': [0.03342461926527114, 0.8716030856155681, 0.368760690878422], 'smooth_factor': 0.6982397795546179}}, 'percoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'PL1C0sm', 'value': 0.0, 'links': {0: 'PL0C0sm'}, 'weights': [0.7145668630555149], 'smooth_factor': 0.389132815098737}}, 'comcoll': {'0': {'type': 'Subtract', 'name': 'CL1C0', 'value': -0.0, 'links': {0: 'RL1C0sm', 1: 'PL1C0sm'}}}, 'outcoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'OL1C0sm', 'value': 0.0, 'links': {0: 'CL1C0'}, 'weights': [-0.14090033152754525], 'smooth_factor': 0.9395346113510177}}}}, 'col1': {'col': 1, 'node': {'type': 'PCTNode', 'name': 'L1C1', 'refcoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'RL1C1sm', 'value': 0.0, 'links': {0: 'OL3C0sm', 1: 'OL3C1sm', 2: 'OL2C2sm'}, 'weights': [-0.817545889491226, -0.1794117608370509, -0.9152587743035161], 'smooth_factor': 0.7802394727937287}}, 'percoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'PL1C1sm', 'value': -0.0, 'links': {0: 'PL0C0sm'}, 'weights': [-0.5594095205000766], 'smooth_factor': 0.3396145514762595}}, 'comcoll': {'0': {'type': 'Subtract', 'name': 'CL1C1', 'value': 0.0, 'links': {0: 'RL1C1sm', 1: 'PL1C1sm'}}}, 'outcoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'OL1C1sm', 'value': 0.0, 'links': {0: 'CL1C1'}, 'weights': [0.1597689123312396], 'smooth_factor': 0.5441298527436246}}}}}}, 'level2': {'level': 2, 'nodes': {'col0': {'col': 0, 'node': {'type': 'PCTNode', 'name': 'L2C01', 'refcoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'RL2C0sm', 'value': 0.0, 'links': {0: 'OL3C0sm', 1: 'OL3C1sm'}, 'weights': [0.7794309545691012, 0.30655984453012763], 'smooth_factor': 0.35100372423554105}}, 'percoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'PL2C0sm', 'value': -0.0, 'links': {0: 'PL1C0sm', 1: 'PL1C1sm'}, 'weights': [-0.2608923910197565, 0.03241143536343023], 'smooth_factor': 0.8316554600284902}}, 'comcoll': {'0': {'type': 'Subtract', 'name': 'CL2C0', 'value': 0.0, 'links': {0: 'RL2C0sm', 1: 'PL2C0sm'}}}, 'outcoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'OL2C0sm', 'value': 0.0, 'links': {0: 'CL2C0'}, 'weights': [0.47923235551870413], 'smooth_factor': 0.8142967728955316}}}}, 'col1': {'col': 1, 'node': {'type': 'PCTNode', 'name': 'L2C11', 'refcoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'RL2C1sm', 'value': 0.0, 'links': {0: 'OL3C0sm', 1: 'OL3C1sm'}, 'weights': [-0.4626689327121205, 0.923021022955252], 'smooth_factor': 0.49446943756748596}}, 'percoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'PL2C1sm', 'value': -0.0, 'links': {0: 'PL1C0sm', 1: 'PL1C1sm'}, 'weights': [-1.4141771552560118, -0.44722788263550833], 'smooth_factor': 0.7329289542663316}}, 'comcoll': {'0': {'type': 'Subtract', 'name': 'CL2C1', 'value': 0.0, 'links': {0: 'RL2C1sm', 1: 'PL2C1sm'}}}, 'outcoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'OL2C1sm', 'value': 0.0, 'links': {0: 'CL2C1'}, 'weights': [1.5937222695846318], 'smooth_factor': 0.5170907286001206}}}}, 'col2': {'col': 2, 'node': {'type': 'PCTNode', 'name': 'L2C2', 'refcoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'RL2C2sm', 'value': 0.0, 'links': {0: 'OL3C0sm', 1: 'OL3C1sm'}, 'weights': [0.7319247445836443, 0.4565144821856246], 'smooth_factor': 0.26519574223408493}}, 'percoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'PL2C2sm', 'value': 0.0, 'links': {0: 'PL1C0sm', 1: 'PL1C1sm'}, 'weights': [0.22177756590119446, -0.028450834296690486], 'smooth_factor': 0.8288556433078511}}, 'comcoll': {'0': {'type': 'Subtract', 'name': 'CL2C2', 'value': -0.0, 'links': {0: 'RL2C2sm', 1: 'PL2C2sm'}}}, 'outcoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'OL2C2sm', 'value': -0.0, 'links': {0: 'CL2C2'}, 'weights': [0.2560116707412241], 'smooth_factor': 0.080754960161875}}}}}}, 'level3': {'level': 3, 'nodes': {'col0': {'col': 0, 'node': {'type': 'PCTNode', 'name': 'L3C0', 'refcoll': {'0': {'type': 'EAVariable', 'name': 'RL3C0v', 'value': -0.2629598007970895, 'links': {}}}, 'percoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'PL3C0sm', 'value': -0.0, 'links': {0: 'PL2C0sm', 1: 'PL2C1sm', 2: 'PL2C2sm'}, 'weights': [0.014127194348241112, 0.25880838603740736, 0.9667309962355382], 'smooth_factor': 0.9765002553969074}}, 'comcoll': {'0': {'type': 'Subtract', 'name': 'CL3C0', 'value': -0.0, 'links': {0: 'RL3C0v', 1: 'PL3C0sm'}}}, 'outcoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'OL3C0sm', 'value': -0.0, 'links': {0: 'CL3C0'}, 'weights': [0.9413255437600909], 'smooth_factor': 0.9600776044794335}}}}, 'col1': {'col': 1, 'node': {'type': 'PCTNode', 'name': 'L3C1', 'refcoll': {'0': {'type': 'EAVariable', 'name': 'RL3C1v', 'value': 0.740559685914173, 'links': {}}}, 'percoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'PL3C1sm', 'value': -0.0, 'links': {0: 'PL2C0sm', 1: 'PL2C1sm', 2: 'PL2C2sm'}, 'weights': [-0.5866590200441408, 0.13308212374784328, -0.4364789547389667], 'smooth_factor': 0.9690583303072999}}, 'comcoll': {'0': {'type': 'Subtract', 'name': 'CL3C1', 'value': 0.0, 'links': {0: 'RL3C1v', 1: 'PL3C1sm'}}}, 'outcoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'OL3C1sm', 'value': 0.0, 'links': {0: 'CL3C1'}, 'weights': [1.5815798270415076], 'smooth_factor': 0.5223941213718245}}}}}}}, 'post': {'post0': {'type': 'EASigmoidSmoothWeightedSum', 'name': 'Action1sgsm', 'value': 0.0, 'links': {0: 'OL0C0sm'}, 'weights': [0.3242848335373043], 'smooth_factor': 0.5557635679321964, 'range': 3.112778680400676, 'slope': 10.526381208137458}}} +score = -1362.40147 +# Time 0.8706 + +### Description: + +desc = RewardError-SummedError-Mode05 +arch_name = WT0538 + +### Environment parameters + +# Full list of input indexes from environment +# List of input indexes from environment for zero level if not full +# List of input indexes from environment for top level# List of reference values +# Number of actions +# Display names for environment inputs + +env_name = WindTurbine +env_inputs_indexes = [1, 2, 4] +zerolevel_inputs_indexes = None +toplevel_inputs_indexes = None +references = [0, 0] +num_actions = 1 +env_inputs_names = [IYE,IWD,IWS,IPW] +environment_properties={'series': 'steady', 'zero_threshold': 1, 'reward_type': 'power'} + +### Configurable parameters + +# Randomisation seed to reproduce results +# Size of population +# Number of generations +# Probability that an attribute will be mutated +# Probability that the structure will be mutated +# Number of runs of environment +# Lower limit of float values +# Upper limit of float values +# Initial limit of levels +# Initial limit of columns +# Lower limit of levels +# Lower limit of columns +# Limit of error on which to terminate individual evaluation +# Probability for crossover +# Probability for mutating an individual +# Number of times the evaulation is run (with different random seeds) +# Type of errors collected +# Error function + +seed = 3 +pop_size = 100 +gens = 10 +attr_mut_pb = 1 +structurepb = 0.9 +runs = 1000 +lower_float = -1 +upper_float = 1 +min_levels_limit = 2 +min_columns_limit = 2 +max_levels_limit = 5 +max_columns_limit = 5 +early_termination = False +error_limit = None +p_crossover = 0.9 +p_mutation = 0.75 +nevals = 1 +error_collector_type = RewardError +error_response_type = SummedError + + +### Structure + +# modes - pattern of nodes at particular levels, zero, n, top and zerotop +# the mode numbers refer to: +# 0 - per:bin-ws, ref:flt-ws, com:sub, out:flt-ws + +mode = 5 +type1 = HPCTLEVEL.ZEROTOP^HPCTFUNCTION.REFERENCE^HPCTVARIABLE.TYPE^Float +type2 = HPCTLEVEL.ZEROTOP^HPCTFUNCTION.REFERENCE^HPCTVARIABLE.FUNCTION_CLASS^EAVariable +type3 = HPCTLEVEL.ZEROTOP^HPCTFUNCTION.REFERENCE^HPCTVARIABLE.PROPERTIES^{'lower_float': -1, 'upper_float': 1} +type4 = HPCTLEVEL.TOP^HPCTFUNCTION.REFERENCE^HPCTVARIABLE.TYPE^Float +type5 = HPCTLEVEL.TOP^HPCTFUNCTION.REFERENCE^HPCTVARIABLE.FUNCTION_CLASS^EAVariable +type6 = HPCTLEVEL.TOP^HPCTFUNCTION.REFERENCE^HPCTVARIABLE.PROPERTIES^{'lower_float': -1, 'upper_float': 1} + + + + + +### gen pop min mean max mut muts timing +### 0 100 -1329.244 -1329.244 -1329.244 +### 1 100 -1335.192 -776.890 -14.466 76% 49% 1.570 +### 2 100 -1356.544 -897.118 -14.466 74% 51% 0.765 +### 3 100 -1356.544 -865.212 -15.490 73% 50% 0.732 +### 4 100 -1356.544 -901.502 -16.525 73% 43% 0.657 +### 5 100 -1359.990 -1053.609 -14.466 80% 51% 0.810 +### 6 100 -1359.990 -1119.561 -14.466 73% 46% 0.925 +### 7 100 -1361.388 -956.733 -14.466 76% 52% 0.865 +### 8 100 -1361.388 -1052.702 -15.490 79% 60% 0.801 +### 9 100 -1361.388 -977.938 -16.525 76% 53% 0.810 +### 10 100 -1362.401 -890.665 -14.466 74% 53% 0.772 +# Best Score: -1362.40147 +# Best Ind: [[[['WindTurbine'], [1], [2], [4]], [[[0.3242848335373043], 0.5557635679321964, 3.112778680400676, 10.526381208137458]]], [[[[[0.5363283431225842, -0.3020876246108368], 0.37626808573352905]], [[[-1.558107094645247, -0.12006569048610073, 0.6039925484415403], 0.447644445087388]], [[[-0.11032634192252495], 0.4432600555050679]]]], [[[[[0.03342461926527114, 0.8716030856155681, 0.368760690878422], 0.6982397795546179]], [[[0.7145668630555149], 0.389132815098737]], [[[-0.14090033152754525], 0.9395346113510177]]], [[[[-0.817545889491226, -0.1794117608370509, -0.9152587743035161], 0.7802394727937287]], [[[-0.5594095205000766], 0.3396145514762595]], [[[0.1597689123312396], 0.5441298527436246]]]], [[[[[0.7794309545691012, 0.30655984453012763], 0.35100372423554105]], [[[-0.2608923910197565, 0.03241143536343023], 0.8316554600284902]], [[[0.47923235551870413], 0.8142967728955316]]], [[[[-0.4626689327121205, 0.923021022955252], 0.49446943756748596]], [[[-1.4141771552560118, -0.44722788263550833], 0.7329289542663316]], [[[1.5937222695846318], 0.5170907286001206]]], [[[[0.7319247445836443, 0.4565144821856246], 0.26519574223408493]], [[[0.22177756590119446, -0.028450834296690486], 0.8288556433078511]], [[[0.2560116707412241], 0.080754960161875]]]], [[[[-0.2629598007970895]], [[[0.014127194348241112, 0.25880838603740736, 0.9667309962355382], 0.9765002553969074]], [[[0.9413255437600909], 0.9600776044794335]]], [[[0.740559685914173]], [[[-0.5866590200441408, 0.13308212374784328, -0.4364789547389667], 0.9690583303072999]], [[[1.5815798270415076], 0.5223941213718245]]]]] +# Mean time: 0.871