1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
/*
* MIT No Attribution
*
* Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl, KU Leuven.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <iostream>
#include <fstream>
#include <ctime>
#include <casadi/casadi.hpp>
using namespace casadi;
int main(){
std::cout << "program started" << std::endl;
// Dimensions
int nu = 20; // Number of control segments
int nj = 100; // Number of integration steps per control segment
// optimization variable
SX u = SX::sym("u", nu); // control
SX s_0 = 0; // initial position
SX v_0 = 0; // initial speed
SX m_0 = 1; // initial mass
SX dt = 10.0/(nj*nu); // time step
SX alpha = 0.05; // friction
SX beta = 0.1; // fuel consumption rate
// Trajectory
std::vector<SX> s_k, v_k, m_k;
// Integrate over the interval with Euler forward
SX s = s_0, v = v_0, m = m_0;
for(int k=0; k<nu; ++k){
for(int j=0; j<nj; ++j){
s += dt*v;
v += dt / m * (u(k)- alpha * v*v);
m += -dt * beta*u(k)*u(k);
}
s_k.push_back(s);
v_k.push_back(v);
m_k.push_back(m);
}
SX s_all=vertcat(s_k), v_all=vertcat(v_k), m_all=vertcat(m_k);
// Objective function
SX f = dot(u, u);
// Terminal constraints
SX g = vertcat(s, v, v_all);
// Create the NLP
SXDict nlp = {{"x", u}, {"f", f}, {"g", g}};
// Allocate an NLP solver and buffers
Function solver = nlpsol("solver", "ipopt", nlp);
// Bounds on g
std::vector<double> gmin = {10, 0};
std::vector<double> gmax = {10, 0};
gmin.resize(2+nu, -std::numeric_limits<double>::infinity());
gmax.resize(2+nu, 1.1);
// Solve the problem
DMDict arg = {{"lbx", -10},
{"ubx", 10},
{"x0", 0.4},
{"lbg", gmin},
{"ubg", gmax}};
DMDict res = solver(arg);
// Print the optimal cost
double cost(res.at("f"));
std::cout << "optimal cost: " << cost << std::endl;
// Print the optimal solution
std::vector<double> uopt(res.at("x"));
std::cout << "optimal control: " << uopt << std::endl;
// Get the state trajectory
Function xfcn("xfcn", {u}, {s_all, v_all, m_all});
std::vector<double> sopt, vopt, mopt;
xfcn({uopt}, {&sopt, &vopt, &mopt});
std::cout << "position: " << sopt << std::endl;
std::cout << "velocity: " << vopt << std::endl;
std::cout << "mass: " << mopt << std::endl;
// Create Matlab script to plot the solution
std::ofstream file;
std::string filename = "rocket_ipopt_results.m";
file.open(filename.c_str());
file << "% Results file from " __FILE__ << std::endl;
file << "% Generated " __DATE__ " at " __TIME__ << std::endl;
file << std::endl;
file << "cost = " << cost << ";" << std::endl;
file << "u = " << uopt << ";" << std::endl;
// Save results to file
file << "t = linspace(0,10.0," << nu << ");"<< std::endl;
file << "s = " << sopt << ";" << std::endl;
file << "v = " << vopt << ";" << std::endl;
file << "m = " << mopt << ";" << std::endl;
// Finalize the results file
file << std::endl;
file << "% Plot the results" << std::endl;
file << "figure(1);" << std::endl;
file << "clf;" << std::endl << std::endl;
file << "subplot(2,2,1);" << std::endl;
file << "plot(t,s);" << std::endl;
file << "grid on;" << std::endl;
file << "xlabel('time [s]');" << std::endl;
file << "ylabel('position [m]');" << std::endl << std::endl;
file << "subplot(2,2,2);" << std::endl;
file << "plot(t,v);" << std::endl;
file << "grid on;" << std::endl;
file << "xlabel('time [s]');" << std::endl;
file << "ylabel('velocity [m/s]');" << std::endl << std::endl;
file << "subplot(2,2,3);" << std::endl;
file << "plot(t,m);" << std::endl;
file << "grid on;" << std::endl;
file << "xlabel('time [s]');" << std::endl;
file << "ylabel('mass [kg]');" << std::endl << std::endl;
file << "subplot(2,2,4);" << std::endl;
file << "plot(t,u);" << std::endl;
file << "grid on;" << std::endl;
file << "xlabel('time [s]');" << std::endl;
file << "ylabel('Thrust [kg m/s^2]');" << std::endl << std::endl;
file.close();
std::cout << "Results saved to \"" << filename << "\"" << std::endl;
return 0;
}
|