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 pulp cbc cmd solver params #778

Merged
merged 6 commits into from
Oct 22, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Move tests to PULP_CBC_CMDTest
  • Loading branch information
marcusreaiche committed Oct 5, 2024
commit 44fd3480d782809a9c3be73892326e54b1c591a1
304 changes: 144 additions & 160 deletions pulp/tests/test_pulp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,166 +1036,6 @@ def test_logPath(self):
if not os.path.getsize(logFilename):
raise PulpError(f"Test failed for solver: {self.solver}")

def test_presolve_off(self):
"""
Test if setting presolve=False in PULP_CBC_CMD adds presolve off to the
command line.
"""
name = self._testMethodName
prob = LpProblem(name, const.LpMinimize)
x = LpVariable("x", 0, 4)
y = LpVariable("y", -1, 1)
z = LpVariable("z", 0)
w = LpVariable("w", 0)
prob += x + 4 * y + 9 * z, "obj"
prob += x + y <= 5, "c1"
prob += x + z >= 10, "c2"
prob += -y + z == 7, "c3"
prob += w >= 0, "c4"
logFilename = name + ".log"
self.solver.optionsDict["logPath"] = logFilename
self.solver.optionsDict["presolve"] = False
if self.solver.name in [
"PULP_CBC_CMD",
]:
pulpTestCheck(
prob,
self.solver,
[const.LpStatusOptimal],
{x: 4, y: -1, z: 6, w: 0},
)
if not os.path.exists(logFilename):
raise PulpError(f"Test failed for solver: {self.solver}")
if not os.path.getsize(logFilename):
raise PulpError(f"Test failed for solver: {self.solver}")
command_line = read_command_line_from_log_file(logFilename)
option_value = extract_option_from_command_line(
command_line, option="presolve"
)
self.assertEqual("off", option_value)

def test_cuts_on(self):
"""
Test if setting cuts=True in PULP_CBC_CMD adds "gomory on knapsack on
probing on" to the command line.
"""
name = self._testMethodName
prob = LpProblem(name, const.LpMinimize)
x = LpVariable("x", 0, 4)
y = LpVariable("y", -1, 1)
z = LpVariable("z", 0)
w = LpVariable("w", 0)
prob += x + 4 * y + 9 * z, "obj"
prob += x + y <= 5, "c1"
prob += x + z >= 10, "c2"
prob += -y + z == 7, "c3"
prob += w >= 0, "c4"
logFilename = name + ".log"
self.solver.optionsDict["logPath"] = logFilename
self.solver.optionsDict["cuts"] = True
if self.solver.name in [
"PULP_CBC_CMD",
]:
pulpTestCheck(
prob,
self.solver,
[const.LpStatusOptimal],
{x: 4, y: -1, z: 6, w: 0},
)
if not os.path.exists(logFilename):
raise PulpError(f"Test failed for solver: {self.solver}")
if not os.path.getsize(logFilename):
raise PulpError(f"Test failed for solver: {self.solver}")
command_line = read_command_line_from_log_file(logFilename)
gomory_value = extract_option_from_command_line(
command_line, option="gomory"
)
knapsack_value = extract_option_from_command_line(
command_line, option="knapsack", prefix=""
)
probing_value = extract_option_from_command_line(
command_line, option="probing", prefix=""
)
self.assertListEqual(
["on", "on", "on"], [gomory_value, knapsack_value, probing_value]
)

