Optimization with Python

Main.PythonOptimization History

Hide minor edits - Show changes to output

Changed line 23 from:
<iframe width="560" height="315" src="//www.youtube.com/embed/Cn91MrBbxqc?list=PLLBUgWXdTBDi-E--rwBujaNkTejLNI6ap" frameborder="0" allowfullscreen></iframe>
to:
<iframe width="560" height="315" src="https://www.youtube.com/embed/Cn91MrBbxqc?list=PLLBUgWXdTBDi-E--rwBujaNkTejLNI6ap" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe>
Changed line 31 from:
<iframe width="560" height="315" src="https://www.youtube.com/embed/cXHvC_FGx24" frameborder="0" allowfullscreen></iframe>
to:
<iframe width="560" height="315" src="https://www.youtube.com/embed/cXHvC_FGx24" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe>
June 16, 2022, at 02:17 AM by 24.50.235.201 -
Deleted lines 100-101:

#Initialize Model
Changed lines 102-129 from:

#help(m)

#define parameter
eq = m.Param(value=40)

#initialize variables
x1,x2,x3,x4 = [m.Var() for i in range(4)]

#initial values
x1.value = 1
x2.value = 5
x3.value = 5
x4.value = 1

# lower bounds
x1.lower = 1
x2.lower = 1
x3.lower = 1
x4.lower = 1

# upper bounds
x1.upper = 5
x2.upper = 5
x3.upper = 5
x4.upper = 5

#Equations
to:
x = m.Array(m.Var,4,value=1,lb=1,ub=5)
x1,x2,x3,x4 = x
# change initial values
x2.value = 5; x3.value = 5
Changed lines 107-115 from:
m.Equation(x1**2+x2**2+x3**2+x4**2==eq)

#Objective

m.Obj(x1*x4*(x1+x2+x3)+x3)

#Set global options
m.options.IMODE = 3 #steady state optimization

#Solve simulation
to:
m.Equation(x1**2+x2**2+x3**2+x4**2==40)
m.Minimize(x1*x4*(x1+x2+x3)+x3)
Changed lines 110-117 from:

#Results

print('')
print('Results')
print('x1: ' + str(x1
.value))
print('x2: ' + str(x2.value))
print('x3: ' + str(x3.value))
print('x4: ' + str(x4.value)
)
to:
print('x: ', x)
print('Objective: ',m.options.OBJFCNVAL)
June 21, 2020, at 04:16 AM by 136.36.211.159 -
Deleted lines 154-172:

----

(:html:)
 <div id="disqus_thread"></div>
    <script type="text/javascript">
        /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
        var disqus_shortname = 'apmonitor'; // required: replace example with your forum shortname

        /* * * DON'T EDIT BELOW THIS LINE * * */
        (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = 'https://' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
        })();
    </script>
    <noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
    <a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
