Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for...

61
FINAL REPORT: FEUP2013.LTPAIVA.01 Optimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1 Department of Electrical and Computer Engineering University of Porto, Faculty of Engineering Rua Dr. Roberto Frias, s/n, 4200–465 Porto, Portugal [email protected] 22 508 1450 February 28, 2013 Abstract This final report briefly describes the work carried out under the project PTDC/EEA- CRO/116014/2009 – “Optimal Control for Constrained Hybrid System”. The aim was to build and maintain a software platform to test an illustrate the use of the conceptual tools developed during the overall project: not only in academic examples but also in case studies in the areas of robotics, medicine and exploitation of renewable resources. The grand holder developed a critical hands–on knowledge of the available optimal control solvers as well as package based on non–linear programming solvers. 1

Transcript of Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for...

Page 1: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

FINAL REPORT FEUP2013LTPAIVA01

Optimal Control for Constrained Hybrid SystemComputational Libraries and Applications

LT Paiva 1

1 Department of Electrical and Computer EngineeringUniversity of Porto Faculty of Engineering

- Rua Dr Roberto Frias sn 4200ndash465 Porto Portugal) ltpaivafeuppt 22 508 1450

February 28 2013

Abstract

This final report briefly describes the work carried out under the project PTDCEEA-CRO1160142009 ndash ldquoOptimal Control for Constrained Hybrid SystemrdquoThe aim was to build and maintain a software platform to test an illustrate the use of theconceptual tools developed during the overall project not only in academic examples but alsoin case studies in the areas of robotics medicine and exploitation of renewable resources Thegrand holder developed a critical handsndashon knowledge of the available optimal control solversas well as package based on nonndashlinear programming solvers

1

2

Contents

1 OC amp NLP Interfaces 711 Introduction 712 AMPL 713 ACADO ndash Automatic Control And Dynamic Optimization 814 BOCOP ndash The optimal control solver 915 DIDO ndash Automatic Control And Dynamic Optimization 1016 ICLOCS ndash Imperial College London Optimal Control Software 1217 TACO ndash Toolkit for AMPL Control Optimization 1218 Pseudospectral Methods in Optimal Control 13

2 NLP Solvers 1721 Introduction 1722 IPOPT ndash Interior Point OPTimizer 1723 KNITRO 2524 WORHP ndash WORHP Optimises Really Huge Problems 3125 Other Commercial Packages 33

3 Project webpage 35

4 Optimal Control Toolbox 37

5 Applications 3951 CarndashLike 3952 Goddard Problem 4353 HIV 44

A Programming code 47A1 Optimal Control Toolbox MATLAB Code 47A2 CarndashLike AMPL Code 55A3 HIV ICLOCS Code 57

3

4

Work Plan

Project PTDCEEA-CRO1160142009 ndash ldquoOptimal Control for Constrained Hybrid Systemrdquo

The main tasks to be carried out are

1 To understand and obtain a handsndashon knowledge of nonndashlinear programming models cur-rent tools to address them (AMPL MATLAB ) and current solvers (KNITRO WORHPSNOPT ) Comparative study of different solvers

2 To understand optimal control problems and how to transcribe them into NLP to be im-plemented in AMPL or MATLAB

3 To obtain a handsndashon knowledge of current optimal control packages such DIDO ACADOOCPID-DAE ASTOS BOCOP and others To perform a comparative study

4 To help to implement and solve case studies and tests to illustrate theoretical develop-ments

5 To maintain the project webndashpage and documentation accessible to the project team

6 (If time permits) To construct a platform to easily solve sequences of optimal control prob-lems in a receding horizon fashion in order to implement model predictive controllers

TODAY

Months 0 1 2 3 4 5 6 7 8 9 10 11 12

100 completeTask 1

100 completeTask 2

100 completeTask 3

100 completeTask 4

100 completeTask 5

50 completeTask 6

Progress reports on tasks just concluded and ongoing tasks should be produced at the endof months 3 5 7 10 and 12

5

6

Chapter 1

Optimal Control andNonlinear Programming Interfaces

11 Introduction

Optimal control and nonlinear programming interfaces are software used to solve optimalcontrol problems A interface is a software that help us to prepare all data as input to the solverWe can divide the interfaces in two groups optimal control interfaces and nonlinear program-ming interfaces The first ones handle with the discretisation and transcription procedureswhile the other ones leave this work to the programmerresearcher (see Figure 11) Severalinterfaces will be presented in this chapter evolving openndashsource freeware and commercialsoftware working under different operating systems

Figure 11

12 AMPL

ldquoAMPL ndash A Mathematical Programming Language ndash is a comprehensive and powerful alge-braic modeling language for linear and nonlinear optimization problems in discrete or contin-uous variablesrdquo The AMPL library can be found in the official site1

121 Basic information

Developed by Robert Fourer David Gay and Brian Kernighan at Bell Laboratories1httpwwwanmplcom

7

Operating system(s) AMPL can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) AMPL is written in C++ and it is released as open source code under theEclipse Public License (EPL)

122 Installing AMPL on Linux

Download your AMPL executable by selecting the Intel (Pentium-compatible) PCs runningLinux on AMPL website2 or type in a Terminal window

wget httpnetlibsandiagovamplstudentlinuxamplgz

The downloaded file must be decompressed and it must be made executable after which itmay be executed from any directory in your search path

gzip -d amplgz

chmod +x ampl

sudo mkdir -p optampl

sudo mv ampl optampl

echo rsquoEXPORT PATH=$PATHoptamplrsquo gtgt $HOMEbashrc

To complete your AMPL installation you can also download solvers to a directory in yoursearch path To use a downloaded solver set AMPLrsquos solver option to the name of the solverrsquosexecutable file

The student edition of AMPL is limited to 300 variables and 300 constraints and objectives(after presolve) for nonlinear problems

123 Getting started with AMPL

The first step in solving a problem is to load the model variables objective function andconstraints It is usual the create a mod file containing these informations First open a AMPLcommand window by typing ampl in a Terminal window

AMPL commands end with semicolon () Some AMPL basic commands

bull to declare a parameter param ltpar namegt

bull to load a model model ltfile namegtmod

bull to run a AMPL script include ltscript namegtrun

bull to load a data file data ltfile namegtdat

bull to view the content of a variable display ltvar namegt

bull to exit a AMPL command window exit

13 ACADO ndash Automatic Control And Dynamic Optimization

ldquoACADO Toolkit is a software environment and algorithm collection for automatic controland dynamic optimization It provides a general framework for using a great variety of al-gorithms for direct optimal control including model predictive control state and parameterestimation and robust optimizationrdquo The ACADO library can be found in the official site3

2httpnetlibsandiagovamplstudentlinuxamplgz3httpsourceforgenetprojectsacado

8

131 Basic information

Developed under the direction of Moritz Diehl

Operating system(s) ACADO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) ACADO Toolkit is implemented as self-contained C++ code it comes alongwith user-friendly MATLAB interface and is released under the LGP License

132 Installing ACADO on Linux

In order to install ACADO Toolkit under LINUXUNIX operating systems make sure thata C++ compiler is correctly installed For the case that a graphical output is desired Gnuplotshould be installed in addition However ACADO Toolkit will also run without Gnuplot - inthis case the graphical output is simply turned off There are no further dependencies

First download ACADO Toolkit and its licence from the offical website4 and extract the filestar xfvz ACADOtoolkit-102613betatargz

sudo mv ACADOtoolkit-102613beta optacado

Then go to the directory optacado and compile the packagecd optacado

sudo make

sudo make doc (optional)

Finally check whether the installation was successfulcd examplesgetting started

simple ocp

The following documentation5 is available

bull ACADO Toolkit Userrsquos Manual

bull ACADO for Matlab Userrsquos Manual

bull ACADO Toolkit Introductory Talk

14 BOCOP ndash The optimal control solver

ldquoThe BOCOP project aims to develop an open-source toolbox for solving optimal controlproblemsrdquo The BOCOP library can be found in the official site6

141 Basic information

Developed by V Grelard P Martinon and F Bonnans at Inria-Saclay

Operating system(s) BOCOP is available for linux precompiled packages (Mac and Windowsversions are still under testing)

Code language(s) BOCOP requires SciLab and it is released under the Eclipse Public License(EPL)

4httpwwwacadotoolkitorgdownloadphp5httpsourceforgenetpacadowikiDocumentation6httpwwwbocoporg

9

142 Installing BOCOP on Linux

In order to install BOCOP software under LINUXUNIX operating system download BO-COP software from the official website7 and extract the files

tar xvf bocop-103-beta-linux32targz

mv bocop-103-beta-linux32 optbocop

cd optbocop

make

sudo chmod +x bocopsh

Add the following to your bashrc or bash profile

BOCOPDIR=optbocop

export PATH=$PATH$BOCOPDIR

143 Getting started with BOCOP

Read the userrsquos guide8 and run the examples shown in the optbocopproblems folder

15 DIDO ndash Automatic Control And Dynamic Optimization

ldquoDIDO the leading optimal control software powers users by offering the easiest and directsolutions to the most complex problemsrdquo The DIDO software can be found in the official site9

151 Basic information

Developed at Elissar Global

Operating system(s) DIDO can be used on Microsoft Windows

Code language(s) DIDO requires MATLAB and it is released under academic and commerciallicenses

152 Installing DIDO

DIDOrsquos foundation is pseudospectral theory and is the only pseudospectral solution withmathematically proven convergence properties DIDO is a MATLAB program for solving hybridoptimal control problems The generalndashpurpose program is named after Dido the legendaryfounder and first queen of Carthage who is famous in mathematics for her remarkable solutionto a constrained optimal control problem even before the invention of calculus

To install DIDO on your computer you should consider the system requirements

bull WINDOWS

bull MATLAB

and the following steps

7httpwwwbocoporg8httpbocopsaclayinriafrdownload=109httpwwwelissarglobalcomindustryproductssoftware-3

10

STEP 1 Obtain a license for DIDO from Elissar Global at the official site10

STEP 2 After you receive an email with the link to a compressed version of DIDO downloadthe zip file and uncompress it It will automatically uncompress with the right folderstructure

STEP 3 Right click on the DIDO icon and click on PROPERTIES In the Target box type locationof your licensed MATLAB file In the Start in box type the full path name of your DIDOfolder

STEP 4 Doublendashclick on the DIDO ICON (this should start MATLAB and automatically initial-ize DIDO)

(a) In the MATLAB command window type TestDIDOA successful installation should produce the final output on the screenldquoCONGRATULATIONS DIDO TEST WAS SUCCESSFULrdquo

(b) A further test may be performed by typing LanderProblemA successful installation should produce a pretty graph

153 Getting started with DIDO

In the MATLAB command window run[cost primal] = dido(Problem)

where Problem is a structure array with fields cost dynamics events and path that point to theinput files

Problemcost = lsquoMyCostFilersquo

Problemdynamics = lsquoMyDynamicsFilersquo

Problemevents = lsquoMyEventsFilersquo

Problempath = lsquoMyPathFilersquo

The variable primal is used in nearly all DIDO files to pass the states controls parametersand other useful quantities To print out the statendash and controlndashtrajectory in the MATLABcommand window runprimalstates primalcontrols

This generality allows for

bull Fairly complex interior point constraints

bull Pre-defined segments

bull Differentially-flat segments

bull Transition conditions

bull Midndashmaneuver changes in dynamics

bull Multi-dynamical systems

bull Midndashmaneuver changes in the cost function

bull Switches

bull Discrete events10httpwwwelissarglobalcomacademicget-didotry-dido

11

16 ICLOCS ndash Imperial College London Optimal Control Software

ldquoThe code allows users to define and solve optimal control problems with general path andboundary constraints and free or fixed final time It is also possible to include constant designparameters as unknownsrdquo The ICLOCS software can be found in the official site11

161 Basic information

Developed by Paola Falugi Eric Kerrigan and Eugene van Wyk

Operating system(s) ICLOCS can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) ICLOCS is implemented in MATLAB and it is released as open source codeunder the BSD License

162 Installing ICLOCS on Linux

In order to install ICLOCS software under LINUXUNIX operating system download ICLOCSsoftware from the official site12 and extract the files

wget httpwwweeicacukICLOCSICLOCS 02 NItarzip

unzip ICLOCS 02 NItarzip

tar xvf ICLOCS 02 NItar

sudo mv ICLOCS 02 NI opticlocs

In order to use it in MATLAB you need to tell MATLAB where to find it To do this justtype addpath(genpath(rsquoopticlocsrsquo)) in the MATLAB command window

163 Getting started with ICLOCS

Read the userrsquos guide13 and run the examples shown in the opticlocsexamples folder

17 TACO ndash Toolkit for AMPL Control Optimization

ldquoTACO is the Toolkit for AMPL Control Optimization It defines some add-ons to the AMPLmodeling language that allow the elegant formulation of ODEDAE optimal control problemsin AMPLrdquo The TACO toolkit can be found in the official site14

171 Basic information

Developed by Christian Kirches and Sven Leyffer

Operating system(s) TACO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) TACO is written in C

11httpwwweeicacukICLOCS12httpwwweeicacukICLOCS13httpwwweeicacukICLOCSuser_guidepdf14httpwwwiwruni-heidelbergde~ChristianKirchessoftwarehtml

12

172 Installing TACO on Linux

In order to install TADO Toolkit under LINUXUNIX operating systems make sure thatCMAKE and a C++ compiler is correctly installed To install CMAKE type in a Terminal win-dow

wget httpslaunchpadnetubuntu+archiveprimary+filescmake 288origtargz

gunzip cmake 288origtargz

tar xvf cmake 288origtar

cd cmake-288

sudo bootstrap

sudo make

sudo make install

First download TACO Toolkit from the offical site15 and extract the files

wget httpswwwiwruni-heidelbergdegroupsagbockFILEStaco sourcetargzgunzip taco sourcetargz

tar xfvz taco sourcetar

sudo ln -s optCoinIpoptThirdPartyASLlibamplsolvera libamplsolvera

18 Pseudospectral Methods in Optimal Control

181 PSOPT

PSOPT is an open source optimal control software package written in C++ that uses directcollocation methods including pseudospectral and local discretizations available in the officesite16 Pseudospectral methods solve optimal control problems by approximating the time-dependent variables using global polynomials such as Legendre or Chebyshev functions Localdiscretisation methods approximate the time dependent functions using local splines and canbe seen as implementations of implicit Runge-Kutta integrators With both global and localmethods differential equations continuous constraints and integrals associated with the prob-lem are discretised over a grid of nodes Sparse nonlinear programming is then used to findlocal optimal solutions

PSOPT is able to deal with problems with the following characteristics

bull Single or multiphase problems

bull Continuous time nonlinear dynamics

bull Nonlinear path constraints

bull General event constraints

bull Integral constraints

bull Interior point constraints

bull Bounds on controls and state variables

bull General cost function with Lagrange and Mayer terms

15httpwwwiwruni-heidelbergde~ChristianKirchessoftwarehtml16httpwwwpsoptorg

13

bull Linear or nonlinear linkages between phases

bull Fixed or free initial phase time

bull Fixed or free final phase time

bull Optimization of static parameters

bull Optimal parameter estimation given sampled observations

The implementation has the following features

bull Choice between Legendre Chebyshev central differences trapezoidal or Hermite-Simpsondiscretisation

bull Large scale nonlinear programming using IPOPT and (optionally) SNOPT

bull Estimation of the discretisation error

bull Automatic mesh refinement

bull Automatic scaling

bull Automatic differentiation using the ADOL-C library

bull Numerical differentiation by using sparse finite differences

bull Automatic identification of the sparsity of the derivative matrices

bull DAE formulation so that differential and algebraic constraints can be implemented in thesame C++ function

bull Easy to use interface to GNUplot to produce graphical output including 2D plots 3Dcurves and surfaces and polar plots

bull Automatic generation of LaTeX code to produce a table that summarizes the mesh refine-ment process

Full details on PSOPT and its features can be found in its documentation17

182 GPOPS-II - MATLAB Optimal Control Software

GPOPS-II is a general purpose optimal control software available in the office site18 GPOPS-II is a new open-source MATLAB optimal control software that implements a brand new hp-adaptive Legendre-Gauss-Radau quadrature integral pseudospectral method for solving generalnonlinear optimal control problems Using GPOPS-II the optimal control problem is transcribedto a nonlinear programming problem (NLP) The NLP is then solved using either the solverSNOPT or the solver IPOPT

The following are some the key features of GPOPS-II

bull Allows for an extremely general formulation of the optimal control problem

bull Allows for inclusion of integral constraints and highly general boundary conditions

bull Complete first and second sparse finite-differencing of optimal control problem to com-pute all derivatives required by the NLP solver

17httpcodegooglecomppsoptdownloadsdetailname=PSOPT_Manual_R3pdfampcan=2ampq=18httpwwwgpopsorg

14

bull The latest advances in mesh refinement including hpndashadaptive pseudospectral methods

bull Gaussian quadrature integration methods for rapid convergence

bull Highly accurate costate estimation

bull Inclusion of the NLP solver SNOPT (for Academic Users) and IPOPT (for Not-for-Profitand Commercial Users)

bull No third-party products other than MATLAB are required

More information about the methodology used in GPOPS-II can be found by reading therelevant articles in the open literature19

The fees for using GPOPS-II are as follows

bull For students and all others employed at academic institutions NO CHARGE

bull US Federal State or Local Government NO CHARGE

bull Users at not-for-profit institutions (including non-US Government agencies) or commer-cial institutions LICENSING FEES APPLY

19httpvdolmaeufledu

15

16

Chapter 2

Nonlinear Programming Solvers

21 Introduction

A solver is a software used to compute numerical solution of an optimal control problemIn general an optimal controlnonlinear programming interface is needed to prepare all dataas input to the solver (see Figure 21) Several solvers will be presented in this chapter evolvingopenndashsource freeware and commercial software working under different operating systemsSince all solvers require configuration lists of parameters will be presented for some of them

Figure 21

22 IPOPT ndash Interior Point OPTimizer

ldquoIPOPT is a software package for largendashscale nonlinear optimisation It is designed to find(local) solutions of mathematical optimisation problemsrdquo The IPOPT library can be found inhttpwwwartelyscom1

221 Basic information

Developed by Andreas Wachter and Carl Laird

Operating system(s) IPOPT can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) IPOPT is written in C++ and is released as open source code under theEclipse Public License (EPL)

1httpwwwartelyscomindexphppage=knitroamphl=en_EN

17

Modeling language interface(s) IPOPT can be used as a library that can be linked to C++ C orFortran code as well as a solver executable for the AMPL modelling environment Thepackage includes interfaces to CUTEr optimisation testing environment as well as theMATLAB and R programming environments

222 Installing IPOPT

How to install IPOPT on Linux

Getting the IPOPT code via subversion

sudo svn co httpsprojectscoin-ororgsvnIpoptstable310 optCoinIpopt

This will create a directory named CoinIpopt in your opt folder

BLAS routines that provide standard building blocks for performing basic vector and matrixoperations

LAPACK routines for solving systems of simultaneous linear equations least-squares solutionsof linear systems of equations eigenvalue problems and singular value problems

HSL state-of-the-art packages for large-scale scientific computation

ASL AMPL Solver Library allows to read the nl files and provides the automatic differentia-tion functionality

MUMPS MUltifrontal Massively Parallel sparse direct Solver

METIS programs for partitioning graphs partitioning finite element meshes and producing fillreducing orderings for sparse matrices

To install IPOPT just run

cd optCoinIpoptThirdPartyBlas

getBlas

cd Lapack

getLapack

cd ASL

getASL

cd Mumps

getMumps

cd Metis

getMetis

IPOPT requires a sparse symmetric linear solver There are different possibilities The userrsquosguide of IPOPT suggests that you use HSL subroutines

1 Go to HSL website2 and find the package list

2 Click on MA27 read the license and submit the registration form

3 Go back to the package list

4 Click on MC19 read the license and submit the registration form

2httphslrlacukarchivehslarchivehtml

18

You will receive in your email the links to obtain the HSL packages Download them to your$HOME directory and run

gunzip ma27-100targz

tar xvf ma27-100tar

gunzip mc19-100targz

tar xvf mc19-100tar

cd optCoinIpoptThirdPartyHSL

sudo mv -r $HOMEmc19-100

sudo mv -r $HOMEma27-100

cd mc19-100

sudo configure

sudo make install

sudo make check

cd ma27-100

sudo configure

sudo make install

sudo make check

The make check command will check if the installation of the HSL packages was successfulReturn to the optCoinIpopt directory and continue the IPOPT installation

cd

sudo mkdir build

cd build

sudo configure

sudo make test

sudo make install

If you get the error message ldquoerrorlsquoNULLrsquo was not declared in this scoperdquo when run-ning sudo make then you have to edit the following filesoptCoinIpoptIpoptsrcCommonIpSmartPtrhpp

optCoinIpoptIpoptsrcAlgorithmLinearSolversIpTripletToCSRConvertercpp

by adding include ltcstddefgt

How to install IPOPT on Mac OS X

In a Terminal window install homebrewusrbinruby -e $(usrbincurl -fsSL httpsrawgithubcommxclhomebrewmaster

LibraryContributionsinstall homebrewrb)

With this software you can easily install dependency packages like wget and gfortran

brew install wget

brew install gfortran

sudo wget httpwwwcoin-ororgdownloadsourceIpoptIpopt-3102zip

sudo unzip Ipopt-3102zip

cd Ipopt-3102

Download Netlib BlasLapack

cd optCoinIpoptThirdPartyBlas

sudo getBlas

19

cd Lapack

sudo getLapack

cd ASL

sudo getASL

cd Mumps

sudo getMumps

cd Metis

sudo getMetis

Make a build for a 64bit target

sudo mkdir build64

cd build64

sudo configure --disable-shared --with-blas=BUILD --with-lapack=BUILD F77=gfortran

FFLAGS=-fexceptions -m64 -fbackslash CFLAGS=-fno-common -no-cpp-precomp -fexceptions

-arch x86 64 -m64 CXXFLAGS=-fno-common -no-cpp-precomp -fexceptions -arch x86 64

-m64

Compile test and install

make

make test

make install

Add the following to your bashrc or bash profile

IPOPTDIR=optCoinIpoptbuild

export PATH=$PATH$IPOPTDIR$IPOPTDIRbinexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyASLexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyBlaslibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyHSLlibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyLapacklibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMetislibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMumpslibs

How to install the Matlab interface on Linux

First of all be sure

cd optmatlabR2008asysosglnx86

mv libgcc sso1 libgcc sso1bck

ln -s libi386-linux-gnulibgcc sso1 libgcc sso1

mv libstdc++so608 libstdc++so608bck

ln -s usrlibi386-linux-gnulibstdc++so6 libstdc++so608

Go to the subdirectory optCoinIpoptbuildIpoptcontribMatlabInterfacesrc andopen the Makefile file

sudo apt-get install hardening-wrapper

You need to edit this file to suit the installation for your system setup You may need tomodify MATLAB HOME MEXSUFFIX and MEX as explained in the comments of the Makefile I setthese variables as follows

20

MATLAB HOME = optmatlabR2008a

MEXSUFFIX = mexglx

MEX = optmatlabR2008abinmex

In my case I removed some of the compiler options as it follows

prefix = optCoinIpopt

CXX = optintelcomposer xe 2011 sp19293binia32icpc

CCXXFLAGS = -O3 -pipe -DNDEBUG -Wparentheses -Wreturn-type -Wcast-qual -Wall

-Wpointer-arith -Wwrite-strings -Wconversion -Wno-unknown-pragmas -DMATLAB MEXFILE

LDFLAGS = $(CXXFLAGS) -Wl --rpath -Wl $prefixlibcoin -Wl --rpath -Wl

--rpath -Wl$prefixlibcoinThirdParty

In my case I had to set explicitly the include Matlab folder

o cpp

$(CXX) $(CXXFLAGS) $(INCL) -IoptmatlabR2008aexterninclude -o $ -c $^

Once you set up the Makefile properly type sudo make install in the same directory as theMakefile If the installation procedure was successful you will end up with a MEX file calledipoptmexglx In order to use it in MATLAB you need to tell MATLAB where to find it Todo this just type addpath optCoinIpoptlib in the MATLAB command window after youcheck if the ipoptmexglx file is in this folder

Documentation

More informations can be found in the following documentation

Online documentation httpwwwcoin-ororgIpoptdocumentation3

A PDF version of this documentation can be downloaded here4

A short IPOPT tutorial can be downloaded here5

MATLAB interface Instructions specific to the MATLAB interface can be found at the Matlabinterface page6

223 Getting started with IPOPT and AMPL

option solver ipopt

option ipopt options rsquooption1=value1 option2=value2rsquo

solve

3httpwwwcoin-ororgIpoptdocumentation4httpsprojectscoin-ororgIpoptbrowserstable39Ipoptdocdocumentationpdfformat=raw5httpdropsdagstuhldevolltexte20092089pdf09061WaechterAndreasPaper2089pdf6httpsprojectscoin-ororgIpoptwikiMatlabInterface

21

Table 21 IPOPT directives for AMPL

Option Description

acceptable compl inf tol Acceptance threshold for the complementarity conditionsacceptable constr viol tol Acceptance threshold for the constraint violationacceptable dual inf tol Acceptance threshold for the dual infeasibilityacceptable tol Acceptable convergence tolerance (relative)alpha for y Step size for constraint multipliersbound frac Desired minimal relative distance of initial point to boundbound mult init val Initial value for the bound multipliersbound push Desired minimal absolute distance of initial point to boundbound relax factor Factor for initial relaxation of the boundscompl inf tol Acceptance threshold for the complementarity conditionsconstr mult init max Maximal allowed least-square guess of constraint multipliersconstr viol tol Desired threshold for the constraint violationdiverging iterates tol Threshold for maximal value of primal iteratesdual inf tol Desired threshold for the dual infeasibilityexpect infeasible problem Enable heuristics to quickly detect an infeasible problemfile print level Verbosity level for output filehalt on ampl error Exit with message on evaluation errorhessian approximation Can enable Quasi-Newton approximation of hessianhonor original bounds If no solution might slightly violate boundslinear scaling on demand Enables heuristic for scaling only when seems requiredlinear solver Linear solver to be used for step calculationlinear system scaling Method for scaling the linear systemsma27 pivtol Pivot tolerance for the linear solver MA27ma27 pivtolmax Maximal pivot tolerance for the linear solver MA27ma57 pivtol Pivot tolerance for the linear solver MA57ma57 pivtolmax Maximal pivot tolerance for the linear solver MA57

22

Table 22 IPOPT directives for AMPL

Option Description

max cpu time CPU time limitmax iter Maximum number of iterations

max refinement stepsMaximal number of iterative refinement steps per linearsystem solve

max soc Maximal number of second order correction trial stepsmaxit Maximum number of iterations (same as max iter)

min refinement stepsMinimum number of iterative refinement steps per linearsystem solve

mu init Initial value for the barrier parametermu max Maximal value for barrier parameter for adaptive strategymu oracle Oracle for a new barrier parameter in the adaptive strategymu strategy Update strategy for barrier parameternlp scaling max gradient Maximum gradient after scalingnlp scaling method Select the technique used for scaling the NLPobj scaling factor Scaling factor for the objective functionoption file name File name of options file (default ipoptopt)outlev Verbosity level (same as print level)output file File name of an output file (leave unset for no file output)pardiso matching strategy Matching strategy for linear solver Pardisopardiso out of core power Enables out-of-core version of linear solver Pardisoprint level Verbosity levelprint options documentation Print all available options (for ipoptopt)print user options Toggle printing of user optionsrequired infeasibility reduction Required infeasibility reduction in restoration phaseslack bound frac Desired minimal relative distance of initial slack to boundslack bound push Desired minimal absolute distance of initial slack to boundtol Desired convergence tolerance (relative)

wantsol

solution report without -AMPL sum of1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

warm start bound pushEnables to specify how much should variables should bepushed inside the feasible region

warm start init point Enables to specify bound multiplier values

warm start mult bound pushEnables to specify how much should bound multipliersshould be pushed inside the feasible region

watchdog shortened iter trigger Trigger counter for watchdog procedurewsmp num threads Number of threads to be used in WSMPwsmp pivtol Pivot tolerance for the linear solver WSMPwsmp pivtolmax Maximum pivot tolerance for the linear solver WSMPwsmp scaling Determines how the matrix is scaled by WSMP

23

24

23 KNITRO

ldquoKNITRO is an optimization software library for finding solutions of both continuous (smo-oth) optimization models (with or without constraints) as well as discrete optimization modelswith integer or binary variables (ie mixed integer programs) KNITRO is primarily designedfor finding local optimal solutions of large-scale continuous nonlinear problemsrdquo The KNITROlibrary can be found in httpwwwartelyscom7

231 Basic information

KNITRO is a software package for solving smooth optimization problems with or withoutconstraints

Developed at Ziena Optimization

Operating system(s) KNITRO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) KNITRO is written C C++ Fortran and Java

Programming interfaces(s) Fortan CC++ Java and Microsoft Excel

Modeling language interface(s) KNITRO offers a total of five interfaces Matlab AMPL Math-ematica AIMMS GAMS and MPL

232 Installing KNITRO on Linux

How to get a license

KNITRO is developed by Ziena Optimization LLC and marketed and supported by ArtelysThe first step is to request a license by selecting the download tab8 Along with the academicand commercial trial versions there is a student version limited in problem size (300 variablesand 300 constraints) available for 6 months Each license is personalised hence among otherpersonal information you should inform about

bull the operating system (OS)

bull the OS bit architecture

bull the Machine ID of the computer on which KNITRO will runDownload extract an script available bellow the Machine ID field

After reading and accepting the End User License Agreement the download of the KNITROwill be available and you will get a license key on your email Then you should put the licensefile whose name begins with ziena lic in your $HOME directory or in the $HOMEzienadirectory

How to install

The KNITRO software package for Unix is delivered as a gzipped tar file After downloadingKNITRO you need to unpack it Type in a Terminal window

gunzip knitro-8x-platformnametargz

tar -xvf knitro-8x-platformnametar

sudo mv knitro-8x-z opt

7httpwwwartelyscomindexphppage=knitroamphl=en_EN8httpwwwartelyscomindexphppage=knitroamphl=en_ENdownloads-tab

25

This will create a directory named knitro-8x-z in your opt folder

In order to run KNITRO binary or executable files from anywhere on your Linux computerit is necessary to set several environment variables In particular you must update the PATH

environment variable so that it indicates the location of the knitroampl directory You mustalso update the LD LIBRARY PATH environment variable so that it indicates the location of theKNITRO lib directory Setting the PATH and LD LIBRARY PATH environment variables on a Linuxsystem can be done as follows

echo rsquoexport PATH=$PATHoptknitro-8x-zrsquo gtgt $HOMEbashrcecho rsquoexport PATH=$PATHoptknitro-8x-zknitroamplrsquo gtgt $HOMEbashrcecho rsquoexport LD LIBRARY PATH=$LD LIBRARY PATHoptknitro-8x-zlibrsquo gtgt $HOMEbashrc

Each Matlab release expects the KNITRO library file on Linux to have a specific name There-fore to use KNITRO 8x or later with Matlab release R2008a through 2011b you must createa symbolic link to the expected name Open a Terminal window and in the KNITRO librarydirectory issue the command

bull for Matlab releases R2008a R2008b or R2009aln -s libknitroso800 libknitroso5

bull for Matlab releases R2009b R2010a or R2010bln -s libknitroso800 libknitroso6

bull for Matlab releases R2011a R2011b or R2012aln -s libknitroso800 libknitroso7

Documentation

More informations can be found in the following documentation9

KNITRO 80 User Manual Knitro80 UserManualpdfVersion December 2011 (816 KB)

Specific KNITROMatlab interface documentation can be found on the Matlab OptimizationToolbox webpages10

233 Getting started with KNITRO and AMPL

option solver knitroampl

option knitro options rsquooption1=value1 option2=value2rsquo

solve

9httpwwwartelyscomuploadspdfsKnitro80_UserManualpdf10httpwwwmathworkscomaccesshelpdeskhelptoolboxoptim

26

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 2: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

2

Contents

1 OC amp NLP Interfaces 711 Introduction 712 AMPL 713 ACADO ndash Automatic Control And Dynamic Optimization 814 BOCOP ndash The optimal control solver 915 DIDO ndash Automatic Control And Dynamic Optimization 1016 ICLOCS ndash Imperial College London Optimal Control Software 1217 TACO ndash Toolkit for AMPL Control Optimization 1218 Pseudospectral Methods in Optimal Control 13

2 NLP Solvers 1721 Introduction 1722 IPOPT ndash Interior Point OPTimizer 1723 KNITRO 2524 WORHP ndash WORHP Optimises Really Huge Problems 3125 Other Commercial Packages 33

3 Project webpage 35

4 Optimal Control Toolbox 37

5 Applications 3951 CarndashLike 3952 Goddard Problem 4353 HIV 44

A Programming code 47A1 Optimal Control Toolbox MATLAB Code 47A2 CarndashLike AMPL Code 55A3 HIV ICLOCS Code 57

3

4

Work Plan

Project PTDCEEA-CRO1160142009 ndash ldquoOptimal Control for Constrained Hybrid Systemrdquo

The main tasks to be carried out are

1 To understand and obtain a handsndashon knowledge of nonndashlinear programming models cur-rent tools to address them (AMPL MATLAB ) and current solvers (KNITRO WORHPSNOPT ) Comparative study of different solvers

2 To understand optimal control problems and how to transcribe them into NLP to be im-plemented in AMPL or MATLAB

3 To obtain a handsndashon knowledge of current optimal control packages such DIDO ACADOOCPID-DAE ASTOS BOCOP and others To perform a comparative study

4 To help to implement and solve case studies and tests to illustrate theoretical develop-ments

5 To maintain the project webndashpage and documentation accessible to the project team

6 (If time permits) To construct a platform to easily solve sequences of optimal control prob-lems in a receding horizon fashion in order to implement model predictive controllers

TODAY

Months 0 1 2 3 4 5 6 7 8 9 10 11 12

100 completeTask 1

100 completeTask 2

100 completeTask 3

100 completeTask 4

100 completeTask 5

50 completeTask 6

Progress reports on tasks just concluded and ongoing tasks should be produced at the endof months 3 5 7 10 and 12

5

6

Chapter 1

Optimal Control andNonlinear Programming Interfaces

11 Introduction

Optimal control and nonlinear programming interfaces are software used to solve optimalcontrol problems A interface is a software that help us to prepare all data as input to the solverWe can divide the interfaces in two groups optimal control interfaces and nonlinear program-ming interfaces The first ones handle with the discretisation and transcription procedureswhile the other ones leave this work to the programmerresearcher (see Figure 11) Severalinterfaces will be presented in this chapter evolving openndashsource freeware and commercialsoftware working under different operating systems

Figure 11

12 AMPL

ldquoAMPL ndash A Mathematical Programming Language ndash is a comprehensive and powerful alge-braic modeling language for linear and nonlinear optimization problems in discrete or contin-uous variablesrdquo The AMPL library can be found in the official site1

121 Basic information

Developed by Robert Fourer David Gay and Brian Kernighan at Bell Laboratories1httpwwwanmplcom

7

Operating system(s) AMPL can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) AMPL is written in C++ and it is released as open source code under theEclipse Public License (EPL)

122 Installing AMPL on Linux

Download your AMPL executable by selecting the Intel (Pentium-compatible) PCs runningLinux on AMPL website2 or type in a Terminal window

wget httpnetlibsandiagovamplstudentlinuxamplgz

The downloaded file must be decompressed and it must be made executable after which itmay be executed from any directory in your search path

gzip -d amplgz

chmod +x ampl

sudo mkdir -p optampl

sudo mv ampl optampl

echo rsquoEXPORT PATH=$PATHoptamplrsquo gtgt $HOMEbashrc

To complete your AMPL installation you can also download solvers to a directory in yoursearch path To use a downloaded solver set AMPLrsquos solver option to the name of the solverrsquosexecutable file

The student edition of AMPL is limited to 300 variables and 300 constraints and objectives(after presolve) for nonlinear problems

123 Getting started with AMPL

The first step in solving a problem is to load the model variables objective function andconstraints It is usual the create a mod file containing these informations First open a AMPLcommand window by typing ampl in a Terminal window

AMPL commands end with semicolon () Some AMPL basic commands

bull to declare a parameter param ltpar namegt

bull to load a model model ltfile namegtmod

bull to run a AMPL script include ltscript namegtrun

bull to load a data file data ltfile namegtdat

bull to view the content of a variable display ltvar namegt

bull to exit a AMPL command window exit

13 ACADO ndash Automatic Control And Dynamic Optimization

ldquoACADO Toolkit is a software environment and algorithm collection for automatic controland dynamic optimization It provides a general framework for using a great variety of al-gorithms for direct optimal control including model predictive control state and parameterestimation and robust optimizationrdquo The ACADO library can be found in the official site3

2httpnetlibsandiagovamplstudentlinuxamplgz3httpsourceforgenetprojectsacado

8

131 Basic information

Developed under the direction of Moritz Diehl

Operating system(s) ACADO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) ACADO Toolkit is implemented as self-contained C++ code it comes alongwith user-friendly MATLAB interface and is released under the LGP License

132 Installing ACADO on Linux

In order to install ACADO Toolkit under LINUXUNIX operating systems make sure thata C++ compiler is correctly installed For the case that a graphical output is desired Gnuplotshould be installed in addition However ACADO Toolkit will also run without Gnuplot - inthis case the graphical output is simply turned off There are no further dependencies

First download ACADO Toolkit and its licence from the offical website4 and extract the filestar xfvz ACADOtoolkit-102613betatargz

sudo mv ACADOtoolkit-102613beta optacado

Then go to the directory optacado and compile the packagecd optacado

sudo make

sudo make doc (optional)

Finally check whether the installation was successfulcd examplesgetting started

simple ocp

The following documentation5 is available

bull ACADO Toolkit Userrsquos Manual

bull ACADO for Matlab Userrsquos Manual

bull ACADO Toolkit Introductory Talk

14 BOCOP ndash The optimal control solver

ldquoThe BOCOP project aims to develop an open-source toolbox for solving optimal controlproblemsrdquo The BOCOP library can be found in the official site6

141 Basic information

Developed by V Grelard P Martinon and F Bonnans at Inria-Saclay

Operating system(s) BOCOP is available for linux precompiled packages (Mac and Windowsversions are still under testing)

Code language(s) BOCOP requires SciLab and it is released under the Eclipse Public License(EPL)

4httpwwwacadotoolkitorgdownloadphp5httpsourceforgenetpacadowikiDocumentation6httpwwwbocoporg

9

142 Installing BOCOP on Linux

In order to install BOCOP software under LINUXUNIX operating system download BO-COP software from the official website7 and extract the files

tar xvf bocop-103-beta-linux32targz

mv bocop-103-beta-linux32 optbocop

cd optbocop

make

sudo chmod +x bocopsh

Add the following to your bashrc or bash profile

BOCOPDIR=optbocop

export PATH=$PATH$BOCOPDIR

143 Getting started with BOCOP

Read the userrsquos guide8 and run the examples shown in the optbocopproblems folder

15 DIDO ndash Automatic Control And Dynamic Optimization

ldquoDIDO the leading optimal control software powers users by offering the easiest and directsolutions to the most complex problemsrdquo The DIDO software can be found in the official site9

151 Basic information

Developed at Elissar Global

Operating system(s) DIDO can be used on Microsoft Windows

Code language(s) DIDO requires MATLAB and it is released under academic and commerciallicenses

152 Installing DIDO

DIDOrsquos foundation is pseudospectral theory and is the only pseudospectral solution withmathematically proven convergence properties DIDO is a MATLAB program for solving hybridoptimal control problems The generalndashpurpose program is named after Dido the legendaryfounder and first queen of Carthage who is famous in mathematics for her remarkable solutionto a constrained optimal control problem even before the invention of calculus

To install DIDO on your computer you should consider the system requirements

bull WINDOWS

bull MATLAB

and the following steps

7httpwwwbocoporg8httpbocopsaclayinriafrdownload=109httpwwwelissarglobalcomindustryproductssoftware-3

10

STEP 1 Obtain a license for DIDO from Elissar Global at the official site10

STEP 2 After you receive an email with the link to a compressed version of DIDO downloadthe zip file and uncompress it It will automatically uncompress with the right folderstructure

STEP 3 Right click on the DIDO icon and click on PROPERTIES In the Target box type locationof your licensed MATLAB file In the Start in box type the full path name of your DIDOfolder

STEP 4 Doublendashclick on the DIDO ICON (this should start MATLAB and automatically initial-ize DIDO)

(a) In the MATLAB command window type TestDIDOA successful installation should produce the final output on the screenldquoCONGRATULATIONS DIDO TEST WAS SUCCESSFULrdquo

(b) A further test may be performed by typing LanderProblemA successful installation should produce a pretty graph

153 Getting started with DIDO

In the MATLAB command window run[cost primal] = dido(Problem)

where Problem is a structure array with fields cost dynamics events and path that point to theinput files

Problemcost = lsquoMyCostFilersquo

Problemdynamics = lsquoMyDynamicsFilersquo

Problemevents = lsquoMyEventsFilersquo

Problempath = lsquoMyPathFilersquo

The variable primal is used in nearly all DIDO files to pass the states controls parametersand other useful quantities To print out the statendash and controlndashtrajectory in the MATLABcommand window runprimalstates primalcontrols

This generality allows for

bull Fairly complex interior point constraints

bull Pre-defined segments

bull Differentially-flat segments

bull Transition conditions

bull Midndashmaneuver changes in dynamics

bull Multi-dynamical systems

bull Midndashmaneuver changes in the cost function

bull Switches

bull Discrete events10httpwwwelissarglobalcomacademicget-didotry-dido

11

16 ICLOCS ndash Imperial College London Optimal Control Software

ldquoThe code allows users to define and solve optimal control problems with general path andboundary constraints and free or fixed final time It is also possible to include constant designparameters as unknownsrdquo The ICLOCS software can be found in the official site11

161 Basic information

Developed by Paola Falugi Eric Kerrigan and Eugene van Wyk

Operating system(s) ICLOCS can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) ICLOCS is implemented in MATLAB and it is released as open source codeunder the BSD License

162 Installing ICLOCS on Linux

In order to install ICLOCS software under LINUXUNIX operating system download ICLOCSsoftware from the official site12 and extract the files

wget httpwwweeicacukICLOCSICLOCS 02 NItarzip

unzip ICLOCS 02 NItarzip

tar xvf ICLOCS 02 NItar

sudo mv ICLOCS 02 NI opticlocs

In order to use it in MATLAB you need to tell MATLAB where to find it To do this justtype addpath(genpath(rsquoopticlocsrsquo)) in the MATLAB command window

163 Getting started with ICLOCS

Read the userrsquos guide13 and run the examples shown in the opticlocsexamples folder

17 TACO ndash Toolkit for AMPL Control Optimization

ldquoTACO is the Toolkit for AMPL Control Optimization It defines some add-ons to the AMPLmodeling language that allow the elegant formulation of ODEDAE optimal control problemsin AMPLrdquo The TACO toolkit can be found in the official site14

171 Basic information

Developed by Christian Kirches and Sven Leyffer

Operating system(s) TACO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) TACO is written in C

11httpwwweeicacukICLOCS12httpwwweeicacukICLOCS13httpwwweeicacukICLOCSuser_guidepdf14httpwwwiwruni-heidelbergde~ChristianKirchessoftwarehtml

12

172 Installing TACO on Linux

In order to install TADO Toolkit under LINUXUNIX operating systems make sure thatCMAKE and a C++ compiler is correctly installed To install CMAKE type in a Terminal win-dow

wget httpslaunchpadnetubuntu+archiveprimary+filescmake 288origtargz

gunzip cmake 288origtargz

tar xvf cmake 288origtar

cd cmake-288

sudo bootstrap

sudo make

sudo make install

First download TACO Toolkit from the offical site15 and extract the files

wget httpswwwiwruni-heidelbergdegroupsagbockFILEStaco sourcetargzgunzip taco sourcetargz

tar xfvz taco sourcetar

sudo ln -s optCoinIpoptThirdPartyASLlibamplsolvera libamplsolvera

18 Pseudospectral Methods in Optimal Control

181 PSOPT

PSOPT is an open source optimal control software package written in C++ that uses directcollocation methods including pseudospectral and local discretizations available in the officesite16 Pseudospectral methods solve optimal control problems by approximating the time-dependent variables using global polynomials such as Legendre or Chebyshev functions Localdiscretisation methods approximate the time dependent functions using local splines and canbe seen as implementations of implicit Runge-Kutta integrators With both global and localmethods differential equations continuous constraints and integrals associated with the prob-lem are discretised over a grid of nodes Sparse nonlinear programming is then used to findlocal optimal solutions

PSOPT is able to deal with problems with the following characteristics

bull Single or multiphase problems

bull Continuous time nonlinear dynamics

bull Nonlinear path constraints

bull General event constraints

bull Integral constraints

bull Interior point constraints

bull Bounds on controls and state variables

bull General cost function with Lagrange and Mayer terms

15httpwwwiwruni-heidelbergde~ChristianKirchessoftwarehtml16httpwwwpsoptorg

13

bull Linear or nonlinear linkages between phases

bull Fixed or free initial phase time

bull Fixed or free final phase time

bull Optimization of static parameters

bull Optimal parameter estimation given sampled observations

The implementation has the following features

bull Choice between Legendre Chebyshev central differences trapezoidal or Hermite-Simpsondiscretisation

bull Large scale nonlinear programming using IPOPT and (optionally) SNOPT

bull Estimation of the discretisation error

bull Automatic mesh refinement

bull Automatic scaling

bull Automatic differentiation using the ADOL-C library

bull Numerical differentiation by using sparse finite differences

bull Automatic identification of the sparsity of the derivative matrices

bull DAE formulation so that differential and algebraic constraints can be implemented in thesame C++ function

bull Easy to use interface to GNUplot to produce graphical output including 2D plots 3Dcurves and surfaces and polar plots

bull Automatic generation of LaTeX code to produce a table that summarizes the mesh refine-ment process

Full details on PSOPT and its features can be found in its documentation17

182 GPOPS-II - MATLAB Optimal Control Software

GPOPS-II is a general purpose optimal control software available in the office site18 GPOPS-II is a new open-source MATLAB optimal control software that implements a brand new hp-adaptive Legendre-Gauss-Radau quadrature integral pseudospectral method for solving generalnonlinear optimal control problems Using GPOPS-II the optimal control problem is transcribedto a nonlinear programming problem (NLP) The NLP is then solved using either the solverSNOPT or the solver IPOPT

The following are some the key features of GPOPS-II

bull Allows for an extremely general formulation of the optimal control problem

bull Allows for inclusion of integral constraints and highly general boundary conditions

bull Complete first and second sparse finite-differencing of optimal control problem to com-pute all derivatives required by the NLP solver

17httpcodegooglecomppsoptdownloadsdetailname=PSOPT_Manual_R3pdfampcan=2ampq=18httpwwwgpopsorg

14

bull The latest advances in mesh refinement including hpndashadaptive pseudospectral methods

bull Gaussian quadrature integration methods for rapid convergence

bull Highly accurate costate estimation

bull Inclusion of the NLP solver SNOPT (for Academic Users) and IPOPT (for Not-for-Profitand Commercial Users)

bull No third-party products other than MATLAB are required

More information about the methodology used in GPOPS-II can be found by reading therelevant articles in the open literature19

The fees for using GPOPS-II are as follows

bull For students and all others employed at academic institutions NO CHARGE

bull US Federal State or Local Government NO CHARGE

bull Users at not-for-profit institutions (including non-US Government agencies) or commer-cial institutions LICENSING FEES APPLY

19httpvdolmaeufledu

15

16

Chapter 2

Nonlinear Programming Solvers

21 Introduction

A solver is a software used to compute numerical solution of an optimal control problemIn general an optimal controlnonlinear programming interface is needed to prepare all dataas input to the solver (see Figure 21) Several solvers will be presented in this chapter evolvingopenndashsource freeware and commercial software working under different operating systemsSince all solvers require configuration lists of parameters will be presented for some of them

Figure 21

22 IPOPT ndash Interior Point OPTimizer

ldquoIPOPT is a software package for largendashscale nonlinear optimisation It is designed to find(local) solutions of mathematical optimisation problemsrdquo The IPOPT library can be found inhttpwwwartelyscom1

221 Basic information

Developed by Andreas Wachter and Carl Laird

Operating system(s) IPOPT can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) IPOPT is written in C++ and is released as open source code under theEclipse Public License (EPL)

1httpwwwartelyscomindexphppage=knitroamphl=en_EN

17

Modeling language interface(s) IPOPT can be used as a library that can be linked to C++ C orFortran code as well as a solver executable for the AMPL modelling environment Thepackage includes interfaces to CUTEr optimisation testing environment as well as theMATLAB and R programming environments

222 Installing IPOPT

How to install IPOPT on Linux

Getting the IPOPT code via subversion

sudo svn co httpsprojectscoin-ororgsvnIpoptstable310 optCoinIpopt

This will create a directory named CoinIpopt in your opt folder

BLAS routines that provide standard building blocks for performing basic vector and matrixoperations

LAPACK routines for solving systems of simultaneous linear equations least-squares solutionsof linear systems of equations eigenvalue problems and singular value problems

HSL state-of-the-art packages for large-scale scientific computation

ASL AMPL Solver Library allows to read the nl files and provides the automatic differentia-tion functionality

MUMPS MUltifrontal Massively Parallel sparse direct Solver

METIS programs for partitioning graphs partitioning finite element meshes and producing fillreducing orderings for sparse matrices

To install IPOPT just run

cd optCoinIpoptThirdPartyBlas

getBlas

cd Lapack

getLapack

cd ASL

getASL

cd Mumps

getMumps

cd Metis

getMetis

IPOPT requires a sparse symmetric linear solver There are different possibilities The userrsquosguide of IPOPT suggests that you use HSL subroutines

1 Go to HSL website2 and find the package list

2 Click on MA27 read the license and submit the registration form

3 Go back to the package list

4 Click on MC19 read the license and submit the registration form

2httphslrlacukarchivehslarchivehtml

18

You will receive in your email the links to obtain the HSL packages Download them to your$HOME directory and run

gunzip ma27-100targz

tar xvf ma27-100tar

gunzip mc19-100targz

tar xvf mc19-100tar

cd optCoinIpoptThirdPartyHSL

sudo mv -r $HOMEmc19-100

sudo mv -r $HOMEma27-100

cd mc19-100

sudo configure

sudo make install

sudo make check

cd ma27-100

sudo configure

sudo make install

sudo make check

The make check command will check if the installation of the HSL packages was successfulReturn to the optCoinIpopt directory and continue the IPOPT installation

cd

sudo mkdir build

cd build

sudo configure

sudo make test

sudo make install

If you get the error message ldquoerrorlsquoNULLrsquo was not declared in this scoperdquo when run-ning sudo make then you have to edit the following filesoptCoinIpoptIpoptsrcCommonIpSmartPtrhpp

optCoinIpoptIpoptsrcAlgorithmLinearSolversIpTripletToCSRConvertercpp

by adding include ltcstddefgt

How to install IPOPT on Mac OS X

In a Terminal window install homebrewusrbinruby -e $(usrbincurl -fsSL httpsrawgithubcommxclhomebrewmaster

LibraryContributionsinstall homebrewrb)

With this software you can easily install dependency packages like wget and gfortran

brew install wget

brew install gfortran

sudo wget httpwwwcoin-ororgdownloadsourceIpoptIpopt-3102zip

sudo unzip Ipopt-3102zip

cd Ipopt-3102

Download Netlib BlasLapack

cd optCoinIpoptThirdPartyBlas

sudo getBlas

19

cd Lapack

sudo getLapack

cd ASL

sudo getASL

cd Mumps

sudo getMumps

cd Metis

sudo getMetis

Make a build for a 64bit target

sudo mkdir build64

cd build64

sudo configure --disable-shared --with-blas=BUILD --with-lapack=BUILD F77=gfortran

FFLAGS=-fexceptions -m64 -fbackslash CFLAGS=-fno-common -no-cpp-precomp -fexceptions

-arch x86 64 -m64 CXXFLAGS=-fno-common -no-cpp-precomp -fexceptions -arch x86 64

-m64

Compile test and install

make

make test

make install

Add the following to your bashrc or bash profile

IPOPTDIR=optCoinIpoptbuild

export PATH=$PATH$IPOPTDIR$IPOPTDIRbinexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyASLexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyBlaslibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyHSLlibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyLapacklibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMetislibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMumpslibs

How to install the Matlab interface on Linux

First of all be sure

cd optmatlabR2008asysosglnx86

mv libgcc sso1 libgcc sso1bck

ln -s libi386-linux-gnulibgcc sso1 libgcc sso1

mv libstdc++so608 libstdc++so608bck

ln -s usrlibi386-linux-gnulibstdc++so6 libstdc++so608

Go to the subdirectory optCoinIpoptbuildIpoptcontribMatlabInterfacesrc andopen the Makefile file

sudo apt-get install hardening-wrapper

You need to edit this file to suit the installation for your system setup You may need tomodify MATLAB HOME MEXSUFFIX and MEX as explained in the comments of the Makefile I setthese variables as follows

20

MATLAB HOME = optmatlabR2008a

MEXSUFFIX = mexglx

MEX = optmatlabR2008abinmex

In my case I removed some of the compiler options as it follows

prefix = optCoinIpopt

CXX = optintelcomposer xe 2011 sp19293binia32icpc

CCXXFLAGS = -O3 -pipe -DNDEBUG -Wparentheses -Wreturn-type -Wcast-qual -Wall

-Wpointer-arith -Wwrite-strings -Wconversion -Wno-unknown-pragmas -DMATLAB MEXFILE

LDFLAGS = $(CXXFLAGS) -Wl --rpath -Wl $prefixlibcoin -Wl --rpath -Wl

--rpath -Wl$prefixlibcoinThirdParty

In my case I had to set explicitly the include Matlab folder

o cpp

$(CXX) $(CXXFLAGS) $(INCL) -IoptmatlabR2008aexterninclude -o $ -c $^

Once you set up the Makefile properly type sudo make install in the same directory as theMakefile If the installation procedure was successful you will end up with a MEX file calledipoptmexglx In order to use it in MATLAB you need to tell MATLAB where to find it Todo this just type addpath optCoinIpoptlib in the MATLAB command window after youcheck if the ipoptmexglx file is in this folder

Documentation

More informations can be found in the following documentation

Online documentation httpwwwcoin-ororgIpoptdocumentation3

A PDF version of this documentation can be downloaded here4

A short IPOPT tutorial can be downloaded here5

MATLAB interface Instructions specific to the MATLAB interface can be found at the Matlabinterface page6

223 Getting started with IPOPT and AMPL

option solver ipopt

option ipopt options rsquooption1=value1 option2=value2rsquo

solve

3httpwwwcoin-ororgIpoptdocumentation4httpsprojectscoin-ororgIpoptbrowserstable39Ipoptdocdocumentationpdfformat=raw5httpdropsdagstuhldevolltexte20092089pdf09061WaechterAndreasPaper2089pdf6httpsprojectscoin-ororgIpoptwikiMatlabInterface

21

Table 21 IPOPT directives for AMPL

Option Description

acceptable compl inf tol Acceptance threshold for the complementarity conditionsacceptable constr viol tol Acceptance threshold for the constraint violationacceptable dual inf tol Acceptance threshold for the dual infeasibilityacceptable tol Acceptable convergence tolerance (relative)alpha for y Step size for constraint multipliersbound frac Desired minimal relative distance of initial point to boundbound mult init val Initial value for the bound multipliersbound push Desired minimal absolute distance of initial point to boundbound relax factor Factor for initial relaxation of the boundscompl inf tol Acceptance threshold for the complementarity conditionsconstr mult init max Maximal allowed least-square guess of constraint multipliersconstr viol tol Desired threshold for the constraint violationdiverging iterates tol Threshold for maximal value of primal iteratesdual inf tol Desired threshold for the dual infeasibilityexpect infeasible problem Enable heuristics to quickly detect an infeasible problemfile print level Verbosity level for output filehalt on ampl error Exit with message on evaluation errorhessian approximation Can enable Quasi-Newton approximation of hessianhonor original bounds If no solution might slightly violate boundslinear scaling on demand Enables heuristic for scaling only when seems requiredlinear solver Linear solver to be used for step calculationlinear system scaling Method for scaling the linear systemsma27 pivtol Pivot tolerance for the linear solver MA27ma27 pivtolmax Maximal pivot tolerance for the linear solver MA27ma57 pivtol Pivot tolerance for the linear solver MA57ma57 pivtolmax Maximal pivot tolerance for the linear solver MA57

22

Table 22 IPOPT directives for AMPL

Option Description

max cpu time CPU time limitmax iter Maximum number of iterations

max refinement stepsMaximal number of iterative refinement steps per linearsystem solve

max soc Maximal number of second order correction trial stepsmaxit Maximum number of iterations (same as max iter)

min refinement stepsMinimum number of iterative refinement steps per linearsystem solve

mu init Initial value for the barrier parametermu max Maximal value for barrier parameter for adaptive strategymu oracle Oracle for a new barrier parameter in the adaptive strategymu strategy Update strategy for barrier parameternlp scaling max gradient Maximum gradient after scalingnlp scaling method Select the technique used for scaling the NLPobj scaling factor Scaling factor for the objective functionoption file name File name of options file (default ipoptopt)outlev Verbosity level (same as print level)output file File name of an output file (leave unset for no file output)pardiso matching strategy Matching strategy for linear solver Pardisopardiso out of core power Enables out-of-core version of linear solver Pardisoprint level Verbosity levelprint options documentation Print all available options (for ipoptopt)print user options Toggle printing of user optionsrequired infeasibility reduction Required infeasibility reduction in restoration phaseslack bound frac Desired minimal relative distance of initial slack to boundslack bound push Desired minimal absolute distance of initial slack to boundtol Desired convergence tolerance (relative)

wantsol

solution report without -AMPL sum of1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

warm start bound pushEnables to specify how much should variables should bepushed inside the feasible region

warm start init point Enables to specify bound multiplier values

warm start mult bound pushEnables to specify how much should bound multipliersshould be pushed inside the feasible region

watchdog shortened iter trigger Trigger counter for watchdog procedurewsmp num threads Number of threads to be used in WSMPwsmp pivtol Pivot tolerance for the linear solver WSMPwsmp pivtolmax Maximum pivot tolerance for the linear solver WSMPwsmp scaling Determines how the matrix is scaled by WSMP

23

24

23 KNITRO

ldquoKNITRO is an optimization software library for finding solutions of both continuous (smo-oth) optimization models (with or without constraints) as well as discrete optimization modelswith integer or binary variables (ie mixed integer programs) KNITRO is primarily designedfor finding local optimal solutions of large-scale continuous nonlinear problemsrdquo The KNITROlibrary can be found in httpwwwartelyscom7

231 Basic information

KNITRO is a software package for solving smooth optimization problems with or withoutconstraints

Developed at Ziena Optimization

Operating system(s) KNITRO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) KNITRO is written C C++ Fortran and Java

Programming interfaces(s) Fortan CC++ Java and Microsoft Excel

Modeling language interface(s) KNITRO offers a total of five interfaces Matlab AMPL Math-ematica AIMMS GAMS and MPL

232 Installing KNITRO on Linux

How to get a license

KNITRO is developed by Ziena Optimization LLC and marketed and supported by ArtelysThe first step is to request a license by selecting the download tab8 Along with the academicand commercial trial versions there is a student version limited in problem size (300 variablesand 300 constraints) available for 6 months Each license is personalised hence among otherpersonal information you should inform about

bull the operating system (OS)

bull the OS bit architecture

bull the Machine ID of the computer on which KNITRO will runDownload extract an script available bellow the Machine ID field

After reading and accepting the End User License Agreement the download of the KNITROwill be available and you will get a license key on your email Then you should put the licensefile whose name begins with ziena lic in your $HOME directory or in the $HOMEzienadirectory

How to install

The KNITRO software package for Unix is delivered as a gzipped tar file After downloadingKNITRO you need to unpack it Type in a Terminal window

gunzip knitro-8x-platformnametargz

tar -xvf knitro-8x-platformnametar

sudo mv knitro-8x-z opt

7httpwwwartelyscomindexphppage=knitroamphl=en_EN8httpwwwartelyscomindexphppage=knitroamphl=en_ENdownloads-tab

25

This will create a directory named knitro-8x-z in your opt folder

In order to run KNITRO binary or executable files from anywhere on your Linux computerit is necessary to set several environment variables In particular you must update the PATH

environment variable so that it indicates the location of the knitroampl directory You mustalso update the LD LIBRARY PATH environment variable so that it indicates the location of theKNITRO lib directory Setting the PATH and LD LIBRARY PATH environment variables on a Linuxsystem can be done as follows

echo rsquoexport PATH=$PATHoptknitro-8x-zrsquo gtgt $HOMEbashrcecho rsquoexport PATH=$PATHoptknitro-8x-zknitroamplrsquo gtgt $HOMEbashrcecho rsquoexport LD LIBRARY PATH=$LD LIBRARY PATHoptknitro-8x-zlibrsquo gtgt $HOMEbashrc

Each Matlab release expects the KNITRO library file on Linux to have a specific name There-fore to use KNITRO 8x or later with Matlab release R2008a through 2011b you must createa symbolic link to the expected name Open a Terminal window and in the KNITRO librarydirectory issue the command

bull for Matlab releases R2008a R2008b or R2009aln -s libknitroso800 libknitroso5

bull for Matlab releases R2009b R2010a or R2010bln -s libknitroso800 libknitroso6

bull for Matlab releases R2011a R2011b or R2012aln -s libknitroso800 libknitroso7

Documentation

More informations can be found in the following documentation9

KNITRO 80 User Manual Knitro80 UserManualpdfVersion December 2011 (816 KB)

Specific KNITROMatlab interface documentation can be found on the Matlab OptimizationToolbox webpages10

233 Getting started with KNITRO and AMPL

option solver knitroampl

option knitro options rsquooption1=value1 option2=value2rsquo

solve

9httpwwwartelyscomuploadspdfsKnitro80_UserManualpdf10httpwwwmathworkscomaccesshelpdeskhelptoolboxoptim

26

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 3: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

Contents

1 OC amp NLP Interfaces 711 Introduction 712 AMPL 713 ACADO ndash Automatic Control And Dynamic Optimization 814 BOCOP ndash The optimal control solver 915 DIDO ndash Automatic Control And Dynamic Optimization 1016 ICLOCS ndash Imperial College London Optimal Control Software 1217 TACO ndash Toolkit for AMPL Control Optimization 1218 Pseudospectral Methods in Optimal Control 13

2 NLP Solvers 1721 Introduction 1722 IPOPT ndash Interior Point OPTimizer 1723 KNITRO 2524 WORHP ndash WORHP Optimises Really Huge Problems 3125 Other Commercial Packages 33

3 Project webpage 35

4 Optimal Control Toolbox 37

5 Applications 3951 CarndashLike 3952 Goddard Problem 4353 HIV 44

A Programming code 47A1 Optimal Control Toolbox MATLAB Code 47A2 CarndashLike AMPL Code 55A3 HIV ICLOCS Code 57

3

4

Work Plan

Project PTDCEEA-CRO1160142009 ndash ldquoOptimal Control for Constrained Hybrid Systemrdquo

The main tasks to be carried out are

1 To understand and obtain a handsndashon knowledge of nonndashlinear programming models cur-rent tools to address them (AMPL MATLAB ) and current solvers (KNITRO WORHPSNOPT ) Comparative study of different solvers

2 To understand optimal control problems and how to transcribe them into NLP to be im-plemented in AMPL or MATLAB

3 To obtain a handsndashon knowledge of current optimal control packages such DIDO ACADOOCPID-DAE ASTOS BOCOP and others To perform a comparative study

4 To help to implement and solve case studies and tests to illustrate theoretical develop-ments

5 To maintain the project webndashpage and documentation accessible to the project team

6 (If time permits) To construct a platform to easily solve sequences of optimal control prob-lems in a receding horizon fashion in order to implement model predictive controllers

TODAY

Months 0 1 2 3 4 5 6 7 8 9 10 11 12

100 completeTask 1

100 completeTask 2

100 completeTask 3

100 completeTask 4

100 completeTask 5

50 completeTask 6

Progress reports on tasks just concluded and ongoing tasks should be produced at the endof months 3 5 7 10 and 12

5

6

Chapter 1

Optimal Control andNonlinear Programming Interfaces

11 Introduction

Optimal control and nonlinear programming interfaces are software used to solve optimalcontrol problems A interface is a software that help us to prepare all data as input to the solverWe can divide the interfaces in two groups optimal control interfaces and nonlinear program-ming interfaces The first ones handle with the discretisation and transcription procedureswhile the other ones leave this work to the programmerresearcher (see Figure 11) Severalinterfaces will be presented in this chapter evolving openndashsource freeware and commercialsoftware working under different operating systems

Figure 11

12 AMPL

ldquoAMPL ndash A Mathematical Programming Language ndash is a comprehensive and powerful alge-braic modeling language for linear and nonlinear optimization problems in discrete or contin-uous variablesrdquo The AMPL library can be found in the official site1

121 Basic information

Developed by Robert Fourer David Gay and Brian Kernighan at Bell Laboratories1httpwwwanmplcom

7

Operating system(s) AMPL can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) AMPL is written in C++ and it is released as open source code under theEclipse Public License (EPL)

122 Installing AMPL on Linux

Download your AMPL executable by selecting the Intel (Pentium-compatible) PCs runningLinux on AMPL website2 or type in a Terminal window

wget httpnetlibsandiagovamplstudentlinuxamplgz

The downloaded file must be decompressed and it must be made executable after which itmay be executed from any directory in your search path

gzip -d amplgz

chmod +x ampl

sudo mkdir -p optampl

sudo mv ampl optampl

echo rsquoEXPORT PATH=$PATHoptamplrsquo gtgt $HOMEbashrc

To complete your AMPL installation you can also download solvers to a directory in yoursearch path To use a downloaded solver set AMPLrsquos solver option to the name of the solverrsquosexecutable file

The student edition of AMPL is limited to 300 variables and 300 constraints and objectives(after presolve) for nonlinear problems

123 Getting started with AMPL

The first step in solving a problem is to load the model variables objective function andconstraints It is usual the create a mod file containing these informations First open a AMPLcommand window by typing ampl in a Terminal window

AMPL commands end with semicolon () Some AMPL basic commands

bull to declare a parameter param ltpar namegt

bull to load a model model ltfile namegtmod

bull to run a AMPL script include ltscript namegtrun

bull to load a data file data ltfile namegtdat

bull to view the content of a variable display ltvar namegt

bull to exit a AMPL command window exit

13 ACADO ndash Automatic Control And Dynamic Optimization

ldquoACADO Toolkit is a software environment and algorithm collection for automatic controland dynamic optimization It provides a general framework for using a great variety of al-gorithms for direct optimal control including model predictive control state and parameterestimation and robust optimizationrdquo The ACADO library can be found in the official site3

2httpnetlibsandiagovamplstudentlinuxamplgz3httpsourceforgenetprojectsacado

8

131 Basic information

Developed under the direction of Moritz Diehl

Operating system(s) ACADO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) ACADO Toolkit is implemented as self-contained C++ code it comes alongwith user-friendly MATLAB interface and is released under the LGP License

132 Installing ACADO on Linux

In order to install ACADO Toolkit under LINUXUNIX operating systems make sure thata C++ compiler is correctly installed For the case that a graphical output is desired Gnuplotshould be installed in addition However ACADO Toolkit will also run without Gnuplot - inthis case the graphical output is simply turned off There are no further dependencies

First download ACADO Toolkit and its licence from the offical website4 and extract the filestar xfvz ACADOtoolkit-102613betatargz

sudo mv ACADOtoolkit-102613beta optacado

Then go to the directory optacado and compile the packagecd optacado

sudo make

sudo make doc (optional)

Finally check whether the installation was successfulcd examplesgetting started

simple ocp

The following documentation5 is available

bull ACADO Toolkit Userrsquos Manual

bull ACADO for Matlab Userrsquos Manual

bull ACADO Toolkit Introductory Talk

14 BOCOP ndash The optimal control solver

ldquoThe BOCOP project aims to develop an open-source toolbox for solving optimal controlproblemsrdquo The BOCOP library can be found in the official site6

141 Basic information

Developed by V Grelard P Martinon and F Bonnans at Inria-Saclay

Operating system(s) BOCOP is available for linux precompiled packages (Mac and Windowsversions are still under testing)

Code language(s) BOCOP requires SciLab and it is released under the Eclipse Public License(EPL)

4httpwwwacadotoolkitorgdownloadphp5httpsourceforgenetpacadowikiDocumentation6httpwwwbocoporg

9

142 Installing BOCOP on Linux

In order to install BOCOP software under LINUXUNIX operating system download BO-COP software from the official website7 and extract the files

tar xvf bocop-103-beta-linux32targz

mv bocop-103-beta-linux32 optbocop

cd optbocop

make

sudo chmod +x bocopsh

Add the following to your bashrc or bash profile

BOCOPDIR=optbocop

export PATH=$PATH$BOCOPDIR

143 Getting started with BOCOP

Read the userrsquos guide8 and run the examples shown in the optbocopproblems folder

15 DIDO ndash Automatic Control And Dynamic Optimization

ldquoDIDO the leading optimal control software powers users by offering the easiest and directsolutions to the most complex problemsrdquo The DIDO software can be found in the official site9

151 Basic information

Developed at Elissar Global

Operating system(s) DIDO can be used on Microsoft Windows

Code language(s) DIDO requires MATLAB and it is released under academic and commerciallicenses

152 Installing DIDO

DIDOrsquos foundation is pseudospectral theory and is the only pseudospectral solution withmathematically proven convergence properties DIDO is a MATLAB program for solving hybridoptimal control problems The generalndashpurpose program is named after Dido the legendaryfounder and first queen of Carthage who is famous in mathematics for her remarkable solutionto a constrained optimal control problem even before the invention of calculus

To install DIDO on your computer you should consider the system requirements

bull WINDOWS

bull MATLAB

and the following steps

7httpwwwbocoporg8httpbocopsaclayinriafrdownload=109httpwwwelissarglobalcomindustryproductssoftware-3

10

STEP 1 Obtain a license for DIDO from Elissar Global at the official site10

STEP 2 After you receive an email with the link to a compressed version of DIDO downloadthe zip file and uncompress it It will automatically uncompress with the right folderstructure

STEP 3 Right click on the DIDO icon and click on PROPERTIES In the Target box type locationof your licensed MATLAB file In the Start in box type the full path name of your DIDOfolder

STEP 4 Doublendashclick on the DIDO ICON (this should start MATLAB and automatically initial-ize DIDO)

(a) In the MATLAB command window type TestDIDOA successful installation should produce the final output on the screenldquoCONGRATULATIONS DIDO TEST WAS SUCCESSFULrdquo

(b) A further test may be performed by typing LanderProblemA successful installation should produce a pretty graph

153 Getting started with DIDO

In the MATLAB command window run[cost primal] = dido(Problem)

where Problem is a structure array with fields cost dynamics events and path that point to theinput files

Problemcost = lsquoMyCostFilersquo

Problemdynamics = lsquoMyDynamicsFilersquo

Problemevents = lsquoMyEventsFilersquo

Problempath = lsquoMyPathFilersquo

The variable primal is used in nearly all DIDO files to pass the states controls parametersand other useful quantities To print out the statendash and controlndashtrajectory in the MATLABcommand window runprimalstates primalcontrols

This generality allows for

bull Fairly complex interior point constraints

bull Pre-defined segments

bull Differentially-flat segments

bull Transition conditions

bull Midndashmaneuver changes in dynamics

bull Multi-dynamical systems

bull Midndashmaneuver changes in the cost function

bull Switches

bull Discrete events10httpwwwelissarglobalcomacademicget-didotry-dido

11

16 ICLOCS ndash Imperial College London Optimal Control Software

ldquoThe code allows users to define and solve optimal control problems with general path andboundary constraints and free or fixed final time It is also possible to include constant designparameters as unknownsrdquo The ICLOCS software can be found in the official site11

161 Basic information

Developed by Paola Falugi Eric Kerrigan and Eugene van Wyk

Operating system(s) ICLOCS can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) ICLOCS is implemented in MATLAB and it is released as open source codeunder the BSD License

162 Installing ICLOCS on Linux

In order to install ICLOCS software under LINUXUNIX operating system download ICLOCSsoftware from the official site12 and extract the files

wget httpwwweeicacukICLOCSICLOCS 02 NItarzip

unzip ICLOCS 02 NItarzip

tar xvf ICLOCS 02 NItar

sudo mv ICLOCS 02 NI opticlocs

In order to use it in MATLAB you need to tell MATLAB where to find it To do this justtype addpath(genpath(rsquoopticlocsrsquo)) in the MATLAB command window

163 Getting started with ICLOCS

Read the userrsquos guide13 and run the examples shown in the opticlocsexamples folder

17 TACO ndash Toolkit for AMPL Control Optimization

ldquoTACO is the Toolkit for AMPL Control Optimization It defines some add-ons to the AMPLmodeling language that allow the elegant formulation of ODEDAE optimal control problemsin AMPLrdquo The TACO toolkit can be found in the official site14

171 Basic information

Developed by Christian Kirches and Sven Leyffer

Operating system(s) TACO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) TACO is written in C

11httpwwweeicacukICLOCS12httpwwweeicacukICLOCS13httpwwweeicacukICLOCSuser_guidepdf14httpwwwiwruni-heidelbergde~ChristianKirchessoftwarehtml

12

172 Installing TACO on Linux

In order to install TADO Toolkit under LINUXUNIX operating systems make sure thatCMAKE and a C++ compiler is correctly installed To install CMAKE type in a Terminal win-dow

wget httpslaunchpadnetubuntu+archiveprimary+filescmake 288origtargz

gunzip cmake 288origtargz

tar xvf cmake 288origtar

cd cmake-288

sudo bootstrap

sudo make

sudo make install

First download TACO Toolkit from the offical site15 and extract the files

wget httpswwwiwruni-heidelbergdegroupsagbockFILEStaco sourcetargzgunzip taco sourcetargz

tar xfvz taco sourcetar

sudo ln -s optCoinIpoptThirdPartyASLlibamplsolvera libamplsolvera

18 Pseudospectral Methods in Optimal Control

181 PSOPT

PSOPT is an open source optimal control software package written in C++ that uses directcollocation methods including pseudospectral and local discretizations available in the officesite16 Pseudospectral methods solve optimal control problems by approximating the time-dependent variables using global polynomials such as Legendre or Chebyshev functions Localdiscretisation methods approximate the time dependent functions using local splines and canbe seen as implementations of implicit Runge-Kutta integrators With both global and localmethods differential equations continuous constraints and integrals associated with the prob-lem are discretised over a grid of nodes Sparse nonlinear programming is then used to findlocal optimal solutions

PSOPT is able to deal with problems with the following characteristics

bull Single or multiphase problems

bull Continuous time nonlinear dynamics

bull Nonlinear path constraints

bull General event constraints

bull Integral constraints

bull Interior point constraints

bull Bounds on controls and state variables

bull General cost function with Lagrange and Mayer terms

15httpwwwiwruni-heidelbergde~ChristianKirchessoftwarehtml16httpwwwpsoptorg

13

bull Linear or nonlinear linkages between phases

bull Fixed or free initial phase time

bull Fixed or free final phase time

bull Optimization of static parameters

bull Optimal parameter estimation given sampled observations

The implementation has the following features

bull Choice between Legendre Chebyshev central differences trapezoidal or Hermite-Simpsondiscretisation

bull Large scale nonlinear programming using IPOPT and (optionally) SNOPT

bull Estimation of the discretisation error

bull Automatic mesh refinement

bull Automatic scaling

bull Automatic differentiation using the ADOL-C library

bull Numerical differentiation by using sparse finite differences

bull Automatic identification of the sparsity of the derivative matrices

bull DAE formulation so that differential and algebraic constraints can be implemented in thesame C++ function

bull Easy to use interface to GNUplot to produce graphical output including 2D plots 3Dcurves and surfaces and polar plots

bull Automatic generation of LaTeX code to produce a table that summarizes the mesh refine-ment process

Full details on PSOPT and its features can be found in its documentation17

182 GPOPS-II - MATLAB Optimal Control Software

GPOPS-II is a general purpose optimal control software available in the office site18 GPOPS-II is a new open-source MATLAB optimal control software that implements a brand new hp-adaptive Legendre-Gauss-Radau quadrature integral pseudospectral method for solving generalnonlinear optimal control problems Using GPOPS-II the optimal control problem is transcribedto a nonlinear programming problem (NLP) The NLP is then solved using either the solverSNOPT or the solver IPOPT

The following are some the key features of GPOPS-II

bull Allows for an extremely general formulation of the optimal control problem

bull Allows for inclusion of integral constraints and highly general boundary conditions

bull Complete first and second sparse finite-differencing of optimal control problem to com-pute all derivatives required by the NLP solver

17httpcodegooglecomppsoptdownloadsdetailname=PSOPT_Manual_R3pdfampcan=2ampq=18httpwwwgpopsorg

14

bull The latest advances in mesh refinement including hpndashadaptive pseudospectral methods

bull Gaussian quadrature integration methods for rapid convergence

bull Highly accurate costate estimation

bull Inclusion of the NLP solver SNOPT (for Academic Users) and IPOPT (for Not-for-Profitand Commercial Users)

bull No third-party products other than MATLAB are required

More information about the methodology used in GPOPS-II can be found by reading therelevant articles in the open literature19

The fees for using GPOPS-II are as follows

bull For students and all others employed at academic institutions NO CHARGE

bull US Federal State or Local Government NO CHARGE

bull Users at not-for-profit institutions (including non-US Government agencies) or commer-cial institutions LICENSING FEES APPLY

19httpvdolmaeufledu

15

16

Chapter 2

Nonlinear Programming Solvers

21 Introduction

A solver is a software used to compute numerical solution of an optimal control problemIn general an optimal controlnonlinear programming interface is needed to prepare all dataas input to the solver (see Figure 21) Several solvers will be presented in this chapter evolvingopenndashsource freeware and commercial software working under different operating systemsSince all solvers require configuration lists of parameters will be presented for some of them

Figure 21

22 IPOPT ndash Interior Point OPTimizer

ldquoIPOPT is a software package for largendashscale nonlinear optimisation It is designed to find(local) solutions of mathematical optimisation problemsrdquo The IPOPT library can be found inhttpwwwartelyscom1

221 Basic information

Developed by Andreas Wachter and Carl Laird

Operating system(s) IPOPT can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) IPOPT is written in C++ and is released as open source code under theEclipse Public License (EPL)

1httpwwwartelyscomindexphppage=knitroamphl=en_EN

17

Modeling language interface(s) IPOPT can be used as a library that can be linked to C++ C orFortran code as well as a solver executable for the AMPL modelling environment Thepackage includes interfaces to CUTEr optimisation testing environment as well as theMATLAB and R programming environments

222 Installing IPOPT

How to install IPOPT on Linux

Getting the IPOPT code via subversion

sudo svn co httpsprojectscoin-ororgsvnIpoptstable310 optCoinIpopt

This will create a directory named CoinIpopt in your opt folder

BLAS routines that provide standard building blocks for performing basic vector and matrixoperations

LAPACK routines for solving systems of simultaneous linear equations least-squares solutionsof linear systems of equations eigenvalue problems and singular value problems

HSL state-of-the-art packages for large-scale scientific computation

ASL AMPL Solver Library allows to read the nl files and provides the automatic differentia-tion functionality

MUMPS MUltifrontal Massively Parallel sparse direct Solver

METIS programs for partitioning graphs partitioning finite element meshes and producing fillreducing orderings for sparse matrices

To install IPOPT just run

cd optCoinIpoptThirdPartyBlas

getBlas

cd Lapack

getLapack

cd ASL

getASL

cd Mumps

getMumps

cd Metis

getMetis

IPOPT requires a sparse symmetric linear solver There are different possibilities The userrsquosguide of IPOPT suggests that you use HSL subroutines

1 Go to HSL website2 and find the package list

2 Click on MA27 read the license and submit the registration form

3 Go back to the package list

4 Click on MC19 read the license and submit the registration form

2httphslrlacukarchivehslarchivehtml

18

You will receive in your email the links to obtain the HSL packages Download them to your$HOME directory and run

gunzip ma27-100targz

tar xvf ma27-100tar

gunzip mc19-100targz

tar xvf mc19-100tar

cd optCoinIpoptThirdPartyHSL

sudo mv -r $HOMEmc19-100

sudo mv -r $HOMEma27-100

cd mc19-100

sudo configure

sudo make install

sudo make check

cd ma27-100

sudo configure

sudo make install

sudo make check

The make check command will check if the installation of the HSL packages was successfulReturn to the optCoinIpopt directory and continue the IPOPT installation

cd

sudo mkdir build

cd build

sudo configure

sudo make test

sudo make install

If you get the error message ldquoerrorlsquoNULLrsquo was not declared in this scoperdquo when run-ning sudo make then you have to edit the following filesoptCoinIpoptIpoptsrcCommonIpSmartPtrhpp

optCoinIpoptIpoptsrcAlgorithmLinearSolversIpTripletToCSRConvertercpp

by adding include ltcstddefgt

How to install IPOPT on Mac OS X

In a Terminal window install homebrewusrbinruby -e $(usrbincurl -fsSL httpsrawgithubcommxclhomebrewmaster

LibraryContributionsinstall homebrewrb)

With this software you can easily install dependency packages like wget and gfortran

brew install wget

brew install gfortran

sudo wget httpwwwcoin-ororgdownloadsourceIpoptIpopt-3102zip

sudo unzip Ipopt-3102zip

cd Ipopt-3102

Download Netlib BlasLapack

cd optCoinIpoptThirdPartyBlas

sudo getBlas

19

cd Lapack

sudo getLapack

cd ASL

sudo getASL

cd Mumps

sudo getMumps

cd Metis

sudo getMetis

Make a build for a 64bit target

sudo mkdir build64

cd build64

sudo configure --disable-shared --with-blas=BUILD --with-lapack=BUILD F77=gfortran

FFLAGS=-fexceptions -m64 -fbackslash CFLAGS=-fno-common -no-cpp-precomp -fexceptions

-arch x86 64 -m64 CXXFLAGS=-fno-common -no-cpp-precomp -fexceptions -arch x86 64

-m64

Compile test and install

make

make test

make install

Add the following to your bashrc or bash profile

IPOPTDIR=optCoinIpoptbuild

export PATH=$PATH$IPOPTDIR$IPOPTDIRbinexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyASLexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyBlaslibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyHSLlibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyLapacklibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMetislibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMumpslibs

How to install the Matlab interface on Linux

First of all be sure

cd optmatlabR2008asysosglnx86

mv libgcc sso1 libgcc sso1bck

ln -s libi386-linux-gnulibgcc sso1 libgcc sso1

mv libstdc++so608 libstdc++so608bck

ln -s usrlibi386-linux-gnulibstdc++so6 libstdc++so608

Go to the subdirectory optCoinIpoptbuildIpoptcontribMatlabInterfacesrc andopen the Makefile file

sudo apt-get install hardening-wrapper

You need to edit this file to suit the installation for your system setup You may need tomodify MATLAB HOME MEXSUFFIX and MEX as explained in the comments of the Makefile I setthese variables as follows

20

MATLAB HOME = optmatlabR2008a

MEXSUFFIX = mexglx

MEX = optmatlabR2008abinmex

In my case I removed some of the compiler options as it follows

prefix = optCoinIpopt

CXX = optintelcomposer xe 2011 sp19293binia32icpc

CCXXFLAGS = -O3 -pipe -DNDEBUG -Wparentheses -Wreturn-type -Wcast-qual -Wall

-Wpointer-arith -Wwrite-strings -Wconversion -Wno-unknown-pragmas -DMATLAB MEXFILE

LDFLAGS = $(CXXFLAGS) -Wl --rpath -Wl $prefixlibcoin -Wl --rpath -Wl

--rpath -Wl$prefixlibcoinThirdParty

In my case I had to set explicitly the include Matlab folder

o cpp

$(CXX) $(CXXFLAGS) $(INCL) -IoptmatlabR2008aexterninclude -o $ -c $^

Once you set up the Makefile properly type sudo make install in the same directory as theMakefile If the installation procedure was successful you will end up with a MEX file calledipoptmexglx In order to use it in MATLAB you need to tell MATLAB where to find it Todo this just type addpath optCoinIpoptlib in the MATLAB command window after youcheck if the ipoptmexglx file is in this folder

Documentation

More informations can be found in the following documentation

Online documentation httpwwwcoin-ororgIpoptdocumentation3

A PDF version of this documentation can be downloaded here4

A short IPOPT tutorial can be downloaded here5

MATLAB interface Instructions specific to the MATLAB interface can be found at the Matlabinterface page6

223 Getting started with IPOPT and AMPL

option solver ipopt

option ipopt options rsquooption1=value1 option2=value2rsquo

solve

3httpwwwcoin-ororgIpoptdocumentation4httpsprojectscoin-ororgIpoptbrowserstable39Ipoptdocdocumentationpdfformat=raw5httpdropsdagstuhldevolltexte20092089pdf09061WaechterAndreasPaper2089pdf6httpsprojectscoin-ororgIpoptwikiMatlabInterface

21

Table 21 IPOPT directives for AMPL

Option Description

acceptable compl inf tol Acceptance threshold for the complementarity conditionsacceptable constr viol tol Acceptance threshold for the constraint violationacceptable dual inf tol Acceptance threshold for the dual infeasibilityacceptable tol Acceptable convergence tolerance (relative)alpha for y Step size for constraint multipliersbound frac Desired minimal relative distance of initial point to boundbound mult init val Initial value for the bound multipliersbound push Desired minimal absolute distance of initial point to boundbound relax factor Factor for initial relaxation of the boundscompl inf tol Acceptance threshold for the complementarity conditionsconstr mult init max Maximal allowed least-square guess of constraint multipliersconstr viol tol Desired threshold for the constraint violationdiverging iterates tol Threshold for maximal value of primal iteratesdual inf tol Desired threshold for the dual infeasibilityexpect infeasible problem Enable heuristics to quickly detect an infeasible problemfile print level Verbosity level for output filehalt on ampl error Exit with message on evaluation errorhessian approximation Can enable Quasi-Newton approximation of hessianhonor original bounds If no solution might slightly violate boundslinear scaling on demand Enables heuristic for scaling only when seems requiredlinear solver Linear solver to be used for step calculationlinear system scaling Method for scaling the linear systemsma27 pivtol Pivot tolerance for the linear solver MA27ma27 pivtolmax Maximal pivot tolerance for the linear solver MA27ma57 pivtol Pivot tolerance for the linear solver MA57ma57 pivtolmax Maximal pivot tolerance for the linear solver MA57

22

Table 22 IPOPT directives for AMPL

Option Description

max cpu time CPU time limitmax iter Maximum number of iterations

max refinement stepsMaximal number of iterative refinement steps per linearsystem solve

max soc Maximal number of second order correction trial stepsmaxit Maximum number of iterations (same as max iter)

min refinement stepsMinimum number of iterative refinement steps per linearsystem solve

mu init Initial value for the barrier parametermu max Maximal value for barrier parameter for adaptive strategymu oracle Oracle for a new barrier parameter in the adaptive strategymu strategy Update strategy for barrier parameternlp scaling max gradient Maximum gradient after scalingnlp scaling method Select the technique used for scaling the NLPobj scaling factor Scaling factor for the objective functionoption file name File name of options file (default ipoptopt)outlev Verbosity level (same as print level)output file File name of an output file (leave unset for no file output)pardiso matching strategy Matching strategy for linear solver Pardisopardiso out of core power Enables out-of-core version of linear solver Pardisoprint level Verbosity levelprint options documentation Print all available options (for ipoptopt)print user options Toggle printing of user optionsrequired infeasibility reduction Required infeasibility reduction in restoration phaseslack bound frac Desired minimal relative distance of initial slack to boundslack bound push Desired minimal absolute distance of initial slack to boundtol Desired convergence tolerance (relative)

wantsol

solution report without -AMPL sum of1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

warm start bound pushEnables to specify how much should variables should bepushed inside the feasible region

warm start init point Enables to specify bound multiplier values

warm start mult bound pushEnables to specify how much should bound multipliersshould be pushed inside the feasible region

watchdog shortened iter trigger Trigger counter for watchdog procedurewsmp num threads Number of threads to be used in WSMPwsmp pivtol Pivot tolerance for the linear solver WSMPwsmp pivtolmax Maximum pivot tolerance for the linear solver WSMPwsmp scaling Determines how the matrix is scaled by WSMP

23

24

23 KNITRO

ldquoKNITRO is an optimization software library for finding solutions of both continuous (smo-oth) optimization models (with or without constraints) as well as discrete optimization modelswith integer or binary variables (ie mixed integer programs) KNITRO is primarily designedfor finding local optimal solutions of large-scale continuous nonlinear problemsrdquo The KNITROlibrary can be found in httpwwwartelyscom7

231 Basic information

KNITRO is a software package for solving smooth optimization problems with or withoutconstraints

Developed at Ziena Optimization

Operating system(s) KNITRO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) KNITRO is written C C++ Fortran and Java

Programming interfaces(s) Fortan CC++ Java and Microsoft Excel

Modeling language interface(s) KNITRO offers a total of five interfaces Matlab AMPL Math-ematica AIMMS GAMS and MPL

232 Installing KNITRO on Linux

How to get a license

KNITRO is developed by Ziena Optimization LLC and marketed and supported by ArtelysThe first step is to request a license by selecting the download tab8 Along with the academicand commercial trial versions there is a student version limited in problem size (300 variablesand 300 constraints) available for 6 months Each license is personalised hence among otherpersonal information you should inform about

bull the operating system (OS)

bull the OS bit architecture

bull the Machine ID of the computer on which KNITRO will runDownload extract an script available bellow the Machine ID field

After reading and accepting the End User License Agreement the download of the KNITROwill be available and you will get a license key on your email Then you should put the licensefile whose name begins with ziena lic in your $HOME directory or in the $HOMEzienadirectory

How to install

The KNITRO software package for Unix is delivered as a gzipped tar file After downloadingKNITRO you need to unpack it Type in a Terminal window

gunzip knitro-8x-platformnametargz

tar -xvf knitro-8x-platformnametar

sudo mv knitro-8x-z opt

7httpwwwartelyscomindexphppage=knitroamphl=en_EN8httpwwwartelyscomindexphppage=knitroamphl=en_ENdownloads-tab

25

This will create a directory named knitro-8x-z in your opt folder

In order to run KNITRO binary or executable files from anywhere on your Linux computerit is necessary to set several environment variables In particular you must update the PATH

environment variable so that it indicates the location of the knitroampl directory You mustalso update the LD LIBRARY PATH environment variable so that it indicates the location of theKNITRO lib directory Setting the PATH and LD LIBRARY PATH environment variables on a Linuxsystem can be done as follows

echo rsquoexport PATH=$PATHoptknitro-8x-zrsquo gtgt $HOMEbashrcecho rsquoexport PATH=$PATHoptknitro-8x-zknitroamplrsquo gtgt $HOMEbashrcecho rsquoexport LD LIBRARY PATH=$LD LIBRARY PATHoptknitro-8x-zlibrsquo gtgt $HOMEbashrc

Each Matlab release expects the KNITRO library file on Linux to have a specific name There-fore to use KNITRO 8x or later with Matlab release R2008a through 2011b you must createa symbolic link to the expected name Open a Terminal window and in the KNITRO librarydirectory issue the command

bull for Matlab releases R2008a R2008b or R2009aln -s libknitroso800 libknitroso5

bull for Matlab releases R2009b R2010a or R2010bln -s libknitroso800 libknitroso6

bull for Matlab releases R2011a R2011b or R2012aln -s libknitroso800 libknitroso7

Documentation

More informations can be found in the following documentation9

KNITRO 80 User Manual Knitro80 UserManualpdfVersion December 2011 (816 KB)

Specific KNITROMatlab interface documentation can be found on the Matlab OptimizationToolbox webpages10

233 Getting started with KNITRO and AMPL

option solver knitroampl

option knitro options rsquooption1=value1 option2=value2rsquo

solve

9httpwwwartelyscomuploadspdfsKnitro80_UserManualpdf10httpwwwmathworkscomaccesshelpdeskhelptoolboxoptim

26

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 4: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

4

Work Plan

Project PTDCEEA-CRO1160142009 ndash ldquoOptimal Control for Constrained Hybrid Systemrdquo

The main tasks to be carried out are

1 To understand and obtain a handsndashon knowledge of nonndashlinear programming models cur-rent tools to address them (AMPL MATLAB ) and current solvers (KNITRO WORHPSNOPT ) Comparative study of different solvers

2 To understand optimal control problems and how to transcribe them into NLP to be im-plemented in AMPL or MATLAB

3 To obtain a handsndashon knowledge of current optimal control packages such DIDO ACADOOCPID-DAE ASTOS BOCOP and others To perform a comparative study

4 To help to implement and solve case studies and tests to illustrate theoretical develop-ments

5 To maintain the project webndashpage and documentation accessible to the project team

6 (If time permits) To construct a platform to easily solve sequences of optimal control prob-lems in a receding horizon fashion in order to implement model predictive controllers

TODAY

Months 0 1 2 3 4 5 6 7 8 9 10 11 12

100 completeTask 1

100 completeTask 2

100 completeTask 3

100 completeTask 4

100 completeTask 5

50 completeTask 6

Progress reports on tasks just concluded and ongoing tasks should be produced at the endof months 3 5 7 10 and 12

5

6

Chapter 1

Optimal Control andNonlinear Programming Interfaces

11 Introduction

Optimal control and nonlinear programming interfaces are software used to solve optimalcontrol problems A interface is a software that help us to prepare all data as input to the solverWe can divide the interfaces in two groups optimal control interfaces and nonlinear program-ming interfaces The first ones handle with the discretisation and transcription procedureswhile the other ones leave this work to the programmerresearcher (see Figure 11) Severalinterfaces will be presented in this chapter evolving openndashsource freeware and commercialsoftware working under different operating systems

Figure 11

12 AMPL

ldquoAMPL ndash A Mathematical Programming Language ndash is a comprehensive and powerful alge-braic modeling language for linear and nonlinear optimization problems in discrete or contin-uous variablesrdquo The AMPL library can be found in the official site1

121 Basic information

Developed by Robert Fourer David Gay and Brian Kernighan at Bell Laboratories1httpwwwanmplcom

7

Operating system(s) AMPL can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) AMPL is written in C++ and it is released as open source code under theEclipse Public License (EPL)

122 Installing AMPL on Linux

Download your AMPL executable by selecting the Intel (Pentium-compatible) PCs runningLinux on AMPL website2 or type in a Terminal window

wget httpnetlibsandiagovamplstudentlinuxamplgz

The downloaded file must be decompressed and it must be made executable after which itmay be executed from any directory in your search path

gzip -d amplgz

chmod +x ampl

sudo mkdir -p optampl

sudo mv ampl optampl

echo rsquoEXPORT PATH=$PATHoptamplrsquo gtgt $HOMEbashrc

To complete your AMPL installation you can also download solvers to a directory in yoursearch path To use a downloaded solver set AMPLrsquos solver option to the name of the solverrsquosexecutable file

The student edition of AMPL is limited to 300 variables and 300 constraints and objectives(after presolve) for nonlinear problems

123 Getting started with AMPL

The first step in solving a problem is to load the model variables objective function andconstraints It is usual the create a mod file containing these informations First open a AMPLcommand window by typing ampl in a Terminal window

AMPL commands end with semicolon () Some AMPL basic commands

bull to declare a parameter param ltpar namegt

bull to load a model model ltfile namegtmod

bull to run a AMPL script include ltscript namegtrun

bull to load a data file data ltfile namegtdat

bull to view the content of a variable display ltvar namegt

bull to exit a AMPL command window exit

13 ACADO ndash Automatic Control And Dynamic Optimization

ldquoACADO Toolkit is a software environment and algorithm collection for automatic controland dynamic optimization It provides a general framework for using a great variety of al-gorithms for direct optimal control including model predictive control state and parameterestimation and robust optimizationrdquo The ACADO library can be found in the official site3

2httpnetlibsandiagovamplstudentlinuxamplgz3httpsourceforgenetprojectsacado

8

131 Basic information

Developed under the direction of Moritz Diehl

Operating system(s) ACADO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) ACADO Toolkit is implemented as self-contained C++ code it comes alongwith user-friendly MATLAB interface and is released under the LGP License

132 Installing ACADO on Linux

In order to install ACADO Toolkit under LINUXUNIX operating systems make sure thata C++ compiler is correctly installed For the case that a graphical output is desired Gnuplotshould be installed in addition However ACADO Toolkit will also run without Gnuplot - inthis case the graphical output is simply turned off There are no further dependencies

First download ACADO Toolkit and its licence from the offical website4 and extract the filestar xfvz ACADOtoolkit-102613betatargz

sudo mv ACADOtoolkit-102613beta optacado

Then go to the directory optacado and compile the packagecd optacado

sudo make

sudo make doc (optional)

Finally check whether the installation was successfulcd examplesgetting started

simple ocp

The following documentation5 is available

bull ACADO Toolkit Userrsquos Manual

bull ACADO for Matlab Userrsquos Manual

bull ACADO Toolkit Introductory Talk

14 BOCOP ndash The optimal control solver

ldquoThe BOCOP project aims to develop an open-source toolbox for solving optimal controlproblemsrdquo The BOCOP library can be found in the official site6

141 Basic information

Developed by V Grelard P Martinon and F Bonnans at Inria-Saclay

Operating system(s) BOCOP is available for linux precompiled packages (Mac and Windowsversions are still under testing)

Code language(s) BOCOP requires SciLab and it is released under the Eclipse Public License(EPL)

4httpwwwacadotoolkitorgdownloadphp5httpsourceforgenetpacadowikiDocumentation6httpwwwbocoporg

9

142 Installing BOCOP on Linux

In order to install BOCOP software under LINUXUNIX operating system download BO-COP software from the official website7 and extract the files

tar xvf bocop-103-beta-linux32targz

mv bocop-103-beta-linux32 optbocop

cd optbocop

make

sudo chmod +x bocopsh

Add the following to your bashrc or bash profile

BOCOPDIR=optbocop

export PATH=$PATH$BOCOPDIR

143 Getting started with BOCOP

Read the userrsquos guide8 and run the examples shown in the optbocopproblems folder

15 DIDO ndash Automatic Control And Dynamic Optimization

ldquoDIDO the leading optimal control software powers users by offering the easiest and directsolutions to the most complex problemsrdquo The DIDO software can be found in the official site9

151 Basic information

Developed at Elissar Global

Operating system(s) DIDO can be used on Microsoft Windows

Code language(s) DIDO requires MATLAB and it is released under academic and commerciallicenses

152 Installing DIDO

DIDOrsquos foundation is pseudospectral theory and is the only pseudospectral solution withmathematically proven convergence properties DIDO is a MATLAB program for solving hybridoptimal control problems The generalndashpurpose program is named after Dido the legendaryfounder and first queen of Carthage who is famous in mathematics for her remarkable solutionto a constrained optimal control problem even before the invention of calculus

To install DIDO on your computer you should consider the system requirements

bull WINDOWS

bull MATLAB

and the following steps

7httpwwwbocoporg8httpbocopsaclayinriafrdownload=109httpwwwelissarglobalcomindustryproductssoftware-3

10

STEP 1 Obtain a license for DIDO from Elissar Global at the official site10

STEP 2 After you receive an email with the link to a compressed version of DIDO downloadthe zip file and uncompress it It will automatically uncompress with the right folderstructure

STEP 3 Right click on the DIDO icon and click on PROPERTIES In the Target box type locationof your licensed MATLAB file In the Start in box type the full path name of your DIDOfolder

STEP 4 Doublendashclick on the DIDO ICON (this should start MATLAB and automatically initial-ize DIDO)

(a) In the MATLAB command window type TestDIDOA successful installation should produce the final output on the screenldquoCONGRATULATIONS DIDO TEST WAS SUCCESSFULrdquo

(b) A further test may be performed by typing LanderProblemA successful installation should produce a pretty graph

153 Getting started with DIDO

In the MATLAB command window run[cost primal] = dido(Problem)

where Problem is a structure array with fields cost dynamics events and path that point to theinput files

Problemcost = lsquoMyCostFilersquo

Problemdynamics = lsquoMyDynamicsFilersquo

Problemevents = lsquoMyEventsFilersquo

Problempath = lsquoMyPathFilersquo

The variable primal is used in nearly all DIDO files to pass the states controls parametersand other useful quantities To print out the statendash and controlndashtrajectory in the MATLABcommand window runprimalstates primalcontrols

This generality allows for

bull Fairly complex interior point constraints

bull Pre-defined segments

bull Differentially-flat segments

bull Transition conditions

bull Midndashmaneuver changes in dynamics

bull Multi-dynamical systems

bull Midndashmaneuver changes in the cost function

bull Switches

bull Discrete events10httpwwwelissarglobalcomacademicget-didotry-dido

11

16 ICLOCS ndash Imperial College London Optimal Control Software

ldquoThe code allows users to define and solve optimal control problems with general path andboundary constraints and free or fixed final time It is also possible to include constant designparameters as unknownsrdquo The ICLOCS software can be found in the official site11

161 Basic information

Developed by Paola Falugi Eric Kerrigan and Eugene van Wyk

Operating system(s) ICLOCS can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) ICLOCS is implemented in MATLAB and it is released as open source codeunder the BSD License

162 Installing ICLOCS on Linux

In order to install ICLOCS software under LINUXUNIX operating system download ICLOCSsoftware from the official site12 and extract the files

wget httpwwweeicacukICLOCSICLOCS 02 NItarzip

unzip ICLOCS 02 NItarzip

tar xvf ICLOCS 02 NItar

sudo mv ICLOCS 02 NI opticlocs

In order to use it in MATLAB you need to tell MATLAB where to find it To do this justtype addpath(genpath(rsquoopticlocsrsquo)) in the MATLAB command window

163 Getting started with ICLOCS

Read the userrsquos guide13 and run the examples shown in the opticlocsexamples folder

17 TACO ndash Toolkit for AMPL Control Optimization

ldquoTACO is the Toolkit for AMPL Control Optimization It defines some add-ons to the AMPLmodeling language that allow the elegant formulation of ODEDAE optimal control problemsin AMPLrdquo The TACO toolkit can be found in the official site14

171 Basic information

Developed by Christian Kirches and Sven Leyffer

Operating system(s) TACO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) TACO is written in C

11httpwwweeicacukICLOCS12httpwwweeicacukICLOCS13httpwwweeicacukICLOCSuser_guidepdf14httpwwwiwruni-heidelbergde~ChristianKirchessoftwarehtml

12

172 Installing TACO on Linux

In order to install TADO Toolkit under LINUXUNIX operating systems make sure thatCMAKE and a C++ compiler is correctly installed To install CMAKE type in a Terminal win-dow

wget httpslaunchpadnetubuntu+archiveprimary+filescmake 288origtargz

gunzip cmake 288origtargz

tar xvf cmake 288origtar

cd cmake-288

sudo bootstrap

sudo make

sudo make install

First download TACO Toolkit from the offical site15 and extract the files

wget httpswwwiwruni-heidelbergdegroupsagbockFILEStaco sourcetargzgunzip taco sourcetargz

tar xfvz taco sourcetar

sudo ln -s optCoinIpoptThirdPartyASLlibamplsolvera libamplsolvera

18 Pseudospectral Methods in Optimal Control

181 PSOPT

PSOPT is an open source optimal control software package written in C++ that uses directcollocation methods including pseudospectral and local discretizations available in the officesite16 Pseudospectral methods solve optimal control problems by approximating the time-dependent variables using global polynomials such as Legendre or Chebyshev functions Localdiscretisation methods approximate the time dependent functions using local splines and canbe seen as implementations of implicit Runge-Kutta integrators With both global and localmethods differential equations continuous constraints and integrals associated with the prob-lem are discretised over a grid of nodes Sparse nonlinear programming is then used to findlocal optimal solutions

PSOPT is able to deal with problems with the following characteristics

bull Single or multiphase problems

bull Continuous time nonlinear dynamics

bull Nonlinear path constraints

bull General event constraints

bull Integral constraints

bull Interior point constraints

bull Bounds on controls and state variables

bull General cost function with Lagrange and Mayer terms

15httpwwwiwruni-heidelbergde~ChristianKirchessoftwarehtml16httpwwwpsoptorg

13

bull Linear or nonlinear linkages between phases

bull Fixed or free initial phase time

bull Fixed or free final phase time

bull Optimization of static parameters

bull Optimal parameter estimation given sampled observations

The implementation has the following features

bull Choice between Legendre Chebyshev central differences trapezoidal or Hermite-Simpsondiscretisation

bull Large scale nonlinear programming using IPOPT and (optionally) SNOPT

bull Estimation of the discretisation error

bull Automatic mesh refinement

bull Automatic scaling

bull Automatic differentiation using the ADOL-C library

bull Numerical differentiation by using sparse finite differences

bull Automatic identification of the sparsity of the derivative matrices

bull DAE formulation so that differential and algebraic constraints can be implemented in thesame C++ function

bull Easy to use interface to GNUplot to produce graphical output including 2D plots 3Dcurves and surfaces and polar plots

bull Automatic generation of LaTeX code to produce a table that summarizes the mesh refine-ment process

Full details on PSOPT and its features can be found in its documentation17

182 GPOPS-II - MATLAB Optimal Control Software

GPOPS-II is a general purpose optimal control software available in the office site18 GPOPS-II is a new open-source MATLAB optimal control software that implements a brand new hp-adaptive Legendre-Gauss-Radau quadrature integral pseudospectral method for solving generalnonlinear optimal control problems Using GPOPS-II the optimal control problem is transcribedto a nonlinear programming problem (NLP) The NLP is then solved using either the solverSNOPT or the solver IPOPT

The following are some the key features of GPOPS-II

bull Allows for an extremely general formulation of the optimal control problem

bull Allows for inclusion of integral constraints and highly general boundary conditions

bull Complete first and second sparse finite-differencing of optimal control problem to com-pute all derivatives required by the NLP solver

17httpcodegooglecomppsoptdownloadsdetailname=PSOPT_Manual_R3pdfampcan=2ampq=18httpwwwgpopsorg

14

bull The latest advances in mesh refinement including hpndashadaptive pseudospectral methods

bull Gaussian quadrature integration methods for rapid convergence

bull Highly accurate costate estimation

bull Inclusion of the NLP solver SNOPT (for Academic Users) and IPOPT (for Not-for-Profitand Commercial Users)

bull No third-party products other than MATLAB are required

More information about the methodology used in GPOPS-II can be found by reading therelevant articles in the open literature19

The fees for using GPOPS-II are as follows

bull For students and all others employed at academic institutions NO CHARGE

bull US Federal State or Local Government NO CHARGE

bull Users at not-for-profit institutions (including non-US Government agencies) or commer-cial institutions LICENSING FEES APPLY

19httpvdolmaeufledu

15

16

Chapter 2

Nonlinear Programming Solvers

21 Introduction

A solver is a software used to compute numerical solution of an optimal control problemIn general an optimal controlnonlinear programming interface is needed to prepare all dataas input to the solver (see Figure 21) Several solvers will be presented in this chapter evolvingopenndashsource freeware and commercial software working under different operating systemsSince all solvers require configuration lists of parameters will be presented for some of them

Figure 21

22 IPOPT ndash Interior Point OPTimizer

ldquoIPOPT is a software package for largendashscale nonlinear optimisation It is designed to find(local) solutions of mathematical optimisation problemsrdquo The IPOPT library can be found inhttpwwwartelyscom1

221 Basic information

Developed by Andreas Wachter and Carl Laird

Operating system(s) IPOPT can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) IPOPT is written in C++ and is released as open source code under theEclipse Public License (EPL)

1httpwwwartelyscomindexphppage=knitroamphl=en_EN

17

Modeling language interface(s) IPOPT can be used as a library that can be linked to C++ C orFortran code as well as a solver executable for the AMPL modelling environment Thepackage includes interfaces to CUTEr optimisation testing environment as well as theMATLAB and R programming environments

222 Installing IPOPT

How to install IPOPT on Linux

Getting the IPOPT code via subversion

sudo svn co httpsprojectscoin-ororgsvnIpoptstable310 optCoinIpopt

This will create a directory named CoinIpopt in your opt folder

BLAS routines that provide standard building blocks for performing basic vector and matrixoperations

LAPACK routines for solving systems of simultaneous linear equations least-squares solutionsof linear systems of equations eigenvalue problems and singular value problems

HSL state-of-the-art packages for large-scale scientific computation

ASL AMPL Solver Library allows to read the nl files and provides the automatic differentia-tion functionality

MUMPS MUltifrontal Massively Parallel sparse direct Solver

METIS programs for partitioning graphs partitioning finite element meshes and producing fillreducing orderings for sparse matrices

To install IPOPT just run

cd optCoinIpoptThirdPartyBlas

getBlas

cd Lapack

getLapack

cd ASL

getASL

cd Mumps

getMumps

cd Metis

getMetis

IPOPT requires a sparse symmetric linear solver There are different possibilities The userrsquosguide of IPOPT suggests that you use HSL subroutines

1 Go to HSL website2 and find the package list

2 Click on MA27 read the license and submit the registration form

3 Go back to the package list

4 Click on MC19 read the license and submit the registration form

2httphslrlacukarchivehslarchivehtml

18

You will receive in your email the links to obtain the HSL packages Download them to your$HOME directory and run

gunzip ma27-100targz

tar xvf ma27-100tar

gunzip mc19-100targz

tar xvf mc19-100tar

cd optCoinIpoptThirdPartyHSL

sudo mv -r $HOMEmc19-100

sudo mv -r $HOMEma27-100

cd mc19-100

sudo configure

sudo make install

sudo make check

cd ma27-100

sudo configure

sudo make install

sudo make check

The make check command will check if the installation of the HSL packages was successfulReturn to the optCoinIpopt directory and continue the IPOPT installation

cd

sudo mkdir build

cd build

sudo configure

sudo make test

sudo make install

If you get the error message ldquoerrorlsquoNULLrsquo was not declared in this scoperdquo when run-ning sudo make then you have to edit the following filesoptCoinIpoptIpoptsrcCommonIpSmartPtrhpp

optCoinIpoptIpoptsrcAlgorithmLinearSolversIpTripletToCSRConvertercpp

by adding include ltcstddefgt

How to install IPOPT on Mac OS X

In a Terminal window install homebrewusrbinruby -e $(usrbincurl -fsSL httpsrawgithubcommxclhomebrewmaster

LibraryContributionsinstall homebrewrb)

With this software you can easily install dependency packages like wget and gfortran

brew install wget

brew install gfortran

sudo wget httpwwwcoin-ororgdownloadsourceIpoptIpopt-3102zip

sudo unzip Ipopt-3102zip

cd Ipopt-3102

Download Netlib BlasLapack

cd optCoinIpoptThirdPartyBlas

sudo getBlas

19

cd Lapack

sudo getLapack

cd ASL

sudo getASL

cd Mumps

sudo getMumps

cd Metis

sudo getMetis

Make a build for a 64bit target

sudo mkdir build64

cd build64

sudo configure --disable-shared --with-blas=BUILD --with-lapack=BUILD F77=gfortran

FFLAGS=-fexceptions -m64 -fbackslash CFLAGS=-fno-common -no-cpp-precomp -fexceptions

-arch x86 64 -m64 CXXFLAGS=-fno-common -no-cpp-precomp -fexceptions -arch x86 64

-m64

Compile test and install

make

make test

make install

Add the following to your bashrc or bash profile

IPOPTDIR=optCoinIpoptbuild

export PATH=$PATH$IPOPTDIR$IPOPTDIRbinexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyASLexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyBlaslibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyHSLlibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyLapacklibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMetislibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMumpslibs

How to install the Matlab interface on Linux

First of all be sure

cd optmatlabR2008asysosglnx86

mv libgcc sso1 libgcc sso1bck

ln -s libi386-linux-gnulibgcc sso1 libgcc sso1

mv libstdc++so608 libstdc++so608bck

ln -s usrlibi386-linux-gnulibstdc++so6 libstdc++so608

Go to the subdirectory optCoinIpoptbuildIpoptcontribMatlabInterfacesrc andopen the Makefile file

sudo apt-get install hardening-wrapper

You need to edit this file to suit the installation for your system setup You may need tomodify MATLAB HOME MEXSUFFIX and MEX as explained in the comments of the Makefile I setthese variables as follows

20

MATLAB HOME = optmatlabR2008a

MEXSUFFIX = mexglx

MEX = optmatlabR2008abinmex

In my case I removed some of the compiler options as it follows

prefix = optCoinIpopt

CXX = optintelcomposer xe 2011 sp19293binia32icpc

CCXXFLAGS = -O3 -pipe -DNDEBUG -Wparentheses -Wreturn-type -Wcast-qual -Wall

-Wpointer-arith -Wwrite-strings -Wconversion -Wno-unknown-pragmas -DMATLAB MEXFILE

LDFLAGS = $(CXXFLAGS) -Wl --rpath -Wl $prefixlibcoin -Wl --rpath -Wl

--rpath -Wl$prefixlibcoinThirdParty

In my case I had to set explicitly the include Matlab folder

o cpp

$(CXX) $(CXXFLAGS) $(INCL) -IoptmatlabR2008aexterninclude -o $ -c $^

Once you set up the Makefile properly type sudo make install in the same directory as theMakefile If the installation procedure was successful you will end up with a MEX file calledipoptmexglx In order to use it in MATLAB you need to tell MATLAB where to find it Todo this just type addpath optCoinIpoptlib in the MATLAB command window after youcheck if the ipoptmexglx file is in this folder

Documentation

More informations can be found in the following documentation

Online documentation httpwwwcoin-ororgIpoptdocumentation3

A PDF version of this documentation can be downloaded here4

A short IPOPT tutorial can be downloaded here5

MATLAB interface Instructions specific to the MATLAB interface can be found at the Matlabinterface page6

223 Getting started with IPOPT and AMPL

option solver ipopt

option ipopt options rsquooption1=value1 option2=value2rsquo

solve

3httpwwwcoin-ororgIpoptdocumentation4httpsprojectscoin-ororgIpoptbrowserstable39Ipoptdocdocumentationpdfformat=raw5httpdropsdagstuhldevolltexte20092089pdf09061WaechterAndreasPaper2089pdf6httpsprojectscoin-ororgIpoptwikiMatlabInterface

21

Table 21 IPOPT directives for AMPL

Option Description

acceptable compl inf tol Acceptance threshold for the complementarity conditionsacceptable constr viol tol Acceptance threshold for the constraint violationacceptable dual inf tol Acceptance threshold for the dual infeasibilityacceptable tol Acceptable convergence tolerance (relative)alpha for y Step size for constraint multipliersbound frac Desired minimal relative distance of initial point to boundbound mult init val Initial value for the bound multipliersbound push Desired minimal absolute distance of initial point to boundbound relax factor Factor for initial relaxation of the boundscompl inf tol Acceptance threshold for the complementarity conditionsconstr mult init max Maximal allowed least-square guess of constraint multipliersconstr viol tol Desired threshold for the constraint violationdiverging iterates tol Threshold for maximal value of primal iteratesdual inf tol Desired threshold for the dual infeasibilityexpect infeasible problem Enable heuristics to quickly detect an infeasible problemfile print level Verbosity level for output filehalt on ampl error Exit with message on evaluation errorhessian approximation Can enable Quasi-Newton approximation of hessianhonor original bounds If no solution might slightly violate boundslinear scaling on demand Enables heuristic for scaling only when seems requiredlinear solver Linear solver to be used for step calculationlinear system scaling Method for scaling the linear systemsma27 pivtol Pivot tolerance for the linear solver MA27ma27 pivtolmax Maximal pivot tolerance for the linear solver MA27ma57 pivtol Pivot tolerance for the linear solver MA57ma57 pivtolmax Maximal pivot tolerance for the linear solver MA57

22

Table 22 IPOPT directives for AMPL

Option Description

max cpu time CPU time limitmax iter Maximum number of iterations

max refinement stepsMaximal number of iterative refinement steps per linearsystem solve

max soc Maximal number of second order correction trial stepsmaxit Maximum number of iterations (same as max iter)

min refinement stepsMinimum number of iterative refinement steps per linearsystem solve

mu init Initial value for the barrier parametermu max Maximal value for barrier parameter for adaptive strategymu oracle Oracle for a new barrier parameter in the adaptive strategymu strategy Update strategy for barrier parameternlp scaling max gradient Maximum gradient after scalingnlp scaling method Select the technique used for scaling the NLPobj scaling factor Scaling factor for the objective functionoption file name File name of options file (default ipoptopt)outlev Verbosity level (same as print level)output file File name of an output file (leave unset for no file output)pardiso matching strategy Matching strategy for linear solver Pardisopardiso out of core power Enables out-of-core version of linear solver Pardisoprint level Verbosity levelprint options documentation Print all available options (for ipoptopt)print user options Toggle printing of user optionsrequired infeasibility reduction Required infeasibility reduction in restoration phaseslack bound frac Desired minimal relative distance of initial slack to boundslack bound push Desired minimal absolute distance of initial slack to boundtol Desired convergence tolerance (relative)

wantsol

solution report without -AMPL sum of1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

warm start bound pushEnables to specify how much should variables should bepushed inside the feasible region

warm start init point Enables to specify bound multiplier values

warm start mult bound pushEnables to specify how much should bound multipliersshould be pushed inside the feasible region

watchdog shortened iter trigger Trigger counter for watchdog procedurewsmp num threads Number of threads to be used in WSMPwsmp pivtol Pivot tolerance for the linear solver WSMPwsmp pivtolmax Maximum pivot tolerance for the linear solver WSMPwsmp scaling Determines how the matrix is scaled by WSMP

23

24

23 KNITRO

ldquoKNITRO is an optimization software library for finding solutions of both continuous (smo-oth) optimization models (with or without constraints) as well as discrete optimization modelswith integer or binary variables (ie mixed integer programs) KNITRO is primarily designedfor finding local optimal solutions of large-scale continuous nonlinear problemsrdquo The KNITROlibrary can be found in httpwwwartelyscom7

231 Basic information

KNITRO is a software package for solving smooth optimization problems with or withoutconstraints

Developed at Ziena Optimization

Operating system(s) KNITRO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) KNITRO is written C C++ Fortran and Java

Programming interfaces(s) Fortan CC++ Java and Microsoft Excel

Modeling language interface(s) KNITRO offers a total of five interfaces Matlab AMPL Math-ematica AIMMS GAMS and MPL

232 Installing KNITRO on Linux

How to get a license

KNITRO is developed by Ziena Optimization LLC and marketed and supported by ArtelysThe first step is to request a license by selecting the download tab8 Along with the academicand commercial trial versions there is a student version limited in problem size (300 variablesand 300 constraints) available for 6 months Each license is personalised hence among otherpersonal information you should inform about

bull the operating system (OS)

bull the OS bit architecture

bull the Machine ID of the computer on which KNITRO will runDownload extract an script available bellow the Machine ID field

After reading and accepting the End User License Agreement the download of the KNITROwill be available and you will get a license key on your email Then you should put the licensefile whose name begins with ziena lic in your $HOME directory or in the $HOMEzienadirectory

How to install

The KNITRO software package for Unix is delivered as a gzipped tar file After downloadingKNITRO you need to unpack it Type in a Terminal window

gunzip knitro-8x-platformnametargz

tar -xvf knitro-8x-platformnametar

sudo mv knitro-8x-z opt

7httpwwwartelyscomindexphppage=knitroamphl=en_EN8httpwwwartelyscomindexphppage=knitroamphl=en_ENdownloads-tab

25

This will create a directory named knitro-8x-z in your opt folder

In order to run KNITRO binary or executable files from anywhere on your Linux computerit is necessary to set several environment variables In particular you must update the PATH

environment variable so that it indicates the location of the knitroampl directory You mustalso update the LD LIBRARY PATH environment variable so that it indicates the location of theKNITRO lib directory Setting the PATH and LD LIBRARY PATH environment variables on a Linuxsystem can be done as follows

echo rsquoexport PATH=$PATHoptknitro-8x-zrsquo gtgt $HOMEbashrcecho rsquoexport PATH=$PATHoptknitro-8x-zknitroamplrsquo gtgt $HOMEbashrcecho rsquoexport LD LIBRARY PATH=$LD LIBRARY PATHoptknitro-8x-zlibrsquo gtgt $HOMEbashrc

Each Matlab release expects the KNITRO library file on Linux to have a specific name There-fore to use KNITRO 8x or later with Matlab release R2008a through 2011b you must createa symbolic link to the expected name Open a Terminal window and in the KNITRO librarydirectory issue the command

bull for Matlab releases R2008a R2008b or R2009aln -s libknitroso800 libknitroso5

bull for Matlab releases R2009b R2010a or R2010bln -s libknitroso800 libknitroso6

bull for Matlab releases R2011a R2011b or R2012aln -s libknitroso800 libknitroso7

Documentation

More informations can be found in the following documentation9

KNITRO 80 User Manual Knitro80 UserManualpdfVersion December 2011 (816 KB)

Specific KNITROMatlab interface documentation can be found on the Matlab OptimizationToolbox webpages10

233 Getting started with KNITRO and AMPL

option solver knitroampl

option knitro options rsquooption1=value1 option2=value2rsquo

solve

9httpwwwartelyscomuploadspdfsKnitro80_UserManualpdf10httpwwwmathworkscomaccesshelpdeskhelptoolboxoptim

26

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 5: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

Work Plan

Project PTDCEEA-CRO1160142009 ndash ldquoOptimal Control for Constrained Hybrid Systemrdquo

The main tasks to be carried out are

1 To understand and obtain a handsndashon knowledge of nonndashlinear programming models cur-rent tools to address them (AMPL MATLAB ) and current solvers (KNITRO WORHPSNOPT ) Comparative study of different solvers

2 To understand optimal control problems and how to transcribe them into NLP to be im-plemented in AMPL or MATLAB

3 To obtain a handsndashon knowledge of current optimal control packages such DIDO ACADOOCPID-DAE ASTOS BOCOP and others To perform a comparative study

4 To help to implement and solve case studies and tests to illustrate theoretical develop-ments

5 To maintain the project webndashpage and documentation accessible to the project team

6 (If time permits) To construct a platform to easily solve sequences of optimal control prob-lems in a receding horizon fashion in order to implement model predictive controllers

TODAY

Months 0 1 2 3 4 5 6 7 8 9 10 11 12

100 completeTask 1

100 completeTask 2

100 completeTask 3

100 completeTask 4

100 completeTask 5

50 completeTask 6

Progress reports on tasks just concluded and ongoing tasks should be produced at the endof months 3 5 7 10 and 12

5

6

Chapter 1

Optimal Control andNonlinear Programming Interfaces

11 Introduction

Optimal control and nonlinear programming interfaces are software used to solve optimalcontrol problems A interface is a software that help us to prepare all data as input to the solverWe can divide the interfaces in two groups optimal control interfaces and nonlinear program-ming interfaces The first ones handle with the discretisation and transcription procedureswhile the other ones leave this work to the programmerresearcher (see Figure 11) Severalinterfaces will be presented in this chapter evolving openndashsource freeware and commercialsoftware working under different operating systems

Figure 11

12 AMPL

ldquoAMPL ndash A Mathematical Programming Language ndash is a comprehensive and powerful alge-braic modeling language for linear and nonlinear optimization problems in discrete or contin-uous variablesrdquo The AMPL library can be found in the official site1

121 Basic information

Developed by Robert Fourer David Gay and Brian Kernighan at Bell Laboratories1httpwwwanmplcom

7

Operating system(s) AMPL can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) AMPL is written in C++ and it is released as open source code under theEclipse Public License (EPL)

122 Installing AMPL on Linux

Download your AMPL executable by selecting the Intel (Pentium-compatible) PCs runningLinux on AMPL website2 or type in a Terminal window

wget httpnetlibsandiagovamplstudentlinuxamplgz

The downloaded file must be decompressed and it must be made executable after which itmay be executed from any directory in your search path

gzip -d amplgz

chmod +x ampl

sudo mkdir -p optampl

sudo mv ampl optampl

echo rsquoEXPORT PATH=$PATHoptamplrsquo gtgt $HOMEbashrc

To complete your AMPL installation you can also download solvers to a directory in yoursearch path To use a downloaded solver set AMPLrsquos solver option to the name of the solverrsquosexecutable file

The student edition of AMPL is limited to 300 variables and 300 constraints and objectives(after presolve) for nonlinear problems

123 Getting started with AMPL

The first step in solving a problem is to load the model variables objective function andconstraints It is usual the create a mod file containing these informations First open a AMPLcommand window by typing ampl in a Terminal window

AMPL commands end with semicolon () Some AMPL basic commands

bull to declare a parameter param ltpar namegt

bull to load a model model ltfile namegtmod

bull to run a AMPL script include ltscript namegtrun

bull to load a data file data ltfile namegtdat

bull to view the content of a variable display ltvar namegt

bull to exit a AMPL command window exit

13 ACADO ndash Automatic Control And Dynamic Optimization

ldquoACADO Toolkit is a software environment and algorithm collection for automatic controland dynamic optimization It provides a general framework for using a great variety of al-gorithms for direct optimal control including model predictive control state and parameterestimation and robust optimizationrdquo The ACADO library can be found in the official site3

2httpnetlibsandiagovamplstudentlinuxamplgz3httpsourceforgenetprojectsacado

8

131 Basic information

Developed under the direction of Moritz Diehl

Operating system(s) ACADO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) ACADO Toolkit is implemented as self-contained C++ code it comes alongwith user-friendly MATLAB interface and is released under the LGP License

132 Installing ACADO on Linux

In order to install ACADO Toolkit under LINUXUNIX operating systems make sure thata C++ compiler is correctly installed For the case that a graphical output is desired Gnuplotshould be installed in addition However ACADO Toolkit will also run without Gnuplot - inthis case the graphical output is simply turned off There are no further dependencies

First download ACADO Toolkit and its licence from the offical website4 and extract the filestar xfvz ACADOtoolkit-102613betatargz

sudo mv ACADOtoolkit-102613beta optacado

Then go to the directory optacado and compile the packagecd optacado

sudo make

sudo make doc (optional)

Finally check whether the installation was successfulcd examplesgetting started

simple ocp

The following documentation5 is available

bull ACADO Toolkit Userrsquos Manual

bull ACADO for Matlab Userrsquos Manual

bull ACADO Toolkit Introductory Talk

14 BOCOP ndash The optimal control solver

ldquoThe BOCOP project aims to develop an open-source toolbox for solving optimal controlproblemsrdquo The BOCOP library can be found in the official site6

141 Basic information

Developed by V Grelard P Martinon and F Bonnans at Inria-Saclay

Operating system(s) BOCOP is available for linux precompiled packages (Mac and Windowsversions are still under testing)

Code language(s) BOCOP requires SciLab and it is released under the Eclipse Public License(EPL)

4httpwwwacadotoolkitorgdownloadphp5httpsourceforgenetpacadowikiDocumentation6httpwwwbocoporg

9

142 Installing BOCOP on Linux

In order to install BOCOP software under LINUXUNIX operating system download BO-COP software from the official website7 and extract the files

tar xvf bocop-103-beta-linux32targz

mv bocop-103-beta-linux32 optbocop

cd optbocop

make

sudo chmod +x bocopsh

Add the following to your bashrc or bash profile

BOCOPDIR=optbocop

export PATH=$PATH$BOCOPDIR

143 Getting started with BOCOP

Read the userrsquos guide8 and run the examples shown in the optbocopproblems folder

15 DIDO ndash Automatic Control And Dynamic Optimization

ldquoDIDO the leading optimal control software powers users by offering the easiest and directsolutions to the most complex problemsrdquo The DIDO software can be found in the official site9

151 Basic information

Developed at Elissar Global

Operating system(s) DIDO can be used on Microsoft Windows

Code language(s) DIDO requires MATLAB and it is released under academic and commerciallicenses

152 Installing DIDO

DIDOrsquos foundation is pseudospectral theory and is the only pseudospectral solution withmathematically proven convergence properties DIDO is a MATLAB program for solving hybridoptimal control problems The generalndashpurpose program is named after Dido the legendaryfounder and first queen of Carthage who is famous in mathematics for her remarkable solutionto a constrained optimal control problem even before the invention of calculus

To install DIDO on your computer you should consider the system requirements

bull WINDOWS

bull MATLAB

and the following steps

7httpwwwbocoporg8httpbocopsaclayinriafrdownload=109httpwwwelissarglobalcomindustryproductssoftware-3

10

STEP 1 Obtain a license for DIDO from Elissar Global at the official site10

STEP 2 After you receive an email with the link to a compressed version of DIDO downloadthe zip file and uncompress it It will automatically uncompress with the right folderstructure

STEP 3 Right click on the DIDO icon and click on PROPERTIES In the Target box type locationof your licensed MATLAB file In the Start in box type the full path name of your DIDOfolder

STEP 4 Doublendashclick on the DIDO ICON (this should start MATLAB and automatically initial-ize DIDO)

(a) In the MATLAB command window type TestDIDOA successful installation should produce the final output on the screenldquoCONGRATULATIONS DIDO TEST WAS SUCCESSFULrdquo

(b) A further test may be performed by typing LanderProblemA successful installation should produce a pretty graph

153 Getting started with DIDO

In the MATLAB command window run[cost primal] = dido(Problem)

where Problem is a structure array with fields cost dynamics events and path that point to theinput files

Problemcost = lsquoMyCostFilersquo

Problemdynamics = lsquoMyDynamicsFilersquo

Problemevents = lsquoMyEventsFilersquo

Problempath = lsquoMyPathFilersquo

The variable primal is used in nearly all DIDO files to pass the states controls parametersand other useful quantities To print out the statendash and controlndashtrajectory in the MATLABcommand window runprimalstates primalcontrols

This generality allows for

bull Fairly complex interior point constraints

bull Pre-defined segments

bull Differentially-flat segments

bull Transition conditions

bull Midndashmaneuver changes in dynamics

bull Multi-dynamical systems

bull Midndashmaneuver changes in the cost function

bull Switches

bull Discrete events10httpwwwelissarglobalcomacademicget-didotry-dido

11

16 ICLOCS ndash Imperial College London Optimal Control Software

ldquoThe code allows users to define and solve optimal control problems with general path andboundary constraints and free or fixed final time It is also possible to include constant designparameters as unknownsrdquo The ICLOCS software can be found in the official site11

161 Basic information

Developed by Paola Falugi Eric Kerrigan and Eugene van Wyk

Operating system(s) ICLOCS can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) ICLOCS is implemented in MATLAB and it is released as open source codeunder the BSD License

162 Installing ICLOCS on Linux

In order to install ICLOCS software under LINUXUNIX operating system download ICLOCSsoftware from the official site12 and extract the files

wget httpwwweeicacukICLOCSICLOCS 02 NItarzip

unzip ICLOCS 02 NItarzip

tar xvf ICLOCS 02 NItar

sudo mv ICLOCS 02 NI opticlocs

In order to use it in MATLAB you need to tell MATLAB where to find it To do this justtype addpath(genpath(rsquoopticlocsrsquo)) in the MATLAB command window

163 Getting started with ICLOCS

Read the userrsquos guide13 and run the examples shown in the opticlocsexamples folder

17 TACO ndash Toolkit for AMPL Control Optimization

ldquoTACO is the Toolkit for AMPL Control Optimization It defines some add-ons to the AMPLmodeling language that allow the elegant formulation of ODEDAE optimal control problemsin AMPLrdquo The TACO toolkit can be found in the official site14

171 Basic information

Developed by Christian Kirches and Sven Leyffer

Operating system(s) TACO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) TACO is written in C

11httpwwweeicacukICLOCS12httpwwweeicacukICLOCS13httpwwweeicacukICLOCSuser_guidepdf14httpwwwiwruni-heidelbergde~ChristianKirchessoftwarehtml

12

172 Installing TACO on Linux

In order to install TADO Toolkit under LINUXUNIX operating systems make sure thatCMAKE and a C++ compiler is correctly installed To install CMAKE type in a Terminal win-dow

wget httpslaunchpadnetubuntu+archiveprimary+filescmake 288origtargz

gunzip cmake 288origtargz

tar xvf cmake 288origtar

cd cmake-288

sudo bootstrap

sudo make

sudo make install

First download TACO Toolkit from the offical site15 and extract the files

wget httpswwwiwruni-heidelbergdegroupsagbockFILEStaco sourcetargzgunzip taco sourcetargz

tar xfvz taco sourcetar

sudo ln -s optCoinIpoptThirdPartyASLlibamplsolvera libamplsolvera

18 Pseudospectral Methods in Optimal Control

181 PSOPT

PSOPT is an open source optimal control software package written in C++ that uses directcollocation methods including pseudospectral and local discretizations available in the officesite16 Pseudospectral methods solve optimal control problems by approximating the time-dependent variables using global polynomials such as Legendre or Chebyshev functions Localdiscretisation methods approximate the time dependent functions using local splines and canbe seen as implementations of implicit Runge-Kutta integrators With both global and localmethods differential equations continuous constraints and integrals associated with the prob-lem are discretised over a grid of nodes Sparse nonlinear programming is then used to findlocal optimal solutions

PSOPT is able to deal with problems with the following characteristics

bull Single or multiphase problems

bull Continuous time nonlinear dynamics

bull Nonlinear path constraints

bull General event constraints

bull Integral constraints

bull Interior point constraints

bull Bounds on controls and state variables

bull General cost function with Lagrange and Mayer terms

15httpwwwiwruni-heidelbergde~ChristianKirchessoftwarehtml16httpwwwpsoptorg

13

bull Linear or nonlinear linkages between phases

bull Fixed or free initial phase time

bull Fixed or free final phase time

bull Optimization of static parameters

bull Optimal parameter estimation given sampled observations

The implementation has the following features

bull Choice between Legendre Chebyshev central differences trapezoidal or Hermite-Simpsondiscretisation

bull Large scale nonlinear programming using IPOPT and (optionally) SNOPT

bull Estimation of the discretisation error

bull Automatic mesh refinement

bull Automatic scaling

bull Automatic differentiation using the ADOL-C library

bull Numerical differentiation by using sparse finite differences

bull Automatic identification of the sparsity of the derivative matrices

bull DAE formulation so that differential and algebraic constraints can be implemented in thesame C++ function

bull Easy to use interface to GNUplot to produce graphical output including 2D plots 3Dcurves and surfaces and polar plots

bull Automatic generation of LaTeX code to produce a table that summarizes the mesh refine-ment process

Full details on PSOPT and its features can be found in its documentation17

182 GPOPS-II - MATLAB Optimal Control Software

GPOPS-II is a general purpose optimal control software available in the office site18 GPOPS-II is a new open-source MATLAB optimal control software that implements a brand new hp-adaptive Legendre-Gauss-Radau quadrature integral pseudospectral method for solving generalnonlinear optimal control problems Using GPOPS-II the optimal control problem is transcribedto a nonlinear programming problem (NLP) The NLP is then solved using either the solverSNOPT or the solver IPOPT

The following are some the key features of GPOPS-II

bull Allows for an extremely general formulation of the optimal control problem

bull Allows for inclusion of integral constraints and highly general boundary conditions

bull Complete first and second sparse finite-differencing of optimal control problem to com-pute all derivatives required by the NLP solver

17httpcodegooglecomppsoptdownloadsdetailname=PSOPT_Manual_R3pdfampcan=2ampq=18httpwwwgpopsorg

14

bull The latest advances in mesh refinement including hpndashadaptive pseudospectral methods

bull Gaussian quadrature integration methods for rapid convergence

bull Highly accurate costate estimation

bull Inclusion of the NLP solver SNOPT (for Academic Users) and IPOPT (for Not-for-Profitand Commercial Users)

bull No third-party products other than MATLAB are required

More information about the methodology used in GPOPS-II can be found by reading therelevant articles in the open literature19

The fees for using GPOPS-II are as follows

bull For students and all others employed at academic institutions NO CHARGE

bull US Federal State or Local Government NO CHARGE

bull Users at not-for-profit institutions (including non-US Government agencies) or commer-cial institutions LICENSING FEES APPLY

19httpvdolmaeufledu

15

16

Chapter 2

Nonlinear Programming Solvers

21 Introduction

A solver is a software used to compute numerical solution of an optimal control problemIn general an optimal controlnonlinear programming interface is needed to prepare all dataas input to the solver (see Figure 21) Several solvers will be presented in this chapter evolvingopenndashsource freeware and commercial software working under different operating systemsSince all solvers require configuration lists of parameters will be presented for some of them

Figure 21

22 IPOPT ndash Interior Point OPTimizer

ldquoIPOPT is a software package for largendashscale nonlinear optimisation It is designed to find(local) solutions of mathematical optimisation problemsrdquo The IPOPT library can be found inhttpwwwartelyscom1

221 Basic information

Developed by Andreas Wachter and Carl Laird

Operating system(s) IPOPT can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) IPOPT is written in C++ and is released as open source code under theEclipse Public License (EPL)

1httpwwwartelyscomindexphppage=knitroamphl=en_EN

17

Modeling language interface(s) IPOPT can be used as a library that can be linked to C++ C orFortran code as well as a solver executable for the AMPL modelling environment Thepackage includes interfaces to CUTEr optimisation testing environment as well as theMATLAB and R programming environments

222 Installing IPOPT

How to install IPOPT on Linux

Getting the IPOPT code via subversion

sudo svn co httpsprojectscoin-ororgsvnIpoptstable310 optCoinIpopt

This will create a directory named CoinIpopt in your opt folder

BLAS routines that provide standard building blocks for performing basic vector and matrixoperations

LAPACK routines for solving systems of simultaneous linear equations least-squares solutionsof linear systems of equations eigenvalue problems and singular value problems

HSL state-of-the-art packages for large-scale scientific computation

ASL AMPL Solver Library allows to read the nl files and provides the automatic differentia-tion functionality

MUMPS MUltifrontal Massively Parallel sparse direct Solver

METIS programs for partitioning graphs partitioning finite element meshes and producing fillreducing orderings for sparse matrices

To install IPOPT just run

cd optCoinIpoptThirdPartyBlas

getBlas

cd Lapack

getLapack

cd ASL

getASL

cd Mumps

getMumps

cd Metis

getMetis

IPOPT requires a sparse symmetric linear solver There are different possibilities The userrsquosguide of IPOPT suggests that you use HSL subroutines

1 Go to HSL website2 and find the package list

2 Click on MA27 read the license and submit the registration form

3 Go back to the package list

4 Click on MC19 read the license and submit the registration form

2httphslrlacukarchivehslarchivehtml

18

You will receive in your email the links to obtain the HSL packages Download them to your$HOME directory and run

gunzip ma27-100targz

tar xvf ma27-100tar

gunzip mc19-100targz

tar xvf mc19-100tar

cd optCoinIpoptThirdPartyHSL

sudo mv -r $HOMEmc19-100

sudo mv -r $HOMEma27-100

cd mc19-100

sudo configure

sudo make install

sudo make check

cd ma27-100

sudo configure

sudo make install

sudo make check

The make check command will check if the installation of the HSL packages was successfulReturn to the optCoinIpopt directory and continue the IPOPT installation

cd

sudo mkdir build

cd build

sudo configure

sudo make test

sudo make install

If you get the error message ldquoerrorlsquoNULLrsquo was not declared in this scoperdquo when run-ning sudo make then you have to edit the following filesoptCoinIpoptIpoptsrcCommonIpSmartPtrhpp

optCoinIpoptIpoptsrcAlgorithmLinearSolversIpTripletToCSRConvertercpp

by adding include ltcstddefgt

How to install IPOPT on Mac OS X

In a Terminal window install homebrewusrbinruby -e $(usrbincurl -fsSL httpsrawgithubcommxclhomebrewmaster

LibraryContributionsinstall homebrewrb)

With this software you can easily install dependency packages like wget and gfortran

brew install wget

brew install gfortran

sudo wget httpwwwcoin-ororgdownloadsourceIpoptIpopt-3102zip

sudo unzip Ipopt-3102zip

cd Ipopt-3102

Download Netlib BlasLapack

cd optCoinIpoptThirdPartyBlas

sudo getBlas

19

cd Lapack

sudo getLapack

cd ASL

sudo getASL

cd Mumps

sudo getMumps

cd Metis

sudo getMetis

Make a build for a 64bit target

sudo mkdir build64

cd build64

sudo configure --disable-shared --with-blas=BUILD --with-lapack=BUILD F77=gfortran

FFLAGS=-fexceptions -m64 -fbackslash CFLAGS=-fno-common -no-cpp-precomp -fexceptions

-arch x86 64 -m64 CXXFLAGS=-fno-common -no-cpp-precomp -fexceptions -arch x86 64

-m64

Compile test and install

make

make test

make install

Add the following to your bashrc or bash profile

IPOPTDIR=optCoinIpoptbuild

export PATH=$PATH$IPOPTDIR$IPOPTDIRbinexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyASLexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyBlaslibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyHSLlibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyLapacklibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMetislibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMumpslibs

How to install the Matlab interface on Linux

First of all be sure

cd optmatlabR2008asysosglnx86

mv libgcc sso1 libgcc sso1bck

ln -s libi386-linux-gnulibgcc sso1 libgcc sso1

mv libstdc++so608 libstdc++so608bck

ln -s usrlibi386-linux-gnulibstdc++so6 libstdc++so608

Go to the subdirectory optCoinIpoptbuildIpoptcontribMatlabInterfacesrc andopen the Makefile file

sudo apt-get install hardening-wrapper

You need to edit this file to suit the installation for your system setup You may need tomodify MATLAB HOME MEXSUFFIX and MEX as explained in the comments of the Makefile I setthese variables as follows

20

MATLAB HOME = optmatlabR2008a

MEXSUFFIX = mexglx

MEX = optmatlabR2008abinmex

In my case I removed some of the compiler options as it follows

prefix = optCoinIpopt

CXX = optintelcomposer xe 2011 sp19293binia32icpc

CCXXFLAGS = -O3 -pipe -DNDEBUG -Wparentheses -Wreturn-type -Wcast-qual -Wall

-Wpointer-arith -Wwrite-strings -Wconversion -Wno-unknown-pragmas -DMATLAB MEXFILE

LDFLAGS = $(CXXFLAGS) -Wl --rpath -Wl $prefixlibcoin -Wl --rpath -Wl

--rpath -Wl$prefixlibcoinThirdParty

In my case I had to set explicitly the include Matlab folder

o cpp

$(CXX) $(CXXFLAGS) $(INCL) -IoptmatlabR2008aexterninclude -o $ -c $^

Once you set up the Makefile properly type sudo make install in the same directory as theMakefile If the installation procedure was successful you will end up with a MEX file calledipoptmexglx In order to use it in MATLAB you need to tell MATLAB where to find it Todo this just type addpath optCoinIpoptlib in the MATLAB command window after youcheck if the ipoptmexglx file is in this folder

Documentation

More informations can be found in the following documentation

Online documentation httpwwwcoin-ororgIpoptdocumentation3

A PDF version of this documentation can be downloaded here4

A short IPOPT tutorial can be downloaded here5

MATLAB interface Instructions specific to the MATLAB interface can be found at the Matlabinterface page6

223 Getting started with IPOPT and AMPL

option solver ipopt

option ipopt options rsquooption1=value1 option2=value2rsquo

solve

3httpwwwcoin-ororgIpoptdocumentation4httpsprojectscoin-ororgIpoptbrowserstable39Ipoptdocdocumentationpdfformat=raw5httpdropsdagstuhldevolltexte20092089pdf09061WaechterAndreasPaper2089pdf6httpsprojectscoin-ororgIpoptwikiMatlabInterface

21

Table 21 IPOPT directives for AMPL

Option Description

acceptable compl inf tol Acceptance threshold for the complementarity conditionsacceptable constr viol tol Acceptance threshold for the constraint violationacceptable dual inf tol Acceptance threshold for the dual infeasibilityacceptable tol Acceptable convergence tolerance (relative)alpha for y Step size for constraint multipliersbound frac Desired minimal relative distance of initial point to boundbound mult init val Initial value for the bound multipliersbound push Desired minimal absolute distance of initial point to boundbound relax factor Factor for initial relaxation of the boundscompl inf tol Acceptance threshold for the complementarity conditionsconstr mult init max Maximal allowed least-square guess of constraint multipliersconstr viol tol Desired threshold for the constraint violationdiverging iterates tol Threshold for maximal value of primal iteratesdual inf tol Desired threshold for the dual infeasibilityexpect infeasible problem Enable heuristics to quickly detect an infeasible problemfile print level Verbosity level for output filehalt on ampl error Exit with message on evaluation errorhessian approximation Can enable Quasi-Newton approximation of hessianhonor original bounds If no solution might slightly violate boundslinear scaling on demand Enables heuristic for scaling only when seems requiredlinear solver Linear solver to be used for step calculationlinear system scaling Method for scaling the linear systemsma27 pivtol Pivot tolerance for the linear solver MA27ma27 pivtolmax Maximal pivot tolerance for the linear solver MA27ma57 pivtol Pivot tolerance for the linear solver MA57ma57 pivtolmax Maximal pivot tolerance for the linear solver MA57

22

Table 22 IPOPT directives for AMPL

Option Description

max cpu time CPU time limitmax iter Maximum number of iterations

max refinement stepsMaximal number of iterative refinement steps per linearsystem solve

max soc Maximal number of second order correction trial stepsmaxit Maximum number of iterations (same as max iter)

min refinement stepsMinimum number of iterative refinement steps per linearsystem solve

mu init Initial value for the barrier parametermu max Maximal value for barrier parameter for adaptive strategymu oracle Oracle for a new barrier parameter in the adaptive strategymu strategy Update strategy for barrier parameternlp scaling max gradient Maximum gradient after scalingnlp scaling method Select the technique used for scaling the NLPobj scaling factor Scaling factor for the objective functionoption file name File name of options file (default ipoptopt)outlev Verbosity level (same as print level)output file File name of an output file (leave unset for no file output)pardiso matching strategy Matching strategy for linear solver Pardisopardiso out of core power Enables out-of-core version of linear solver Pardisoprint level Verbosity levelprint options documentation Print all available options (for ipoptopt)print user options Toggle printing of user optionsrequired infeasibility reduction Required infeasibility reduction in restoration phaseslack bound frac Desired minimal relative distance of initial slack to boundslack bound push Desired minimal absolute distance of initial slack to boundtol Desired convergence tolerance (relative)

wantsol

solution report without -AMPL sum of1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

warm start bound pushEnables to specify how much should variables should bepushed inside the feasible region

warm start init point Enables to specify bound multiplier values

warm start mult bound pushEnables to specify how much should bound multipliersshould be pushed inside the feasible region

watchdog shortened iter trigger Trigger counter for watchdog procedurewsmp num threads Number of threads to be used in WSMPwsmp pivtol Pivot tolerance for the linear solver WSMPwsmp pivtolmax Maximum pivot tolerance for the linear solver WSMPwsmp scaling Determines how the matrix is scaled by WSMP

23

24

23 KNITRO

ldquoKNITRO is an optimization software library for finding solutions of both continuous (smo-oth) optimization models (with or without constraints) as well as discrete optimization modelswith integer or binary variables (ie mixed integer programs) KNITRO is primarily designedfor finding local optimal solutions of large-scale continuous nonlinear problemsrdquo The KNITROlibrary can be found in httpwwwartelyscom7

231 Basic information

KNITRO is a software package for solving smooth optimization problems with or withoutconstraints

Developed at Ziena Optimization

Operating system(s) KNITRO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) KNITRO is written C C++ Fortran and Java

Programming interfaces(s) Fortan CC++ Java and Microsoft Excel

Modeling language interface(s) KNITRO offers a total of five interfaces Matlab AMPL Math-ematica AIMMS GAMS and MPL

232 Installing KNITRO on Linux

How to get a license

KNITRO is developed by Ziena Optimization LLC and marketed and supported by ArtelysThe first step is to request a license by selecting the download tab8 Along with the academicand commercial trial versions there is a student version limited in problem size (300 variablesand 300 constraints) available for 6 months Each license is personalised hence among otherpersonal information you should inform about

bull the operating system (OS)

bull the OS bit architecture

bull the Machine ID of the computer on which KNITRO will runDownload extract an script available bellow the Machine ID field

After reading and accepting the End User License Agreement the download of the KNITROwill be available and you will get a license key on your email Then you should put the licensefile whose name begins with ziena lic in your $HOME directory or in the $HOMEzienadirectory

How to install

The KNITRO software package for Unix is delivered as a gzipped tar file After downloadingKNITRO you need to unpack it Type in a Terminal window

gunzip knitro-8x-platformnametargz

tar -xvf knitro-8x-platformnametar

sudo mv knitro-8x-z opt

7httpwwwartelyscomindexphppage=knitroamphl=en_EN8httpwwwartelyscomindexphppage=knitroamphl=en_ENdownloads-tab

25

This will create a directory named knitro-8x-z in your opt folder

In order to run KNITRO binary or executable files from anywhere on your Linux computerit is necessary to set several environment variables In particular you must update the PATH

environment variable so that it indicates the location of the knitroampl directory You mustalso update the LD LIBRARY PATH environment variable so that it indicates the location of theKNITRO lib directory Setting the PATH and LD LIBRARY PATH environment variables on a Linuxsystem can be done as follows

echo rsquoexport PATH=$PATHoptknitro-8x-zrsquo gtgt $HOMEbashrcecho rsquoexport PATH=$PATHoptknitro-8x-zknitroamplrsquo gtgt $HOMEbashrcecho rsquoexport LD LIBRARY PATH=$LD LIBRARY PATHoptknitro-8x-zlibrsquo gtgt $HOMEbashrc

Each Matlab release expects the KNITRO library file on Linux to have a specific name There-fore to use KNITRO 8x or later with Matlab release R2008a through 2011b you must createa symbolic link to the expected name Open a Terminal window and in the KNITRO librarydirectory issue the command

bull for Matlab releases R2008a R2008b or R2009aln -s libknitroso800 libknitroso5

bull for Matlab releases R2009b R2010a or R2010bln -s libknitroso800 libknitroso6

bull for Matlab releases R2011a R2011b or R2012aln -s libknitroso800 libknitroso7

Documentation

More informations can be found in the following documentation9

KNITRO 80 User Manual Knitro80 UserManualpdfVersion December 2011 (816 KB)

Specific KNITROMatlab interface documentation can be found on the Matlab OptimizationToolbox webpages10

233 Getting started with KNITRO and AMPL

option solver knitroampl

option knitro options rsquooption1=value1 option2=value2rsquo

solve

9httpwwwartelyscomuploadspdfsKnitro80_UserManualpdf10httpwwwmathworkscomaccesshelpdeskhelptoolboxoptim

26

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 6: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

6

Chapter 1

Optimal Control andNonlinear Programming Interfaces

11 Introduction

Optimal control and nonlinear programming interfaces are software used to solve optimalcontrol problems A interface is a software that help us to prepare all data as input to the solverWe can divide the interfaces in two groups optimal control interfaces and nonlinear program-ming interfaces The first ones handle with the discretisation and transcription procedureswhile the other ones leave this work to the programmerresearcher (see Figure 11) Severalinterfaces will be presented in this chapter evolving openndashsource freeware and commercialsoftware working under different operating systems

Figure 11

12 AMPL

ldquoAMPL ndash A Mathematical Programming Language ndash is a comprehensive and powerful alge-braic modeling language for linear and nonlinear optimization problems in discrete or contin-uous variablesrdquo The AMPL library can be found in the official site1

121 Basic information

Developed by Robert Fourer David Gay and Brian Kernighan at Bell Laboratories1httpwwwanmplcom

7

Operating system(s) AMPL can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) AMPL is written in C++ and it is released as open source code under theEclipse Public License (EPL)

122 Installing AMPL on Linux

Download your AMPL executable by selecting the Intel (Pentium-compatible) PCs runningLinux on AMPL website2 or type in a Terminal window

wget httpnetlibsandiagovamplstudentlinuxamplgz

The downloaded file must be decompressed and it must be made executable after which itmay be executed from any directory in your search path

gzip -d amplgz

chmod +x ampl

sudo mkdir -p optampl

sudo mv ampl optampl

echo rsquoEXPORT PATH=$PATHoptamplrsquo gtgt $HOMEbashrc

To complete your AMPL installation you can also download solvers to a directory in yoursearch path To use a downloaded solver set AMPLrsquos solver option to the name of the solverrsquosexecutable file

The student edition of AMPL is limited to 300 variables and 300 constraints and objectives(after presolve) for nonlinear problems

123 Getting started with AMPL

The first step in solving a problem is to load the model variables objective function andconstraints It is usual the create a mod file containing these informations First open a AMPLcommand window by typing ampl in a Terminal window

AMPL commands end with semicolon () Some AMPL basic commands

bull to declare a parameter param ltpar namegt

bull to load a model model ltfile namegtmod

bull to run a AMPL script include ltscript namegtrun

bull to load a data file data ltfile namegtdat

bull to view the content of a variable display ltvar namegt

bull to exit a AMPL command window exit

13 ACADO ndash Automatic Control And Dynamic Optimization

ldquoACADO Toolkit is a software environment and algorithm collection for automatic controland dynamic optimization It provides a general framework for using a great variety of al-gorithms for direct optimal control including model predictive control state and parameterestimation and robust optimizationrdquo The ACADO library can be found in the official site3

2httpnetlibsandiagovamplstudentlinuxamplgz3httpsourceforgenetprojectsacado

8

131 Basic information

Developed under the direction of Moritz Diehl

Operating system(s) ACADO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) ACADO Toolkit is implemented as self-contained C++ code it comes alongwith user-friendly MATLAB interface and is released under the LGP License

132 Installing ACADO on Linux

In order to install ACADO Toolkit under LINUXUNIX operating systems make sure thata C++ compiler is correctly installed For the case that a graphical output is desired Gnuplotshould be installed in addition However ACADO Toolkit will also run without Gnuplot - inthis case the graphical output is simply turned off There are no further dependencies

First download ACADO Toolkit and its licence from the offical website4 and extract the filestar xfvz ACADOtoolkit-102613betatargz

sudo mv ACADOtoolkit-102613beta optacado

Then go to the directory optacado and compile the packagecd optacado

sudo make

sudo make doc (optional)

Finally check whether the installation was successfulcd examplesgetting started

simple ocp

The following documentation5 is available

bull ACADO Toolkit Userrsquos Manual

bull ACADO for Matlab Userrsquos Manual

bull ACADO Toolkit Introductory Talk

14 BOCOP ndash The optimal control solver

ldquoThe BOCOP project aims to develop an open-source toolbox for solving optimal controlproblemsrdquo The BOCOP library can be found in the official site6

141 Basic information

Developed by V Grelard P Martinon and F Bonnans at Inria-Saclay

Operating system(s) BOCOP is available for linux precompiled packages (Mac and Windowsversions are still under testing)

Code language(s) BOCOP requires SciLab and it is released under the Eclipse Public License(EPL)

4httpwwwacadotoolkitorgdownloadphp5httpsourceforgenetpacadowikiDocumentation6httpwwwbocoporg

9

142 Installing BOCOP on Linux

In order to install BOCOP software under LINUXUNIX operating system download BO-COP software from the official website7 and extract the files

tar xvf bocop-103-beta-linux32targz

mv bocop-103-beta-linux32 optbocop

cd optbocop

make

sudo chmod +x bocopsh

Add the following to your bashrc or bash profile

BOCOPDIR=optbocop

export PATH=$PATH$BOCOPDIR

143 Getting started with BOCOP

Read the userrsquos guide8 and run the examples shown in the optbocopproblems folder

15 DIDO ndash Automatic Control And Dynamic Optimization

ldquoDIDO the leading optimal control software powers users by offering the easiest and directsolutions to the most complex problemsrdquo The DIDO software can be found in the official site9

151 Basic information

Developed at Elissar Global

Operating system(s) DIDO can be used on Microsoft Windows

Code language(s) DIDO requires MATLAB and it is released under academic and commerciallicenses

152 Installing DIDO

DIDOrsquos foundation is pseudospectral theory and is the only pseudospectral solution withmathematically proven convergence properties DIDO is a MATLAB program for solving hybridoptimal control problems The generalndashpurpose program is named after Dido the legendaryfounder and first queen of Carthage who is famous in mathematics for her remarkable solutionto a constrained optimal control problem even before the invention of calculus

To install DIDO on your computer you should consider the system requirements

bull WINDOWS

bull MATLAB

and the following steps

7httpwwwbocoporg8httpbocopsaclayinriafrdownload=109httpwwwelissarglobalcomindustryproductssoftware-3

10

STEP 1 Obtain a license for DIDO from Elissar Global at the official site10

STEP 2 After you receive an email with the link to a compressed version of DIDO downloadthe zip file and uncompress it It will automatically uncompress with the right folderstructure

STEP 3 Right click on the DIDO icon and click on PROPERTIES In the Target box type locationof your licensed MATLAB file In the Start in box type the full path name of your DIDOfolder

STEP 4 Doublendashclick on the DIDO ICON (this should start MATLAB and automatically initial-ize DIDO)

(a) In the MATLAB command window type TestDIDOA successful installation should produce the final output on the screenldquoCONGRATULATIONS DIDO TEST WAS SUCCESSFULrdquo

(b) A further test may be performed by typing LanderProblemA successful installation should produce a pretty graph

153 Getting started with DIDO

In the MATLAB command window run[cost primal] = dido(Problem)

where Problem is a structure array with fields cost dynamics events and path that point to theinput files

Problemcost = lsquoMyCostFilersquo

Problemdynamics = lsquoMyDynamicsFilersquo

Problemevents = lsquoMyEventsFilersquo

Problempath = lsquoMyPathFilersquo

The variable primal is used in nearly all DIDO files to pass the states controls parametersand other useful quantities To print out the statendash and controlndashtrajectory in the MATLABcommand window runprimalstates primalcontrols

This generality allows for

bull Fairly complex interior point constraints

bull Pre-defined segments

bull Differentially-flat segments

bull Transition conditions

bull Midndashmaneuver changes in dynamics

bull Multi-dynamical systems

bull Midndashmaneuver changes in the cost function

bull Switches

bull Discrete events10httpwwwelissarglobalcomacademicget-didotry-dido

11

16 ICLOCS ndash Imperial College London Optimal Control Software

ldquoThe code allows users to define and solve optimal control problems with general path andboundary constraints and free or fixed final time It is also possible to include constant designparameters as unknownsrdquo The ICLOCS software can be found in the official site11

161 Basic information

Developed by Paola Falugi Eric Kerrigan and Eugene van Wyk

Operating system(s) ICLOCS can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) ICLOCS is implemented in MATLAB and it is released as open source codeunder the BSD License

162 Installing ICLOCS on Linux

In order to install ICLOCS software under LINUXUNIX operating system download ICLOCSsoftware from the official site12 and extract the files

wget httpwwweeicacukICLOCSICLOCS 02 NItarzip

unzip ICLOCS 02 NItarzip

tar xvf ICLOCS 02 NItar

sudo mv ICLOCS 02 NI opticlocs

In order to use it in MATLAB you need to tell MATLAB where to find it To do this justtype addpath(genpath(rsquoopticlocsrsquo)) in the MATLAB command window

163 Getting started with ICLOCS

Read the userrsquos guide13 and run the examples shown in the opticlocsexamples folder

17 TACO ndash Toolkit for AMPL Control Optimization

ldquoTACO is the Toolkit for AMPL Control Optimization It defines some add-ons to the AMPLmodeling language that allow the elegant formulation of ODEDAE optimal control problemsin AMPLrdquo The TACO toolkit can be found in the official site14

171 Basic information

Developed by Christian Kirches and Sven Leyffer

Operating system(s) TACO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) TACO is written in C

11httpwwweeicacukICLOCS12httpwwweeicacukICLOCS13httpwwweeicacukICLOCSuser_guidepdf14httpwwwiwruni-heidelbergde~ChristianKirchessoftwarehtml

12

172 Installing TACO on Linux

In order to install TADO Toolkit under LINUXUNIX operating systems make sure thatCMAKE and a C++ compiler is correctly installed To install CMAKE type in a Terminal win-dow

wget httpslaunchpadnetubuntu+archiveprimary+filescmake 288origtargz

gunzip cmake 288origtargz

tar xvf cmake 288origtar

cd cmake-288

sudo bootstrap

sudo make

sudo make install

First download TACO Toolkit from the offical site15 and extract the files

wget httpswwwiwruni-heidelbergdegroupsagbockFILEStaco sourcetargzgunzip taco sourcetargz

tar xfvz taco sourcetar

sudo ln -s optCoinIpoptThirdPartyASLlibamplsolvera libamplsolvera

18 Pseudospectral Methods in Optimal Control

181 PSOPT

PSOPT is an open source optimal control software package written in C++ that uses directcollocation methods including pseudospectral and local discretizations available in the officesite16 Pseudospectral methods solve optimal control problems by approximating the time-dependent variables using global polynomials such as Legendre or Chebyshev functions Localdiscretisation methods approximate the time dependent functions using local splines and canbe seen as implementations of implicit Runge-Kutta integrators With both global and localmethods differential equations continuous constraints and integrals associated with the prob-lem are discretised over a grid of nodes Sparse nonlinear programming is then used to findlocal optimal solutions

PSOPT is able to deal with problems with the following characteristics

bull Single or multiphase problems

bull Continuous time nonlinear dynamics

bull Nonlinear path constraints

bull General event constraints

bull Integral constraints

bull Interior point constraints

bull Bounds on controls and state variables

bull General cost function with Lagrange and Mayer terms

15httpwwwiwruni-heidelbergde~ChristianKirchessoftwarehtml16httpwwwpsoptorg

13

bull Linear or nonlinear linkages between phases

bull Fixed or free initial phase time

bull Fixed or free final phase time

bull Optimization of static parameters

bull Optimal parameter estimation given sampled observations

The implementation has the following features

bull Choice between Legendre Chebyshev central differences trapezoidal or Hermite-Simpsondiscretisation

bull Large scale nonlinear programming using IPOPT and (optionally) SNOPT

bull Estimation of the discretisation error

bull Automatic mesh refinement

bull Automatic scaling

bull Automatic differentiation using the ADOL-C library

bull Numerical differentiation by using sparse finite differences

bull Automatic identification of the sparsity of the derivative matrices

bull DAE formulation so that differential and algebraic constraints can be implemented in thesame C++ function

bull Easy to use interface to GNUplot to produce graphical output including 2D plots 3Dcurves and surfaces and polar plots

bull Automatic generation of LaTeX code to produce a table that summarizes the mesh refine-ment process

Full details on PSOPT and its features can be found in its documentation17

182 GPOPS-II - MATLAB Optimal Control Software

GPOPS-II is a general purpose optimal control software available in the office site18 GPOPS-II is a new open-source MATLAB optimal control software that implements a brand new hp-adaptive Legendre-Gauss-Radau quadrature integral pseudospectral method for solving generalnonlinear optimal control problems Using GPOPS-II the optimal control problem is transcribedto a nonlinear programming problem (NLP) The NLP is then solved using either the solverSNOPT or the solver IPOPT

The following are some the key features of GPOPS-II

bull Allows for an extremely general formulation of the optimal control problem

bull Allows for inclusion of integral constraints and highly general boundary conditions

bull Complete first and second sparse finite-differencing of optimal control problem to com-pute all derivatives required by the NLP solver

17httpcodegooglecomppsoptdownloadsdetailname=PSOPT_Manual_R3pdfampcan=2ampq=18httpwwwgpopsorg

14

bull The latest advances in mesh refinement including hpndashadaptive pseudospectral methods

bull Gaussian quadrature integration methods for rapid convergence

bull Highly accurate costate estimation

bull Inclusion of the NLP solver SNOPT (for Academic Users) and IPOPT (for Not-for-Profitand Commercial Users)

bull No third-party products other than MATLAB are required

More information about the methodology used in GPOPS-II can be found by reading therelevant articles in the open literature19

The fees for using GPOPS-II are as follows

bull For students and all others employed at academic institutions NO CHARGE

bull US Federal State or Local Government NO CHARGE

bull Users at not-for-profit institutions (including non-US Government agencies) or commer-cial institutions LICENSING FEES APPLY

19httpvdolmaeufledu

15

16

Chapter 2

Nonlinear Programming Solvers

21 Introduction

A solver is a software used to compute numerical solution of an optimal control problemIn general an optimal controlnonlinear programming interface is needed to prepare all dataas input to the solver (see Figure 21) Several solvers will be presented in this chapter evolvingopenndashsource freeware and commercial software working under different operating systemsSince all solvers require configuration lists of parameters will be presented for some of them

Figure 21

22 IPOPT ndash Interior Point OPTimizer

ldquoIPOPT is a software package for largendashscale nonlinear optimisation It is designed to find(local) solutions of mathematical optimisation problemsrdquo The IPOPT library can be found inhttpwwwartelyscom1

221 Basic information

Developed by Andreas Wachter and Carl Laird

Operating system(s) IPOPT can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) IPOPT is written in C++ and is released as open source code under theEclipse Public License (EPL)

1httpwwwartelyscomindexphppage=knitroamphl=en_EN

17

Modeling language interface(s) IPOPT can be used as a library that can be linked to C++ C orFortran code as well as a solver executable for the AMPL modelling environment Thepackage includes interfaces to CUTEr optimisation testing environment as well as theMATLAB and R programming environments

222 Installing IPOPT

How to install IPOPT on Linux

Getting the IPOPT code via subversion

sudo svn co httpsprojectscoin-ororgsvnIpoptstable310 optCoinIpopt

This will create a directory named CoinIpopt in your opt folder

BLAS routines that provide standard building blocks for performing basic vector and matrixoperations

LAPACK routines for solving systems of simultaneous linear equations least-squares solutionsof linear systems of equations eigenvalue problems and singular value problems

HSL state-of-the-art packages for large-scale scientific computation

ASL AMPL Solver Library allows to read the nl files and provides the automatic differentia-tion functionality

MUMPS MUltifrontal Massively Parallel sparse direct Solver

METIS programs for partitioning graphs partitioning finite element meshes and producing fillreducing orderings for sparse matrices

To install IPOPT just run

cd optCoinIpoptThirdPartyBlas

getBlas

cd Lapack

getLapack

cd ASL

getASL

cd Mumps

getMumps

cd Metis

getMetis

IPOPT requires a sparse symmetric linear solver There are different possibilities The userrsquosguide of IPOPT suggests that you use HSL subroutines

1 Go to HSL website2 and find the package list

2 Click on MA27 read the license and submit the registration form

3 Go back to the package list

4 Click on MC19 read the license and submit the registration form

2httphslrlacukarchivehslarchivehtml

18

You will receive in your email the links to obtain the HSL packages Download them to your$HOME directory and run

gunzip ma27-100targz

tar xvf ma27-100tar

gunzip mc19-100targz

tar xvf mc19-100tar

cd optCoinIpoptThirdPartyHSL

sudo mv -r $HOMEmc19-100

sudo mv -r $HOMEma27-100

cd mc19-100

sudo configure

sudo make install

sudo make check

cd ma27-100

sudo configure

sudo make install

sudo make check

The make check command will check if the installation of the HSL packages was successfulReturn to the optCoinIpopt directory and continue the IPOPT installation

cd

sudo mkdir build

cd build

sudo configure

sudo make test

sudo make install

If you get the error message ldquoerrorlsquoNULLrsquo was not declared in this scoperdquo when run-ning sudo make then you have to edit the following filesoptCoinIpoptIpoptsrcCommonIpSmartPtrhpp

optCoinIpoptIpoptsrcAlgorithmLinearSolversIpTripletToCSRConvertercpp

by adding include ltcstddefgt

How to install IPOPT on Mac OS X

In a Terminal window install homebrewusrbinruby -e $(usrbincurl -fsSL httpsrawgithubcommxclhomebrewmaster

LibraryContributionsinstall homebrewrb)

With this software you can easily install dependency packages like wget and gfortran

brew install wget

brew install gfortran

sudo wget httpwwwcoin-ororgdownloadsourceIpoptIpopt-3102zip

sudo unzip Ipopt-3102zip

cd Ipopt-3102

Download Netlib BlasLapack

cd optCoinIpoptThirdPartyBlas

sudo getBlas

19

cd Lapack

sudo getLapack

cd ASL

sudo getASL

cd Mumps

sudo getMumps

cd Metis

sudo getMetis

Make a build for a 64bit target

sudo mkdir build64

cd build64

sudo configure --disable-shared --with-blas=BUILD --with-lapack=BUILD F77=gfortran

FFLAGS=-fexceptions -m64 -fbackslash CFLAGS=-fno-common -no-cpp-precomp -fexceptions

-arch x86 64 -m64 CXXFLAGS=-fno-common -no-cpp-precomp -fexceptions -arch x86 64

-m64

Compile test and install

make

make test

make install

Add the following to your bashrc or bash profile

IPOPTDIR=optCoinIpoptbuild

export PATH=$PATH$IPOPTDIR$IPOPTDIRbinexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyASLexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyBlaslibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyHSLlibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyLapacklibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMetislibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMumpslibs

How to install the Matlab interface on Linux

First of all be sure

cd optmatlabR2008asysosglnx86

mv libgcc sso1 libgcc sso1bck

ln -s libi386-linux-gnulibgcc sso1 libgcc sso1

mv libstdc++so608 libstdc++so608bck

ln -s usrlibi386-linux-gnulibstdc++so6 libstdc++so608

Go to the subdirectory optCoinIpoptbuildIpoptcontribMatlabInterfacesrc andopen the Makefile file

sudo apt-get install hardening-wrapper

You need to edit this file to suit the installation for your system setup You may need tomodify MATLAB HOME MEXSUFFIX and MEX as explained in the comments of the Makefile I setthese variables as follows

20

MATLAB HOME = optmatlabR2008a

MEXSUFFIX = mexglx

MEX = optmatlabR2008abinmex

In my case I removed some of the compiler options as it follows

prefix = optCoinIpopt

CXX = optintelcomposer xe 2011 sp19293binia32icpc

CCXXFLAGS = -O3 -pipe -DNDEBUG -Wparentheses -Wreturn-type -Wcast-qual -Wall

-Wpointer-arith -Wwrite-strings -Wconversion -Wno-unknown-pragmas -DMATLAB MEXFILE

LDFLAGS = $(CXXFLAGS) -Wl --rpath -Wl $prefixlibcoin -Wl --rpath -Wl

--rpath -Wl$prefixlibcoinThirdParty

In my case I had to set explicitly the include Matlab folder

o cpp

$(CXX) $(CXXFLAGS) $(INCL) -IoptmatlabR2008aexterninclude -o $ -c $^

Once you set up the Makefile properly type sudo make install in the same directory as theMakefile If the installation procedure was successful you will end up with a MEX file calledipoptmexglx In order to use it in MATLAB you need to tell MATLAB where to find it Todo this just type addpath optCoinIpoptlib in the MATLAB command window after youcheck if the ipoptmexglx file is in this folder

Documentation

More informations can be found in the following documentation

Online documentation httpwwwcoin-ororgIpoptdocumentation3

A PDF version of this documentation can be downloaded here4

A short IPOPT tutorial can be downloaded here5

MATLAB interface Instructions specific to the MATLAB interface can be found at the Matlabinterface page6

223 Getting started with IPOPT and AMPL

option solver ipopt

option ipopt options rsquooption1=value1 option2=value2rsquo

solve

3httpwwwcoin-ororgIpoptdocumentation4httpsprojectscoin-ororgIpoptbrowserstable39Ipoptdocdocumentationpdfformat=raw5httpdropsdagstuhldevolltexte20092089pdf09061WaechterAndreasPaper2089pdf6httpsprojectscoin-ororgIpoptwikiMatlabInterface

21

Table 21 IPOPT directives for AMPL

Option Description

acceptable compl inf tol Acceptance threshold for the complementarity conditionsacceptable constr viol tol Acceptance threshold for the constraint violationacceptable dual inf tol Acceptance threshold for the dual infeasibilityacceptable tol Acceptable convergence tolerance (relative)alpha for y Step size for constraint multipliersbound frac Desired minimal relative distance of initial point to boundbound mult init val Initial value for the bound multipliersbound push Desired minimal absolute distance of initial point to boundbound relax factor Factor for initial relaxation of the boundscompl inf tol Acceptance threshold for the complementarity conditionsconstr mult init max Maximal allowed least-square guess of constraint multipliersconstr viol tol Desired threshold for the constraint violationdiverging iterates tol Threshold for maximal value of primal iteratesdual inf tol Desired threshold for the dual infeasibilityexpect infeasible problem Enable heuristics to quickly detect an infeasible problemfile print level Verbosity level for output filehalt on ampl error Exit with message on evaluation errorhessian approximation Can enable Quasi-Newton approximation of hessianhonor original bounds If no solution might slightly violate boundslinear scaling on demand Enables heuristic for scaling only when seems requiredlinear solver Linear solver to be used for step calculationlinear system scaling Method for scaling the linear systemsma27 pivtol Pivot tolerance for the linear solver MA27ma27 pivtolmax Maximal pivot tolerance for the linear solver MA27ma57 pivtol Pivot tolerance for the linear solver MA57ma57 pivtolmax Maximal pivot tolerance for the linear solver MA57

22

Table 22 IPOPT directives for AMPL

Option Description

max cpu time CPU time limitmax iter Maximum number of iterations

max refinement stepsMaximal number of iterative refinement steps per linearsystem solve

max soc Maximal number of second order correction trial stepsmaxit Maximum number of iterations (same as max iter)

min refinement stepsMinimum number of iterative refinement steps per linearsystem solve

mu init Initial value for the barrier parametermu max Maximal value for barrier parameter for adaptive strategymu oracle Oracle for a new barrier parameter in the adaptive strategymu strategy Update strategy for barrier parameternlp scaling max gradient Maximum gradient after scalingnlp scaling method Select the technique used for scaling the NLPobj scaling factor Scaling factor for the objective functionoption file name File name of options file (default ipoptopt)outlev Verbosity level (same as print level)output file File name of an output file (leave unset for no file output)pardiso matching strategy Matching strategy for linear solver Pardisopardiso out of core power Enables out-of-core version of linear solver Pardisoprint level Verbosity levelprint options documentation Print all available options (for ipoptopt)print user options Toggle printing of user optionsrequired infeasibility reduction Required infeasibility reduction in restoration phaseslack bound frac Desired minimal relative distance of initial slack to boundslack bound push Desired minimal absolute distance of initial slack to boundtol Desired convergence tolerance (relative)

wantsol

solution report without -AMPL sum of1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

warm start bound pushEnables to specify how much should variables should bepushed inside the feasible region

warm start init point Enables to specify bound multiplier values

warm start mult bound pushEnables to specify how much should bound multipliersshould be pushed inside the feasible region

watchdog shortened iter trigger Trigger counter for watchdog procedurewsmp num threads Number of threads to be used in WSMPwsmp pivtol Pivot tolerance for the linear solver WSMPwsmp pivtolmax Maximum pivot tolerance for the linear solver WSMPwsmp scaling Determines how the matrix is scaled by WSMP

23

24

23 KNITRO

ldquoKNITRO is an optimization software library for finding solutions of both continuous (smo-oth) optimization models (with or without constraints) as well as discrete optimization modelswith integer or binary variables (ie mixed integer programs) KNITRO is primarily designedfor finding local optimal solutions of large-scale continuous nonlinear problemsrdquo The KNITROlibrary can be found in httpwwwartelyscom7

231 Basic information

KNITRO is a software package for solving smooth optimization problems with or withoutconstraints

Developed at Ziena Optimization

Operating system(s) KNITRO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) KNITRO is written C C++ Fortran and Java

Programming interfaces(s) Fortan CC++ Java and Microsoft Excel

Modeling language interface(s) KNITRO offers a total of five interfaces Matlab AMPL Math-ematica AIMMS GAMS and MPL

232 Installing KNITRO on Linux

How to get a license

KNITRO is developed by Ziena Optimization LLC and marketed and supported by ArtelysThe first step is to request a license by selecting the download tab8 Along with the academicand commercial trial versions there is a student version limited in problem size (300 variablesand 300 constraints) available for 6 months Each license is personalised hence among otherpersonal information you should inform about

bull the operating system (OS)

bull the OS bit architecture

bull the Machine ID of the computer on which KNITRO will runDownload extract an script available bellow the Machine ID field

After reading and accepting the End User License Agreement the download of the KNITROwill be available and you will get a license key on your email Then you should put the licensefile whose name begins with ziena lic in your $HOME directory or in the $HOMEzienadirectory

How to install

The KNITRO software package for Unix is delivered as a gzipped tar file After downloadingKNITRO you need to unpack it Type in a Terminal window

gunzip knitro-8x-platformnametargz

tar -xvf knitro-8x-platformnametar

sudo mv knitro-8x-z opt

7httpwwwartelyscomindexphppage=knitroamphl=en_EN8httpwwwartelyscomindexphppage=knitroamphl=en_ENdownloads-tab

25

This will create a directory named knitro-8x-z in your opt folder

In order to run KNITRO binary or executable files from anywhere on your Linux computerit is necessary to set several environment variables In particular you must update the PATH

environment variable so that it indicates the location of the knitroampl directory You mustalso update the LD LIBRARY PATH environment variable so that it indicates the location of theKNITRO lib directory Setting the PATH and LD LIBRARY PATH environment variables on a Linuxsystem can be done as follows

echo rsquoexport PATH=$PATHoptknitro-8x-zrsquo gtgt $HOMEbashrcecho rsquoexport PATH=$PATHoptknitro-8x-zknitroamplrsquo gtgt $HOMEbashrcecho rsquoexport LD LIBRARY PATH=$LD LIBRARY PATHoptknitro-8x-zlibrsquo gtgt $HOMEbashrc

Each Matlab release expects the KNITRO library file on Linux to have a specific name There-fore to use KNITRO 8x or later with Matlab release R2008a through 2011b you must createa symbolic link to the expected name Open a Terminal window and in the KNITRO librarydirectory issue the command

bull for Matlab releases R2008a R2008b or R2009aln -s libknitroso800 libknitroso5

bull for Matlab releases R2009b R2010a or R2010bln -s libknitroso800 libknitroso6

bull for Matlab releases R2011a R2011b or R2012aln -s libknitroso800 libknitroso7

Documentation

More informations can be found in the following documentation9

KNITRO 80 User Manual Knitro80 UserManualpdfVersion December 2011 (816 KB)

Specific KNITROMatlab interface documentation can be found on the Matlab OptimizationToolbox webpages10

233 Getting started with KNITRO and AMPL

option solver knitroampl

option knitro options rsquooption1=value1 option2=value2rsquo

solve

9httpwwwartelyscomuploadspdfsKnitro80_UserManualpdf10httpwwwmathworkscomaccesshelpdeskhelptoolboxoptim

26

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 7: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

Chapter 1

Optimal Control andNonlinear Programming Interfaces

11 Introduction

Optimal control and nonlinear programming interfaces are software used to solve optimalcontrol problems A interface is a software that help us to prepare all data as input to the solverWe can divide the interfaces in two groups optimal control interfaces and nonlinear program-ming interfaces The first ones handle with the discretisation and transcription procedureswhile the other ones leave this work to the programmerresearcher (see Figure 11) Severalinterfaces will be presented in this chapter evolving openndashsource freeware and commercialsoftware working under different operating systems

Figure 11

12 AMPL

ldquoAMPL ndash A Mathematical Programming Language ndash is a comprehensive and powerful alge-braic modeling language for linear and nonlinear optimization problems in discrete or contin-uous variablesrdquo The AMPL library can be found in the official site1

121 Basic information

Developed by Robert Fourer David Gay and Brian Kernighan at Bell Laboratories1httpwwwanmplcom

7

Operating system(s) AMPL can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) AMPL is written in C++ and it is released as open source code under theEclipse Public License (EPL)

122 Installing AMPL on Linux

Download your AMPL executable by selecting the Intel (Pentium-compatible) PCs runningLinux on AMPL website2 or type in a Terminal window

wget httpnetlibsandiagovamplstudentlinuxamplgz

The downloaded file must be decompressed and it must be made executable after which itmay be executed from any directory in your search path

gzip -d amplgz

chmod +x ampl

sudo mkdir -p optampl

sudo mv ampl optampl

echo rsquoEXPORT PATH=$PATHoptamplrsquo gtgt $HOMEbashrc

To complete your AMPL installation you can also download solvers to a directory in yoursearch path To use a downloaded solver set AMPLrsquos solver option to the name of the solverrsquosexecutable file

The student edition of AMPL is limited to 300 variables and 300 constraints and objectives(after presolve) for nonlinear problems

123 Getting started with AMPL

The first step in solving a problem is to load the model variables objective function andconstraints It is usual the create a mod file containing these informations First open a AMPLcommand window by typing ampl in a Terminal window

AMPL commands end with semicolon () Some AMPL basic commands

bull to declare a parameter param ltpar namegt

bull to load a model model ltfile namegtmod

bull to run a AMPL script include ltscript namegtrun

bull to load a data file data ltfile namegtdat

bull to view the content of a variable display ltvar namegt

bull to exit a AMPL command window exit

13 ACADO ndash Automatic Control And Dynamic Optimization

ldquoACADO Toolkit is a software environment and algorithm collection for automatic controland dynamic optimization It provides a general framework for using a great variety of al-gorithms for direct optimal control including model predictive control state and parameterestimation and robust optimizationrdquo The ACADO library can be found in the official site3

2httpnetlibsandiagovamplstudentlinuxamplgz3httpsourceforgenetprojectsacado

8

131 Basic information

Developed under the direction of Moritz Diehl

Operating system(s) ACADO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) ACADO Toolkit is implemented as self-contained C++ code it comes alongwith user-friendly MATLAB interface and is released under the LGP License

132 Installing ACADO on Linux

In order to install ACADO Toolkit under LINUXUNIX operating systems make sure thata C++ compiler is correctly installed For the case that a graphical output is desired Gnuplotshould be installed in addition However ACADO Toolkit will also run without Gnuplot - inthis case the graphical output is simply turned off There are no further dependencies

First download ACADO Toolkit and its licence from the offical website4 and extract the filestar xfvz ACADOtoolkit-102613betatargz

sudo mv ACADOtoolkit-102613beta optacado

Then go to the directory optacado and compile the packagecd optacado

sudo make

sudo make doc (optional)

Finally check whether the installation was successfulcd examplesgetting started

simple ocp

The following documentation5 is available

bull ACADO Toolkit Userrsquos Manual

bull ACADO for Matlab Userrsquos Manual

bull ACADO Toolkit Introductory Talk

14 BOCOP ndash The optimal control solver

ldquoThe BOCOP project aims to develop an open-source toolbox for solving optimal controlproblemsrdquo The BOCOP library can be found in the official site6

141 Basic information

Developed by V Grelard P Martinon and F Bonnans at Inria-Saclay

Operating system(s) BOCOP is available for linux precompiled packages (Mac and Windowsversions are still under testing)

Code language(s) BOCOP requires SciLab and it is released under the Eclipse Public License(EPL)

4httpwwwacadotoolkitorgdownloadphp5httpsourceforgenetpacadowikiDocumentation6httpwwwbocoporg

9

142 Installing BOCOP on Linux

In order to install BOCOP software under LINUXUNIX operating system download BO-COP software from the official website7 and extract the files

tar xvf bocop-103-beta-linux32targz

mv bocop-103-beta-linux32 optbocop

cd optbocop

make

sudo chmod +x bocopsh

Add the following to your bashrc or bash profile

BOCOPDIR=optbocop

export PATH=$PATH$BOCOPDIR

143 Getting started with BOCOP

Read the userrsquos guide8 and run the examples shown in the optbocopproblems folder

15 DIDO ndash Automatic Control And Dynamic Optimization

ldquoDIDO the leading optimal control software powers users by offering the easiest and directsolutions to the most complex problemsrdquo The DIDO software can be found in the official site9

151 Basic information

Developed at Elissar Global

Operating system(s) DIDO can be used on Microsoft Windows

Code language(s) DIDO requires MATLAB and it is released under academic and commerciallicenses

152 Installing DIDO

DIDOrsquos foundation is pseudospectral theory and is the only pseudospectral solution withmathematically proven convergence properties DIDO is a MATLAB program for solving hybridoptimal control problems The generalndashpurpose program is named after Dido the legendaryfounder and first queen of Carthage who is famous in mathematics for her remarkable solutionto a constrained optimal control problem even before the invention of calculus

To install DIDO on your computer you should consider the system requirements

bull WINDOWS

bull MATLAB

and the following steps

7httpwwwbocoporg8httpbocopsaclayinriafrdownload=109httpwwwelissarglobalcomindustryproductssoftware-3

10

STEP 1 Obtain a license for DIDO from Elissar Global at the official site10

STEP 2 After you receive an email with the link to a compressed version of DIDO downloadthe zip file and uncompress it It will automatically uncompress with the right folderstructure

STEP 3 Right click on the DIDO icon and click on PROPERTIES In the Target box type locationof your licensed MATLAB file In the Start in box type the full path name of your DIDOfolder

STEP 4 Doublendashclick on the DIDO ICON (this should start MATLAB and automatically initial-ize DIDO)

(a) In the MATLAB command window type TestDIDOA successful installation should produce the final output on the screenldquoCONGRATULATIONS DIDO TEST WAS SUCCESSFULrdquo

(b) A further test may be performed by typing LanderProblemA successful installation should produce a pretty graph

153 Getting started with DIDO

In the MATLAB command window run[cost primal] = dido(Problem)

where Problem is a structure array with fields cost dynamics events and path that point to theinput files

Problemcost = lsquoMyCostFilersquo

Problemdynamics = lsquoMyDynamicsFilersquo

Problemevents = lsquoMyEventsFilersquo

Problempath = lsquoMyPathFilersquo

The variable primal is used in nearly all DIDO files to pass the states controls parametersand other useful quantities To print out the statendash and controlndashtrajectory in the MATLABcommand window runprimalstates primalcontrols

This generality allows for

bull Fairly complex interior point constraints

bull Pre-defined segments

bull Differentially-flat segments

bull Transition conditions

bull Midndashmaneuver changes in dynamics

bull Multi-dynamical systems

bull Midndashmaneuver changes in the cost function

bull Switches

bull Discrete events10httpwwwelissarglobalcomacademicget-didotry-dido

11

16 ICLOCS ndash Imperial College London Optimal Control Software

ldquoThe code allows users to define and solve optimal control problems with general path andboundary constraints and free or fixed final time It is also possible to include constant designparameters as unknownsrdquo The ICLOCS software can be found in the official site11

161 Basic information

Developed by Paola Falugi Eric Kerrigan and Eugene van Wyk

Operating system(s) ICLOCS can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) ICLOCS is implemented in MATLAB and it is released as open source codeunder the BSD License

162 Installing ICLOCS on Linux

In order to install ICLOCS software under LINUXUNIX operating system download ICLOCSsoftware from the official site12 and extract the files

wget httpwwweeicacukICLOCSICLOCS 02 NItarzip

unzip ICLOCS 02 NItarzip

tar xvf ICLOCS 02 NItar

sudo mv ICLOCS 02 NI opticlocs

In order to use it in MATLAB you need to tell MATLAB where to find it To do this justtype addpath(genpath(rsquoopticlocsrsquo)) in the MATLAB command window

163 Getting started with ICLOCS

Read the userrsquos guide13 and run the examples shown in the opticlocsexamples folder

17 TACO ndash Toolkit for AMPL Control Optimization

ldquoTACO is the Toolkit for AMPL Control Optimization It defines some add-ons to the AMPLmodeling language that allow the elegant formulation of ODEDAE optimal control problemsin AMPLrdquo The TACO toolkit can be found in the official site14

171 Basic information

Developed by Christian Kirches and Sven Leyffer

Operating system(s) TACO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) TACO is written in C

11httpwwweeicacukICLOCS12httpwwweeicacukICLOCS13httpwwweeicacukICLOCSuser_guidepdf14httpwwwiwruni-heidelbergde~ChristianKirchessoftwarehtml

12

172 Installing TACO on Linux

In order to install TADO Toolkit under LINUXUNIX operating systems make sure thatCMAKE and a C++ compiler is correctly installed To install CMAKE type in a Terminal win-dow

wget httpslaunchpadnetubuntu+archiveprimary+filescmake 288origtargz

gunzip cmake 288origtargz

tar xvf cmake 288origtar

cd cmake-288

sudo bootstrap

sudo make

sudo make install

First download TACO Toolkit from the offical site15 and extract the files

wget httpswwwiwruni-heidelbergdegroupsagbockFILEStaco sourcetargzgunzip taco sourcetargz

tar xfvz taco sourcetar

sudo ln -s optCoinIpoptThirdPartyASLlibamplsolvera libamplsolvera

18 Pseudospectral Methods in Optimal Control

181 PSOPT

PSOPT is an open source optimal control software package written in C++ that uses directcollocation methods including pseudospectral and local discretizations available in the officesite16 Pseudospectral methods solve optimal control problems by approximating the time-dependent variables using global polynomials such as Legendre or Chebyshev functions Localdiscretisation methods approximate the time dependent functions using local splines and canbe seen as implementations of implicit Runge-Kutta integrators With both global and localmethods differential equations continuous constraints and integrals associated with the prob-lem are discretised over a grid of nodes Sparse nonlinear programming is then used to findlocal optimal solutions

PSOPT is able to deal with problems with the following characteristics

bull Single or multiphase problems

bull Continuous time nonlinear dynamics

bull Nonlinear path constraints

bull General event constraints

bull Integral constraints

bull Interior point constraints

bull Bounds on controls and state variables

bull General cost function with Lagrange and Mayer terms

15httpwwwiwruni-heidelbergde~ChristianKirchessoftwarehtml16httpwwwpsoptorg

13

bull Linear or nonlinear linkages between phases

bull Fixed or free initial phase time

bull Fixed or free final phase time

bull Optimization of static parameters

bull Optimal parameter estimation given sampled observations

The implementation has the following features

bull Choice between Legendre Chebyshev central differences trapezoidal or Hermite-Simpsondiscretisation

bull Large scale nonlinear programming using IPOPT and (optionally) SNOPT

bull Estimation of the discretisation error

bull Automatic mesh refinement

bull Automatic scaling

bull Automatic differentiation using the ADOL-C library

bull Numerical differentiation by using sparse finite differences

bull Automatic identification of the sparsity of the derivative matrices

bull DAE formulation so that differential and algebraic constraints can be implemented in thesame C++ function

bull Easy to use interface to GNUplot to produce graphical output including 2D plots 3Dcurves and surfaces and polar plots

bull Automatic generation of LaTeX code to produce a table that summarizes the mesh refine-ment process

Full details on PSOPT and its features can be found in its documentation17

182 GPOPS-II - MATLAB Optimal Control Software

GPOPS-II is a general purpose optimal control software available in the office site18 GPOPS-II is a new open-source MATLAB optimal control software that implements a brand new hp-adaptive Legendre-Gauss-Radau quadrature integral pseudospectral method for solving generalnonlinear optimal control problems Using GPOPS-II the optimal control problem is transcribedto a nonlinear programming problem (NLP) The NLP is then solved using either the solverSNOPT or the solver IPOPT

The following are some the key features of GPOPS-II

bull Allows for an extremely general formulation of the optimal control problem

bull Allows for inclusion of integral constraints and highly general boundary conditions

bull Complete first and second sparse finite-differencing of optimal control problem to com-pute all derivatives required by the NLP solver

17httpcodegooglecomppsoptdownloadsdetailname=PSOPT_Manual_R3pdfampcan=2ampq=18httpwwwgpopsorg

14

bull The latest advances in mesh refinement including hpndashadaptive pseudospectral methods

bull Gaussian quadrature integration methods for rapid convergence

bull Highly accurate costate estimation

bull Inclusion of the NLP solver SNOPT (for Academic Users) and IPOPT (for Not-for-Profitand Commercial Users)

bull No third-party products other than MATLAB are required

More information about the methodology used in GPOPS-II can be found by reading therelevant articles in the open literature19

The fees for using GPOPS-II are as follows

bull For students and all others employed at academic institutions NO CHARGE

bull US Federal State or Local Government NO CHARGE

bull Users at not-for-profit institutions (including non-US Government agencies) or commer-cial institutions LICENSING FEES APPLY

19httpvdolmaeufledu

15

16

Chapter 2

Nonlinear Programming Solvers

21 Introduction

A solver is a software used to compute numerical solution of an optimal control problemIn general an optimal controlnonlinear programming interface is needed to prepare all dataas input to the solver (see Figure 21) Several solvers will be presented in this chapter evolvingopenndashsource freeware and commercial software working under different operating systemsSince all solvers require configuration lists of parameters will be presented for some of them

Figure 21

22 IPOPT ndash Interior Point OPTimizer

ldquoIPOPT is a software package for largendashscale nonlinear optimisation It is designed to find(local) solutions of mathematical optimisation problemsrdquo The IPOPT library can be found inhttpwwwartelyscom1

221 Basic information

Developed by Andreas Wachter and Carl Laird

Operating system(s) IPOPT can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) IPOPT is written in C++ and is released as open source code under theEclipse Public License (EPL)

1httpwwwartelyscomindexphppage=knitroamphl=en_EN

17

Modeling language interface(s) IPOPT can be used as a library that can be linked to C++ C orFortran code as well as a solver executable for the AMPL modelling environment Thepackage includes interfaces to CUTEr optimisation testing environment as well as theMATLAB and R programming environments

222 Installing IPOPT

How to install IPOPT on Linux

Getting the IPOPT code via subversion

sudo svn co httpsprojectscoin-ororgsvnIpoptstable310 optCoinIpopt

This will create a directory named CoinIpopt in your opt folder

BLAS routines that provide standard building blocks for performing basic vector and matrixoperations

LAPACK routines for solving systems of simultaneous linear equations least-squares solutionsof linear systems of equations eigenvalue problems and singular value problems

HSL state-of-the-art packages for large-scale scientific computation

ASL AMPL Solver Library allows to read the nl files and provides the automatic differentia-tion functionality

MUMPS MUltifrontal Massively Parallel sparse direct Solver

METIS programs for partitioning graphs partitioning finite element meshes and producing fillreducing orderings for sparse matrices

To install IPOPT just run

cd optCoinIpoptThirdPartyBlas

getBlas

cd Lapack

getLapack

cd ASL

getASL

cd Mumps

getMumps

cd Metis

getMetis

IPOPT requires a sparse symmetric linear solver There are different possibilities The userrsquosguide of IPOPT suggests that you use HSL subroutines

1 Go to HSL website2 and find the package list

2 Click on MA27 read the license and submit the registration form

3 Go back to the package list

4 Click on MC19 read the license and submit the registration form

2httphslrlacukarchivehslarchivehtml

18

You will receive in your email the links to obtain the HSL packages Download them to your$HOME directory and run

gunzip ma27-100targz

tar xvf ma27-100tar

gunzip mc19-100targz

tar xvf mc19-100tar

cd optCoinIpoptThirdPartyHSL

sudo mv -r $HOMEmc19-100

sudo mv -r $HOMEma27-100

cd mc19-100

sudo configure

sudo make install

sudo make check

cd ma27-100

sudo configure

sudo make install

sudo make check

The make check command will check if the installation of the HSL packages was successfulReturn to the optCoinIpopt directory and continue the IPOPT installation

cd

sudo mkdir build

cd build

sudo configure

sudo make test

sudo make install

If you get the error message ldquoerrorlsquoNULLrsquo was not declared in this scoperdquo when run-ning sudo make then you have to edit the following filesoptCoinIpoptIpoptsrcCommonIpSmartPtrhpp

optCoinIpoptIpoptsrcAlgorithmLinearSolversIpTripletToCSRConvertercpp

by adding include ltcstddefgt

How to install IPOPT on Mac OS X

In a Terminal window install homebrewusrbinruby -e $(usrbincurl -fsSL httpsrawgithubcommxclhomebrewmaster

LibraryContributionsinstall homebrewrb)

With this software you can easily install dependency packages like wget and gfortran

brew install wget

brew install gfortran

sudo wget httpwwwcoin-ororgdownloadsourceIpoptIpopt-3102zip

sudo unzip Ipopt-3102zip

cd Ipopt-3102

Download Netlib BlasLapack

cd optCoinIpoptThirdPartyBlas

sudo getBlas

19

cd Lapack

sudo getLapack

cd ASL

sudo getASL

cd Mumps

sudo getMumps

cd Metis

sudo getMetis

Make a build for a 64bit target

sudo mkdir build64

cd build64

sudo configure --disable-shared --with-blas=BUILD --with-lapack=BUILD F77=gfortran

FFLAGS=-fexceptions -m64 -fbackslash CFLAGS=-fno-common -no-cpp-precomp -fexceptions

-arch x86 64 -m64 CXXFLAGS=-fno-common -no-cpp-precomp -fexceptions -arch x86 64

-m64

Compile test and install

make

make test

make install

Add the following to your bashrc or bash profile

IPOPTDIR=optCoinIpoptbuild

export PATH=$PATH$IPOPTDIR$IPOPTDIRbinexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyASLexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyBlaslibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyHSLlibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyLapacklibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMetislibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMumpslibs

How to install the Matlab interface on Linux

First of all be sure

cd optmatlabR2008asysosglnx86

mv libgcc sso1 libgcc sso1bck

ln -s libi386-linux-gnulibgcc sso1 libgcc sso1

mv libstdc++so608 libstdc++so608bck

ln -s usrlibi386-linux-gnulibstdc++so6 libstdc++so608

Go to the subdirectory optCoinIpoptbuildIpoptcontribMatlabInterfacesrc andopen the Makefile file

sudo apt-get install hardening-wrapper

You need to edit this file to suit the installation for your system setup You may need tomodify MATLAB HOME MEXSUFFIX and MEX as explained in the comments of the Makefile I setthese variables as follows

20

MATLAB HOME = optmatlabR2008a

MEXSUFFIX = mexglx

MEX = optmatlabR2008abinmex

In my case I removed some of the compiler options as it follows

prefix = optCoinIpopt

CXX = optintelcomposer xe 2011 sp19293binia32icpc

CCXXFLAGS = -O3 -pipe -DNDEBUG -Wparentheses -Wreturn-type -Wcast-qual -Wall

-Wpointer-arith -Wwrite-strings -Wconversion -Wno-unknown-pragmas -DMATLAB MEXFILE

LDFLAGS = $(CXXFLAGS) -Wl --rpath -Wl $prefixlibcoin -Wl --rpath -Wl

--rpath -Wl$prefixlibcoinThirdParty

In my case I had to set explicitly the include Matlab folder

o cpp

$(CXX) $(CXXFLAGS) $(INCL) -IoptmatlabR2008aexterninclude -o $ -c $^

Once you set up the Makefile properly type sudo make install in the same directory as theMakefile If the installation procedure was successful you will end up with a MEX file calledipoptmexglx In order to use it in MATLAB you need to tell MATLAB where to find it Todo this just type addpath optCoinIpoptlib in the MATLAB command window after youcheck if the ipoptmexglx file is in this folder

Documentation

More informations can be found in the following documentation

Online documentation httpwwwcoin-ororgIpoptdocumentation3

A PDF version of this documentation can be downloaded here4

A short IPOPT tutorial can be downloaded here5

MATLAB interface Instructions specific to the MATLAB interface can be found at the Matlabinterface page6

223 Getting started with IPOPT and AMPL

option solver ipopt

option ipopt options rsquooption1=value1 option2=value2rsquo

solve

3httpwwwcoin-ororgIpoptdocumentation4httpsprojectscoin-ororgIpoptbrowserstable39Ipoptdocdocumentationpdfformat=raw5httpdropsdagstuhldevolltexte20092089pdf09061WaechterAndreasPaper2089pdf6httpsprojectscoin-ororgIpoptwikiMatlabInterface

21

Table 21 IPOPT directives for AMPL

Option Description

acceptable compl inf tol Acceptance threshold for the complementarity conditionsacceptable constr viol tol Acceptance threshold for the constraint violationacceptable dual inf tol Acceptance threshold for the dual infeasibilityacceptable tol Acceptable convergence tolerance (relative)alpha for y Step size for constraint multipliersbound frac Desired minimal relative distance of initial point to boundbound mult init val Initial value for the bound multipliersbound push Desired minimal absolute distance of initial point to boundbound relax factor Factor for initial relaxation of the boundscompl inf tol Acceptance threshold for the complementarity conditionsconstr mult init max Maximal allowed least-square guess of constraint multipliersconstr viol tol Desired threshold for the constraint violationdiverging iterates tol Threshold for maximal value of primal iteratesdual inf tol Desired threshold for the dual infeasibilityexpect infeasible problem Enable heuristics to quickly detect an infeasible problemfile print level Verbosity level for output filehalt on ampl error Exit with message on evaluation errorhessian approximation Can enable Quasi-Newton approximation of hessianhonor original bounds If no solution might slightly violate boundslinear scaling on demand Enables heuristic for scaling only when seems requiredlinear solver Linear solver to be used for step calculationlinear system scaling Method for scaling the linear systemsma27 pivtol Pivot tolerance for the linear solver MA27ma27 pivtolmax Maximal pivot tolerance for the linear solver MA27ma57 pivtol Pivot tolerance for the linear solver MA57ma57 pivtolmax Maximal pivot tolerance for the linear solver MA57

22

Table 22 IPOPT directives for AMPL

Option Description

max cpu time CPU time limitmax iter Maximum number of iterations

max refinement stepsMaximal number of iterative refinement steps per linearsystem solve

max soc Maximal number of second order correction trial stepsmaxit Maximum number of iterations (same as max iter)

min refinement stepsMinimum number of iterative refinement steps per linearsystem solve

mu init Initial value for the barrier parametermu max Maximal value for barrier parameter for adaptive strategymu oracle Oracle for a new barrier parameter in the adaptive strategymu strategy Update strategy for barrier parameternlp scaling max gradient Maximum gradient after scalingnlp scaling method Select the technique used for scaling the NLPobj scaling factor Scaling factor for the objective functionoption file name File name of options file (default ipoptopt)outlev Verbosity level (same as print level)output file File name of an output file (leave unset for no file output)pardiso matching strategy Matching strategy for linear solver Pardisopardiso out of core power Enables out-of-core version of linear solver Pardisoprint level Verbosity levelprint options documentation Print all available options (for ipoptopt)print user options Toggle printing of user optionsrequired infeasibility reduction Required infeasibility reduction in restoration phaseslack bound frac Desired minimal relative distance of initial slack to boundslack bound push Desired minimal absolute distance of initial slack to boundtol Desired convergence tolerance (relative)

wantsol

solution report without -AMPL sum of1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

warm start bound pushEnables to specify how much should variables should bepushed inside the feasible region

warm start init point Enables to specify bound multiplier values

warm start mult bound pushEnables to specify how much should bound multipliersshould be pushed inside the feasible region

watchdog shortened iter trigger Trigger counter for watchdog procedurewsmp num threads Number of threads to be used in WSMPwsmp pivtol Pivot tolerance for the linear solver WSMPwsmp pivtolmax Maximum pivot tolerance for the linear solver WSMPwsmp scaling Determines how the matrix is scaled by WSMP

23

24

23 KNITRO

ldquoKNITRO is an optimization software library for finding solutions of both continuous (smo-oth) optimization models (with or without constraints) as well as discrete optimization modelswith integer or binary variables (ie mixed integer programs) KNITRO is primarily designedfor finding local optimal solutions of large-scale continuous nonlinear problemsrdquo The KNITROlibrary can be found in httpwwwartelyscom7

231 Basic information

KNITRO is a software package for solving smooth optimization problems with or withoutconstraints

Developed at Ziena Optimization

Operating system(s) KNITRO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) KNITRO is written C C++ Fortran and Java

Programming interfaces(s) Fortan CC++ Java and Microsoft Excel

Modeling language interface(s) KNITRO offers a total of five interfaces Matlab AMPL Math-ematica AIMMS GAMS and MPL

232 Installing KNITRO on Linux

How to get a license

KNITRO is developed by Ziena Optimization LLC and marketed and supported by ArtelysThe first step is to request a license by selecting the download tab8 Along with the academicand commercial trial versions there is a student version limited in problem size (300 variablesand 300 constraints) available for 6 months Each license is personalised hence among otherpersonal information you should inform about

bull the operating system (OS)

bull the OS bit architecture

bull the Machine ID of the computer on which KNITRO will runDownload extract an script available bellow the Machine ID field

After reading and accepting the End User License Agreement the download of the KNITROwill be available and you will get a license key on your email Then you should put the licensefile whose name begins with ziena lic in your $HOME directory or in the $HOMEzienadirectory

How to install

The KNITRO software package for Unix is delivered as a gzipped tar file After downloadingKNITRO you need to unpack it Type in a Terminal window

gunzip knitro-8x-platformnametargz

tar -xvf knitro-8x-platformnametar

sudo mv knitro-8x-z opt

7httpwwwartelyscomindexphppage=knitroamphl=en_EN8httpwwwartelyscomindexphppage=knitroamphl=en_ENdownloads-tab

25

This will create a directory named knitro-8x-z in your opt folder

In order to run KNITRO binary or executable files from anywhere on your Linux computerit is necessary to set several environment variables In particular you must update the PATH

environment variable so that it indicates the location of the knitroampl directory You mustalso update the LD LIBRARY PATH environment variable so that it indicates the location of theKNITRO lib directory Setting the PATH and LD LIBRARY PATH environment variables on a Linuxsystem can be done as follows

echo rsquoexport PATH=$PATHoptknitro-8x-zrsquo gtgt $HOMEbashrcecho rsquoexport PATH=$PATHoptknitro-8x-zknitroamplrsquo gtgt $HOMEbashrcecho rsquoexport LD LIBRARY PATH=$LD LIBRARY PATHoptknitro-8x-zlibrsquo gtgt $HOMEbashrc

Each Matlab release expects the KNITRO library file on Linux to have a specific name There-fore to use KNITRO 8x or later with Matlab release R2008a through 2011b you must createa symbolic link to the expected name Open a Terminal window and in the KNITRO librarydirectory issue the command

bull for Matlab releases R2008a R2008b or R2009aln -s libknitroso800 libknitroso5

bull for Matlab releases R2009b R2010a or R2010bln -s libknitroso800 libknitroso6

bull for Matlab releases R2011a R2011b or R2012aln -s libknitroso800 libknitroso7

Documentation

More informations can be found in the following documentation9

KNITRO 80 User Manual Knitro80 UserManualpdfVersion December 2011 (816 KB)

Specific KNITROMatlab interface documentation can be found on the Matlab OptimizationToolbox webpages10

233 Getting started with KNITRO and AMPL

option solver knitroampl

option knitro options rsquooption1=value1 option2=value2rsquo

solve

9httpwwwartelyscomuploadspdfsKnitro80_UserManualpdf10httpwwwmathworkscomaccesshelpdeskhelptoolboxoptim

26

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 8: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

Operating system(s) AMPL can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) AMPL is written in C++ and it is released as open source code under theEclipse Public License (EPL)

122 Installing AMPL on Linux

Download your AMPL executable by selecting the Intel (Pentium-compatible) PCs runningLinux on AMPL website2 or type in a Terminal window

wget httpnetlibsandiagovamplstudentlinuxamplgz

The downloaded file must be decompressed and it must be made executable after which itmay be executed from any directory in your search path

gzip -d amplgz

chmod +x ampl

sudo mkdir -p optampl

sudo mv ampl optampl

echo rsquoEXPORT PATH=$PATHoptamplrsquo gtgt $HOMEbashrc

To complete your AMPL installation you can also download solvers to a directory in yoursearch path To use a downloaded solver set AMPLrsquos solver option to the name of the solverrsquosexecutable file

The student edition of AMPL is limited to 300 variables and 300 constraints and objectives(after presolve) for nonlinear problems

123 Getting started with AMPL

The first step in solving a problem is to load the model variables objective function andconstraints It is usual the create a mod file containing these informations First open a AMPLcommand window by typing ampl in a Terminal window

AMPL commands end with semicolon () Some AMPL basic commands

bull to declare a parameter param ltpar namegt

bull to load a model model ltfile namegtmod

bull to run a AMPL script include ltscript namegtrun

bull to load a data file data ltfile namegtdat

bull to view the content of a variable display ltvar namegt

bull to exit a AMPL command window exit

13 ACADO ndash Automatic Control And Dynamic Optimization

ldquoACADO Toolkit is a software environment and algorithm collection for automatic controland dynamic optimization It provides a general framework for using a great variety of al-gorithms for direct optimal control including model predictive control state and parameterestimation and robust optimizationrdquo The ACADO library can be found in the official site3

2httpnetlibsandiagovamplstudentlinuxamplgz3httpsourceforgenetprojectsacado

8

131 Basic information

Developed under the direction of Moritz Diehl

Operating system(s) ACADO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) ACADO Toolkit is implemented as self-contained C++ code it comes alongwith user-friendly MATLAB interface and is released under the LGP License

132 Installing ACADO on Linux

In order to install ACADO Toolkit under LINUXUNIX operating systems make sure thata C++ compiler is correctly installed For the case that a graphical output is desired Gnuplotshould be installed in addition However ACADO Toolkit will also run without Gnuplot - inthis case the graphical output is simply turned off There are no further dependencies

First download ACADO Toolkit and its licence from the offical website4 and extract the filestar xfvz ACADOtoolkit-102613betatargz

sudo mv ACADOtoolkit-102613beta optacado

Then go to the directory optacado and compile the packagecd optacado

sudo make

sudo make doc (optional)

Finally check whether the installation was successfulcd examplesgetting started

simple ocp

The following documentation5 is available

bull ACADO Toolkit Userrsquos Manual

bull ACADO for Matlab Userrsquos Manual

bull ACADO Toolkit Introductory Talk

14 BOCOP ndash The optimal control solver

ldquoThe BOCOP project aims to develop an open-source toolbox for solving optimal controlproblemsrdquo The BOCOP library can be found in the official site6

141 Basic information

Developed by V Grelard P Martinon and F Bonnans at Inria-Saclay

Operating system(s) BOCOP is available for linux precompiled packages (Mac and Windowsversions are still under testing)

Code language(s) BOCOP requires SciLab and it is released under the Eclipse Public License(EPL)

4httpwwwacadotoolkitorgdownloadphp5httpsourceforgenetpacadowikiDocumentation6httpwwwbocoporg

9

142 Installing BOCOP on Linux

In order to install BOCOP software under LINUXUNIX operating system download BO-COP software from the official website7 and extract the files

tar xvf bocop-103-beta-linux32targz

mv bocop-103-beta-linux32 optbocop

cd optbocop

make

sudo chmod +x bocopsh

Add the following to your bashrc or bash profile

BOCOPDIR=optbocop

export PATH=$PATH$BOCOPDIR

143 Getting started with BOCOP

Read the userrsquos guide8 and run the examples shown in the optbocopproblems folder

15 DIDO ndash Automatic Control And Dynamic Optimization

ldquoDIDO the leading optimal control software powers users by offering the easiest and directsolutions to the most complex problemsrdquo The DIDO software can be found in the official site9

151 Basic information

Developed at Elissar Global

Operating system(s) DIDO can be used on Microsoft Windows

Code language(s) DIDO requires MATLAB and it is released under academic and commerciallicenses

152 Installing DIDO

DIDOrsquos foundation is pseudospectral theory and is the only pseudospectral solution withmathematically proven convergence properties DIDO is a MATLAB program for solving hybridoptimal control problems The generalndashpurpose program is named after Dido the legendaryfounder and first queen of Carthage who is famous in mathematics for her remarkable solutionto a constrained optimal control problem even before the invention of calculus

To install DIDO on your computer you should consider the system requirements

bull WINDOWS

bull MATLAB

and the following steps

7httpwwwbocoporg8httpbocopsaclayinriafrdownload=109httpwwwelissarglobalcomindustryproductssoftware-3

10

STEP 1 Obtain a license for DIDO from Elissar Global at the official site10

STEP 2 After you receive an email with the link to a compressed version of DIDO downloadthe zip file and uncompress it It will automatically uncompress with the right folderstructure

STEP 3 Right click on the DIDO icon and click on PROPERTIES In the Target box type locationof your licensed MATLAB file In the Start in box type the full path name of your DIDOfolder

STEP 4 Doublendashclick on the DIDO ICON (this should start MATLAB and automatically initial-ize DIDO)

(a) In the MATLAB command window type TestDIDOA successful installation should produce the final output on the screenldquoCONGRATULATIONS DIDO TEST WAS SUCCESSFULrdquo

(b) A further test may be performed by typing LanderProblemA successful installation should produce a pretty graph

153 Getting started with DIDO

In the MATLAB command window run[cost primal] = dido(Problem)

where Problem is a structure array with fields cost dynamics events and path that point to theinput files

Problemcost = lsquoMyCostFilersquo

Problemdynamics = lsquoMyDynamicsFilersquo

Problemevents = lsquoMyEventsFilersquo

Problempath = lsquoMyPathFilersquo

The variable primal is used in nearly all DIDO files to pass the states controls parametersand other useful quantities To print out the statendash and controlndashtrajectory in the MATLABcommand window runprimalstates primalcontrols

This generality allows for

bull Fairly complex interior point constraints

bull Pre-defined segments

bull Differentially-flat segments

bull Transition conditions

bull Midndashmaneuver changes in dynamics

bull Multi-dynamical systems

bull Midndashmaneuver changes in the cost function

bull Switches

bull Discrete events10httpwwwelissarglobalcomacademicget-didotry-dido

11

16 ICLOCS ndash Imperial College London Optimal Control Software

ldquoThe code allows users to define and solve optimal control problems with general path andboundary constraints and free or fixed final time It is also possible to include constant designparameters as unknownsrdquo The ICLOCS software can be found in the official site11

161 Basic information

Developed by Paola Falugi Eric Kerrigan and Eugene van Wyk

Operating system(s) ICLOCS can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) ICLOCS is implemented in MATLAB and it is released as open source codeunder the BSD License

162 Installing ICLOCS on Linux

In order to install ICLOCS software under LINUXUNIX operating system download ICLOCSsoftware from the official site12 and extract the files

wget httpwwweeicacukICLOCSICLOCS 02 NItarzip

unzip ICLOCS 02 NItarzip

tar xvf ICLOCS 02 NItar

sudo mv ICLOCS 02 NI opticlocs

In order to use it in MATLAB you need to tell MATLAB where to find it To do this justtype addpath(genpath(rsquoopticlocsrsquo)) in the MATLAB command window

163 Getting started with ICLOCS

Read the userrsquos guide13 and run the examples shown in the opticlocsexamples folder

17 TACO ndash Toolkit for AMPL Control Optimization

ldquoTACO is the Toolkit for AMPL Control Optimization It defines some add-ons to the AMPLmodeling language that allow the elegant formulation of ODEDAE optimal control problemsin AMPLrdquo The TACO toolkit can be found in the official site14

171 Basic information

Developed by Christian Kirches and Sven Leyffer

Operating system(s) TACO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) TACO is written in C

11httpwwweeicacukICLOCS12httpwwweeicacukICLOCS13httpwwweeicacukICLOCSuser_guidepdf14httpwwwiwruni-heidelbergde~ChristianKirchessoftwarehtml

12

172 Installing TACO on Linux

In order to install TADO Toolkit under LINUXUNIX operating systems make sure thatCMAKE and a C++ compiler is correctly installed To install CMAKE type in a Terminal win-dow

wget httpslaunchpadnetubuntu+archiveprimary+filescmake 288origtargz

gunzip cmake 288origtargz

tar xvf cmake 288origtar

cd cmake-288

sudo bootstrap

sudo make

sudo make install

First download TACO Toolkit from the offical site15 and extract the files

wget httpswwwiwruni-heidelbergdegroupsagbockFILEStaco sourcetargzgunzip taco sourcetargz

tar xfvz taco sourcetar

sudo ln -s optCoinIpoptThirdPartyASLlibamplsolvera libamplsolvera

18 Pseudospectral Methods in Optimal Control

181 PSOPT

PSOPT is an open source optimal control software package written in C++ that uses directcollocation methods including pseudospectral and local discretizations available in the officesite16 Pseudospectral methods solve optimal control problems by approximating the time-dependent variables using global polynomials such as Legendre or Chebyshev functions Localdiscretisation methods approximate the time dependent functions using local splines and canbe seen as implementations of implicit Runge-Kutta integrators With both global and localmethods differential equations continuous constraints and integrals associated with the prob-lem are discretised over a grid of nodes Sparse nonlinear programming is then used to findlocal optimal solutions

PSOPT is able to deal with problems with the following characteristics

bull Single or multiphase problems

bull Continuous time nonlinear dynamics

bull Nonlinear path constraints

bull General event constraints

bull Integral constraints

bull Interior point constraints

bull Bounds on controls and state variables

bull General cost function with Lagrange and Mayer terms

15httpwwwiwruni-heidelbergde~ChristianKirchessoftwarehtml16httpwwwpsoptorg

13

bull Linear or nonlinear linkages between phases

bull Fixed or free initial phase time

bull Fixed or free final phase time

bull Optimization of static parameters

bull Optimal parameter estimation given sampled observations

The implementation has the following features

bull Choice between Legendre Chebyshev central differences trapezoidal or Hermite-Simpsondiscretisation

bull Large scale nonlinear programming using IPOPT and (optionally) SNOPT

bull Estimation of the discretisation error

bull Automatic mesh refinement

bull Automatic scaling

bull Automatic differentiation using the ADOL-C library

bull Numerical differentiation by using sparse finite differences

bull Automatic identification of the sparsity of the derivative matrices

bull DAE formulation so that differential and algebraic constraints can be implemented in thesame C++ function

bull Easy to use interface to GNUplot to produce graphical output including 2D plots 3Dcurves and surfaces and polar plots

bull Automatic generation of LaTeX code to produce a table that summarizes the mesh refine-ment process

Full details on PSOPT and its features can be found in its documentation17

182 GPOPS-II - MATLAB Optimal Control Software

GPOPS-II is a general purpose optimal control software available in the office site18 GPOPS-II is a new open-source MATLAB optimal control software that implements a brand new hp-adaptive Legendre-Gauss-Radau quadrature integral pseudospectral method for solving generalnonlinear optimal control problems Using GPOPS-II the optimal control problem is transcribedto a nonlinear programming problem (NLP) The NLP is then solved using either the solverSNOPT or the solver IPOPT

The following are some the key features of GPOPS-II

bull Allows for an extremely general formulation of the optimal control problem

bull Allows for inclusion of integral constraints and highly general boundary conditions

bull Complete first and second sparse finite-differencing of optimal control problem to com-pute all derivatives required by the NLP solver

17httpcodegooglecomppsoptdownloadsdetailname=PSOPT_Manual_R3pdfampcan=2ampq=18httpwwwgpopsorg

14

bull The latest advances in mesh refinement including hpndashadaptive pseudospectral methods

bull Gaussian quadrature integration methods for rapid convergence

bull Highly accurate costate estimation

bull Inclusion of the NLP solver SNOPT (for Academic Users) and IPOPT (for Not-for-Profitand Commercial Users)

bull No third-party products other than MATLAB are required

More information about the methodology used in GPOPS-II can be found by reading therelevant articles in the open literature19

The fees for using GPOPS-II are as follows

bull For students and all others employed at academic institutions NO CHARGE

bull US Federal State or Local Government NO CHARGE

bull Users at not-for-profit institutions (including non-US Government agencies) or commer-cial institutions LICENSING FEES APPLY

19httpvdolmaeufledu

15

16

Chapter 2

Nonlinear Programming Solvers

21 Introduction

A solver is a software used to compute numerical solution of an optimal control problemIn general an optimal controlnonlinear programming interface is needed to prepare all dataas input to the solver (see Figure 21) Several solvers will be presented in this chapter evolvingopenndashsource freeware and commercial software working under different operating systemsSince all solvers require configuration lists of parameters will be presented for some of them

Figure 21

22 IPOPT ndash Interior Point OPTimizer

ldquoIPOPT is a software package for largendashscale nonlinear optimisation It is designed to find(local) solutions of mathematical optimisation problemsrdquo The IPOPT library can be found inhttpwwwartelyscom1

221 Basic information

Developed by Andreas Wachter and Carl Laird

Operating system(s) IPOPT can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) IPOPT is written in C++ and is released as open source code under theEclipse Public License (EPL)

1httpwwwartelyscomindexphppage=knitroamphl=en_EN

17

Modeling language interface(s) IPOPT can be used as a library that can be linked to C++ C orFortran code as well as a solver executable for the AMPL modelling environment Thepackage includes interfaces to CUTEr optimisation testing environment as well as theMATLAB and R programming environments

222 Installing IPOPT

How to install IPOPT on Linux

Getting the IPOPT code via subversion

sudo svn co httpsprojectscoin-ororgsvnIpoptstable310 optCoinIpopt

This will create a directory named CoinIpopt in your opt folder

BLAS routines that provide standard building blocks for performing basic vector and matrixoperations

LAPACK routines for solving systems of simultaneous linear equations least-squares solutionsof linear systems of equations eigenvalue problems and singular value problems

HSL state-of-the-art packages for large-scale scientific computation

ASL AMPL Solver Library allows to read the nl files and provides the automatic differentia-tion functionality

MUMPS MUltifrontal Massively Parallel sparse direct Solver

METIS programs for partitioning graphs partitioning finite element meshes and producing fillreducing orderings for sparse matrices

To install IPOPT just run

cd optCoinIpoptThirdPartyBlas

getBlas

cd Lapack

getLapack

cd ASL

getASL

cd Mumps

getMumps

cd Metis

getMetis

IPOPT requires a sparse symmetric linear solver There are different possibilities The userrsquosguide of IPOPT suggests that you use HSL subroutines

1 Go to HSL website2 and find the package list

2 Click on MA27 read the license and submit the registration form

3 Go back to the package list

4 Click on MC19 read the license and submit the registration form

2httphslrlacukarchivehslarchivehtml

18

You will receive in your email the links to obtain the HSL packages Download them to your$HOME directory and run

gunzip ma27-100targz

tar xvf ma27-100tar

gunzip mc19-100targz

tar xvf mc19-100tar

cd optCoinIpoptThirdPartyHSL

sudo mv -r $HOMEmc19-100

sudo mv -r $HOMEma27-100

cd mc19-100

sudo configure

sudo make install

sudo make check

cd ma27-100

sudo configure

sudo make install

sudo make check

The make check command will check if the installation of the HSL packages was successfulReturn to the optCoinIpopt directory and continue the IPOPT installation

cd

sudo mkdir build

cd build

sudo configure

sudo make test

sudo make install

If you get the error message ldquoerrorlsquoNULLrsquo was not declared in this scoperdquo when run-ning sudo make then you have to edit the following filesoptCoinIpoptIpoptsrcCommonIpSmartPtrhpp

optCoinIpoptIpoptsrcAlgorithmLinearSolversIpTripletToCSRConvertercpp

by adding include ltcstddefgt

How to install IPOPT on Mac OS X

In a Terminal window install homebrewusrbinruby -e $(usrbincurl -fsSL httpsrawgithubcommxclhomebrewmaster

LibraryContributionsinstall homebrewrb)

With this software you can easily install dependency packages like wget and gfortran

brew install wget

brew install gfortran

sudo wget httpwwwcoin-ororgdownloadsourceIpoptIpopt-3102zip

sudo unzip Ipopt-3102zip

cd Ipopt-3102

Download Netlib BlasLapack

cd optCoinIpoptThirdPartyBlas

sudo getBlas

19

cd Lapack

sudo getLapack

cd ASL

sudo getASL

cd Mumps

sudo getMumps

cd Metis

sudo getMetis

Make a build for a 64bit target

sudo mkdir build64

cd build64

sudo configure --disable-shared --with-blas=BUILD --with-lapack=BUILD F77=gfortran

FFLAGS=-fexceptions -m64 -fbackslash CFLAGS=-fno-common -no-cpp-precomp -fexceptions

-arch x86 64 -m64 CXXFLAGS=-fno-common -no-cpp-precomp -fexceptions -arch x86 64

-m64

Compile test and install

make

make test

make install

Add the following to your bashrc or bash profile

IPOPTDIR=optCoinIpoptbuild

export PATH=$PATH$IPOPTDIR$IPOPTDIRbinexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyASLexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyBlaslibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyHSLlibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyLapacklibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMetislibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMumpslibs

How to install the Matlab interface on Linux

First of all be sure

cd optmatlabR2008asysosglnx86

mv libgcc sso1 libgcc sso1bck

ln -s libi386-linux-gnulibgcc sso1 libgcc sso1

mv libstdc++so608 libstdc++so608bck

ln -s usrlibi386-linux-gnulibstdc++so6 libstdc++so608

Go to the subdirectory optCoinIpoptbuildIpoptcontribMatlabInterfacesrc andopen the Makefile file

sudo apt-get install hardening-wrapper

You need to edit this file to suit the installation for your system setup You may need tomodify MATLAB HOME MEXSUFFIX and MEX as explained in the comments of the Makefile I setthese variables as follows

20

MATLAB HOME = optmatlabR2008a

MEXSUFFIX = mexglx

MEX = optmatlabR2008abinmex

In my case I removed some of the compiler options as it follows

prefix = optCoinIpopt

CXX = optintelcomposer xe 2011 sp19293binia32icpc

CCXXFLAGS = -O3 -pipe -DNDEBUG -Wparentheses -Wreturn-type -Wcast-qual -Wall

-Wpointer-arith -Wwrite-strings -Wconversion -Wno-unknown-pragmas -DMATLAB MEXFILE

LDFLAGS = $(CXXFLAGS) -Wl --rpath -Wl $prefixlibcoin -Wl --rpath -Wl

--rpath -Wl$prefixlibcoinThirdParty

In my case I had to set explicitly the include Matlab folder

o cpp

$(CXX) $(CXXFLAGS) $(INCL) -IoptmatlabR2008aexterninclude -o $ -c $^

Once you set up the Makefile properly type sudo make install in the same directory as theMakefile If the installation procedure was successful you will end up with a MEX file calledipoptmexglx In order to use it in MATLAB you need to tell MATLAB where to find it Todo this just type addpath optCoinIpoptlib in the MATLAB command window after youcheck if the ipoptmexglx file is in this folder

Documentation

More informations can be found in the following documentation

Online documentation httpwwwcoin-ororgIpoptdocumentation3

A PDF version of this documentation can be downloaded here4

A short IPOPT tutorial can be downloaded here5

MATLAB interface Instructions specific to the MATLAB interface can be found at the Matlabinterface page6

223 Getting started with IPOPT and AMPL

option solver ipopt

option ipopt options rsquooption1=value1 option2=value2rsquo

solve

3httpwwwcoin-ororgIpoptdocumentation4httpsprojectscoin-ororgIpoptbrowserstable39Ipoptdocdocumentationpdfformat=raw5httpdropsdagstuhldevolltexte20092089pdf09061WaechterAndreasPaper2089pdf6httpsprojectscoin-ororgIpoptwikiMatlabInterface

21

Table 21 IPOPT directives for AMPL

Option Description

acceptable compl inf tol Acceptance threshold for the complementarity conditionsacceptable constr viol tol Acceptance threshold for the constraint violationacceptable dual inf tol Acceptance threshold for the dual infeasibilityacceptable tol Acceptable convergence tolerance (relative)alpha for y Step size for constraint multipliersbound frac Desired minimal relative distance of initial point to boundbound mult init val Initial value for the bound multipliersbound push Desired minimal absolute distance of initial point to boundbound relax factor Factor for initial relaxation of the boundscompl inf tol Acceptance threshold for the complementarity conditionsconstr mult init max Maximal allowed least-square guess of constraint multipliersconstr viol tol Desired threshold for the constraint violationdiverging iterates tol Threshold for maximal value of primal iteratesdual inf tol Desired threshold for the dual infeasibilityexpect infeasible problem Enable heuristics to quickly detect an infeasible problemfile print level Verbosity level for output filehalt on ampl error Exit with message on evaluation errorhessian approximation Can enable Quasi-Newton approximation of hessianhonor original bounds If no solution might slightly violate boundslinear scaling on demand Enables heuristic for scaling only when seems requiredlinear solver Linear solver to be used for step calculationlinear system scaling Method for scaling the linear systemsma27 pivtol Pivot tolerance for the linear solver MA27ma27 pivtolmax Maximal pivot tolerance for the linear solver MA27ma57 pivtol Pivot tolerance for the linear solver MA57ma57 pivtolmax Maximal pivot tolerance for the linear solver MA57

22

Table 22 IPOPT directives for AMPL

Option Description

max cpu time CPU time limitmax iter Maximum number of iterations

max refinement stepsMaximal number of iterative refinement steps per linearsystem solve

max soc Maximal number of second order correction trial stepsmaxit Maximum number of iterations (same as max iter)

min refinement stepsMinimum number of iterative refinement steps per linearsystem solve

mu init Initial value for the barrier parametermu max Maximal value for barrier parameter for adaptive strategymu oracle Oracle for a new barrier parameter in the adaptive strategymu strategy Update strategy for barrier parameternlp scaling max gradient Maximum gradient after scalingnlp scaling method Select the technique used for scaling the NLPobj scaling factor Scaling factor for the objective functionoption file name File name of options file (default ipoptopt)outlev Verbosity level (same as print level)output file File name of an output file (leave unset for no file output)pardiso matching strategy Matching strategy for linear solver Pardisopardiso out of core power Enables out-of-core version of linear solver Pardisoprint level Verbosity levelprint options documentation Print all available options (for ipoptopt)print user options Toggle printing of user optionsrequired infeasibility reduction Required infeasibility reduction in restoration phaseslack bound frac Desired minimal relative distance of initial slack to boundslack bound push Desired minimal absolute distance of initial slack to boundtol Desired convergence tolerance (relative)

wantsol

solution report without -AMPL sum of1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

warm start bound pushEnables to specify how much should variables should bepushed inside the feasible region

warm start init point Enables to specify bound multiplier values

warm start mult bound pushEnables to specify how much should bound multipliersshould be pushed inside the feasible region

watchdog shortened iter trigger Trigger counter for watchdog procedurewsmp num threads Number of threads to be used in WSMPwsmp pivtol Pivot tolerance for the linear solver WSMPwsmp pivtolmax Maximum pivot tolerance for the linear solver WSMPwsmp scaling Determines how the matrix is scaled by WSMP

23

24

23 KNITRO

ldquoKNITRO is an optimization software library for finding solutions of both continuous (smo-oth) optimization models (with or without constraints) as well as discrete optimization modelswith integer or binary variables (ie mixed integer programs) KNITRO is primarily designedfor finding local optimal solutions of large-scale continuous nonlinear problemsrdquo The KNITROlibrary can be found in httpwwwartelyscom7

231 Basic information

KNITRO is a software package for solving smooth optimization problems with or withoutconstraints

Developed at Ziena Optimization

Operating system(s) KNITRO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) KNITRO is written C C++ Fortran and Java

Programming interfaces(s) Fortan CC++ Java and Microsoft Excel

Modeling language interface(s) KNITRO offers a total of five interfaces Matlab AMPL Math-ematica AIMMS GAMS and MPL

232 Installing KNITRO on Linux

How to get a license

KNITRO is developed by Ziena Optimization LLC and marketed and supported by ArtelysThe first step is to request a license by selecting the download tab8 Along with the academicand commercial trial versions there is a student version limited in problem size (300 variablesand 300 constraints) available for 6 months Each license is personalised hence among otherpersonal information you should inform about

bull the operating system (OS)

bull the OS bit architecture

bull the Machine ID of the computer on which KNITRO will runDownload extract an script available bellow the Machine ID field

After reading and accepting the End User License Agreement the download of the KNITROwill be available and you will get a license key on your email Then you should put the licensefile whose name begins with ziena lic in your $HOME directory or in the $HOMEzienadirectory

How to install

The KNITRO software package for Unix is delivered as a gzipped tar file After downloadingKNITRO you need to unpack it Type in a Terminal window

gunzip knitro-8x-platformnametargz

tar -xvf knitro-8x-platformnametar

sudo mv knitro-8x-z opt

7httpwwwartelyscomindexphppage=knitroamphl=en_EN8httpwwwartelyscomindexphppage=knitroamphl=en_ENdownloads-tab

25

This will create a directory named knitro-8x-z in your opt folder

In order to run KNITRO binary or executable files from anywhere on your Linux computerit is necessary to set several environment variables In particular you must update the PATH

environment variable so that it indicates the location of the knitroampl directory You mustalso update the LD LIBRARY PATH environment variable so that it indicates the location of theKNITRO lib directory Setting the PATH and LD LIBRARY PATH environment variables on a Linuxsystem can be done as follows

echo rsquoexport PATH=$PATHoptknitro-8x-zrsquo gtgt $HOMEbashrcecho rsquoexport PATH=$PATHoptknitro-8x-zknitroamplrsquo gtgt $HOMEbashrcecho rsquoexport LD LIBRARY PATH=$LD LIBRARY PATHoptknitro-8x-zlibrsquo gtgt $HOMEbashrc

Each Matlab release expects the KNITRO library file on Linux to have a specific name There-fore to use KNITRO 8x or later with Matlab release R2008a through 2011b you must createa symbolic link to the expected name Open a Terminal window and in the KNITRO librarydirectory issue the command

bull for Matlab releases R2008a R2008b or R2009aln -s libknitroso800 libknitroso5

bull for Matlab releases R2009b R2010a or R2010bln -s libknitroso800 libknitroso6

bull for Matlab releases R2011a R2011b or R2012aln -s libknitroso800 libknitroso7

Documentation

More informations can be found in the following documentation9

KNITRO 80 User Manual Knitro80 UserManualpdfVersion December 2011 (816 KB)

Specific KNITROMatlab interface documentation can be found on the Matlab OptimizationToolbox webpages10

233 Getting started with KNITRO and AMPL

option solver knitroampl

option knitro options rsquooption1=value1 option2=value2rsquo

solve

9httpwwwartelyscomuploadspdfsKnitro80_UserManualpdf10httpwwwmathworkscomaccesshelpdeskhelptoolboxoptim

26

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 9: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

131 Basic information

Developed under the direction of Moritz Diehl

Operating system(s) ACADO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) ACADO Toolkit is implemented as self-contained C++ code it comes alongwith user-friendly MATLAB interface and is released under the LGP License

132 Installing ACADO on Linux

In order to install ACADO Toolkit under LINUXUNIX operating systems make sure thata C++ compiler is correctly installed For the case that a graphical output is desired Gnuplotshould be installed in addition However ACADO Toolkit will also run without Gnuplot - inthis case the graphical output is simply turned off There are no further dependencies

First download ACADO Toolkit and its licence from the offical website4 and extract the filestar xfvz ACADOtoolkit-102613betatargz

sudo mv ACADOtoolkit-102613beta optacado

Then go to the directory optacado and compile the packagecd optacado

sudo make

sudo make doc (optional)

Finally check whether the installation was successfulcd examplesgetting started

simple ocp

The following documentation5 is available

bull ACADO Toolkit Userrsquos Manual

bull ACADO for Matlab Userrsquos Manual

bull ACADO Toolkit Introductory Talk

14 BOCOP ndash The optimal control solver

ldquoThe BOCOP project aims to develop an open-source toolbox for solving optimal controlproblemsrdquo The BOCOP library can be found in the official site6

141 Basic information

Developed by V Grelard P Martinon and F Bonnans at Inria-Saclay

Operating system(s) BOCOP is available for linux precompiled packages (Mac and Windowsversions are still under testing)

Code language(s) BOCOP requires SciLab and it is released under the Eclipse Public License(EPL)

4httpwwwacadotoolkitorgdownloadphp5httpsourceforgenetpacadowikiDocumentation6httpwwwbocoporg

9

142 Installing BOCOP on Linux

In order to install BOCOP software under LINUXUNIX operating system download BO-COP software from the official website7 and extract the files

tar xvf bocop-103-beta-linux32targz

mv bocop-103-beta-linux32 optbocop

cd optbocop

make

sudo chmod +x bocopsh

Add the following to your bashrc or bash profile

BOCOPDIR=optbocop

export PATH=$PATH$BOCOPDIR

143 Getting started with BOCOP

Read the userrsquos guide8 and run the examples shown in the optbocopproblems folder

15 DIDO ndash Automatic Control And Dynamic Optimization

ldquoDIDO the leading optimal control software powers users by offering the easiest and directsolutions to the most complex problemsrdquo The DIDO software can be found in the official site9

151 Basic information

Developed at Elissar Global

Operating system(s) DIDO can be used on Microsoft Windows

Code language(s) DIDO requires MATLAB and it is released under academic and commerciallicenses

152 Installing DIDO

DIDOrsquos foundation is pseudospectral theory and is the only pseudospectral solution withmathematically proven convergence properties DIDO is a MATLAB program for solving hybridoptimal control problems The generalndashpurpose program is named after Dido the legendaryfounder and first queen of Carthage who is famous in mathematics for her remarkable solutionto a constrained optimal control problem even before the invention of calculus

To install DIDO on your computer you should consider the system requirements

bull WINDOWS

bull MATLAB

and the following steps

7httpwwwbocoporg8httpbocopsaclayinriafrdownload=109httpwwwelissarglobalcomindustryproductssoftware-3

10

STEP 1 Obtain a license for DIDO from Elissar Global at the official site10

STEP 2 After you receive an email with the link to a compressed version of DIDO downloadthe zip file and uncompress it It will automatically uncompress with the right folderstructure

STEP 3 Right click on the DIDO icon and click on PROPERTIES In the Target box type locationof your licensed MATLAB file In the Start in box type the full path name of your DIDOfolder

STEP 4 Doublendashclick on the DIDO ICON (this should start MATLAB and automatically initial-ize DIDO)

(a) In the MATLAB command window type TestDIDOA successful installation should produce the final output on the screenldquoCONGRATULATIONS DIDO TEST WAS SUCCESSFULrdquo

(b) A further test may be performed by typing LanderProblemA successful installation should produce a pretty graph

153 Getting started with DIDO

In the MATLAB command window run[cost primal] = dido(Problem)

where Problem is a structure array with fields cost dynamics events and path that point to theinput files

Problemcost = lsquoMyCostFilersquo

Problemdynamics = lsquoMyDynamicsFilersquo

Problemevents = lsquoMyEventsFilersquo

Problempath = lsquoMyPathFilersquo

The variable primal is used in nearly all DIDO files to pass the states controls parametersand other useful quantities To print out the statendash and controlndashtrajectory in the MATLABcommand window runprimalstates primalcontrols

This generality allows for

bull Fairly complex interior point constraints

bull Pre-defined segments

bull Differentially-flat segments

bull Transition conditions

bull Midndashmaneuver changes in dynamics

bull Multi-dynamical systems

bull Midndashmaneuver changes in the cost function

bull Switches

bull Discrete events10httpwwwelissarglobalcomacademicget-didotry-dido

11

16 ICLOCS ndash Imperial College London Optimal Control Software

ldquoThe code allows users to define and solve optimal control problems with general path andboundary constraints and free or fixed final time It is also possible to include constant designparameters as unknownsrdquo The ICLOCS software can be found in the official site11

161 Basic information

Developed by Paola Falugi Eric Kerrigan and Eugene van Wyk

Operating system(s) ICLOCS can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) ICLOCS is implemented in MATLAB and it is released as open source codeunder the BSD License

162 Installing ICLOCS on Linux

In order to install ICLOCS software under LINUXUNIX operating system download ICLOCSsoftware from the official site12 and extract the files

wget httpwwweeicacukICLOCSICLOCS 02 NItarzip

unzip ICLOCS 02 NItarzip

tar xvf ICLOCS 02 NItar

sudo mv ICLOCS 02 NI opticlocs

In order to use it in MATLAB you need to tell MATLAB where to find it To do this justtype addpath(genpath(rsquoopticlocsrsquo)) in the MATLAB command window

163 Getting started with ICLOCS

Read the userrsquos guide13 and run the examples shown in the opticlocsexamples folder

17 TACO ndash Toolkit for AMPL Control Optimization

ldquoTACO is the Toolkit for AMPL Control Optimization It defines some add-ons to the AMPLmodeling language that allow the elegant formulation of ODEDAE optimal control problemsin AMPLrdquo The TACO toolkit can be found in the official site14

171 Basic information

Developed by Christian Kirches and Sven Leyffer

Operating system(s) TACO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) TACO is written in C

11httpwwweeicacukICLOCS12httpwwweeicacukICLOCS13httpwwweeicacukICLOCSuser_guidepdf14httpwwwiwruni-heidelbergde~ChristianKirchessoftwarehtml

12

172 Installing TACO on Linux

In order to install TADO Toolkit under LINUXUNIX operating systems make sure thatCMAKE and a C++ compiler is correctly installed To install CMAKE type in a Terminal win-dow

wget httpslaunchpadnetubuntu+archiveprimary+filescmake 288origtargz

gunzip cmake 288origtargz

tar xvf cmake 288origtar

cd cmake-288

sudo bootstrap

sudo make

sudo make install

First download TACO Toolkit from the offical site15 and extract the files

wget httpswwwiwruni-heidelbergdegroupsagbockFILEStaco sourcetargzgunzip taco sourcetargz

tar xfvz taco sourcetar

sudo ln -s optCoinIpoptThirdPartyASLlibamplsolvera libamplsolvera

18 Pseudospectral Methods in Optimal Control

181 PSOPT

PSOPT is an open source optimal control software package written in C++ that uses directcollocation methods including pseudospectral and local discretizations available in the officesite16 Pseudospectral methods solve optimal control problems by approximating the time-dependent variables using global polynomials such as Legendre or Chebyshev functions Localdiscretisation methods approximate the time dependent functions using local splines and canbe seen as implementations of implicit Runge-Kutta integrators With both global and localmethods differential equations continuous constraints and integrals associated with the prob-lem are discretised over a grid of nodes Sparse nonlinear programming is then used to findlocal optimal solutions

PSOPT is able to deal with problems with the following characteristics

bull Single or multiphase problems

bull Continuous time nonlinear dynamics

bull Nonlinear path constraints

bull General event constraints

bull Integral constraints

bull Interior point constraints

bull Bounds on controls and state variables

bull General cost function with Lagrange and Mayer terms

15httpwwwiwruni-heidelbergde~ChristianKirchessoftwarehtml16httpwwwpsoptorg

13

bull Linear or nonlinear linkages between phases

bull Fixed or free initial phase time

bull Fixed or free final phase time

bull Optimization of static parameters

bull Optimal parameter estimation given sampled observations

The implementation has the following features

bull Choice between Legendre Chebyshev central differences trapezoidal or Hermite-Simpsondiscretisation

bull Large scale nonlinear programming using IPOPT and (optionally) SNOPT

bull Estimation of the discretisation error

bull Automatic mesh refinement

bull Automatic scaling

bull Automatic differentiation using the ADOL-C library

bull Numerical differentiation by using sparse finite differences

bull Automatic identification of the sparsity of the derivative matrices

bull DAE formulation so that differential and algebraic constraints can be implemented in thesame C++ function

bull Easy to use interface to GNUplot to produce graphical output including 2D plots 3Dcurves and surfaces and polar plots

bull Automatic generation of LaTeX code to produce a table that summarizes the mesh refine-ment process

Full details on PSOPT and its features can be found in its documentation17

182 GPOPS-II - MATLAB Optimal Control Software

GPOPS-II is a general purpose optimal control software available in the office site18 GPOPS-II is a new open-source MATLAB optimal control software that implements a brand new hp-adaptive Legendre-Gauss-Radau quadrature integral pseudospectral method for solving generalnonlinear optimal control problems Using GPOPS-II the optimal control problem is transcribedto a nonlinear programming problem (NLP) The NLP is then solved using either the solverSNOPT or the solver IPOPT

The following are some the key features of GPOPS-II

bull Allows for an extremely general formulation of the optimal control problem

bull Allows for inclusion of integral constraints and highly general boundary conditions

bull Complete first and second sparse finite-differencing of optimal control problem to com-pute all derivatives required by the NLP solver

17httpcodegooglecomppsoptdownloadsdetailname=PSOPT_Manual_R3pdfampcan=2ampq=18httpwwwgpopsorg

14

bull The latest advances in mesh refinement including hpndashadaptive pseudospectral methods

bull Gaussian quadrature integration methods for rapid convergence

bull Highly accurate costate estimation

bull Inclusion of the NLP solver SNOPT (for Academic Users) and IPOPT (for Not-for-Profitand Commercial Users)

bull No third-party products other than MATLAB are required

More information about the methodology used in GPOPS-II can be found by reading therelevant articles in the open literature19

The fees for using GPOPS-II are as follows

bull For students and all others employed at academic institutions NO CHARGE

bull US Federal State or Local Government NO CHARGE

bull Users at not-for-profit institutions (including non-US Government agencies) or commer-cial institutions LICENSING FEES APPLY

19httpvdolmaeufledu

15

16

Chapter 2

Nonlinear Programming Solvers

21 Introduction

A solver is a software used to compute numerical solution of an optimal control problemIn general an optimal controlnonlinear programming interface is needed to prepare all dataas input to the solver (see Figure 21) Several solvers will be presented in this chapter evolvingopenndashsource freeware and commercial software working under different operating systemsSince all solvers require configuration lists of parameters will be presented for some of them

Figure 21

22 IPOPT ndash Interior Point OPTimizer

ldquoIPOPT is a software package for largendashscale nonlinear optimisation It is designed to find(local) solutions of mathematical optimisation problemsrdquo The IPOPT library can be found inhttpwwwartelyscom1

221 Basic information

Developed by Andreas Wachter and Carl Laird

Operating system(s) IPOPT can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) IPOPT is written in C++ and is released as open source code under theEclipse Public License (EPL)

1httpwwwartelyscomindexphppage=knitroamphl=en_EN

17

Modeling language interface(s) IPOPT can be used as a library that can be linked to C++ C orFortran code as well as a solver executable for the AMPL modelling environment Thepackage includes interfaces to CUTEr optimisation testing environment as well as theMATLAB and R programming environments

222 Installing IPOPT

How to install IPOPT on Linux

Getting the IPOPT code via subversion

sudo svn co httpsprojectscoin-ororgsvnIpoptstable310 optCoinIpopt

This will create a directory named CoinIpopt in your opt folder

BLAS routines that provide standard building blocks for performing basic vector and matrixoperations

LAPACK routines for solving systems of simultaneous linear equations least-squares solutionsof linear systems of equations eigenvalue problems and singular value problems

HSL state-of-the-art packages for large-scale scientific computation

ASL AMPL Solver Library allows to read the nl files and provides the automatic differentia-tion functionality

MUMPS MUltifrontal Massively Parallel sparse direct Solver

METIS programs for partitioning graphs partitioning finite element meshes and producing fillreducing orderings for sparse matrices

To install IPOPT just run

cd optCoinIpoptThirdPartyBlas

getBlas

cd Lapack

getLapack

cd ASL

getASL

cd Mumps

getMumps

cd Metis

getMetis

IPOPT requires a sparse symmetric linear solver There are different possibilities The userrsquosguide of IPOPT suggests that you use HSL subroutines

1 Go to HSL website2 and find the package list

2 Click on MA27 read the license and submit the registration form

3 Go back to the package list

4 Click on MC19 read the license and submit the registration form

2httphslrlacukarchivehslarchivehtml

18

You will receive in your email the links to obtain the HSL packages Download them to your$HOME directory and run

gunzip ma27-100targz

tar xvf ma27-100tar

gunzip mc19-100targz

tar xvf mc19-100tar

cd optCoinIpoptThirdPartyHSL

sudo mv -r $HOMEmc19-100

sudo mv -r $HOMEma27-100

cd mc19-100

sudo configure

sudo make install

sudo make check

cd ma27-100

sudo configure

sudo make install

sudo make check

The make check command will check if the installation of the HSL packages was successfulReturn to the optCoinIpopt directory and continue the IPOPT installation

cd

sudo mkdir build

cd build

sudo configure

sudo make test

sudo make install

If you get the error message ldquoerrorlsquoNULLrsquo was not declared in this scoperdquo when run-ning sudo make then you have to edit the following filesoptCoinIpoptIpoptsrcCommonIpSmartPtrhpp

optCoinIpoptIpoptsrcAlgorithmLinearSolversIpTripletToCSRConvertercpp

by adding include ltcstddefgt

How to install IPOPT on Mac OS X

In a Terminal window install homebrewusrbinruby -e $(usrbincurl -fsSL httpsrawgithubcommxclhomebrewmaster

LibraryContributionsinstall homebrewrb)

With this software you can easily install dependency packages like wget and gfortran

brew install wget

brew install gfortran

sudo wget httpwwwcoin-ororgdownloadsourceIpoptIpopt-3102zip

sudo unzip Ipopt-3102zip

cd Ipopt-3102

Download Netlib BlasLapack

cd optCoinIpoptThirdPartyBlas

sudo getBlas

19

cd Lapack

sudo getLapack

cd ASL

sudo getASL

cd Mumps

sudo getMumps

cd Metis

sudo getMetis

Make a build for a 64bit target

sudo mkdir build64

cd build64

sudo configure --disable-shared --with-blas=BUILD --with-lapack=BUILD F77=gfortran

FFLAGS=-fexceptions -m64 -fbackslash CFLAGS=-fno-common -no-cpp-precomp -fexceptions

-arch x86 64 -m64 CXXFLAGS=-fno-common -no-cpp-precomp -fexceptions -arch x86 64

-m64

Compile test and install

make

make test

make install

Add the following to your bashrc or bash profile

IPOPTDIR=optCoinIpoptbuild

export PATH=$PATH$IPOPTDIR$IPOPTDIRbinexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyASLexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyBlaslibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyHSLlibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyLapacklibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMetislibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMumpslibs

How to install the Matlab interface on Linux

First of all be sure

cd optmatlabR2008asysosglnx86

mv libgcc sso1 libgcc sso1bck

ln -s libi386-linux-gnulibgcc sso1 libgcc sso1

mv libstdc++so608 libstdc++so608bck

ln -s usrlibi386-linux-gnulibstdc++so6 libstdc++so608

Go to the subdirectory optCoinIpoptbuildIpoptcontribMatlabInterfacesrc andopen the Makefile file

sudo apt-get install hardening-wrapper

You need to edit this file to suit the installation for your system setup You may need tomodify MATLAB HOME MEXSUFFIX and MEX as explained in the comments of the Makefile I setthese variables as follows

20

MATLAB HOME = optmatlabR2008a

MEXSUFFIX = mexglx

MEX = optmatlabR2008abinmex

In my case I removed some of the compiler options as it follows

prefix = optCoinIpopt

CXX = optintelcomposer xe 2011 sp19293binia32icpc

CCXXFLAGS = -O3 -pipe -DNDEBUG -Wparentheses -Wreturn-type -Wcast-qual -Wall

-Wpointer-arith -Wwrite-strings -Wconversion -Wno-unknown-pragmas -DMATLAB MEXFILE

LDFLAGS = $(CXXFLAGS) -Wl --rpath -Wl $prefixlibcoin -Wl --rpath -Wl

--rpath -Wl$prefixlibcoinThirdParty

In my case I had to set explicitly the include Matlab folder

o cpp

$(CXX) $(CXXFLAGS) $(INCL) -IoptmatlabR2008aexterninclude -o $ -c $^

Once you set up the Makefile properly type sudo make install in the same directory as theMakefile If the installation procedure was successful you will end up with a MEX file calledipoptmexglx In order to use it in MATLAB you need to tell MATLAB where to find it Todo this just type addpath optCoinIpoptlib in the MATLAB command window after youcheck if the ipoptmexglx file is in this folder

Documentation

More informations can be found in the following documentation

Online documentation httpwwwcoin-ororgIpoptdocumentation3

A PDF version of this documentation can be downloaded here4

A short IPOPT tutorial can be downloaded here5

MATLAB interface Instructions specific to the MATLAB interface can be found at the Matlabinterface page6

223 Getting started with IPOPT and AMPL

option solver ipopt

option ipopt options rsquooption1=value1 option2=value2rsquo

solve

3httpwwwcoin-ororgIpoptdocumentation4httpsprojectscoin-ororgIpoptbrowserstable39Ipoptdocdocumentationpdfformat=raw5httpdropsdagstuhldevolltexte20092089pdf09061WaechterAndreasPaper2089pdf6httpsprojectscoin-ororgIpoptwikiMatlabInterface

21

Table 21 IPOPT directives for AMPL

Option Description

acceptable compl inf tol Acceptance threshold for the complementarity conditionsacceptable constr viol tol Acceptance threshold for the constraint violationacceptable dual inf tol Acceptance threshold for the dual infeasibilityacceptable tol Acceptable convergence tolerance (relative)alpha for y Step size for constraint multipliersbound frac Desired minimal relative distance of initial point to boundbound mult init val Initial value for the bound multipliersbound push Desired minimal absolute distance of initial point to boundbound relax factor Factor for initial relaxation of the boundscompl inf tol Acceptance threshold for the complementarity conditionsconstr mult init max Maximal allowed least-square guess of constraint multipliersconstr viol tol Desired threshold for the constraint violationdiverging iterates tol Threshold for maximal value of primal iteratesdual inf tol Desired threshold for the dual infeasibilityexpect infeasible problem Enable heuristics to quickly detect an infeasible problemfile print level Verbosity level for output filehalt on ampl error Exit with message on evaluation errorhessian approximation Can enable Quasi-Newton approximation of hessianhonor original bounds If no solution might slightly violate boundslinear scaling on demand Enables heuristic for scaling only when seems requiredlinear solver Linear solver to be used for step calculationlinear system scaling Method for scaling the linear systemsma27 pivtol Pivot tolerance for the linear solver MA27ma27 pivtolmax Maximal pivot tolerance for the linear solver MA27ma57 pivtol Pivot tolerance for the linear solver MA57ma57 pivtolmax Maximal pivot tolerance for the linear solver MA57

22

Table 22 IPOPT directives for AMPL

Option Description

max cpu time CPU time limitmax iter Maximum number of iterations

max refinement stepsMaximal number of iterative refinement steps per linearsystem solve

max soc Maximal number of second order correction trial stepsmaxit Maximum number of iterations (same as max iter)

min refinement stepsMinimum number of iterative refinement steps per linearsystem solve

mu init Initial value for the barrier parametermu max Maximal value for barrier parameter for adaptive strategymu oracle Oracle for a new barrier parameter in the adaptive strategymu strategy Update strategy for barrier parameternlp scaling max gradient Maximum gradient after scalingnlp scaling method Select the technique used for scaling the NLPobj scaling factor Scaling factor for the objective functionoption file name File name of options file (default ipoptopt)outlev Verbosity level (same as print level)output file File name of an output file (leave unset for no file output)pardiso matching strategy Matching strategy for linear solver Pardisopardiso out of core power Enables out-of-core version of linear solver Pardisoprint level Verbosity levelprint options documentation Print all available options (for ipoptopt)print user options Toggle printing of user optionsrequired infeasibility reduction Required infeasibility reduction in restoration phaseslack bound frac Desired minimal relative distance of initial slack to boundslack bound push Desired minimal absolute distance of initial slack to boundtol Desired convergence tolerance (relative)

wantsol

solution report without -AMPL sum of1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

warm start bound pushEnables to specify how much should variables should bepushed inside the feasible region

warm start init point Enables to specify bound multiplier values

warm start mult bound pushEnables to specify how much should bound multipliersshould be pushed inside the feasible region

watchdog shortened iter trigger Trigger counter for watchdog procedurewsmp num threads Number of threads to be used in WSMPwsmp pivtol Pivot tolerance for the linear solver WSMPwsmp pivtolmax Maximum pivot tolerance for the linear solver WSMPwsmp scaling Determines how the matrix is scaled by WSMP

23

24

23 KNITRO

ldquoKNITRO is an optimization software library for finding solutions of both continuous (smo-oth) optimization models (with or without constraints) as well as discrete optimization modelswith integer or binary variables (ie mixed integer programs) KNITRO is primarily designedfor finding local optimal solutions of large-scale continuous nonlinear problemsrdquo The KNITROlibrary can be found in httpwwwartelyscom7

231 Basic information

KNITRO is a software package for solving smooth optimization problems with or withoutconstraints

Developed at Ziena Optimization

Operating system(s) KNITRO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) KNITRO is written C C++ Fortran and Java

Programming interfaces(s) Fortan CC++ Java and Microsoft Excel

Modeling language interface(s) KNITRO offers a total of five interfaces Matlab AMPL Math-ematica AIMMS GAMS and MPL

232 Installing KNITRO on Linux

How to get a license

KNITRO is developed by Ziena Optimization LLC and marketed and supported by ArtelysThe first step is to request a license by selecting the download tab8 Along with the academicand commercial trial versions there is a student version limited in problem size (300 variablesand 300 constraints) available for 6 months Each license is personalised hence among otherpersonal information you should inform about

bull the operating system (OS)

bull the OS bit architecture

bull the Machine ID of the computer on which KNITRO will runDownload extract an script available bellow the Machine ID field

After reading and accepting the End User License Agreement the download of the KNITROwill be available and you will get a license key on your email Then you should put the licensefile whose name begins with ziena lic in your $HOME directory or in the $HOMEzienadirectory

How to install

The KNITRO software package for Unix is delivered as a gzipped tar file After downloadingKNITRO you need to unpack it Type in a Terminal window

gunzip knitro-8x-platformnametargz

tar -xvf knitro-8x-platformnametar

sudo mv knitro-8x-z opt

7httpwwwartelyscomindexphppage=knitroamphl=en_EN8httpwwwartelyscomindexphppage=knitroamphl=en_ENdownloads-tab

25

This will create a directory named knitro-8x-z in your opt folder

In order to run KNITRO binary or executable files from anywhere on your Linux computerit is necessary to set several environment variables In particular you must update the PATH

environment variable so that it indicates the location of the knitroampl directory You mustalso update the LD LIBRARY PATH environment variable so that it indicates the location of theKNITRO lib directory Setting the PATH and LD LIBRARY PATH environment variables on a Linuxsystem can be done as follows

echo rsquoexport PATH=$PATHoptknitro-8x-zrsquo gtgt $HOMEbashrcecho rsquoexport PATH=$PATHoptknitro-8x-zknitroamplrsquo gtgt $HOMEbashrcecho rsquoexport LD LIBRARY PATH=$LD LIBRARY PATHoptknitro-8x-zlibrsquo gtgt $HOMEbashrc

Each Matlab release expects the KNITRO library file on Linux to have a specific name There-fore to use KNITRO 8x or later with Matlab release R2008a through 2011b you must createa symbolic link to the expected name Open a Terminal window and in the KNITRO librarydirectory issue the command

bull for Matlab releases R2008a R2008b or R2009aln -s libknitroso800 libknitroso5

bull for Matlab releases R2009b R2010a or R2010bln -s libknitroso800 libknitroso6

bull for Matlab releases R2011a R2011b or R2012aln -s libknitroso800 libknitroso7

Documentation

More informations can be found in the following documentation9

KNITRO 80 User Manual Knitro80 UserManualpdfVersion December 2011 (816 KB)

Specific KNITROMatlab interface documentation can be found on the Matlab OptimizationToolbox webpages10

233 Getting started with KNITRO and AMPL

option solver knitroampl

option knitro options rsquooption1=value1 option2=value2rsquo

solve

9httpwwwartelyscomuploadspdfsKnitro80_UserManualpdf10httpwwwmathworkscomaccesshelpdeskhelptoolboxoptim

26

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 10: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

142 Installing BOCOP on Linux

In order to install BOCOP software under LINUXUNIX operating system download BO-COP software from the official website7 and extract the files

tar xvf bocop-103-beta-linux32targz

mv bocop-103-beta-linux32 optbocop

cd optbocop

make

sudo chmod +x bocopsh

Add the following to your bashrc or bash profile

BOCOPDIR=optbocop

export PATH=$PATH$BOCOPDIR

143 Getting started with BOCOP

Read the userrsquos guide8 and run the examples shown in the optbocopproblems folder

15 DIDO ndash Automatic Control And Dynamic Optimization

ldquoDIDO the leading optimal control software powers users by offering the easiest and directsolutions to the most complex problemsrdquo The DIDO software can be found in the official site9

151 Basic information

Developed at Elissar Global

Operating system(s) DIDO can be used on Microsoft Windows

Code language(s) DIDO requires MATLAB and it is released under academic and commerciallicenses

152 Installing DIDO

DIDOrsquos foundation is pseudospectral theory and is the only pseudospectral solution withmathematically proven convergence properties DIDO is a MATLAB program for solving hybridoptimal control problems The generalndashpurpose program is named after Dido the legendaryfounder and first queen of Carthage who is famous in mathematics for her remarkable solutionto a constrained optimal control problem even before the invention of calculus

To install DIDO on your computer you should consider the system requirements

bull WINDOWS

bull MATLAB

and the following steps

7httpwwwbocoporg8httpbocopsaclayinriafrdownload=109httpwwwelissarglobalcomindustryproductssoftware-3

10

STEP 1 Obtain a license for DIDO from Elissar Global at the official site10

STEP 2 After you receive an email with the link to a compressed version of DIDO downloadthe zip file and uncompress it It will automatically uncompress with the right folderstructure

STEP 3 Right click on the DIDO icon and click on PROPERTIES In the Target box type locationof your licensed MATLAB file In the Start in box type the full path name of your DIDOfolder

STEP 4 Doublendashclick on the DIDO ICON (this should start MATLAB and automatically initial-ize DIDO)

(a) In the MATLAB command window type TestDIDOA successful installation should produce the final output on the screenldquoCONGRATULATIONS DIDO TEST WAS SUCCESSFULrdquo

(b) A further test may be performed by typing LanderProblemA successful installation should produce a pretty graph

153 Getting started with DIDO

In the MATLAB command window run[cost primal] = dido(Problem)

where Problem is a structure array with fields cost dynamics events and path that point to theinput files

Problemcost = lsquoMyCostFilersquo

Problemdynamics = lsquoMyDynamicsFilersquo

Problemevents = lsquoMyEventsFilersquo

Problempath = lsquoMyPathFilersquo

The variable primal is used in nearly all DIDO files to pass the states controls parametersand other useful quantities To print out the statendash and controlndashtrajectory in the MATLABcommand window runprimalstates primalcontrols

This generality allows for

bull Fairly complex interior point constraints

bull Pre-defined segments

bull Differentially-flat segments

bull Transition conditions

bull Midndashmaneuver changes in dynamics

bull Multi-dynamical systems

bull Midndashmaneuver changes in the cost function

bull Switches

bull Discrete events10httpwwwelissarglobalcomacademicget-didotry-dido

11

16 ICLOCS ndash Imperial College London Optimal Control Software

ldquoThe code allows users to define and solve optimal control problems with general path andboundary constraints and free or fixed final time It is also possible to include constant designparameters as unknownsrdquo The ICLOCS software can be found in the official site11

161 Basic information

Developed by Paola Falugi Eric Kerrigan and Eugene van Wyk

Operating system(s) ICLOCS can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) ICLOCS is implemented in MATLAB and it is released as open source codeunder the BSD License

162 Installing ICLOCS on Linux

In order to install ICLOCS software under LINUXUNIX operating system download ICLOCSsoftware from the official site12 and extract the files

wget httpwwweeicacukICLOCSICLOCS 02 NItarzip

unzip ICLOCS 02 NItarzip

tar xvf ICLOCS 02 NItar

sudo mv ICLOCS 02 NI opticlocs

In order to use it in MATLAB you need to tell MATLAB where to find it To do this justtype addpath(genpath(rsquoopticlocsrsquo)) in the MATLAB command window

163 Getting started with ICLOCS

Read the userrsquos guide13 and run the examples shown in the opticlocsexamples folder

17 TACO ndash Toolkit for AMPL Control Optimization

ldquoTACO is the Toolkit for AMPL Control Optimization It defines some add-ons to the AMPLmodeling language that allow the elegant formulation of ODEDAE optimal control problemsin AMPLrdquo The TACO toolkit can be found in the official site14

171 Basic information

Developed by Christian Kirches and Sven Leyffer

Operating system(s) TACO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) TACO is written in C

11httpwwweeicacukICLOCS12httpwwweeicacukICLOCS13httpwwweeicacukICLOCSuser_guidepdf14httpwwwiwruni-heidelbergde~ChristianKirchessoftwarehtml

12

172 Installing TACO on Linux

In order to install TADO Toolkit under LINUXUNIX operating systems make sure thatCMAKE and a C++ compiler is correctly installed To install CMAKE type in a Terminal win-dow

wget httpslaunchpadnetubuntu+archiveprimary+filescmake 288origtargz

gunzip cmake 288origtargz

tar xvf cmake 288origtar

cd cmake-288

sudo bootstrap

sudo make

sudo make install

First download TACO Toolkit from the offical site15 and extract the files

wget httpswwwiwruni-heidelbergdegroupsagbockFILEStaco sourcetargzgunzip taco sourcetargz

tar xfvz taco sourcetar

sudo ln -s optCoinIpoptThirdPartyASLlibamplsolvera libamplsolvera

18 Pseudospectral Methods in Optimal Control

181 PSOPT

PSOPT is an open source optimal control software package written in C++ that uses directcollocation methods including pseudospectral and local discretizations available in the officesite16 Pseudospectral methods solve optimal control problems by approximating the time-dependent variables using global polynomials such as Legendre or Chebyshev functions Localdiscretisation methods approximate the time dependent functions using local splines and canbe seen as implementations of implicit Runge-Kutta integrators With both global and localmethods differential equations continuous constraints and integrals associated with the prob-lem are discretised over a grid of nodes Sparse nonlinear programming is then used to findlocal optimal solutions

PSOPT is able to deal with problems with the following characteristics

bull Single or multiphase problems

bull Continuous time nonlinear dynamics

bull Nonlinear path constraints

bull General event constraints

bull Integral constraints

bull Interior point constraints

bull Bounds on controls and state variables

bull General cost function with Lagrange and Mayer terms

15httpwwwiwruni-heidelbergde~ChristianKirchessoftwarehtml16httpwwwpsoptorg

13

bull Linear or nonlinear linkages between phases

bull Fixed or free initial phase time

bull Fixed or free final phase time

bull Optimization of static parameters

bull Optimal parameter estimation given sampled observations

The implementation has the following features

bull Choice between Legendre Chebyshev central differences trapezoidal or Hermite-Simpsondiscretisation

bull Large scale nonlinear programming using IPOPT and (optionally) SNOPT

bull Estimation of the discretisation error

bull Automatic mesh refinement

bull Automatic scaling

bull Automatic differentiation using the ADOL-C library

bull Numerical differentiation by using sparse finite differences

bull Automatic identification of the sparsity of the derivative matrices

bull DAE formulation so that differential and algebraic constraints can be implemented in thesame C++ function

bull Easy to use interface to GNUplot to produce graphical output including 2D plots 3Dcurves and surfaces and polar plots

bull Automatic generation of LaTeX code to produce a table that summarizes the mesh refine-ment process

Full details on PSOPT and its features can be found in its documentation17

182 GPOPS-II - MATLAB Optimal Control Software

GPOPS-II is a general purpose optimal control software available in the office site18 GPOPS-II is a new open-source MATLAB optimal control software that implements a brand new hp-adaptive Legendre-Gauss-Radau quadrature integral pseudospectral method for solving generalnonlinear optimal control problems Using GPOPS-II the optimal control problem is transcribedto a nonlinear programming problem (NLP) The NLP is then solved using either the solverSNOPT or the solver IPOPT

The following are some the key features of GPOPS-II

bull Allows for an extremely general formulation of the optimal control problem

bull Allows for inclusion of integral constraints and highly general boundary conditions

bull Complete first and second sparse finite-differencing of optimal control problem to com-pute all derivatives required by the NLP solver

17httpcodegooglecomppsoptdownloadsdetailname=PSOPT_Manual_R3pdfampcan=2ampq=18httpwwwgpopsorg

14

bull The latest advances in mesh refinement including hpndashadaptive pseudospectral methods

bull Gaussian quadrature integration methods for rapid convergence

bull Highly accurate costate estimation

bull Inclusion of the NLP solver SNOPT (for Academic Users) and IPOPT (for Not-for-Profitand Commercial Users)

bull No third-party products other than MATLAB are required

More information about the methodology used in GPOPS-II can be found by reading therelevant articles in the open literature19

The fees for using GPOPS-II are as follows

bull For students and all others employed at academic institutions NO CHARGE

bull US Federal State or Local Government NO CHARGE

bull Users at not-for-profit institutions (including non-US Government agencies) or commer-cial institutions LICENSING FEES APPLY

19httpvdolmaeufledu

15

16

Chapter 2

Nonlinear Programming Solvers

21 Introduction

A solver is a software used to compute numerical solution of an optimal control problemIn general an optimal controlnonlinear programming interface is needed to prepare all dataas input to the solver (see Figure 21) Several solvers will be presented in this chapter evolvingopenndashsource freeware and commercial software working under different operating systemsSince all solvers require configuration lists of parameters will be presented for some of them

Figure 21

22 IPOPT ndash Interior Point OPTimizer

ldquoIPOPT is a software package for largendashscale nonlinear optimisation It is designed to find(local) solutions of mathematical optimisation problemsrdquo The IPOPT library can be found inhttpwwwartelyscom1

221 Basic information

Developed by Andreas Wachter and Carl Laird

Operating system(s) IPOPT can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) IPOPT is written in C++ and is released as open source code under theEclipse Public License (EPL)

1httpwwwartelyscomindexphppage=knitroamphl=en_EN

17

Modeling language interface(s) IPOPT can be used as a library that can be linked to C++ C orFortran code as well as a solver executable for the AMPL modelling environment Thepackage includes interfaces to CUTEr optimisation testing environment as well as theMATLAB and R programming environments

222 Installing IPOPT

How to install IPOPT on Linux

Getting the IPOPT code via subversion

sudo svn co httpsprojectscoin-ororgsvnIpoptstable310 optCoinIpopt

This will create a directory named CoinIpopt in your opt folder

BLAS routines that provide standard building blocks for performing basic vector and matrixoperations

LAPACK routines for solving systems of simultaneous linear equations least-squares solutionsof linear systems of equations eigenvalue problems and singular value problems

HSL state-of-the-art packages for large-scale scientific computation

ASL AMPL Solver Library allows to read the nl files and provides the automatic differentia-tion functionality

MUMPS MUltifrontal Massively Parallel sparse direct Solver

METIS programs for partitioning graphs partitioning finite element meshes and producing fillreducing orderings for sparse matrices

To install IPOPT just run

cd optCoinIpoptThirdPartyBlas

getBlas

cd Lapack

getLapack

cd ASL

getASL

cd Mumps

getMumps

cd Metis

getMetis

IPOPT requires a sparse symmetric linear solver There are different possibilities The userrsquosguide of IPOPT suggests that you use HSL subroutines

1 Go to HSL website2 and find the package list

2 Click on MA27 read the license and submit the registration form

3 Go back to the package list

4 Click on MC19 read the license and submit the registration form

2httphslrlacukarchivehslarchivehtml

18

You will receive in your email the links to obtain the HSL packages Download them to your$HOME directory and run

gunzip ma27-100targz

tar xvf ma27-100tar

gunzip mc19-100targz

tar xvf mc19-100tar

cd optCoinIpoptThirdPartyHSL

sudo mv -r $HOMEmc19-100

sudo mv -r $HOMEma27-100

cd mc19-100

sudo configure

sudo make install

sudo make check

cd ma27-100

sudo configure

sudo make install

sudo make check

The make check command will check if the installation of the HSL packages was successfulReturn to the optCoinIpopt directory and continue the IPOPT installation

cd

sudo mkdir build

cd build

sudo configure

sudo make test

sudo make install

If you get the error message ldquoerrorlsquoNULLrsquo was not declared in this scoperdquo when run-ning sudo make then you have to edit the following filesoptCoinIpoptIpoptsrcCommonIpSmartPtrhpp

optCoinIpoptIpoptsrcAlgorithmLinearSolversIpTripletToCSRConvertercpp

by adding include ltcstddefgt

How to install IPOPT on Mac OS X

In a Terminal window install homebrewusrbinruby -e $(usrbincurl -fsSL httpsrawgithubcommxclhomebrewmaster

LibraryContributionsinstall homebrewrb)

With this software you can easily install dependency packages like wget and gfortran

brew install wget

brew install gfortran

sudo wget httpwwwcoin-ororgdownloadsourceIpoptIpopt-3102zip

sudo unzip Ipopt-3102zip

cd Ipopt-3102

Download Netlib BlasLapack

cd optCoinIpoptThirdPartyBlas

sudo getBlas

19

cd Lapack

sudo getLapack

cd ASL

sudo getASL

cd Mumps

sudo getMumps

cd Metis

sudo getMetis

Make a build for a 64bit target

sudo mkdir build64

cd build64

sudo configure --disable-shared --with-blas=BUILD --with-lapack=BUILD F77=gfortran

FFLAGS=-fexceptions -m64 -fbackslash CFLAGS=-fno-common -no-cpp-precomp -fexceptions

-arch x86 64 -m64 CXXFLAGS=-fno-common -no-cpp-precomp -fexceptions -arch x86 64

-m64

Compile test and install

make

make test

make install

Add the following to your bashrc or bash profile

IPOPTDIR=optCoinIpoptbuild

export PATH=$PATH$IPOPTDIR$IPOPTDIRbinexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyASLexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyBlaslibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyHSLlibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyLapacklibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMetislibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMumpslibs

How to install the Matlab interface on Linux

First of all be sure

cd optmatlabR2008asysosglnx86

mv libgcc sso1 libgcc sso1bck

ln -s libi386-linux-gnulibgcc sso1 libgcc sso1

mv libstdc++so608 libstdc++so608bck

ln -s usrlibi386-linux-gnulibstdc++so6 libstdc++so608

Go to the subdirectory optCoinIpoptbuildIpoptcontribMatlabInterfacesrc andopen the Makefile file

sudo apt-get install hardening-wrapper

You need to edit this file to suit the installation for your system setup You may need tomodify MATLAB HOME MEXSUFFIX and MEX as explained in the comments of the Makefile I setthese variables as follows

20

MATLAB HOME = optmatlabR2008a

MEXSUFFIX = mexglx

MEX = optmatlabR2008abinmex

In my case I removed some of the compiler options as it follows

prefix = optCoinIpopt

CXX = optintelcomposer xe 2011 sp19293binia32icpc

CCXXFLAGS = -O3 -pipe -DNDEBUG -Wparentheses -Wreturn-type -Wcast-qual -Wall

-Wpointer-arith -Wwrite-strings -Wconversion -Wno-unknown-pragmas -DMATLAB MEXFILE

LDFLAGS = $(CXXFLAGS) -Wl --rpath -Wl $prefixlibcoin -Wl --rpath -Wl

--rpath -Wl$prefixlibcoinThirdParty

In my case I had to set explicitly the include Matlab folder

o cpp

$(CXX) $(CXXFLAGS) $(INCL) -IoptmatlabR2008aexterninclude -o $ -c $^

Once you set up the Makefile properly type sudo make install in the same directory as theMakefile If the installation procedure was successful you will end up with a MEX file calledipoptmexglx In order to use it in MATLAB you need to tell MATLAB where to find it Todo this just type addpath optCoinIpoptlib in the MATLAB command window after youcheck if the ipoptmexglx file is in this folder

Documentation

More informations can be found in the following documentation

Online documentation httpwwwcoin-ororgIpoptdocumentation3

A PDF version of this documentation can be downloaded here4

A short IPOPT tutorial can be downloaded here5

MATLAB interface Instructions specific to the MATLAB interface can be found at the Matlabinterface page6

223 Getting started with IPOPT and AMPL

option solver ipopt

option ipopt options rsquooption1=value1 option2=value2rsquo

solve

3httpwwwcoin-ororgIpoptdocumentation4httpsprojectscoin-ororgIpoptbrowserstable39Ipoptdocdocumentationpdfformat=raw5httpdropsdagstuhldevolltexte20092089pdf09061WaechterAndreasPaper2089pdf6httpsprojectscoin-ororgIpoptwikiMatlabInterface

21

Table 21 IPOPT directives for AMPL

Option Description

acceptable compl inf tol Acceptance threshold for the complementarity conditionsacceptable constr viol tol Acceptance threshold for the constraint violationacceptable dual inf tol Acceptance threshold for the dual infeasibilityacceptable tol Acceptable convergence tolerance (relative)alpha for y Step size for constraint multipliersbound frac Desired minimal relative distance of initial point to boundbound mult init val Initial value for the bound multipliersbound push Desired minimal absolute distance of initial point to boundbound relax factor Factor for initial relaxation of the boundscompl inf tol Acceptance threshold for the complementarity conditionsconstr mult init max Maximal allowed least-square guess of constraint multipliersconstr viol tol Desired threshold for the constraint violationdiverging iterates tol Threshold for maximal value of primal iteratesdual inf tol Desired threshold for the dual infeasibilityexpect infeasible problem Enable heuristics to quickly detect an infeasible problemfile print level Verbosity level for output filehalt on ampl error Exit with message on evaluation errorhessian approximation Can enable Quasi-Newton approximation of hessianhonor original bounds If no solution might slightly violate boundslinear scaling on demand Enables heuristic for scaling only when seems requiredlinear solver Linear solver to be used for step calculationlinear system scaling Method for scaling the linear systemsma27 pivtol Pivot tolerance for the linear solver MA27ma27 pivtolmax Maximal pivot tolerance for the linear solver MA27ma57 pivtol Pivot tolerance for the linear solver MA57ma57 pivtolmax Maximal pivot tolerance for the linear solver MA57

22

Table 22 IPOPT directives for AMPL

Option Description

max cpu time CPU time limitmax iter Maximum number of iterations

max refinement stepsMaximal number of iterative refinement steps per linearsystem solve

max soc Maximal number of second order correction trial stepsmaxit Maximum number of iterations (same as max iter)

min refinement stepsMinimum number of iterative refinement steps per linearsystem solve

mu init Initial value for the barrier parametermu max Maximal value for barrier parameter for adaptive strategymu oracle Oracle for a new barrier parameter in the adaptive strategymu strategy Update strategy for barrier parameternlp scaling max gradient Maximum gradient after scalingnlp scaling method Select the technique used for scaling the NLPobj scaling factor Scaling factor for the objective functionoption file name File name of options file (default ipoptopt)outlev Verbosity level (same as print level)output file File name of an output file (leave unset for no file output)pardiso matching strategy Matching strategy for linear solver Pardisopardiso out of core power Enables out-of-core version of linear solver Pardisoprint level Verbosity levelprint options documentation Print all available options (for ipoptopt)print user options Toggle printing of user optionsrequired infeasibility reduction Required infeasibility reduction in restoration phaseslack bound frac Desired minimal relative distance of initial slack to boundslack bound push Desired minimal absolute distance of initial slack to boundtol Desired convergence tolerance (relative)

wantsol

solution report without -AMPL sum of1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

warm start bound pushEnables to specify how much should variables should bepushed inside the feasible region

warm start init point Enables to specify bound multiplier values

warm start mult bound pushEnables to specify how much should bound multipliersshould be pushed inside the feasible region

watchdog shortened iter trigger Trigger counter for watchdog procedurewsmp num threads Number of threads to be used in WSMPwsmp pivtol Pivot tolerance for the linear solver WSMPwsmp pivtolmax Maximum pivot tolerance for the linear solver WSMPwsmp scaling Determines how the matrix is scaled by WSMP

23

24

23 KNITRO

ldquoKNITRO is an optimization software library for finding solutions of both continuous (smo-oth) optimization models (with or without constraints) as well as discrete optimization modelswith integer or binary variables (ie mixed integer programs) KNITRO is primarily designedfor finding local optimal solutions of large-scale continuous nonlinear problemsrdquo The KNITROlibrary can be found in httpwwwartelyscom7

231 Basic information

KNITRO is a software package for solving smooth optimization problems with or withoutconstraints

Developed at Ziena Optimization

Operating system(s) KNITRO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) KNITRO is written C C++ Fortran and Java

Programming interfaces(s) Fortan CC++ Java and Microsoft Excel

Modeling language interface(s) KNITRO offers a total of five interfaces Matlab AMPL Math-ematica AIMMS GAMS and MPL

232 Installing KNITRO on Linux

How to get a license

KNITRO is developed by Ziena Optimization LLC and marketed and supported by ArtelysThe first step is to request a license by selecting the download tab8 Along with the academicand commercial trial versions there is a student version limited in problem size (300 variablesand 300 constraints) available for 6 months Each license is personalised hence among otherpersonal information you should inform about

bull the operating system (OS)

bull the OS bit architecture

bull the Machine ID of the computer on which KNITRO will runDownload extract an script available bellow the Machine ID field

After reading and accepting the End User License Agreement the download of the KNITROwill be available and you will get a license key on your email Then you should put the licensefile whose name begins with ziena lic in your $HOME directory or in the $HOMEzienadirectory

How to install

The KNITRO software package for Unix is delivered as a gzipped tar file After downloadingKNITRO you need to unpack it Type in a Terminal window

gunzip knitro-8x-platformnametargz

tar -xvf knitro-8x-platformnametar

sudo mv knitro-8x-z opt

7httpwwwartelyscomindexphppage=knitroamphl=en_EN8httpwwwartelyscomindexphppage=knitroamphl=en_ENdownloads-tab

25

This will create a directory named knitro-8x-z in your opt folder

In order to run KNITRO binary or executable files from anywhere on your Linux computerit is necessary to set several environment variables In particular you must update the PATH

environment variable so that it indicates the location of the knitroampl directory You mustalso update the LD LIBRARY PATH environment variable so that it indicates the location of theKNITRO lib directory Setting the PATH and LD LIBRARY PATH environment variables on a Linuxsystem can be done as follows

echo rsquoexport PATH=$PATHoptknitro-8x-zrsquo gtgt $HOMEbashrcecho rsquoexport PATH=$PATHoptknitro-8x-zknitroamplrsquo gtgt $HOMEbashrcecho rsquoexport LD LIBRARY PATH=$LD LIBRARY PATHoptknitro-8x-zlibrsquo gtgt $HOMEbashrc

Each Matlab release expects the KNITRO library file on Linux to have a specific name There-fore to use KNITRO 8x or later with Matlab release R2008a through 2011b you must createa symbolic link to the expected name Open a Terminal window and in the KNITRO librarydirectory issue the command

bull for Matlab releases R2008a R2008b or R2009aln -s libknitroso800 libknitroso5

bull for Matlab releases R2009b R2010a or R2010bln -s libknitroso800 libknitroso6

bull for Matlab releases R2011a R2011b or R2012aln -s libknitroso800 libknitroso7

Documentation

More informations can be found in the following documentation9

KNITRO 80 User Manual Knitro80 UserManualpdfVersion December 2011 (816 KB)

Specific KNITROMatlab interface documentation can be found on the Matlab OptimizationToolbox webpages10

233 Getting started with KNITRO and AMPL

option solver knitroampl

option knitro options rsquooption1=value1 option2=value2rsquo

solve

9httpwwwartelyscomuploadspdfsKnitro80_UserManualpdf10httpwwwmathworkscomaccesshelpdeskhelptoolboxoptim

26

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 11: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

STEP 1 Obtain a license for DIDO from Elissar Global at the official site10

STEP 2 After you receive an email with the link to a compressed version of DIDO downloadthe zip file and uncompress it It will automatically uncompress with the right folderstructure

STEP 3 Right click on the DIDO icon and click on PROPERTIES In the Target box type locationof your licensed MATLAB file In the Start in box type the full path name of your DIDOfolder

STEP 4 Doublendashclick on the DIDO ICON (this should start MATLAB and automatically initial-ize DIDO)

(a) In the MATLAB command window type TestDIDOA successful installation should produce the final output on the screenldquoCONGRATULATIONS DIDO TEST WAS SUCCESSFULrdquo

(b) A further test may be performed by typing LanderProblemA successful installation should produce a pretty graph

153 Getting started with DIDO

In the MATLAB command window run[cost primal] = dido(Problem)

where Problem is a structure array with fields cost dynamics events and path that point to theinput files

Problemcost = lsquoMyCostFilersquo

Problemdynamics = lsquoMyDynamicsFilersquo

Problemevents = lsquoMyEventsFilersquo

Problempath = lsquoMyPathFilersquo

The variable primal is used in nearly all DIDO files to pass the states controls parametersand other useful quantities To print out the statendash and controlndashtrajectory in the MATLABcommand window runprimalstates primalcontrols

This generality allows for

bull Fairly complex interior point constraints

bull Pre-defined segments

bull Differentially-flat segments

bull Transition conditions

bull Midndashmaneuver changes in dynamics

bull Multi-dynamical systems

bull Midndashmaneuver changes in the cost function

bull Switches

bull Discrete events10httpwwwelissarglobalcomacademicget-didotry-dido

11

16 ICLOCS ndash Imperial College London Optimal Control Software

ldquoThe code allows users to define and solve optimal control problems with general path andboundary constraints and free or fixed final time It is also possible to include constant designparameters as unknownsrdquo The ICLOCS software can be found in the official site11

161 Basic information

Developed by Paola Falugi Eric Kerrigan and Eugene van Wyk

Operating system(s) ICLOCS can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) ICLOCS is implemented in MATLAB and it is released as open source codeunder the BSD License

162 Installing ICLOCS on Linux

In order to install ICLOCS software under LINUXUNIX operating system download ICLOCSsoftware from the official site12 and extract the files

wget httpwwweeicacukICLOCSICLOCS 02 NItarzip

unzip ICLOCS 02 NItarzip

tar xvf ICLOCS 02 NItar

sudo mv ICLOCS 02 NI opticlocs

In order to use it in MATLAB you need to tell MATLAB where to find it To do this justtype addpath(genpath(rsquoopticlocsrsquo)) in the MATLAB command window

163 Getting started with ICLOCS

Read the userrsquos guide13 and run the examples shown in the opticlocsexamples folder

17 TACO ndash Toolkit for AMPL Control Optimization

ldquoTACO is the Toolkit for AMPL Control Optimization It defines some add-ons to the AMPLmodeling language that allow the elegant formulation of ODEDAE optimal control problemsin AMPLrdquo The TACO toolkit can be found in the official site14

171 Basic information

Developed by Christian Kirches and Sven Leyffer

Operating system(s) TACO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) TACO is written in C

11httpwwweeicacukICLOCS12httpwwweeicacukICLOCS13httpwwweeicacukICLOCSuser_guidepdf14httpwwwiwruni-heidelbergde~ChristianKirchessoftwarehtml

12

172 Installing TACO on Linux

In order to install TADO Toolkit under LINUXUNIX operating systems make sure thatCMAKE and a C++ compiler is correctly installed To install CMAKE type in a Terminal win-dow

wget httpslaunchpadnetubuntu+archiveprimary+filescmake 288origtargz

gunzip cmake 288origtargz

tar xvf cmake 288origtar

cd cmake-288

sudo bootstrap

sudo make

sudo make install

First download TACO Toolkit from the offical site15 and extract the files

wget httpswwwiwruni-heidelbergdegroupsagbockFILEStaco sourcetargzgunzip taco sourcetargz

tar xfvz taco sourcetar

sudo ln -s optCoinIpoptThirdPartyASLlibamplsolvera libamplsolvera

18 Pseudospectral Methods in Optimal Control

181 PSOPT

PSOPT is an open source optimal control software package written in C++ that uses directcollocation methods including pseudospectral and local discretizations available in the officesite16 Pseudospectral methods solve optimal control problems by approximating the time-dependent variables using global polynomials such as Legendre or Chebyshev functions Localdiscretisation methods approximate the time dependent functions using local splines and canbe seen as implementations of implicit Runge-Kutta integrators With both global and localmethods differential equations continuous constraints and integrals associated with the prob-lem are discretised over a grid of nodes Sparse nonlinear programming is then used to findlocal optimal solutions

PSOPT is able to deal with problems with the following characteristics

bull Single or multiphase problems

bull Continuous time nonlinear dynamics

bull Nonlinear path constraints

bull General event constraints

bull Integral constraints

bull Interior point constraints

bull Bounds on controls and state variables

bull General cost function with Lagrange and Mayer terms

15httpwwwiwruni-heidelbergde~ChristianKirchessoftwarehtml16httpwwwpsoptorg

13

bull Linear or nonlinear linkages between phases

bull Fixed or free initial phase time

bull Fixed or free final phase time

bull Optimization of static parameters

bull Optimal parameter estimation given sampled observations

The implementation has the following features

bull Choice between Legendre Chebyshev central differences trapezoidal or Hermite-Simpsondiscretisation

bull Large scale nonlinear programming using IPOPT and (optionally) SNOPT

bull Estimation of the discretisation error

bull Automatic mesh refinement

bull Automatic scaling

bull Automatic differentiation using the ADOL-C library

bull Numerical differentiation by using sparse finite differences

bull Automatic identification of the sparsity of the derivative matrices

bull DAE formulation so that differential and algebraic constraints can be implemented in thesame C++ function

bull Easy to use interface to GNUplot to produce graphical output including 2D plots 3Dcurves and surfaces and polar plots

bull Automatic generation of LaTeX code to produce a table that summarizes the mesh refine-ment process

Full details on PSOPT and its features can be found in its documentation17

182 GPOPS-II - MATLAB Optimal Control Software

GPOPS-II is a general purpose optimal control software available in the office site18 GPOPS-II is a new open-source MATLAB optimal control software that implements a brand new hp-adaptive Legendre-Gauss-Radau quadrature integral pseudospectral method for solving generalnonlinear optimal control problems Using GPOPS-II the optimal control problem is transcribedto a nonlinear programming problem (NLP) The NLP is then solved using either the solverSNOPT or the solver IPOPT

The following are some the key features of GPOPS-II

bull Allows for an extremely general formulation of the optimal control problem

bull Allows for inclusion of integral constraints and highly general boundary conditions

bull Complete first and second sparse finite-differencing of optimal control problem to com-pute all derivatives required by the NLP solver

17httpcodegooglecomppsoptdownloadsdetailname=PSOPT_Manual_R3pdfampcan=2ampq=18httpwwwgpopsorg

14

bull The latest advances in mesh refinement including hpndashadaptive pseudospectral methods

bull Gaussian quadrature integration methods for rapid convergence

bull Highly accurate costate estimation

bull Inclusion of the NLP solver SNOPT (for Academic Users) and IPOPT (for Not-for-Profitand Commercial Users)

bull No third-party products other than MATLAB are required

More information about the methodology used in GPOPS-II can be found by reading therelevant articles in the open literature19

The fees for using GPOPS-II are as follows

bull For students and all others employed at academic institutions NO CHARGE

bull US Federal State or Local Government NO CHARGE

bull Users at not-for-profit institutions (including non-US Government agencies) or commer-cial institutions LICENSING FEES APPLY

19httpvdolmaeufledu

15

16

Chapter 2

Nonlinear Programming Solvers

21 Introduction

A solver is a software used to compute numerical solution of an optimal control problemIn general an optimal controlnonlinear programming interface is needed to prepare all dataas input to the solver (see Figure 21) Several solvers will be presented in this chapter evolvingopenndashsource freeware and commercial software working under different operating systemsSince all solvers require configuration lists of parameters will be presented for some of them

Figure 21

22 IPOPT ndash Interior Point OPTimizer

ldquoIPOPT is a software package for largendashscale nonlinear optimisation It is designed to find(local) solutions of mathematical optimisation problemsrdquo The IPOPT library can be found inhttpwwwartelyscom1

221 Basic information

Developed by Andreas Wachter and Carl Laird

Operating system(s) IPOPT can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) IPOPT is written in C++ and is released as open source code under theEclipse Public License (EPL)

1httpwwwartelyscomindexphppage=knitroamphl=en_EN

17

Modeling language interface(s) IPOPT can be used as a library that can be linked to C++ C orFortran code as well as a solver executable for the AMPL modelling environment Thepackage includes interfaces to CUTEr optimisation testing environment as well as theMATLAB and R programming environments

222 Installing IPOPT

How to install IPOPT on Linux

Getting the IPOPT code via subversion

sudo svn co httpsprojectscoin-ororgsvnIpoptstable310 optCoinIpopt

This will create a directory named CoinIpopt in your opt folder

BLAS routines that provide standard building blocks for performing basic vector and matrixoperations

LAPACK routines for solving systems of simultaneous linear equations least-squares solutionsof linear systems of equations eigenvalue problems and singular value problems

HSL state-of-the-art packages for large-scale scientific computation

ASL AMPL Solver Library allows to read the nl files and provides the automatic differentia-tion functionality

MUMPS MUltifrontal Massively Parallel sparse direct Solver

METIS programs for partitioning graphs partitioning finite element meshes and producing fillreducing orderings for sparse matrices

To install IPOPT just run

cd optCoinIpoptThirdPartyBlas

getBlas

cd Lapack

getLapack

cd ASL

getASL

cd Mumps

getMumps

cd Metis

getMetis

IPOPT requires a sparse symmetric linear solver There are different possibilities The userrsquosguide of IPOPT suggests that you use HSL subroutines

1 Go to HSL website2 and find the package list

2 Click on MA27 read the license and submit the registration form

3 Go back to the package list

4 Click on MC19 read the license and submit the registration form

2httphslrlacukarchivehslarchivehtml

18

You will receive in your email the links to obtain the HSL packages Download them to your$HOME directory and run

gunzip ma27-100targz

tar xvf ma27-100tar

gunzip mc19-100targz

tar xvf mc19-100tar

cd optCoinIpoptThirdPartyHSL

sudo mv -r $HOMEmc19-100

sudo mv -r $HOMEma27-100

cd mc19-100

sudo configure

sudo make install

sudo make check

cd ma27-100

sudo configure

sudo make install

sudo make check

The make check command will check if the installation of the HSL packages was successfulReturn to the optCoinIpopt directory and continue the IPOPT installation

cd

sudo mkdir build

cd build

sudo configure

sudo make test

sudo make install

If you get the error message ldquoerrorlsquoNULLrsquo was not declared in this scoperdquo when run-ning sudo make then you have to edit the following filesoptCoinIpoptIpoptsrcCommonIpSmartPtrhpp

optCoinIpoptIpoptsrcAlgorithmLinearSolversIpTripletToCSRConvertercpp

by adding include ltcstddefgt

How to install IPOPT on Mac OS X

In a Terminal window install homebrewusrbinruby -e $(usrbincurl -fsSL httpsrawgithubcommxclhomebrewmaster

LibraryContributionsinstall homebrewrb)

With this software you can easily install dependency packages like wget and gfortran

brew install wget

brew install gfortran

sudo wget httpwwwcoin-ororgdownloadsourceIpoptIpopt-3102zip

sudo unzip Ipopt-3102zip

cd Ipopt-3102

Download Netlib BlasLapack

cd optCoinIpoptThirdPartyBlas

sudo getBlas

19

cd Lapack

sudo getLapack

cd ASL

sudo getASL

cd Mumps

sudo getMumps

cd Metis

sudo getMetis

Make a build for a 64bit target

sudo mkdir build64

cd build64

sudo configure --disable-shared --with-blas=BUILD --with-lapack=BUILD F77=gfortran

FFLAGS=-fexceptions -m64 -fbackslash CFLAGS=-fno-common -no-cpp-precomp -fexceptions

-arch x86 64 -m64 CXXFLAGS=-fno-common -no-cpp-precomp -fexceptions -arch x86 64

-m64

Compile test and install

make

make test

make install

Add the following to your bashrc or bash profile

IPOPTDIR=optCoinIpoptbuild

export PATH=$PATH$IPOPTDIR$IPOPTDIRbinexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyASLexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyBlaslibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyHSLlibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyLapacklibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMetislibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMumpslibs

How to install the Matlab interface on Linux

First of all be sure

cd optmatlabR2008asysosglnx86

mv libgcc sso1 libgcc sso1bck

ln -s libi386-linux-gnulibgcc sso1 libgcc sso1

mv libstdc++so608 libstdc++so608bck

ln -s usrlibi386-linux-gnulibstdc++so6 libstdc++so608

Go to the subdirectory optCoinIpoptbuildIpoptcontribMatlabInterfacesrc andopen the Makefile file

sudo apt-get install hardening-wrapper

You need to edit this file to suit the installation for your system setup You may need tomodify MATLAB HOME MEXSUFFIX and MEX as explained in the comments of the Makefile I setthese variables as follows

20

MATLAB HOME = optmatlabR2008a

MEXSUFFIX = mexglx

MEX = optmatlabR2008abinmex

In my case I removed some of the compiler options as it follows

prefix = optCoinIpopt

CXX = optintelcomposer xe 2011 sp19293binia32icpc

CCXXFLAGS = -O3 -pipe -DNDEBUG -Wparentheses -Wreturn-type -Wcast-qual -Wall

-Wpointer-arith -Wwrite-strings -Wconversion -Wno-unknown-pragmas -DMATLAB MEXFILE

LDFLAGS = $(CXXFLAGS) -Wl --rpath -Wl $prefixlibcoin -Wl --rpath -Wl

--rpath -Wl$prefixlibcoinThirdParty

In my case I had to set explicitly the include Matlab folder

o cpp

$(CXX) $(CXXFLAGS) $(INCL) -IoptmatlabR2008aexterninclude -o $ -c $^

Once you set up the Makefile properly type sudo make install in the same directory as theMakefile If the installation procedure was successful you will end up with a MEX file calledipoptmexglx In order to use it in MATLAB you need to tell MATLAB where to find it Todo this just type addpath optCoinIpoptlib in the MATLAB command window after youcheck if the ipoptmexglx file is in this folder

Documentation

More informations can be found in the following documentation

Online documentation httpwwwcoin-ororgIpoptdocumentation3

A PDF version of this documentation can be downloaded here4

A short IPOPT tutorial can be downloaded here5

MATLAB interface Instructions specific to the MATLAB interface can be found at the Matlabinterface page6

223 Getting started with IPOPT and AMPL

option solver ipopt

option ipopt options rsquooption1=value1 option2=value2rsquo

solve

3httpwwwcoin-ororgIpoptdocumentation4httpsprojectscoin-ororgIpoptbrowserstable39Ipoptdocdocumentationpdfformat=raw5httpdropsdagstuhldevolltexte20092089pdf09061WaechterAndreasPaper2089pdf6httpsprojectscoin-ororgIpoptwikiMatlabInterface

21

Table 21 IPOPT directives for AMPL

Option Description

acceptable compl inf tol Acceptance threshold for the complementarity conditionsacceptable constr viol tol Acceptance threshold for the constraint violationacceptable dual inf tol Acceptance threshold for the dual infeasibilityacceptable tol Acceptable convergence tolerance (relative)alpha for y Step size for constraint multipliersbound frac Desired minimal relative distance of initial point to boundbound mult init val Initial value for the bound multipliersbound push Desired minimal absolute distance of initial point to boundbound relax factor Factor for initial relaxation of the boundscompl inf tol Acceptance threshold for the complementarity conditionsconstr mult init max Maximal allowed least-square guess of constraint multipliersconstr viol tol Desired threshold for the constraint violationdiverging iterates tol Threshold for maximal value of primal iteratesdual inf tol Desired threshold for the dual infeasibilityexpect infeasible problem Enable heuristics to quickly detect an infeasible problemfile print level Verbosity level for output filehalt on ampl error Exit with message on evaluation errorhessian approximation Can enable Quasi-Newton approximation of hessianhonor original bounds If no solution might slightly violate boundslinear scaling on demand Enables heuristic for scaling only when seems requiredlinear solver Linear solver to be used for step calculationlinear system scaling Method for scaling the linear systemsma27 pivtol Pivot tolerance for the linear solver MA27ma27 pivtolmax Maximal pivot tolerance for the linear solver MA27ma57 pivtol Pivot tolerance for the linear solver MA57ma57 pivtolmax Maximal pivot tolerance for the linear solver MA57

22

Table 22 IPOPT directives for AMPL

Option Description

max cpu time CPU time limitmax iter Maximum number of iterations

max refinement stepsMaximal number of iterative refinement steps per linearsystem solve

max soc Maximal number of second order correction trial stepsmaxit Maximum number of iterations (same as max iter)

min refinement stepsMinimum number of iterative refinement steps per linearsystem solve

mu init Initial value for the barrier parametermu max Maximal value for barrier parameter for adaptive strategymu oracle Oracle for a new barrier parameter in the adaptive strategymu strategy Update strategy for barrier parameternlp scaling max gradient Maximum gradient after scalingnlp scaling method Select the technique used for scaling the NLPobj scaling factor Scaling factor for the objective functionoption file name File name of options file (default ipoptopt)outlev Verbosity level (same as print level)output file File name of an output file (leave unset for no file output)pardiso matching strategy Matching strategy for linear solver Pardisopardiso out of core power Enables out-of-core version of linear solver Pardisoprint level Verbosity levelprint options documentation Print all available options (for ipoptopt)print user options Toggle printing of user optionsrequired infeasibility reduction Required infeasibility reduction in restoration phaseslack bound frac Desired minimal relative distance of initial slack to boundslack bound push Desired minimal absolute distance of initial slack to boundtol Desired convergence tolerance (relative)

wantsol

solution report without -AMPL sum of1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

warm start bound pushEnables to specify how much should variables should bepushed inside the feasible region

warm start init point Enables to specify bound multiplier values

warm start mult bound pushEnables to specify how much should bound multipliersshould be pushed inside the feasible region

watchdog shortened iter trigger Trigger counter for watchdog procedurewsmp num threads Number of threads to be used in WSMPwsmp pivtol Pivot tolerance for the linear solver WSMPwsmp pivtolmax Maximum pivot tolerance for the linear solver WSMPwsmp scaling Determines how the matrix is scaled by WSMP

23

24

23 KNITRO

ldquoKNITRO is an optimization software library for finding solutions of both continuous (smo-oth) optimization models (with or without constraints) as well as discrete optimization modelswith integer or binary variables (ie mixed integer programs) KNITRO is primarily designedfor finding local optimal solutions of large-scale continuous nonlinear problemsrdquo The KNITROlibrary can be found in httpwwwartelyscom7

231 Basic information

KNITRO is a software package for solving smooth optimization problems with or withoutconstraints

Developed at Ziena Optimization

Operating system(s) KNITRO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) KNITRO is written C C++ Fortran and Java

Programming interfaces(s) Fortan CC++ Java and Microsoft Excel

Modeling language interface(s) KNITRO offers a total of five interfaces Matlab AMPL Math-ematica AIMMS GAMS and MPL

232 Installing KNITRO on Linux

How to get a license

KNITRO is developed by Ziena Optimization LLC and marketed and supported by ArtelysThe first step is to request a license by selecting the download tab8 Along with the academicand commercial trial versions there is a student version limited in problem size (300 variablesand 300 constraints) available for 6 months Each license is personalised hence among otherpersonal information you should inform about

bull the operating system (OS)

bull the OS bit architecture

bull the Machine ID of the computer on which KNITRO will runDownload extract an script available bellow the Machine ID field

After reading and accepting the End User License Agreement the download of the KNITROwill be available and you will get a license key on your email Then you should put the licensefile whose name begins with ziena lic in your $HOME directory or in the $HOMEzienadirectory

How to install

The KNITRO software package for Unix is delivered as a gzipped tar file After downloadingKNITRO you need to unpack it Type in a Terminal window

gunzip knitro-8x-platformnametargz

tar -xvf knitro-8x-platformnametar

sudo mv knitro-8x-z opt

7httpwwwartelyscomindexphppage=knitroamphl=en_EN8httpwwwartelyscomindexphppage=knitroamphl=en_ENdownloads-tab

25

This will create a directory named knitro-8x-z in your opt folder

In order to run KNITRO binary or executable files from anywhere on your Linux computerit is necessary to set several environment variables In particular you must update the PATH

environment variable so that it indicates the location of the knitroampl directory You mustalso update the LD LIBRARY PATH environment variable so that it indicates the location of theKNITRO lib directory Setting the PATH and LD LIBRARY PATH environment variables on a Linuxsystem can be done as follows

echo rsquoexport PATH=$PATHoptknitro-8x-zrsquo gtgt $HOMEbashrcecho rsquoexport PATH=$PATHoptknitro-8x-zknitroamplrsquo gtgt $HOMEbashrcecho rsquoexport LD LIBRARY PATH=$LD LIBRARY PATHoptknitro-8x-zlibrsquo gtgt $HOMEbashrc

Each Matlab release expects the KNITRO library file on Linux to have a specific name There-fore to use KNITRO 8x or later with Matlab release R2008a through 2011b you must createa symbolic link to the expected name Open a Terminal window and in the KNITRO librarydirectory issue the command

bull for Matlab releases R2008a R2008b or R2009aln -s libknitroso800 libknitroso5

bull for Matlab releases R2009b R2010a or R2010bln -s libknitroso800 libknitroso6

bull for Matlab releases R2011a R2011b or R2012aln -s libknitroso800 libknitroso7

Documentation

More informations can be found in the following documentation9

KNITRO 80 User Manual Knitro80 UserManualpdfVersion December 2011 (816 KB)

Specific KNITROMatlab interface documentation can be found on the Matlab OptimizationToolbox webpages10

233 Getting started with KNITRO and AMPL

option solver knitroampl

option knitro options rsquooption1=value1 option2=value2rsquo

solve

9httpwwwartelyscomuploadspdfsKnitro80_UserManualpdf10httpwwwmathworkscomaccesshelpdeskhelptoolboxoptim

26

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 12: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

16 ICLOCS ndash Imperial College London Optimal Control Software

ldquoThe code allows users to define and solve optimal control problems with general path andboundary constraints and free or fixed final time It is also possible to include constant designparameters as unknownsrdquo The ICLOCS software can be found in the official site11

161 Basic information

Developed by Paola Falugi Eric Kerrigan and Eugene van Wyk

Operating system(s) ICLOCS can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) ICLOCS is implemented in MATLAB and it is released as open source codeunder the BSD License

162 Installing ICLOCS on Linux

In order to install ICLOCS software under LINUXUNIX operating system download ICLOCSsoftware from the official site12 and extract the files

wget httpwwweeicacukICLOCSICLOCS 02 NItarzip

unzip ICLOCS 02 NItarzip

tar xvf ICLOCS 02 NItar

sudo mv ICLOCS 02 NI opticlocs

In order to use it in MATLAB you need to tell MATLAB where to find it To do this justtype addpath(genpath(rsquoopticlocsrsquo)) in the MATLAB command window

163 Getting started with ICLOCS

Read the userrsquos guide13 and run the examples shown in the opticlocsexamples folder

17 TACO ndash Toolkit for AMPL Control Optimization

ldquoTACO is the Toolkit for AMPL Control Optimization It defines some add-ons to the AMPLmodeling language that allow the elegant formulation of ODEDAE optimal control problemsin AMPLrdquo The TACO toolkit can be found in the official site14

171 Basic information

Developed by Christian Kirches and Sven Leyffer

Operating system(s) TACO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) TACO is written in C

11httpwwweeicacukICLOCS12httpwwweeicacukICLOCS13httpwwweeicacukICLOCSuser_guidepdf14httpwwwiwruni-heidelbergde~ChristianKirchessoftwarehtml

12

172 Installing TACO on Linux

In order to install TADO Toolkit under LINUXUNIX operating systems make sure thatCMAKE and a C++ compiler is correctly installed To install CMAKE type in a Terminal win-dow

wget httpslaunchpadnetubuntu+archiveprimary+filescmake 288origtargz

gunzip cmake 288origtargz

tar xvf cmake 288origtar

cd cmake-288

sudo bootstrap

sudo make

sudo make install

First download TACO Toolkit from the offical site15 and extract the files

wget httpswwwiwruni-heidelbergdegroupsagbockFILEStaco sourcetargzgunzip taco sourcetargz

tar xfvz taco sourcetar

sudo ln -s optCoinIpoptThirdPartyASLlibamplsolvera libamplsolvera

18 Pseudospectral Methods in Optimal Control

181 PSOPT

PSOPT is an open source optimal control software package written in C++ that uses directcollocation methods including pseudospectral and local discretizations available in the officesite16 Pseudospectral methods solve optimal control problems by approximating the time-dependent variables using global polynomials such as Legendre or Chebyshev functions Localdiscretisation methods approximate the time dependent functions using local splines and canbe seen as implementations of implicit Runge-Kutta integrators With both global and localmethods differential equations continuous constraints and integrals associated with the prob-lem are discretised over a grid of nodes Sparse nonlinear programming is then used to findlocal optimal solutions

PSOPT is able to deal with problems with the following characteristics

bull Single or multiphase problems

bull Continuous time nonlinear dynamics

bull Nonlinear path constraints

bull General event constraints

bull Integral constraints

bull Interior point constraints

bull Bounds on controls and state variables

bull General cost function with Lagrange and Mayer terms

15httpwwwiwruni-heidelbergde~ChristianKirchessoftwarehtml16httpwwwpsoptorg

13

bull Linear or nonlinear linkages between phases

bull Fixed or free initial phase time

bull Fixed or free final phase time

bull Optimization of static parameters

bull Optimal parameter estimation given sampled observations

The implementation has the following features

bull Choice between Legendre Chebyshev central differences trapezoidal or Hermite-Simpsondiscretisation

bull Large scale nonlinear programming using IPOPT and (optionally) SNOPT

bull Estimation of the discretisation error

bull Automatic mesh refinement

bull Automatic scaling

bull Automatic differentiation using the ADOL-C library

bull Numerical differentiation by using sparse finite differences

bull Automatic identification of the sparsity of the derivative matrices

bull DAE formulation so that differential and algebraic constraints can be implemented in thesame C++ function

bull Easy to use interface to GNUplot to produce graphical output including 2D plots 3Dcurves and surfaces and polar plots

bull Automatic generation of LaTeX code to produce a table that summarizes the mesh refine-ment process

Full details on PSOPT and its features can be found in its documentation17

182 GPOPS-II - MATLAB Optimal Control Software

GPOPS-II is a general purpose optimal control software available in the office site18 GPOPS-II is a new open-source MATLAB optimal control software that implements a brand new hp-adaptive Legendre-Gauss-Radau quadrature integral pseudospectral method for solving generalnonlinear optimal control problems Using GPOPS-II the optimal control problem is transcribedto a nonlinear programming problem (NLP) The NLP is then solved using either the solverSNOPT or the solver IPOPT

The following are some the key features of GPOPS-II

bull Allows for an extremely general formulation of the optimal control problem

bull Allows for inclusion of integral constraints and highly general boundary conditions

bull Complete first and second sparse finite-differencing of optimal control problem to com-pute all derivatives required by the NLP solver

17httpcodegooglecomppsoptdownloadsdetailname=PSOPT_Manual_R3pdfampcan=2ampq=18httpwwwgpopsorg

14

bull The latest advances in mesh refinement including hpndashadaptive pseudospectral methods

bull Gaussian quadrature integration methods for rapid convergence

bull Highly accurate costate estimation

bull Inclusion of the NLP solver SNOPT (for Academic Users) and IPOPT (for Not-for-Profitand Commercial Users)

bull No third-party products other than MATLAB are required

More information about the methodology used in GPOPS-II can be found by reading therelevant articles in the open literature19

The fees for using GPOPS-II are as follows

bull For students and all others employed at academic institutions NO CHARGE

bull US Federal State or Local Government NO CHARGE

bull Users at not-for-profit institutions (including non-US Government agencies) or commer-cial institutions LICENSING FEES APPLY

19httpvdolmaeufledu

15

16

Chapter 2

Nonlinear Programming Solvers

21 Introduction

A solver is a software used to compute numerical solution of an optimal control problemIn general an optimal controlnonlinear programming interface is needed to prepare all dataas input to the solver (see Figure 21) Several solvers will be presented in this chapter evolvingopenndashsource freeware and commercial software working under different operating systemsSince all solvers require configuration lists of parameters will be presented for some of them

Figure 21

22 IPOPT ndash Interior Point OPTimizer

ldquoIPOPT is a software package for largendashscale nonlinear optimisation It is designed to find(local) solutions of mathematical optimisation problemsrdquo The IPOPT library can be found inhttpwwwartelyscom1

221 Basic information

Developed by Andreas Wachter and Carl Laird

Operating system(s) IPOPT can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) IPOPT is written in C++ and is released as open source code under theEclipse Public License (EPL)

1httpwwwartelyscomindexphppage=knitroamphl=en_EN

17

Modeling language interface(s) IPOPT can be used as a library that can be linked to C++ C orFortran code as well as a solver executable for the AMPL modelling environment Thepackage includes interfaces to CUTEr optimisation testing environment as well as theMATLAB and R programming environments

222 Installing IPOPT

How to install IPOPT on Linux

Getting the IPOPT code via subversion

sudo svn co httpsprojectscoin-ororgsvnIpoptstable310 optCoinIpopt

This will create a directory named CoinIpopt in your opt folder

BLAS routines that provide standard building blocks for performing basic vector and matrixoperations

LAPACK routines for solving systems of simultaneous linear equations least-squares solutionsof linear systems of equations eigenvalue problems and singular value problems

HSL state-of-the-art packages for large-scale scientific computation

ASL AMPL Solver Library allows to read the nl files and provides the automatic differentia-tion functionality

MUMPS MUltifrontal Massively Parallel sparse direct Solver

METIS programs for partitioning graphs partitioning finite element meshes and producing fillreducing orderings for sparse matrices

To install IPOPT just run

cd optCoinIpoptThirdPartyBlas

getBlas

cd Lapack

getLapack

cd ASL

getASL

cd Mumps

getMumps

cd Metis

getMetis

IPOPT requires a sparse symmetric linear solver There are different possibilities The userrsquosguide of IPOPT suggests that you use HSL subroutines

1 Go to HSL website2 and find the package list

2 Click on MA27 read the license and submit the registration form

3 Go back to the package list

4 Click on MC19 read the license and submit the registration form

2httphslrlacukarchivehslarchivehtml

18

You will receive in your email the links to obtain the HSL packages Download them to your$HOME directory and run

gunzip ma27-100targz

tar xvf ma27-100tar

gunzip mc19-100targz

tar xvf mc19-100tar

cd optCoinIpoptThirdPartyHSL

sudo mv -r $HOMEmc19-100

sudo mv -r $HOMEma27-100

cd mc19-100

sudo configure

sudo make install

sudo make check

cd ma27-100

sudo configure

sudo make install

sudo make check

The make check command will check if the installation of the HSL packages was successfulReturn to the optCoinIpopt directory and continue the IPOPT installation

cd

sudo mkdir build

cd build

sudo configure

sudo make test

sudo make install

If you get the error message ldquoerrorlsquoNULLrsquo was not declared in this scoperdquo when run-ning sudo make then you have to edit the following filesoptCoinIpoptIpoptsrcCommonIpSmartPtrhpp

optCoinIpoptIpoptsrcAlgorithmLinearSolversIpTripletToCSRConvertercpp

by adding include ltcstddefgt

How to install IPOPT on Mac OS X

In a Terminal window install homebrewusrbinruby -e $(usrbincurl -fsSL httpsrawgithubcommxclhomebrewmaster

LibraryContributionsinstall homebrewrb)

With this software you can easily install dependency packages like wget and gfortran

brew install wget

brew install gfortran

sudo wget httpwwwcoin-ororgdownloadsourceIpoptIpopt-3102zip

sudo unzip Ipopt-3102zip

cd Ipopt-3102

Download Netlib BlasLapack

cd optCoinIpoptThirdPartyBlas

sudo getBlas

19

cd Lapack

sudo getLapack

cd ASL

sudo getASL

cd Mumps

sudo getMumps

cd Metis

sudo getMetis

Make a build for a 64bit target

sudo mkdir build64

cd build64

sudo configure --disable-shared --with-blas=BUILD --with-lapack=BUILD F77=gfortran

FFLAGS=-fexceptions -m64 -fbackslash CFLAGS=-fno-common -no-cpp-precomp -fexceptions

-arch x86 64 -m64 CXXFLAGS=-fno-common -no-cpp-precomp -fexceptions -arch x86 64

-m64

Compile test and install

make

make test

make install

Add the following to your bashrc or bash profile

IPOPTDIR=optCoinIpoptbuild

export PATH=$PATH$IPOPTDIR$IPOPTDIRbinexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyASLexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyBlaslibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyHSLlibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyLapacklibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMetislibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMumpslibs

How to install the Matlab interface on Linux

First of all be sure

cd optmatlabR2008asysosglnx86

mv libgcc sso1 libgcc sso1bck

ln -s libi386-linux-gnulibgcc sso1 libgcc sso1

mv libstdc++so608 libstdc++so608bck

ln -s usrlibi386-linux-gnulibstdc++so6 libstdc++so608

Go to the subdirectory optCoinIpoptbuildIpoptcontribMatlabInterfacesrc andopen the Makefile file

sudo apt-get install hardening-wrapper

You need to edit this file to suit the installation for your system setup You may need tomodify MATLAB HOME MEXSUFFIX and MEX as explained in the comments of the Makefile I setthese variables as follows

20

MATLAB HOME = optmatlabR2008a

MEXSUFFIX = mexglx

MEX = optmatlabR2008abinmex

In my case I removed some of the compiler options as it follows

prefix = optCoinIpopt

CXX = optintelcomposer xe 2011 sp19293binia32icpc

CCXXFLAGS = -O3 -pipe -DNDEBUG -Wparentheses -Wreturn-type -Wcast-qual -Wall

-Wpointer-arith -Wwrite-strings -Wconversion -Wno-unknown-pragmas -DMATLAB MEXFILE

LDFLAGS = $(CXXFLAGS) -Wl --rpath -Wl $prefixlibcoin -Wl --rpath -Wl

--rpath -Wl$prefixlibcoinThirdParty

In my case I had to set explicitly the include Matlab folder

o cpp

$(CXX) $(CXXFLAGS) $(INCL) -IoptmatlabR2008aexterninclude -o $ -c $^

Once you set up the Makefile properly type sudo make install in the same directory as theMakefile If the installation procedure was successful you will end up with a MEX file calledipoptmexglx In order to use it in MATLAB you need to tell MATLAB where to find it Todo this just type addpath optCoinIpoptlib in the MATLAB command window after youcheck if the ipoptmexglx file is in this folder

Documentation

More informations can be found in the following documentation

Online documentation httpwwwcoin-ororgIpoptdocumentation3

A PDF version of this documentation can be downloaded here4

A short IPOPT tutorial can be downloaded here5

MATLAB interface Instructions specific to the MATLAB interface can be found at the Matlabinterface page6

223 Getting started with IPOPT and AMPL

option solver ipopt

option ipopt options rsquooption1=value1 option2=value2rsquo

solve

3httpwwwcoin-ororgIpoptdocumentation4httpsprojectscoin-ororgIpoptbrowserstable39Ipoptdocdocumentationpdfformat=raw5httpdropsdagstuhldevolltexte20092089pdf09061WaechterAndreasPaper2089pdf6httpsprojectscoin-ororgIpoptwikiMatlabInterface

21

Table 21 IPOPT directives for AMPL

Option Description

acceptable compl inf tol Acceptance threshold for the complementarity conditionsacceptable constr viol tol Acceptance threshold for the constraint violationacceptable dual inf tol Acceptance threshold for the dual infeasibilityacceptable tol Acceptable convergence tolerance (relative)alpha for y Step size for constraint multipliersbound frac Desired minimal relative distance of initial point to boundbound mult init val Initial value for the bound multipliersbound push Desired minimal absolute distance of initial point to boundbound relax factor Factor for initial relaxation of the boundscompl inf tol Acceptance threshold for the complementarity conditionsconstr mult init max Maximal allowed least-square guess of constraint multipliersconstr viol tol Desired threshold for the constraint violationdiverging iterates tol Threshold for maximal value of primal iteratesdual inf tol Desired threshold for the dual infeasibilityexpect infeasible problem Enable heuristics to quickly detect an infeasible problemfile print level Verbosity level for output filehalt on ampl error Exit with message on evaluation errorhessian approximation Can enable Quasi-Newton approximation of hessianhonor original bounds If no solution might slightly violate boundslinear scaling on demand Enables heuristic for scaling only when seems requiredlinear solver Linear solver to be used for step calculationlinear system scaling Method for scaling the linear systemsma27 pivtol Pivot tolerance for the linear solver MA27ma27 pivtolmax Maximal pivot tolerance for the linear solver MA27ma57 pivtol Pivot tolerance for the linear solver MA57ma57 pivtolmax Maximal pivot tolerance for the linear solver MA57

22

Table 22 IPOPT directives for AMPL

Option Description

max cpu time CPU time limitmax iter Maximum number of iterations

max refinement stepsMaximal number of iterative refinement steps per linearsystem solve

max soc Maximal number of second order correction trial stepsmaxit Maximum number of iterations (same as max iter)

min refinement stepsMinimum number of iterative refinement steps per linearsystem solve

mu init Initial value for the barrier parametermu max Maximal value for barrier parameter for adaptive strategymu oracle Oracle for a new barrier parameter in the adaptive strategymu strategy Update strategy for barrier parameternlp scaling max gradient Maximum gradient after scalingnlp scaling method Select the technique used for scaling the NLPobj scaling factor Scaling factor for the objective functionoption file name File name of options file (default ipoptopt)outlev Verbosity level (same as print level)output file File name of an output file (leave unset for no file output)pardiso matching strategy Matching strategy for linear solver Pardisopardiso out of core power Enables out-of-core version of linear solver Pardisoprint level Verbosity levelprint options documentation Print all available options (for ipoptopt)print user options Toggle printing of user optionsrequired infeasibility reduction Required infeasibility reduction in restoration phaseslack bound frac Desired minimal relative distance of initial slack to boundslack bound push Desired minimal absolute distance of initial slack to boundtol Desired convergence tolerance (relative)

wantsol

solution report without -AMPL sum of1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

warm start bound pushEnables to specify how much should variables should bepushed inside the feasible region

warm start init point Enables to specify bound multiplier values

warm start mult bound pushEnables to specify how much should bound multipliersshould be pushed inside the feasible region

watchdog shortened iter trigger Trigger counter for watchdog procedurewsmp num threads Number of threads to be used in WSMPwsmp pivtol Pivot tolerance for the linear solver WSMPwsmp pivtolmax Maximum pivot tolerance for the linear solver WSMPwsmp scaling Determines how the matrix is scaled by WSMP

23

24

23 KNITRO

ldquoKNITRO is an optimization software library for finding solutions of both continuous (smo-oth) optimization models (with or without constraints) as well as discrete optimization modelswith integer or binary variables (ie mixed integer programs) KNITRO is primarily designedfor finding local optimal solutions of large-scale continuous nonlinear problemsrdquo The KNITROlibrary can be found in httpwwwartelyscom7

231 Basic information

KNITRO is a software package for solving smooth optimization problems with or withoutconstraints

Developed at Ziena Optimization

Operating system(s) KNITRO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) KNITRO is written C C++ Fortran and Java

Programming interfaces(s) Fortan CC++ Java and Microsoft Excel

Modeling language interface(s) KNITRO offers a total of five interfaces Matlab AMPL Math-ematica AIMMS GAMS and MPL

232 Installing KNITRO on Linux

How to get a license

KNITRO is developed by Ziena Optimization LLC and marketed and supported by ArtelysThe first step is to request a license by selecting the download tab8 Along with the academicand commercial trial versions there is a student version limited in problem size (300 variablesand 300 constraints) available for 6 months Each license is personalised hence among otherpersonal information you should inform about

bull the operating system (OS)

bull the OS bit architecture

bull the Machine ID of the computer on which KNITRO will runDownload extract an script available bellow the Machine ID field

After reading and accepting the End User License Agreement the download of the KNITROwill be available and you will get a license key on your email Then you should put the licensefile whose name begins with ziena lic in your $HOME directory or in the $HOMEzienadirectory

How to install

The KNITRO software package for Unix is delivered as a gzipped tar file After downloadingKNITRO you need to unpack it Type in a Terminal window

gunzip knitro-8x-platformnametargz

tar -xvf knitro-8x-platformnametar

sudo mv knitro-8x-z opt

7httpwwwartelyscomindexphppage=knitroamphl=en_EN8httpwwwartelyscomindexphppage=knitroamphl=en_ENdownloads-tab

25

This will create a directory named knitro-8x-z in your opt folder

In order to run KNITRO binary or executable files from anywhere on your Linux computerit is necessary to set several environment variables In particular you must update the PATH

environment variable so that it indicates the location of the knitroampl directory You mustalso update the LD LIBRARY PATH environment variable so that it indicates the location of theKNITRO lib directory Setting the PATH and LD LIBRARY PATH environment variables on a Linuxsystem can be done as follows

echo rsquoexport PATH=$PATHoptknitro-8x-zrsquo gtgt $HOMEbashrcecho rsquoexport PATH=$PATHoptknitro-8x-zknitroamplrsquo gtgt $HOMEbashrcecho rsquoexport LD LIBRARY PATH=$LD LIBRARY PATHoptknitro-8x-zlibrsquo gtgt $HOMEbashrc

Each Matlab release expects the KNITRO library file on Linux to have a specific name There-fore to use KNITRO 8x or later with Matlab release R2008a through 2011b you must createa symbolic link to the expected name Open a Terminal window and in the KNITRO librarydirectory issue the command

bull for Matlab releases R2008a R2008b or R2009aln -s libknitroso800 libknitroso5

bull for Matlab releases R2009b R2010a or R2010bln -s libknitroso800 libknitroso6

bull for Matlab releases R2011a R2011b or R2012aln -s libknitroso800 libknitroso7

Documentation

More informations can be found in the following documentation9

KNITRO 80 User Manual Knitro80 UserManualpdfVersion December 2011 (816 KB)

Specific KNITROMatlab interface documentation can be found on the Matlab OptimizationToolbox webpages10

233 Getting started with KNITRO and AMPL

option solver knitroampl

option knitro options rsquooption1=value1 option2=value2rsquo

solve

9httpwwwartelyscomuploadspdfsKnitro80_UserManualpdf10httpwwwmathworkscomaccesshelpdeskhelptoolboxoptim

26

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 13: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

172 Installing TACO on Linux

In order to install TADO Toolkit under LINUXUNIX operating systems make sure thatCMAKE and a C++ compiler is correctly installed To install CMAKE type in a Terminal win-dow

wget httpslaunchpadnetubuntu+archiveprimary+filescmake 288origtargz

gunzip cmake 288origtargz

tar xvf cmake 288origtar

cd cmake-288

sudo bootstrap

sudo make

sudo make install

First download TACO Toolkit from the offical site15 and extract the files

wget httpswwwiwruni-heidelbergdegroupsagbockFILEStaco sourcetargzgunzip taco sourcetargz

tar xfvz taco sourcetar

sudo ln -s optCoinIpoptThirdPartyASLlibamplsolvera libamplsolvera

18 Pseudospectral Methods in Optimal Control

181 PSOPT

PSOPT is an open source optimal control software package written in C++ that uses directcollocation methods including pseudospectral and local discretizations available in the officesite16 Pseudospectral methods solve optimal control problems by approximating the time-dependent variables using global polynomials such as Legendre or Chebyshev functions Localdiscretisation methods approximate the time dependent functions using local splines and canbe seen as implementations of implicit Runge-Kutta integrators With both global and localmethods differential equations continuous constraints and integrals associated with the prob-lem are discretised over a grid of nodes Sparse nonlinear programming is then used to findlocal optimal solutions

PSOPT is able to deal with problems with the following characteristics

bull Single or multiphase problems

bull Continuous time nonlinear dynamics

bull Nonlinear path constraints

bull General event constraints

bull Integral constraints

bull Interior point constraints

bull Bounds on controls and state variables

bull General cost function with Lagrange and Mayer terms

15httpwwwiwruni-heidelbergde~ChristianKirchessoftwarehtml16httpwwwpsoptorg

13

bull Linear or nonlinear linkages between phases

bull Fixed or free initial phase time

bull Fixed or free final phase time

bull Optimization of static parameters

bull Optimal parameter estimation given sampled observations

The implementation has the following features

bull Choice between Legendre Chebyshev central differences trapezoidal or Hermite-Simpsondiscretisation

bull Large scale nonlinear programming using IPOPT and (optionally) SNOPT

bull Estimation of the discretisation error

bull Automatic mesh refinement

bull Automatic scaling

bull Automatic differentiation using the ADOL-C library

bull Numerical differentiation by using sparse finite differences

bull Automatic identification of the sparsity of the derivative matrices

bull DAE formulation so that differential and algebraic constraints can be implemented in thesame C++ function

bull Easy to use interface to GNUplot to produce graphical output including 2D plots 3Dcurves and surfaces and polar plots

bull Automatic generation of LaTeX code to produce a table that summarizes the mesh refine-ment process

Full details on PSOPT and its features can be found in its documentation17

182 GPOPS-II - MATLAB Optimal Control Software

GPOPS-II is a general purpose optimal control software available in the office site18 GPOPS-II is a new open-source MATLAB optimal control software that implements a brand new hp-adaptive Legendre-Gauss-Radau quadrature integral pseudospectral method for solving generalnonlinear optimal control problems Using GPOPS-II the optimal control problem is transcribedto a nonlinear programming problem (NLP) The NLP is then solved using either the solverSNOPT or the solver IPOPT

The following are some the key features of GPOPS-II

bull Allows for an extremely general formulation of the optimal control problem

bull Allows for inclusion of integral constraints and highly general boundary conditions

bull Complete first and second sparse finite-differencing of optimal control problem to com-pute all derivatives required by the NLP solver

17httpcodegooglecomppsoptdownloadsdetailname=PSOPT_Manual_R3pdfampcan=2ampq=18httpwwwgpopsorg

14

bull The latest advances in mesh refinement including hpndashadaptive pseudospectral methods

bull Gaussian quadrature integration methods for rapid convergence

bull Highly accurate costate estimation

bull Inclusion of the NLP solver SNOPT (for Academic Users) and IPOPT (for Not-for-Profitand Commercial Users)

bull No third-party products other than MATLAB are required

More information about the methodology used in GPOPS-II can be found by reading therelevant articles in the open literature19

The fees for using GPOPS-II are as follows

bull For students and all others employed at academic institutions NO CHARGE

bull US Federal State or Local Government NO CHARGE

bull Users at not-for-profit institutions (including non-US Government agencies) or commer-cial institutions LICENSING FEES APPLY

19httpvdolmaeufledu

15

16

Chapter 2

Nonlinear Programming Solvers

21 Introduction

A solver is a software used to compute numerical solution of an optimal control problemIn general an optimal controlnonlinear programming interface is needed to prepare all dataas input to the solver (see Figure 21) Several solvers will be presented in this chapter evolvingopenndashsource freeware and commercial software working under different operating systemsSince all solvers require configuration lists of parameters will be presented for some of them

Figure 21

22 IPOPT ndash Interior Point OPTimizer

ldquoIPOPT is a software package for largendashscale nonlinear optimisation It is designed to find(local) solutions of mathematical optimisation problemsrdquo The IPOPT library can be found inhttpwwwartelyscom1

221 Basic information

Developed by Andreas Wachter and Carl Laird

Operating system(s) IPOPT can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) IPOPT is written in C++ and is released as open source code under theEclipse Public License (EPL)

1httpwwwartelyscomindexphppage=knitroamphl=en_EN

17

Modeling language interface(s) IPOPT can be used as a library that can be linked to C++ C orFortran code as well as a solver executable for the AMPL modelling environment Thepackage includes interfaces to CUTEr optimisation testing environment as well as theMATLAB and R programming environments

222 Installing IPOPT

How to install IPOPT on Linux

Getting the IPOPT code via subversion

sudo svn co httpsprojectscoin-ororgsvnIpoptstable310 optCoinIpopt

This will create a directory named CoinIpopt in your opt folder

BLAS routines that provide standard building blocks for performing basic vector and matrixoperations

LAPACK routines for solving systems of simultaneous linear equations least-squares solutionsof linear systems of equations eigenvalue problems and singular value problems

HSL state-of-the-art packages for large-scale scientific computation

ASL AMPL Solver Library allows to read the nl files and provides the automatic differentia-tion functionality

MUMPS MUltifrontal Massively Parallel sparse direct Solver

METIS programs for partitioning graphs partitioning finite element meshes and producing fillreducing orderings for sparse matrices

To install IPOPT just run

cd optCoinIpoptThirdPartyBlas

getBlas

cd Lapack

getLapack

cd ASL

getASL

cd Mumps

getMumps

cd Metis

getMetis

IPOPT requires a sparse symmetric linear solver There are different possibilities The userrsquosguide of IPOPT suggests that you use HSL subroutines

1 Go to HSL website2 and find the package list

2 Click on MA27 read the license and submit the registration form

3 Go back to the package list

4 Click on MC19 read the license and submit the registration form

2httphslrlacukarchivehslarchivehtml

18

You will receive in your email the links to obtain the HSL packages Download them to your$HOME directory and run

gunzip ma27-100targz

tar xvf ma27-100tar

gunzip mc19-100targz

tar xvf mc19-100tar

cd optCoinIpoptThirdPartyHSL

sudo mv -r $HOMEmc19-100

sudo mv -r $HOMEma27-100

cd mc19-100

sudo configure

sudo make install

sudo make check

cd ma27-100

sudo configure

sudo make install

sudo make check

The make check command will check if the installation of the HSL packages was successfulReturn to the optCoinIpopt directory and continue the IPOPT installation

cd

sudo mkdir build

cd build

sudo configure

sudo make test

sudo make install

If you get the error message ldquoerrorlsquoNULLrsquo was not declared in this scoperdquo when run-ning sudo make then you have to edit the following filesoptCoinIpoptIpoptsrcCommonIpSmartPtrhpp

optCoinIpoptIpoptsrcAlgorithmLinearSolversIpTripletToCSRConvertercpp

by adding include ltcstddefgt

How to install IPOPT on Mac OS X

In a Terminal window install homebrewusrbinruby -e $(usrbincurl -fsSL httpsrawgithubcommxclhomebrewmaster

LibraryContributionsinstall homebrewrb)

With this software you can easily install dependency packages like wget and gfortran

brew install wget

brew install gfortran

sudo wget httpwwwcoin-ororgdownloadsourceIpoptIpopt-3102zip

sudo unzip Ipopt-3102zip

cd Ipopt-3102

Download Netlib BlasLapack

cd optCoinIpoptThirdPartyBlas

sudo getBlas

19

cd Lapack

sudo getLapack

cd ASL

sudo getASL

cd Mumps

sudo getMumps

cd Metis

sudo getMetis

Make a build for a 64bit target

sudo mkdir build64

cd build64

sudo configure --disable-shared --with-blas=BUILD --with-lapack=BUILD F77=gfortran

FFLAGS=-fexceptions -m64 -fbackslash CFLAGS=-fno-common -no-cpp-precomp -fexceptions

-arch x86 64 -m64 CXXFLAGS=-fno-common -no-cpp-precomp -fexceptions -arch x86 64

-m64

Compile test and install

make

make test

make install

Add the following to your bashrc or bash profile

IPOPTDIR=optCoinIpoptbuild

export PATH=$PATH$IPOPTDIR$IPOPTDIRbinexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyASLexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyBlaslibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyHSLlibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyLapacklibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMetislibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMumpslibs

How to install the Matlab interface on Linux

First of all be sure

cd optmatlabR2008asysosglnx86

mv libgcc sso1 libgcc sso1bck

ln -s libi386-linux-gnulibgcc sso1 libgcc sso1

mv libstdc++so608 libstdc++so608bck

ln -s usrlibi386-linux-gnulibstdc++so6 libstdc++so608

Go to the subdirectory optCoinIpoptbuildIpoptcontribMatlabInterfacesrc andopen the Makefile file

sudo apt-get install hardening-wrapper

You need to edit this file to suit the installation for your system setup You may need tomodify MATLAB HOME MEXSUFFIX and MEX as explained in the comments of the Makefile I setthese variables as follows

20

MATLAB HOME = optmatlabR2008a

MEXSUFFIX = mexglx

MEX = optmatlabR2008abinmex

In my case I removed some of the compiler options as it follows

prefix = optCoinIpopt

CXX = optintelcomposer xe 2011 sp19293binia32icpc

CCXXFLAGS = -O3 -pipe -DNDEBUG -Wparentheses -Wreturn-type -Wcast-qual -Wall

-Wpointer-arith -Wwrite-strings -Wconversion -Wno-unknown-pragmas -DMATLAB MEXFILE

LDFLAGS = $(CXXFLAGS) -Wl --rpath -Wl $prefixlibcoin -Wl --rpath -Wl

--rpath -Wl$prefixlibcoinThirdParty

In my case I had to set explicitly the include Matlab folder

o cpp

$(CXX) $(CXXFLAGS) $(INCL) -IoptmatlabR2008aexterninclude -o $ -c $^

Once you set up the Makefile properly type sudo make install in the same directory as theMakefile If the installation procedure was successful you will end up with a MEX file calledipoptmexglx In order to use it in MATLAB you need to tell MATLAB where to find it Todo this just type addpath optCoinIpoptlib in the MATLAB command window after youcheck if the ipoptmexglx file is in this folder

Documentation

More informations can be found in the following documentation

Online documentation httpwwwcoin-ororgIpoptdocumentation3

A PDF version of this documentation can be downloaded here4

A short IPOPT tutorial can be downloaded here5

MATLAB interface Instructions specific to the MATLAB interface can be found at the Matlabinterface page6

223 Getting started with IPOPT and AMPL

option solver ipopt

option ipopt options rsquooption1=value1 option2=value2rsquo

solve

3httpwwwcoin-ororgIpoptdocumentation4httpsprojectscoin-ororgIpoptbrowserstable39Ipoptdocdocumentationpdfformat=raw5httpdropsdagstuhldevolltexte20092089pdf09061WaechterAndreasPaper2089pdf6httpsprojectscoin-ororgIpoptwikiMatlabInterface

21

Table 21 IPOPT directives for AMPL

Option Description

acceptable compl inf tol Acceptance threshold for the complementarity conditionsacceptable constr viol tol Acceptance threshold for the constraint violationacceptable dual inf tol Acceptance threshold for the dual infeasibilityacceptable tol Acceptable convergence tolerance (relative)alpha for y Step size for constraint multipliersbound frac Desired minimal relative distance of initial point to boundbound mult init val Initial value for the bound multipliersbound push Desired minimal absolute distance of initial point to boundbound relax factor Factor for initial relaxation of the boundscompl inf tol Acceptance threshold for the complementarity conditionsconstr mult init max Maximal allowed least-square guess of constraint multipliersconstr viol tol Desired threshold for the constraint violationdiverging iterates tol Threshold for maximal value of primal iteratesdual inf tol Desired threshold for the dual infeasibilityexpect infeasible problem Enable heuristics to quickly detect an infeasible problemfile print level Verbosity level for output filehalt on ampl error Exit with message on evaluation errorhessian approximation Can enable Quasi-Newton approximation of hessianhonor original bounds If no solution might slightly violate boundslinear scaling on demand Enables heuristic for scaling only when seems requiredlinear solver Linear solver to be used for step calculationlinear system scaling Method for scaling the linear systemsma27 pivtol Pivot tolerance for the linear solver MA27ma27 pivtolmax Maximal pivot tolerance for the linear solver MA27ma57 pivtol Pivot tolerance for the linear solver MA57ma57 pivtolmax Maximal pivot tolerance for the linear solver MA57

22

Table 22 IPOPT directives for AMPL

Option Description

max cpu time CPU time limitmax iter Maximum number of iterations

max refinement stepsMaximal number of iterative refinement steps per linearsystem solve

max soc Maximal number of second order correction trial stepsmaxit Maximum number of iterations (same as max iter)

min refinement stepsMinimum number of iterative refinement steps per linearsystem solve

mu init Initial value for the barrier parametermu max Maximal value for barrier parameter for adaptive strategymu oracle Oracle for a new barrier parameter in the adaptive strategymu strategy Update strategy for barrier parameternlp scaling max gradient Maximum gradient after scalingnlp scaling method Select the technique used for scaling the NLPobj scaling factor Scaling factor for the objective functionoption file name File name of options file (default ipoptopt)outlev Verbosity level (same as print level)output file File name of an output file (leave unset for no file output)pardiso matching strategy Matching strategy for linear solver Pardisopardiso out of core power Enables out-of-core version of linear solver Pardisoprint level Verbosity levelprint options documentation Print all available options (for ipoptopt)print user options Toggle printing of user optionsrequired infeasibility reduction Required infeasibility reduction in restoration phaseslack bound frac Desired minimal relative distance of initial slack to boundslack bound push Desired minimal absolute distance of initial slack to boundtol Desired convergence tolerance (relative)

wantsol

solution report without -AMPL sum of1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

warm start bound pushEnables to specify how much should variables should bepushed inside the feasible region

warm start init point Enables to specify bound multiplier values

warm start mult bound pushEnables to specify how much should bound multipliersshould be pushed inside the feasible region

watchdog shortened iter trigger Trigger counter for watchdog procedurewsmp num threads Number of threads to be used in WSMPwsmp pivtol Pivot tolerance for the linear solver WSMPwsmp pivtolmax Maximum pivot tolerance for the linear solver WSMPwsmp scaling Determines how the matrix is scaled by WSMP

23

24

23 KNITRO

ldquoKNITRO is an optimization software library for finding solutions of both continuous (smo-oth) optimization models (with or without constraints) as well as discrete optimization modelswith integer or binary variables (ie mixed integer programs) KNITRO is primarily designedfor finding local optimal solutions of large-scale continuous nonlinear problemsrdquo The KNITROlibrary can be found in httpwwwartelyscom7

231 Basic information

KNITRO is a software package for solving smooth optimization problems with or withoutconstraints

Developed at Ziena Optimization

Operating system(s) KNITRO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) KNITRO is written C C++ Fortran and Java

Programming interfaces(s) Fortan CC++ Java and Microsoft Excel

Modeling language interface(s) KNITRO offers a total of five interfaces Matlab AMPL Math-ematica AIMMS GAMS and MPL

232 Installing KNITRO on Linux

How to get a license

KNITRO is developed by Ziena Optimization LLC and marketed and supported by ArtelysThe first step is to request a license by selecting the download tab8 Along with the academicand commercial trial versions there is a student version limited in problem size (300 variablesand 300 constraints) available for 6 months Each license is personalised hence among otherpersonal information you should inform about

bull the operating system (OS)

bull the OS bit architecture

bull the Machine ID of the computer on which KNITRO will runDownload extract an script available bellow the Machine ID field

After reading and accepting the End User License Agreement the download of the KNITROwill be available and you will get a license key on your email Then you should put the licensefile whose name begins with ziena lic in your $HOME directory or in the $HOMEzienadirectory

How to install

The KNITRO software package for Unix is delivered as a gzipped tar file After downloadingKNITRO you need to unpack it Type in a Terminal window

gunzip knitro-8x-platformnametargz

tar -xvf knitro-8x-platformnametar

sudo mv knitro-8x-z opt

7httpwwwartelyscomindexphppage=knitroamphl=en_EN8httpwwwartelyscomindexphppage=knitroamphl=en_ENdownloads-tab

25

This will create a directory named knitro-8x-z in your opt folder

In order to run KNITRO binary or executable files from anywhere on your Linux computerit is necessary to set several environment variables In particular you must update the PATH

environment variable so that it indicates the location of the knitroampl directory You mustalso update the LD LIBRARY PATH environment variable so that it indicates the location of theKNITRO lib directory Setting the PATH and LD LIBRARY PATH environment variables on a Linuxsystem can be done as follows

echo rsquoexport PATH=$PATHoptknitro-8x-zrsquo gtgt $HOMEbashrcecho rsquoexport PATH=$PATHoptknitro-8x-zknitroamplrsquo gtgt $HOMEbashrcecho rsquoexport LD LIBRARY PATH=$LD LIBRARY PATHoptknitro-8x-zlibrsquo gtgt $HOMEbashrc

Each Matlab release expects the KNITRO library file on Linux to have a specific name There-fore to use KNITRO 8x or later with Matlab release R2008a through 2011b you must createa symbolic link to the expected name Open a Terminal window and in the KNITRO librarydirectory issue the command

bull for Matlab releases R2008a R2008b or R2009aln -s libknitroso800 libknitroso5

bull for Matlab releases R2009b R2010a or R2010bln -s libknitroso800 libknitroso6

bull for Matlab releases R2011a R2011b or R2012aln -s libknitroso800 libknitroso7

Documentation

More informations can be found in the following documentation9

KNITRO 80 User Manual Knitro80 UserManualpdfVersion December 2011 (816 KB)

Specific KNITROMatlab interface documentation can be found on the Matlab OptimizationToolbox webpages10

233 Getting started with KNITRO and AMPL

option solver knitroampl

option knitro options rsquooption1=value1 option2=value2rsquo

solve

9httpwwwartelyscomuploadspdfsKnitro80_UserManualpdf10httpwwwmathworkscomaccesshelpdeskhelptoolboxoptim

26

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 14: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

bull Linear or nonlinear linkages between phases

bull Fixed or free initial phase time

bull Fixed or free final phase time

bull Optimization of static parameters

bull Optimal parameter estimation given sampled observations

The implementation has the following features

bull Choice between Legendre Chebyshev central differences trapezoidal or Hermite-Simpsondiscretisation

bull Large scale nonlinear programming using IPOPT and (optionally) SNOPT

bull Estimation of the discretisation error

bull Automatic mesh refinement

bull Automatic scaling

bull Automatic differentiation using the ADOL-C library

bull Numerical differentiation by using sparse finite differences

bull Automatic identification of the sparsity of the derivative matrices

bull DAE formulation so that differential and algebraic constraints can be implemented in thesame C++ function

bull Easy to use interface to GNUplot to produce graphical output including 2D plots 3Dcurves and surfaces and polar plots

bull Automatic generation of LaTeX code to produce a table that summarizes the mesh refine-ment process

Full details on PSOPT and its features can be found in its documentation17

182 GPOPS-II - MATLAB Optimal Control Software

GPOPS-II is a general purpose optimal control software available in the office site18 GPOPS-II is a new open-source MATLAB optimal control software that implements a brand new hp-adaptive Legendre-Gauss-Radau quadrature integral pseudospectral method for solving generalnonlinear optimal control problems Using GPOPS-II the optimal control problem is transcribedto a nonlinear programming problem (NLP) The NLP is then solved using either the solverSNOPT or the solver IPOPT

The following are some the key features of GPOPS-II

bull Allows for an extremely general formulation of the optimal control problem

bull Allows for inclusion of integral constraints and highly general boundary conditions

bull Complete first and second sparse finite-differencing of optimal control problem to com-pute all derivatives required by the NLP solver

17httpcodegooglecomppsoptdownloadsdetailname=PSOPT_Manual_R3pdfampcan=2ampq=18httpwwwgpopsorg

14

bull The latest advances in mesh refinement including hpndashadaptive pseudospectral methods

bull Gaussian quadrature integration methods for rapid convergence

bull Highly accurate costate estimation

bull Inclusion of the NLP solver SNOPT (for Academic Users) and IPOPT (for Not-for-Profitand Commercial Users)

bull No third-party products other than MATLAB are required

More information about the methodology used in GPOPS-II can be found by reading therelevant articles in the open literature19

The fees for using GPOPS-II are as follows

bull For students and all others employed at academic institutions NO CHARGE

bull US Federal State or Local Government NO CHARGE

bull Users at not-for-profit institutions (including non-US Government agencies) or commer-cial institutions LICENSING FEES APPLY

19httpvdolmaeufledu

15

16

Chapter 2

Nonlinear Programming Solvers

21 Introduction

A solver is a software used to compute numerical solution of an optimal control problemIn general an optimal controlnonlinear programming interface is needed to prepare all dataas input to the solver (see Figure 21) Several solvers will be presented in this chapter evolvingopenndashsource freeware and commercial software working under different operating systemsSince all solvers require configuration lists of parameters will be presented for some of them

Figure 21

22 IPOPT ndash Interior Point OPTimizer

ldquoIPOPT is a software package for largendashscale nonlinear optimisation It is designed to find(local) solutions of mathematical optimisation problemsrdquo The IPOPT library can be found inhttpwwwartelyscom1

221 Basic information

Developed by Andreas Wachter and Carl Laird

Operating system(s) IPOPT can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) IPOPT is written in C++ and is released as open source code under theEclipse Public License (EPL)

1httpwwwartelyscomindexphppage=knitroamphl=en_EN

17

Modeling language interface(s) IPOPT can be used as a library that can be linked to C++ C orFortran code as well as a solver executable for the AMPL modelling environment Thepackage includes interfaces to CUTEr optimisation testing environment as well as theMATLAB and R programming environments

222 Installing IPOPT

How to install IPOPT on Linux

Getting the IPOPT code via subversion

sudo svn co httpsprojectscoin-ororgsvnIpoptstable310 optCoinIpopt

This will create a directory named CoinIpopt in your opt folder

BLAS routines that provide standard building blocks for performing basic vector and matrixoperations

LAPACK routines for solving systems of simultaneous linear equations least-squares solutionsof linear systems of equations eigenvalue problems and singular value problems

HSL state-of-the-art packages for large-scale scientific computation

ASL AMPL Solver Library allows to read the nl files and provides the automatic differentia-tion functionality

MUMPS MUltifrontal Massively Parallel sparse direct Solver

METIS programs for partitioning graphs partitioning finite element meshes and producing fillreducing orderings for sparse matrices

To install IPOPT just run

cd optCoinIpoptThirdPartyBlas

getBlas

cd Lapack

getLapack

cd ASL

getASL

cd Mumps

getMumps

cd Metis

getMetis

IPOPT requires a sparse symmetric linear solver There are different possibilities The userrsquosguide of IPOPT suggests that you use HSL subroutines

1 Go to HSL website2 and find the package list

2 Click on MA27 read the license and submit the registration form

3 Go back to the package list

4 Click on MC19 read the license and submit the registration form

2httphslrlacukarchivehslarchivehtml

18

You will receive in your email the links to obtain the HSL packages Download them to your$HOME directory and run

gunzip ma27-100targz

tar xvf ma27-100tar

gunzip mc19-100targz

tar xvf mc19-100tar

cd optCoinIpoptThirdPartyHSL

sudo mv -r $HOMEmc19-100

sudo mv -r $HOMEma27-100

cd mc19-100

sudo configure

sudo make install

sudo make check

cd ma27-100

sudo configure

sudo make install

sudo make check

The make check command will check if the installation of the HSL packages was successfulReturn to the optCoinIpopt directory and continue the IPOPT installation

cd

sudo mkdir build

cd build

sudo configure

sudo make test

sudo make install

If you get the error message ldquoerrorlsquoNULLrsquo was not declared in this scoperdquo when run-ning sudo make then you have to edit the following filesoptCoinIpoptIpoptsrcCommonIpSmartPtrhpp

optCoinIpoptIpoptsrcAlgorithmLinearSolversIpTripletToCSRConvertercpp

by adding include ltcstddefgt

How to install IPOPT on Mac OS X

In a Terminal window install homebrewusrbinruby -e $(usrbincurl -fsSL httpsrawgithubcommxclhomebrewmaster

LibraryContributionsinstall homebrewrb)

With this software you can easily install dependency packages like wget and gfortran

brew install wget

brew install gfortran

sudo wget httpwwwcoin-ororgdownloadsourceIpoptIpopt-3102zip

sudo unzip Ipopt-3102zip

cd Ipopt-3102

Download Netlib BlasLapack

cd optCoinIpoptThirdPartyBlas

sudo getBlas

19

cd Lapack

sudo getLapack

cd ASL

sudo getASL

cd Mumps

sudo getMumps

cd Metis

sudo getMetis

Make a build for a 64bit target

sudo mkdir build64

cd build64

sudo configure --disable-shared --with-blas=BUILD --with-lapack=BUILD F77=gfortran

FFLAGS=-fexceptions -m64 -fbackslash CFLAGS=-fno-common -no-cpp-precomp -fexceptions

-arch x86 64 -m64 CXXFLAGS=-fno-common -no-cpp-precomp -fexceptions -arch x86 64

-m64

Compile test and install

make

make test

make install

Add the following to your bashrc or bash profile

IPOPTDIR=optCoinIpoptbuild

export PATH=$PATH$IPOPTDIR$IPOPTDIRbinexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyASLexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyBlaslibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyHSLlibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyLapacklibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMetislibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMumpslibs

How to install the Matlab interface on Linux

First of all be sure

cd optmatlabR2008asysosglnx86

mv libgcc sso1 libgcc sso1bck

ln -s libi386-linux-gnulibgcc sso1 libgcc sso1

mv libstdc++so608 libstdc++so608bck

ln -s usrlibi386-linux-gnulibstdc++so6 libstdc++so608

Go to the subdirectory optCoinIpoptbuildIpoptcontribMatlabInterfacesrc andopen the Makefile file

sudo apt-get install hardening-wrapper

You need to edit this file to suit the installation for your system setup You may need tomodify MATLAB HOME MEXSUFFIX and MEX as explained in the comments of the Makefile I setthese variables as follows

20

MATLAB HOME = optmatlabR2008a

MEXSUFFIX = mexglx

MEX = optmatlabR2008abinmex

In my case I removed some of the compiler options as it follows

prefix = optCoinIpopt

CXX = optintelcomposer xe 2011 sp19293binia32icpc

CCXXFLAGS = -O3 -pipe -DNDEBUG -Wparentheses -Wreturn-type -Wcast-qual -Wall

-Wpointer-arith -Wwrite-strings -Wconversion -Wno-unknown-pragmas -DMATLAB MEXFILE

LDFLAGS = $(CXXFLAGS) -Wl --rpath -Wl $prefixlibcoin -Wl --rpath -Wl

--rpath -Wl$prefixlibcoinThirdParty

In my case I had to set explicitly the include Matlab folder

o cpp

$(CXX) $(CXXFLAGS) $(INCL) -IoptmatlabR2008aexterninclude -o $ -c $^

Once you set up the Makefile properly type sudo make install in the same directory as theMakefile If the installation procedure was successful you will end up with a MEX file calledipoptmexglx In order to use it in MATLAB you need to tell MATLAB where to find it Todo this just type addpath optCoinIpoptlib in the MATLAB command window after youcheck if the ipoptmexglx file is in this folder

Documentation

More informations can be found in the following documentation

Online documentation httpwwwcoin-ororgIpoptdocumentation3

A PDF version of this documentation can be downloaded here4

A short IPOPT tutorial can be downloaded here5

MATLAB interface Instructions specific to the MATLAB interface can be found at the Matlabinterface page6

223 Getting started with IPOPT and AMPL

option solver ipopt

option ipopt options rsquooption1=value1 option2=value2rsquo

solve

3httpwwwcoin-ororgIpoptdocumentation4httpsprojectscoin-ororgIpoptbrowserstable39Ipoptdocdocumentationpdfformat=raw5httpdropsdagstuhldevolltexte20092089pdf09061WaechterAndreasPaper2089pdf6httpsprojectscoin-ororgIpoptwikiMatlabInterface

21

Table 21 IPOPT directives for AMPL

Option Description

acceptable compl inf tol Acceptance threshold for the complementarity conditionsacceptable constr viol tol Acceptance threshold for the constraint violationacceptable dual inf tol Acceptance threshold for the dual infeasibilityacceptable tol Acceptable convergence tolerance (relative)alpha for y Step size for constraint multipliersbound frac Desired minimal relative distance of initial point to boundbound mult init val Initial value for the bound multipliersbound push Desired minimal absolute distance of initial point to boundbound relax factor Factor for initial relaxation of the boundscompl inf tol Acceptance threshold for the complementarity conditionsconstr mult init max Maximal allowed least-square guess of constraint multipliersconstr viol tol Desired threshold for the constraint violationdiverging iterates tol Threshold for maximal value of primal iteratesdual inf tol Desired threshold for the dual infeasibilityexpect infeasible problem Enable heuristics to quickly detect an infeasible problemfile print level Verbosity level for output filehalt on ampl error Exit with message on evaluation errorhessian approximation Can enable Quasi-Newton approximation of hessianhonor original bounds If no solution might slightly violate boundslinear scaling on demand Enables heuristic for scaling only when seems requiredlinear solver Linear solver to be used for step calculationlinear system scaling Method for scaling the linear systemsma27 pivtol Pivot tolerance for the linear solver MA27ma27 pivtolmax Maximal pivot tolerance for the linear solver MA27ma57 pivtol Pivot tolerance for the linear solver MA57ma57 pivtolmax Maximal pivot tolerance for the linear solver MA57

22

Table 22 IPOPT directives for AMPL

Option Description

max cpu time CPU time limitmax iter Maximum number of iterations

max refinement stepsMaximal number of iterative refinement steps per linearsystem solve

max soc Maximal number of second order correction trial stepsmaxit Maximum number of iterations (same as max iter)

min refinement stepsMinimum number of iterative refinement steps per linearsystem solve

mu init Initial value for the barrier parametermu max Maximal value for barrier parameter for adaptive strategymu oracle Oracle for a new barrier parameter in the adaptive strategymu strategy Update strategy for barrier parameternlp scaling max gradient Maximum gradient after scalingnlp scaling method Select the technique used for scaling the NLPobj scaling factor Scaling factor for the objective functionoption file name File name of options file (default ipoptopt)outlev Verbosity level (same as print level)output file File name of an output file (leave unset for no file output)pardiso matching strategy Matching strategy for linear solver Pardisopardiso out of core power Enables out-of-core version of linear solver Pardisoprint level Verbosity levelprint options documentation Print all available options (for ipoptopt)print user options Toggle printing of user optionsrequired infeasibility reduction Required infeasibility reduction in restoration phaseslack bound frac Desired minimal relative distance of initial slack to boundslack bound push Desired minimal absolute distance of initial slack to boundtol Desired convergence tolerance (relative)

wantsol

solution report without -AMPL sum of1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

warm start bound pushEnables to specify how much should variables should bepushed inside the feasible region

warm start init point Enables to specify bound multiplier values

warm start mult bound pushEnables to specify how much should bound multipliersshould be pushed inside the feasible region

watchdog shortened iter trigger Trigger counter for watchdog procedurewsmp num threads Number of threads to be used in WSMPwsmp pivtol Pivot tolerance for the linear solver WSMPwsmp pivtolmax Maximum pivot tolerance for the linear solver WSMPwsmp scaling Determines how the matrix is scaled by WSMP

23

24

23 KNITRO

ldquoKNITRO is an optimization software library for finding solutions of both continuous (smo-oth) optimization models (with or without constraints) as well as discrete optimization modelswith integer or binary variables (ie mixed integer programs) KNITRO is primarily designedfor finding local optimal solutions of large-scale continuous nonlinear problemsrdquo The KNITROlibrary can be found in httpwwwartelyscom7

231 Basic information

KNITRO is a software package for solving smooth optimization problems with or withoutconstraints

Developed at Ziena Optimization

Operating system(s) KNITRO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) KNITRO is written C C++ Fortran and Java

Programming interfaces(s) Fortan CC++ Java and Microsoft Excel

Modeling language interface(s) KNITRO offers a total of five interfaces Matlab AMPL Math-ematica AIMMS GAMS and MPL

232 Installing KNITRO on Linux

How to get a license

KNITRO is developed by Ziena Optimization LLC and marketed and supported by ArtelysThe first step is to request a license by selecting the download tab8 Along with the academicand commercial trial versions there is a student version limited in problem size (300 variablesand 300 constraints) available for 6 months Each license is personalised hence among otherpersonal information you should inform about

bull the operating system (OS)

bull the OS bit architecture

bull the Machine ID of the computer on which KNITRO will runDownload extract an script available bellow the Machine ID field

After reading and accepting the End User License Agreement the download of the KNITROwill be available and you will get a license key on your email Then you should put the licensefile whose name begins with ziena lic in your $HOME directory or in the $HOMEzienadirectory

How to install

The KNITRO software package for Unix is delivered as a gzipped tar file After downloadingKNITRO you need to unpack it Type in a Terminal window

gunzip knitro-8x-platformnametargz

tar -xvf knitro-8x-platformnametar

sudo mv knitro-8x-z opt

7httpwwwartelyscomindexphppage=knitroamphl=en_EN8httpwwwartelyscomindexphppage=knitroamphl=en_ENdownloads-tab

25

This will create a directory named knitro-8x-z in your opt folder

In order to run KNITRO binary or executable files from anywhere on your Linux computerit is necessary to set several environment variables In particular you must update the PATH

environment variable so that it indicates the location of the knitroampl directory You mustalso update the LD LIBRARY PATH environment variable so that it indicates the location of theKNITRO lib directory Setting the PATH and LD LIBRARY PATH environment variables on a Linuxsystem can be done as follows

echo rsquoexport PATH=$PATHoptknitro-8x-zrsquo gtgt $HOMEbashrcecho rsquoexport PATH=$PATHoptknitro-8x-zknitroamplrsquo gtgt $HOMEbashrcecho rsquoexport LD LIBRARY PATH=$LD LIBRARY PATHoptknitro-8x-zlibrsquo gtgt $HOMEbashrc

Each Matlab release expects the KNITRO library file on Linux to have a specific name There-fore to use KNITRO 8x or later with Matlab release R2008a through 2011b you must createa symbolic link to the expected name Open a Terminal window and in the KNITRO librarydirectory issue the command

bull for Matlab releases R2008a R2008b or R2009aln -s libknitroso800 libknitroso5

bull for Matlab releases R2009b R2010a or R2010bln -s libknitroso800 libknitroso6

bull for Matlab releases R2011a R2011b or R2012aln -s libknitroso800 libknitroso7

Documentation

More informations can be found in the following documentation9

KNITRO 80 User Manual Knitro80 UserManualpdfVersion December 2011 (816 KB)

Specific KNITROMatlab interface documentation can be found on the Matlab OptimizationToolbox webpages10

233 Getting started with KNITRO and AMPL

option solver knitroampl

option knitro options rsquooption1=value1 option2=value2rsquo

solve

9httpwwwartelyscomuploadspdfsKnitro80_UserManualpdf10httpwwwmathworkscomaccesshelpdeskhelptoolboxoptim

26

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 15: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

bull The latest advances in mesh refinement including hpndashadaptive pseudospectral methods

bull Gaussian quadrature integration methods for rapid convergence

bull Highly accurate costate estimation

bull Inclusion of the NLP solver SNOPT (for Academic Users) and IPOPT (for Not-for-Profitand Commercial Users)

bull No third-party products other than MATLAB are required

More information about the methodology used in GPOPS-II can be found by reading therelevant articles in the open literature19

The fees for using GPOPS-II are as follows

bull For students and all others employed at academic institutions NO CHARGE

bull US Federal State or Local Government NO CHARGE

bull Users at not-for-profit institutions (including non-US Government agencies) or commer-cial institutions LICENSING FEES APPLY

19httpvdolmaeufledu

15

16

Chapter 2

Nonlinear Programming Solvers

21 Introduction

A solver is a software used to compute numerical solution of an optimal control problemIn general an optimal controlnonlinear programming interface is needed to prepare all dataas input to the solver (see Figure 21) Several solvers will be presented in this chapter evolvingopenndashsource freeware and commercial software working under different operating systemsSince all solvers require configuration lists of parameters will be presented for some of them

Figure 21

22 IPOPT ndash Interior Point OPTimizer

ldquoIPOPT is a software package for largendashscale nonlinear optimisation It is designed to find(local) solutions of mathematical optimisation problemsrdquo The IPOPT library can be found inhttpwwwartelyscom1

221 Basic information

Developed by Andreas Wachter and Carl Laird

Operating system(s) IPOPT can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) IPOPT is written in C++ and is released as open source code under theEclipse Public License (EPL)

1httpwwwartelyscomindexphppage=knitroamphl=en_EN

17

Modeling language interface(s) IPOPT can be used as a library that can be linked to C++ C orFortran code as well as a solver executable for the AMPL modelling environment Thepackage includes interfaces to CUTEr optimisation testing environment as well as theMATLAB and R programming environments

222 Installing IPOPT

How to install IPOPT on Linux

Getting the IPOPT code via subversion

sudo svn co httpsprojectscoin-ororgsvnIpoptstable310 optCoinIpopt

This will create a directory named CoinIpopt in your opt folder

BLAS routines that provide standard building blocks for performing basic vector and matrixoperations

LAPACK routines for solving systems of simultaneous linear equations least-squares solutionsof linear systems of equations eigenvalue problems and singular value problems

HSL state-of-the-art packages for large-scale scientific computation

ASL AMPL Solver Library allows to read the nl files and provides the automatic differentia-tion functionality

MUMPS MUltifrontal Massively Parallel sparse direct Solver

METIS programs for partitioning graphs partitioning finite element meshes and producing fillreducing orderings for sparse matrices

To install IPOPT just run

cd optCoinIpoptThirdPartyBlas

getBlas

cd Lapack

getLapack

cd ASL

getASL

cd Mumps

getMumps

cd Metis

getMetis

IPOPT requires a sparse symmetric linear solver There are different possibilities The userrsquosguide of IPOPT suggests that you use HSL subroutines

1 Go to HSL website2 and find the package list

2 Click on MA27 read the license and submit the registration form

3 Go back to the package list

4 Click on MC19 read the license and submit the registration form

2httphslrlacukarchivehslarchivehtml

18

You will receive in your email the links to obtain the HSL packages Download them to your$HOME directory and run

gunzip ma27-100targz

tar xvf ma27-100tar

gunzip mc19-100targz

tar xvf mc19-100tar

cd optCoinIpoptThirdPartyHSL

sudo mv -r $HOMEmc19-100

sudo mv -r $HOMEma27-100

cd mc19-100

sudo configure

sudo make install

sudo make check

cd ma27-100

sudo configure

sudo make install

sudo make check

The make check command will check if the installation of the HSL packages was successfulReturn to the optCoinIpopt directory and continue the IPOPT installation

cd

sudo mkdir build

cd build

sudo configure

sudo make test

sudo make install

If you get the error message ldquoerrorlsquoNULLrsquo was not declared in this scoperdquo when run-ning sudo make then you have to edit the following filesoptCoinIpoptIpoptsrcCommonIpSmartPtrhpp

optCoinIpoptIpoptsrcAlgorithmLinearSolversIpTripletToCSRConvertercpp

by adding include ltcstddefgt

How to install IPOPT on Mac OS X

In a Terminal window install homebrewusrbinruby -e $(usrbincurl -fsSL httpsrawgithubcommxclhomebrewmaster

LibraryContributionsinstall homebrewrb)

With this software you can easily install dependency packages like wget and gfortran

brew install wget

brew install gfortran

sudo wget httpwwwcoin-ororgdownloadsourceIpoptIpopt-3102zip

sudo unzip Ipopt-3102zip

cd Ipopt-3102

Download Netlib BlasLapack

cd optCoinIpoptThirdPartyBlas

sudo getBlas

19

cd Lapack

sudo getLapack

cd ASL

sudo getASL

cd Mumps

sudo getMumps

cd Metis

sudo getMetis

Make a build for a 64bit target

sudo mkdir build64

cd build64

sudo configure --disable-shared --with-blas=BUILD --with-lapack=BUILD F77=gfortran

FFLAGS=-fexceptions -m64 -fbackslash CFLAGS=-fno-common -no-cpp-precomp -fexceptions

-arch x86 64 -m64 CXXFLAGS=-fno-common -no-cpp-precomp -fexceptions -arch x86 64

-m64

Compile test and install

make

make test

make install

Add the following to your bashrc or bash profile

IPOPTDIR=optCoinIpoptbuild

export PATH=$PATH$IPOPTDIR$IPOPTDIRbinexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyASLexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyBlaslibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyHSLlibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyLapacklibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMetislibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMumpslibs

How to install the Matlab interface on Linux

First of all be sure

cd optmatlabR2008asysosglnx86

mv libgcc sso1 libgcc sso1bck

ln -s libi386-linux-gnulibgcc sso1 libgcc sso1

mv libstdc++so608 libstdc++so608bck

ln -s usrlibi386-linux-gnulibstdc++so6 libstdc++so608

Go to the subdirectory optCoinIpoptbuildIpoptcontribMatlabInterfacesrc andopen the Makefile file

sudo apt-get install hardening-wrapper

You need to edit this file to suit the installation for your system setup You may need tomodify MATLAB HOME MEXSUFFIX and MEX as explained in the comments of the Makefile I setthese variables as follows

20

MATLAB HOME = optmatlabR2008a

MEXSUFFIX = mexglx

MEX = optmatlabR2008abinmex

In my case I removed some of the compiler options as it follows

prefix = optCoinIpopt

CXX = optintelcomposer xe 2011 sp19293binia32icpc

CCXXFLAGS = -O3 -pipe -DNDEBUG -Wparentheses -Wreturn-type -Wcast-qual -Wall

-Wpointer-arith -Wwrite-strings -Wconversion -Wno-unknown-pragmas -DMATLAB MEXFILE

LDFLAGS = $(CXXFLAGS) -Wl --rpath -Wl $prefixlibcoin -Wl --rpath -Wl

--rpath -Wl$prefixlibcoinThirdParty

In my case I had to set explicitly the include Matlab folder

o cpp

$(CXX) $(CXXFLAGS) $(INCL) -IoptmatlabR2008aexterninclude -o $ -c $^

Once you set up the Makefile properly type sudo make install in the same directory as theMakefile If the installation procedure was successful you will end up with a MEX file calledipoptmexglx In order to use it in MATLAB you need to tell MATLAB where to find it Todo this just type addpath optCoinIpoptlib in the MATLAB command window after youcheck if the ipoptmexglx file is in this folder

Documentation

More informations can be found in the following documentation

Online documentation httpwwwcoin-ororgIpoptdocumentation3

A PDF version of this documentation can be downloaded here4

A short IPOPT tutorial can be downloaded here5

MATLAB interface Instructions specific to the MATLAB interface can be found at the Matlabinterface page6

223 Getting started with IPOPT and AMPL

option solver ipopt

option ipopt options rsquooption1=value1 option2=value2rsquo

solve

3httpwwwcoin-ororgIpoptdocumentation4httpsprojectscoin-ororgIpoptbrowserstable39Ipoptdocdocumentationpdfformat=raw5httpdropsdagstuhldevolltexte20092089pdf09061WaechterAndreasPaper2089pdf6httpsprojectscoin-ororgIpoptwikiMatlabInterface

21

Table 21 IPOPT directives for AMPL

Option Description

acceptable compl inf tol Acceptance threshold for the complementarity conditionsacceptable constr viol tol Acceptance threshold for the constraint violationacceptable dual inf tol Acceptance threshold for the dual infeasibilityacceptable tol Acceptable convergence tolerance (relative)alpha for y Step size for constraint multipliersbound frac Desired minimal relative distance of initial point to boundbound mult init val Initial value for the bound multipliersbound push Desired minimal absolute distance of initial point to boundbound relax factor Factor for initial relaxation of the boundscompl inf tol Acceptance threshold for the complementarity conditionsconstr mult init max Maximal allowed least-square guess of constraint multipliersconstr viol tol Desired threshold for the constraint violationdiverging iterates tol Threshold for maximal value of primal iteratesdual inf tol Desired threshold for the dual infeasibilityexpect infeasible problem Enable heuristics to quickly detect an infeasible problemfile print level Verbosity level for output filehalt on ampl error Exit with message on evaluation errorhessian approximation Can enable Quasi-Newton approximation of hessianhonor original bounds If no solution might slightly violate boundslinear scaling on demand Enables heuristic for scaling only when seems requiredlinear solver Linear solver to be used for step calculationlinear system scaling Method for scaling the linear systemsma27 pivtol Pivot tolerance for the linear solver MA27ma27 pivtolmax Maximal pivot tolerance for the linear solver MA27ma57 pivtol Pivot tolerance for the linear solver MA57ma57 pivtolmax Maximal pivot tolerance for the linear solver MA57

22

Table 22 IPOPT directives for AMPL

Option Description

max cpu time CPU time limitmax iter Maximum number of iterations

max refinement stepsMaximal number of iterative refinement steps per linearsystem solve

max soc Maximal number of second order correction trial stepsmaxit Maximum number of iterations (same as max iter)

min refinement stepsMinimum number of iterative refinement steps per linearsystem solve

mu init Initial value for the barrier parametermu max Maximal value for barrier parameter for adaptive strategymu oracle Oracle for a new barrier parameter in the adaptive strategymu strategy Update strategy for barrier parameternlp scaling max gradient Maximum gradient after scalingnlp scaling method Select the technique used for scaling the NLPobj scaling factor Scaling factor for the objective functionoption file name File name of options file (default ipoptopt)outlev Verbosity level (same as print level)output file File name of an output file (leave unset for no file output)pardiso matching strategy Matching strategy for linear solver Pardisopardiso out of core power Enables out-of-core version of linear solver Pardisoprint level Verbosity levelprint options documentation Print all available options (for ipoptopt)print user options Toggle printing of user optionsrequired infeasibility reduction Required infeasibility reduction in restoration phaseslack bound frac Desired minimal relative distance of initial slack to boundslack bound push Desired minimal absolute distance of initial slack to boundtol Desired convergence tolerance (relative)

wantsol

solution report without -AMPL sum of1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

warm start bound pushEnables to specify how much should variables should bepushed inside the feasible region

warm start init point Enables to specify bound multiplier values

warm start mult bound pushEnables to specify how much should bound multipliersshould be pushed inside the feasible region

watchdog shortened iter trigger Trigger counter for watchdog procedurewsmp num threads Number of threads to be used in WSMPwsmp pivtol Pivot tolerance for the linear solver WSMPwsmp pivtolmax Maximum pivot tolerance for the linear solver WSMPwsmp scaling Determines how the matrix is scaled by WSMP

23

24

23 KNITRO

ldquoKNITRO is an optimization software library for finding solutions of both continuous (smo-oth) optimization models (with or without constraints) as well as discrete optimization modelswith integer or binary variables (ie mixed integer programs) KNITRO is primarily designedfor finding local optimal solutions of large-scale continuous nonlinear problemsrdquo The KNITROlibrary can be found in httpwwwartelyscom7

231 Basic information

KNITRO is a software package for solving smooth optimization problems with or withoutconstraints

Developed at Ziena Optimization

Operating system(s) KNITRO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) KNITRO is written C C++ Fortran and Java

Programming interfaces(s) Fortan CC++ Java and Microsoft Excel

Modeling language interface(s) KNITRO offers a total of five interfaces Matlab AMPL Math-ematica AIMMS GAMS and MPL

232 Installing KNITRO on Linux

How to get a license

KNITRO is developed by Ziena Optimization LLC and marketed and supported by ArtelysThe first step is to request a license by selecting the download tab8 Along with the academicand commercial trial versions there is a student version limited in problem size (300 variablesand 300 constraints) available for 6 months Each license is personalised hence among otherpersonal information you should inform about

bull the operating system (OS)

bull the OS bit architecture

bull the Machine ID of the computer on which KNITRO will runDownload extract an script available bellow the Machine ID field

After reading and accepting the End User License Agreement the download of the KNITROwill be available and you will get a license key on your email Then you should put the licensefile whose name begins with ziena lic in your $HOME directory or in the $HOMEzienadirectory

How to install

The KNITRO software package for Unix is delivered as a gzipped tar file After downloadingKNITRO you need to unpack it Type in a Terminal window

gunzip knitro-8x-platformnametargz

tar -xvf knitro-8x-platformnametar

sudo mv knitro-8x-z opt

7httpwwwartelyscomindexphppage=knitroamphl=en_EN8httpwwwartelyscomindexphppage=knitroamphl=en_ENdownloads-tab

25

This will create a directory named knitro-8x-z in your opt folder

In order to run KNITRO binary or executable files from anywhere on your Linux computerit is necessary to set several environment variables In particular you must update the PATH

environment variable so that it indicates the location of the knitroampl directory You mustalso update the LD LIBRARY PATH environment variable so that it indicates the location of theKNITRO lib directory Setting the PATH and LD LIBRARY PATH environment variables on a Linuxsystem can be done as follows

echo rsquoexport PATH=$PATHoptknitro-8x-zrsquo gtgt $HOMEbashrcecho rsquoexport PATH=$PATHoptknitro-8x-zknitroamplrsquo gtgt $HOMEbashrcecho rsquoexport LD LIBRARY PATH=$LD LIBRARY PATHoptknitro-8x-zlibrsquo gtgt $HOMEbashrc

Each Matlab release expects the KNITRO library file on Linux to have a specific name There-fore to use KNITRO 8x or later with Matlab release R2008a through 2011b you must createa symbolic link to the expected name Open a Terminal window and in the KNITRO librarydirectory issue the command

bull for Matlab releases R2008a R2008b or R2009aln -s libknitroso800 libknitroso5

bull for Matlab releases R2009b R2010a or R2010bln -s libknitroso800 libknitroso6

bull for Matlab releases R2011a R2011b or R2012aln -s libknitroso800 libknitroso7

Documentation

More informations can be found in the following documentation9

KNITRO 80 User Manual Knitro80 UserManualpdfVersion December 2011 (816 KB)

Specific KNITROMatlab interface documentation can be found on the Matlab OptimizationToolbox webpages10

233 Getting started with KNITRO and AMPL

option solver knitroampl

option knitro options rsquooption1=value1 option2=value2rsquo

solve

9httpwwwartelyscomuploadspdfsKnitro80_UserManualpdf10httpwwwmathworkscomaccesshelpdeskhelptoolboxoptim

26

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 16: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

16

Chapter 2

Nonlinear Programming Solvers

21 Introduction

A solver is a software used to compute numerical solution of an optimal control problemIn general an optimal controlnonlinear programming interface is needed to prepare all dataas input to the solver (see Figure 21) Several solvers will be presented in this chapter evolvingopenndashsource freeware and commercial software working under different operating systemsSince all solvers require configuration lists of parameters will be presented for some of them

Figure 21

22 IPOPT ndash Interior Point OPTimizer

ldquoIPOPT is a software package for largendashscale nonlinear optimisation It is designed to find(local) solutions of mathematical optimisation problemsrdquo The IPOPT library can be found inhttpwwwartelyscom1

221 Basic information

Developed by Andreas Wachter and Carl Laird

Operating system(s) IPOPT can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) IPOPT is written in C++ and is released as open source code under theEclipse Public License (EPL)

1httpwwwartelyscomindexphppage=knitroamphl=en_EN

17

Modeling language interface(s) IPOPT can be used as a library that can be linked to C++ C orFortran code as well as a solver executable for the AMPL modelling environment Thepackage includes interfaces to CUTEr optimisation testing environment as well as theMATLAB and R programming environments

222 Installing IPOPT

How to install IPOPT on Linux

Getting the IPOPT code via subversion

sudo svn co httpsprojectscoin-ororgsvnIpoptstable310 optCoinIpopt

This will create a directory named CoinIpopt in your opt folder

BLAS routines that provide standard building blocks for performing basic vector and matrixoperations

LAPACK routines for solving systems of simultaneous linear equations least-squares solutionsof linear systems of equations eigenvalue problems and singular value problems

HSL state-of-the-art packages for large-scale scientific computation

ASL AMPL Solver Library allows to read the nl files and provides the automatic differentia-tion functionality

MUMPS MUltifrontal Massively Parallel sparse direct Solver

METIS programs for partitioning graphs partitioning finite element meshes and producing fillreducing orderings for sparse matrices

To install IPOPT just run

cd optCoinIpoptThirdPartyBlas

getBlas

cd Lapack

getLapack

cd ASL

getASL

cd Mumps

getMumps

cd Metis

getMetis

IPOPT requires a sparse symmetric linear solver There are different possibilities The userrsquosguide of IPOPT suggests that you use HSL subroutines

1 Go to HSL website2 and find the package list

2 Click on MA27 read the license and submit the registration form

3 Go back to the package list

4 Click on MC19 read the license and submit the registration form

2httphslrlacukarchivehslarchivehtml

18

You will receive in your email the links to obtain the HSL packages Download them to your$HOME directory and run

gunzip ma27-100targz

tar xvf ma27-100tar

gunzip mc19-100targz

tar xvf mc19-100tar

cd optCoinIpoptThirdPartyHSL

sudo mv -r $HOMEmc19-100

sudo mv -r $HOMEma27-100

cd mc19-100

sudo configure

sudo make install

sudo make check

cd ma27-100

sudo configure

sudo make install

sudo make check

The make check command will check if the installation of the HSL packages was successfulReturn to the optCoinIpopt directory and continue the IPOPT installation

cd

sudo mkdir build

cd build

sudo configure

sudo make test

sudo make install

If you get the error message ldquoerrorlsquoNULLrsquo was not declared in this scoperdquo when run-ning sudo make then you have to edit the following filesoptCoinIpoptIpoptsrcCommonIpSmartPtrhpp

optCoinIpoptIpoptsrcAlgorithmLinearSolversIpTripletToCSRConvertercpp

by adding include ltcstddefgt

How to install IPOPT on Mac OS X

In a Terminal window install homebrewusrbinruby -e $(usrbincurl -fsSL httpsrawgithubcommxclhomebrewmaster

LibraryContributionsinstall homebrewrb)

With this software you can easily install dependency packages like wget and gfortran

brew install wget

brew install gfortran

sudo wget httpwwwcoin-ororgdownloadsourceIpoptIpopt-3102zip

sudo unzip Ipopt-3102zip

cd Ipopt-3102

Download Netlib BlasLapack

cd optCoinIpoptThirdPartyBlas

sudo getBlas

19

cd Lapack

sudo getLapack

cd ASL

sudo getASL

cd Mumps

sudo getMumps

cd Metis

sudo getMetis

Make a build for a 64bit target

sudo mkdir build64

cd build64

sudo configure --disable-shared --with-blas=BUILD --with-lapack=BUILD F77=gfortran

FFLAGS=-fexceptions -m64 -fbackslash CFLAGS=-fno-common -no-cpp-precomp -fexceptions

-arch x86 64 -m64 CXXFLAGS=-fno-common -no-cpp-precomp -fexceptions -arch x86 64

-m64

Compile test and install

make

make test

make install

Add the following to your bashrc or bash profile

IPOPTDIR=optCoinIpoptbuild

export PATH=$PATH$IPOPTDIR$IPOPTDIRbinexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyASLexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyBlaslibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyHSLlibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyLapacklibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMetislibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMumpslibs

How to install the Matlab interface on Linux

First of all be sure

cd optmatlabR2008asysosglnx86

mv libgcc sso1 libgcc sso1bck

ln -s libi386-linux-gnulibgcc sso1 libgcc sso1

mv libstdc++so608 libstdc++so608bck

ln -s usrlibi386-linux-gnulibstdc++so6 libstdc++so608

Go to the subdirectory optCoinIpoptbuildIpoptcontribMatlabInterfacesrc andopen the Makefile file

sudo apt-get install hardening-wrapper

You need to edit this file to suit the installation for your system setup You may need tomodify MATLAB HOME MEXSUFFIX and MEX as explained in the comments of the Makefile I setthese variables as follows

20

MATLAB HOME = optmatlabR2008a

MEXSUFFIX = mexglx

MEX = optmatlabR2008abinmex

In my case I removed some of the compiler options as it follows

prefix = optCoinIpopt

CXX = optintelcomposer xe 2011 sp19293binia32icpc

CCXXFLAGS = -O3 -pipe -DNDEBUG -Wparentheses -Wreturn-type -Wcast-qual -Wall

-Wpointer-arith -Wwrite-strings -Wconversion -Wno-unknown-pragmas -DMATLAB MEXFILE

LDFLAGS = $(CXXFLAGS) -Wl --rpath -Wl $prefixlibcoin -Wl --rpath -Wl

--rpath -Wl$prefixlibcoinThirdParty

In my case I had to set explicitly the include Matlab folder

o cpp

$(CXX) $(CXXFLAGS) $(INCL) -IoptmatlabR2008aexterninclude -o $ -c $^

Once you set up the Makefile properly type sudo make install in the same directory as theMakefile If the installation procedure was successful you will end up with a MEX file calledipoptmexglx In order to use it in MATLAB you need to tell MATLAB where to find it Todo this just type addpath optCoinIpoptlib in the MATLAB command window after youcheck if the ipoptmexglx file is in this folder

Documentation

More informations can be found in the following documentation

Online documentation httpwwwcoin-ororgIpoptdocumentation3

A PDF version of this documentation can be downloaded here4

A short IPOPT tutorial can be downloaded here5

MATLAB interface Instructions specific to the MATLAB interface can be found at the Matlabinterface page6

223 Getting started with IPOPT and AMPL

option solver ipopt

option ipopt options rsquooption1=value1 option2=value2rsquo

solve

3httpwwwcoin-ororgIpoptdocumentation4httpsprojectscoin-ororgIpoptbrowserstable39Ipoptdocdocumentationpdfformat=raw5httpdropsdagstuhldevolltexte20092089pdf09061WaechterAndreasPaper2089pdf6httpsprojectscoin-ororgIpoptwikiMatlabInterface

21

Table 21 IPOPT directives for AMPL

Option Description

acceptable compl inf tol Acceptance threshold for the complementarity conditionsacceptable constr viol tol Acceptance threshold for the constraint violationacceptable dual inf tol Acceptance threshold for the dual infeasibilityacceptable tol Acceptable convergence tolerance (relative)alpha for y Step size for constraint multipliersbound frac Desired minimal relative distance of initial point to boundbound mult init val Initial value for the bound multipliersbound push Desired minimal absolute distance of initial point to boundbound relax factor Factor for initial relaxation of the boundscompl inf tol Acceptance threshold for the complementarity conditionsconstr mult init max Maximal allowed least-square guess of constraint multipliersconstr viol tol Desired threshold for the constraint violationdiverging iterates tol Threshold for maximal value of primal iteratesdual inf tol Desired threshold for the dual infeasibilityexpect infeasible problem Enable heuristics to quickly detect an infeasible problemfile print level Verbosity level for output filehalt on ampl error Exit with message on evaluation errorhessian approximation Can enable Quasi-Newton approximation of hessianhonor original bounds If no solution might slightly violate boundslinear scaling on demand Enables heuristic for scaling only when seems requiredlinear solver Linear solver to be used for step calculationlinear system scaling Method for scaling the linear systemsma27 pivtol Pivot tolerance for the linear solver MA27ma27 pivtolmax Maximal pivot tolerance for the linear solver MA27ma57 pivtol Pivot tolerance for the linear solver MA57ma57 pivtolmax Maximal pivot tolerance for the linear solver MA57

22

Table 22 IPOPT directives for AMPL

Option Description

max cpu time CPU time limitmax iter Maximum number of iterations

max refinement stepsMaximal number of iterative refinement steps per linearsystem solve

max soc Maximal number of second order correction trial stepsmaxit Maximum number of iterations (same as max iter)

min refinement stepsMinimum number of iterative refinement steps per linearsystem solve

mu init Initial value for the barrier parametermu max Maximal value for barrier parameter for adaptive strategymu oracle Oracle for a new barrier parameter in the adaptive strategymu strategy Update strategy for barrier parameternlp scaling max gradient Maximum gradient after scalingnlp scaling method Select the technique used for scaling the NLPobj scaling factor Scaling factor for the objective functionoption file name File name of options file (default ipoptopt)outlev Verbosity level (same as print level)output file File name of an output file (leave unset for no file output)pardiso matching strategy Matching strategy for linear solver Pardisopardiso out of core power Enables out-of-core version of linear solver Pardisoprint level Verbosity levelprint options documentation Print all available options (for ipoptopt)print user options Toggle printing of user optionsrequired infeasibility reduction Required infeasibility reduction in restoration phaseslack bound frac Desired minimal relative distance of initial slack to boundslack bound push Desired minimal absolute distance of initial slack to boundtol Desired convergence tolerance (relative)

wantsol

solution report without -AMPL sum of1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

warm start bound pushEnables to specify how much should variables should bepushed inside the feasible region

warm start init point Enables to specify bound multiplier values

warm start mult bound pushEnables to specify how much should bound multipliersshould be pushed inside the feasible region

watchdog shortened iter trigger Trigger counter for watchdog procedurewsmp num threads Number of threads to be used in WSMPwsmp pivtol Pivot tolerance for the linear solver WSMPwsmp pivtolmax Maximum pivot tolerance for the linear solver WSMPwsmp scaling Determines how the matrix is scaled by WSMP

23

24

23 KNITRO

ldquoKNITRO is an optimization software library for finding solutions of both continuous (smo-oth) optimization models (with or without constraints) as well as discrete optimization modelswith integer or binary variables (ie mixed integer programs) KNITRO is primarily designedfor finding local optimal solutions of large-scale continuous nonlinear problemsrdquo The KNITROlibrary can be found in httpwwwartelyscom7

231 Basic information

KNITRO is a software package for solving smooth optimization problems with or withoutconstraints

Developed at Ziena Optimization

Operating system(s) KNITRO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) KNITRO is written C C++ Fortran and Java

Programming interfaces(s) Fortan CC++ Java and Microsoft Excel

Modeling language interface(s) KNITRO offers a total of five interfaces Matlab AMPL Math-ematica AIMMS GAMS and MPL

232 Installing KNITRO on Linux

How to get a license

KNITRO is developed by Ziena Optimization LLC and marketed and supported by ArtelysThe first step is to request a license by selecting the download tab8 Along with the academicand commercial trial versions there is a student version limited in problem size (300 variablesand 300 constraints) available for 6 months Each license is personalised hence among otherpersonal information you should inform about

bull the operating system (OS)

bull the OS bit architecture

bull the Machine ID of the computer on which KNITRO will runDownload extract an script available bellow the Machine ID field

After reading and accepting the End User License Agreement the download of the KNITROwill be available and you will get a license key on your email Then you should put the licensefile whose name begins with ziena lic in your $HOME directory or in the $HOMEzienadirectory

How to install

The KNITRO software package for Unix is delivered as a gzipped tar file After downloadingKNITRO you need to unpack it Type in a Terminal window

gunzip knitro-8x-platformnametargz

tar -xvf knitro-8x-platformnametar

sudo mv knitro-8x-z opt

7httpwwwartelyscomindexphppage=knitroamphl=en_EN8httpwwwartelyscomindexphppage=knitroamphl=en_ENdownloads-tab

25

This will create a directory named knitro-8x-z in your opt folder

In order to run KNITRO binary or executable files from anywhere on your Linux computerit is necessary to set several environment variables In particular you must update the PATH

environment variable so that it indicates the location of the knitroampl directory You mustalso update the LD LIBRARY PATH environment variable so that it indicates the location of theKNITRO lib directory Setting the PATH and LD LIBRARY PATH environment variables on a Linuxsystem can be done as follows

echo rsquoexport PATH=$PATHoptknitro-8x-zrsquo gtgt $HOMEbashrcecho rsquoexport PATH=$PATHoptknitro-8x-zknitroamplrsquo gtgt $HOMEbashrcecho rsquoexport LD LIBRARY PATH=$LD LIBRARY PATHoptknitro-8x-zlibrsquo gtgt $HOMEbashrc

Each Matlab release expects the KNITRO library file on Linux to have a specific name There-fore to use KNITRO 8x or later with Matlab release R2008a through 2011b you must createa symbolic link to the expected name Open a Terminal window and in the KNITRO librarydirectory issue the command

bull for Matlab releases R2008a R2008b or R2009aln -s libknitroso800 libknitroso5

bull for Matlab releases R2009b R2010a or R2010bln -s libknitroso800 libknitroso6

bull for Matlab releases R2011a R2011b or R2012aln -s libknitroso800 libknitroso7

Documentation

More informations can be found in the following documentation9

KNITRO 80 User Manual Knitro80 UserManualpdfVersion December 2011 (816 KB)

Specific KNITROMatlab interface documentation can be found on the Matlab OptimizationToolbox webpages10

233 Getting started with KNITRO and AMPL

option solver knitroampl

option knitro options rsquooption1=value1 option2=value2rsquo

solve

9httpwwwartelyscomuploadspdfsKnitro80_UserManualpdf10httpwwwmathworkscomaccesshelpdeskhelptoolboxoptim

26

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 17: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

Chapter 2

Nonlinear Programming Solvers

21 Introduction

A solver is a software used to compute numerical solution of an optimal control problemIn general an optimal controlnonlinear programming interface is needed to prepare all dataas input to the solver (see Figure 21) Several solvers will be presented in this chapter evolvingopenndashsource freeware and commercial software working under different operating systemsSince all solvers require configuration lists of parameters will be presented for some of them

Figure 21

22 IPOPT ndash Interior Point OPTimizer

ldquoIPOPT is a software package for largendashscale nonlinear optimisation It is designed to find(local) solutions of mathematical optimisation problemsrdquo The IPOPT library can be found inhttpwwwartelyscom1

221 Basic information

Developed by Andreas Wachter and Carl Laird

Operating system(s) IPOPT can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) IPOPT is written in C++ and is released as open source code under theEclipse Public License (EPL)

1httpwwwartelyscomindexphppage=knitroamphl=en_EN

17

Modeling language interface(s) IPOPT can be used as a library that can be linked to C++ C orFortran code as well as a solver executable for the AMPL modelling environment Thepackage includes interfaces to CUTEr optimisation testing environment as well as theMATLAB and R programming environments

222 Installing IPOPT

How to install IPOPT on Linux

Getting the IPOPT code via subversion

sudo svn co httpsprojectscoin-ororgsvnIpoptstable310 optCoinIpopt

This will create a directory named CoinIpopt in your opt folder

BLAS routines that provide standard building blocks for performing basic vector and matrixoperations

LAPACK routines for solving systems of simultaneous linear equations least-squares solutionsof linear systems of equations eigenvalue problems and singular value problems

HSL state-of-the-art packages for large-scale scientific computation

ASL AMPL Solver Library allows to read the nl files and provides the automatic differentia-tion functionality

MUMPS MUltifrontal Massively Parallel sparse direct Solver

METIS programs for partitioning graphs partitioning finite element meshes and producing fillreducing orderings for sparse matrices

To install IPOPT just run

cd optCoinIpoptThirdPartyBlas

getBlas

cd Lapack

getLapack

cd ASL

getASL

cd Mumps

getMumps

cd Metis

getMetis

IPOPT requires a sparse symmetric linear solver There are different possibilities The userrsquosguide of IPOPT suggests that you use HSL subroutines

1 Go to HSL website2 and find the package list

2 Click on MA27 read the license and submit the registration form

3 Go back to the package list

4 Click on MC19 read the license and submit the registration form

2httphslrlacukarchivehslarchivehtml

18

You will receive in your email the links to obtain the HSL packages Download them to your$HOME directory and run

gunzip ma27-100targz

tar xvf ma27-100tar

gunzip mc19-100targz

tar xvf mc19-100tar

cd optCoinIpoptThirdPartyHSL

sudo mv -r $HOMEmc19-100

sudo mv -r $HOMEma27-100

cd mc19-100

sudo configure

sudo make install

sudo make check

cd ma27-100

sudo configure

sudo make install

sudo make check

The make check command will check if the installation of the HSL packages was successfulReturn to the optCoinIpopt directory and continue the IPOPT installation

cd

sudo mkdir build

cd build

sudo configure

sudo make test

sudo make install

If you get the error message ldquoerrorlsquoNULLrsquo was not declared in this scoperdquo when run-ning sudo make then you have to edit the following filesoptCoinIpoptIpoptsrcCommonIpSmartPtrhpp

optCoinIpoptIpoptsrcAlgorithmLinearSolversIpTripletToCSRConvertercpp

by adding include ltcstddefgt

How to install IPOPT on Mac OS X

In a Terminal window install homebrewusrbinruby -e $(usrbincurl -fsSL httpsrawgithubcommxclhomebrewmaster

LibraryContributionsinstall homebrewrb)

With this software you can easily install dependency packages like wget and gfortran

brew install wget

brew install gfortran

sudo wget httpwwwcoin-ororgdownloadsourceIpoptIpopt-3102zip

sudo unzip Ipopt-3102zip

cd Ipopt-3102

Download Netlib BlasLapack

cd optCoinIpoptThirdPartyBlas

sudo getBlas

19

cd Lapack

sudo getLapack

cd ASL

sudo getASL

cd Mumps

sudo getMumps

cd Metis

sudo getMetis

Make a build for a 64bit target

sudo mkdir build64

cd build64

sudo configure --disable-shared --with-blas=BUILD --with-lapack=BUILD F77=gfortran

FFLAGS=-fexceptions -m64 -fbackslash CFLAGS=-fno-common -no-cpp-precomp -fexceptions

-arch x86 64 -m64 CXXFLAGS=-fno-common -no-cpp-precomp -fexceptions -arch x86 64

-m64

Compile test and install

make

make test

make install

Add the following to your bashrc or bash profile

IPOPTDIR=optCoinIpoptbuild

export PATH=$PATH$IPOPTDIR$IPOPTDIRbinexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyASLexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyBlaslibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyHSLlibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyLapacklibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMetislibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMumpslibs

How to install the Matlab interface on Linux

First of all be sure

cd optmatlabR2008asysosglnx86

mv libgcc sso1 libgcc sso1bck

ln -s libi386-linux-gnulibgcc sso1 libgcc sso1

mv libstdc++so608 libstdc++so608bck

ln -s usrlibi386-linux-gnulibstdc++so6 libstdc++so608

Go to the subdirectory optCoinIpoptbuildIpoptcontribMatlabInterfacesrc andopen the Makefile file

sudo apt-get install hardening-wrapper

You need to edit this file to suit the installation for your system setup You may need tomodify MATLAB HOME MEXSUFFIX and MEX as explained in the comments of the Makefile I setthese variables as follows

20

MATLAB HOME = optmatlabR2008a

MEXSUFFIX = mexglx

MEX = optmatlabR2008abinmex

In my case I removed some of the compiler options as it follows

prefix = optCoinIpopt

CXX = optintelcomposer xe 2011 sp19293binia32icpc

CCXXFLAGS = -O3 -pipe -DNDEBUG -Wparentheses -Wreturn-type -Wcast-qual -Wall

-Wpointer-arith -Wwrite-strings -Wconversion -Wno-unknown-pragmas -DMATLAB MEXFILE

LDFLAGS = $(CXXFLAGS) -Wl --rpath -Wl $prefixlibcoin -Wl --rpath -Wl

--rpath -Wl$prefixlibcoinThirdParty

In my case I had to set explicitly the include Matlab folder

o cpp

$(CXX) $(CXXFLAGS) $(INCL) -IoptmatlabR2008aexterninclude -o $ -c $^

Once you set up the Makefile properly type sudo make install in the same directory as theMakefile If the installation procedure was successful you will end up with a MEX file calledipoptmexglx In order to use it in MATLAB you need to tell MATLAB where to find it Todo this just type addpath optCoinIpoptlib in the MATLAB command window after youcheck if the ipoptmexglx file is in this folder

Documentation

More informations can be found in the following documentation

Online documentation httpwwwcoin-ororgIpoptdocumentation3

A PDF version of this documentation can be downloaded here4

A short IPOPT tutorial can be downloaded here5

MATLAB interface Instructions specific to the MATLAB interface can be found at the Matlabinterface page6

223 Getting started with IPOPT and AMPL

option solver ipopt

option ipopt options rsquooption1=value1 option2=value2rsquo

solve

3httpwwwcoin-ororgIpoptdocumentation4httpsprojectscoin-ororgIpoptbrowserstable39Ipoptdocdocumentationpdfformat=raw5httpdropsdagstuhldevolltexte20092089pdf09061WaechterAndreasPaper2089pdf6httpsprojectscoin-ororgIpoptwikiMatlabInterface

21

Table 21 IPOPT directives for AMPL

Option Description

acceptable compl inf tol Acceptance threshold for the complementarity conditionsacceptable constr viol tol Acceptance threshold for the constraint violationacceptable dual inf tol Acceptance threshold for the dual infeasibilityacceptable tol Acceptable convergence tolerance (relative)alpha for y Step size for constraint multipliersbound frac Desired minimal relative distance of initial point to boundbound mult init val Initial value for the bound multipliersbound push Desired minimal absolute distance of initial point to boundbound relax factor Factor for initial relaxation of the boundscompl inf tol Acceptance threshold for the complementarity conditionsconstr mult init max Maximal allowed least-square guess of constraint multipliersconstr viol tol Desired threshold for the constraint violationdiverging iterates tol Threshold for maximal value of primal iteratesdual inf tol Desired threshold for the dual infeasibilityexpect infeasible problem Enable heuristics to quickly detect an infeasible problemfile print level Verbosity level for output filehalt on ampl error Exit with message on evaluation errorhessian approximation Can enable Quasi-Newton approximation of hessianhonor original bounds If no solution might slightly violate boundslinear scaling on demand Enables heuristic for scaling only when seems requiredlinear solver Linear solver to be used for step calculationlinear system scaling Method for scaling the linear systemsma27 pivtol Pivot tolerance for the linear solver MA27ma27 pivtolmax Maximal pivot tolerance for the linear solver MA27ma57 pivtol Pivot tolerance for the linear solver MA57ma57 pivtolmax Maximal pivot tolerance for the linear solver MA57

22

Table 22 IPOPT directives for AMPL

Option Description

max cpu time CPU time limitmax iter Maximum number of iterations

max refinement stepsMaximal number of iterative refinement steps per linearsystem solve

max soc Maximal number of second order correction trial stepsmaxit Maximum number of iterations (same as max iter)

min refinement stepsMinimum number of iterative refinement steps per linearsystem solve

mu init Initial value for the barrier parametermu max Maximal value for barrier parameter for adaptive strategymu oracle Oracle for a new barrier parameter in the adaptive strategymu strategy Update strategy for barrier parameternlp scaling max gradient Maximum gradient after scalingnlp scaling method Select the technique used for scaling the NLPobj scaling factor Scaling factor for the objective functionoption file name File name of options file (default ipoptopt)outlev Verbosity level (same as print level)output file File name of an output file (leave unset for no file output)pardiso matching strategy Matching strategy for linear solver Pardisopardiso out of core power Enables out-of-core version of linear solver Pardisoprint level Verbosity levelprint options documentation Print all available options (for ipoptopt)print user options Toggle printing of user optionsrequired infeasibility reduction Required infeasibility reduction in restoration phaseslack bound frac Desired minimal relative distance of initial slack to boundslack bound push Desired minimal absolute distance of initial slack to boundtol Desired convergence tolerance (relative)

wantsol

solution report without -AMPL sum of1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

warm start bound pushEnables to specify how much should variables should bepushed inside the feasible region

warm start init point Enables to specify bound multiplier values

warm start mult bound pushEnables to specify how much should bound multipliersshould be pushed inside the feasible region

watchdog shortened iter trigger Trigger counter for watchdog procedurewsmp num threads Number of threads to be used in WSMPwsmp pivtol Pivot tolerance for the linear solver WSMPwsmp pivtolmax Maximum pivot tolerance for the linear solver WSMPwsmp scaling Determines how the matrix is scaled by WSMP

23

24

23 KNITRO

ldquoKNITRO is an optimization software library for finding solutions of both continuous (smo-oth) optimization models (with or without constraints) as well as discrete optimization modelswith integer or binary variables (ie mixed integer programs) KNITRO is primarily designedfor finding local optimal solutions of large-scale continuous nonlinear problemsrdquo The KNITROlibrary can be found in httpwwwartelyscom7

231 Basic information

KNITRO is a software package for solving smooth optimization problems with or withoutconstraints

Developed at Ziena Optimization

Operating system(s) KNITRO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) KNITRO is written C C++ Fortran and Java

Programming interfaces(s) Fortan CC++ Java and Microsoft Excel

Modeling language interface(s) KNITRO offers a total of five interfaces Matlab AMPL Math-ematica AIMMS GAMS and MPL

232 Installing KNITRO on Linux

How to get a license

KNITRO is developed by Ziena Optimization LLC and marketed and supported by ArtelysThe first step is to request a license by selecting the download tab8 Along with the academicand commercial trial versions there is a student version limited in problem size (300 variablesand 300 constraints) available for 6 months Each license is personalised hence among otherpersonal information you should inform about

bull the operating system (OS)

bull the OS bit architecture

bull the Machine ID of the computer on which KNITRO will runDownload extract an script available bellow the Machine ID field

After reading and accepting the End User License Agreement the download of the KNITROwill be available and you will get a license key on your email Then you should put the licensefile whose name begins with ziena lic in your $HOME directory or in the $HOMEzienadirectory

How to install

The KNITRO software package for Unix is delivered as a gzipped tar file After downloadingKNITRO you need to unpack it Type in a Terminal window

gunzip knitro-8x-platformnametargz

tar -xvf knitro-8x-platformnametar

sudo mv knitro-8x-z opt

7httpwwwartelyscomindexphppage=knitroamphl=en_EN8httpwwwartelyscomindexphppage=knitroamphl=en_ENdownloads-tab

25

This will create a directory named knitro-8x-z in your opt folder

In order to run KNITRO binary or executable files from anywhere on your Linux computerit is necessary to set several environment variables In particular you must update the PATH

environment variable so that it indicates the location of the knitroampl directory You mustalso update the LD LIBRARY PATH environment variable so that it indicates the location of theKNITRO lib directory Setting the PATH and LD LIBRARY PATH environment variables on a Linuxsystem can be done as follows

echo rsquoexport PATH=$PATHoptknitro-8x-zrsquo gtgt $HOMEbashrcecho rsquoexport PATH=$PATHoptknitro-8x-zknitroamplrsquo gtgt $HOMEbashrcecho rsquoexport LD LIBRARY PATH=$LD LIBRARY PATHoptknitro-8x-zlibrsquo gtgt $HOMEbashrc

Each Matlab release expects the KNITRO library file on Linux to have a specific name There-fore to use KNITRO 8x or later with Matlab release R2008a through 2011b you must createa symbolic link to the expected name Open a Terminal window and in the KNITRO librarydirectory issue the command

bull for Matlab releases R2008a R2008b or R2009aln -s libknitroso800 libknitroso5

bull for Matlab releases R2009b R2010a or R2010bln -s libknitroso800 libknitroso6

bull for Matlab releases R2011a R2011b or R2012aln -s libknitroso800 libknitroso7

Documentation

More informations can be found in the following documentation9

KNITRO 80 User Manual Knitro80 UserManualpdfVersion December 2011 (816 KB)

Specific KNITROMatlab interface documentation can be found on the Matlab OptimizationToolbox webpages10

233 Getting started with KNITRO and AMPL

option solver knitroampl

option knitro options rsquooption1=value1 option2=value2rsquo

solve

9httpwwwartelyscomuploadspdfsKnitro80_UserManualpdf10httpwwwmathworkscomaccesshelpdeskhelptoolboxoptim

26

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 18: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

Modeling language interface(s) IPOPT can be used as a library that can be linked to C++ C orFortran code as well as a solver executable for the AMPL modelling environment Thepackage includes interfaces to CUTEr optimisation testing environment as well as theMATLAB and R programming environments

222 Installing IPOPT

How to install IPOPT on Linux

Getting the IPOPT code via subversion

sudo svn co httpsprojectscoin-ororgsvnIpoptstable310 optCoinIpopt

This will create a directory named CoinIpopt in your opt folder

BLAS routines that provide standard building blocks for performing basic vector and matrixoperations

LAPACK routines for solving systems of simultaneous linear equations least-squares solutionsof linear systems of equations eigenvalue problems and singular value problems

HSL state-of-the-art packages for large-scale scientific computation

ASL AMPL Solver Library allows to read the nl files and provides the automatic differentia-tion functionality

MUMPS MUltifrontal Massively Parallel sparse direct Solver

METIS programs for partitioning graphs partitioning finite element meshes and producing fillreducing orderings for sparse matrices

To install IPOPT just run

cd optCoinIpoptThirdPartyBlas

getBlas

cd Lapack

getLapack

cd ASL

getASL

cd Mumps

getMumps

cd Metis

getMetis

IPOPT requires a sparse symmetric linear solver There are different possibilities The userrsquosguide of IPOPT suggests that you use HSL subroutines

1 Go to HSL website2 and find the package list

2 Click on MA27 read the license and submit the registration form

3 Go back to the package list

4 Click on MC19 read the license and submit the registration form

2httphslrlacukarchivehslarchivehtml

18

You will receive in your email the links to obtain the HSL packages Download them to your$HOME directory and run

gunzip ma27-100targz

tar xvf ma27-100tar

gunzip mc19-100targz

tar xvf mc19-100tar

cd optCoinIpoptThirdPartyHSL

sudo mv -r $HOMEmc19-100

sudo mv -r $HOMEma27-100

cd mc19-100

sudo configure

sudo make install

sudo make check

cd ma27-100

sudo configure

sudo make install

sudo make check

The make check command will check if the installation of the HSL packages was successfulReturn to the optCoinIpopt directory and continue the IPOPT installation

cd

sudo mkdir build

cd build

sudo configure

sudo make test

sudo make install

If you get the error message ldquoerrorlsquoNULLrsquo was not declared in this scoperdquo when run-ning sudo make then you have to edit the following filesoptCoinIpoptIpoptsrcCommonIpSmartPtrhpp

optCoinIpoptIpoptsrcAlgorithmLinearSolversIpTripletToCSRConvertercpp

by adding include ltcstddefgt

How to install IPOPT on Mac OS X

In a Terminal window install homebrewusrbinruby -e $(usrbincurl -fsSL httpsrawgithubcommxclhomebrewmaster

LibraryContributionsinstall homebrewrb)

With this software you can easily install dependency packages like wget and gfortran

brew install wget

brew install gfortran

sudo wget httpwwwcoin-ororgdownloadsourceIpoptIpopt-3102zip

sudo unzip Ipopt-3102zip

cd Ipopt-3102

Download Netlib BlasLapack

cd optCoinIpoptThirdPartyBlas

sudo getBlas

19

cd Lapack

sudo getLapack

cd ASL

sudo getASL

cd Mumps

sudo getMumps

cd Metis

sudo getMetis

Make a build for a 64bit target

sudo mkdir build64

cd build64

sudo configure --disable-shared --with-blas=BUILD --with-lapack=BUILD F77=gfortran

FFLAGS=-fexceptions -m64 -fbackslash CFLAGS=-fno-common -no-cpp-precomp -fexceptions

-arch x86 64 -m64 CXXFLAGS=-fno-common -no-cpp-precomp -fexceptions -arch x86 64

-m64

Compile test and install

make

make test

make install

Add the following to your bashrc or bash profile

IPOPTDIR=optCoinIpoptbuild

export PATH=$PATH$IPOPTDIR$IPOPTDIRbinexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyASLexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyBlaslibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyHSLlibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyLapacklibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMetislibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMumpslibs

How to install the Matlab interface on Linux

First of all be sure

cd optmatlabR2008asysosglnx86

mv libgcc sso1 libgcc sso1bck

ln -s libi386-linux-gnulibgcc sso1 libgcc sso1

mv libstdc++so608 libstdc++so608bck

ln -s usrlibi386-linux-gnulibstdc++so6 libstdc++so608

Go to the subdirectory optCoinIpoptbuildIpoptcontribMatlabInterfacesrc andopen the Makefile file

sudo apt-get install hardening-wrapper

You need to edit this file to suit the installation for your system setup You may need tomodify MATLAB HOME MEXSUFFIX and MEX as explained in the comments of the Makefile I setthese variables as follows

20

MATLAB HOME = optmatlabR2008a

MEXSUFFIX = mexglx

MEX = optmatlabR2008abinmex

In my case I removed some of the compiler options as it follows

prefix = optCoinIpopt

CXX = optintelcomposer xe 2011 sp19293binia32icpc

CCXXFLAGS = -O3 -pipe -DNDEBUG -Wparentheses -Wreturn-type -Wcast-qual -Wall

-Wpointer-arith -Wwrite-strings -Wconversion -Wno-unknown-pragmas -DMATLAB MEXFILE

LDFLAGS = $(CXXFLAGS) -Wl --rpath -Wl $prefixlibcoin -Wl --rpath -Wl

--rpath -Wl$prefixlibcoinThirdParty

In my case I had to set explicitly the include Matlab folder

o cpp

$(CXX) $(CXXFLAGS) $(INCL) -IoptmatlabR2008aexterninclude -o $ -c $^

Once you set up the Makefile properly type sudo make install in the same directory as theMakefile If the installation procedure was successful you will end up with a MEX file calledipoptmexglx In order to use it in MATLAB you need to tell MATLAB where to find it Todo this just type addpath optCoinIpoptlib in the MATLAB command window after youcheck if the ipoptmexglx file is in this folder

Documentation

More informations can be found in the following documentation

Online documentation httpwwwcoin-ororgIpoptdocumentation3

A PDF version of this documentation can be downloaded here4

A short IPOPT tutorial can be downloaded here5

MATLAB interface Instructions specific to the MATLAB interface can be found at the Matlabinterface page6

223 Getting started with IPOPT and AMPL

option solver ipopt

option ipopt options rsquooption1=value1 option2=value2rsquo

solve

3httpwwwcoin-ororgIpoptdocumentation4httpsprojectscoin-ororgIpoptbrowserstable39Ipoptdocdocumentationpdfformat=raw5httpdropsdagstuhldevolltexte20092089pdf09061WaechterAndreasPaper2089pdf6httpsprojectscoin-ororgIpoptwikiMatlabInterface

21

Table 21 IPOPT directives for AMPL

Option Description

acceptable compl inf tol Acceptance threshold for the complementarity conditionsacceptable constr viol tol Acceptance threshold for the constraint violationacceptable dual inf tol Acceptance threshold for the dual infeasibilityacceptable tol Acceptable convergence tolerance (relative)alpha for y Step size for constraint multipliersbound frac Desired minimal relative distance of initial point to boundbound mult init val Initial value for the bound multipliersbound push Desired minimal absolute distance of initial point to boundbound relax factor Factor for initial relaxation of the boundscompl inf tol Acceptance threshold for the complementarity conditionsconstr mult init max Maximal allowed least-square guess of constraint multipliersconstr viol tol Desired threshold for the constraint violationdiverging iterates tol Threshold for maximal value of primal iteratesdual inf tol Desired threshold for the dual infeasibilityexpect infeasible problem Enable heuristics to quickly detect an infeasible problemfile print level Verbosity level for output filehalt on ampl error Exit with message on evaluation errorhessian approximation Can enable Quasi-Newton approximation of hessianhonor original bounds If no solution might slightly violate boundslinear scaling on demand Enables heuristic for scaling only when seems requiredlinear solver Linear solver to be used for step calculationlinear system scaling Method for scaling the linear systemsma27 pivtol Pivot tolerance for the linear solver MA27ma27 pivtolmax Maximal pivot tolerance for the linear solver MA27ma57 pivtol Pivot tolerance for the linear solver MA57ma57 pivtolmax Maximal pivot tolerance for the linear solver MA57

22

Table 22 IPOPT directives for AMPL

Option Description

max cpu time CPU time limitmax iter Maximum number of iterations

max refinement stepsMaximal number of iterative refinement steps per linearsystem solve

max soc Maximal number of second order correction trial stepsmaxit Maximum number of iterations (same as max iter)

min refinement stepsMinimum number of iterative refinement steps per linearsystem solve

mu init Initial value for the barrier parametermu max Maximal value for barrier parameter for adaptive strategymu oracle Oracle for a new barrier parameter in the adaptive strategymu strategy Update strategy for barrier parameternlp scaling max gradient Maximum gradient after scalingnlp scaling method Select the technique used for scaling the NLPobj scaling factor Scaling factor for the objective functionoption file name File name of options file (default ipoptopt)outlev Verbosity level (same as print level)output file File name of an output file (leave unset for no file output)pardiso matching strategy Matching strategy for linear solver Pardisopardiso out of core power Enables out-of-core version of linear solver Pardisoprint level Verbosity levelprint options documentation Print all available options (for ipoptopt)print user options Toggle printing of user optionsrequired infeasibility reduction Required infeasibility reduction in restoration phaseslack bound frac Desired minimal relative distance of initial slack to boundslack bound push Desired minimal absolute distance of initial slack to boundtol Desired convergence tolerance (relative)

wantsol

solution report without -AMPL sum of1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

warm start bound pushEnables to specify how much should variables should bepushed inside the feasible region

warm start init point Enables to specify bound multiplier values

warm start mult bound pushEnables to specify how much should bound multipliersshould be pushed inside the feasible region

watchdog shortened iter trigger Trigger counter for watchdog procedurewsmp num threads Number of threads to be used in WSMPwsmp pivtol Pivot tolerance for the linear solver WSMPwsmp pivtolmax Maximum pivot tolerance for the linear solver WSMPwsmp scaling Determines how the matrix is scaled by WSMP

23

24

23 KNITRO

ldquoKNITRO is an optimization software library for finding solutions of both continuous (smo-oth) optimization models (with or without constraints) as well as discrete optimization modelswith integer or binary variables (ie mixed integer programs) KNITRO is primarily designedfor finding local optimal solutions of large-scale continuous nonlinear problemsrdquo The KNITROlibrary can be found in httpwwwartelyscom7

231 Basic information

KNITRO is a software package for solving smooth optimization problems with or withoutconstraints

Developed at Ziena Optimization

Operating system(s) KNITRO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) KNITRO is written C C++ Fortran and Java

Programming interfaces(s) Fortan CC++ Java and Microsoft Excel

Modeling language interface(s) KNITRO offers a total of five interfaces Matlab AMPL Math-ematica AIMMS GAMS and MPL

232 Installing KNITRO on Linux

How to get a license

KNITRO is developed by Ziena Optimization LLC and marketed and supported by ArtelysThe first step is to request a license by selecting the download tab8 Along with the academicand commercial trial versions there is a student version limited in problem size (300 variablesand 300 constraints) available for 6 months Each license is personalised hence among otherpersonal information you should inform about

bull the operating system (OS)

bull the OS bit architecture

bull the Machine ID of the computer on which KNITRO will runDownload extract an script available bellow the Machine ID field

After reading and accepting the End User License Agreement the download of the KNITROwill be available and you will get a license key on your email Then you should put the licensefile whose name begins with ziena lic in your $HOME directory or in the $HOMEzienadirectory

How to install

The KNITRO software package for Unix is delivered as a gzipped tar file After downloadingKNITRO you need to unpack it Type in a Terminal window

gunzip knitro-8x-platformnametargz

tar -xvf knitro-8x-platformnametar

sudo mv knitro-8x-z opt

7httpwwwartelyscomindexphppage=knitroamphl=en_EN8httpwwwartelyscomindexphppage=knitroamphl=en_ENdownloads-tab

25

This will create a directory named knitro-8x-z in your opt folder

In order to run KNITRO binary or executable files from anywhere on your Linux computerit is necessary to set several environment variables In particular you must update the PATH

environment variable so that it indicates the location of the knitroampl directory You mustalso update the LD LIBRARY PATH environment variable so that it indicates the location of theKNITRO lib directory Setting the PATH and LD LIBRARY PATH environment variables on a Linuxsystem can be done as follows

echo rsquoexport PATH=$PATHoptknitro-8x-zrsquo gtgt $HOMEbashrcecho rsquoexport PATH=$PATHoptknitro-8x-zknitroamplrsquo gtgt $HOMEbashrcecho rsquoexport LD LIBRARY PATH=$LD LIBRARY PATHoptknitro-8x-zlibrsquo gtgt $HOMEbashrc

Each Matlab release expects the KNITRO library file on Linux to have a specific name There-fore to use KNITRO 8x or later with Matlab release R2008a through 2011b you must createa symbolic link to the expected name Open a Terminal window and in the KNITRO librarydirectory issue the command

bull for Matlab releases R2008a R2008b or R2009aln -s libknitroso800 libknitroso5

bull for Matlab releases R2009b R2010a or R2010bln -s libknitroso800 libknitroso6

bull for Matlab releases R2011a R2011b or R2012aln -s libknitroso800 libknitroso7

Documentation

More informations can be found in the following documentation9

KNITRO 80 User Manual Knitro80 UserManualpdfVersion December 2011 (816 KB)

Specific KNITROMatlab interface documentation can be found on the Matlab OptimizationToolbox webpages10

233 Getting started with KNITRO and AMPL

option solver knitroampl

option knitro options rsquooption1=value1 option2=value2rsquo

solve

9httpwwwartelyscomuploadspdfsKnitro80_UserManualpdf10httpwwwmathworkscomaccesshelpdeskhelptoolboxoptim

26

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 19: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

You will receive in your email the links to obtain the HSL packages Download them to your$HOME directory and run

gunzip ma27-100targz

tar xvf ma27-100tar

gunzip mc19-100targz

tar xvf mc19-100tar

cd optCoinIpoptThirdPartyHSL

sudo mv -r $HOMEmc19-100

sudo mv -r $HOMEma27-100

cd mc19-100

sudo configure

sudo make install

sudo make check

cd ma27-100

sudo configure

sudo make install

sudo make check

The make check command will check if the installation of the HSL packages was successfulReturn to the optCoinIpopt directory and continue the IPOPT installation

cd

sudo mkdir build

cd build

sudo configure

sudo make test

sudo make install

If you get the error message ldquoerrorlsquoNULLrsquo was not declared in this scoperdquo when run-ning sudo make then you have to edit the following filesoptCoinIpoptIpoptsrcCommonIpSmartPtrhpp

optCoinIpoptIpoptsrcAlgorithmLinearSolversIpTripletToCSRConvertercpp

by adding include ltcstddefgt

How to install IPOPT on Mac OS X

In a Terminal window install homebrewusrbinruby -e $(usrbincurl -fsSL httpsrawgithubcommxclhomebrewmaster

LibraryContributionsinstall homebrewrb)

With this software you can easily install dependency packages like wget and gfortran

brew install wget

brew install gfortran

sudo wget httpwwwcoin-ororgdownloadsourceIpoptIpopt-3102zip

sudo unzip Ipopt-3102zip

cd Ipopt-3102

Download Netlib BlasLapack

cd optCoinIpoptThirdPartyBlas

sudo getBlas

19

cd Lapack

sudo getLapack

cd ASL

sudo getASL

cd Mumps

sudo getMumps

cd Metis

sudo getMetis

Make a build for a 64bit target

sudo mkdir build64

cd build64

sudo configure --disable-shared --with-blas=BUILD --with-lapack=BUILD F77=gfortran

FFLAGS=-fexceptions -m64 -fbackslash CFLAGS=-fno-common -no-cpp-precomp -fexceptions

-arch x86 64 -m64 CXXFLAGS=-fno-common -no-cpp-precomp -fexceptions -arch x86 64

-m64

Compile test and install

make

make test

make install

Add the following to your bashrc or bash profile

IPOPTDIR=optCoinIpoptbuild

export PATH=$PATH$IPOPTDIR$IPOPTDIRbinexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyASLexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyBlaslibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyHSLlibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyLapacklibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMetislibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMumpslibs

How to install the Matlab interface on Linux

First of all be sure

cd optmatlabR2008asysosglnx86

mv libgcc sso1 libgcc sso1bck

ln -s libi386-linux-gnulibgcc sso1 libgcc sso1

mv libstdc++so608 libstdc++so608bck

ln -s usrlibi386-linux-gnulibstdc++so6 libstdc++so608

Go to the subdirectory optCoinIpoptbuildIpoptcontribMatlabInterfacesrc andopen the Makefile file

sudo apt-get install hardening-wrapper

You need to edit this file to suit the installation for your system setup You may need tomodify MATLAB HOME MEXSUFFIX and MEX as explained in the comments of the Makefile I setthese variables as follows

20

MATLAB HOME = optmatlabR2008a

MEXSUFFIX = mexglx

MEX = optmatlabR2008abinmex

In my case I removed some of the compiler options as it follows

prefix = optCoinIpopt

CXX = optintelcomposer xe 2011 sp19293binia32icpc

CCXXFLAGS = -O3 -pipe -DNDEBUG -Wparentheses -Wreturn-type -Wcast-qual -Wall

-Wpointer-arith -Wwrite-strings -Wconversion -Wno-unknown-pragmas -DMATLAB MEXFILE

LDFLAGS = $(CXXFLAGS) -Wl --rpath -Wl $prefixlibcoin -Wl --rpath -Wl

--rpath -Wl$prefixlibcoinThirdParty

In my case I had to set explicitly the include Matlab folder

o cpp

$(CXX) $(CXXFLAGS) $(INCL) -IoptmatlabR2008aexterninclude -o $ -c $^

Once you set up the Makefile properly type sudo make install in the same directory as theMakefile If the installation procedure was successful you will end up with a MEX file calledipoptmexglx In order to use it in MATLAB you need to tell MATLAB where to find it Todo this just type addpath optCoinIpoptlib in the MATLAB command window after youcheck if the ipoptmexglx file is in this folder

Documentation

More informations can be found in the following documentation

Online documentation httpwwwcoin-ororgIpoptdocumentation3

A PDF version of this documentation can be downloaded here4

A short IPOPT tutorial can be downloaded here5

MATLAB interface Instructions specific to the MATLAB interface can be found at the Matlabinterface page6

223 Getting started with IPOPT and AMPL

option solver ipopt

option ipopt options rsquooption1=value1 option2=value2rsquo

solve

3httpwwwcoin-ororgIpoptdocumentation4httpsprojectscoin-ororgIpoptbrowserstable39Ipoptdocdocumentationpdfformat=raw5httpdropsdagstuhldevolltexte20092089pdf09061WaechterAndreasPaper2089pdf6httpsprojectscoin-ororgIpoptwikiMatlabInterface

21

Table 21 IPOPT directives for AMPL

Option Description

acceptable compl inf tol Acceptance threshold for the complementarity conditionsacceptable constr viol tol Acceptance threshold for the constraint violationacceptable dual inf tol Acceptance threshold for the dual infeasibilityacceptable tol Acceptable convergence tolerance (relative)alpha for y Step size for constraint multipliersbound frac Desired minimal relative distance of initial point to boundbound mult init val Initial value for the bound multipliersbound push Desired minimal absolute distance of initial point to boundbound relax factor Factor for initial relaxation of the boundscompl inf tol Acceptance threshold for the complementarity conditionsconstr mult init max Maximal allowed least-square guess of constraint multipliersconstr viol tol Desired threshold for the constraint violationdiverging iterates tol Threshold for maximal value of primal iteratesdual inf tol Desired threshold for the dual infeasibilityexpect infeasible problem Enable heuristics to quickly detect an infeasible problemfile print level Verbosity level for output filehalt on ampl error Exit with message on evaluation errorhessian approximation Can enable Quasi-Newton approximation of hessianhonor original bounds If no solution might slightly violate boundslinear scaling on demand Enables heuristic for scaling only when seems requiredlinear solver Linear solver to be used for step calculationlinear system scaling Method for scaling the linear systemsma27 pivtol Pivot tolerance for the linear solver MA27ma27 pivtolmax Maximal pivot tolerance for the linear solver MA27ma57 pivtol Pivot tolerance for the linear solver MA57ma57 pivtolmax Maximal pivot tolerance for the linear solver MA57

22

Table 22 IPOPT directives for AMPL

Option Description

max cpu time CPU time limitmax iter Maximum number of iterations

max refinement stepsMaximal number of iterative refinement steps per linearsystem solve

max soc Maximal number of second order correction trial stepsmaxit Maximum number of iterations (same as max iter)

min refinement stepsMinimum number of iterative refinement steps per linearsystem solve

mu init Initial value for the barrier parametermu max Maximal value for barrier parameter for adaptive strategymu oracle Oracle for a new barrier parameter in the adaptive strategymu strategy Update strategy for barrier parameternlp scaling max gradient Maximum gradient after scalingnlp scaling method Select the technique used for scaling the NLPobj scaling factor Scaling factor for the objective functionoption file name File name of options file (default ipoptopt)outlev Verbosity level (same as print level)output file File name of an output file (leave unset for no file output)pardiso matching strategy Matching strategy for linear solver Pardisopardiso out of core power Enables out-of-core version of linear solver Pardisoprint level Verbosity levelprint options documentation Print all available options (for ipoptopt)print user options Toggle printing of user optionsrequired infeasibility reduction Required infeasibility reduction in restoration phaseslack bound frac Desired minimal relative distance of initial slack to boundslack bound push Desired minimal absolute distance of initial slack to boundtol Desired convergence tolerance (relative)

wantsol

solution report without -AMPL sum of1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

warm start bound pushEnables to specify how much should variables should bepushed inside the feasible region

warm start init point Enables to specify bound multiplier values

warm start mult bound pushEnables to specify how much should bound multipliersshould be pushed inside the feasible region

watchdog shortened iter trigger Trigger counter for watchdog procedurewsmp num threads Number of threads to be used in WSMPwsmp pivtol Pivot tolerance for the linear solver WSMPwsmp pivtolmax Maximum pivot tolerance for the linear solver WSMPwsmp scaling Determines how the matrix is scaled by WSMP

23

24

23 KNITRO

ldquoKNITRO is an optimization software library for finding solutions of both continuous (smo-oth) optimization models (with or without constraints) as well as discrete optimization modelswith integer or binary variables (ie mixed integer programs) KNITRO is primarily designedfor finding local optimal solutions of large-scale continuous nonlinear problemsrdquo The KNITROlibrary can be found in httpwwwartelyscom7

231 Basic information

KNITRO is a software package for solving smooth optimization problems with or withoutconstraints

Developed at Ziena Optimization

Operating system(s) KNITRO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) KNITRO is written C C++ Fortran and Java

Programming interfaces(s) Fortan CC++ Java and Microsoft Excel

Modeling language interface(s) KNITRO offers a total of five interfaces Matlab AMPL Math-ematica AIMMS GAMS and MPL

232 Installing KNITRO on Linux

How to get a license

KNITRO is developed by Ziena Optimization LLC and marketed and supported by ArtelysThe first step is to request a license by selecting the download tab8 Along with the academicand commercial trial versions there is a student version limited in problem size (300 variablesand 300 constraints) available for 6 months Each license is personalised hence among otherpersonal information you should inform about

bull the operating system (OS)

bull the OS bit architecture

bull the Machine ID of the computer on which KNITRO will runDownload extract an script available bellow the Machine ID field

After reading and accepting the End User License Agreement the download of the KNITROwill be available and you will get a license key on your email Then you should put the licensefile whose name begins with ziena lic in your $HOME directory or in the $HOMEzienadirectory

How to install

The KNITRO software package for Unix is delivered as a gzipped tar file After downloadingKNITRO you need to unpack it Type in a Terminal window

gunzip knitro-8x-platformnametargz

tar -xvf knitro-8x-platformnametar

sudo mv knitro-8x-z opt

7httpwwwartelyscomindexphppage=knitroamphl=en_EN8httpwwwartelyscomindexphppage=knitroamphl=en_ENdownloads-tab

25

This will create a directory named knitro-8x-z in your opt folder

In order to run KNITRO binary or executable files from anywhere on your Linux computerit is necessary to set several environment variables In particular you must update the PATH

environment variable so that it indicates the location of the knitroampl directory You mustalso update the LD LIBRARY PATH environment variable so that it indicates the location of theKNITRO lib directory Setting the PATH and LD LIBRARY PATH environment variables on a Linuxsystem can be done as follows

echo rsquoexport PATH=$PATHoptknitro-8x-zrsquo gtgt $HOMEbashrcecho rsquoexport PATH=$PATHoptknitro-8x-zknitroamplrsquo gtgt $HOMEbashrcecho rsquoexport LD LIBRARY PATH=$LD LIBRARY PATHoptknitro-8x-zlibrsquo gtgt $HOMEbashrc

Each Matlab release expects the KNITRO library file on Linux to have a specific name There-fore to use KNITRO 8x or later with Matlab release R2008a through 2011b you must createa symbolic link to the expected name Open a Terminal window and in the KNITRO librarydirectory issue the command

bull for Matlab releases R2008a R2008b or R2009aln -s libknitroso800 libknitroso5

bull for Matlab releases R2009b R2010a or R2010bln -s libknitroso800 libknitroso6

bull for Matlab releases R2011a R2011b or R2012aln -s libknitroso800 libknitroso7

Documentation

More informations can be found in the following documentation9

KNITRO 80 User Manual Knitro80 UserManualpdfVersion December 2011 (816 KB)

Specific KNITROMatlab interface documentation can be found on the Matlab OptimizationToolbox webpages10

233 Getting started with KNITRO and AMPL

option solver knitroampl

option knitro options rsquooption1=value1 option2=value2rsquo

solve

9httpwwwartelyscomuploadspdfsKnitro80_UserManualpdf10httpwwwmathworkscomaccesshelpdeskhelptoolboxoptim

26

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 20: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

cd Lapack

sudo getLapack

cd ASL

sudo getASL

cd Mumps

sudo getMumps

cd Metis

sudo getMetis

Make a build for a 64bit target

sudo mkdir build64

cd build64

sudo configure --disable-shared --with-blas=BUILD --with-lapack=BUILD F77=gfortran

FFLAGS=-fexceptions -m64 -fbackslash CFLAGS=-fno-common -no-cpp-precomp -fexceptions

-arch x86 64 -m64 CXXFLAGS=-fno-common -no-cpp-precomp -fexceptions -arch x86 64

-m64

Compile test and install

make

make test

make install

Add the following to your bashrc or bash profile

IPOPTDIR=optCoinIpoptbuild

export PATH=$PATH$IPOPTDIR$IPOPTDIRbinexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyASLexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyBlaslibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyHSLlibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyLapacklibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMetislibsexport LD LIBRARY PATH=$LD LIBRARY PATH$IPOPTDIRThirdPartyMumpslibs

How to install the Matlab interface on Linux

First of all be sure

cd optmatlabR2008asysosglnx86

mv libgcc sso1 libgcc sso1bck

ln -s libi386-linux-gnulibgcc sso1 libgcc sso1

mv libstdc++so608 libstdc++so608bck

ln -s usrlibi386-linux-gnulibstdc++so6 libstdc++so608

Go to the subdirectory optCoinIpoptbuildIpoptcontribMatlabInterfacesrc andopen the Makefile file

sudo apt-get install hardening-wrapper

You need to edit this file to suit the installation for your system setup You may need tomodify MATLAB HOME MEXSUFFIX and MEX as explained in the comments of the Makefile I setthese variables as follows

20

MATLAB HOME = optmatlabR2008a

MEXSUFFIX = mexglx

MEX = optmatlabR2008abinmex

In my case I removed some of the compiler options as it follows

prefix = optCoinIpopt

CXX = optintelcomposer xe 2011 sp19293binia32icpc

CCXXFLAGS = -O3 -pipe -DNDEBUG -Wparentheses -Wreturn-type -Wcast-qual -Wall

-Wpointer-arith -Wwrite-strings -Wconversion -Wno-unknown-pragmas -DMATLAB MEXFILE

LDFLAGS = $(CXXFLAGS) -Wl --rpath -Wl $prefixlibcoin -Wl --rpath -Wl

--rpath -Wl$prefixlibcoinThirdParty

In my case I had to set explicitly the include Matlab folder

o cpp

$(CXX) $(CXXFLAGS) $(INCL) -IoptmatlabR2008aexterninclude -o $ -c $^

Once you set up the Makefile properly type sudo make install in the same directory as theMakefile If the installation procedure was successful you will end up with a MEX file calledipoptmexglx In order to use it in MATLAB you need to tell MATLAB where to find it Todo this just type addpath optCoinIpoptlib in the MATLAB command window after youcheck if the ipoptmexglx file is in this folder

Documentation

More informations can be found in the following documentation

Online documentation httpwwwcoin-ororgIpoptdocumentation3

A PDF version of this documentation can be downloaded here4

A short IPOPT tutorial can be downloaded here5

MATLAB interface Instructions specific to the MATLAB interface can be found at the Matlabinterface page6

223 Getting started with IPOPT and AMPL

option solver ipopt

option ipopt options rsquooption1=value1 option2=value2rsquo

solve

3httpwwwcoin-ororgIpoptdocumentation4httpsprojectscoin-ororgIpoptbrowserstable39Ipoptdocdocumentationpdfformat=raw5httpdropsdagstuhldevolltexte20092089pdf09061WaechterAndreasPaper2089pdf6httpsprojectscoin-ororgIpoptwikiMatlabInterface

21

Table 21 IPOPT directives for AMPL

Option Description

acceptable compl inf tol Acceptance threshold for the complementarity conditionsacceptable constr viol tol Acceptance threshold for the constraint violationacceptable dual inf tol Acceptance threshold for the dual infeasibilityacceptable tol Acceptable convergence tolerance (relative)alpha for y Step size for constraint multipliersbound frac Desired minimal relative distance of initial point to boundbound mult init val Initial value for the bound multipliersbound push Desired minimal absolute distance of initial point to boundbound relax factor Factor for initial relaxation of the boundscompl inf tol Acceptance threshold for the complementarity conditionsconstr mult init max Maximal allowed least-square guess of constraint multipliersconstr viol tol Desired threshold for the constraint violationdiverging iterates tol Threshold for maximal value of primal iteratesdual inf tol Desired threshold for the dual infeasibilityexpect infeasible problem Enable heuristics to quickly detect an infeasible problemfile print level Verbosity level for output filehalt on ampl error Exit with message on evaluation errorhessian approximation Can enable Quasi-Newton approximation of hessianhonor original bounds If no solution might slightly violate boundslinear scaling on demand Enables heuristic for scaling only when seems requiredlinear solver Linear solver to be used for step calculationlinear system scaling Method for scaling the linear systemsma27 pivtol Pivot tolerance for the linear solver MA27ma27 pivtolmax Maximal pivot tolerance for the linear solver MA27ma57 pivtol Pivot tolerance for the linear solver MA57ma57 pivtolmax Maximal pivot tolerance for the linear solver MA57

22

Table 22 IPOPT directives for AMPL

Option Description

max cpu time CPU time limitmax iter Maximum number of iterations

max refinement stepsMaximal number of iterative refinement steps per linearsystem solve

max soc Maximal number of second order correction trial stepsmaxit Maximum number of iterations (same as max iter)

min refinement stepsMinimum number of iterative refinement steps per linearsystem solve

mu init Initial value for the barrier parametermu max Maximal value for barrier parameter for adaptive strategymu oracle Oracle for a new barrier parameter in the adaptive strategymu strategy Update strategy for barrier parameternlp scaling max gradient Maximum gradient after scalingnlp scaling method Select the technique used for scaling the NLPobj scaling factor Scaling factor for the objective functionoption file name File name of options file (default ipoptopt)outlev Verbosity level (same as print level)output file File name of an output file (leave unset for no file output)pardiso matching strategy Matching strategy for linear solver Pardisopardiso out of core power Enables out-of-core version of linear solver Pardisoprint level Verbosity levelprint options documentation Print all available options (for ipoptopt)print user options Toggle printing of user optionsrequired infeasibility reduction Required infeasibility reduction in restoration phaseslack bound frac Desired minimal relative distance of initial slack to boundslack bound push Desired minimal absolute distance of initial slack to boundtol Desired convergence tolerance (relative)

wantsol

solution report without -AMPL sum of1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

warm start bound pushEnables to specify how much should variables should bepushed inside the feasible region

warm start init point Enables to specify bound multiplier values

warm start mult bound pushEnables to specify how much should bound multipliersshould be pushed inside the feasible region

watchdog shortened iter trigger Trigger counter for watchdog procedurewsmp num threads Number of threads to be used in WSMPwsmp pivtol Pivot tolerance for the linear solver WSMPwsmp pivtolmax Maximum pivot tolerance for the linear solver WSMPwsmp scaling Determines how the matrix is scaled by WSMP

23

24

23 KNITRO

ldquoKNITRO is an optimization software library for finding solutions of both continuous (smo-oth) optimization models (with or without constraints) as well as discrete optimization modelswith integer or binary variables (ie mixed integer programs) KNITRO is primarily designedfor finding local optimal solutions of large-scale continuous nonlinear problemsrdquo The KNITROlibrary can be found in httpwwwartelyscom7

231 Basic information

KNITRO is a software package for solving smooth optimization problems with or withoutconstraints

Developed at Ziena Optimization

Operating system(s) KNITRO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) KNITRO is written C C++ Fortran and Java

Programming interfaces(s) Fortan CC++ Java and Microsoft Excel

Modeling language interface(s) KNITRO offers a total of five interfaces Matlab AMPL Math-ematica AIMMS GAMS and MPL

232 Installing KNITRO on Linux

How to get a license

KNITRO is developed by Ziena Optimization LLC and marketed and supported by ArtelysThe first step is to request a license by selecting the download tab8 Along with the academicand commercial trial versions there is a student version limited in problem size (300 variablesand 300 constraints) available for 6 months Each license is personalised hence among otherpersonal information you should inform about

bull the operating system (OS)

bull the OS bit architecture

bull the Machine ID of the computer on which KNITRO will runDownload extract an script available bellow the Machine ID field

After reading and accepting the End User License Agreement the download of the KNITROwill be available and you will get a license key on your email Then you should put the licensefile whose name begins with ziena lic in your $HOME directory or in the $HOMEzienadirectory

How to install

The KNITRO software package for Unix is delivered as a gzipped tar file After downloadingKNITRO you need to unpack it Type in a Terminal window

gunzip knitro-8x-platformnametargz

tar -xvf knitro-8x-platformnametar

sudo mv knitro-8x-z opt

7httpwwwartelyscomindexphppage=knitroamphl=en_EN8httpwwwartelyscomindexphppage=knitroamphl=en_ENdownloads-tab

25

This will create a directory named knitro-8x-z in your opt folder

In order to run KNITRO binary or executable files from anywhere on your Linux computerit is necessary to set several environment variables In particular you must update the PATH

environment variable so that it indicates the location of the knitroampl directory You mustalso update the LD LIBRARY PATH environment variable so that it indicates the location of theKNITRO lib directory Setting the PATH and LD LIBRARY PATH environment variables on a Linuxsystem can be done as follows

echo rsquoexport PATH=$PATHoptknitro-8x-zrsquo gtgt $HOMEbashrcecho rsquoexport PATH=$PATHoptknitro-8x-zknitroamplrsquo gtgt $HOMEbashrcecho rsquoexport LD LIBRARY PATH=$LD LIBRARY PATHoptknitro-8x-zlibrsquo gtgt $HOMEbashrc

Each Matlab release expects the KNITRO library file on Linux to have a specific name There-fore to use KNITRO 8x or later with Matlab release R2008a through 2011b you must createa symbolic link to the expected name Open a Terminal window and in the KNITRO librarydirectory issue the command

bull for Matlab releases R2008a R2008b or R2009aln -s libknitroso800 libknitroso5

bull for Matlab releases R2009b R2010a or R2010bln -s libknitroso800 libknitroso6

bull for Matlab releases R2011a R2011b or R2012aln -s libknitroso800 libknitroso7

Documentation

More informations can be found in the following documentation9

KNITRO 80 User Manual Knitro80 UserManualpdfVersion December 2011 (816 KB)

Specific KNITROMatlab interface documentation can be found on the Matlab OptimizationToolbox webpages10

233 Getting started with KNITRO and AMPL

option solver knitroampl

option knitro options rsquooption1=value1 option2=value2rsquo

solve

9httpwwwartelyscomuploadspdfsKnitro80_UserManualpdf10httpwwwmathworkscomaccesshelpdeskhelptoolboxoptim

26

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 21: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

MATLAB HOME = optmatlabR2008a

MEXSUFFIX = mexglx

MEX = optmatlabR2008abinmex

In my case I removed some of the compiler options as it follows

prefix = optCoinIpopt

CXX = optintelcomposer xe 2011 sp19293binia32icpc

CCXXFLAGS = -O3 -pipe -DNDEBUG -Wparentheses -Wreturn-type -Wcast-qual -Wall

-Wpointer-arith -Wwrite-strings -Wconversion -Wno-unknown-pragmas -DMATLAB MEXFILE

LDFLAGS = $(CXXFLAGS) -Wl --rpath -Wl $prefixlibcoin -Wl --rpath -Wl

--rpath -Wl$prefixlibcoinThirdParty

In my case I had to set explicitly the include Matlab folder

o cpp

$(CXX) $(CXXFLAGS) $(INCL) -IoptmatlabR2008aexterninclude -o $ -c $^

Once you set up the Makefile properly type sudo make install in the same directory as theMakefile If the installation procedure was successful you will end up with a MEX file calledipoptmexglx In order to use it in MATLAB you need to tell MATLAB where to find it Todo this just type addpath optCoinIpoptlib in the MATLAB command window after youcheck if the ipoptmexglx file is in this folder

Documentation

More informations can be found in the following documentation

Online documentation httpwwwcoin-ororgIpoptdocumentation3

A PDF version of this documentation can be downloaded here4

A short IPOPT tutorial can be downloaded here5

MATLAB interface Instructions specific to the MATLAB interface can be found at the Matlabinterface page6

223 Getting started with IPOPT and AMPL

option solver ipopt

option ipopt options rsquooption1=value1 option2=value2rsquo

solve

3httpwwwcoin-ororgIpoptdocumentation4httpsprojectscoin-ororgIpoptbrowserstable39Ipoptdocdocumentationpdfformat=raw5httpdropsdagstuhldevolltexte20092089pdf09061WaechterAndreasPaper2089pdf6httpsprojectscoin-ororgIpoptwikiMatlabInterface

21

Table 21 IPOPT directives for AMPL

Option Description

acceptable compl inf tol Acceptance threshold for the complementarity conditionsacceptable constr viol tol Acceptance threshold for the constraint violationacceptable dual inf tol Acceptance threshold for the dual infeasibilityacceptable tol Acceptable convergence tolerance (relative)alpha for y Step size for constraint multipliersbound frac Desired minimal relative distance of initial point to boundbound mult init val Initial value for the bound multipliersbound push Desired minimal absolute distance of initial point to boundbound relax factor Factor for initial relaxation of the boundscompl inf tol Acceptance threshold for the complementarity conditionsconstr mult init max Maximal allowed least-square guess of constraint multipliersconstr viol tol Desired threshold for the constraint violationdiverging iterates tol Threshold for maximal value of primal iteratesdual inf tol Desired threshold for the dual infeasibilityexpect infeasible problem Enable heuristics to quickly detect an infeasible problemfile print level Verbosity level for output filehalt on ampl error Exit with message on evaluation errorhessian approximation Can enable Quasi-Newton approximation of hessianhonor original bounds If no solution might slightly violate boundslinear scaling on demand Enables heuristic for scaling only when seems requiredlinear solver Linear solver to be used for step calculationlinear system scaling Method for scaling the linear systemsma27 pivtol Pivot tolerance for the linear solver MA27ma27 pivtolmax Maximal pivot tolerance for the linear solver MA27ma57 pivtol Pivot tolerance for the linear solver MA57ma57 pivtolmax Maximal pivot tolerance for the linear solver MA57

22

Table 22 IPOPT directives for AMPL

Option Description

max cpu time CPU time limitmax iter Maximum number of iterations

max refinement stepsMaximal number of iterative refinement steps per linearsystem solve

max soc Maximal number of second order correction trial stepsmaxit Maximum number of iterations (same as max iter)

min refinement stepsMinimum number of iterative refinement steps per linearsystem solve

mu init Initial value for the barrier parametermu max Maximal value for barrier parameter for adaptive strategymu oracle Oracle for a new barrier parameter in the adaptive strategymu strategy Update strategy for barrier parameternlp scaling max gradient Maximum gradient after scalingnlp scaling method Select the technique used for scaling the NLPobj scaling factor Scaling factor for the objective functionoption file name File name of options file (default ipoptopt)outlev Verbosity level (same as print level)output file File name of an output file (leave unset for no file output)pardiso matching strategy Matching strategy for linear solver Pardisopardiso out of core power Enables out-of-core version of linear solver Pardisoprint level Verbosity levelprint options documentation Print all available options (for ipoptopt)print user options Toggle printing of user optionsrequired infeasibility reduction Required infeasibility reduction in restoration phaseslack bound frac Desired minimal relative distance of initial slack to boundslack bound push Desired minimal absolute distance of initial slack to boundtol Desired convergence tolerance (relative)

wantsol

solution report without -AMPL sum of1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

warm start bound pushEnables to specify how much should variables should bepushed inside the feasible region

warm start init point Enables to specify bound multiplier values

warm start mult bound pushEnables to specify how much should bound multipliersshould be pushed inside the feasible region

watchdog shortened iter trigger Trigger counter for watchdog procedurewsmp num threads Number of threads to be used in WSMPwsmp pivtol Pivot tolerance for the linear solver WSMPwsmp pivtolmax Maximum pivot tolerance for the linear solver WSMPwsmp scaling Determines how the matrix is scaled by WSMP

23

24

23 KNITRO

ldquoKNITRO is an optimization software library for finding solutions of both continuous (smo-oth) optimization models (with or without constraints) as well as discrete optimization modelswith integer or binary variables (ie mixed integer programs) KNITRO is primarily designedfor finding local optimal solutions of large-scale continuous nonlinear problemsrdquo The KNITROlibrary can be found in httpwwwartelyscom7

231 Basic information

KNITRO is a software package for solving smooth optimization problems with or withoutconstraints

Developed at Ziena Optimization

Operating system(s) KNITRO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) KNITRO is written C C++ Fortran and Java

Programming interfaces(s) Fortan CC++ Java and Microsoft Excel

Modeling language interface(s) KNITRO offers a total of five interfaces Matlab AMPL Math-ematica AIMMS GAMS and MPL

232 Installing KNITRO on Linux

How to get a license

KNITRO is developed by Ziena Optimization LLC and marketed and supported by ArtelysThe first step is to request a license by selecting the download tab8 Along with the academicand commercial trial versions there is a student version limited in problem size (300 variablesand 300 constraints) available for 6 months Each license is personalised hence among otherpersonal information you should inform about

bull the operating system (OS)

bull the OS bit architecture

bull the Machine ID of the computer on which KNITRO will runDownload extract an script available bellow the Machine ID field

After reading and accepting the End User License Agreement the download of the KNITROwill be available and you will get a license key on your email Then you should put the licensefile whose name begins with ziena lic in your $HOME directory or in the $HOMEzienadirectory

How to install

The KNITRO software package for Unix is delivered as a gzipped tar file After downloadingKNITRO you need to unpack it Type in a Terminal window

gunzip knitro-8x-platformnametargz

tar -xvf knitro-8x-platformnametar

sudo mv knitro-8x-z opt

7httpwwwartelyscomindexphppage=knitroamphl=en_EN8httpwwwartelyscomindexphppage=knitroamphl=en_ENdownloads-tab

25

This will create a directory named knitro-8x-z in your opt folder

In order to run KNITRO binary or executable files from anywhere on your Linux computerit is necessary to set several environment variables In particular you must update the PATH

environment variable so that it indicates the location of the knitroampl directory You mustalso update the LD LIBRARY PATH environment variable so that it indicates the location of theKNITRO lib directory Setting the PATH and LD LIBRARY PATH environment variables on a Linuxsystem can be done as follows

echo rsquoexport PATH=$PATHoptknitro-8x-zrsquo gtgt $HOMEbashrcecho rsquoexport PATH=$PATHoptknitro-8x-zknitroamplrsquo gtgt $HOMEbashrcecho rsquoexport LD LIBRARY PATH=$LD LIBRARY PATHoptknitro-8x-zlibrsquo gtgt $HOMEbashrc

Each Matlab release expects the KNITRO library file on Linux to have a specific name There-fore to use KNITRO 8x or later with Matlab release R2008a through 2011b you must createa symbolic link to the expected name Open a Terminal window and in the KNITRO librarydirectory issue the command

bull for Matlab releases R2008a R2008b or R2009aln -s libknitroso800 libknitroso5

bull for Matlab releases R2009b R2010a or R2010bln -s libknitroso800 libknitroso6

bull for Matlab releases R2011a R2011b or R2012aln -s libknitroso800 libknitroso7

Documentation

More informations can be found in the following documentation9

KNITRO 80 User Manual Knitro80 UserManualpdfVersion December 2011 (816 KB)

Specific KNITROMatlab interface documentation can be found on the Matlab OptimizationToolbox webpages10

233 Getting started with KNITRO and AMPL

option solver knitroampl

option knitro options rsquooption1=value1 option2=value2rsquo

solve

9httpwwwartelyscomuploadspdfsKnitro80_UserManualpdf10httpwwwmathworkscomaccesshelpdeskhelptoolboxoptim

26

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 22: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

Table 21 IPOPT directives for AMPL

Option Description

acceptable compl inf tol Acceptance threshold for the complementarity conditionsacceptable constr viol tol Acceptance threshold for the constraint violationacceptable dual inf tol Acceptance threshold for the dual infeasibilityacceptable tol Acceptable convergence tolerance (relative)alpha for y Step size for constraint multipliersbound frac Desired minimal relative distance of initial point to boundbound mult init val Initial value for the bound multipliersbound push Desired minimal absolute distance of initial point to boundbound relax factor Factor for initial relaxation of the boundscompl inf tol Acceptance threshold for the complementarity conditionsconstr mult init max Maximal allowed least-square guess of constraint multipliersconstr viol tol Desired threshold for the constraint violationdiverging iterates tol Threshold for maximal value of primal iteratesdual inf tol Desired threshold for the dual infeasibilityexpect infeasible problem Enable heuristics to quickly detect an infeasible problemfile print level Verbosity level for output filehalt on ampl error Exit with message on evaluation errorhessian approximation Can enable Quasi-Newton approximation of hessianhonor original bounds If no solution might slightly violate boundslinear scaling on demand Enables heuristic for scaling only when seems requiredlinear solver Linear solver to be used for step calculationlinear system scaling Method for scaling the linear systemsma27 pivtol Pivot tolerance for the linear solver MA27ma27 pivtolmax Maximal pivot tolerance for the linear solver MA27ma57 pivtol Pivot tolerance for the linear solver MA57ma57 pivtolmax Maximal pivot tolerance for the linear solver MA57

22

Table 22 IPOPT directives for AMPL

Option Description

max cpu time CPU time limitmax iter Maximum number of iterations

max refinement stepsMaximal number of iterative refinement steps per linearsystem solve

max soc Maximal number of second order correction trial stepsmaxit Maximum number of iterations (same as max iter)

min refinement stepsMinimum number of iterative refinement steps per linearsystem solve

mu init Initial value for the barrier parametermu max Maximal value for barrier parameter for adaptive strategymu oracle Oracle for a new barrier parameter in the adaptive strategymu strategy Update strategy for barrier parameternlp scaling max gradient Maximum gradient after scalingnlp scaling method Select the technique used for scaling the NLPobj scaling factor Scaling factor for the objective functionoption file name File name of options file (default ipoptopt)outlev Verbosity level (same as print level)output file File name of an output file (leave unset for no file output)pardiso matching strategy Matching strategy for linear solver Pardisopardiso out of core power Enables out-of-core version of linear solver Pardisoprint level Verbosity levelprint options documentation Print all available options (for ipoptopt)print user options Toggle printing of user optionsrequired infeasibility reduction Required infeasibility reduction in restoration phaseslack bound frac Desired minimal relative distance of initial slack to boundslack bound push Desired minimal absolute distance of initial slack to boundtol Desired convergence tolerance (relative)

wantsol

solution report without -AMPL sum of1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

warm start bound pushEnables to specify how much should variables should bepushed inside the feasible region

warm start init point Enables to specify bound multiplier values

warm start mult bound pushEnables to specify how much should bound multipliersshould be pushed inside the feasible region

watchdog shortened iter trigger Trigger counter for watchdog procedurewsmp num threads Number of threads to be used in WSMPwsmp pivtol Pivot tolerance for the linear solver WSMPwsmp pivtolmax Maximum pivot tolerance for the linear solver WSMPwsmp scaling Determines how the matrix is scaled by WSMP

23

24

23 KNITRO

ldquoKNITRO is an optimization software library for finding solutions of both continuous (smo-oth) optimization models (with or without constraints) as well as discrete optimization modelswith integer or binary variables (ie mixed integer programs) KNITRO is primarily designedfor finding local optimal solutions of large-scale continuous nonlinear problemsrdquo The KNITROlibrary can be found in httpwwwartelyscom7

231 Basic information

KNITRO is a software package for solving smooth optimization problems with or withoutconstraints

Developed at Ziena Optimization

Operating system(s) KNITRO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) KNITRO is written C C++ Fortran and Java

Programming interfaces(s) Fortan CC++ Java and Microsoft Excel

Modeling language interface(s) KNITRO offers a total of five interfaces Matlab AMPL Math-ematica AIMMS GAMS and MPL

232 Installing KNITRO on Linux

How to get a license

KNITRO is developed by Ziena Optimization LLC and marketed and supported by ArtelysThe first step is to request a license by selecting the download tab8 Along with the academicand commercial trial versions there is a student version limited in problem size (300 variablesand 300 constraints) available for 6 months Each license is personalised hence among otherpersonal information you should inform about

bull the operating system (OS)

bull the OS bit architecture

bull the Machine ID of the computer on which KNITRO will runDownload extract an script available bellow the Machine ID field

After reading and accepting the End User License Agreement the download of the KNITROwill be available and you will get a license key on your email Then you should put the licensefile whose name begins with ziena lic in your $HOME directory or in the $HOMEzienadirectory

How to install

The KNITRO software package for Unix is delivered as a gzipped tar file After downloadingKNITRO you need to unpack it Type in a Terminal window

gunzip knitro-8x-platformnametargz

tar -xvf knitro-8x-platformnametar

sudo mv knitro-8x-z opt

7httpwwwartelyscomindexphppage=knitroamphl=en_EN8httpwwwartelyscomindexphppage=knitroamphl=en_ENdownloads-tab

25

This will create a directory named knitro-8x-z in your opt folder

In order to run KNITRO binary or executable files from anywhere on your Linux computerit is necessary to set several environment variables In particular you must update the PATH

environment variable so that it indicates the location of the knitroampl directory You mustalso update the LD LIBRARY PATH environment variable so that it indicates the location of theKNITRO lib directory Setting the PATH and LD LIBRARY PATH environment variables on a Linuxsystem can be done as follows

echo rsquoexport PATH=$PATHoptknitro-8x-zrsquo gtgt $HOMEbashrcecho rsquoexport PATH=$PATHoptknitro-8x-zknitroamplrsquo gtgt $HOMEbashrcecho rsquoexport LD LIBRARY PATH=$LD LIBRARY PATHoptknitro-8x-zlibrsquo gtgt $HOMEbashrc

Each Matlab release expects the KNITRO library file on Linux to have a specific name There-fore to use KNITRO 8x or later with Matlab release R2008a through 2011b you must createa symbolic link to the expected name Open a Terminal window and in the KNITRO librarydirectory issue the command

bull for Matlab releases R2008a R2008b or R2009aln -s libknitroso800 libknitroso5

bull for Matlab releases R2009b R2010a or R2010bln -s libknitroso800 libknitroso6

bull for Matlab releases R2011a R2011b or R2012aln -s libknitroso800 libknitroso7

Documentation

More informations can be found in the following documentation9

KNITRO 80 User Manual Knitro80 UserManualpdfVersion December 2011 (816 KB)

Specific KNITROMatlab interface documentation can be found on the Matlab OptimizationToolbox webpages10

233 Getting started with KNITRO and AMPL

option solver knitroampl

option knitro options rsquooption1=value1 option2=value2rsquo

solve

9httpwwwartelyscomuploadspdfsKnitro80_UserManualpdf10httpwwwmathworkscomaccesshelpdeskhelptoolboxoptim

26

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 23: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

Table 22 IPOPT directives for AMPL

Option Description

max cpu time CPU time limitmax iter Maximum number of iterations

max refinement stepsMaximal number of iterative refinement steps per linearsystem solve

max soc Maximal number of second order correction trial stepsmaxit Maximum number of iterations (same as max iter)

min refinement stepsMinimum number of iterative refinement steps per linearsystem solve

mu init Initial value for the barrier parametermu max Maximal value for barrier parameter for adaptive strategymu oracle Oracle for a new barrier parameter in the adaptive strategymu strategy Update strategy for barrier parameternlp scaling max gradient Maximum gradient after scalingnlp scaling method Select the technique used for scaling the NLPobj scaling factor Scaling factor for the objective functionoption file name File name of options file (default ipoptopt)outlev Verbosity level (same as print level)output file File name of an output file (leave unset for no file output)pardiso matching strategy Matching strategy for linear solver Pardisopardiso out of core power Enables out-of-core version of linear solver Pardisoprint level Verbosity levelprint options documentation Print all available options (for ipoptopt)print user options Toggle printing of user optionsrequired infeasibility reduction Required infeasibility reduction in restoration phaseslack bound frac Desired minimal relative distance of initial slack to boundslack bound push Desired minimal absolute distance of initial slack to boundtol Desired convergence tolerance (relative)

wantsol

solution report without -AMPL sum of1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

warm start bound pushEnables to specify how much should variables should bepushed inside the feasible region

warm start init point Enables to specify bound multiplier values

warm start mult bound pushEnables to specify how much should bound multipliersshould be pushed inside the feasible region

watchdog shortened iter trigger Trigger counter for watchdog procedurewsmp num threads Number of threads to be used in WSMPwsmp pivtol Pivot tolerance for the linear solver WSMPwsmp pivtolmax Maximum pivot tolerance for the linear solver WSMPwsmp scaling Determines how the matrix is scaled by WSMP

23

24

23 KNITRO

ldquoKNITRO is an optimization software library for finding solutions of both continuous (smo-oth) optimization models (with or without constraints) as well as discrete optimization modelswith integer or binary variables (ie mixed integer programs) KNITRO is primarily designedfor finding local optimal solutions of large-scale continuous nonlinear problemsrdquo The KNITROlibrary can be found in httpwwwartelyscom7

231 Basic information

KNITRO is a software package for solving smooth optimization problems with or withoutconstraints

Developed at Ziena Optimization

Operating system(s) KNITRO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) KNITRO is written C C++ Fortran and Java

Programming interfaces(s) Fortan CC++ Java and Microsoft Excel

Modeling language interface(s) KNITRO offers a total of five interfaces Matlab AMPL Math-ematica AIMMS GAMS and MPL

232 Installing KNITRO on Linux

How to get a license

KNITRO is developed by Ziena Optimization LLC and marketed and supported by ArtelysThe first step is to request a license by selecting the download tab8 Along with the academicand commercial trial versions there is a student version limited in problem size (300 variablesand 300 constraints) available for 6 months Each license is personalised hence among otherpersonal information you should inform about

bull the operating system (OS)

bull the OS bit architecture

bull the Machine ID of the computer on which KNITRO will runDownload extract an script available bellow the Machine ID field

After reading and accepting the End User License Agreement the download of the KNITROwill be available and you will get a license key on your email Then you should put the licensefile whose name begins with ziena lic in your $HOME directory or in the $HOMEzienadirectory

How to install

The KNITRO software package for Unix is delivered as a gzipped tar file After downloadingKNITRO you need to unpack it Type in a Terminal window

gunzip knitro-8x-platformnametargz

tar -xvf knitro-8x-platformnametar

sudo mv knitro-8x-z opt

7httpwwwartelyscomindexphppage=knitroamphl=en_EN8httpwwwartelyscomindexphppage=knitroamphl=en_ENdownloads-tab

25

This will create a directory named knitro-8x-z in your opt folder

In order to run KNITRO binary or executable files from anywhere on your Linux computerit is necessary to set several environment variables In particular you must update the PATH

environment variable so that it indicates the location of the knitroampl directory You mustalso update the LD LIBRARY PATH environment variable so that it indicates the location of theKNITRO lib directory Setting the PATH and LD LIBRARY PATH environment variables on a Linuxsystem can be done as follows

echo rsquoexport PATH=$PATHoptknitro-8x-zrsquo gtgt $HOMEbashrcecho rsquoexport PATH=$PATHoptknitro-8x-zknitroamplrsquo gtgt $HOMEbashrcecho rsquoexport LD LIBRARY PATH=$LD LIBRARY PATHoptknitro-8x-zlibrsquo gtgt $HOMEbashrc

Each Matlab release expects the KNITRO library file on Linux to have a specific name There-fore to use KNITRO 8x or later with Matlab release R2008a through 2011b you must createa symbolic link to the expected name Open a Terminal window and in the KNITRO librarydirectory issue the command

bull for Matlab releases R2008a R2008b or R2009aln -s libknitroso800 libknitroso5

bull for Matlab releases R2009b R2010a or R2010bln -s libknitroso800 libknitroso6

bull for Matlab releases R2011a R2011b or R2012aln -s libknitroso800 libknitroso7

Documentation

More informations can be found in the following documentation9

KNITRO 80 User Manual Knitro80 UserManualpdfVersion December 2011 (816 KB)

Specific KNITROMatlab interface documentation can be found on the Matlab OptimizationToolbox webpages10

233 Getting started with KNITRO and AMPL

option solver knitroampl

option knitro options rsquooption1=value1 option2=value2rsquo

solve

9httpwwwartelyscomuploadspdfsKnitro80_UserManualpdf10httpwwwmathworkscomaccesshelpdeskhelptoolboxoptim

26

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 24: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

24

23 KNITRO

ldquoKNITRO is an optimization software library for finding solutions of both continuous (smo-oth) optimization models (with or without constraints) as well as discrete optimization modelswith integer or binary variables (ie mixed integer programs) KNITRO is primarily designedfor finding local optimal solutions of large-scale continuous nonlinear problemsrdquo The KNITROlibrary can be found in httpwwwartelyscom7

231 Basic information

KNITRO is a software package for solving smooth optimization problems with or withoutconstraints

Developed at Ziena Optimization

Operating system(s) KNITRO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) KNITRO is written C C++ Fortran and Java

Programming interfaces(s) Fortan CC++ Java and Microsoft Excel

Modeling language interface(s) KNITRO offers a total of five interfaces Matlab AMPL Math-ematica AIMMS GAMS and MPL

232 Installing KNITRO on Linux

How to get a license

KNITRO is developed by Ziena Optimization LLC and marketed and supported by ArtelysThe first step is to request a license by selecting the download tab8 Along with the academicand commercial trial versions there is a student version limited in problem size (300 variablesand 300 constraints) available for 6 months Each license is personalised hence among otherpersonal information you should inform about

bull the operating system (OS)

bull the OS bit architecture

bull the Machine ID of the computer on which KNITRO will runDownload extract an script available bellow the Machine ID field

After reading and accepting the End User License Agreement the download of the KNITROwill be available and you will get a license key on your email Then you should put the licensefile whose name begins with ziena lic in your $HOME directory or in the $HOMEzienadirectory

How to install

The KNITRO software package for Unix is delivered as a gzipped tar file After downloadingKNITRO you need to unpack it Type in a Terminal window

gunzip knitro-8x-platformnametargz

tar -xvf knitro-8x-platformnametar

sudo mv knitro-8x-z opt

7httpwwwartelyscomindexphppage=knitroamphl=en_EN8httpwwwartelyscomindexphppage=knitroamphl=en_ENdownloads-tab

25

This will create a directory named knitro-8x-z in your opt folder

In order to run KNITRO binary or executable files from anywhere on your Linux computerit is necessary to set several environment variables In particular you must update the PATH

environment variable so that it indicates the location of the knitroampl directory You mustalso update the LD LIBRARY PATH environment variable so that it indicates the location of theKNITRO lib directory Setting the PATH and LD LIBRARY PATH environment variables on a Linuxsystem can be done as follows

echo rsquoexport PATH=$PATHoptknitro-8x-zrsquo gtgt $HOMEbashrcecho rsquoexport PATH=$PATHoptknitro-8x-zknitroamplrsquo gtgt $HOMEbashrcecho rsquoexport LD LIBRARY PATH=$LD LIBRARY PATHoptknitro-8x-zlibrsquo gtgt $HOMEbashrc

Each Matlab release expects the KNITRO library file on Linux to have a specific name There-fore to use KNITRO 8x or later with Matlab release R2008a through 2011b you must createa symbolic link to the expected name Open a Terminal window and in the KNITRO librarydirectory issue the command

bull for Matlab releases R2008a R2008b or R2009aln -s libknitroso800 libknitroso5

bull for Matlab releases R2009b R2010a or R2010bln -s libknitroso800 libknitroso6

bull for Matlab releases R2011a R2011b or R2012aln -s libknitroso800 libknitroso7

Documentation

More informations can be found in the following documentation9

KNITRO 80 User Manual Knitro80 UserManualpdfVersion December 2011 (816 KB)

Specific KNITROMatlab interface documentation can be found on the Matlab OptimizationToolbox webpages10

233 Getting started with KNITRO and AMPL

option solver knitroampl

option knitro options rsquooption1=value1 option2=value2rsquo

solve

9httpwwwartelyscomuploadspdfsKnitro80_UserManualpdf10httpwwwmathworkscomaccesshelpdeskhelptoolboxoptim

26

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 25: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

23 KNITRO

ldquoKNITRO is an optimization software library for finding solutions of both continuous (smo-oth) optimization models (with or without constraints) as well as discrete optimization modelswith integer or binary variables (ie mixed integer programs) KNITRO is primarily designedfor finding local optimal solutions of large-scale continuous nonlinear problemsrdquo The KNITROlibrary can be found in httpwwwartelyscom7

231 Basic information

KNITRO is a software package for solving smooth optimization problems with or withoutconstraints

Developed at Ziena Optimization

Operating system(s) KNITRO can be used on Linux Unix Mac OS X and Microsoft Windows

Code language(s) KNITRO is written C C++ Fortran and Java

Programming interfaces(s) Fortan CC++ Java and Microsoft Excel

Modeling language interface(s) KNITRO offers a total of five interfaces Matlab AMPL Math-ematica AIMMS GAMS and MPL

232 Installing KNITRO on Linux

How to get a license

KNITRO is developed by Ziena Optimization LLC and marketed and supported by ArtelysThe first step is to request a license by selecting the download tab8 Along with the academicand commercial trial versions there is a student version limited in problem size (300 variablesand 300 constraints) available for 6 months Each license is personalised hence among otherpersonal information you should inform about

bull the operating system (OS)

bull the OS bit architecture

bull the Machine ID of the computer on which KNITRO will runDownload extract an script available bellow the Machine ID field

After reading and accepting the End User License Agreement the download of the KNITROwill be available and you will get a license key on your email Then you should put the licensefile whose name begins with ziena lic in your $HOME directory or in the $HOMEzienadirectory

How to install

The KNITRO software package for Unix is delivered as a gzipped tar file After downloadingKNITRO you need to unpack it Type in a Terminal window

gunzip knitro-8x-platformnametargz

tar -xvf knitro-8x-platformnametar

sudo mv knitro-8x-z opt

7httpwwwartelyscomindexphppage=knitroamphl=en_EN8httpwwwartelyscomindexphppage=knitroamphl=en_ENdownloads-tab

25

This will create a directory named knitro-8x-z in your opt folder

In order to run KNITRO binary or executable files from anywhere on your Linux computerit is necessary to set several environment variables In particular you must update the PATH

environment variable so that it indicates the location of the knitroampl directory You mustalso update the LD LIBRARY PATH environment variable so that it indicates the location of theKNITRO lib directory Setting the PATH and LD LIBRARY PATH environment variables on a Linuxsystem can be done as follows

echo rsquoexport PATH=$PATHoptknitro-8x-zrsquo gtgt $HOMEbashrcecho rsquoexport PATH=$PATHoptknitro-8x-zknitroamplrsquo gtgt $HOMEbashrcecho rsquoexport LD LIBRARY PATH=$LD LIBRARY PATHoptknitro-8x-zlibrsquo gtgt $HOMEbashrc

Each Matlab release expects the KNITRO library file on Linux to have a specific name There-fore to use KNITRO 8x or later with Matlab release R2008a through 2011b you must createa symbolic link to the expected name Open a Terminal window and in the KNITRO librarydirectory issue the command

bull for Matlab releases R2008a R2008b or R2009aln -s libknitroso800 libknitroso5

bull for Matlab releases R2009b R2010a or R2010bln -s libknitroso800 libknitroso6

bull for Matlab releases R2011a R2011b or R2012aln -s libknitroso800 libknitroso7

Documentation

More informations can be found in the following documentation9

KNITRO 80 User Manual Knitro80 UserManualpdfVersion December 2011 (816 KB)

Specific KNITROMatlab interface documentation can be found on the Matlab OptimizationToolbox webpages10

233 Getting started with KNITRO and AMPL

option solver knitroampl

option knitro options rsquooption1=value1 option2=value2rsquo

solve

9httpwwwartelyscomuploadspdfsKnitro80_UserManualpdf10httpwwwmathworkscomaccesshelpdeskhelptoolboxoptim

26

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 26: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

This will create a directory named knitro-8x-z in your opt folder

In order to run KNITRO binary or executable files from anywhere on your Linux computerit is necessary to set several environment variables In particular you must update the PATH

environment variable so that it indicates the location of the knitroampl directory You mustalso update the LD LIBRARY PATH environment variable so that it indicates the location of theKNITRO lib directory Setting the PATH and LD LIBRARY PATH environment variables on a Linuxsystem can be done as follows

echo rsquoexport PATH=$PATHoptknitro-8x-zrsquo gtgt $HOMEbashrcecho rsquoexport PATH=$PATHoptknitro-8x-zknitroamplrsquo gtgt $HOMEbashrcecho rsquoexport LD LIBRARY PATH=$LD LIBRARY PATHoptknitro-8x-zlibrsquo gtgt $HOMEbashrc

Each Matlab release expects the KNITRO library file on Linux to have a specific name There-fore to use KNITRO 8x or later with Matlab release R2008a through 2011b you must createa symbolic link to the expected name Open a Terminal window and in the KNITRO librarydirectory issue the command

bull for Matlab releases R2008a R2008b or R2009aln -s libknitroso800 libknitroso5

bull for Matlab releases R2009b R2010a or R2010bln -s libknitroso800 libknitroso6

bull for Matlab releases R2011a R2011b or R2012aln -s libknitroso800 libknitroso7

Documentation

More informations can be found in the following documentation9

KNITRO 80 User Manual Knitro80 UserManualpdfVersion December 2011 (816 KB)

Specific KNITROMatlab interface documentation can be found on the Matlab OptimizationToolbox webpages10

233 Getting started with KNITRO and AMPL

option solver knitroampl

option knitro options rsquooption1=value1 option2=value2rsquo

solve

9httpwwwartelyscomuploadspdfsKnitro80_UserManualpdf10httpwwwmathworkscomaccesshelpdeskhelptoolboxoptim

26

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 27: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

Table 23 KNITRO directives for AMPL (1)

Option Description

alg Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)algorithm Algorithm (0=auto 1=direct 2=cg 3=active 5=multi)bar directinterval Frequency for trying to force direct stepsbar feasible Emphasize feasibilitybar feasmodetol Tolerance for entering stay feasible modebar initmu Initial value for barrier parameterbar initpt Barrier initial point strategybar maxbacktrack Maximum number of linesearch backtracksbar maxcrossit Maximum number of crossover iterationsbar maxrefactor Maximum number of KKT refactorizations allowedbar murule Rule for updating the barrier parameterbar penaltycons Apply penalty method to constraintsbar penaltyrule Rule for updating the penalty parameterbar switchrule Rule for barrier switching algblasoption Which BLASLAPACK library to useblasoptionlib Name of dynamic BLASLAPACK librarycplexlibname Name of dynamic CPLEX librarydebug Debugging level (0=none 1=problem 2=execution)delta Initial trust region radiusfeastol Feasibility stopping tolerancefeastol abs Absolute feasibility tolerancefeastolabs Absolute feasibility tolerancegradopt Gradient computation methodhessopt Hessian computation methodhonorbnds Enforce satisfaction of the boundsinfeastol Infeasibility stopping tolerancelinsolver Which linear solver to uselmsize Number of limited-memory pairs stored for LBFGSlpsolver LP solver used by Active Set algorithmma maxtime cpu Maximum CPU time when rsquoalg=multirsquo in secondsma maxtime real Maximum real time when rsquoalg=multirsquo in secondsma outsub Enable subproblem output when rsquoalg=multirsquoma terminate Termination condition when option rsquoalg=multirsquomaxcgit Maximum number of conjugate gradient iterationsmaxit Maximum number of iterationsmaxtime cpu Maximum CPU time in seconds per start pointmaxtime real Maximum real time in seconds per start pointmip branchrule MIP branching rulemip debug MIP debugging level (0=none 1=all)mip gub branch Branch on GUBs (0=no 1=yes)mip heuristic MIP heuristic searchmip heuristic maxit MIP heuristic iteration limitmip implications Add logical implications (0=no 1=yes)mip integer tol Threshold for deciding integralitymip integral gap abs Absolute integrality gap stop tolerancemip integral gap rel Relative integrality gap stop tolerancemip knapsack Add knapsack cuts (0=no 1=ineqs 2=ineqs+eqs)mip lpalg LP subproblem algorithm

27

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 28: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

Table 24 KNITRO directives for AMPL (2)

Option Description

mip maxnodes Maximum nodes exploredmip maxsolves Maximum subproblem solvesmip maxtime cpu Maximum CPU time in seconds for MIPmip maxtime real Maximum real in seconds time for MIPmip method MIP method (0=auto 1=BB 2=HQG)mip outinterval MIP output intervalmip outlevel MIP output levelmip outsub Enable MIP subproblem outputmip pseudoinit Pseudo-cost initializationmip rootalg Root node relaxation algorithmmip rounding MIP rounding rulemip selectrule MIP node selection rulemip strong candlim Strong branching candidate limitmip strong level Strong branching tree level limitmip strong maxit Strong branching iteration limitmip terminate Termination condition for MIPms enable Enable multistartms maxbndrange Maximum unbounded variable range for multistartms maxsolves Maximum KNITRO solves for multistartms maxtime cpu Maximum CPU time for multistart in secondsms maxtime real Maximum real time for multistart in secondsms num to save Feasible points to save from multistartms outsub Enable subproblem output for parallel multistartms savetol Tol for feasible points being equalms seed Seed for multistart random generatorms startptrange Maximum variable range for multistartms terminate Termination condition for multistartnewpoint Use newpoint featureobjno objective number 0 = none 1 = first (default)

2 = second (if nobjs gt 1) etcobjrange Objective rangeobjrep Whether to replace

minimize obj vwithminimize obj f(x)when variable v appears linearlyin exactly one constraint of the formst c v ge f (x)orst c v = f (x)Possible objrep values0 = no1 = yes for v ge f (x) (default)2 = yes for v = f (x)3 = yes in both cases

28

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 29: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

Table 25 KNITRO directives for AMPL (3)

Option Description

opttol Optimality stopping toleranceopttol abs Absolute optimality toleranceopttolabs Absolute optimality toleranceoutappend Append to output files (0=no 1=yes)outdir Directory for output filesoutlev Control printing leveloutmode Where to direct output (0=screen 1=file 2=both)par blasnumthreads Number of parallel threads for BLASpar numthreads Number of parallel threadspivot Initial pivot tolerancepresolve KNITRO presolver levelpresolve dbg KNITRO presolver debugging levelpresolve tol KNITRO presolver tolerancerelax whether to ignore integrality 0 (default) = no 1 = yesscale Automatic scaling optionsoc Second order correction optionstiming Whether to report problem IO and solve times

0 (default) = no1 = yes on stdout

version Report software versionwantsol solution report without -AMPL sum of

1 write sol file2 print primal variable values4 print dual variable values8 do not print solution message

xpresslibname Name of dynamic Xpress libraryxtol Stepsize stopping tolerance

29

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 30: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

30

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 31: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

24 WORHP ndash WORHP Optimises Really Huge Problems

ldquoWORHP is a software library for mathematical nonlinear optimization suitable for solvingproblems with thousands or even millions of variables and constraintsrdquo The WORHP librarycan be found in httpwwwworhpde11

241 Basic information

Developed under the direction of Christof Buskens with Matthias Gerdts

Operating system(s) WORHP runs on Linux Unix Mac OS X and Microsoft Windows

Code language(s) WORHP is written Fortan and C

Modeling language interface(s) WORHP offers a total of nine interfaces 3 for Fortran 3 forCC++ Matlab ASTOS and AMPL for different programming languages and communi-cation paradigms

242 Installing WORHP on Linux

How to get a license

The first step is to check the availability of WORHP for your favourite platform WORHP isavailable for several platforms although some restrictions apply

bull Minimum supported gcc version is 43

bull Windows Visual Studio builds are only available for 32-bit systems(64-bit builds should be available by mid-2012)

bull The MATLAB interface for Windows is available for 32-bit systems only

bull Builds of the MATLAB interface may have compatibility issues between different MATLABversions The developers can generally supply builds for a number of different MATLABversions upon request

bull Builds for Apple Macs are subject to platform availability

Then you should request a license by logging in the WORHP website12 Licenses are avail-able for academic and commercial users You should fillndashup the form available in Get WORHPgt For Academic gt Request a license

Each license is personalised hence among other personal information you should informabout

bull the operating system (OS)

bull the OS version(in a Terminal window run lsb release -cs)

bull the OS bit architecture

bull the compiler family (C or Fortran) to be used(in a Terminal window run gfortran --version or gcc --version)

bull the MAC address of the computer on which WORHP will run11httpwwwworhpde12httpwwwworhpde

31

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 32: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

Documentation

The following documentation13 is available

WORHP User Manual Worhp User ManualpdfDocumentation of WORHP from first steps to usage of different interfacesVersion 2012-03-02 (74998 KB)

WORHP Tutorial Worhp TutorialpdfIntroduction to using WORHP with detailled installation instructionsVersion 2012-03-08 (4236 KB)

WORHP Technical Datasheet Worhp Datasheetpdf7-page datasheet about the key technical properties of WORHPVersion 2011-08-18 (78865 KB)

WORHP Conference Publication Worhp Conference PaperpdfPrepared for 62nd International Astronautical Congress in Cape Town South Africa (re-formatted with generic layout)Version 2011-11-01 (95946 KB)

13httpwwwworhpdecontentpublications

32

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 33: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

25 Other Commercial Packages

251 SOCS ndash Sparse Optimal Control Software

ldquoThe Sparse Optimal Control Family developed by The Boeing Company contains two ad-vanced software packages available separately or togetherrdquoSparse Optimal Control Software (SOCS) is general-purpose software for solving optimal controlproblems Applications include trajectory optimization chemical process control and machinetool path definition The SNOPT library can be found in httpwwwboeingcom14

Developed by The Boeing Company

Operating system(s) SOCS is supported on most UNIX and Windows systems The software issupported on most major platforms with at least 14 decimal digits of precision which onmost systems means double precision

Code language(s) This software and all lower-level support routines are written in ANSI-StandardFORTRAN 77

Interface(s) FORTRAN 77

252 SNOPT ndash Sparse Nonlinear OPTimizer

SNOPT is a software package for solving large-scale optimization problems (linear and nonndashlinear programs) It is especially effective for nonndashlinear problems whose functions and gradi-ents are expensive to evaluate The functions should be smooth but need not be convex TheSNOPT library can be found in httpwwwsbsi-sol-optimizecom15

Developed by Philip Gill Walter Murray and Michael Saunders

Operating system(s) It is intended for any machine with a reasonable amount of memory anda FORTRAN compiler

Code language(s) SNOPT is implemented in FORTRAN 77 and distributed as source code

Interface(s) It may be called from a driver program typically in Fortran C or MATLAB

14httpwwwboeingcomphantomsocs15httpwwwsbsi-sol-optimizecomaspsol_product_snopthtm

33

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 34: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

34

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 35: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

Chapter 3

Project webpage

One of main tasks of this project was to develop and to maintain the project webndashsite makingdocumentation accessible to the project team All documentation that is supporting this projectis available in the project webndashsite1 This web site was written in HTML language and it can beeasily updated

Figure 31 Screenshot of the project webndashsite

1httppaginasfeuppt~fafProjectFCT2009

35

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 36: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

36

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 37: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

Chapter 4

Optimal Control Toolbox

One of tasks to be carried out was to construct a platform to easily solve sequences ofoptimal control problems in a receding horizon fashion in order to implement model predictivecontrollers We took this goal a little further and we thought to develop a Matlab platform thatwould be able to solve optimal control problems using different solvers and evolving severalODE methods as in Figure 41 This would require a huge time investment and we knew fromthe beginning it would be a hard task to finish during this project Still we archived a goodwork that could be the starting point for a new project (See appendix A1)

Figure 41 Screenshot of the Optimal Control Toolbox on MATLAB

37

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 38: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

38

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 39: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

Chapter 5

Applications

Another task of this project was too help to implement and to solve case studies and tests Inthis chapter several problems are presented using some of the interfaces and solvers previouslydiscussed

51 CarndashLike

The Carndashlike problem can be started as

Minimize T

subject to x = u cos(θ)

y = u sen(θ)

θ = u w

umin le u le umax

wmin le w le wmax

y ge yminus k(xminus x)

x(0) = x0 x(T) = x f

y(0) = x0 y(T) = y f

θ(0) = θ0 θ(T) = θ f

Change of variables

39

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 40: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

τ isin [0 1]

t(τ) [0 1]rarr [0 T]

t(0) = 0 t(1) = T

dtdτ

= v v gt 0

dxdτ

=dxdt

dtdτ

= vdxdt

dydτ

=dydt

dtdτ

= vdydt

dτ=

dtdtdτ

= vdθ

dt

MinimizevisinR u1 u2isinU

t(1)

subject todxdτ

= v u cos (θ(τ)) forallτ isin [0 1]

dydτ

= v u sen (θ(τ)) forallτ isin [0 1]

dτ= v u w forallτ isin [0 1]

dtdτ

= v forallτ isin [0 1]

0 le u le 2 forallτ isin [0 1]

minus 07 le w le 07 forallτ isin [0 1]

1 le v le 2radic(xn minus x0)

2 + (yn minus y0)2 forallτ isin [0 1]

y ge yminus k(xminus x)2 forallτ isin [0 1]

Initial conditions

x0 = 0 x f = 10

y0 = 0 y f = 0

θ0 = 0 θ f = 0(xn minus x f

)2+(

yn minus y f

)2+(

θn minus θ f

)2le 05

n = 40 (x y) = (5 1) k = 10

Interface AMPL (see appendix A2)

Solver IPOPT

Solver settingsoption solver ipopt

40

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 41: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

ipopt options max iter=999 acceptable tol=1e 8

Solution v = 47236725474 The problem solved in 324 iterationsTime taken 123 s

Figure 51 Optimal trajectory and controls

41

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 42: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

42

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 43: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

52 Goddard Problem

The Goddad problem can be started as

Maximize m (T)subject to r = v forallt isin [0 T]

v = minus 1r2 +

1m

(Tmaxuminus D(r v)) forallt isin [0 T]

m = minusbTmaxu forallt isin [0 T]

0 le u le 1 forallt isin [0 T]

D(r(middot) v(middot)) ge C forallt isin [0 T]

Initial conditions

r(0) = 1 r(T) = 101

v(0) = 0

m(0) = 1

where D(r v) = Av2ρ(r) ρ(r) = eminusk(rminusr0)

and b = 7 Tmax = 35

A = 310 k = 500 r0 = 1

Interface BOCOP

Solver IPOPT

Solver settingsmax iter=1000

tol=10000000000e-6

mu strategy=adaptive

Solution m = minus6327366times 10minus1

The problem solved in 20 iterationsTime taken 224 s

(a) State variables (b) Control Variables

Figure 52 Optimal trajectories and control

43

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 44: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

53 HIV

For most of the present HIV chemotherapy drugs the state system would be

d Td t

=s

1 + Vminus microTT + rT

(1minus T + Tlowast + Tlowastlowast

Tmax

)minus k1VT

d Tlowast

d t= k1VT minus microTlowastTlowast minus k2Tlowast

d Tlowastlowast

d t= k2Tlowast minus microbTlowastlowast

d Vd t

= (1minus u(t)) NmicrobTlowastlowast minus k1VT minus microVV

where

T(t) concentration of uninfected CD4+ T cellsTlowast(t) concentration of latently infected CD4+ T cellsTlowastlowast(t) concentration of actively infected CD4+ T cellsV(t) concentration of free infectious virus particlesu(t) control rate of chemotherapy 0 le u(t) le 1

u(t) = 1 maximal chemotherapyu(t) = 0 no chemotherapy

The objective function

max

Tfint0

(T(t)minus 1

2Bu(t)2

)d t

Initial conditions parameters and constants

T(0) = 9828

Tlowast(0) = 005155

Tlowastlowast(0) = 6175times 10minus4

V(0) = 007306

Parameters and constants Values

B parameter 100microT death rate of ininfected CD4+ T cell population 002 dminus1

microTlowast death rate of latently infected CD4+ T cell population 02 dminus1

microTlowastlowast death rate of actively infected CD4+ T cell population 024 dminus1

microV death rate of free virus 24 dminus1

k1 rate CD4+ T cells becomes infected by free virus 24times 10minus5 mm3dminus1

k2 rate Tlowast cells convert to actively infected 3times 10minus3 mm3dminus1

r rate of growth for the CD4+ T cell population 003 dminus1

N number of free virus produced by Tlowastlowast cells 1200Tmax maximum CD4+ T cell population level 15times 103 mmminus3

s source term for uninfected CD4+ T cells 10 dminus1mmminus3

Table 51 Parameters and constants

44

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 45: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

Interface ICLOCS (see appendix A3)

Solver IPOPT

Solver settingsoptionstranscription=rsquohermitersquo

optionsderivatives=rsquonumericrsquo

optionshessianFD=rsquocentralrsquo

optionsNLPsolver=rsquoipoptrsquo

optionsipopttol=1e-9

optionsipoptprint level=5

optionsipoptmax iter=200

optionsipoptmu strategy =rsquoadaptiversquo

optionsipopthessian approximation=rsquoexactrsquo

optionsfmincon=optimset

optionsscaling=1

optionsprinttime=1

optionsprintrelative local error=1

optionsprintcost=1

optionsplotstates=1

optionsplotinputs=1

optionsplotmultipliers=1

optionsnodes=1000

optionstau=0

optionsODEsolver=rsquocvodesrsquo

Method=rsquoAdamsrsquo

Solver=rsquoNewtonrsquo

Solution minus49204times 105

The problem solved in 17 iterationsTime taken 1126 s

Figure 53 Treatment period of 500 days starting after 800 days of infection

45

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 46: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

46

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 47: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

Appendix A

Programming code

A1 Optimal Control Toolbox MATLAB Code

1

func t ion OptimalControlToolbox3

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 5

Auxi l iary vars7 ss = get ( 0 rsquo ScreenSize rsquo )

FontSize panel t i t l e = 1 4 9 FontSize axes t i t l e = 1 6

Length panel = 0 1 2 11 Length axes = Length panel + 0 0 7

Button height = 0 0 4 13

15 FIGURE h f i g = f i g u r e ( rsquo P o s i t i o n rsquo ss )

17 s e t ( hf ig rsquo V i s i b l e rsquo rsquo o f f rsquo ) s e t ( hf ig rsquoName rsquo rsquo Optimal Control Toolbox rsquo rsquo NumberTitle rsquo rsquo o f f rsquo )

19 s e t ( hf ig rsquo MenuBar rsquo rsquo none rsquo )

21

FIGURE MENU 23 figmenu = uimenu ( rsquo Label rsquo rsquo Workspace rsquo )

uimenu ( figmenu rsquo Label rsquo rsquoNew Figure rsquo rsquo Cal lback rsquo rsquo f i g u r e rsquo ) 25 uimenu ( figmenu rsquo Label rsquo rsquoODE Solver rsquo rsquo Cal lback rsquo rsquo SolveODEmethod rsquo )

uimenu ( figmenu rsquo Label rsquo rsquo Save rsquo rsquo Cal lback rsquo rsquo save rsquo ) 27 uimenu ( figmenu rsquo Label rsquo rsquo Quit rsquo rsquo Cal lback rsquo rsquo e x i t rsquo rsquo Separator rsquo rsquo on rsquo rsquo A c c e l e r a t o r rsquo rsquoQrsquo )

29

AXES 31 haxes = gca

s e t ( haxes rsquo P o s i t i o n rsquo [ Length axes 1 0 7 5 0 8 ] ) 33 s e t ( get ( haxes rsquo T i t l e rsquo ) rsquo S t r ing rsquo rsquo RESULTS rsquo rsquo FontSize rsquo FontSize axes t i t l e )

35

START PANEL 37 Create the button group

s t a r t buttons = 3 39 b s i ze = ButtonSize ( s t a r t buttons )

s t a r t panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 8 5 Length panel Button height s t a r t buttons] )

41 s e t ( s t a r t panel rsquo T i t l e rsquo rsquoSTART rsquo rsquo FontSize rsquo FontSize panel t i t l e ) Create push buttons in the button group

47

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 48: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

43 s t a r t new = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquoNew Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

s t a r t load = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Load Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

45 s t a r t save = u i c o n t r o l ( rsquo Parent rsquo s t a r t panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo rsquoS t r i n g rsquo rsquo Save Problem rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) rsquo Cal lback rsquo s t a r t c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 47 s e t ( s t a r t save rsquo Enable rsquo rsquo Off rsquo )

49

SOLVER PANEL 51 Create the button group

s o l v e r buttons = 6 53 b s i ze = ButtonSize ( s o l v e r buttons )

s o l v e r panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 6 Length panel Button height s o l v e r buttons ] rsquo SelectionChangeFcn rsquo s o l v e r s e l e c t )

55 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( s o l v e r panel rsquo T i t l e rsquo rsquoSOLVER rsquo rsquo FontSize rsquo FontSize panel t i t l e )

57 Create radio buttons in the button group s o l v e r fmincon = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoFMINCONrsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 59 s o l v e r k n i t r o = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoKNITRO rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) s o l v e r ipopt = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo IPOPT rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 61 s o l v e r i c l o c s = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo ICLOCS rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) s o l v e r worhp = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoWORHPrsquo rsquo P o s i t i o n rsquo bs iz e ( 5 ) ) 63 Create push buttons in the button group to def ine opt ions

s o l v e r opt ions = u i c o n t r o l ( rsquo Parent rsquo s o l v e r panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquonormalized rsquo rsquo S t r i n g rsquo rsquo Options rsquo rsquo P o s i t i o n rsquo b s i ze ( s o l v e r buttons ) )

65 I n i t i a l i z e some button group p r o p e r t i e s s e t ( s o l v e r panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

67 s e t ( s o l v e r options rsquo Enable rsquo rsquo o f f rsquo )

69

ODE METHOD PANEL 71 Create the button group

ODE buttons = 4 73 b s i ze = ButtonSize (ODE buttons )

ODE panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 4 3 Length panel Button height ODE buttons ] ) 75 s e t (ODE panel rsquo V i s i b l e rsquo rsquo Off rsquo )

s e t (ODE panel rsquo T i t l e rsquo rsquoODE Method rsquo rsquo FontSize rsquo FontSize panel t i t l e rsquo SelectionChangeFcn rsquo method select )

77 Create radio buttons in the button group ODE but ton Euler = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Euler Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 79 ODE button Heun = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquoHeun Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) ODE button RK2 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 2nd Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 81 ODE button RK4 = u i c o n t r o l ( rsquo Parent rsquo ODE panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo 4 th Rungeminuskutta Method rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) I n i t i a l i z e some button group p r o p e r t i e s

83 s e t (ODE panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o ns e t (ODE panel rsquo Se lec tedObjec t rsquo ODE but ton Euler ) d e f a u l t s e l e c t i o n

85

87 INITIAL CONDITION PANEL Create the button group

89 i n i t s o l buttons = 1 b s i ze = ButtonSize ( i n i t s o l buttons )

48

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 49: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

91 i n i t s o l panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 7 Length panel Button height i n i t s o l buttons ] )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquo Off rsquo ) 93 Create radio buttons in the button group

b s i ze ( 1 3 ) = 0 4 9 95 i n i t s o l cold = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized

rsquo rsquo S t r i n g rsquo rsquo Cold S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k ) b s i ze ( 1 1 ) = 0 5

97 i n i t s o l hot = u i c o n t r o l ( rsquo Parent rsquo i n i t s o l panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalizedrsquo rsquo S t r i n g rsquo rsquo Hot S t a r t rsquo rsquo P o s i t i o n rsquo bsize rsquo Cal lback rsquo i n i t s o l c a l l b a c k )

I n i t i a l i z e some button group p r o p e r t i e s 99

101 SOLVE BUTTON Create the button group

103 solve buttons = 1 b s i ze = ButtonSize ( so lve buttons )

105 solve panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 3 Length panel Button height ] ) s e t ( so lve panel rsquo V i s i b l e rsquo rsquo Off rsquo )

107 Create radio buttons in the button group solve button = u i c o n t r o l ( rsquo Parent rsquo so lve panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoSOLVE rsquo rsquo P o s i t i o n rsquo bsize rsquo Enable rsquo rsquoOn rsquo ) 109 s e t ( so lve button rsquo Enable rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

111

PLOT PANEL 113 Create the button group

p l o t s buttons = 5 115 b s i ze = ButtonSize ( p l o t s buttons )

p l o t s panel = uibuttongroup ( rsquo P o s i t i o n rsquo [ 0 2 0 3 Length panel Button height p l o t s buttons] )

117 s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquo Off rsquo ) s e t ( p l o t s panel rsquo T i t l e rsquo rsquoPLOT rsquo rsquo FontSize rsquo FontSize panel t i t l e )

119 Create radio buttons in the button group p l o t s s t a t e = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo S t a t e Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 1 ) ) 121 p l o t s c o n t r o l = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo Control Var iab les rsquo rsquo P o s i t i o n rsquo bs iz e ( 2 ) ) p l o t s t r a j e c t = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquo T r a j e c t o r y rsquo rsquo P o s i t i o n rsquo bs iz e ( 3 ) ) 123 p l o t s other = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo Radio rsquo rsquo Units rsquo rsquo normalized rsquo rsquo

S t r i n g rsquo rsquominusminusother optionminusminus rsquo rsquo P o s i t i o n rsquo bs iz e ( 4 ) ) p l o t s button = u i c o n t r o l ( rsquo Parent rsquo p l o t s panel rsquo S t y l e rsquo rsquo PushButton rsquo rsquo Units rsquo rsquo normalized rsquo

rsquo S t r i n g rsquo rsquoPLOT rsquo rsquo P o s i t i o n rsquo b s i ze ( 5 ) rsquo I n t e r r u p t i b l e rsquo rsquoOn rsquo rsquo Cal lback rsquo s i n g l e b u t t o n c a l l b a c k )

125 I n i t i a l i z e some button group p r o p e r t i e s s e t ( p l o t s panel rsquo SelectionChangeFcn rsquo selcbk )

127 s e t ( p l o t s panel rsquo S e l e c t e d O b j e c t rsquo [ ] ) No s e l e c t i o n

129

CONTEXT MENU 131 cmenu = uicontextmenu ( rsquo Parent rsquo hf ig rsquo P o s i t i o n rsquo [ 1 0 2 1 5 ] )

mh1 = uimenu ( cmenu rsquo Label rsquo rsquo Item 1 rsquo rsquo Cal lback rsquo uimenu callback ) 133 mh2 = uimenu ( cmenu rsquo Label rsquo rsquo Item 2 rsquo rsquo Cal lback rsquo uimenu callback )

mh3 = uimenu ( cmenu rsquo Label rsquo rsquo Item 3 rsquo rsquo Cal lback rsquo uimenu callback ) 135 s e t ( hf ig rsquo UIContextMenu rsquo cmenu )

s e t ( haxes rsquo UIContextMenu rsquo cmenu ) 137 s e t ( cmenu rsquo V i s i b l e rsquo rsquo on rsquo )

139 s e t ( hf ig rsquo V i s i b l e rsquo rsquo on rsquo ) end

141

143 SINGLE BUTTONS CALLBACKS

49

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 50: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

func t ion s i n g l e b u t t o n c a l l b a c k ( hObject event )145 HELP

147 globa l s t a r t s o l v e r ODE solve p l o t s

switch lower ( get ( hObject rsquo S t r i n g rsquo ) )149

SOLVE BUTTON CALLBACK151 case rsquo so lve rsquo

s e t ( hObject rsquo S t r i n g rsquo rsquoSOLVED rsquo ) 153 s e t ( hObject rsquo Enable rsquo rsquo Off rsquo )

s e t ( p l o t s panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 155

LOAD PROBLEM BUTTON CALLBACK157 case rsquo p l o t rsquo

159

OTHERWISE161 otherwise

disp ( rsquoUnknown button rsquo )163 end

end165

167 START PANEL CALLBACKS funct ion s t a r t c a l l b a c k ( hObject event )

169 HELP

171 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

173

NEW PROBLEM BUTTON CALLBACK175 case rsquonew problem rsquo

dee 177 s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo )

s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 179 s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 181 s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo )

183 LOAD PROBLEM BUTTON CALLBACKcase rsquo load problem rsquo

185 [ l o a d f i l e load path ] = u i g e t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 187 i f i s e q u a l ( l o a d f i l e 0 ) | | i s e q u a l ( load path 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 189 e l s e

s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) ) 191 s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( load path l o a d f i l e ) )

s e t ( s t a r t save rsquo Enable rsquo rsquoOn rsquo ) 193 s e t ( s o l v e r panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t (ODE panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 195 s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo )

s e t ( i n i t s o l panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 197 load ( f u l l f i l e ( load path l o a d f i l e ) rsquominusmat rsquo )

end 199

SAVE PROBLEM BUTTON CALLBACK201 case rsquo save problem rsquo

i f isempty ( get ( hObject rsquo UserData rsquo ) )203 [ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) )

50

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 51: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

205 e l s e[ s a v e f i l e save path ] = u i p u t f i l e ( rsquo oc t dat rsquo rsquo Data f i l e s ( oct dat ) rsquo

207 rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo

Al l F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) get ( hObject rsquo UserData rsquo ) ) end

209

i f i s e q u a l ( s a v e f i l e 0 ) | | i s e q u a l ( save path 0 )211 disp ( rsquo User s e l e c t e d Cancel rsquo )

e l s e213 s e t ( s t a r t load rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) )

s e t ( s t a r t save rsquo UserData rsquo f u l l f i l e ( save path s a v e f i l e ) ) 215 save ( f u l l f i l e ( save path s a v e f i l e ) rsquominusmat rsquo rsquominusv7 rsquo )

end217

OTHERWISE219 otherwise

disp ( rsquoUnknown button rsquo )221 end

223 end

225

SOLVER PANEL CALLBACKS 227 func t ion s o l v e r s e l e c t ( hObject event )

HELP 229

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 231 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

233 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

235 disp ( rsquo fmincon rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

237

HOT START BUTTON CALLBACK239 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )241 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

243 COLD START BUTTON CALLBACKcase rsquo ipopt rsquo

245 disp ( rsquo ipopt rsquo )s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

247

HOT START BUTTON CALLBACK249 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )251 s e t ( s o l v e r options rsquo Enable rsquo rsquoOn rsquo )

253 OTHERWISEotherwise

255 disp ( rsquoUnknown button rsquo )end

257 end

259

SOLVER PANEL CALLBACKS 261 func t ion odemethod select ( hObject event )

HELP 263

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 265 switch lower ( get ( get ( hObject rsquo S e l e c t e d O b j e c t rsquo ) rsquo S t r i n g rsquo ) )

51

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 52: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

267 FMINCON BUTTON CALLBACKcase rsquo fmincon rsquo

269 disp ( rsquo fmincon rsquo )s e t ( s o l v e r push fmincon rsquo Enable rsquo rsquoOn rsquo )

271

KNITRO BUTTON CALLBACK273 case rsquo k n i t r o rsquo

disp ( rsquo k n i t r o rsquo )275

IPOPT BUTTON CALLBACK277 case rsquo ipopt rsquo

disp ( rsquo ipopt rsquo )279

ICLOCS BUTTON CALLBACK281 case rsquo i c l o c s rsquo

disp ( rsquo i c l o c s rsquo )283

WORHP BUTTON CALLBACK285 case rsquoworhp rsquo

disp ( rsquoworhp rsquo )287

OTHERWISE289 otherwise

disp ( rsquoUnknown button rsquo )291 end

end293

295 INITIAL SOLUTION PANEL CALLBACKS funct ion i n i t s o l c a l l b a c k ( hObject event )

297 HELP

299 globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l switch lower ( get ( hObject rsquo S t r i n g rsquo ) )

301

COLD START BUTTON CALLBACK303 case rsquo cold s t a r t rsquo

305 HOT START BUTTON CALLBACKcase rsquo hot s t a r t rsquo

307 [ i n i t s o l h o t s t a r t f i l e i n i t s o l h o t s t a r t p a t h ] = u i g e t f i l e ( rsquo dat rsquo rsquo Data f i l e s( dat ) rsquo rsquo m f i g mat mdl rsquo rsquoMATLAB F i l e s ( m f ig mat mdl ) rsquo rsquo rsquo rsquo Al l

F i l e s ( ) rsquo get ( hObject rsquo S t r i n g rsquo ) ) 309 i f i s e q u a l ( i n i t s o l h o t s t a r t f i l e 0 ) | | i s e q u a l ( i n i t s o l h o t s t a r t p a t h 0 )

disp ( rsquo User s e l e c t e d Cancel rsquo ) 311 e l s e

s e t ( so lve panel rsquo V i s i b l e rsquo rsquoOn rsquo ) 313 end

315 OTHERWISEotherwise

317 disp ( rsquoUnknown button rsquo )end

319 end

321

CONTEXT MENU CALLBACKS 323 func t ion uimenu callback ( hObject event )

HELP 325

globa l s t a r t s o l v e r ODE solve p l o t s i n i t s o l 327 switch lower ( get ( hObject rsquo Label rsquo ) )

52

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 53: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

329 NEW PROBLEM BUTTON CALLBACKcase rsquo item 1 rsquo

331

333 LOAD PROBLEM BUTTON CALLBACKcase rsquo item 2 rsquo

335

337 SAVE PROBLEM BUTTON CALLBACKcase rsquo item 3 rsquo

339

341 OTHERWISEotherwise

343 disp ( rsquoUnknown option rsquo )end

345

end347

349

func t ion b s i ze = ButtonSize ( buttons )351 HELP

353 bs iz e = zeros ( buttons 4 )

bs iz e ( 1 ) = 0 0 1 355 bs iz e ( 3 ) = 0 9 8

bs iz e ( 4 ) = 1 buttons 357 f o r i = 1 buttons

b s i ze ( i 2 ) = ( buttonsminusi ) buttons 359 end

end

MatlabOCTOptimalControlToolboxm

53

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 54: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

54

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 55: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

A2 CarndashLike AMPL Code

1 optamplampl ( c ) 2012 minus L T Paiva and F ACC Fontes

3

r e s e t 5 s h e l l rdquo c l e a r rdquo

7 PARAMETERS param t f = 1 0

9 param n = 40 param h = t f n

11

param k = 1 0 0 13 param xbar = 5 0

param ybar = 1 0 15 param rfmax = 0 5

param pi = 4 atan ( 1 0 ) 17

param x0 = 0 0 19 param y0 = 0 0

param t h e t a 0 = pi 20 21 param t h e t a 0 = 0 0

param xf = 1 0 0 23 param yf = 0 0

param t h e t a f = minuspi 20 25 param t h e t a f = 0 0

27 param umax = 2 0 param umin = 0 0

29 param wmax = 0 7 param wmin = minuswmax

31 param vmin = 1 0 param vmax = 2 s q r t ( ( xfminusx0 ) ˆ 2+ ( yfminusy0 ) ˆ 2 )

33

param t0 = 0 0 35

STATE VARIABLES 37 var x i in 0 n

var y i in 0 n 39 var t h e t a i in 0 n

var t i in 0 n 41

CONTROL VARIABLES 43 var u i in 0 n

var v 45 var w i in 0 n

47 OBJECTIVE FUNCTION minimize ob j t [ n ]

49

CONSTRAINTS 51

INITIAL VALUES 53 s t ivx x [ 0 ] = x0

s t ivy y [ 0 ] = y0 55 s t i v t h e t a t h e t a [ 0 ] = t h e t a 0

s t i v t t [ 0 ] = t0 57

OBSTACULO 59 var y2 i in 0 n = ybar minus k ( x [ i ] minus xbar ) ˆ2

s t mu y i in 0 n y [ i ] gt= y2 [ i ] 61

FINAL RESTRICTION

55

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 56: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

63 var r f = ( x [ n]minusxf ) ˆ2 + ( y [ n]minusyf ) ˆ2 + ( t h e t a [ n]minus t h e t a f ) ˆ2 s t mu rf r f lt= rfmax

65

a u x i l i a r y f u n c t i o n s f o r improved Euler67 var fx i in 0 n = vu [ i ] cos ( t h e t a [ i ] )

var fy i in 0 n = vu [ i ] s i n ( t h e t a [ i ] ) 69 var f t h e t a i in 0 n = vu [ i ] w[ i ]

71 EULER Method s t l x i in 0 nminus1 x [ i +1]=x [ i ] + h fx [ i ]

73 s t ly i in 0 nminus1 y [ i +1]=y [ i ] + h fy [ i ] s t l t h e t a i in 0 nminus1 t h e t a [ i +1]= t h e t a [ i ] + h f t h e t a [ i ]

75 s t l t i in 0 nminus1 t [ i +1]= t [ i ] + hv

77 Control c o n s t r a i n t ss t mu v i in 0 n vmin lt= v lt= vmax

79 s t mu u i in 0 n umin lt= u [ i ] lt= umax s t mu w i in 0 n wmin lt= w[ i ] lt= wmax

81

SOLVER 83 option s o l v e r knitroampl

option k n i t r o o p t i o n s rdquo maxit =1000 o p t t o l =1eminus8 debug=2 f e a s t o l a b s =1eminus5rdquo85

option s o l v e r ipopt 87 option ip o pt o pt i on s rdquo max i ter =999 a c c e p t a b l e t o l =1eminus8rdquo

89 solve

91 SAVE RESULTS p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo x [ i ] y [ i ] gt rsquo t r a j dat rsquo

93 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h x [ i ] gt rsquo x dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y [ i ] gt rsquo y dat rsquo

95 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h y2 [ i ] gt rsquo y2 dat rsquo p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t h e t a [ i ] gt rsquo t h e t a dat rsquo

97 p r i n t f i in 0 n rdquo1810 f 1810 f nrdquo i h t [ i ] gt rsquo t dat rsquo

99 p r i n t f rdquo1810 f nrdquo v gt rsquov dat rsquo p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h u [ i ] gt rsquou dat rsquo

101 p r i n t f i in 0 nminus1 rdquo1810 f 1810 f nrdquo i h w[ i ] gt rsquow dat rsquo

103 p r i n t f i in 0 nminus1 rdquo1810 f rdquo ob j gt rsquo ob j dat rsquo

56

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 57: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

A3 HIV ICLOCS Code

1 c l e a r a l l c l o s e a l l c l c format compact

3 Fetch the problem d e f i n i t i o n[ problem guess ] = OptChemotherapeuticStrat

5 Get opt ions and s o l v e r s e t t i n g sopt ions = s e t t i n g s

7

Format f o r NLP s o l v e r9 [ infoNLP data ] = transcribeOCP ( problem guess opt ions )

Solve the NLP11 [ so lut ion s t a t u s ] = solveNLP ( infoNLP data )

Output s o l u t i o n s13 output ( so lut ion options data )

15 plotHIV

ApplicationsOptChemotherapeuticStraticlocsmainm

1 func t ion [ problem guess ] = OptChemotherapeuticStrat

3 I n i t i a l Time t0ltt fproblem time t0 = 0

5

F i n a l time Let t f min=tf max i f t f i s f i x e d 7 problem time t f min = 5 0 0

problem time tf max = problem time t f min 9 guess t f = [ ]

11 Parameters bounds pl=lt p lt=puproblem parameters pl = [ ]

13 problem parameters pu = [ ] guess parameters = [ ]

15

some parameters17 m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]

r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0 miu T tmax= 30gt10 =s19

I n i t i a l condi t ions f o r the system 21 f o r i n f e c t i o n by f r e e v i rus

Ta0 = tmax 20 (1 minus m( 1 ) r + s q r t ((1minusm( 1 ) r ) ˆ2 + 4 s ( r tmax ) ) ) Tl0 =0 Ti0 =0 V0=1Eminus3

23 f o r i n f e c t i o n by both i n f e c t e d c e l l s and virusTa0 = 9 7 5 Tl0 = 0 0 5 Ti0 = 0 0 1 V0 = 1Eminus3

25 problem s t a t e s x0 = [ Ta0 Tl0 Ti0 V0 ]

27 I n i t i a l condi t ions f o r system Bounds i f x0 i s f r e e s t x0 l=lt x0 lt=x0uproblem s t a t e s x0 l = [ Ta0 Tl0 Ti0 V0 ]

29 problem s t a t e s x0u = [ Ta0 Tl0 Ti0 V0 ]

31 S t a t e bounds x l=lt x lt=xuproblem s t a t e s x l = [minus i n f minusi n f minusi n f minusi n f ]

33 problem s t a t e s xu = [ i n f i n f i n f i n f ]

35 Terminal s t a t e bounds x f l=lt xf lt=xfuTaf = 9 5 0 T l f = 5eminus5 T i f = 5Eminus5 Vf = 0 5

37 problem s t a t e s x f = [ Taf T l f T i f Vf ] problem s t a t e s x f l = [minus i n f minusi n f minusi n f minusi n f ]

39 problem s t a t e s xfu = [ i n f i n f i n f i n f ]

41 Guess the s t a t e t r a j e c t o r i e s with [ x0 xf ]guess s t a t e s = [ problem s t a t e s x0 problem s t a t e s x f ]

57

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 58: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

43

Number of c o n t r o l a c t i o n s N45 problem inputs N = 0

47 Input bounds ( usual these are arrays )problem inputs ul = 0

49 problem inputs uu = 1

51 Guess the input sequences with [ u0 uf ]guess inputs = [ ]

53

Choose the setminuspoints i f required55 problem s e t p o i n t s s t a t e s = [ ]

problem s e t p o i n t s inputs = [ ] 57

Bounds f o r path c o n s t r a i n t funct ion gl =lt g ( x u p t ) =lt gu59 problem c o n s t r a i n t s g l = [ ]

problem c o n s t r a i n t s gu = [ ] 61

Bounds f o r boundary c o n s t r a i n t s b l =lt b ( x0 xf u0 uf p t0 t f ) =lt bu63 problem c o n s t r a i n t s b l = [ ]

problem c o n s t r a i n t s bu = [ ] 65

s t o r e the necessary problem parameters used in the f u n c t i o n s67 problem data = [m r N tmax s ]

69 Get funct ion handles and return to Main mproblem f u n c t i o n s =L E f g b

71

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus73

75 func t ion stageCost=L ( x xr u ur p t data )

77 B = 1 0 0 coef = 0 5

79

stageCost = minusx ( 1 ) + coef B u ( 1 ) u ( 1 ) 81

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus83

85 func t ion boundaryCost=E ( x0 xf u0 uf p t f data )

87 boundaryCost= t f

89 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

91

func t ion dx = f ( x u p t data )93

x1 = x ( 1 ) x2 = x ( 2 ) x3 = x ( 3 ) x4 = x ( 4 ) 95 u1 = u ( 1 )

97 m = data ( 1 6 ) r = data ( 7 ) N = data ( 8 ) tmax = data ( 9 ) s = data ( 1 0 )

99

dx ( 1 ) = s (1+ x4 ) minus m( 1 ) x1 + r x1 ( 1 minus ( x1+x2+x3 ) tmax ) minus m( 5 ) x4 x1 101

dx ( 2 ) = m( 5 ) x4 x1 minus m( 2 ) x2 minus m( 6 ) x2 103

dx ( 3 ) = m( 6 ) x2 minus m( 3 ) x3 105

dx ( 4 ) = (1minusu1 ) N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

58

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 59: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

107 dx ( 4 ) = u1 N m( 3 ) x3 minus m( 5 ) x4 x1 minus m( 4 ) x4

109 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

111

func t ion c=g ( x u p t data )113

c = [ ] 115

minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus117

119 func t ion bc=b ( x0 xf u0 uf p t f data )

121 bc = [ ]

123 minusminusminusminusminusminusminusminusminusminusminusminusminus END OF CODE minusminusminusminusminusminusminusminusminusminusminusminusminusminus

ApplicationsOptChemotherapeuticStraticlocsOptChemotherapeuticStratm

1 func t ion opt ions = s e t t i n g s

3 options t r a n s c r i p t i o n = rsquo hermite rsquo opt ions d e r i v a t i v e s = rsquo numeric rsquo

5 options hessianFD= rsquo c e n t r a l rsquo

7 options per turbat ion H= [ ] opt ions per turbat ion J = [ ]

9

NLP s o l v e r11 options NLPsolver= rsquo ipopt rsquo

13 IPOPT s e t t i n g s ( i f required )opt ions ipopt t o l =1eminus9

15 options ipopt p r i n t l e v e l =5opt ions ipopt max i ter =200

17 options ipopt mu strategy = rsquo adaptive rsquo opt ions ipopt hessian approximation= rsquo exac t rsquo

19

fmincon s e t t i n g s ( i f required )21 options fmincon=optimset

23 Automatic s c a l i n g ( recommended )opt ions s c a l i n g =1

25

Display computation time27 options p r i n t time =1

29 Display r e l a t i v e l o c a l d i s c r e t i z a t i o n e r r o ropt ions p r i n t r e l a t i v e l o c a l e r r o r =1

31

Display c o s t33 options p r i n t c o s t =1

35 P l o t s t a t e sopt ions p l o t s t a t e s =1

37

P l o t inputs39 options p l o t inputs =1

41 P l o t Lagrange m u l t i p l i e r sopt ions p l o t m u l t i p l i e r s =1

43

59

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 60: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

45 D i r e c t t r a n s c r i p t i o n s e t t i n g sopt ions nodes =1000

47 options tau =0opt ions ODEsolver= rsquo cvodes rsquo

49

CVODES s e t t i n g s ( i f required )51 Method= rsquoAdams rsquo

Solver= rsquoNewton rsquo

ApplicationsOptChemotherapeuticStraticlocssettingsm

2 hf ig1 = f i g u r e ( 1 ) haxis1 = get ( hf ig1 rsquo CurrentAxes rsquo )

4 hplot1 = get ( haxis1 rsquo Children rsquo )

6 T = [ get ( hplot1 ( 4 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 4 ) rsquo Ydata rsquo ) rsquo ] Tl = [ get ( hplot1 ( 3 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 3 ) rsquo Ydata rsquo ) rsquo ]

8 Ti = [ get ( hplot1 ( 2 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 2 ) rsquo Ydata rsquo ) rsquo ] V = [ get ( hplot1 ( 1 ) rsquo Xdata rsquo ) rsquo get ( hplot1 ( 1 ) rsquo Ydata rsquo ) rsquo ]

10

hf ig2 = f i g u r e ( 2 ) 12 haxis2 = get ( hf ig2 rsquo CurrentAxes rsquo )

hplot2 = get ( haxis2 rsquo Children rsquo ) 14

u = [ get ( hplot2 rsquo Xdata rsquo ) rsquo get ( hplot2 rsquo Ydata rsquo ) rsquo ] 16

data = [ T Tl Ti V u ] 18 t i t l e s = char ( rsquo Uninfected c e l l s rsquo rsquo La ten t l y i n f e c t e d c e l l s rsquo rsquo Act ive ly i n f e c t e d c e l l s rsquo rsquo

Virus rsquo rsquo Chemotherapy rsquo ) sz = s i z e ( t i t l e s ) t i t l e s i z e = sz ( 1 ) sz ( 2 ) t i t l e n = sz ( 1 )

20 legends = char ( rsquo S ( t ) rsquo rsquoE ( t ) rsquo rsquo I ( t ) rsquo rsquoR( t ) rsquo rsquou ( t ) rsquo ) sz = s i z e ( legends ) l egendsize = sz ( 1 ) sz ( 2 ) legendn = sz ( 1 )

22 l o c a t i o n s = char ( rsquo NorthWest rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthEast rsquo rsquo NorthWest rsquo rsquo NorthEast rsquo)

sz = s i z e ( l o c a t i o n s ) l o c a t i o n s i z e = sz ( 1 ) sz ( 2 ) l o c a t i o n n = sz ( 1 ) 24

h f i g = f i g u r e ( ) 26 f o r i = 1 s i z e ( data 2 ) 2

subplot ( 2 3 i ) 28 hplot = p l o t ( data ( 2 i minus1) data ( 2 i ) rsquo rminus rsquo )

t i t l e ( s t r t r i m ( t i t l e s ( i t i t l e n t i t l e s i z e ) ) rsquo FontSize rsquo 2 0 ) 30 x l a b e l ( rsquo Time ( days ) rsquo rsquo FontSize rsquo 1 8 )

s e t ( gca rsquo FontSize rsquo 1 7 ) 32 legend ( s t r t r i m ( legends ( i legendn l o c a t i o n s ) ) rsquo Location rsquo l o c a t i o n s ( i l o c a t i o n n

l o c a t i o n s i z e ) ) s e t ( gca rsquo Xlim rsquo [ 0 5 0 0 ] )

34 grid end

ApplicationsOptChemotherapeuticStraticlocsplotHIVm

1 func t ion [ t f Xf ] = solveODEsys

3 c l o s e a l l c l c format long

5

t f = 8 0 0 7 Ta0 = 1000 Tl0 = 0 Ti0 = 0 V0 = 1Eminus3

9 options = odeset ( rsquo RelTol rsquo 1 eminus4 rsquo AbsTol rsquo [ 1 eminus4 1eminus4 1eminus4 1eminus4]) [ t X] = ode45 ( ODEsystem [ 0 t f ] [ Ta0 Tl0 Ti0 V0 ] opt ions )

11

h f i g = f i g u r e ( )

60

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code
Page 61: Optimal Control for Constrained Hybrid System …faf/ProjectFCT2009/report.pdfOptimal Control for Constrained Hybrid System Computational Libraries and Applications L.T. Paiva 1 1

13 p l o t ( t X ( 1 ) rsquo rminus rsquo t X ( 2 ) rsquogminus rsquo t X ( 3 ) rsquo b rsquo t X ( 4 ) rsquo k rsquo )

15 Xf = X( length (X) ) disp ( Xf )

17 end

19 func t ion dx = ODEsystem ( t x )dx = zeros ( 4 1 )

21

m = [ 0 0 2 0 0 2 0 2 4 2 4 2 4 Eminus5 3Eminus3]23 r = 0 0 3 N = 1200 tmax = 1 5 E3 s = 1 0

u = 0 25

dx ( 1 ) = s (1+x ( 4 ) ) minus m( 1 ) x ( 1 ) + r x ( 1 ) (1minus( x ( 1 ) +x ( 2 ) +x ( 3 ) ) tmax ) minus m( 5 ) x ( 4 ) x ( 1 ) 27 dx ( 2 ) = m( 5 ) x ( 4 ) x ( 1 ) minus m( 2 ) x ( 2 ) minus m( 6 ) x ( 2 )

dx ( 3 ) = m( 6 ) x ( 2 ) minus m( 3 ) x ( 3 ) 29 dx ( 4 ) = (1minusu ) Nm( 3 ) x ( 3 ) minus m( 5 ) x ( 4 ) x ( 1 ) minus m( 4 ) x ( 4 )

end

ApplicationsOptChemotherapeuticStraticlocssolveODEsysm

61

  • OC amp NLP Interfaces
    • Introduction
    • AMPL
    • ACADO ndash Automatic Control And Dynamic Optimization
    • BOCOP ndash The optimal control solver
    • DIDO ndash Automatic Control And Dynamic Optimization
    • ICLOCS ndash Imperial College London Optimal Control Software
    • TACO ndash Toolkit for AMPL Control Optimization
    • Pseudospectral Methods in Optimal Control
      • NLP Solvers
        • Introduction
        • IPOPT ndash Interior Point OPTimizer
        • KNITRO
        • WORHP ndash WORHP Optimises Really Huge Problems
        • Other Commercial Packages
          • Project webpage
          • Optimal Control Toolbox
          • Applications
            • CarndashLike
            • Goddard Problem
            • HIV
              • Programming code
                • Optimal Control Toolbox MATLAB Code
                • CarndashLike AMPL Code
                • HIV ICLOCS Code