Austin Schuh | 7400da0 | 2018-01-28 19:54:58 -0800 | [diff] [blame^] | 1 | /********************************************************************************************************************** |
| 2 | This file is part of the Control Toolbox (https://adrlab.bitbucket.io/ct), copyright by ETH Zurich, Google Inc. |
| 3 | Authors: Michael Neunert, Markus Giftthaler, Markus Stäuble, Diego Pardo, Farbod Farshidian |
| 4 | Licensed under Apache2 license (see LICENSE file in main directory) |
| 5 | **********************************************************************************************************************/ |
| 6 | |
| 7 | /*! |
| 8 | * This Example displays some constraint toolbox outputs, giving the user a feeling for |
| 9 | * sparsity patterns, etc. |
| 10 | */ |
| 11 | |
| 12 | #include <ct/optcon/optcon.h> |
| 13 | |
| 14 | using namespace ct::core; |
| 15 | using namespace ct::optcon; |
| 16 | |
| 17 | // some random state and control dimensions |
| 18 | const size_t state_dim = 10; |
| 19 | const size_t control_dim = 5; |
| 20 | |
| 21 | |
| 22 | void controlInputBoxConstraintExample() |
| 23 | { |
| 24 | // create constraint container |
| 25 | std::shared_ptr<ConstraintContainerAnalytical<state_dim, control_dim>> constraints( |
| 26 | new ct::optcon::ConstraintContainerAnalytical<state_dim, control_dim>()); |
| 27 | |
| 28 | // desired boundaries |
| 29 | ControlVector<control_dim> u_lb = -ControlVector<control_dim>::Ones(); |
| 30 | ControlVector<control_dim> u_ub = ControlVector<control_dim>::Ones(); |
| 31 | |
| 32 | // constraint term |
| 33 | std::shared_ptr<ControlInputConstraint<state_dim, control_dim>> controlConstraint( |
| 34 | new ControlInputConstraint<state_dim, control_dim>(u_lb, u_ub)); |
| 35 | controlConstraint->setName("ControlInputConstraint"); |
| 36 | |
| 37 | // add and initialize constraint term |
| 38 | constraints->addIntermediateConstraint(controlConstraint, true); |
| 39 | constraints->initialize(); |
| 40 | |
| 41 | std::cout << "=============================================" << std::endl; |
| 42 | std::cout << "Printing example for control input constraint:" << std::endl; |
| 43 | std::cout << "=============================================" << std::endl; |
| 44 | constraints->printout(); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | void terminalConstraintExample() |
| 49 | { |
| 50 | // create constraint container |
| 51 | std::shared_ptr<ConstraintContainerAnalytical<state_dim, control_dim>> constraints( |
| 52 | new ct::optcon::ConstraintContainerAnalytical<state_dim, control_dim>()); |
| 53 | |
| 54 | // desired terminal state |
| 55 | StateVector<state_dim> x_final = StateVector<state_dim>::Random(); |
| 56 | |
| 57 | // terminal constrain term |
| 58 | std::shared_ptr<TerminalConstraint<state_dim, control_dim>> terminalConstraint( |
| 59 | new TerminalConstraint<state_dim, control_dim>(x_final)); |
| 60 | terminalConstraint->setName("TerminalConstraint"); |
| 61 | |
| 62 | // add and initialize terminal constraint term |
| 63 | constraints->addTerminalConstraint(terminalConstraint, true); |
| 64 | constraints->initialize(); |
| 65 | |
| 66 | std::cout << "==============================================" << std::endl; |
| 67 | std::cout << "Printing example for terminal state constraint:" << std::endl; |
| 68 | std::cout << "==============================================" << std::endl; |
| 69 | constraints->printout(); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | void boxConstraintsExample() |
| 74 | { |
| 75 | // create constraint container |
| 76 | std::shared_ptr<ConstraintContainerAnalytical<state_dim, control_dim>> constraints( |
| 77 | new ct::optcon::ConstraintContainerAnalytical<state_dim, control_dim>()); |
| 78 | |
| 79 | // desired terminal state |
| 80 | ControlVector<control_dim> u_lb = -1.11 * ControlVector<control_dim>::Ones(); |
| 81 | ControlVector<control_dim> u_ub = 1.11 * ControlVector<control_dim>::Ones(); |
| 82 | StateVector<state_dim> x_lb = -3.33 * StateVector<state_dim>::Ones(); |
| 83 | StateVector<state_dim> x_ub = 3.33 * StateVector<state_dim>::Ones(); |
| 84 | |
| 85 | // constrain terms |
| 86 | std::shared_ptr<ControlInputConstraint<state_dim, control_dim>> controlConstraint( |
| 87 | new ControlInputConstraint<state_dim, control_dim>(u_lb, u_ub)); |
| 88 | controlConstraint->setName("ControlInputConstraint"); |
| 89 | std::shared_ptr<StateConstraint<state_dim, control_dim>> stateConstraint( |
| 90 | new StateConstraint<state_dim, control_dim>(x_lb, x_ub)); |
| 91 | stateConstraint->setName("StateConstraint"); |
| 92 | |
| 93 | // add and initialize constraint terms |
| 94 | constraints->addIntermediateConstraint(controlConstraint, true); |
| 95 | constraints->addIntermediateConstraint(stateConstraint, true); |
| 96 | constraints->initialize(); |
| 97 | |
| 98 | std::cout << "=============================================" << std::endl; |
| 99 | std::cout << "Printing example for combined box constraint:" << std::endl; |
| 100 | std::cout << "=============================================" << std::endl; |
| 101 | constraints->printout(); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | void sparseBoxConstraintsExample() |
| 106 | { |
| 107 | // create constraint container |
| 108 | std::shared_ptr<ConstraintContainerAnalytical<state_dim, control_dim>> constraints( |
| 109 | new ct::optcon::ConstraintContainerAnalytical<state_dim, control_dim>()); |
| 110 | |
| 111 | // box constraint boundaries with sparsities |
| 112 | Eigen::VectorXi sp_control(control_dim); |
| 113 | sp_control << 0, 1, 0, 0, 1; |
| 114 | Eigen::VectorXd u_lb(2); |
| 115 | Eigen::VectorXd u_ub(2); |
| 116 | u_lb.setConstant(-1.11); |
| 117 | u_ub = -u_lb; |
| 118 | |
| 119 | Eigen::VectorXi sp_state(state_dim); |
| 120 | sp_state << 0, 1, 0, 0, 1, 0, 1, 1, 0, 0; |
| 121 | Eigen::VectorXd x_lb(4); |
| 122 | Eigen::VectorXd x_ub(4); |
| 123 | x_lb.setConstant(-3.33); |
| 124 | x_ub = -x_lb; |
| 125 | |
| 126 | // constrain terms |
| 127 | std::shared_ptr<ControlInputConstraint<state_dim, control_dim>> controlConstraint( |
| 128 | new ControlInputConstraint<state_dim, control_dim>(u_lb, u_ub, sp_control)); |
| 129 | controlConstraint->setName("ControlInputConstraint"); |
| 130 | |
| 131 | std::shared_ptr<StateConstraint<state_dim, control_dim>> stateConstraint( |
| 132 | new StateConstraint<state_dim, control_dim>(x_lb, x_ub, sp_state)); |
| 133 | stateConstraint->setName("StateConstraint"); |
| 134 | |
| 135 | // add and initialize constraint terms |
| 136 | constraints->addIntermediateConstraint(controlConstraint, true); |
| 137 | constraints->addIntermediateConstraint(stateConstraint, true); |
| 138 | constraints->initialize(); |
| 139 | |
| 140 | std::cout << "=============================================" << std::endl; |
| 141 | std::cout << "Printing example for sparse box constraint:" << std::endl; |
| 142 | std::cout << "=============================================" << std::endl; |
| 143 | constraints->printout(); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | int main(int argc, char **argv) |
| 148 | { |
| 149 | controlInputBoxConstraintExample(); |
| 150 | terminalConstraintExample(); |
| 151 | boxConstraintsExample(); |
| 152 | sparseBoxConstraintsExample(); |
| 153 | return 1; |
| 154 | } |