Skip to content

Commit 458515d

Browse files
authored
Merge pull request #74 from eminyouskn/knitro-set-obj-coeff
[KNITRO] Add support for set objective coeff
2 parents e8284e7 + 623ae51 commit 458515d

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

include/pyoptinterface/knitro_model.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
B(KN_add_obj_quadratic_struct); \
5757
B(KN_del_obj_quadratic_struct); \
5858
B(KN_del_obj_quadratic_struct_all); \
59+
B(KN_chg_obj_linear_term); \
5960
B(KN_add_con_constant); \
6061
B(KN_add_con_linear_struct); \
6162
B(KN_add_con_linear_term); \
@@ -400,6 +401,7 @@ class KNITROModel : public OnesideLinearConstraintMixin<KNITROModel>,
400401
double get_obj_value() const;
401402
void set_obj_sense(ObjectiveSense sense);
402403
ObjectiveSense get_obj_sense() const;
404+
void set_objective_coefficient(const VariableIndex &variable, double coefficient);
403405

404406
// Solve functions
405407
void optimize();

lib/knitro_model.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,17 @@ void KNITROModel::set_objective(const ExprBuilder &expr, ObjectiveSense sense)
646646
}
647647
}
648648

649+
void KNITROModel::set_objective_coefficient(const VariableIndex &variable, double coefficient)
650+
{
651+
KNINT indexVar = _variable_index(variable);
652+
// NOTE: To make sure the coefficient is updated correctly,
653+
// we need to call KN_update before changing the linear term
654+
_update();
655+
int error = knitro::KN_chg_obj_linear_term(m_kc.get(), indexVar, coefficient);
656+
_check_error(error);
657+
m_is_dirty = true;
658+
}
659+
649660
void KNITROModel::add_single_nl_objective(ExpressionGraph &graph, const ExpressionHandle &result)
650661
{
651662
_add_graph(graph);

lib/knitro_model_ext.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ NB_MODULE(knitro_model_ext, m)
154154
nb::arg("expr"), nb::arg("sense") = ObjectiveSense::Minimize)
155155
.def("_add_single_nl_objective", &KNITROModel::add_single_nl_objective, nb::arg("graph"),
156156
nb::arg("result"))
157+
.def("set_objective_coefficient", &KNITROModel::set_objective_coefficient, nb::arg("variable"),
158+
nb::arg("coefficient"))
157159

158160
// clang-format off
159161
BIND_F(get_obj_value)

tests/test_update.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,12 @@ def test_update(model_interface):
3737

3838
assert model.get_value(x[0]) == approx(2.0)
3939
assert model.get_value(x[2]) == approx(1.0)
40+
41+
model.set_variable_attribute(x[0], poi.VariableAttribute.LowerBound, 1.5)
42+
model.set_variable_attribute(x[2], poi.VariableAttribute.LowerBound, 0.5)
43+
model.set_objective_coefficient(x[0], -2.0)
44+
model.set_obj_sense(poi.ObjectiveSense.Maximize)
45+
model.optimize()
46+
47+
assert model.get_value(x[0]) == approx(1.5)
48+
assert model.get_value(x[2]) == approx(0.75)

0 commit comments

Comments
 (0)