(:htmlend:)
April 02, 2019, at 09:21 PM by 10.37.195.189 -
Changed line 142 from:
m.solve(remote=True)
to:
m.solve()
August 11, 2018, at 09:03 PM by 174.148.138.184 -
Changed lines 120-124 from:
x1.lb = 1
x2.lb = 1
x3.lb = 1
x4.lb = 1
to:
x1.lower = 1
x2.lower = 1
x3.lower = 1
x4.lower = 1
Changed lines 126-129 from:
x1.ub = 5
x2.ub = 5
x3.ub = 5
x4.ub = 5
to:
x1.upper = 5
x2.upper = 5
x3.upper = 5
x4.upper = 5
Changed line 90 from:
[[https://gekko.readthedocs.io/en/latest/|%width=300px%Attach:gekko.png]]
to:
%width=300px%[[https://gekko.readthedocs.io/en/latest/|Attach:gekko.png]]
Changed line 90 from:
%width=300px%Attach:gekko.png
to:
[[https://gekko.readthedocs.io/en/latest/|%width=300px%Attach:gekko.png]]
Changed line 90 from:
%width=550px%Attach:gekko.png
to:
%width=300px%Attach:gekko.png
Added lines 91-94:

(:html:)
<iframe width="560" height="315" src="https://www.youtube.com/embed/SH753YX2K1A" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe>
(:htmlend:)
Changed lines 90-92 from:
(:html:)
<iframe width="560" height="315" src="https://www
.youtube.com/embed/cXHvC_FGx24" frameborder="0" allowfullscreen></iframe>
(:htmlend:)
to:
%width=550px%Attach:gekko.png
Added line 149:
(:sourceend:)
Deleted line 150:
(:sourceend:)
Added line 96:
(:source lang=python:)
Added line 150:
(:sourceend:)
Added lines 34-36:
(:toggle hide scipy button show="Show SciPy Solution":)
(:div id=scipy:)

Added lines 84-148:
(:divend:)

----

!!!! Method #3: GEKKO Solution

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

(:toggle hide gekko button show="Show GEKKO Solution":)
(:div id=gekko:)
from gekko import GEKKO   
import numpy as np

#Initialize Model
m = GEKKO()

#help(m)

#define parameter
eq = m.Param(value=40)

#initialize variables
x1,x2,x3,x4 = [m.Var() for i in range(4)]

#initial values
x1.value = 1
x2.value = 5
x3.value = 5
x4.value = 1

# lower bounds
x1.lb = 1
x2.lb = 1
x3.lb = 1
x4.lb = 1

# upper bounds
x1.ub = 5
x2.ub = 5
x3.ub = 5
x4.ub = 5

#Equations
m.Equation(x1*x2*x3*x4>=25)
m.Equation(x1**2+x2**2+x3**2+x4**2==eq)

#Objective
m.Obj(x1*x4*(x1+x2+x3)+x3)

#Set global options
m.options.IMODE = 3 #steady state optimization

#Solve simulation
m.solve(remote=True)

#Results
print('')
print('Results')
print('x1: ' + str(x1.value))
print('x2: ' + str(x2.value))
print('x3: ' + str(x3.value))
print('x4: ' + str(x4.value))
(:divend:)
April 06, 2017, at 02:20 AM by 10.5.113.121 -
Changed lines 59-60 from:
print('Initial SSE Objective: ' + str(objective(x0)))
to:
print('Initial Objective: ' + str(objective(x0)))
Changed line 72 from:
print('Final SSE Objective: ' + str(objective(x)))
to:
print('Final Objective: ' + str(objective(x)))
Added lines 26-27:
----
Added lines 29-32:

(:html:)
<iframe width="560" height="315" src="https://www.youtube.com/embed/cXHvC_FGx24" frameborder="0" allowfullscreen></iframe>
(:htmlend:)
Added lines 17-18:
!!!! Method #1: APM Python
Added lines 25-74:

!!!! Method #2: SciPy Optimize Minimize

(:source lang=python:)
import numpy as np
from scipy.optimize import minimize

def objective(x):
    return x[0]*x[3]*(x[0]+x[1]+x[2])+x[2]

def constraint1(x):
    return x[0]*x[1]*x[2]*x[3]-25.0

def constraint2(x):
    sum_eq = 40.0
    for i in range(4):
        sum_eq = sum_eq - x[i]**2
    return sum_eq

# initial guesses
n = 4
x0 = np.zeros(n)
x0[0] = 1.0
x0[1] = 5.0
x0[2] = 5.0
x0[3] = 1.0

# show initial objective
print('Initial SSE Objective: ' + str(objective(x0)))

# optimize
b = (1.0,5.0)
bnds = (b, b, b, b)
con1 = {'type': 'ineq', 'fun': constraint1}
con2 = {'type': 'eq', 'fun': constraint2}
cons = ([con1,con2])
solution = minimize(objective,x0,method='SLSQP',\
                    bounds=bnds,constraints=cons)
x = solution.x

# show final objective
print('Final SSE Objective: ' + str(objective(x)))

# print solution
print('Solution')
print('x1 = ' + str(x[0]))
print('x2 = ' + str(x[1]))
print('x3 = ' + str(x[2]))
print('x4 = ' + str(x[3]))
(:sourceend:)
Changed lines 9-13 from:
Attach:hs71.gif
to:
{$\min x_1 x_4 \left(x_1 + x_2 + x_3\right) + x_3$}
{$\mathrm{s
.t.} \quad x_1 x_2 x_3 x_4 \ge 25$}
{$x_1^2 + x_2^2 + x_3^2 + x_4^2 = 40$}
{$1\le x_1, x_2, x_3, x_4 \le 5$}
{$x_0 = (1,5,5,1)$}
May 06, 2014, at 06:01 AM by 23.255.240.62 -
Changed line 17 from:
<iframe width="560" height="315" src="//www.youtube.com/embed/BZmSH8Y8_fc?list=PLLBUgWXdTBDi-E--rwBujaNkTejLNI6ap" frameborder="0" allowfullscreen></iframe>
to:
<iframe width="560" height="315" src="//www.youtube.com/embed/Cn91MrBbxqc?list=PLLBUgWXdTBDi-E--rwBujaNkTejLNI6ap" frameborder="0" allowfullscreen></iframe>
May 06, 2014, at 05:14 AM by 23.255.240.62 -
Changed line 7 from:
Mathematical optimization problems may include equality constraints (e.g. =), inequality constraints (e.g. <, <=, >, >=), objective functions, algebraic equations, differential equations, continuous variables, discrete or integer variables, etc. One example of an optimization problem from a benchmark test set is the Hock Schittkowski problem #71.
to:
Mathematical optimization problems may include equality constraints (e.g. =), inequality constraints (e.g. <, <=, >, >=), objective functions, algebraic equations, differential equations, continuous variables, discrete or integer variables, etc. One example of an optimization problem from a benchmark test set is the Hock Schittkowski problem #71. 
May 06, 2014, at 04:29 AM by 23.255.240.62 -
Added lines 1-39:
(:title Optimization with Python:)
(:keywords Optimization, Nonlinear Programming, Python, nonlinear, optimization, engineering optimization, university course:)
(:description Optimization with Python - Problem-Solving Techniques for Chemical Engineers at Brigham Young University:)

Optimization deals with selecting the best option among a number of possible choices that are feasible or don't violate constraints. Python can be used to optimize parameters in a model to best fit data, increase profitability of a potential engineering design, or meet some other type of objective that can be described mathematically with variables and equations.

Mathematical optimization problems may include equality constraints (e.g. =), inequality constraints (e.g. <, <=, >, >=), objective functions, algebraic equations, differential equations, continuous variables, discrete or integer variables, etc. One example of an optimization problem from a benchmark test set is the Hock Schittkowski problem #71.

Attach:hs71.gif

This problem has a nonlinear objective that the optimizer attempts to minimize. The variable values at the optimal solution are subject to (s.t.) both equality (=40) and inequality (>25) constraints. The product of the four variables must be greater than 25 while the sum of squares of the variables must also equal 40. In addition, all variables must be between 1 and 5 and the initial guess is x'_1_' = 1, x'_2_' = 5, x'_3_' = 5, and x'_4_' = 1.

* [[Attach:hs71_Python.zip|Download HS71 Example Problem in Python]]
-->Attach:hs71_Python.png

(:html:)
<iframe width="560" height="315" src="//www.youtube.com/embed/BZmSH8Y8_fc?list=PLLBUgWXdTBDi-E--rwBujaNkTejLNI6ap" frameborder="0" allowfullscreen></iframe>
(:htmlend:)

This tutorial can also be completed with nonlinear programming optimizers that are available with the [[Main/ExcelSolver|Excel Solver]] and [[Main/MatlabOptimization|MATLAB Optimization Toolbox]]. Click on the appropriate link for additional information and source code.

----

(:html:)
 <div id="disqus_thread"></div>
    <script type="text/javascript">
        /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
        var disqus_shortname = 'apmonitor'; // required: replace example with your forum shortname

        /* * * DON'T EDIT BELOW THIS LINE * * */
        (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = 'https://' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
        })();
    </script>
    <noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
    <a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
(:htmlend:)