PCT Examples Usage

Examples of PCT Examples usage.
The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload
from pct.pctexamples import PCTExamples

Wind Turbine

# Example of how to use the PCTExamples class
pct = PCTExamples('testfiles/WindTurbine/ga--1362.401-s003-4x3-m005-WT0538-bddf277b0f729cc630efacf91b9f494f.properties', history=True, additional_props={'keep_history':True})
pct.results(args={'comparisons':True, 'comparisons_print_plots':False}, environment_properties={'keep_history':True, 'range': 'test'})
# pct.summary()
# pct.run(100)
print('*** keys', pct.get_history_keys())
history_data = pct.set_history_data()
fig = pct.plot_single(plot={'plot_items': {'RL0C0sm': 'Ref', 'PL0C0sm': 'Perc'}, 'title': 'Level 0'}, history_data=history_data)
{'series': 'steady', 'zero_threshold': 1, 'reward_type': 'power', 'keep_history': True}
2025-01-07 20:04:53.939897 21956 Start
Score=-1362.401 power=1362.401
{'start': 1100, 'end': 2100, 'average yaw error_baseline_logs': 6.907099999999999, 'angle covered_trad_baseline_logs': 27.30000000000004, 'yaw count_trad_baseline_logs': 5, 'time_yawing_trad_baseline_logs': 0.43004300430043}
{'start': 1100, 'end': 2100, 'average yaw error_baseline_simu': 6.5231, 'angle covered_trad_baseline_simu': 53.46000000000012, 'yaw count_trad_baseline_simu': 15, 'time_yawing_trad_baseline_simu': 2.0502050205020503}
{'start_index': 1100, 'stop_index': 2100, 'power_trad': 1360.8065024307061, 'power_no_loss': 1394.8282594711568, 'power_control': 1362.4014711179545, 'average yaw error': 5.716155999999999, 'average reward': -1362.401471117955, 'angle covered': 33.0, 'yaw count': 5, 'time_yawing': 1.1011011011011012}
average_yaw_error_decrease_base=17.24
average_yaw_error_decrease_simu=12.37
energy_gain = 0.12
net_energy_gain = 0.14
2025-01-07 20:05:02.450897 21956 End
Elapsed time: 8.51
*** keys ['WindTurbine', 'reward', 'IYE', 'IWD', 'IWS', 'Action1sgsm', 'RL0C0sm', 'PL0C0sm', 'CL0C0', 'OL0C0sm', 'RL1C0sm', 'PL1C0sm', 'CL1C0', 'OL1C0sm', 'RL1C1sm', 'PL1C1sm', 'CL1C1', 'OL1C1sm', 'RL2C0sm', 'PL2C0sm', 'CL2C0', 'OL2C0sm', 'RL2C1sm', 'PL2C1sm', 'CL2C1', 'OL2C1sm', 'RL2C2sm', 'PL2C2sm', 'CL2C2', 'OL2C2sm', 'RL3C0v', 'PL3C0sm', 'CL3C0', 'OL3C0sm', 'RL3C1v', 'PL3C1sm', 'CL3C1', 'OL3C1sm']
pct.draw( figsize=(10,10), font_size=6, funcdata=True, with_edge_labels=True, node_size=200)

import os
if os.name == 'nt':
    from IPython.display import Image, display
    image_path = '/tmp/WindTurbine/test/steady/testfiles/WindTurbine/ga--1362.401-s003-4x3-m005-WT0538-bddf277b0f729cc630efacf91b9f494f/res-test.png'
    display(Image(filename=image_path))

Mountain Car

# Example of how to use the PCTExamples class
pct = PCTExamples('testfiles/MountainCar/ga-000.331-s032-2x2-m004-cdf7cc1497ad143c0b04a3d9e72ab783.properties', early_termination=True, history=True, render=True)
# pct.summary()
pct.run(steps=500)#, render=True)
False
pct.close()
print(pct.get_history_keys())
history_data = pct.set_history_data()
# pct.plot_history(plots='scEdges', title_prefix='')
fig = pct.plot_single(plot= {'plot_items': {'PL1C0': 'Actual Position', 'RL1C0': 'Target Position'}, 'title': 'L1C0'}, history_data=history_data)
['MountainCarContinuousV0', 'reward', 'IP', 'IV', 'Action1', 'RL0C0', 'PL0C0', 'CL0C0', 'OL0C0', 'RL0C1', 'PL0C1', 'CL0C1', 'OL0C1', 'RL1C0', 'PL1C0', 'CL1C0', 'OL1C0']
pct.draw( figsize=(10,10), font_size=6, funcdata=True, with_edge_labels=True, node_size=200)

print(pct.get_config_file_contents())
# Date 2023-07-01 19:05:33.641404
# Result
# Best individual
raw = grid: [2, 1]
env: [['MountainCarContinuousV0'], [0], [1]] act: [[1, 1]]
level0 
col: 0 ref: [[3.672], 0.171] per: [[1], 0.04] out: [[-0.678], 0.011]
col: 1 ref: [[2.245], 0.79] per: [[1], 0.236] out: [[1.76], 0.841]
level1 
col: 0 ref: [[0.45]] per: [[1], 0.764] out: [[1.291], 0.54]

