-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cut generation: additional options, query instance features
- Loading branch information
Showing
36 changed files
with
1,272 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"""This example reads a MIP (in .lp or .mps), solves its linear programming | ||
relaxation and then tests the impact of adding different types of cutting | ||
planes.""" | ||
|
||
import sys | ||
from mip import Model, features, compute_features | ||
import mip | ||
|
||
lp_path = "" | ||
|
||
# using test data, replace with your instance | ||
lp_path = mip.__file__.replace("mip/__init__.py", "test/data/1443_0-9.lp").replace( | ||
"mip\\__init__.py", "test\\data\\1443_0-9.lp" | ||
) | ||
|
||
m = Model() | ||
if m.solver_name.upper() in ["GRB", "GUROBI"]: | ||
print("This feature is only supported in CBC.") | ||
else: | ||
m.read(lp_path) | ||
|
||
print("instance features:") | ||
X = compute_features(m) | ||
for i, fn in enumerate(features()): | ||
print("%s: %g" % (fn, X[i])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"""Example of use of general cutting planes in a small MIP.""" | ||
|
||
import sys | ||
from mip import Model, INTEGER, maximize, CutType, OptimizationStatus | ||
|
||
larger_diff = -1 | ||
best_cut = None | ||
|
||
for ct in CutType: | ||
print("Trying cut type: {}".format(ct.name)) | ||
|
||
m = Model() | ||
if m.solver_name.upper() in ["GRB", "GUROBI"]: | ||
print("This feature is currently not supported in Gurobi.") | ||
else: | ||
m.verbose = 0 | ||
|
||
x = m.add_var_tensor(shape=(2, 1), name="x", var_type=INTEGER) | ||
|
||
m.objective = maximize(2 * x[0] + x[1]) | ||
|
||
m += 7 * x[0] + x[1] <= 28 | ||
m += -x[0] + 3 * x[1] <= 7 | ||
m += -8 * x[0] - 9 * x[1] <= -32 | ||
|
||
m.optimize(relax=True) | ||
olr = m.objective_value | ||
|
||
cp = m.generate_cuts([ct]) | ||
if cp and cp.cuts: | ||
print("{} cuts generated:".format(len(cp.cuts))) | ||
for c in cp.cuts: | ||
print(" " + str(c)) | ||
|
||
if cp.cuts: | ||
m += cp | ||
m.optimize(relax=True) | ||
|
||
print("Dual bound now: {}".format(m.objective_value)) | ||
assert m.status == OptimizationStatus.OPTIMAL | ||
diff = m.objective_value - olr | ||
if diff > larger_diff: | ||
larger_diff = diff | ||
best_cut = ct | ||
|
||
print("Best cut: {}".format(best_cut)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
"""This example reads a MIP (in .lp or .mps), solves its linear programming | ||
relaxation and then tests the impact of adding different types of cutting | ||
planes. In the end, the it informs which cut generator produced the best bound | ||
improvement.""" | ||
|
||
from textwrap import shorten | ||
import sys | ||
from mip import Model, CutType, OptimizationStatus | ||
import mip | ||
|
||
lp_path = "" | ||
|
||
# using test data | ||
lp_path = mip.__file__.replace("mip/__init__.py", "test/data/1443_0-9.lp").replace( | ||
"mip\\__init__.py", "test\\data\\1443_0-9.lp" | ||
) | ||
|
||
m = Model() | ||
if m.solver_name.upper() in ["GRB", "GUROBI"]: | ||
print("This feature is currently not supported in Gurobi.") | ||
else: | ||
m.read(lp_path) | ||
|
||
m.verbose = 0 | ||
m.optimize(relax=True) | ||
print("Original LP bound: {}".format(m.objective_value)) | ||
|
||
best_impr = -1 | ||
best_cut = "" | ||
|
||
for ct in CutType: | ||
print() | ||
m2 = m.copy() | ||
m2.verbose = 0 | ||
m2.optimize(relax=True) | ||
assert ( | ||
m2.status == OptimizationStatus.OPTIMAL | ||
and abs(m2.objective_value - m.objective_value) <= 1e-4 | ||
) | ||
print("Searching for violated {} inequalities ...".format(ct.name)) | ||
cp = m2.generate_cuts([ct]) | ||
if cp and cp.cuts: | ||
print("{} cuts found:".format(len(cp.cuts))) | ||
for c in cp.cuts[0 : min(10, len(cp.cuts))]: | ||
print(" {}".format(shorten(str(c), width=90, placeholder="..."))) | ||
m2 += cp | ||
m2.optimize(relax=True) | ||
perc_impr = ( | ||
abs(m2.objective_value - m.objective_value) | ||
/ max(abs(m2.objective_value), abs(m.objective_value)) | ||
) * 100 | ||
|
||
if perc_impr > best_impr: | ||
best_impr = perc_impr | ||
best_cut = ct | ||
|
||
print( | ||
"Linear programming relaxation bound now: %g, improvement of %.2f" | ||
% (m2.objective_value, perc_impr) | ||
) | ||
else: | ||
continue | ||
|
||
print("Best cut: {} improved dual bound: {:.2f} ".format(best_cut, best_impr)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.