{ "cells": [ { "cell_type": "markdown", "metadata": { "nbpages": { "level": 0, "link": "[](https://jckantor.github.io/CBE30338/07.08-Path-Constraints.html)", "section": "" } }, "source": [ "\n", "*This notebook contains material from [CBE30338](https://jckantor.github.io/CBE30338);\n", "content is available [on Github](https://github.com/jckantor/CBE30338.git).*\n" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 0, "link": "[](https://jckantor.github.io/CBE30338/07.08-Path-Constraints.html)", "section": "" } }, "source": [ "\n", "< [7.7 Transient Heat Transfer in Various Geometries](https://jckantor.github.io/CBE30338/07.07-Transient-Heat-Transfer-in-Various-Geometries.html) | [Contents](toc.html) | [Tag Index](tag_index.html) | [8.0 Predictive Control](https://jckantor.github.io/CBE30338/08.00-Predictive-Control.html) >
"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 1,
"link": "[7.8 Path Constraints](https://jckantor.github.io/CBE30338/07.08-Path-Constraints.html#7.8-Path-Constraints)",
"section": "7.8 Path Constraints"
}
},
"source": [
"# 7.8 Path Constraints\n",
"\n",
"https://github.com/Pyomo/pyomo/blob/master/examples/dae/Path_Constraint.py"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"nbpages": {
"level": 1,
"link": "[7.8 Path Constraints](https://jckantor.github.io/CBE30338/07.08-Path-Constraints.html#7.8-Path-Constraints)",
"section": "7.8 Path Constraints"
}
},
"outputs": [],
"source": [
"#\n",
"# Pyomo: Python Optimization Modeling Objects\n",
"# Copyright 2017 National Technology and Engineering Solutions of Sandia, LLC\n",
"# Under the terms of Contract DE-NA0003525 with National Technology and \n",
"# Engineering Solutions of Sandia, LLC, the U.S. Government retains certain \n",
"# rights in this software.\n",
"# This software is distributed under the 3-clause BSD License.\n",
"\n",
"# Sample Problem 3: Inequality State Path Constraint\n",
"# (Ex 4 from Dynopt Guide)\n",
"#\n",
"# min x3(tf)\n",
"# s.t. X1_dot = X2 X1(0) = 0\n",
"# X2_dot = -X2+u X2(0) = -1\n",
"# X3_dot = X1^2+x2^2+0.005*u^2 X3(0) = 0\n",
"# X2-8*(t-0.5)^2+0.5 <= 0\n",
"# tf = 1\n",
"#"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"nbpages": {
"level": 1,
"link": "[7.8 Path Constraints](https://jckantor.github.io/CBE30338/07.08-Path-Constraints.html#7.8-Path-Constraints)",
"section": "7.8 Path Constraints"
}
},
"outputs": [],
"source": [
"from pyomo.environ import *\n",
"from pyomo.dae import *\n",
"\n",
"m = ConcreteModel()\n",
"m.tf = Param(initialize=1)\n",
"m.t = ContinuousSet(bounds=(0,m.tf))\n",
"\n",
"m.u = Var(m.t, initialize=0)\n",
"m.x1 = Var(m.t)\n",
"m.x2 = Var(m.t)\n",
"m.x3 = Var(m.t)\n",
"\n",
"m.dx1 = DerivativeVar(m.x1, wrt=m.t)\n",
"m.dx2 = DerivativeVar(m.x2, wrt=m.t)\n",
"m.dx3 = DerivativeVar(m.x3, wrt=m.t)\n",
"\n",
"m.obj = Objective(expr=m.x3[m.tf])\n",
"\n",
"def _x1dot(m, t):\n",
" if t == 0:\n",
" return Constraint.Skip\n",
" return m.dx1[t] == m.x2[t]\n",
"m.x1dotcon = Constraint(m.t, rule=_x1dot)\n",
"\n",
"def _x2dot(m, t):\n",
" if t == 0:\n",
" return Constraint.Skip\n",
"\n",
" return m.dx2[t] == -m.x2[t]+m.u[t]\n",
"m.x2dotcon = Constraint(m.t, rule=_x2dot)\n",
"\n",
"def _x3dot(m, t):\n",
" if t == 0:\n",
" return Constraint.Skip\n",
"\n",
" return m.dx3[t] == m.x1[t]**2+m.x2[t]**2+0.005*m.u[t]**2\n",
"m.x3dotcon = Constraint(m.t, rule=_x3dot)\n",
"\n",
"def _con(m, t):\n",
" return m.x2[t]-8*(t-0.5)**2+0.5 <= 0\n",
"m.con = Constraint(m.t, rule=_con)\n",
"\n",
"def _init(m):\n",
" yield m.x1[0] == 0\n",
" yield m.x2[0] == -1\n",
" yield m.x3[0] == 0\n",
"m.init_conditions = ConstraintList(rule=_init)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"nbpages": {
"level": 1,
"link": "[7.8 Path Constraints](https://jckantor.github.io/CBE30338/07.08-Path-Constraints.html#7.8-Path-Constraints)",
"section": "7.8 Path Constraints"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"# ==========================================================\n",
"# = Solver Results =\n",
"# ==========================================================\n",
"# ----------------------------------------------------------\n",
"# Problem Information\n",
"# ----------------------------------------------------------\n",
"Problem: \n",
"- Lower bound: -inf\n",
" Upper bound: inf\n",
" Number of objectives: 1\n",
" Number of constraints: 256\n",
" Number of variables: 255\n",
" Sense: unknown\n",
"# ----------------------------------------------------------\n",
"# Solver Information\n",
"# ----------------------------------------------------------\n",
"Solver: \n",
"- Status: ok\n",
" Message: Ipopt 3.12.8\\x3a Optimal Solution Found\n",
" Termination condition: optimal\n",
" Id: 0\n",
" Error rc: 0\n",
" Time: 0.08427214622497559\n",
"# ----------------------------------------------------------\n",
"# Solution Information\n",
"# ----------------------------------------------------------\n",
"Solution: \n",
"- number of solutions: 0\n",
" number of solutions displayed: 0\n"
]
}
],
"source": [
"# transform and solve\n",
"TransformationFactory('dae.collocation').apply_to(m, wrt=m.t, nfe=3, ncp=12, method='BACKWARD')\n",
"SolverFactory('ipopt').solve(m).write()"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"nbpages": {
"level": 1,
"link": "[7.8 Path Constraints](https://jckantor.github.io/CBE30338/07.08-Path-Constraints.html#7.8-Path-Constraints)",
"section": "7.8 Path Constraints"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[
"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}