Skip to main content

A library for Optimal Control and System Dynamics

Project description

OpenCtrl OpenCtrl is an open source library for Control System Dynamics and Optimal Control Alogrithms.

The library facilitates:

  • System Objects
  • Multi-Horizon Optimization
  • Control Algorithms
  • Real-time visualization

How to install

Run the flowing code using pip: pip install OpenCtrl

For specific version X use: pip install OpenCtrl==X

Quick Guide

Version Check: python -c "import OpenCtrl print(OpenCtrl.__version__)"

Create a Linear System

To create Linear System:

import numpy as np
from OpenCtrl.SystemDynamicExample import LinearSystem
''' define your input space parameters '''
input_space = { '1' : {'discrete' : [0,512]},
                '2' : {'random' : ['uniform',-128,127]},
                '3' : {'continuous' : [-10,10]},
                '4' : {'continuous' : [512,1024]},
              }
''' Instantiate Linear System Object '''
linear_sys = LinearSystem(sys_dim = 4,
                          input_dim = 4,
                          input_space = input_space,
                          disturbance_type = 'uniform',
                          disturbance_scale = [-255, 255],
                          disturbance_params = None
                          )
''' Info : sys_dim equals input_dim since we considered we can control 4 degrees of freedom, if the case is different like we have 1 degree of freedom then input_space can only have { '1' : {'discrete' : [0,512] } } only. For disturbance_params some degree of freedom can have no impact by disturbance if only 1 degree is impacted the disturbance_params will be 1. '''
u_o = np.array([np.random.randint(0,512), 
              np.random.uniform(-128,127), 
              np.random.uniform(-10,10), 
              np.random.uniform(512,1024)]
            )
linear_sys.step(u_o)
print(linear_sys.x)

Create an Optimizer

To create an optimizer object to optimize the system:

from OpenCtrl.optim import VanillaOptim
''' Let's create 3 different types of Optimizers '''
cost_func = 'quadratic'
horizon = 5
max_iteration = 50
tolerance_step = 10

def get_predictions(horizon):
    return [u_o for _ in range(horizon)]

def print_metrics(type,cost,u):
    print(f"For optimizer type {type} \nCost: {cost}\nOptimal Input : {u}")

''' Random Search '''
optimizer_random = VanillaOptim( system = linear_sys,
                                 horizon = horizon,
                                 cost_function = cost_func,
                                 optimizer_type = 'random',
                                 max_iterations = max_iteration,
                                 tolerance_step = tolerance_step
                                )
cost_random, u_random = optimizer_random.optimize( preds = get_predictions(horizon),
                                                    verbose = True
                                                  )
print_metrics('random',cost_random,u_random)     

''' Gradient Descent '''
optimizer_gradient = VanillaOptim( system = linear_sys,
                                   horizon = 5,
                                   cost_function = 'quadratic',
                                   optimizer_type = 'gradient',
                                   alpha = 1e-3,
                                   max_iterations = max_iteration,
                                   tolerance_step = tolerance_step
                                )
cost_gradient,u_gradient = optimizer_gradient.optimize(preds = get_predictions(horizon),
verbose = True )
print_metrics('gradient',cost_gradient,u_gradient)
''' Genetic Algorithm '''
optimizer_genetic = VanillaOptim( system = linear_sys,
                                  horiozn = horizon,
                                  cost_function = cost_func,
                                  optimizer_type = 'genetic',
                                  population_size = 1000,
                                  cross_over_rate = 0.42,
                                  mutation_rate = 0.23,
                                  cut_off_rate = 0.5,
                                  max_iterations = max_iteration,
                                  tolerance_step = tolerance_step
                                  )
cost_genetic,u_genetic = optimizer_genetic.optimize(pred = get_predictios(horizon),
verbose = True)
print_metrics('genetic',cost_genetic,u_genetic)

OptimPlot

Use Control Algorithm

To use a control algorith to tune the system:

from OpenCtrl.controls import LAC
control_horizon = 10
lac = LAC(system = linear_sys,
          optimizer = optimizer_gradient,
          horizon = horizon,
          nominal_disturbance = 'baseline'
          )
''' nominal_disturbance is a conventional method of predicting disturbance '''
for _ in range(control_horizon):
    preds = get_predictions(horizon)
    cost,u = lac.tune(preds = preds,
                      verbose = True,
                      base_line= _
                     )
    print_metrics('gradient',cost, u)

ControlMetrics

To Create Extension of Algos and System Dynamics

*** Developer Documentation in Progress ***

To Contribute

You can check out the dev repo of OpenCtrl: OpenCtrl

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openctrl-1.0.16.tar.gz (19.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

openctrl-1.0.16-py3-none-any.whl (23.3 kB view details)

Uploaded Python 3

File details

Details for the file openctrl-1.0.16.tar.gz.

File metadata

  • Download URL: openctrl-1.0.16.tar.gz
  • Upload date:
  • Size: 19.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for openctrl-1.0.16.tar.gz
Algorithm Hash digest
SHA256 01e0720091607f154d41e482a005d605b8b2f1043404ff6d714f73183b476ed0
MD5 14a5e310e01bc76de5599a0e184e7076
BLAKE2b-256 bc4ddd1b033024ded8e0c294837bf3cb784f39e48f78a0671914260c40f472a3

See more details on using hashes here.

File details

Details for the file openctrl-1.0.16-py3-none-any.whl.

File metadata

  • Download URL: openctrl-1.0.16-py3-none-any.whl
  • Upload date:
  • Size: 23.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for openctrl-1.0.16-py3-none-any.whl
Algorithm Hash digest
SHA256 a3a3f9d71c6281c01e082f64ed7b7b614430577fdfba436ad20c8e44c8713c4c
MD5 e358cd6c9691329927108672c5e2ea02
BLAKE2b-256 ebf2d7706ee2bcfbe75e423b0ff710269a8792b02382ba807be2fd7c6cad8d03

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page