Skip to content

Commit 71837e2

Browse files
committed
Allow explicitly setting intermediate_solutions to False
Resolves #193
1 parent 40635e8 commit 71837e2

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ Changed
3737
``Instance.solve``, ``Instance.solve_async``, and ``Instance.solutions``. The
3838
``timeout`` parameter is still accepted, but will add a
3939
``DeprecationWarning`` and will be removed in future versions.
40+
- The ``intermediate_solutions`` parameter can now be explicitly set to
41+
``False`` to avoid the ``-i`` flag to be passed to MiniZinc, which is
42+
generally added to ensure that a final solution is available.
4043

4144
Fixed
4245
^^^^^

src/minizinc/instance.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def solve(
110110
processes: Optional[int] = None,
111111
random_seed: Optional[int] = None,
112112
all_solutions: bool = False,
113-
intermediate_solutions: bool = False,
113+
intermediate_solutions: Optional[bool] = None,
114114
free_search: bool = False,
115115
optimisation_level: Optional[int] = None,
116116
timeout: Optional[timedelta] = None,
@@ -141,10 +141,13 @@ def solve(
141141
all_solutions (bool): Request to solver to find all solutions. (Only
142142
available on satisfaction problems and when the ``-a`` flag is
143143
supported by the solver)
144-
intermediate_solutions (bool): Request the solver to output any
145-
intermediate solutions that are found during the solving
146-
process. (Only available on optimisation problems and when the
147-
``-a`` flag is supported by the solver)
144+
intermediate_solutions (Optional[bool]): Request the solver to
145+
output any intermediate solutions that are found during the
146+
solving process. If left to ``None``, then intermediate
147+
solutions might still be requested to ensure that the solving
148+
process gives its final solution. (Only available on
149+
optimisation problems and when the ``-i`` or ``-a`` flag is
150+
supported by the solver)
148151
optimisation_level (Optional[int]): Set the MiniZinc compiler
149152
optimisation level.
150153
@@ -207,8 +210,8 @@ async def solve_async(
207210
nr_solutions: Optional[int] = None,
208211
processes: Optional[int] = None,
209212
random_seed: Optional[int] = None,
210-
all_solutions=False,
211-
intermediate_solutions=False,
213+
all_solutions: bool = False,
214+
intermediate_solutions: Optional[bool] = None,
212215
free_search: bool = False,
213216
optimisation_level: Optional[int] = None,
214217
timeout: Optional[timedelta] = None,
@@ -256,6 +259,7 @@ async def solve_async(
256259
statistics.update(result.statistics)
257260
if result.solution is not None:
258261
if multiple_solutions:
262+
assert solution is not None
259263
solution.append(result.solution)
260264
else:
261265
solution = result.solution
@@ -454,8 +458,8 @@ async def solutions(
454458
nr_solutions: Optional[int] = None,
455459
processes: Optional[int] = None,
456460
random_seed: Optional[int] = None,
457-
all_solutions=False,
458-
intermediate_solutions=False,
461+
all_solutions: bool = False,
462+
intermediate_solutions: Optional[bool] = None,
459463
free_search: bool = False,
460464
optimisation_level: Optional[int] = None,
461465
verbose: bool = False,
@@ -537,12 +541,20 @@ async def solutions(
537541
"Solver does not support the -n-o flag"
538542
)
539543
cmd.extend(["--num-optimal", str(nr_solutions)])
540-
elif (
541-
"-i" not in self._solver.stdFlags
542-
or "-a" not in self._solver.stdFlags
544+
elif intermediate_solutions:
545+
if (
546+
"-i" not in self._solver.stdFlags
547+
and "-a" not in self._solver.stdFlags
548+
):
549+
raise NotImplementedError(
550+
"Solver does not support the -i and -a flags"
551+
)
552+
cmd.append("--intermediate-solutions")
553+
elif (intermediate_solutions is None and time_limit is not None) and (
554+
"-i" in self._solver.stdFlags or "-a" in self._solver.stdFlags
543555
):
544-
# Enable intermediate solutions when possible
545-
# (ensure that solvers always output their best solution)
556+
# Enable intermediate solutions just in case to ensure that there is
557+
# a best solution available at the time limit.
546558
cmd.append("--intermediate-solutions")
547559
# Set number of processes to be used
548560
if processes is not None:

0 commit comments

Comments
 (0)