Skip to content

Commit

Permalink
fix objective size when setting quadratic term (#25)
Browse files Browse the repository at this point in the history
* fix resize

* add test
  • Loading branch information
tlambert03 authored Apr 10, 2023
1 parent 8a45606 commit 074e1cf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ilpy/impl/solvers/Objective.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ Objective::getCoefficients() const {
void
Objective::setQuadraticCoefficient(unsigned int varNum1, unsigned int varNum2, double coef) {

if (varNum1 >= size())
resize(varNum1 + 1);
if (varNum2 >= size())
resize(varNum2 + 1);

if (coef == 0) {

_quadraticCoefs.erase(std::make_pair(varNum1, varNum2));
Expand Down
16 changes: 16 additions & 0 deletions tests/test_objective.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import ilpy


def test_resize() -> None:
obj = ilpy.Objective()
assert len(obj) == 0

obj.set_coefficient(0, 2) # linear term (2x)
assert len(obj) == 1

obj.set_quadratic_coefficient(1, 1, 3) # quadratic term (3y^2)
assert len(obj) == 2

obj2 = ilpy.Objective()
obj2.set_quadratic_coefficient(0, 0, 1) # quadratic term (x^2)
assert len(obj2) == 1

0 comments on commit 074e1cf

Please sign in to comment.