19#define OPTIM_ENABLE_EIGEN_WRAPPERS
20#include <optim/header_only_version/optim.hpp>
31template <
typename T>
class gsOptimBFGS;
32template <
typename T>
class gsOptimLBFGS;
33template <
typename T>
class gsOptimCG;
34template <
typename T>
class gsOptimGD;
35template <
typename T>
class gsOptimNewton;
36template <
typename T>
class gsOptimNM;
37template <
typename T>
class gsOptimDE;
38template <
typename T>
class gsOptimDEPRMM;
39template <
typename T>
class gsOptimPSO;
40template <
typename T>
class gsOptimPSODV;
41template <
typename T>
class gsOptimSUMT;
62 T operator() (
const optim::ColVec_t& vals_in, optim::ColVec_t* grad_out,
void* opt_data)
67 grad_out->resize(vals_in.size());
69 m_op.gradObj_into(in, out);
71 return m_op.evalObj(in);
91 optim::ColVec_t
operator() (
const optim::ColVec_t& vals_in, optim::Mat_t* jacob_out,
void* constr_data)
96 std::vector<T> jacTmp(vals_in.size()*m_op.numConstraints());
98 m_op.jacobCon_into(in, jacOut);
104 jacob_out->resize(m_op.numConstraints(),vals_in.size());
105 gsAsMatrix<T> out(jacob_out->data(),jacob_out->rows(),jacob_out->cols());
107 std::vector<index_t> conJacRows = m_op.conJacRows();
108 std::vector<index_t> conJacCols = m_op.conJacCols();
109 for (
size_t k=0; k!=conJacRows.size(); k++)
110 out(conJacRows[k],conJacCols[k]) = jacTmp[k];
114 m_op.evalCon_into(in,conOut);
134 typedef memory::unique_ptr<gsOptim> uPtr;
177 if (slv==
"BFGS")
return uPtr(
new BFGS(problem));
178 if (slv==
"LBFGS")
return uPtr(
new LBFGS(problem));
179 if (slv==
"CG")
return uPtr(
new CG(problem));
180 if (slv==
"GD")
return uPtr(
new GD(problem));
182 if (slv==
"NM")
return uPtr(
new NM(problem));
183 if (slv==
"DE")
return uPtr(
new DE(problem));
184 if (slv==
"DEPRMM")
return uPtr(
new DEPRMM(problem));
185 if (slv==
"PSO")
return uPtr(
new PSO(problem));
186 if (slv==
"PSODV")
return uPtr(
new PSODV(problem));
187 if (slv==
"SUMT")
return uPtr(
new SUMT(problem));
188 GISMO_ERROR(
"Solver \'"<< slv <<
"\' not known to G+Smo");
194 Base::defaultOptions();
196 m_options.
addInt(
"ConfFailureSwitch",
"Switch for convergence failure",0);
197 m_options.
addReal(
"GradErrTol",
"Gradient error tolerance (default: 1e-8)",1e-8);
198 m_options.
addReal(
"RelSolChangeTol",
"Relative solution change tolerance (default: 1e-14)",1e-14);
199 m_options.
addReal(
"RelObjFnChangeTol",
"Relative objective function change tolerance (default: 1e-8)",1e-8);
213 m_optimSettings.print_level = m_verbose;
214 m_optimSettings.conv_failure_switch =
m_options.
getInt(
"ConfFailureSwitch");
217 m_optimSettings.iter_max = m_maxIterations;
221 m_optimSettings.rel_objfn_change_tol =
m_options.
getReal(
"RelObjFnChangeTol");
233 GISMO_ASSERT(initialGuess.cols()==1,
"The initial guess should have vector format");
235 m_success =
callOptim(x, *m_op, m_optimSettings);
237 m_numIterations = m_optimSettings.opt_iter;
238 m_finalObjective = m_optimSettings.opt_fn_value;
253 if(m_op->desLowerBounds().size()!=0)
255 m_optimSettings.lower_bounds = m_op->desLowerBounds();
256 m_optimSettings.upper_bounds = m_op->desUpperBounds();
257 m_optimSettings.vals_bound =
true;
264 using Base::m_numIterations;
265 using Base::m_finalObjective;
268 using Base::m_verbose;
269 using Base::m_maxIterations;
271 using Base::defaultOptions;
272 using Base::getOptions;
276 optim::algo_settings_t m_optimSettings;
302 optim::algo_settings_t & optimSettings)
override
309 m_options.
addReal(
"WolfeCons1",
"Line search tuning parameter that controls the tolerance on the Armijo sufficient decrease condition.",1e-3);
310 m_options.
addReal(
"WolfeCons2",
"Line search tuning parameter that controls the tolerance on the curvature condition.",0.9);
317 m_optimSettings.bfgs_settings.wolfe_cons_1 =
m_options.
getReal(
"WolfeCons1");
318 m_optimSettings.bfgs_settings.wolfe_cons_2 =
m_options.
getReal(
"WolfeCons2");
324 using Base::m_optimSettings;
349 optim::algo_settings_t & optimSettings)
override
356 m_options.
addInt(
"ParM",
"The number of past gradient vectors to use when forming the approximate Hessian matrix.",10);
357 m_options.
addReal(
"WolfeCons1",
"Line search tuning parameter that controls the tolerance on the Armijo sufficient decrease condition.",1e-3);
358 m_options.
addReal(
"WolfeCons2",
"Line search tuning parameter that controls the tolerance on the curvature condition.",0.9);
366 m_optimSettings.lbfgs_settings.wolfe_cons_1 =
m_options.
getReal(
"WolfeCons1");
367 m_optimSettings.lbfgs_settings.wolfe_cons_2 =
m_options.
getReal(
"WolfeCons2");
373 using Base::m_optimSettings;
398 optim::algo_settings_t & optimSettings)
override
405 m_options.
addInt(
"UpdateMethod",
"Update method: 1) Fletcher–Reeves, 2) Polak-Ribiere, 3) FR-PR Hybrid, 4) Hestenes-Stiefel, 5) Dai-Yuan, 6) Hager-Zhang.",2);
406 m_options.
addReal(
"RestartThreshold",
"parameter ν from step 2 in the algorithm description.",0.1);
407 m_options.
addSwitch(
"UseRelSolChangeCrit",
"whether to enable the `rel_sol_change_tol` stopping criterion.",
false);
408 m_options.
addReal(
"WolfeCons1",
"Line search tuning parameter that controls the tolerance on the Armijo sufficient decrease condition.",1e-3);
409 m_options.
addReal(
"WolfeCons2",
"Line search tuning parameter that controls the tolerance on the curvature condition.",0.9);
417 m_optimSettings.cg_settings.restart_threshold =
m_options.
getReal(
"RestartThreshold");
418 m_optimSettings.cg_settings.use_rel_sol_change_crit =
m_options.
getSwitch(
"UseRelSolChangeCrit");
426 using Base::m_optimSettings;
451 optim::algo_settings_t & optimSettings)
override
459 m_options.
addInt(
"Method",
"Method to use: 0) Vanilla GD, 1) GD with momentum, 2) Nesterov accelerated gradient descent, 3) AdaGrad. 4) RMSProp, 5) AdaDelta. 6) Adam (adaptive moment estimation) and AdaMax, 7) Nadam (adaptive moment estimation) and NadaMax",0);
462 m_options.
addInt(
"StepDecayPeriods",
"Number of periods after which to decay the step size.",10);
463 m_options.
addReal(
"StepDecayValue",
"The value by which to decay the step size.",0.5);
465 m_options.
addReal(
"AdaNormTerm",
"The normalization term for AdaGrad.",1.0e-08);
473 m_options.
addInt(
"ClipNormType",
"The type of norm to use for clipping.",2);
474 m_options.
addReal(
"ClipNormBound",
"The bound for the norm used for clipping.",5.0);
489 m_optimSettings.gd_settings.step_decay_periods =
m_options.
getInt(
"StepDecayPeriods");
490 m_optimSettings.gd_settings.step_decay_val =
m_options.
getReal(
"StepDecayValue");
496 m_optimSettings.gd_settings.par_ada_norm_term =
m_options.
getReal(
"AdaNormTerm");
501 m_optimSettings.gd_settings.par_adam_beta_1 =
m_options.
getReal(
"AdamBeta1");
502 m_optimSettings.gd_settings.par_adam_beta_2 =
m_options.
getReal(
"AdamBeta2");
508 m_optimSettings.gd_settings.clip_norm_type =
m_options.
getInt(
"ClipNormType");
509 m_optimSettings.gd_settings.clip_norm_bound =
m_options.
getReal(
"ClipNormBound");
515 using Base::m_optimSettings;
566 optim::algo_settings_t & optimSettings)
override
573 m_options.
addSwitch(
"AdaptivePars",
"scale the contraction, expansion, and shrinkage parameters using the dimension of the optimization problem.",
true);
578 m_options.
addSwitch(
"CustomInitialSimplex",
"whether to use user-defined values for the initial simplex matrix.",
false);
586 m_optimSettings.nm_settings.initial_simplex_points = points;
600 m_optimSettings.nm_settings.custom_initial_simplex =
m_options.
getSwitch(
"CustomInitialSimplex");
606 using Base::m_optimSettings;
631 optim::algo_settings_t & optimSettings)
override
638 m_options.
addInt(
"nPop",
"size of population for each generation.",200);
639 m_options.
addInt(
"nPopBest",
"number of best individuals to select.",6);
641 m_options.
addInt(
"MutationMethod",
"the mutation strategy, as described in step one of the algorithm description: 1) rand, 2) best.",1);
642 m_options.
addInt(
"CheckFreq",
"how many generations to skip when evaluating whether the best candidate value has improved between generations (i.e., to check for potential convergence).",std::numeric_limits<index_t>::max()-1);
646 m_options.
addInt(
"MaxFnEval",
"Maximum number of function evaluations.",100000);
650 m_options.
addReal(
"ParTauCR",
"Scaling factor for the crossover probability.",0.1);
652 m_options.
addSwitch(
"ReturnPopulationMat",
"Whether to return the population matrix.",
false);
658 m_optimSettings.de_settings.initial_lb = bounds.col(0);
659 m_optimSettings.de_settings.initial_ub = bounds.col(1);
665 GISMO_ASSERT(m_optimSettings.de_settings.return_population_mat,
"The option ReturnPopulationMat was not set to true.");
666 return m_optimSettings.de_settings.population_mat;
678 m_optimSettings.de_settings.omp_n_threads = omp_get_num_threads();
680 m_optimSettings.de_settings.omp_n_threads = -1;
683 m_optimSettings.de_settings.mutation_method =
m_options.
getInt(
"MutationMethod");
685 m_optimSettings.de_settings.check_freq =
m_options.
getInt(
"CheckFreq");
693 m_optimSettings.de_settings.max_fn_eval =
m_options.
getInt(
"MaxFnEval");
707 using Base::m_optimSettings;
728 optim::algo_settings_t & optimSettings)
override
754 optim::algo_settings_t & optimSettings)
override
761 m_options.
addSwitch(
"CenterParticle",
"whether to add a particle that averages across the population in each generation.",
true);
762 m_options.
addInt(
"nPop",
"size of population for each generation.",200);
765 m_options.
addInt(
"InertiaMethod",
"1 for linear decreasing between w_min and w_max; 2 for dampening.",1);
766 m_options.
addInt(
"CheckFreq",
"how many generations to skip when evaluating whether the best candidate value has improved between generations (i.e., to check for potential convergence).",std::numeric_limits<index_t>::max()-1);
768 m_options.
addReal(
"ParWDamp",
"initial value of the weight parameter w.",0.99);
771 m_options.
addInt(
"VelocityMethod",
"set velocity method (1 for fixed values or 2 for linear change from initial to final values).",1);
778 m_options.
addSwitch(
"ReturnPopulationMat",
"Whether to return the population matrix.",
false);
784 m_optimSettings.pso_settings.initial_lb = bounds.col(0);
785 m_optimSettings.pso_settings.initial_ub = bounds.col(1);
791 GISMO_ASSERT(m_optimSettings.pso_settings.return_position_mat,
"The option ReturnPopulationMat was not set to true.");
792 return m_optimSettings.pso_settings.position_mat;
804 m_optimSettings.pso_settings.omp_n_threads = omp_get_num_threads();
806 m_optimSettings.pso_settings.omp_n_threads = -1;
809 m_optimSettings.pso_settings.inertia_method =
m_options.
getInt(
"InertiaMethod");
810 m_optimSettings.pso_settings.check_freq =
m_options.
getInt(
"CheckFreq");
815 m_optimSettings.pso_settings.velocity_method =
m_options.
getInt(
"VelocityMethod");
819 m_optimSettings.pso_settings.par_final_c_cog =
m_options.
getReal(
"ParFinalCCog");
821 m_optimSettings.pso_settings.par_final_c_soc =
m_options.
getReal(
"ParFinalCSoc");
822 m_optimSettings.pso_settings.return_position_mat =
m_options.
getSwitch(
"ReturnPopulationMat");
828 using Base::m_optimSettings;
851 optim::algo_settings_t & optimSettings)
override
877 optim::algo_settings_t & optimSettings)
override
886 GISMO_ASSERT(initialGuess.cols()==1,
"The initial guess should have vector format");
888 m_success =
callOptim(x, *m_op, m_optimSettings);
890 m_numIterations = m_optimSettings.opt_iter;
891 m_finalObjective = m_optimSettings.opt_fn_value;
911 using Base::m_optimSettings;
914 using Base::m_success;
916 using Base::m_numIterations;
917 using Base::m_finalObjective;
Creates a mapped object or data pointer to a const vector without copying data.
Definition gsAsMatrix.h:285
Creates a mapped object or data pointer to a matrix without copying data.
Definition gsAsMatrix.h:32
Creates a mapped object or data pointer to a vector without copying data.
Definition gsAsMatrix.h:239
A matrix with arbitrary coefficient type and fixed or dynamic size.
Definition gsMatrix.h:41
Class defining an optimization problem.
Definition gsOptProblem.h:25
Binding to Optim's BFGS solver.
Definition gsOptim.h:287
gsOptimBFGS(gsOptProblem< T > *problem)
See gsOptim.
Definition gsOptim.h:294
bool callOptim(gsVector< T > &x, gsOptProblem< T > &op, optim::algo_settings_t &optimSettings) override
See gsOptim.
Definition gsOptim.h:300
void defaultOptions() override
See gsOptim.
Definition gsOptim.h:306
void getOptions() override
See gsOptim.
Definition gsOptim.h:314
gsOptionList m_options
Options.
Definition gsOptimizer.h:108
Binding to Optim's CG solver.
Definition gsOptim.h:383
gsOptimCG(gsOptProblem< T > *problem)
See gsOptim.
Definition gsOptim.h:390
bool callOptim(gsVector< T > &x, gsOptProblem< T > &op, optim::algo_settings_t &optimSettings) override
See gsOptim.
Definition gsOptim.h:396
void defaultOptions() override
See gsOptim.
Definition gsOptim.h:402
void getOptions() override
See gsOptim.
Definition gsOptim.h:413
gsOptionList m_options
Options.
Definition gsOptimizer.h:108
Binding to Optim's DEPRMM solver.
Definition gsOptim.h:717
bool callOptim(gsVector< T > &x, gsOptProblem< T > &op, optim::algo_settings_t &optimSettings) override
See gsOptim.
Definition gsOptim.h:726
Binding to Optim's DE solver.
Definition gsOptim.h:616
gsOptimDE(gsOptProblem< T > *problem)
See gsOptim.
Definition gsOptim.h:623
bool callOptim(gsVector< T > &x, gsOptProblem< T > &op, optim::algo_settings_t &optimSettings) override
See gsOptim.
Definition gsOptim.h:629
gsMatrix< T > getPopulationMatrix()
Gets the population matrix.
Definition gsOptim.h:663
void defaultOptions() override
See gsOptim.
Definition gsOptim.h:635
void getOptions() override
See gsOptim.
Definition gsOptim.h:670
gsOptionList m_options
Options.
Definition gsOptimizer.h:108
void setBounds(const gsMatrix< T, Dynamic, 2 > &bounds)
Set the Upper and lower bounds of the uniform distributions used to generate the initial population.
Definition gsOptim.h:656
Binding to Optim's GD solver.
Definition gsOptim.h:436
gsOptimGD(gsOptProblem< T > *problem)
See gsOptim.
Definition gsOptim.h:443
bool callOptim(gsVector< T > &x, gsOptProblem< T > &op, optim::algo_settings_t &optimSettings) override
See gsOptim.
Definition gsOptim.h:449
void defaultOptions() override
See gsOptim.
Definition gsOptim.h:455
void getOptions() override
See gsOptim.
Definition gsOptim.h:478
gsOptionList m_options
Options.
Definition gsOptimizer.h:108
Binding to Optim's LBFGS solver.
Definition gsOptim.h:334
bool callOptim(gsVector< T > &x, gsOptProblem< T > &op, optim::algo_settings_t &optimSettings) override
See gsOptim.
Definition gsOptim.h:347
gsOptimLBFGS(gsOptProblem< T > *problem)
See gsOptim.
Definition gsOptim.h:341
void defaultOptions() override
See gsOptim.
Definition gsOptim.h:353
void getOptions() override
See gsOptim.
Definition gsOptim.h:362
gsOptionList m_options
Options.
Definition gsOptimizer.h:108
Binding to Optim's NM solver.
Definition gsOptim.h:551
bool callOptim(gsVector< T > &x, gsOptProblem< T > &op, optim::algo_settings_t &optimSettings) override
See gsOptim.
Definition gsOptim.h:564
gsOptimNM(gsOptProblem< T > *problem)
See gsOptim.
Definition gsOptim.h:558
void defaultOptions() override
See gsOptim.
Definition gsOptim.h:570
void getOptions() override
See gsOptim.
Definition gsOptim.h:590
gsOptionList m_options
Options.
Definition gsOptimizer.h:108
void setSimplexPoints(const gsMatrix< T > &points)
Definition gsOptim.h:584
Binding to Optim's PSODV solver.
Definition gsOptim.h:838
bool callOptim(gsVector< T > &x, gsOptProblem< T > &op, optim::algo_settings_t &optimSettings) override
See gsOptim.
Definition gsOptim.h:849
gsOptimPSODV(gsOptProblem< T > *problem)
See gsOptim.
Definition gsOptim.h:843
Binding to Optim's PSO solver.
Definition gsOptim.h:739
gsOptimPSO(gsOptProblem< T > *problem)
See gsOptim.
Definition gsOptim.h:746
bool callOptim(gsVector< T > &x, gsOptProblem< T > &op, optim::algo_settings_t &optimSettings) override
See gsOptim.
Definition gsOptim.h:752
gsMatrix< T > getPopulationMatrix()
Gets the population matrix.
Definition gsOptim.h:789
void defaultOptions() override
See gsOptim.
Definition gsOptim.h:758
void getOptions() override
See gsOptim.
Definition gsOptim.h:796
gsOptionList m_options
Options.
Definition gsOptimizer.h:108
void setBounds(const gsMatrix< T, Dynamic, 2 > &bounds)
Set the Upper and lower bounds of the uniform distributions used to generate the initial population.
Definition gsOptim.h:782
Binding to Optim's SUMT solver.
Definition gsOptim.h:862
bool callOptim(gsVector< T > &x, gsOptProblem< T > &op, optim::algo_settings_t &optimSettings) override
See gsOptim.
Definition gsOptim.h:875
void solve(const gsMatrix< T > &initialGuess) override
See gsOptim.
Definition gsOptim.h:881
void defaultOptions() override
See gsOptim.
Definition gsOptim.h:895
void getOptions() override
See gsOptim.
Definition gsOptim.h:902
gsOptionList m_options
Options.
Definition gsOptimizer.h:108
gsMatrix< T > m_curDesign
Current design variables (and starting point )
Definition gsOptimizer.h:105
gsOptimSUMT(gsOptProblem< T > *problem)
See gsOptim.
Definition gsOptim.h:869
Base class for the Optim wrapper.
Definition gsOptim.h:130
static uPtr get(const std::string &slv, gsOptProblem< T > *problem)
Getter for a specific solver.
Definition gsOptim.h:175
virtual bool callOptim(gsVector< T > &initialGuess, gsOptProblem< T > &op, optim::algo_settings_t &settings)=0
Misc function to call optim.
bool success()
Function returning true when optimization was successful.
Definition gsOptim.h:243
virtual void getOptions()
Options getter.
Definition gsOptim.h:203
virtual void solve(const gsMatrix< T > &initialGuess)
Solve, see gsOptimizer.
Definition gsOptim.h:228
gsOptionList m_options
Options.
Definition gsOptimizer.h:108
gsOptim(gsOptProblem< T > *problem)
Constructor with a gsOptProblem.
Definition gsOptim.h:159
void setConstraints()
Sets the box constraints.
Definition gsOptim.h:251
gsMatrix< T > m_curDesign
Current design variables (and starting point )
Definition gsOptimizer.h:105
gsOptim()
Empty constructor.
Definition gsOptim.h:151
virtual void defaultOptions()
Default options.
Definition gsOptim.h:192
Class defining an optimizer.
Definition gsOptimizer.h:28
gsOptionList m_options
Options.
Definition gsOptimizer.h:108
gsMatrix< T > m_curDesign
Current design variables (and starting point )
Definition gsOptimizer.h:105
void addInt(const std::string &label, const std::string &desc, const index_t &value)
Adds a option named label, with description desc and value value.
Definition gsOptionList.cpp:201
bool getSwitch(const std::string &label) const
Reads value for option label from options.
Definition gsOptionList.cpp:51
const index_t & getInt(const std::string &label) const
Reads value for option label from options.
Definition gsOptionList.cpp:37
Real getReal(const std::string &label) const
Reads value for option label from options.
Definition gsOptionList.cpp:44
void addReal(const std::string &label, const std::string &desc, const Real &value)
Adds a option named label, with description desc and value value.
Definition gsOptionList.cpp:211
void addSwitch(const std::string &label, const std::string &desc, const bool &value)
Adds a option named label, with description desc and value value.
Definition gsOptionList.cpp:235
A vector with arbitrary coefficient type and fixed or dynamic size.
Definition gsVector.h:37
#define GISMO_ERROR(message)
Definition gsDebug.h:118
#define GISMO_ASSERT(cond, message)
Definition gsDebug.h:89
This is the main header file that collects wrappers of Eigen for linear algebra.
Provides declaration of an optimization problem.
This file is part of the G+Smo library.
The G+Smo namespace, containing all definitions for the library.
Wraps the constraint of an optimization problem to a function accepted by Optim.
Definition gsOptim.h:85
optim::ColVec_t operator()(const optim::ColVec_t &vals_in, optim::Mat_t *jacob_out, void *constr_data)
Definition gsOptim.h:91
Wraps the objective of an optimization problem to a function accepted by Optim.
Definition gsOptim.h:56