Git Product home page Git Product logo

nurkanovic / nosnoc Goto Github PK

View Code? Open in Web Editor NEW
49.0 5.0 10.0 89.66 MB

nosnoc is an open source software package for NOnSmooth Numerical Optimal Control.

Home Page: https://nosnoc.readthedocs.io/en/latest/index.html

License: BSD 2-Clause "Simplified" License

MATLAB 100.00%
optimization hybrid-systems nonsmooth-dynamical-systems nonsmooth-mechanics nonsmooth-optimization numerical-methods optimal-control simulation switched-systems

nosnoc's Introduction

nosnoc

nosnoc is an open-source MATLAB software package for NOnSmooth Numerical Optimal Control. The Python package nosnoc_py with similar functionality is available as well.

General

nosnoc is a tool for numerically solving optimal control problems with nonsmooth dynamical systems that have switches and/or state jumps. It supports:

  1. Automatic discretization via the FESD method:

    • High accuracy and correct sensitivities.
    • Classical time-stepping methods only have first-order accuracy and incorrect sensitivities, even when they appear to be differentiable.
  2. Automatic reformulations of systems with state jumps:

    • For example, contact problems via time-freezing into Filippov systems/Piecewise Smooth Systems (PSS).
    • This enables high accuracy even for systems with state jumps.
  3. Solving nonsmooth nonlinear programs via homotopy methods:

    • Supports numerous relaxation-based algorithms for solving mathematical programs with complementarity constraints (MPCCs).
    • Enables the use of off-the-shelf solvers like IPOPT and SNOPT.

With nosnoc, one can solve optimal control problems subject to several classes of hybrid and nonsmooth systems:

  • Switched systems
  • Piecewise affine, piecewise smooth systems
  • Filippov systems
  • Systems with logical Heaviside step functions
  • Relay systems
  • Projected dynamical systems
  • First-order Sweeping processes
  • Rigid body models with impacts and friction
  • Hybrid systems with hysteresis

Users may directly provide a DCS or the problem in a standard form which is automatically reformulated into a DCS. nosnoc discretizes a Dynamic Complementarity System (DCS) with the FESD method and solves the resulting MPCCs.

Installation

nosnoc requires CasADi version 3.5.5 and Matlab version R2021b or later.

Installation for MATLAB

  1. Install CasADi and make sure it is added to your MATLAB path. For CasADi installation instructions follow visit: https://web.casadi.org/get/

  2. Clone this repository

git clone --recursive https://github.com/nurkanovic/nosnoc.git
  1. Open the nosnoc folder in Matlab and run the install_nosnoc script
>> install_nosnoc

Note that IPOPT is shipped with CasADi. More information including detailed documentation can be found on its homepage

Installation for python

Go to the nosnoc_py repository for more info.

Using nosnoc

The interface of nosnoc is based on the symbolic modeling framework CasADi. User inputs should be provided as CasADi expressions.

To get started, we recommend you look into our example libraries for:

Literature - theory and algorithms

FESD

Finite Elements with Switch Detection for Direct Optimal Control of Nonsmooth Systems
A.Nurkanović, M. Sperl, S. Albrecht, M. Diehl
Numerische Mathematik (2024): 1-48

