Optimization Modeling Languages

Main.ModelingLanguages History

Show minor edits - Show changes to output

Changed line 1 from:
(:title Dynamic Optimization Basics:)
to:
(:title Optimization Modeling Languages:)
Changed lines 9-13 from:
The objective of this section is to introduce MATLAB and Python. This is not intended to be a general tutorial but more specifically to cover topics that are relevant for dynamic optimization. Topics of interest for dynamic optimization are listed below.

!!! Introduction to Programming Languages

Many programming languages can be used in this course with [[https://youtu.be/t84YMw8p34w | examples
in both MATLAB and Python programming languages]]. There are many resources to [[https://www.mathworks.com/help/matlab/getting-started-with-matlab.html | get started in MATLAB]] or [[https://www.python.org/about/gettingstarted/ | get started with Python]]. There are also excellent resources for experienced MATLAB programmers who would like to learn Python such as [[https://wiki.scipy.org/NumPy_for_Matlab_Users | Numpy for Matlab Users]]. Introductory material on programming with MATLAB or Python is in the [[https://apmonitor.com/che263|Computational Tools Course]].
to:
Here is a list of some of the most popular optimization packages in Matlab:

* fmincon: This is
a built-in function in Matlab that implements a variety of optimization algorithms for constrained nonlinear optimization.
* fminunc: This is another built-in function in Matlab that implements a variety of optimization algorithms for unconstrained nonlinear optimization.
* linprog: This is a built-in function
in Matlab that implements linear programming algorithms for solving optimization problems with linear constraints.
* quadprog: This is a built-in function in Matlab that implements quadratic programming algorithms for solving optimization problems with quadratic objective functions and linear constraints.
* Global Optimization Toolbox: This is a toolbox in
Matlab that provides a variety of algorithms for global optimization, including genetic algorithms, particle swarm optimization, and simulated annealing.

Here is a list of some of the most popular optimization packages in Python:

* scipy.optimize: This is a module in the SciPy library that provides a variety of optimization algorithms for solving nonlinear optimization problems.
* cvxpy: This is a package that provides a modeling language for convex optimization problems, and it has interfaces to several solvers including CVXOPT, ECOS, and SCS.
* gekko: This is a package that provides a modeling language for linear, nonlinear, and mixed integer programming problems, and it has interfaces to several solvers including IPOPT, APOPT, and BPOPT.
* pygmo: This is a package that provides a variety of optimization algorithms for global optimization, including genetic algorithms, particle swarm optimization, and simulated annealing.
* PyOpt: This is a package that provides a variety of optimization algorithms for solving nonlinear optimization problems, including gradient-based and derivative-free methods.

The objective of this section is to introduce optimization in MATLAB and Python. This is not intended to be a general tutorial but more specifically to cover topics that are relevant for dynamic optimization. Topics of interest for dynamic optimization are listed below
.
Changed lines 5-7 from:
Two tools used in this course are MATLAB and Python.  APMonitor is used as a toolbox in MATLAB or as a package within Python to provide support for solution of dynamic simulation, estimation, and control.

There are many tools for simulation and optimization of dynamic systems. There are also many factors to consider when selecting the appropriate tool
for a particular task. Some of the considerations for a dynamic optimization tool are size (scaling with number equations and degrees of freedom), speed (use in real-time estimation or control), usability (training requirements and ease of adaptation), extensibility (ability to modify for new or complex systems), support (commercial sustainment or open-source community), and portability (ability to embed on a system or communicate with a particular platform).
to:
Two tools used in this course are MATLAB and Python. [[https://gekko.readthedocs.io/en/latest/|Python Gekko]] is a package within Python to provide support for solution of dynamic simulation, estimation, and control.

There are many tools for
simulation and optimization of dynamic systems. There are also many factors to consider when selecting the appropriate tool for a particular task. Some of the considerations for a dynamic optimization tool are size (scaling with number equations and degrees of freedom), speed (use in real-time estimation or control), usability (training requirements and ease of adaptation), extensibility (ability to modify for new or complex systems), support (commercial sustainability or open-source community), and portability (ability to embed on a system or communicate with a particular platform).
June 23, 2020, at 08:11 PM by 136.36.211.159 -
Added lines 26-27:

[[https://gekko.readthedocs.io/en/latest/|Gekko Documentation]]
March 19, 2019, at 02:31 PM by 10.35.117.63 -
Changed lines 23-41 from:
!!! APMonitor Commands
to:
!!! GEKKO (Python)

GEKKO Python is designed for large-scale optimization and accesses solvers of constrained, unconstrained, continuous, and discrete problems. Problems in linear programming, quadratic programming, integer programming, nonlinear optimization, systems of dynamic nonlinear equations, and multi-objective optimization can be solved. The platform can find optimal solutions, perform tradeoff analyses, balance multiple design alternatives, and incorporate optimization methods into external modeling and analysis software. It is free for academic and commercial use under the MIT license.

%width=300px%Attach:gekko.png

The GEKKO package is available through the package manager '''pip''' in Python.

(:source lang=python:)
python -m pip install gekko
(:sourceend:)

[[https://apmonitor.com/wiki/index.php/Main/GekkoPythonOptimization|View Source for 18 GEKKO Example Solutions]]

(:html:)
<iframe width="560" height="315" src="https://www.youtube.com/embed/videoseries?list=PLLBUgWXdTBDjxcpH9hRuq-bsm_ti2UvoB&amp;hl=en_US" frameborder="0" allowfullscreen></iframe>
(:htmlend:)

!!! APMonitor (MATLAB and Python)
Changed lines 77-78 from:
# specify MV / CV
to:
# specify FV, MV, SV, CV
apm_info(s,a,'FV','K')
Added line 80:
apm_info(s,a,'SV','x')
Changed lines 95-96 from:
to:
apm_option(s,a,'u.dcost',0.1)
Added lines 108-110:

# retrieve solution
z = apm_sol(s,a)
Changed line 59 from:
Attach:download.png [[Attach:model_predictive_control_intro.zip|Model Predictive Control Example]]
to:
Attach:download.png [[Attach:model_predictive_control_intro.zip|Model Predictive Control Example (MATLAB and Python)]]
Changed line 59 from:
Attach:download.png [[Attach:model_predictive_control.zip|Model Predictive Control Example]]
to:
Attach:download.png [[Attach:model_predictive_control_intro.zip|Model Predictive Control Example]]
Added lines 58-59:

Attach:download.png [[Attach:model_predictive_control.zip|Model Predictive Control Example]]
Changed line 41 from:
** [[Main/INFO|More information about FV, MV, SV, and CV types]]
to:
** [[https://apmonitor.com/wiki/index.php/Main/INFO|More information about FV, MV, SV, and CV types]]
Changed lines 60-61 from:
# import apm.py, available from https://apmonitor.com/wiki/index.php/Main/PythonApp
to:
# import apm.py, available from 
https://apmonitor.com/wiki/index.php/Main/PythonApp
Changed line 27 from:
 server = 'https://127.0.0.1'        # for locally installed server
to:
 server = 'https://127.0.0.1'        # for local server
Changed line 65 from:
a = 'tuning'
to:
a = 'myApp'
Changed lines 17-18 from:
This course uses principles from nonlinear programming but does not delve into the details of solution strategies. Instead of detailing how a nonlinear programming solver iterates to find a solution, this course focuses on formulating problems and the use of differential constraints in describing and optimizing dynamic systems. For more background on how nonlinear programming solvers work, refer to the [[https://apmonitor.com/me575|Course on Optimization]] and the associated online [[https://apmonitor.com/me575/index.php/Main/BookChapters|Design Optimization Textbook]].
to:
This course uses principles from nonlinear programming but does not delve into the details of solution strategies. Instead of detailing how a nonlinear programming solver iterates to find a solution, this course focuses on formulating problems and the use of differential constraints in describing and optimizing dynamic systems. For more background on how nonlinear programming solvers work, refer to the [[https://apmonitor.com/me575|Course on Optimization]] and the associated online [[https://apmonitor.com/me575/index.php/Main/BookChapters|Design Optimization Textbook]]. A good illustrative example is the [[https://apmonitor.com/me575/index.php/Main/BookChapters|Two Bar Truss]] optimization or the example below.
Changed lines 23-37 from:
Contour plots are a powerful tool to visualize a feasible region (all potential solutions) and where the optimal solution is located relative to binding constraints. A [[https://apmonitor.com/me575/index.php/Main/TwoBarTruss | two-bar truss optimization problem]] is solved with MATLAB and Python and visualized with contour plots that show binding constraints, objective values, and trade-offs between the design variables. While this problem is a static design optimization problem (no time-varation), the same methods can be applied to dynamic optimization.

(:html:)
<iframe width="560" height="315" src="https://www.youtube.com/embed/uOTdLfvgYHU?rel=0" frameborder="0" allowfullscreen></iframe>
(:htmlend:)

(:html:)
<iframe width="560" height="315" src="https://www.youtube.com/embed/ah-Cbrim93I?rel=0" frameborder="0" allowfullscreen></iframe>
(:htmlend:)

A second example is a Linear Programming (LP) refinery problem where the balance between two crude supplies is optimized. This example shows a contour plot as the solver progresses towards an optimal solution.

(:html:)
<iframe width="560" height="315" src="https://www.youtube.com/embed/M_mpRrGKKMo?rel=0" frameborder="0" allowfullscreen></iframe>
(:htmlend:)
to:
!!! APMonitor Commands

APMonitor is a tool for nonlinear optimization and dynamic optimization. Source files are available for [[https://apmonitor.com/wiki/index.php/Main/JuliaOpt|Julia]], [[https://apmonitor.com/wiki/index.php/Main/PythonApp|Python]], and [[https://apmonitor.com/wiki/index.php/Main/MATLAB|MATLAB]]. Users can select either a public server or a local host if the [[https://apmonitor.com/wiki/index.php/Main/APMonitorServer|APMonitor Server]] is installed locally. The user must also specify an application name.

 server = 'https://127.0.0.1'        # for locally installed server
 server = 'https://byu.apmonitor.com' # for public server
 app = myApp                        # application name

Once the server and application name are defined, the following is a list of available commands in each package.

* apm(server,app,aline) - Send commands to the server
** Typical commands are 'clear all', 'clear csv', 'clear apm', 'solve'.
** 'clear all': Clear the prior application
** 'clear csv': Clear just the data file (CSV)
** 'clear apm': Clear just the model file (APM)
** 'solve': Solve the problem
* apm_get(server,app,filename) - Retrieve a file from the server
* apm_info(server,app,type,aline) - Classify a parameter or variable]]
** [[Main/INFO|More information about FV, MV, SV, and CV types]]
** Type FV: Fixed value, one decision or fixed value over the time horizon
** Type MV: Manipulated variable, multiple decision values over the horizon
** Type SV: State variable, view in web-viewer but don't control
** Type CV: Controlled variable with target variable
* apm_ip(server) - Retrieve the client IP address
** Example: ip = apm_ip('https://apmonitor.com')
* apm_load(server,app,filename) - Load model file
* apm_meas(server,app,name,value) - Specify a measurement
* apm_option(server,app,name,value) - Set an option
* apm_sol(server,app) - Retrieve the solution
* apm_tag(server,app,name) - Retrieve an NLC, FV, MV, SV, or CV option
* apm_web(server,app) - Open a web interface to the dashboard
* apm_web_var(server,app) - Open a web interface to the variable values
* csv_load(server,app,filename) - Load data file

An example script is below. The same commands are used in Python, MATLAB, and Julia with minor modifications such as the ';' in MATLAB to suppress console output or change in the comment characters.

(:source lang=python:)
# import apm.py, available from https://apmonitor.com/wiki/index.php/Main/PythonApp
from apm import *
# for MATLAB use: addpath('apm')

s = 'https://byu.apmonitor.com'
a = 'tuning'

# clear prior application
apm(s,a,'clear all')

# load model and data
apm_load(s,a,'model.apm')
csv_load(s,a,'data.csv')

# specify MV / CV
apm_info(s,a,'MV','u')
apm_info(s,a,'CV','y')

# configuration parameters
apm_option(s,a,'nlc.imode',6)
apm_option(s,a,'nlc.nodes',3)

# turn on MV as a degree of freedom
apm_option(s,a,'u.status',1)
# turn on CV to add terms to objective function
apm_option(s,a,'y.status',1)

# tune MV in the controller
apm_option(s,a,'u.lower',0)
apm_option(s,a,'u.upper',100)

# tune CV in the controller
apm_option(s,a,'y.tau',5)
apm_option(s,a,'y.sphi',26)
apm_option(s,a,'y.splo',24)

# solve and retrieve results
output = apm(s,a,'solve')
print(output)

# open web-viewer
apm_web(s,a)
(:sourceend:)
Changed lines 13-14 from:
Many programming languages can be used in this course with [[https://youtu.be/t84YMw8p34w | examples in both MATLAB and Python programming languages]]. There are many resources to [[https://www.mathworks.com/help/matlab/getting-started-with-matlab.html | get started in MATLAB]] or [[https://www.python.org/about/gettingstarted/ | get started with Python]]. There are also excellent resources for experienced MATLAB programmers who would like to learn Python such as [[https://wiki.scipy.org/NumPy_for_Matlab_Users | Numpy for Matlab Users]]. The following videos give a brief introduction to MATLAB and Python including how to create a first program and debug the application by stepping through each line.
to:
Many programming languages can be used in this course with [[https://youtu.be/t84YMw8p34w | examples in both MATLAB and Python programming languages]]. There are many resources to [[https://www.mathworks.com/help/matlab/getting-started-with-matlab.html | get started in MATLAB]] or [[https://www.python.org/about/gettingstarted/ | get started with Python]]. There are also excellent resources for experienced MATLAB programmers who would like to learn Python such as [[https://wiki.scipy.org/NumPy_for_Matlab_Users | Numpy for Matlab Users]]. Introductory material on programming with MATLAB or Python is in the [[https://apmonitor.com/che263|Computational Tools Course]].
Changed line 17 from:
This course uses principles from nonlinear programming but does not delve into the details of solution strategies. Instead of detailing how a nonlinear programming solver iterates to find a solution, this course focuses on formulating problems and the use of differential constraints in describing and optimizing dynamic systems. For more background on how nonlinear programming solvers work, please see the [[ https://apmonitor.com/me575 | Course on Optimization]].
to:
This course uses principles from nonlinear programming but does not delve into the details of solution strategies. Instead of detailing how a nonlinear programming solver iterates to find a solution, this course focuses on formulating problems and the use of differential constraints in describing and optimizing dynamic systems. For more background on how nonlinear programming solvers work, refer to the [[https://apmonitor.com/me575|Course on Optimization]] and the associated online [[https://apmonitor.com/me575/index.php/Main/BookChapters|Design Optimization Textbook]].
Changed lines 17-18 from:
This course uses principles from nonlinear programming but does not delve into the details of solution strategies. Instead of detailing how a nonlinear programming solver iterates to find a solution, this course focuses on formulating problems and the use of differential constraints in describing and optimizing dynamic systems. For background on how solvers work, please see the [[ https://apmonitor.com/me575 | Course on Optimization]].
to:
This course uses principles from nonlinear programming but does not delve into the details of solution strategies. Instead of detailing how a nonlinear programming solver iterates to find a solution, this course focuses on formulating problems and the use of differential constraints in describing and optimizing dynamic systems. For more background on how nonlinear programming solvers work, please see the [[ https://apmonitor.com/me575 | Course on Optimization]].
Deleted lines 21-22:

!!! Visualize constraints with contour plots
Changed lines 15-16 from:
!!!! "Hello, World" in MATLAB
to:
!!! Introduction to Nonlinear Optimization

This course uses principles from nonlinear programming but does not delve into the details of solution strategies. Instead of detailing how a nonlinear programming solver iterates to find a solution, this course focuses on formulating problems and the use of differential constraints in describing and optimizing dynamic systems. For background on how solvers work, please see the [[ https://apmonitor.com/me575 | Course on Optimization]].

Changed lines 20-26 from:
<iframe width="560" height="315" src="https://www.youtube.com/embed/tsgZP_D4DFQ" frameborder="0" allowfullscreen></iframe>
(:htmlend:)

!!!! "Hello, World" in Python

(:html:)
<iframe width="560" height="315" src="https://www.youtube.com/embed/lnNqRCXZ2cw
" frameborder="0" allowfullscreen></iframe>
to:
<iframe width="560" height="315" src="https://www.youtube.com/embed/9F3Dwgo34Lc" frameborder="0" allowfullscreen></iframe>
April 11, 2015, at 02:49 PM by 45.56.12.124 -
Deleted line 1:
(:nogroupheader:)
April 11, 2015, at 02:49 PM by 45.56.12.124 -
Changed line 2 from:
(:noheader:)
to:
(:nogroupheader:)
April 11, 2015, at 02:48 PM by 45.56.12.124 -
Added line 2:
(:noheader:)
April 04, 2015, at 02:05 PM by 45.56.12.124 -
Deleted lines 25-38:

!!! Manipulate data sets

Data set manipulation is a very important skill for dynamic optimization. Several aspects of dynamic optimization involve the import, validation, filtering, manipulation, and display of large data sets. Select one of the following tutorials below on using MATLAB or Python to import, manipulate, and export data sets.

(:html:)
<iframe width="560" height="315" src="https://www.youtube.com/embed/E56egH10RJA" frameborder="0" allowfullscreen></iframe>
(:htmlend:)

(:html:)
<iframe width="560" height="315" src="https://www.youtube.com/embed/Tq6rCWPdXoQ" frameborder="0" allowfullscreen></iframe>
(:htmlend:)

!!! Visualize time-varying data and predictions
Changed lines 15-16 from:
!!!! MATLAB
to:
!!!! "Hello, World" in MATLAB
Changed lines 21-22 from:
!!!! Python
to:
!!!! "Hello, World" in Python
Changed line 24 from:
<iframe width="560" height="315" src="https://www.youtube.com/embed/Tq6rCWPdXoQ" frameborder="0" allowfullscreen></iframe>
to:
<iframe width="560" height="315" src="https://www.youtube.com/embed/lnNqRCXZ2cw" frameborder="0" allowfullscreen></iframe>
Changed lines 11-12 from:
!!!! Introduction to Programming Languages (MATLAB and Python)
to:
!!! Introduction to Programming Languages
Added lines 15-16:
!!!! MATLAB
Added lines 21-22:
!!!! Python
Changed lines 27-28 from:
!!!! Manipulate data sets
to:
!!! Manipulate data sets
Changed lines 39-41 from:
!!!! Visualize time-varying data and predictions

!!!! Visualize constraints with contour plots
to:
!!! Visualize time-varying data and predictions

!!! Visualize constraints with contour plots
Changed lines 9-19 from:
The objective of this section is to introduce MATLAB and Python. This is not intended to be a general tutorial but more specifically to cover topics that are relevant for dynamic optimization. Topics of interest for dynamic optimization are:

* Manipulating data sets
* Setting up dynamic optimization problems
* Solving dynamic optimization problems
* Retrieving and parsing results
* Visualizing time-varying results
* Understanding the effect of constraints
* Explaining optimal solutions

(section for each below)
to:
The objective of this section is to introduce MATLAB and Python. This is not intended to be a general tutorial but more specifically to cover topics that are relevant for dynamic optimization. Topics of interest for dynamic optimization are listed below.

!!!! Introduction to Programming Languages (MATLAB and Python)

Many programming languages can be used in this course with [[https://youtu.be/t84YMw8p34w | examples in both MATLAB and Python programming languages]]. There are many resources to [[https://www.mathworks.com/help/matlab/getting-started-with-matlab.html | get started in MATLAB]] or [[https://www.python.org/about/gettingstarted/ | get started with Python]]. There are also excellent resources for experienced MATLAB programmers who would like to learn Python such as [[https://wiki.scipy.org/NumPy_for_Matlab_Users | Numpy for Matlab Users]]. The following videos give a brief introduction to MATLAB and Python including how to create a first program and debug the application by stepping through each line.

(:html:)
<iframe width="560" height="315" src="https://www.youtube.com/embed/tsgZP_D4DFQ" frameborder="0" allowfullscreen></iframe>
(:htmlend:)

(:html:)
<iframe width="560" height="315" src="https://www.youtube.com/embed/Tq6rCWPdXoQ" frameborder="0" allowfullscreen></iframe>
(:htmlend:)

!!!! Manipulate data sets

Data set manipulation is a very important skill for dynamic optimization. Several aspects of dynamic optimization involve the import, validation, filtering, manipulation, and display of large data sets. Select one of the following tutorials below on using MATLAB or Python to import, manipulate, and export data sets.

(:html:)
<iframe width="560" height="315" src="https://www.youtube.com/embed/E56egH10RJA" frameborder="0" allowfullscreen></iframe>
(:htmlend:)

(:html:)
<iframe width="560" height="315" src="https://www.youtube.com/embed/Tq6rCWPdXoQ" frameborder="0" allowfullscreen></iframe>
(:htmlend:)

!!!! Visualize time-varying data and predictions

!!!! Visualize constraints with contour plots

Contour plots are a powerful tool to visualize a feasible region (all potential solutions) and where the optimal solution is located relative to binding constraints. A [[https://apmonitor.com/me575/index.php/Main/TwoBarTruss | two-bar truss optimization problem]] is solved with MATLAB and Python and visualized with contour plots that show binding constraints, objective values, and trade-offs between the design variables. While this problem is a static design optimization problem (no time-varation), the same methods can be applied to dynamic optimization.

(:html:)
<iframe width="560" height="315" src="https://www.youtube.com/embed/uOTdLfvgYHU?rel=0" frameborder="0" allowfullscreen></iframe>
(:htmlend:)

(:html:)
<iframe width="560" height="315" src="https://www.youtube.com/embed/ah-Cbrim93I?rel=0" frameborder="0" allowfullscreen></iframe>
(:htmlend:)

A second example is a Linear Programming (LP) refinery problem where the balance between two crude supplies is optimized. This example shows a contour plot as the solver progresses towards an optimal solution.

(:html:)
<iframe width="560" height="315" src="https://www.youtube.com/embed/M_mpRrGKKMo?rel=0" frameborder="0" allowfullscreen></iframe>
(:htmlend:)
Changed line 1 from:
(:title Languages for Dynamic Optimization:)
to:
(:title Dynamic Optimization Basics:)
Changed line 7 from:
There are many tools for simulation and optimization of dynamic systems. There are also many factors to consider when selecting the appropriate tool for a particular task. Some of the considerations for a dynamic optimization tool are size (scaling with number equations and degrees of freedom), speed (use in real-time estimation or control), usability (training requirements and ease of adaptation), extensibility (ability to adapt for new or complex systems), support (commercial sustainment or open-source community), and portability (ability to embed on a system or communicate with a particular platform).
to:
There are many tools for simulation and optimization of dynamic systems. There are also many factors to consider when selecting the appropriate tool for a particular task. Some of the considerations for a dynamic optimization tool are size (scaling with number equations and degrees of freedom), speed (use in real-time estimation or control), usability (training requirements and ease of adaptation), extensibility (ability to modify for new or complex systems), support (commercial sustainment or open-source community), and portability (ability to embed on a system or communicate with a particular platform).
Changed lines 1-3 from:
(:title Overview of Dynamic Modeling Languages:)
(:keywords modeling language, differential, algebraic, tutorial:)
(:description Modeling languages for Differential Algebraic Equations (DAEs
) and dynamic simulation, estimation, and control:)
to:
(:title Languages for Dynamic Optimization:)
(:keywords modeling language, optimization, model predictive control, moving horizon estimation, differential, algebraic, tutorial:)
(:description Modeling languages for solution of Differential Algebraic Equations (DAEs) in simulation, estimation, and control:)

Two tools used in this course are MATLAB and Python.  APMonitor is used as a toolbox in MATLAB or as a package within Python to provide support for solution of dynamic simulation, estimation, and control.

There are many tools for simulation and optimization of dynamic systems. There are also many factors to consider when selecting the appropriate tool for a particular task. Some of the considerations for a dynamic optimization tool are size (scaling with number equations and degrees of freedom), speed (use in real-time estimation or control), usability (training requirements and ease of adaptation), extensibility (ability to adapt for new or complex systems), support (commercial sustainment or open-source community), and portability (ability to embed on a system or communicate with a particular platform).

The objective of this section is to introduce MATLAB and Python. This is not intended to be a general tutorial but more specifically to cover topics that are relevant for dynamic optimization. Topics of interest for dynamic optimization are:

* Manipulating data sets
* Setting up dynamic optimization problems
* Solving dynamic optimization problems
* Retrieving and parsing results
* Visualizing time-varying results
* Understanding the effect of constraints
* Explaining optimal solutions

(section for each below
)
Added lines 1-3:
(:title Overview of Dynamic Modeling Languages:)
(:keywords modeling language, differential, algebraic, tutorial:)
(:description Modeling languages for Differential Algebraic Equations (DAEs) and dynamic simulation, estimation, and control:)