Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <ifopt/snopt_adapter.h>
00028
00029 namespace ifopt {
00030
00031 SnoptAdapter::NLPPtr SnoptAdapter::nlp_;
00032
00033 SnoptAdapter::SnoptAdapter (Problem& ref)
00034 {
00035 nlp_ = &ref;
00036 }
00037
00038 void
00039 SnoptAdapter::Init ()
00040 {
00041 int obj_count = nlp_->HasCostTerms()? 1 : 0;
00042 n = nlp_->GetNumberOfOptimizationVariables();
00043 neF = nlp_->GetNumberOfConstraints() + obj_count;
00044
00045 x = new double[n];
00046 xlow = new double[n];
00047 xupp = new double[n];
00048 xmul = new double[n];
00049 xstate = new int[n];
00050
00051 F = new double[neF];
00052 Flow = new double[neF];
00053 Fupp = new double[neF];
00054 Fmul = new double[neF];
00055 Fstate = new int[neF];
00056
00057
00058
00059 auto bounds_x = nlp_->GetBoundsOnOptimizationVariables();
00060 for (uint _n=0; _n<bounds_x.size(); ++_n) {
00061 xlow[_n] = bounds_x.at(_n).lower_;
00062 xupp[_n] = bounds_x.at(_n).upper_;
00063 xstate[_n] = 0;
00064 }
00065
00066
00067 int c = 0;
00068 if (nlp_->HasCostTerms()) {
00069 Flow[c] = -1e20; Fupp[c] = 1e20; Fmul[c] = 0.0;
00070 c++;
00071 }
00072
00073
00074 auto bounds_g = nlp_->GetBoundsOnConstraints();
00075 for (const auto& b : bounds_g) {
00076 Flow[c] = b.lower_; Fupp[c] = b.upper_; Fmul[c] = 0.0;
00077 c++;
00078 }
00079
00080
00081 VectorXd x_all = nlp_->GetVariableValues();
00082 Eigen::Map<VectorXd>(&x[0], x_all.rows()) = x_all;
00083
00084 ObjRow = nlp_->HasCostTerms()? 0 : -1;
00085 ObjAdd = 0.0;
00086
00087
00088 lenA = 0;
00089 neA = 0;
00090 iAfun = nullptr;
00091 jAvar = nullptr;
00092 A = nullptr;
00093
00094
00095 lenG = obj_count*n + nlp_->GetJacobianOfConstraints().nonZeros();
00096 iGfun = new int[lenG];
00097 jGvar = new int[lenG];
00098
00099
00100 neG=0;
00101
00102
00103 if(nlp_->HasCostTerms()) {
00104 for (int var=0; var<n; ++var) {
00105 iGfun[neG] = 0;
00106 jGvar[neG] = var;
00107 neG++;
00108 }
00109 }
00110
00111
00112 auto jac = nlp_->GetJacobianOfConstraints();
00113 for (int k=0; k<jac.outerSize(); ++k) {
00114 for (Jacobian::InnerIterator it(jac,k); it; ++it) {
00115 iGfun[neG] = it.row() + obj_count;
00116 jGvar[neG] = it.col();
00117 neG++;
00118 }
00119 }
00120
00121 setUserFun(&SnoptAdapter::ObjectiveAndConstraintFct);
00122 }
00123
00124 void
00125 SnoptAdapter::ObjectiveAndConstraintFct (int* Status, int* n, double x[],
00126 int* needF, int* neF, double F[],
00127 int* needG, int* neG, double G[],
00128 char* cu, int* lencu, int iu[],
00129 int* leniu, double ru[], int* lenru)
00130 {
00131 if ( *needF > 0 ) {
00132 int i=0;
00133
00134
00135 if (nlp_->HasCostTerms())
00136 F[i++] = nlp_->EvaluateCostFunction(x);
00137
00138
00139 VectorXd g_eig = nlp_->EvaluateConstraints(x);
00140 Eigen::Map<VectorXd>(F+i, g_eig.rows()) = g_eig;
00141 }
00142
00143
00144 if ( *needG > 0 ) {
00145 int i=0;
00146
00147
00148 if (nlp_->HasCostTerms()) {
00149 Eigen::VectorXd grad = nlp_->EvaluateCostFunctionGradient(x);
00150 i = grad.rows();
00151 Eigen::Map<VectorXd>(G, i) = grad;
00152 }
00153
00154
00155 nlp_->EvalNonzerosOfJacobian(x, G+i);
00156 nlp_->SaveCurrent();
00157 }
00158 }
00159
00160 void
00161 SnoptAdapter::SetVariables ()
00162 {
00163 nlp_->SetVariables(x);
00164 }
00165
00166 }