def test_cuts_off(self):
"""
Test if setting cuts=False in PULP_CBC_CMD adds cuts off to the
command line.
"""
name = self._testMethodName
prob = LpProblem(name, const.LpMinimize)
x = LpVariable("x", 0, 4)
y = LpVariable("y", -1, 1)
z = LpVariable("z", 0)
w = LpVariable("w", 0)
prob += x + 4 * y + 9 * z, "obj"
prob += x + y <= 5, "c1"
prob += x + z >= 10, "c2"
prob += -y + z == 7, "c3"
prob += w >= 0, "c4"
logFilename = name + ".log"
self.solver.optionsDict["logPath"] = logFilename
self.solver.optionsDict["cuts"] = False
if self.solver.name in [
"PULP_CBC_CMD",
]:
pulpTestCheck(
prob,
self.solver,
[const.LpStatusOptimal],
{x: 4, y: -1, z: 6, w: 0},
)
if not os.path.exists(logFilename):
raise PulpError(f"Test failed for solver: {self.solver}")
if not os.path.getsize(logFilename):
raise PulpError(f"Test failed for solver: {self.solver}")
command_line = read_command_line_from_log_file(logFilename)
option_value = extract_option_from_command_line(
command_line, option="cuts"
)
self.assertEqual("off", option_value)

def test_strong(self):
"""
Test if setting strong=10 in PULP_CBC_CMD adds strong 10 to the
command line.
"""
name = self._testMethodName
prob = LpProblem(name, const.LpMinimize)
x = LpVariable("x", 0, 4)
y = LpVariable("y", -1, 1)
z = LpVariable("z", 0)
w = LpVariable("w", 0)
prob += x + 4 * y + 9 * z, "obj"
prob += x + y <= 5, "c1"
prob += x + z >= 10, "c2"
prob += -y + z == 7, "c3"
prob += w >= 0, "c4"
logFilename = name + ".log"
self.solver.optionsDict["logPath"] = logFilename
self.solver.optionsDict["strong"] = 10
if self.solver.name in [
"PULP_CBC_CMD",
]:
pulpTestCheck(
prob,
self.solver,
[const.LpStatusOptimal],
{x: 4, y: -1, z: 6, w: 0},
)
if not os.path.exists(logFilename):
raise PulpError(f"Test failed for solver: {self.solver}")
if not os.path.getsize(logFilename):
raise PulpError(f"Test failed for solver: {self.solver}")
command_line = read_command_line_from_log_file(logFilename)
option_value = extract_option_from_command_line(
command_line, option="strong", grp_pattern="\d+"
)
self.assertEqual("10", option_value)

