forked from Pyomo/pyomo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOptimal_Control.py
51 lines (40 loc) · 1.32 KB
/
Optimal_Control.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# ___________________________________________________________________________
#
# Pyomo: Python Optimization Modeling Objects
# Copyright 2017 National Technology and Engineering Solutions of Sandia, LLC
# Under the terms of Contract DE-NA0003525 with National Technology and
# Engineering Solutions of Sandia, LLC, the U.S. Government retains certain
# rights in this software.
# This software is distributed under the 3-clause BSD License.
# ___________________________________________________________________________
# Sample Problem 1 (Ex 1 from Dynopt Guide)
#
# min X2(tf)
# s.t. X1_dot = u X1(0) = 1
# X2_dot = X1^2 + u^2 X2(0) = 0
# tf = 1
from pyomo.environ import *
from pyomo.dae import *
m = ConcreteModel()
m.t = ContinuousSet(bounds=(0,1))
m.x1 = Var(m.t, bounds=(0,1))
m.x2 = Var(m.t, bounds=(0,1))
m.u = Var(m.t, initialize=0)
m.x1dot = DerivativeVar(m.x1)
m.x2dot = DerivativeVar(m.x2)
m.obj = Objective(expr=m.x2[1])
def _x1dot(M,i):
if i == 0:
return Constraint.Skip
return M.x1dot[i] == M.u[i]
m.x1dotcon = Constraint(m.t, rule=_x1dot)
def _x2dot(M,i):
if i == 0:
return Constraint.Skip
return M.x2dot[i] == M.x1[i]**2 + M.u[i]**2
m.x2dotcon = Constraint(m.t, rule=_x2dot)
def _init(M):
yield M.x1[0] == 1
yield M.x2[0] == 0
yield ConstraintList.End
m.init_conditions = ConstraintList(rule=_init)