4343"""
4444
4545import shutil
46+ import time
4647import warnings
4748
4849from .solver_interface import SolverInterface , SolverStatus , ExitStatus
@@ -230,14 +231,28 @@ def solveAll(self, display=None, time_limit=None, solution_limit=None, call_from
230231
231232 docp = self .get_docp ()
232233 solution_count = 0
233- while solution_limit is None or solution_count < solution_limit :
234- cpo_result = self .solve (time_limit = time_limit , ** kwargs )
235- if not cpo_result :
236- break
234+ start = time .time ()
235+ while ((time_limit is None ) or (time_limit > 0 )) and self .solve (time_limit = time_limit , ** kwargs ):
236+
237+ # display if needed
238+ if display is not None :
239+ if isinstance (display , Expression ):
240+ print (argval (display ))
241+ elif isinstance (display , list ):
242+ print (argvals (display ))
243+ else :
244+ display () # callback
245+
246+ # count and stop
237247 solution_count += 1
248+ if solution_count == solution_limit :
249+ break
250+
238251 if self .has_objective ():
239252 # only find all optimal solutions
240253 self .cpo_model .add (self .cpo_model .get_objective_expression ().children [0 ] == self .objective_value_ )
254+
255+ # add nogood on the user variables
241256 solvars = []
242257 vals = []
243258 for cpm_var in self .user_vars :
@@ -251,13 +266,26 @@ def solveAll(self, display=None, time_limit=None, solution_limit=None, call_from
251266 solvars .append (sol_var )
252267 vals .append (cpm_value )
253268 self .cpo_model .add (docp .modeler .forbidden_assignments (solvars , [vals ]))
254- if display is not None :
255- if isinstance (display , Expression ):
256- print (argval (display ))
257- elif isinstance (display , list ):
258- print (argvals (display ))
259- else :
260- display () # callback
269+
270+ if time_limit is not None : # update remaining time
271+ time_limit -= self .status ().runtime
272+ end = time .time ()
273+
274+ # update solver status
275+ self .cpm_status .runtime = end - start
276+ if solution_count :
277+ if solution_count == solution_limit :
278+ self .cpm_status .exitstatus = ExitStatus .FEASIBLE
279+ elif self .cpm_status .exitstatus == ExitStatus .UNSATISFIABLE :
280+ self .cpm_status .exitstatus = ExitStatus .OPTIMAL
281+ else :
282+ self .cpm_status .exitstatus = ExitStatus .FEASIBLE
283+ # else: <- is implicit since nothing needs to update
284+ # if self.cpm_status.exitstatus == ExitStatus.UNSATISFIABLE:
285+ # self.cpm_status.exitstatus = ExitStatus.UNSATISFIABLE
286+ # elif self.cpm_status.exitstatus == ExitStatus.UNKNOWN:
287+ # self.cpm_status.exitstatus = ExitStatus.UNKNOWN
288+
261289 return solution_count
262290
263291 def solver_var (self , cpm_var ):
0 commit comments