Skip to content

Commit 32abc16

Browse files
committed
Nanobind 2.0 and IPOPT test case
1 parent 59932a7 commit 32abc16

File tree

16 files changed

+687
-141
lines changed

16 files changed

+687
-141
lines changed

include/pyoptinterface/ipopt_model.hpp

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,35 +49,60 @@ struct IpoptModel
4949
/* Methods */
5050
IpoptModel();
5151

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);
52+
VariableIndex add_variable(double lb = -INFINITY, double ub = INFINITY, double start = 0.0,
53+
const char *name = nullptr);
54+
double get_variable_lb(const VariableIndex &variable);
55+
double get_variable_ub(const VariableIndex &variable);
56+
void set_variable_lb(const VariableIndex &variable, double lb);
57+
void set_variable_ub(const VariableIndex &variable, double ub);
58+
void set_variable_bounds(const VariableIndex &variable, double lb, double ub);
59+
60+
double get_variable_start(const VariableIndex &variable);
61+
void set_variable_start(const VariableIndex &variable, double start);
62+
63+
std::string get_variable_name(const VariableIndex &variable);
64+
void set_variable_name(const VariableIndex &variable, const std::string &name);
65+
5666
double get_variable_value(const VariableIndex &variable);
67+
double get_expression_value(const ScalarAffineFunction &function);
68+
double get_expression_value(const ScalarQuadraticFunction &function);
69+
double get_expression_value(const ExprBuilder &function);
70+
71+
std::string pprint_variable(const VariableIndex &variable);
72+
std::string pprint_expression(const ScalarAffineFunction &function, int precision = 4);
73+
std::string pprint_expression(const ScalarQuadraticFunction &function, int precision = 4);
74+
std::string pprint_expression(const ExprBuilder &function, int precision = 4);
5775

5876
ParameterIndex add_parameter(double value = 0.0);
5977
void set_parameter(const ParameterIndex &parameter, double value);
6078

79+
double get_obj_value();
80+
double get_constraint_primal(IndexT index);
81+
double get_constraint_dual(IndexT index);
82+
6183
ConstraintIndex add_linear_constraint(const ScalarAffineFunction &f, ConstraintSense sense,
62-
double rhs);
84+
double rhs, const char *name = nullptr);
6385
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);
86+
double lb, double ub, const char *name = nullptr);
87+
ConstraintIndex add_linear_constraint(const ExprBuilder &f, ConstraintSense sense, double rhs,
88+
const char *name = nullptr);
6689
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);
90+
double ub, const char *name = nullptr);
91+
ConstraintIndex add_linear_constraint(const VariableIndex &f, ConstraintSense sense, double rhs,
92+
const char *name = nullptr);
7093
ConstraintIndex add_linear_constraint(const VariableIndex &f, ConstraintSense sense, double lb,
71-
double ub);
94+
double ub, const char *name = nullptr);
7295

7396
ConstraintIndex add_quadratic_constraint(const ScalarQuadraticFunction &f,
74-
ConstraintSense sense, double rhs);
97+
ConstraintSense sense, double rhs,
98+
const char *name = nullptr);
7599
ConstraintIndex add_quadratic_constraint(const ScalarQuadraticFunction &f,
76-
ConstraintSense sense, double lb, double ub);
100+
ConstraintSense sense, double lb, double ub,
101+
const char *name = nullptr);
77102
ConstraintIndex add_quadratic_constraint(const ExprBuilder &f, ConstraintSense sense,
78-
double rhs);
103+
double rhs, const char *name = nullptr);
79104
ConstraintIndex add_quadratic_constraint(const ExprBuilder &f, ConstraintSense sense, double lb,
80-
double ub);
105+
double ub, const char *name = nullptr);
81106

