Dynamic Optimization - Best Practice and Troubleshooting🔗
1. Introduction🔗
This article is the third in a series of articles related to dynamic optimization:
- Concepts and Terminology
- Tutorial: Energy System Optimization
- Best Practice and Troubleshooting
Many things need to be considered when setting up a dynamic optimization problem. This article gives guidance on best practice and helps with troubleshooting of commonly encountered problems.
2. Best Practice🔗
General advice for setting up an optimization problem:
- Think before you implement
- Start small and add complexity as you go along
- Test often and make sure you understand what is going on
- Use a version control system such as GIT or SVN
- Ask for help when you get stuck: support@modelon.com
More concrete advice when working with the ESL:
- Use components from the EnergySystems library (especially the
EnergySystems.ScenarioSetup.EconomyOptimizationTemplate
) - Conditionally remove parts that are only needed for initialization
- Use 0 and 1 as limits for optimal control inputs when connecting to a component's u_ctrl connector
- Note how some "physical" (electrical, fluid, heat) connectors have a black background. Every connector that does not have the black background must be connected to exactly one "black" connector to achieve a balanced model
- When using storages: if possible, start with an empty storage and/or set the state of charge to be equal at start time and end time of the optimization horizon. Otherwise, the results may be influenced by the "free" energy in the storage at the start of the optimization
- When using
EnergySystems.Storage.Electricity.BatteryDualControl
: Avoid charging and discharging at the same time
3. Troubleshooting🔗
Different issues can arise and lead to a "failed" result:
- Problems with the initial simulation
- The optimization does not start
- The optimization solver does not find a solution
The log will tell you at which stage the workflow failed. If not, increase the log level to (at least) "info".
3.1 Problems with the initial simulation🔗
Possible problems can be that the initial simulation does not start or that the simulation solver does not find a solution. Please see this article for general advice on debugging simulations.
Typical problems with the initial simulation specific to ESL are
- Storages (batteries, tanks, etc.) that run empty - either adjust starting levels or adjust control strategies of consumers and producers
- Chattering problems causing the simulation to stall - make sure that the controller's speed is adequate for the time horizon
3.2 The optimization does not start🔗
Check log for hints. Make sure the log level is appropriate (at least INFO). Look out for the State, Algebraic and Input variables and their limits to make sure the problem is set up as desired.
Note that it can take several minutes for the optimization to actually show progress, especially for long time horizons (Progress indicator shows 'Optimization 4/4' during that time). Be patient and wait for the optimization to actually start/fail.
3.3 The optimization solver does not find a solution🔗
When using only ESL components🔗
Optimizer settings
- Check that the optimization objective is well-defined in the optimizer
- Check that the samplingTime in the optimizer is reasonable
- Consider changing expert settings if you know their effect
Improve the initialization of optimalControl elements
Feasibility is not a requirement, but beneficial. This is especially true if the optimizer claims that an "Infeasible Problem" has been detected. Control the log to see if constraint violations have been observed during the initial simulation and try to avoid them.
Analyze the solver output from Ipopt
Ipopt is a widely used software to solve optimization problem. Please refer to the documentation of the ipopt project for details
When using/developing own models🔗
Check model smoothness
Gradient-based optimization solvers require that the optimization problem is C2-smooth, i.e. twice continuously differentiable. Note that not all Modelica constructs are supported (e.g. when
or while
keywords), mostly due to conceptually differences.
This also means that you should not use abs/min/max/sign functions, even though they will not produce an error. Instead, use the smooth approximations available in EnergySystems.Internal.Functions
. However, be aware that they may impede convergence, so use with care.
Avoid nonlinearities
For increased robustness, it is recommended to reduce the size of nonlinear systems as much as possible. See this article on diagnostics to find such systems.