def test_makeDict_behavior(self):
"""
Test if makeDict is returning the expected value.
Expand Down Expand Up @@ -1604,6 +1444,150 @@ def test_multiply_nan_values(self):
class PULP_CBC_CMDTest(BaseSolverTest.PuLPTest):
solveInst = PULP_CBC_CMD

def test_presolve_off(self):
"""
Test if setting presolve=False in PULP_CBC_CMD adds presolve off to the
command line.
"""
name = self._testMethodName
prob = LpProblem(name, const.LpMinimize)
x = LpVariable("x", 0, 4)
y = LpVariable("y", -1, 1)
z = LpVariable("z", 0)
w = LpVariable("w", 0)
prob += x + 4 * y + 9 * z, "obj"
prob += x + y <= 5, "c1"
prob += x + z >= 10, "c2"
prob += -y + z == 7, "c3"
prob += w >= 0, "c4"
logFilename = name + ".log"
self.solver.optionsDict["logPath"] = logFilename
self.solver.optionsDict["presolve"] = False
pulpTestCheck(
prob,
self.solver,
[const.LpStatusOptimal],
{x: 4, y: -1, z: 6, w: 0},
)
if not os.path.exists(logFilename):
raise PulpError(f"Test failed for solver: {self.solver}")
if not os.path.getsize(logFilename):
raise PulpError(f"Test failed for solver: {self.solver}")
# Extract option_value from command line
command_line = read_command_line_from_log_file(logFilename)
option_value = extract_option_from_command_line(command_line, option="presolve")
self.assertEqual("off", option_value)

def test_cuts_on(self):
"""
Test if setting cuts=True in PULP_CBC_CMD adds "gomory on knapsack on
probing on" to the command line.
"""
name = self._testMethodName
prob = LpProblem(name, const.LpMinimize)
x = LpVariable("x", 0, 4)
y = LpVariable("y", -1, 1)
z = LpVariable("z", 0)
w = LpVariable("w", 0)
prob += x + 4 * y + 9 * z, "obj"
prob += x + y <= 5, "c1"
prob += x + z >= 10, "c2"
prob += -y + z == 7, "c3"
prob += w >= 0, "c4"
logFilename = name + ".log"
self.solver.optionsDict["logPath"] = logFilename
self.solver.optionsDict["cuts"] = True
pulpTestCheck(
prob,
self.solver,
[const.LpStatusOptimal],
{x: 4, y: -1, z: 6, w: 0},
)
if not os.path.exists(logFilename):
raise PulpError(f"Test failed for solver: {self.solver}")
if not os.path.getsize(logFilename):
raise PulpError(f"Test failed for solver: {self.solver}")
# Extract option values from command line
command_line = read_command_line_from_log_file(logFilename)
gomory_value = extract_option_from_command_line(command_line, option="gomory")
knapsack_value = extract_option_from_command_line(
command_line, option="knapsack", prefix=""
)
probing_value = extract_option_from_command_line(
command_line, option="probing", prefix=""
)
self.assertListEqual(
["on", "on", "on"], [gomory_value, knapsack_value, probing_value]
)

def test_cuts_off(self):
"""
Test if setting cuts=False adds cuts off to the command line.
"""
name = self._testMethodName
prob = LpProblem(name, const.LpMinimize)
x = LpVariable("x", 0, 4)
y = LpVariable("y", -1, 1)
z = LpVariable("z", 0)
w = LpVariable("w", 0)
prob += x + 4 * y + 9 * z, "obj"
prob += x + y <= 5, "c1"
prob += x + z >= 10, "c2"
prob += -y + z == 7, "c3"
prob += w >= 0, "c4"
logFilename = name + ".log"
self.solver.optionsDict["logPath"] = logFilename
self.solver.optionsDict["cuts"] = False
pulpTestCheck(
prob,
self.solver,
[const.LpStatusOptimal],
{x: 4, y: -1, z: 6, w: 0},
)
if not os.path.exists(logFilename):
raise PulpError(f"Test failed for solver: {self.solver}")
if not os.path.getsize(logFilename):
raise PulpError(f"Test failed for solver: {self.solver}")
# Extract option value from the command line
command_line = read_command_line_from_log_file(logFilename)
option_value = extract_option_from_command_line(command_line, option="cuts")
self.assertEqual("off", option_value)

def test_strong(self):
"""
Test if setting strong=10 adds strong 10 to the command line.
"""
name = self._testMethodName
prob = LpProblem(name, const.LpMinimize)
x = LpVariable("x", 0, 4)
y = LpVariable("y", -1, 1)
z = LpVariable("z", 0)
w = LpVariable("w", 0)
prob += x + 4 * y + 9 * z, "obj"
prob += x + y <= 5, "c1"
prob += x + z >= 10, "c2"
prob += -y + z == 7, "c3"
prob += w >= 0, "c4"
logFilename = name + ".log"
self.solver.optionsDict["logPath"] = logFilename
self.solver.optionsDict["strong"] = 10
pulpTestCheck(
prob,
self.solver,
[const.LpStatusOptimal],
{x: 4, y: -1, z: 6, w: 0},
)
if not os.path.exists(logFilename):
raise PulpError(f"Test failed for solver: {self.solver}")
if not os.path.getsize(logFilename):
raise PulpError(f"Test failed for solver: {self.solver}")
# Extract option value from command line
command_line = read_command_line_from_log_file(logFilename)
option_value = extract_option_from_command_line(
command_line, option="strong", grp_pattern="\d+"
)
self.assertEqual("10", option_value)


class CPLEX_CMDTest(BaseSolverTest.PuLPTest):
solveInst = CPLEX_CMD
Expand Down