82107
template <typename T>
83108
void add_objective(const T &expr)
@@ -99,8 +124,8 @@ struct IpoptModel
99124
ConstraintSense sense, const std::vector<double> &rhss);
100125
NLConstraintIndex add_nl_constraint(const FunctionIndex &k,
101126
const std::vector<VariableIndex> &xs,
102-
const std::vector<double> &ps,
103-
ConstraintSense sense, const std::vector<double> &rhss);
127+
const std::vector<double> &ps, ConstraintSense sense,
128+
const std::vector<double> &rhss);
104129
NLConstraintIndex add_nl_constraint(const FunctionIndex &k,
105130
const std::vector<VariableIndex> &xs, ConstraintSense sense,
106131
const std::vector<double> &rhss);
@@ -112,8 +137,8 @@ struct IpoptModel
112137
const std::vector<double> &ubs);
113138
NLConstraintIndex add_nl_constraint(const FunctionIndex &k,
114139
const std::vector<VariableIndex> &xs,
115-
const std::vector<double> &ps,
116-
ConstraintSense sense, const std::vector<double> &lbs,
140+
const std::vector<double> &ps, ConstraintSense sense,
141+
const std::vector<double> &lbs,
117142
const std::vector<double> &ubs);
118143
NLConstraintIndex add_nl_constraint(const FunctionIndex &k,
119144
const std::vector<VariableIndex> &xs, ConstraintSense sense,
@@ -124,20 +149,22 @@ struct IpoptModel
124149
const std::vector<VariableIndex> &xs,
125150
const std::vector<ParameterIndex> &ps);
126151
void add_nl_expression(const NLConstraintIndex &constraint, const FunctionIndex &k,
127-
const std::vector<VariableIndex> &xs,
128-
const std::vector<double> &ps);
152+
const std::vector<VariableIndex> &xs, const std::vector<double> &ps);
129153
void add_nl_expression(const NLConstraintIndex &constraint, const FunctionIndex &k,
130154
const std::vector<VariableIndex> &xs);
131155

156+
void add_nl_objective(const FunctionIndex &k, const std::vector<VariableIndex> &xs);
132157
void add_nl_objective(const FunctionIndex &k, const std::vector<VariableIndex> &xs,
133158
const std::vector<ParameterIndex> &ps);
159+
void add_nl_objective(const FunctionIndex &k, const std::vector<VariableIndex> &xs,
160+
const std::vector<double> &ps);
134161

135162
void optimize();
136163

137164
// set options
138-
void set_option_int(const std::string &name, int value);
139-
void set_option_num(const std::string &name, double value);
140-
void set_option_str(const std::string &name, const std::string &value);
165+
void set_raw_option_int(const std::string &name, int value);
166+
void set_raw_option_double(const std::string &name, double value);
167+
void set_raw_option_string(const std::string &name, const std::string &value);
141168

142169
/* Members */
143170

@@ -147,6 +174,8 @@ struct IpoptModel
147174
std::vector<double> m_var_lb, m_var_ub, m_var_init;
148175
std::vector<double> m_con_lb, m_con_ub;
149176

177+
Hashmap<IndexT, std::string> m_var_names, m_con_names;
178+
150179
size_t m_jacobian_nnz = 0;
151180
std::vector<size_t> m_jacobian_rows, m_jacobian_cols;
152181

include/pyoptinterface/solver_common.hpp

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,9 @@ ConstraintIndex CommercialSolverMixin<T>::add_quadratic_constraint_from_expr(
8787
return get_base()->add_quadratic_constraint(f, sense, rhs, name);
8888
}
8989

