@@ -175,30 +175,35 @@ def solve_nlp(problem, solver, warm_start, verbose, **kwargs):
175175 The optimal problem value.
176176 """
177177 nlp_chain , kwargs = _build_nlp_chain (problem , solver , kwargs )
178- best_of = kwargs .pop ("best_of" , 1 )
179-
180- if not isinstance (best_of , int ) or best_of < 1 :
181- raise ValueError ("best_of must be a positive integer." )
182-
178+
183179 # Standard single solve
184- if best_of == 1 :
180+ if " best_of" not in kwargs :
185181 _set_nlp_initial_point (problem )
186182 canon_problem , inverse_data = nlp_chain .apply (problem = problem )
187183 solution = nlp_chain .solver .solve_via_data (canon_problem , warm_start ,
188184 verbose , solver_opts = kwargs )
189185 problem .unpack_results (solution , nlp_chain , inverse_data )
190186 return problem .value
187+
188+ best_of = kwargs .pop ("best_of" )
189+ if not isinstance (best_of , int ) or best_of < 1 :
190+ raise ValueError ("best_of must be a positive integer." )
191191
192192 # Best-of-N solve
193193 best_obj , best_solution = float ("inf" ), None
194194 all_objs = np .zeros (shape = (best_of ,))
195195 user_initials = {}
196196
197+ # inside solve_via_data we cache the construction of the C problem
198+ # (which includes the construction of the oracles)
199+ solver_cache = {}
200+
197201 for run in range (best_of ):
198202 _set_random_nlp_initial_point (problem , run , user_initials )
199203 canon_problem , inverse_data = nlp_chain .apply (problem = problem )
200204 solution = nlp_chain .solver .solve_via_data (canon_problem , warm_start ,
201- verbose , solver_opts = kwargs )
205+ verbose , solver_opts = kwargs ,
206+ solver_cache = solver_cache )
202207
203208 # Unpack to get the objective value in the original problem space
204209 problem .unpack_results (solution , nlp_chain , inverse_data )
@@ -212,6 +217,7 @@ def solve_nlp(problem, solver, warm_start, verbose, **kwargs):
212217 if verbose :
213218 print ("Run %d/%d: obj = %.6e | best so far = %.6e"
214219 % (run + 1 , best_of , obj_value , best_obj ))
220+ print ("-" * 60 )
215221
216222 # Unpack best solution
217223 if type (problem .objective ) == Maximize :
0 commit comments