config = {'type': 'Individual', 'name': 'pcthierarchy', 'pre': {'pre0': {'type': 'MountainCarContinuousV0', 'name': 'MountainCarContinuousV0', 'value': [0.0, 0.0, 0.0], 'links': {0: 'Action1'}, 'env_name': 'MountainCarContinuous-v0', 'reward': 0, 'done': True, 'info': {}}, 'pre1': {'type': 'IndexedParameter', 'name': 'IP', 'value': 0.0, 'links': {0: 'MountainCarContinuousV0'}, 'index': 0}, 'pre2': {'type': 'IndexedParameter', 'name': 'IV', 'value': 0.0, 'links': {0: 'MountainCarContinuousV0'}, 'index': 1}}, 'levels': {'level0': {'level': 0, 'nodes': {'col0': {'col': 0, 'node': {'type': 'PCTNode', 'name': 'L0C0', 'refcoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'RL0C0', 'value': -0.0, 'links': {0: 'OL1C0'}, 'weights': [3.6717124436775688], 'smooth_factor': 0.1712586159619749}}, 'percoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'PL0C0', 'value': 0.0, 'links': {0: 'IV'}, 'weights': [1], 'smooth_factor': 0.04005348677671297}}, 'comcoll': {'0': {'type': 'Subtract', 'name': 'CL0C0', 'value': -0.0, 'links': {0: 'RL0C0', 1: 'PL0C0'}}}, 'outcoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'OL0C0', 'value': 0.0, 'links': {0: 'CL0C0'}, 'weights': [-0.6781355729427617], 'smooth_factor': 0.010880609316351283}}}}, 'col1': {'col': 1, 'node': {'type': 'PCTNode', 'name': 'L0C1', 'refcoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'RL0C1', 'value': -0.0, 'links': {0: 'OL1C0'}, 'weights': [2.244745806835832], 'smooth_factor': 0.7897479531723087}}, 'percoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'PL0C1', 'value': 0.0, 'links': {0: 'IV'}, 'weights': [1], 'smooth_factor': 0.2361147455074499}}, 'comcoll': {'0': {'type': 'Subtract', 'name': 'CL0C1', 'value': -0.0, 'links': {0: 'RL0C1', 1: 'PL0C1'}}}, 'outcoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'OL0C1', 'value': -0.0, 'links': {0: 'CL0C1'}, 'weights': [1.7601945162568398], 'smooth_factor': 0.8412594680381309}}}}}}, 'level1': {'level': 1, 'nodes': {'col0': {'col': 0, 'node': {'type': 'PCTNode', 'name': 'L1C0', 'refcoll': {'0': {'type': 'EAConstant', 'name': 'RL1C0', 'value': 0.45, 'links': {}}}, 'percoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'PL1C0', 'value': 0.0, 'links': {0: 'IP'}, 'weights': [1], 'smooth_factor': 0.7641976478636343}}, 'comcoll': {'0': {'type': 'Subtract', 'name': 'CL1C0', 'value': -0.0, 'links': {0: 'RL1C0', 1: 'PL1C0'}}}, 'outcoll': {'0': {'type': 'EASmoothWeightedSum', 'name': 'OL1C0', 'value': -0.0, 'links': {0: 'CL1C0'}, 'weights': [1.2912361488002317], 'smooth_factor': 0.5399395563852809}}}}}}}, 'post': {'post0': {'type': 'EAWeightedSum', 'name': 'Action1', 'value': -0.0, 'links': {0: 'OL0C0', 1: 'OL0C1'}, 'weights': [1, 1]}}}
score = 0.33099
# Time  0.0584

### Description:

desc = MC08-ReferencedInputsError-RootMeanSquareError-Mode04

### 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 = MountainCarContinuousV0
env_inputs_indexes = [0, 1]
zerolevel_inputs_indexes = [1]
toplevel_inputs_indexes = [0]
references = [0.45]
num_actions = 1
env_inputs_names = [IP,IV]
environment_properties={}

### 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 = 32
pop_size = 100
gens = 10
attr_mut_pb = 1
structurepb = 0
runs = 500
lower_float = -1
upper_float = 1
max_levels_limit = 2
max_columns_limit = 2
early_termination = False
min_levels_limit = 2
min_columns_limit = 2
error_limit = 100
p_crossover = 0.9
p_mutation = 0.5
nevals = 1
error_collector_type = ReferencedInputsError
error_response_type = RootMeanSquareError

### Additional properties

property1 = error:referenced_inputs,0&0.45

### 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 = 4





# ###  gen  pop      min       mean        max   mut muts  timing
###    0  100    0.841      0.972      1.107 
###    1  100    0.625      0.969      1.218   51%   0%   0.106
###    2  100    0.524      0.932      1.219   55%   0%   0.051
###    3  100    0.520      0.841      1.220   49%   0%   0.055
###    4  100    0.520      0.795      1.220   49%   0%   0.051
###    5  100    0.398      0.785      1.219   53%   0%   0.054
###    6  100    0.342      0.770      1.218   46%   0%   0.054
###    7  100    0.342      0.746      1.218   48%   0%   0.051
###    8  100    0.331      0.706      1.218   53%   0%   0.054
###    9  100    0.331      0.731      1.218   56%   0%   0.054
###   10  100    0.331      0.677      1.219   53%   0%   0.053
# Best Score: 0.33099
# Best Ind: [[[['MountainCarContinuousV0'], [0], [1]], [[1, 1]]], [[[[[3.6717124436775688], 0.1712586159619749]], [[[1], 0.04005348677671297]], [[[-0.6781355729427617], 0.010880609316351283]]], [[[[2.244745806835832], 0.7897479531723087]], [[[1], 0.2361147455074499]], [[[1.7601945162568398], 0.8412594680381309]]]], [[[[0.45]], [[[1], 0.7641976478636343]], [[[1.2912361488002317], 0.5399395563852809]]]]]
# Mean time:  0.058