26 #ifndef CASADI_BLOCKSQP_HPP
27 #define CASADI_BLOCKSQP_HPP
29 #include <casadi/interfaces/blocksqp/casadi_nlpsol_blocksqp_export.h>
30 #include "casadi/core/linsol.hpp"
31 #include "casadi/core/nlpsol_impl.hpp"
32 #include "../qpoases/qpoases_interface.hpp"
51 struct BlocksqpMemory :
public NlpsolMemory {
58 qpOASES::SymSparseMat *H;
60 qpOASES::SQProblem* qp;
63 QpoasesMemory* qpoases_mem;
67 casadi_int qpIterations;
68 casadi_int qpIterations2;
73 casadi_int nRestHeurCalls;
74 casadi_int nRestPhaseCalls;
75 casadi_int rejectedSR1;
76 casadi_int hessSkipped;
77 casadi_int hessDamped;
78 casadi_int nTotalUpdates;
79 casadi_int nTotalSkippedUpdates;
80 double averageSizingFactor;
88 double lambdaStepNorm;
91 double *lam_xk, *lam_gk;
114 double *lbx_qp, *ubx_qp, *lba_qp, *uba_qp;
116 double* jac_times_dxk;
122 double* delta_norm_old;
124 double* delta_gamma_old;
125 casadi_int *noUpdateCounter;
133 casadi_int reducedStepCount;
136 std::set< std::pair<double, double> > filter;
138 std::vector<int> colind, row;
142 double* exact_hess_lag;
151 class Blocksqp :
public Nlpsol {
153 explicit Blocksqp(
const std::string& name,
const Function& nlp);
154 ~Blocksqp()
override;
157 const char* plugin_name()
const override {
return "blocksqp";}
160 std::string class_name()
const override {
return "Blocksqp";}
163 static Nlpsol* creator(
const std::string& name,
const Function& nlp) {
164 return new Blocksqp(name, nlp);
169 static const Options options_;
170 const Options& get_options()
const override {
return options_;}
174 void init(
const Dict& opts)
override;
177 void* alloc_mem()
const override {
return new BlocksqpMemory();}
180 int init_mem(
void* mem)
const override;
183 void free_mem(
void *mem)
const override {
delete static_cast<BlocksqpMemory*
>(mem);}
186 void set_work(
void* mem,
const double**& arg,
double**& res,
187 casadi_int*& iw,
double*& w)
const override;
190 int solve(
void* mem)
const override;
193 static const std::string meta_doc;
197 std::vector<casadi_int> blocks_;
198 std::vector<casadi_int> dim_;
203 Sparsity exact_hess_lag_sp_;
206 casadi_int run(BlocksqpMemory* m, casadi_int maxIt, casadi_int warmStart = 0)
const;
208 void calcLagrangeGradient(BlocksqpMemory* m,
209 const double* lam_x,
const double* lam_g,
210 const double* grad_f,
const double *jacNz,
211 double *grad_lag, casadi_int flag)
const;
214 void calcLagrangeGradient(BlocksqpMemory* m,
double* grad_lag, casadi_int flag)
const;
216 void printInfo(BlocksqpMemory* m)
const;
218 bool calcOptTol(BlocksqpMemory* m)
const;
224 void updateStepBounds(BlocksqpMemory* m,
bool soc)
const;
227 casadi_int solveQP(BlocksqpMemory* m,
double* deltaXi,
double* lambdaQP,
228 bool matricesChanged =
true)
const;
231 void computeNextHessian(BlocksqpMemory* m, casadi_int idx, casadi_int maxQP)
const;
237 casadi_int fullstep(BlocksqpMemory* m)
const;
239 void acceptStep(BlocksqpMemory* m,
const double* deltaXi,
240 const double* lambdaQP,
double alpha, casadi_int nSOCS)
const;
242 void acceptStep(BlocksqpMemory* m,
double alpha)
const;
244 void reduceStepsize(BlocksqpMemory* m,
double *alpha)
const;
246 casadi_int filterLineSearch(BlocksqpMemory* m)
const;
248 void initializeFilter(BlocksqpMemory* m)
const;
250 bool pairInFilter(BlocksqpMemory* m,
double cNorm,
double obj)
const;
252 void augmentFilter(BlocksqpMemory* m,
double cNorm,
double obj)
const;
254 bool secondOrderCorrection(BlocksqpMemory* m,
double cNorm,
double cNormTrial,
255 double dfTdeltaXi,
bool swCond, casadi_int it)
const;
257 void reduceSOCStepsize(BlocksqpMemory* m,
double *alphaSOC)
const;
259 casadi_int feasibilityRestorationHeuristic(BlocksqpMemory* m)
const;
261 casadi_int feasibilityRestorationPhase(BlocksqpMemory* m)
const;
263 casadi_int kktErrorReduction(BlocksqpMemory* m)
const;
269 void calcInitialHessian(BlocksqpMemory* m)
const;
271 void calcInitialHessian(BlocksqpMemory* m, casadi_int b)
const;
273 void resetHessian(BlocksqpMemory* m)
const;
276 void resetHessian(BlocksqpMemory* m, casadi_int b)
const;
278 void calcHessianUpdate(BlocksqpMemory* m, casadi_int updateType, casadi_int hessScaling)
const;
280 void calcHessianUpdateLimitedMemory(BlocksqpMemory* m,
281 casadi_int updateType, casadi_int hessScaling)
const;
283 void calcHessianUpdateExact(BlocksqpMemory* m)
const;
285 void calcSR1(BlocksqpMemory* m,
const double* gamma,
const double* delta,
288 void calcBFGS(BlocksqpMemory* m,
const double* gamma,
const double* delta,
291 void updateDeltaGamma(BlocksqpMemory* m)
const;
297 void sizeInitialHessian(BlocksqpMemory* m,
const double* gamma,
298 const double* delta, casadi_int b, casadi_int option)
const;
300 void sizeHessianCOL(BlocksqpMemory* m,
const double* gamma,
301 const double* delta, casadi_int b)
const;
306 void initStats(BlocksqpMemory* m)
const;
307 void updateStats(BlocksqpMemory* m)
const;
309 void printProgress(BlocksqpMemory* m)
const;
311 void reset_sqp(BlocksqpMemory* m)
const;
313 void convertHessian(BlocksqpMemory* m)
const;
315 void initIterate(BlocksqpMemory* m)
const;
318 casadi_int evaluate(BlocksqpMemory* m,
double *f,
double *g,
319 double *grad_f,
double *jac_g)
const;
322 casadi_int evaluate(BlocksqpMemory* m,
const double *xk,
323 double *f,
double *g)
const;
326 casadi_int evaluate(BlocksqpMemory* m,
327 double *exact_hess_lag)
const;
330 double lInfConstraintNorm(BlocksqpMemory* m,
const double* xk,
const double* g)
const;
336 std::string linsol_plugin_;
340 bool print_iteration_;
349 casadi_int max_line_search_;
350 casadi_int max_consec_reduced_steps_;
351 casadi_int max_consec_skipped_updates_;
352 casadi_int max_it_qp_;
353 casadi_int max_iter_;
357 casadi_int hess_scaling_;
358 casadi_int fallback_scaling_;
360 double ini_hess_diag_;
364 casadi_int hess_damp_;
365 double hess_damp_fac_;
366 casadi_int hess_update_;
367 casadi_int fallback_update_;
368 casadi_int hess_lim_mem_;
369 casadi_int hess_memsize_;
370 casadi_int which_second_derv_;
372 bool skip_first_globalization_;
373 casadi_int conv_strategy_;
374 casadi_int max_conv_qp_;
378 casadi_int max_soc_iter_;
390 double kappa_plus_max_;
400 bool print_maxit_reached_;
403 void serialize_body(SerializingStream &s)
const override;
406 static ProtoFunction* deserialize(DeserializingStream& s) {
return new Blocksqp(s); }
410 explicit Blocksqp(DeserializingStream& s);
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.