Skip to content

Commit

Permalink
[#93] Add dataframe and eval tests
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbowly committed Nov 15, 2024
1 parent aaef3e9 commit c26707c
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def test_inequality(self):
def test_wrong_usage(self):
index = pd.RangeIndex(3)
x = gppd.add_vars(self.model, index, name="x")
y = gppd.add_vars(self.model, index, name="x")
y = gppd.add_vars(self.model, index, name="y")

with self.assertRaisesRegex(
gp.GurobiError, "Objective must be linear or quadratic"
Expand All @@ -355,6 +355,55 @@ def test_wrong_usage(self):
):
gppd.add_constrs(self.model, y**4, GRB.EQUAL, x)

with self.assertRaisesRegex(
ValueError, "Nonlinear constraints must be in the form"
):
x.to_frame().gppd.add_constrs(self.model, "x**3 == 1", name="bad")

def test_eval(self):
index = pd.RangeIndex(3)
df = (
gppd.add_vars(self.model, index, name="x")
.to_frame()
.gppd.add_vars(self.model, name="y")
.gppd.add_constrs(self.model, "y == x**3", name="nlconstr")
)

self.model.update()
for row in df.itertuples(index=False):
self.assert_nlconstr_equal(
row.nlconstr,
row.y,
[
(GRB.OPCODE_POW, -1.0, -1),
(GRB.OPCODE_VARIABLE, row.x, 0),
(GRB.OPCODE_CONSTANT, 3.0, 0),
],
)

def test_frame(self):
from gurobipy import nlfunc

index = pd.RangeIndex(3)
df = (
gppd.add_vars(self.model, index, name="x")
.to_frame()
.gppd.add_vars(self.model, name="y")
.assign(exp_x=lambda df: df["x"].apply(nlfunc.exp))
.gppd.add_constrs(self.model, "y", GRB.EQUAL, "exp_x", name="nlconstr")
)

self.model.update()
for row in df.itertuples(index=False):
self.assert_nlconstr_equal(
row.nlconstr,
row.y,
[
(GRB.OPCODE_EXP, -1.0, -1),
(GRB.OPCODE_VARIABLE, row.x, 0),
],
)


class TestDataValidation(GurobiModelTestCase):
# Test that we throw some more informative errors, instead of obscure
Expand Down

0 comments on commit c26707c

Please sign in to comment.