90-
template <CommercialSolverConstraint T>
91-
double CommercialSolverMixin<T>::get_expression_value(const ScalarAffineFunction &function)
90+
template <typename T>
91+
double get_affine_expression_value(T *model, const ScalarAffineFunction &function)
9292
{
93-
T *model = get_base();
9493
auto N = function.size();
9594
double value = 0.0;
9695
for (auto i = 0; i < N; ++i)
@@ -102,9 +101,15 @@ double CommercialSolverMixin<T>::get_expression_value(const ScalarAffineFunction
102101
}
103102

104103
template <CommercialSolverConstraint T>
105-
double CommercialSolverMixin<T>::get_expression_value(const ScalarQuadraticFunction &function)
104+
double CommercialSolverMixin<T>::get_expression_value(const ScalarAffineFunction &function)
106105
{
107106
T *model = get_base();
107+
return ::get_affine_expression_value<T>(model, function);
108+
}
109+
110+
template <typename T>
111+
double get_quadratic_expression_value(T *model, const ScalarQuadraticFunction &function)
112+
{
108113
auto N = function.size();
109114
double value = 0.0;
110115
for (auto i = 0; i < N; ++i)
@@ -125,16 +130,22 @@ double CommercialSolverMixin<T>::get_expression_value(const ScalarQuadraticFunct
125130
}
126131
if (function.affine_part)
127132
{
128-
auto affine_value = get_expression_value(function.affine_part.value());
133+
auto affine_value = ::get_affine_expression_value(model, function.affine_part.value());
129134
value += affine_value;
130135
}
131136
return value;
132137
}
133138

134139
template <CommercialSolverConstraint T>
135-
double CommercialSolverMixin<T>::get_expression_value(const ExprBuilder &function)
140+
double CommercialSolverMixin<T>::get_expression_value(const ScalarQuadraticFunction &function)
136141
{
137142
T *model = get_base();
143+
return ::get_quadratic_expression_value<T>(model, function);
144+
}
145+
146+
template <typename T>
147+
double get_expression_builder_value(T *model, const ExprBuilder &function)
148+
{
138149
double value = 0.0;
139150
for (const auto &[varpair, coef] : function.quadratic_terms)
140151
{
@@ -161,10 +172,15 @@ double CommercialSolverMixin<T>::get_expression_value(const ExprBuilder &functio
161172
}
162173

163174
template <CommercialSolverConstraint T>
164-
std::string CommercialSolverMixin<T>::pprint_expression(const ScalarAffineFunction &function,
165-
int precision)
175+
double CommercialSolverMixin<T>::get_expression_value(const ExprBuilder &function)
166176
{
167177
T *model = get_base();
178+
return ::get_expression_builder_value<T>(model, function);
179+
}
180+
181+
template <typename T>
182+
std::string pprint_affine_expression(T *model, const ScalarAffineFunction &function, int precision)
183+
{
168184
auto N = function.size();
169185
std::vector<std::string> terms;
170186
terms.reserve(N + 1);
@@ -192,10 +208,17 @@ std::string CommercialSolverMixin<T>::pprint_expression(const ScalarAffineFuncti
192208
}
193209

194210
template <CommercialSolverConstraint T>
195-
std::string CommercialSolverMixin<T>::pprint_expression(const ScalarQuadraticFunction &function,
211+
std::string CommercialSolverMixin<T>::pprint_expression(const ScalarAffineFunction &function,
196212
int precision)
197213
{
198214
T *model = get_base();
215+
return ::pprint_affine_expression<T>(model, function, precision);
216+
}
217+
218+
template <typename T>
219+
std::string pprint_quadratic_expression(T *model, const ScalarQuadraticFunction &function,
220+
int precision)
221+
{
199222
auto N = function.size();
200223
std::vector<std::string> terms;
201224
terms.reserve(N + 1);
@@ -226,15 +249,22 @@ std::string CommercialSolverMixin<T>::pprint_expression(const ScalarQuadraticFun
226249
}
227250
if (function.affine_part)
228251
{
229-
terms.push_back(pprint_expression(function.affine_part.value(), precision));
252+
terms.push_back(::pprint_affine_expression(model, function.affine_part.value(), precision));
230253
}
231254
return fmt::format("{}", fmt::join(terms, "+"));
232255
}
233256

234257
template <CommercialSolverConstraint T>
235-
std::string CommercialSolverMixin<T>::pprint_expression(const ExprBuilder &function, int precision)
258+
std::string CommercialSolverMixin<T>::pprint_expression(const ScalarQuadraticFunction &function,
259+
int precision)
236260
{
237261
T *model = get_base();
262+
return ::pprint_quadratic_expression<T>(model, function, precision);
263+
}
264+
265+
template <typename T>
266+
std::string pprint_expression_builder(T *model, const ExprBuilder &function, int precision)
267+
{
238268
std::vector<std::string> terms;
239269
terms.reserve(function.quadratic_terms.size() + function.affine_terms.size() + 1);
240270

@@ -282,6 +312,13 @@ std::string CommercialSolverMixin<T>::pprint_expression(const ExprBuilder &funct
282312
return fmt::format("{}", fmt::join(terms, "+"));
283313
}
284314

315+
template <CommercialSolverConstraint T>
316+
std::string CommercialSolverMixin<T>::pprint_expression(const ExprBuilder &function, int precision)
317+
{
318+
T *model = get_base();
319+
return ::pprint_expression_builder<T>(model, function, precision);
320+
}
321+
285322
template <CommercialSolverConstraint T>
286323
void CommercialSolverMixin<T>::set_objective_as_constant(CoeffT c, ObjectiveSense sense)
287324
{

lib/highs_model_ext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ NB_MODULE(highs_model_ext, m)
102102
nb::overload_cast<CoeffT, ObjectiveSense>(&HighsModelMixin::set_objective_as_constant),
103103
nb::arg("expr"), nb::arg("sense") = ObjectiveSense::Minimize)
104104

105-
.def("optimize", &HighsModelMixin::optimize, nb::call_guard<nb::gil_scoped_release>())
105+
.def("optimize", &HighsModelMixin::optimize, nb::call_guard<nb::gil_scoped_release>())
106106

107107
// clang-format off
108108
BIND_F(version_string)

0 commit comments

Comments
 (0)