|
17 | 17 | def optimal(
|
18 | 18 | binner: Binner, numbins: int, items: List[any], entitlements: List[any] = None,
|
19 | 19 | copies=1,
|
20 |
| - time_limit=inf, |
| 20 | + time_limit=inf, # time limit in seconds |
21 | 21 | verbose=0,
|
22 |
| - solver_name=mip.CBC, # passed to MIP. See https://docs.python-mip.com/en/latest/quickstart.html#creating-models. |
23 |
| - # solver_name = mip.GRB, # passed to MIP. See https://docs.python-mip.com/en/latest/quickstart.html#creating-models. |
| 22 | + solver_name=mip.CBC, # [or mip.GRB] passed to MIP. See https://docs.python-mip.com/en/latest/quickstart.html#creating-models. |
24 | 23 | model_filename = None,
|
25 | 24 | solution_filename = None,
|
26 | 25 | ):
|
@@ -83,7 +82,7 @@ def optimal(
|
83 | 82 | iitems = range(len(items))
|
84 | 83 | if isinstance(copies, Number):
|
85 | 84 | copies = {iitem: copies for iitem in iitems}
|
86 |
| - model = mip.Model(name = '', solver_name=solver_name) |
| 85 | + model = mip.Model(name='', sense='MIN', solver_name=solver_name) |
87 | 86 | counts: dict = {
|
88 | 87 | iitem: [model.add_var(var_type=mip.INTEGER) for ibin in ibins]
|
89 | 88 | for iitem in iitems
|
@@ -132,8 +131,15 @@ def optimal(
|
132 | 131 | model.write(model_filename)
|
133 | 132 | # logger.info("MIP model: %s", model)
|
134 | 133 | status = model.optimize(max_seconds=time_limit)
|
135 |
| - if status != mip.OptimizationStatus.OPTIMAL: |
136 |
| - raise ValueError(f"Problem status is not optimal - it is {status}.") |
| 134 | + |
| 135 | + # Check problem status: |
| 136 | + if time_limit == inf: |
| 137 | + if status != mip.OptimizationStatus.OPTIMAL: |
| 138 | + raise ValueError(f"Problem status is not optimal - it is {status}.") |
| 139 | + else: |
| 140 | + if status not in {mip.OptimizationStatus.OPTIMAL,mip.OptimizationStatus.FEASIBLE}: |
| 141 | + raise ValueError(f"Problem status is neither optimal nor feasible - it is {status}.") |
| 142 | + |
137 | 143 |
|
138 | 144 | # Construct the output:
|
139 | 145 | output = binner.new_bins(numbins)
|
|
0 commit comments