PCTExamples class

Class to run hierarchy configurations.

PCTExamples Class

The PCTExamples class is designed to load a PCTHierarchy configuration file and provide various methods to interact with the hierarchy.


source

PCTExamples

 PCTExamples (config_file, min=True, early_termination=False,
              history=False, additional_props=None, render=False,
              video_params=None, suffixes=False)

PCTExamples class provides methods to load a PCT hierarchy from a configuration file, summarize the hierarchy, get the configuration, draw the hierarchy, run the hierarchy for a specified number of steps, and close the environment. Attributes: hierarchy (PCTHierarchy): The loaded PCT hierarchy. env (Environment): The environment associated with the PCT hierarchy.

Usage Examples for PCTExamples.run_example

Below are several usage examples demonstrating the new keyword-argument-based interface for run_example.

The class also provides model structure analysis capabilities:

  1. The get_model_details() method returns information about the model’s structure in JSON format, including total node count and parameter count.

  2. You can now use the get_model_details=True parameter in run_example() to include model statistics in the results without having to create a separate instance.

# Basic run with MountainCar config
config_file='testfiles/MountainCar/MountainCar-cdf7cc.properties'
result_basic = PCTExamples.run_example(config_file, steps=100)
result_basic
{'run_result': False, 'steps_completed': 100, 'success': True}
# Run with model details
result_with_details = PCTExamples.run_example(
    config_file=config_file,
    run_hierarchy=True,
    get_model_details=True,
    verbose=False
)
if 'model_details' in result_with_details:
    print("\nModel Statistics:")
    print(f"Total nodes: {result_with_details['model_details']['total_nodes']}")
    print(f"Total parameters: {result_with_details['model_details']['total_parameters']}")

Model Statistics:
Total nodes: 3
Total parameters: 13
# Full featured run
result_full = PCTExamples.run_example(
    config_file=config_file,
    run_hierarchy=True,
    render=True,
    early_termination=True,
    steps=5000,
    print_summary=False,
    return_config=True,
    verbose=False,
    get_model_details=True,
    # image_params={'figsize': (16, 10), 'with_labels': True},
    video_params={'fps': 30, 'filename': '/tmp/mountaincar_demo.mp4'},
    # plot_params={'plots_figsize': (14, 8), 'title_prefix': 'MountainCar_'}
)
result_full
IMAGEIO FFMPEG_WRITER WARNING: input image is not divisible by macro_block_size=16, resizing from (600, 400) to (608, 400) to ensure video compatibility with most codecs and players. To prevent resizing, make your input image divisible by the macro_block_size or set the macro_block_size to 1 (risking incompatibility).
IMAGEIO FFMPEG_WRITER WARNING: input image is not divisible by macro_block_size=16, resizing from (600, 400) to (608, 400) to ensure video compatibility with most codecs and players. To prevent resizing, make your input image divisible by the macro_block_size or set the macro_block_size to 1 (risking incompatibility).
Video saved as /tmp/mountaincar_demo.mp4
Video saved as /tmp/mountaincar_demo.mp4
{'config': {'type': 'PCTHierarchy',
  'name': 'pcthierarchy',
  'pre': {'pre0': {'type': 'MountainCarContinuousV0',
    'name': 'MountainCarContinuousV0',
    'value': 0,
    'links': {0: 'Action1'},
    'env_name': 'MountainCarContinuous-v0',
    'reward': 0,
    'done': False,
    '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': 'SmoothWeightedSum',
         'name': 'RL0C0',
         'value': -0.0,
         'links': {0: 'OL1C0'},
         'weights': [3.6717124436775688],
         'smooth_factor': 0.1712586159619749}},
       'percoll': {'0': {'type': 'SmoothWeightedSum',
         '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': 'SmoothWeightedSum',
         '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': 'SmoothWeightedSum',
         'name': 'RL0C1',
         'value': -0.0,
         'links': {0: 'OL1C0'},
         'weights': [2.244745806835832],
         'smooth_factor': 0.7897479531723087}},
       'percoll': {'0': {'type': 'SmoothWeightedSum',
         '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': 'SmoothWeightedSum',
         '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': 'Constant',
         'name': 'RL1C0',
         'value': 0.45,
         'links': {}}},
       'percoll': {'0': {'type': 'SmoothWeightedSum',
         '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': 'SmoothWeightedSum',
         'name': 'OL1C0',
         'value': -0.0,
         'links': {0: 'CL1C0'},
         'weights': [1.2912361488002317],
         'smooth_factor': 0.5399395563852809}}}}}}},
  'post': {'post0': {'type': 'WeightedSum',
    'name': 'Action1',
    'value': 0,
    'links': {0: 'OL0C0', 1: 'OL0C1'},
    'weights': [1, 1]}}},
 'model_details': {'total_nodes': 3, 'total_parameters': 13},
 'run_result': False,
 'steps_completed': 5000,
 'success': True}
# Just create image and get model details without running
result_image_and_details = PCTExamples.run_example(
    config_file=config_file,
    run_hierarchy=False,
    return_config=False,
    print_summary=False,
    get_model_details=True,
    image_params={'figsize': (12, 8), 'with_labels': True}
)
if 'model_details' in result_image_and_details:
    print("\nModel Details (without running):")
    print(f"Total nodes: {result_image_and_details['model_details']['total_nodes']}")
    print(f"Total parameters: {result_image_and_details['model_details']['total_parameters']}")

# Display usage help
PCTExamples.run_example(display_usage=True)

Model Details (without running):
Total nodes: 3
Total parameters: 13

            PCTExamples.run_example() Usage:
            
            Basic usage:
                PCTExamples.run_example('testfiles/MountainCar/MountainCar-cdf7cc1497ad143c0b04a3d9e72ab783.properties')
            
            With options:
                PCTExamples.run_example(
                    config_file='testfiles/MountainCar/MountainCar-cdf7cc1497ad143c0b04a3d9e72ab783.properties',
                    run_hierarchy=True,      # Run the hierarchy
                    render=True,             # Render environment
                    image_params={...},      # Create hierarchy diagram if not None
                    video_params={...},      # Create video if not None
                    plot_params={...},       # Create plots if not None
                    get_model_details=True,  # Include model statistics in results
                    early_termination=True,  # Enable early termination
                    steps=1000,              # Override step count
                    print_summary=True,      # Print hierarchy summary
                    return_config=True,      # Return configuration
                    verbose=True             # Verbose output
                )
            
            Returns dictionary with results and any requested outputs.
            
C:\Users\ruper\Versioning\python\nbdev\pct\pct\hierarchy.py:354: UserWarning:

This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.
{'usage_displayed': True}
# Example of using get_model_details to obtain model statistics

# Create a PCTExamples instance
filename = 'testfiles/MountainCar/MountainCar-cdf7cc.properties'
example = PCTExamples(config_file=filename)

# Get model details as a JSON object
model_details = example.get_model_details()
print("Model Details:")
print(json.dumps(model_details, indent=4))

# Access specific information
print(f"\nTotal nodes: {model_details['total_nodes']}")
print(f"Total parameters: {model_details['total_parameters']}")

# Clean up
example.close()
Model Details:
{
    "total_nodes": 3,
    "total_parameters": 13
}

Total nodes: 3
Total parameters: 13