0

I'm running a Python code I wrote that involves using scipy.optimize.minimize method='trust-constr'. The optimization involves a set of linear constraints, which I pass to Python using scipy.optimize.LinearConstraint where I pass in a matrix (the matrix of coefficients of the variables) and two vectors (lower and upper bounds for each of the constraints) of numbers.

It ran fine three times, each time with different initial values for parameters, but on the fourth time it crashed due to NaN. After hours of searching, I found the issue: the minimize function (unsurprisingly) involves calling the function-to-be-minimized many times. One time it was called, deep into the minimize operation (certainly long past the starting point x0), one of the arguments passed to the objective function had the value nan.

I was wondering how trust-costr revises which values to evaluate the objective function at, and why it may have picked nan as the value for one of the variables?

Sorry for the lack of a replicable example. I'm not sure how to replicate this problem simply.

Edit: Here is console output, from the fatal call to minimize onwards

result=minimize(objective,x0,method='trust-constr',constraints=[prob_constraints],options={'xtol':10**(-10)}).x

File "C:\Users(my name)\Anaconda3\lib\site-packages\scipy\optimize_minimize.py", line 612, in minimize callback=callback, **options)

File "C:\Users(my name)\Anaconda3\lib\site-packages\scipy\optimize_trustregion_constr\minimize_trustregion_constr.py", line 519, in _minimize_trustregion_constr factorization_method)

File "C:\Users(my name)\Anaconda3\lib\site-packages\scipy\optimize_trustregion_constr\tr_interior_point.py", line 329, in tr_interior_point factorization_method, trust_lb, trust_ub, subprob.scaling)

File "C:\Users(my name)\Anaconda3\lib\site-packages\scipy\optimize_trustregion_constr\equality_constrained_sqp.py", line 121, in equality_constrained_sqp lb_t, ub_t)

File "C:\Users(my name)\Anaconda3\lib\site-packages\scipy\optimize_trustregion_constr\qp_subproblem.py", line 499, in projected_cg r = Z.dot(H.dot(x) + c)

File "C:\Users(my name)\Anaconda3\lib\site-packages\scipy\sparse\linalg\interface.py", line 370, in dot return self.matvec(x)

File "C:\Users(my name)\Anaconda3\lib\site-packages\scipy\sparse\linalg\interface.py", line 227, in matvec y = self._matvec(x)

File "C:\Users(my name)\Anaconda3\lib\site-packages\scipy\sparse\linalg\interface.py", line 479, in _matvec return self.__matvec_impl(x)

File "C:\Users(my name)\Anaconda3\lib\site-packages\scipy\optimize_trustregion_constr\projections.py", line 193, in null_space aux2 = scipy.linalg.solve_triangular(R, aux1, lower=False)

File "C:\Users(my name)\Anaconda3\lib\site-packages\scipy\linalg\basic.py", line 336, in solve_triangular b1 = _asarray_validated(b, check_finite=check_finite)

File "C:\Users(my name)\Anaconda3\lib\site-packages\scipy_lib_util.py", line 239, in _asarray_validated a = toarray(a)

File "C:\Users(my name)\Anaconda3\lib\site-packages\numpy\lib\function_base.py", line 498, in asarray_chkfinite "array must not contain infs or NaNs")

ValueError: array must not contain infs or NaNs

J.D.
  • 139
  • 4
  • 14

1 Answers1

0

One possible explanation for trust-costr attempting to evaluate nan values in your objective function, is that, at some point in the optimisation, your objective function returned nan to trust-costr.

This would "put off" the algorithm which would then try to evaluate your functions at nan

You might want to look into bounding your optimisation problem to prevent the algorithm from venturing into places where your objective function is susceptible to return nans

Malo Pocheau
  • 101
  • 4