|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "solvers/ipopt/IpStdCInterface.h" |
| 4 | +#include "pyoptinterface/nlcore.hpp" |
| 5 | + |
| 6 | +#define APILIST \ |
| 7 | + B(CreateIpoptProblem); \ |
| 8 | + B(FreeIpoptProblem); \ |
| 9 | + B(AddIpoptStrOption); \ |
| 10 | + B(AddIpoptNumOption); \ |
| 11 | + B(AddIpoptIntOption); \ |
| 12 | + B(OpenIpoptOutputFile); \ |
| 13 | + B(SetIpoptProblemScaling); \ |
| 14 | + B(SetIntermediateCallback); \ |
| 15 | + B(IpoptSolve); \ |
| 16 | + B(GetIpoptCurrentIterate); \ |
| 17 | + B(GetIpoptCurrentViolations); |
| 18 | + |
| 19 | +namespace ipopt |
| 20 | +{ |
| 21 | +#define B(f) extern decltype(&::f) f |
| 22 | + |
| 23 | +APILIST |
| 24 | + |
| 25 | +#undef B |
| 26 | + |
| 27 | +bool is_library_loaded(); |
| 28 | + |
| 29 | +bool load_library(const std::string &path); |
| 30 | +} // namespace ipopt |
| 31 | + |
| 32 | +struct IpoptfreeproblemT |
| 33 | +{ |
| 34 | + void operator()(IpoptProblemInfo *model) const |
| 35 | + { |
| 36 | + ipopt::FreeIpoptProblem(model); |
| 37 | + }; |
| 38 | +}; |
| 39 | + |
| 40 | +struct IpoptResult |
| 41 | +{ |
| 42 | + // store results |
| 43 | + std::vector<double> x, g, mult_g, mult_x_L, mult_x_U; |
| 44 | + double obj_val; |
| 45 | +}; |
| 46 | + |
| 47 | +struct IpoptModel |
| 48 | +{ |
| 49 | + /* Methods */ |
| 50 | + IpoptModel(); |
| 51 | + |
| 52 | + VariableIndex add_variable(double lb = -INFINITY, double ub = INFINITY, double start = 0.0); |
| 53 | + void change_variable_lb(const VariableIndex &variable, double lb); |
| 54 | + void change_variable_ub(const VariableIndex &variable, double ub); |
| 55 | + void change_variable_bounds(const VariableIndex &variable, double lb, double ub); |
| 56 | + double get_variable_value(const VariableIndex &variable); |
| 57 | + |
| 58 | + ParameterIndex add_parameter(double value = 0.0); |
| 59 | + void set_parameter(const ParameterIndex ¶meter, double value); |
| 60 | + |
| 61 | + ConstraintIndex add_linear_constraint(const ScalarAffineFunction &f, ConstraintSense sense, |
| 62 | + double rhs); |
| 63 | + ConstraintIndex add_linear_constraint(const ScalarAffineFunction &f, ConstraintSense sense, |
| 64 | + double lb, double ub); |
| 65 | + ConstraintIndex add_linear_constraint(const ExprBuilder &f, ConstraintSense sense, double rhs); |
| 66 | + ConstraintIndex add_linear_constraint(const ExprBuilder &f, ConstraintSense sense, double lb, |
| 67 | + double ub); |
| 68 | + ConstraintIndex add_linear_constraint(const VariableIndex &f, ConstraintSense sense, |
| 69 | + double rhs); |
| 70 | + ConstraintIndex add_linear_constraint(const VariableIndex &f, ConstraintSense sense, double lb, |
| 71 | + double ub); |
| 72 | + |
| 73 | + ConstraintIndex add_quadratic_constraint(const ScalarQuadraticFunction &f, |
| 74 | + ConstraintSense sense, double rhs); |
| 75 | + ConstraintIndex add_quadratic_constraint(const ScalarQuadraticFunction &f, |
| 76 | + ConstraintSense sense, double lb, double ub); |
| 77 | + ConstraintIndex add_quadratic_constraint(const ExprBuilder &f, ConstraintSense sense, |
| 78 | + double rhs); |
| 79 | + ConstraintIndex add_quadratic_constraint(const ExprBuilder &f, ConstraintSense sense, double lb, |
| 80 | + double ub); |
| 81 | + |
| 82 | + template <typename T> |
| 83 | + void add_objective(const T &expr) |
| 84 | + { |
| 85 | + m_lq_model.add_objective(expr); |
| 86 | + } |
| 87 | + |
| 88 | + FunctionIndex register_function(ADFunD &f, const std::string &name); |
| 89 | + |
| 90 | + NLConstraintIndex add_empty_nl_constraint(int dim, ConstraintSense sense, |
| 91 | + const std::vector<double> &rhss); |
| 92 | + NLConstraintIndex add_empty_nl_constraint(int dim, ConstraintSense sense, |
| 93 | + const std::vector<double> &lbs, |
| 94 | + const std::vector<double> &ubs); |
| 95 | + |
| 96 | + NLConstraintIndex add_nl_constraint(const FunctionIndex &k, |
| 97 | + const std::vector<VariableIndex> &xs, |
| 98 | + const std::vector<ParameterIndex> &ps, |
| 99 | + ConstraintSense sense, const std::vector<double> &rhss); |
| 100 | + NLConstraintIndex add_nl_constraint(const FunctionIndex &k, |
| 101 | + const std::vector<VariableIndex> &xs, ConstraintSense sense, |
| 102 | + const std::vector<double> &rhss); |
| 103 | + |
| 104 | + NLConstraintIndex add_nl_constraint(const FunctionIndex &k, |
| 105 | + const std::vector<VariableIndex> &xs, |
| 106 | + const std::vector<ParameterIndex> &ps, |
| 107 | + ConstraintSense sense, const std::vector<double> &lbs, |
| 108 | + const std::vector<double> &ubs); |
| 109 | + NLConstraintIndex add_nl_constraint(const FunctionIndex &k, |
| 110 | + const std::vector<VariableIndex> &xs, ConstraintSense sense, |
| 111 | + const std::vector<double> &lbs, |
| 112 | + const std::vector<double> &ubs); |
| 113 | + |
| 114 | + void add_nl_expression(const NLConstraintIndex &constraint, const FunctionIndex &k, |
| 115 | + const std::vector<VariableIndex> &xs, |
| 116 | + const std::vector<ParameterIndex> &ps); |
| 117 | + void add_nl_expression(const NLConstraintIndex &constraint,const FunctionIndex &k, |
| 118 | + const std::vector<VariableIndex> &xs); |
| 119 | + |
| 120 | + void add_nl_objective(const FunctionIndex &k, const std::vector<VariableIndex> &xs, |
| 121 | + const std::vector<ParameterIndex> &ps); |
| 122 | + |
| 123 | + void optimize(); |
| 124 | + |
| 125 | + // set options |
| 126 | + void set_option_int(const std::string &name, int value); |
| 127 | + void set_option_num(const std::string &name, double value); |
| 128 | + void set_option_str(const std::string &name, const std::string &value); |
| 129 | + |
| 130 | + /* Members */ |
| 131 | + |
| 132 | + size_t n_variables = 0; |
| 133 | + size_t n_constraints = 0; |
| 134 | + |
| 135 | + std::vector<double> m_var_lb, m_var_ub, m_var_init; |
| 136 | + std::vector<double> m_con_lb, m_con_ub; |
| 137 | + |
| 138 | + size_t m_jacobian_nnz = 0; |
| 139 | + std::vector<size_t> m_jacobian_rows, m_jacobian_cols; |
| 140 | + |
| 141 | + size_t m_hessian_nnz = 0; |
| 142 | + std::vector<size_t> m_hessian_rows, m_hessian_cols; |
| 143 | + Hashmap<VariablePair, size_t> m_hessian_index_map; |
| 144 | + |
| 145 | + NonlinearFunctionModel m_function_model; |
| 146 | + LinearQuadraticModel m_lq_model; |
| 147 | + |
| 148 | + // The options of the Ipopt solver, we cache them before constructing the m_problem |
| 149 | + Hashmap<std::string, int> m_options_int; |
| 150 | + Hashmap<std::string, double> m_options_num; |
| 151 | + Hashmap<std::string, std::string> m_options_str; |
| 152 | + |
| 153 | + IpoptResult m_result; |
| 154 | + enum ApplicationReturnStatus m_status; |
| 155 | + |
| 156 | + std::unique_ptr<IpoptProblemInfo, IpoptfreeproblemT> m_problem = nullptr; |
| 157 | +}; |
0 commit comments