Skip to content

Energy Systems Optimization🔗

Background🔗

When building a system model in Modelica, typical questions to answer are:

  • What are the best parameters for my system?
  • What is the best way to control the system?

A "parameter sweep" repeating the simulation with all possible parameter combinations may help you to find the optimal parameters. However, this may be time consuming and quickly becomes infeasible when more than 2-3 parameters are to be "swept". While the process of repeating simulations to find optimal parameters can be automated using standard algorithms (e.g. the Nelder-Mead algorithm), the feasibility is still limited to a handful of parameters due to exponential growth of possible combinations - and if you use this method to tune a controller, the resulting control strategy can only be as good as the implemented controller logic allows.

This is were Dynamic Optimization comes into play.

Purpose🔗

This article summarizes crucial concepts of dynamic optimization and separates it from dynamic simulation. In brief, you will learn

  • What is the main difference between dynamic simulation and dynamic optimization
  • About commonly used terminology and how this relates to Modelon Impact's dynamic optimization framework
  • Which component model enables easy drag-and-drop definition of complex optimization problems

1. Dynamic Simulation vs. Dynamic Optimization🔗

Both dynamic simulation and optimization make use of equation-based models. However, the way the model equations are being processed by the respective solver is very different.

Dynamic Simulation: In a dynamic simulation, the solver initializes with predefined initial conditions (typically at time t=0) and progresses in time. For each step, the equation system is solved numerically - i.e., all state and variable values are calculated within a defined tolerance. The time-step length can be variable or fixed, depending on the simulation settings. For variable step length solvers, the length depends on its current state and the dynamics of the modeled system. The control structure is usually part of the system model and can influence the dynamic behavior significantly. Below you see a conceptual animation of a dynamic simulation with different step lengths.

Definition of objective

Dynamic Optimization: In a dynamic optimization, the time horizon is discretized into segments of equal length and the equation system is solved for all time points simultaneously. Therefore, the solver needs complete trajectories of the variables to initialize the optimization problem. Unlike simulation, the solver does not step forward in time, but towards the optimal solution. It does so by varying the degrees of freedom and utilizing gradients to efficiently find the optimum. Degrees of freedom are typically component sizes and/or control signals at any time point considered. The solution will show optimal system performance, depending on the definition of "optimal", for example lowest emissions, highest cash flow, etc. Below you see a conceptual animation of a dynamic optimization.

Definition of objective

2. Modeling Concepts in the EnergySystems Library🔗

A key concept of Modelon's EnergySystems library (ESL) is making the advanced methodology of dynamic optimization available to non-experts. The most important elements of such an optimization problem are listed below. Afterwards, the corresponding elements in the library are briefly explained.

A dynamic optimization problem can not only include parameters as degrees of freedom but also control inputs that affect the system behavior. To define a dynamic optimization problem the following must be defined: + Dynamic model containing the equations that define the behavior of the system + Optimization objective (a function to be minimized or maximized) + An initialization for the optimization + Time horizon and sampling rate for discretization

Either or both of the following: + Degrees of freedom varying in time + Degrees of freedom constant in time (parameters)

Optionally + Inequality constraints + Boundary conditions

Important Blocks🔗

EnergySystems.ScenarioSetup.Optimizer This model contains the basic definition of the optimization problem (objective function, time horizon, and discretization) as well as settings for the optimization solver.

EnergySystems.ScenarioSetup.BoundaryCondition This model reads and stores boundary condition data for the optimization, e.g. energy demands or price signals. By storing the values, it allows to remove the data source during the optimization and thus reducing the size of the optimization problem.

EnergySystems.ScenarioSetup.OptimalControl This model should be used for optimal control inputs, i.e., control signals that are degrees of freedom and thus can be changed by the solver. The trajectories are initialized by a simulation, so the user needs to provide some external logic to its inputs, for example using a PI-controller. Similarly to the BoundaryCondition model, such logic should be removed during optimization.

EnergySystems.ScenarioSetup.Constraint This model should be used for defining inequality constraints. The limits can be constant or variable in time.

EnergySystems.ScenarioSetup.OptimalParameter This model should be used for defining parameters to be a Degree of Freedom (DoF).

EnergySystems.Internal.Economy.Economy This model gathers and sums economic values (capex, opex, cash flow, etc.) from all components in the system automatically. It also provides objective functions that can be used in the Optimizer.

Optimization Template and Usage🔗

