In this example, we show how to solve an Optimal Control problem for a oscillator system using classical Direct Multiple Shooting with an NLP solver backend.
- Note
- Generally, we suggest using a Gauss-Newton Multiple Shooting (ct::optcon::GNMS) approach due to its linear time complexity and faster computation. For problems with a fine control discretization, it typically performs better than classical DMS.
-
This example requires either IPOPT or SNOPT to be installed. If you have a global installation of one of these solvers, and an environment variable set, it will be automatically detected.
int main(
int argc,
char** argv)
{
const size_t state_dim = SecondOrderSystem::STATE_DIM;
const size_t control_dim = SecondOrderSystem::CONTROL_DIM;
x_0 << 0.0, 0.0;
x_final << 2.0, -1.0;
double w_n = 0.5;
double zeta = 0.01;
std::shared_ptr<ct::optcon::TermQuadratic<state_dim, control_dim>> intermediateCost(
intermediateCost->loadConfigFile(ct::optcon::exampleDir + "/dmsCost.info", "intermediateCost", true);
std::shared_ptr<CostFunctionQuadratic<state_dim, control_dim>> costFunction(
costFunction->addIntermediateTerm(intermediateCost);
std::shared_ptr<ct::optcon::ConstraintContainerAnalytical<state_dim, control_dim>> finalConstraints(
std::shared_ptr<TerminalConstraint<state_dim, control_dim>> terminalConstraint(
terminalConstraint->setName("TerminalConstraint");
finalConstraints->addTerminalConstraint(terminalConstraint, true);
finalConstraints->initialize();
optConProblem.setInitialState(x_0);
optConProblem.setGeneralConstraints(finalConstraints);
for (
size_t i = 0; i < settings.
N_ + 1; ++i)
{
x_initguess[i] = x_0 + (x_final - x_0) * (i / settings.
N_);
}
optConProblem.setTimeHorizon(settings.
T_);
std::shared_ptr<DmsSolver<state_dim, control_dim>> dmsSolver(
dmsSolver->setInitialGuess(initialPolicy);
dmsSolver->solve();
}
You can run this example with the following command