@article{Nurkanovic2024,
  title={Finite elements with switch detection for direct optimal control of nonsmooth systems},
  author={Nurkanovi{\'c}, Armin and Sperl, Mario and Albrecht, Sebastian and Diehl, Moritz},
  journal={Numerische Mathematik},
  pages={1--48},
  year={2024},
  publisher={Springer}
}

Finite Elements with Switch Detection for numerical optimal control of nonsmooth dynamical systems with set-valued heaviside step functions
A. Nurkanović, A. Pozharskiy, J. Frey, M. Diehl
Nonlinear Analysis: Hybrid Systems 54, 101518

@article{Nurkanovic2024a,
  title={Finite Elements with Switch Detection for numerical optimal control of nonsmooth dynamical systems with set-valued heaviside step functions},
  author={Nurkanovi{\'c}, Armin and Pozharskiy, Anton and Frey, Jonathan and Diehl, Moritz},
  journal={Nonlinear Analysis: Hybrid Systems},
  volume={54},
  pages={101518},
  year={2024},
  publisher={Elsevier}
}

Time-Freezing

A Time-Freezing Approach for Numerical Optimal Control of Nonsmooth Differential Equations with State Jumps
A. Nurkanović, T. Sartor, S. Albrecht, M. Diehl
IEEE Control Systems Letters 2021

The Time-Freezing Reformulation for Numerical Optimal Control of Complementarity Lagrangian Systems with State Jumps
A. Nurkanović, S. Albrecht, B. Brogliato, M. Diehl
Automatica 158 (2023): 111295.

Continuous Optimization for Control of Hybrid Systems with Hysteresis via Time-Freezing
A.Nurkanović , M. Diehl
IEEE Control Systems Letters 2022

Literature - Software

nosnoc

nosnoc: A Software Package for Numerical Optimal Control of Nonsmooth Systems
A.Nurkanović , M. Diehl
IEEE Control Systems Letters 2022

@article{Nurkanovic2022,
  title={nosnoc: A software package for numerical optimal control of nonsmooth systems},
  author={Nurkanovi{\'c}, Armin and Diehl, Moritz},
  journal={IEEE Control Systems Letters},
  volume={6},
  pages={3110--3115},
  year={2022},
  publisher={IEEE}
}

CasADi

CasADi - A software framework for nonlinear optimization and optimal control
J.A.E. Andersson, J. Gillis, G. Horn, J.B. Rawlings, M. Diehl
Mathematical Programming Computation, 2019

IPOPT

On the implementation of an interior-point filter line-search algorithm for large-scale nonlinear programming
A. Wächter, L. T. Biegler Mathematical programming, 2006

Contact

If you have questions, remarks, or comments, you are strongly encouraged to report them by creating a new issue on this Github page.

Feel free to contact one of the main developers directly: Armin Nurkanović ([email protected]), Anton Pozharskiy ([email protected]), Jonathan Frey ([email protected]).

Success stories and source code contributions are very welcome.

nosnoc's People

Contributors

apozharski avatar freyjo avatar nurkanovic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

nosnoc's Issues

Add option `relax_terminal_physical_time` for time_freezing problems

We have this for the plain numerical time, to make an ell_1 relaxation of the terminal time constraint.
It would be nice to have the same also for physical time constraints (maybe even also for the stagewise constraints - but to be discussed).

I want also to test with this the ease of use of vdx, once we migrated to it.

Can nosnoc determine the state transition borders (gap functions)?

Hi,
Thanks for the nice tool and quite interesting research papers.

I am not sure how to formulate one hybrid dynamics problem in order to solve it with Nosnoc. In simple terms, it is as follows:

master; % integer, influences the speed capabilities by switching the profiles 
u; % continuous control

if master==0
    f = profile_0; % define dynamics profile 0
elseif master==1
    f = profile_1; % define dynamics profile 1 
end

% goal: achieve *maximum speed*. 
% Determine best `u` (continuous control) and `master` (profile sequence e.g. profile_0, profile_1, profile_0)

I have checked examples, but I start to think you might not be dealing with these cases. I saw that you are defining the gap function. However, in this case the gap function should be kinda determined by the algorithm and it might be changing in time as well.

Would really appreciate feedback.

Best,

Dynamic nonmonotone homotopy in mpccsol for more robustness

If a problem gets infeasible, we can increase the sigma value once or twice to see will the homotopy loop recover.
This should be an option called, e.g., nonmonotone_homotopy_parameter, and we need a "kappa_2" for how much we update it.

If problem fails: sigma_k = kappa_2*sigma_k, % kappa2 >1

Add lifted f_v in time freezing

In CLS, we have the option to lift the velocity state for numerical inversion of M. Add analogous functionality to the cls time freezing reformulations.

I believe this is only easy to do for the multicontact reformulation, but this needs more thought.

Translate existing examples into new design

Most examples use the old nosnoc. As new design is coming in, we should translate all examples and update the comments such that they match the new features.
For example, tutorial examples should be more timely (and maybe have only two files - one ocp and one simulation, or one per major feature: stewart/heaviseide, pds, time freezing, cls)

nosnoc mpc class

As we will have a new integration and OCP class after the refactoring, it would be nice to add afterwards an mpc loop that solves a sequence of OCPs.

This will also have some options, e.g. how to initialize? To use an early homotopy iterate or the solution of the previous step?
For having the new x0, use the same integrator (e..g., just shift ocp solution) or simulate with a different nosnoc integrator (e.g. higher order, more intermediate FEs).

Create a new README

New README should contain a clearer explanation of the steps that NOSNOC uses to solve an OCP/simulate a system.

Clean up and document results

Currently the results struct has lots of noise in it with possibly empty entries. Remove these and document what we store in the results.

This may be irrelevant if we use vdx.

Support for MPVCs via mpccsol

By introducing a slack variable, MPVCs are equivalent to MPCCs. This can always be done manually, but it would be maybe useful to automate this step if the user indicates that the given problem is a mpvc (or we automatically recognize it by looking at the bounds of G and H) and transform into an mpcc.
maybe it makes sense to rename everything that is mpcc into mpec.

Relaxed and slacked constraints in nosnoc

Currently, we support ell_1 relaxation of some terminal constraints

  1. the terminal constraints of the ocp
  2. some of the FESD/time-freezing terminal constraints to meet a certain final numerical/physical time.

I would still steer the relaxation of these constraints with appropriate separate options, as we have them now.

In addition to this, it would be nice to be able to relax other constraints (e.g. using vdx internally to define according variables and constraints). In particular it would be nice to relax:

  1. Path constraints
  2. Dynamics constraints (maybe to separate even between the differential and algebraic part of the dynamics)

Choices for relaxing:

  1. ell 2 (every constraint gets a separate slack), maybe we can have upper bounds on slacks to avoid unbounded problems
  2. ell1 (every constraint gets a separate slack)
  3. ell_inf (every constraint group gets one slack)

Choice for penalty parameter:

  1. fixed value (separate parameter for every constraint group, by groups i mean: path, dynamics, terminal, terminal_algoritmic (nice and instructive names are a plus))
  2. use rho = 1/sigma - we should decide if we can mix fixed values and homotopy penalities. one one hand it gives a lot of flexibility, on the other hand too many choices might be confusing.

Simplify index tracking

Currently indexes are tracked independently at different levels and must be handled somewhat manually.
Several things need to be done to make maintenance of NOSNOC easier:

  • Remove FiniteElement and ControlStage classes
  • Use a smarter index tracking utility to avoid pre-definition, etc. Something similar to CasADi "structures" in casadi.tools

Clock state for integration grid and block band structure in FESD

Currently, we have for every FE an h.
Alternatively, we can define a clock state t, and have a "control variable" h_n to determine the length of a finite element.
We simply integrate the clock state as t_{n+1} = t_n + h_n; and enforce a "terminal" constraint
t_N = T_final instead of sum_n h_n = T_final
The intermediate variables t_n help eliminate sum_n h_n = T_final, which kills the block banded structure.

What does the `plugin` mean in `nosnoc.solver.mpccsol` and how should it be passed.

The current interface for mpccsol is designed to be similar to nlpsol i.e. mpccsol(solver_name, plugin, problem, options). In its current state we only support nosnoc.solver.MpccSolver as a backend for mpccsol which is a relaxation solver for MPCCs. This means plugin is used to pass the relaxation method to mpccsol. This is in my opinion problematic as it both makes changing the relaxation mode a bit unintuitive and will become more confusing as other solver backends are added.

We have as I see it several options:

  1. Continue as is but then add other plugins.
  2. Drop the plugin as a part of the interface and simply pass it as part of the options.
  3. Change the plugin name for the current MpccSolver backend to something like relaxation (name open to change) and have the relaxation mode as part of the solver options.

I view 3 as the best option as it is relatively ergonomic and makes it clear which solver backend is being used. 2 is also passable for me though it moves away from the nlpsol interface which may be undesirable in terms of user ergonomics.

The main_throwing_ball.m example is not working, the setting is wrong?

clear all
clc
close all
import casadi.*
%% model parameters
e = 0.2; u_max = 9; beta = 0.0; mu=0;
%% NOSNOC settings
problem_options = NosnocProblemOptions();
solver_options = NosnocSolverOptions(); %% Optionally call this function to have an overview of all options.

problem_options.n_s = 3;
solver_options.mpcc_mode = 'Scholtes_ineq';
solver_options.homotopy_update_rule = 'superlinear';
% problem_options.step_equilibration = 'direct_homotopy';
problem_options.use_fesd = 1;
problem_options.time_freezing = 1;

problem_options.local_speed_of_time_variable = 1;
problem_options.stagewise_clock_constraint = 0;
problem_options.nonsmooth_switching_fun = 0;
problem_options.pss_lift_step_functions = 0;

problem_options.no_initial_impacts = 0;
% solver
solver_options.opts_casadi_nlp.ipopt.max_iter = 1e5;
solver_options.opts_casadi_nlp.ipopt.tol = 1e-7;
solver_options.opts_casadi_nlp.ipopt.acceptable_tol = 1e-5;
solver_options.use_previous_solution_as_initial_guess = 0;
solver_options.N_homotopy = 6;
solver_options.print_level = 3;
solver_options.comp_tol = 1e-6;
solver_options.elastic_scholtes = 1;
%% model parameters
g=9.81;
q_target = [4;0.5];

%% solver
model = NosnocModel();
model.mu_f = mu;

problem_options.T = 4;
problem_options.N_stages = 20;
problem_options.N_finite_elements = 3;
% model equations
q = SX.sym('q',2);
v = SX.sym('v',2);
u = SX.sym('u',2);

model.lbx = [0;-0.001;0;-inf]; model.ubx = [4;inf;inf;inf];
model.x0 = [0;0.5;0;0];
model.x = [q;v]; model.u = u; model.e = e ;
model.f_c = q(2)-0;
model.f_v = [u-[0;g]-betavsqrt(v(1)^2^2+v(2)^2+1e-3)];
model.J_tangent = [1; 0];
model.dims.n_dim_contact = 2;

% Objective and constraints
model.f_q = u'u; model.f_q_T = 100v'*v;
model.g_path = u'*u-u_max^2;
model.g_terminal = q-[q_target];

mpcc = NosnocMPCC(problem_options, model);
solver = NosnocSolver(mpcc, solver_options);
[results,stats] = solver.solve();

Rework step equilibration options.

Rework how we slice the step equilibration options. Particularly with the introduction of relaxation mode support in vdx we can perhaps build the step equilibration out of several building blocks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.