To initialize the optimization, a simulation is needed to provide the starting "point" for the solver. Ideally, the starting point should be feasible and as close as possible to the optimum. It is possible to perform both the initial simulation and the optimization with the same system model. However, it should be ensured that elements that are only needed for the initial simulation are removed from the optimization.

EnergySystems.ScenarioSetup.EconomyOptimizationTemplate is a template to help users to set up an optimization problem. It includes the models Optimizer and Economy (described above) as well as a Boolean parameter initialSimulationPhase. This parameter allows to include elements only in the initial simulation phase (and not the optimization). This can be done by adding if initialSimulationPhase to an element in the text editor.

All examples in the EnergySystems library have a frame to illustrate the boundary between the simulation model and the optimization model. Setting the parameter initialSimulationPhase to False will gray out components that are only used for the initial simulation as shown in the image below. Exceptions include the ExcelTableReader (where this functionality is implemented in the model itself), the Economy, and the Optimizer, which are located outside the frame for convenience.

3. Advanced Terminology🔗

Some important terms for optimization problems are briefly explained below. These explanations are meant for advanced users, if you are new to EnergySystems library, we recommend getting hand-on experience by working through the tutorial in the article.

Objective Function: The objective function calculates the value that the optimization solver seeks to minimize (or maximize). Choosing a suitable objective function is a crucial part of defining the optimization problem and achieving meaningful results. Typical objective functions in Modelon's Energy System Library are a system's energy use (to be minimized) or cash flow (to be maximized). Using Modelon Impact, the objective function can be defined in the block EnergySystems.ScenarioSetup.Optimizer which is present when extending the model EnergySystems.ScenarioSetup.EconomyOptimizationTemplate.

Lagrange and Mayer Term: These are two terms that can define the optimization objective. When using a Lagrange term, the variable value is integrated by the solver over the entire time horizon (e.g. running costs) and the integrated value is minimized/maximized. When using a Mayer term, only one variable value at a specific time point (typically the end of the time horizon) is minimized/maximized (e.g. total costs). The image below illustrates a Mayer term (left) and a Lagrange term (right). Using Modelon Impact, terms to be used are defined in the model Optimizer which is part of the template EnergySystems.ScenarioSetup.EconomyOptimizationTemplate.

Gradient-based Optimization Solver: These solvers utilize algorithms that determine the search direction by analyzing the gradient of the objective function at the current point. This approach allows for efficient convergence towards the optimal solution. However, it also requires that information about the gradients is available, which in turn imposes requirements on the model equations.

Global and Local Optimum: A local optimum refers to a point where the objective function reaches a minimum (or maximum) within a specific region, while the global optimum represents the overall minimum (or maximum) of the objective function across the entire solution space. Usually, the goal is to find the global optimum. However, many solvers can only find local optima and it is therefore important to ensure good initialization which means close to the optimum.

Initialization: An optimization solver needs a starting "point" for the optimization. Providing this starting point is called initialization. A good initialization usually leads to faster convergence of the solver and is especially important when the solution space includes many local optima. For dynamic optimization, the starting point is a solution to the equation system over the entire time horizon. Using Modelon Impact, the initialization is automatically calculated by running a dynamic simulation of the system before starting the optimization process. This is an integral part of the optimization custom function available in the execution menu.

Constraint Handling: Constraints play a vital role in defining an optimization problem. They are used to reduce the solution space by imposing limits on variable values, e.g. operational constraints of components or emission limits. The way that constraints are handled can differ significantly between different solvers. In Modelon Impact, constraints are defined via instances of EnergySystems.ScenarioSetup.Constraint.

Feasible Solution: A feasible solution satisfies all constraints of the optimization problem. Some solvers evaluate infeasible solutions during their iterative search for the optimum, but the optimal solution must obviously be feasible to obtain meaningful results. If the optimization problem is not well defined, the solver may struggle to find any feasible solution at all.

Penalty Functions: Penalty functions are one approach to handling constraints in optimization problems. These functions penalize the objective function whenever a constraint is violated, encouraging the optimizer to prioritize feasible solutions.

Discretization: Dynamic optimization problems involving continuous variables and differential equations require discretization methods to transfer the continuous time problem into a discrete form suitable for numerical optimization algorithms. Common discretization methods include finite difference, finite element, and collocation methods.

Model Predictive Control (MPC): MPC is a control strategy that employs dynamic optimization techniques to solve control problems online. By continuously solving optimization problems over a finite time horizon, MPC adjusts control actions based on system dynamics and constraints, making it suitable for real-time applications.