Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix objective size when setting quadratic term #25

Merged
merged 2 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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