@@ -118,15 +118,15 @@ def s2mpj_load(problem_name, *args):
118118 grad = lambda x : _getgrad (p , is_feasibility , x )
119119 hess = lambda x : _gethess (p , is_feasibility , x )
120120
121- x0 = p .x0
122- xl = p .xlower
123- xu = p .xupper
121+ x0 = p .x0 . flatten ()
122+ xl = p .xlower . flatten ()
123+ xu = p .xupper . flatten ()
124124 # We replace 1.0e+20 (which represents infinity) bounds with np.inf.
125125 xl = np .where (xl <= - 1.0e+20 , - np .inf , xl )
126126 xu = np .where (xu >= 1.0e+20 , np .inf , xu )
127127
128- cl = p .clower if hasattr (p , 'clower' ) else None
129- cu = p .cupper if hasattr (p , 'cupper' ) else None
128+ cl = p .clower . flatten () if hasattr (p , 'clower' ) else None
129+ cu = p .cupper . flatten () if hasattr (p , 'cupper' ) else None
130130 if cl is not None :
131131 cl = np .where (cl <= - 1.0e+20 , - np .inf , cl )
132132 if cu is not None :
@@ -148,11 +148,12 @@ def s2mpj_load(problem_name, *args):
148148 buf = io .StringIO ()
149149 with redirect_stdout (buf ):
150150 try :
151- cx , jx = p .cJx (x0 )[:2 ]
151+ cx , jx = p .cJx (p . x0 )[:2 ]
152152 if hasattr (cx , 'toarray' ):
153153 cx = cx .toarray ()
154154 if hasattr (jx , 'toarray' ):
155155 jx = jx .toarray ()
156+ cx = cx .flatten ()
156157 bx = jx @ x0 - cx
157158 except Exception :
158159 jx = None
@@ -193,13 +194,13 @@ def s2mpj_load(problem_name, *args):
193194 def ceq (x ):
194195 if idx_ceq .size == 0 : return None
195196 y = _getcx (p , x )
196- if y is None : return None
197+ if y is None : return np . full ( idx_ceq . size , np . nan )
197198 z = y [idx_ceq ] - cu [idx_ceq ]
198- return None if (hasattr (z , "size" ) and z .size == 0 ) else z
199+ return np . full ( idx_ceq . size , np . nan ) if (hasattr (z , "size" ) and z .size == 0 ) else z
199200 def cub (x ):
200201 if idx_cle .size == 0 and idx_cge .size == 0 : return None
201202 y = _getcx (p , x )
202- if y is None : return None
203+ if y is None : return np . full ( idx_cle . size + idx_cge . size , np . nan )
203204 le = y [idx_cle ] - cu [idx_cle ] if idx_cle .size > 0 else None
204205 ge = y [idx_cge ] - cl [idx_cge ] if idx_cge .size > 0 else None
205206 if le is None and ge is None : return None
@@ -209,25 +210,25 @@ def cub(x):
209210 def hceq (x ):
210211 if idx_ceq .size == 0 : return []
211212 Hx = _getHx (p , x )
212- if Hx is None : raise RuntimeError ( "Hessian evaluation failed" )
213+ if Hx is None : return [ np . full (( x . size , x . size ), np . nan )] * idx_ceq . size
213214 return [Hx [i ] for i in idx_ceq ]
214215 def hcub (x ):
215216 if idx_cle .size == 0 and idx_cge .size == 0 : return []
216217 Hx = _getHx (p , x )
217- if Hx is None : raise RuntimeError ( "Hessian evaluation failed" )
218+ if Hx is None : return [ np . full (( x . size , x . size ), np . nan )] * ( idx_cle . size + idx_cge . size )
218219 le = [Hx [i ] for i in idx_cle ] if idx_cle .size > 0 else []
219220 ge = [Hx [i ] for i in idx_cge ] if idx_cge .size > 0 else []
220221 return le + ge
221222
222223 def jceq (x ):
223224 if idx_ceq .size == 0 : return np .empty ((0 , p .n ))
224225 Jx = _getJx (p , x )
225- if Jx is None : return None
226+ if Jx is None : return np . full (( idx_ceq . size , x . size ), np . nan )
226227 return Jx [idx_ceq , :]
227228 def jcub (x ):
228229 if idx_cle .size == 0 and idx_cge .size == 0 : return np .empty ((0 , p .n ))
229230 Jx = _getJx (p , x )
230- if Jx is None : return None
231+ if Jx is None : return np . full (( idx_cle . size + idx_cge . size , x . size ), np . nan )
231232 le = Jx [idx_cle , :] if idx_cle .size > 0 else None
232233 ge = Jx [idx_cge , :] if idx_cge .size > 0 else None
233234 if le is None and ge is None : return np .empty ((0 , p .n ))
@@ -481,14 +482,16 @@ def safe_convert(value, default=0):
481482def _getfun (p , is_feasibility , problem_name , x ):
482483 if is_feasibility and problem_name != 'HS8' :
483484 # Note that 'HS8' has a constant objective function (-1).
484- f = 0
485+ f = 0.0
485486 else :
486487 buf = io .StringIO ()
487488 with redirect_stdout (buf ):
488489 try :
489490 f = p .fx (x )
491+ if hasattr (f , 'item' ):
492+ f = f .item ()
490493 except Exception :
491- f = np .empty ( 0 )
494+ f = np .nan
492495 return f
493496
494497def _getgrad (p , is_feasibility , x ):
@@ -500,8 +503,9 @@ def _getgrad(p, is_feasibility, x):
500503 try :
501504 _ , g = p .fgx (x )
502505 g = g .toarray () if hasattr (g , 'toarray' ) else g
506+ g = g .flatten ()
503507 except Exception :
504- g = None
508+ g = np . full ( x . size , np . nan )
505509 return g
506510
507511def _gethess (p , is_feasibility , x ):
@@ -513,9 +517,8 @@ def _gethess(p, is_feasibility, x):
513517 try :
514518 _ , _ , h = p .fgHx (x )
515519 h = h .toarray () if hasattr (h , 'toarray' ) else h
516- except Exception as e :
517- print (f"Error eval Hx: { e } " )
518- h = None
520+ except Exception :
521+ h = np .full ((x .size , x .size ), np .nan )
519522 return h
520523
521524def _getcx (p , x ):
@@ -524,8 +527,8 @@ def _getcx(p, x):
524527 try :
525528 c = p .cx (x )
526529 c = c .toarray () if hasattr (c , 'toarray' ) else c
527- except Exception as e :
528- print ( f"Error eval cx: { e } " )
530+ c = c . flatten ()
531+ except Exception :
529532 c = None
530533 return c
531534
@@ -535,8 +538,7 @@ def _getJx(p, x):
535538 try :
536539 _ , j = p .cJx (x )[:2 ]
537540 j = j .toarray () if hasattr (j , 'toarray' ) else j
538- except Exception as e :
539- print (f"Error eval Jx: { e } " )
541+ except Exception :
540542 j = None
541543 return j
542544
@@ -548,7 +550,6 @@ def _getHx(p, x):
548550 for i in range (len (h )):
549551 if hasattr (h [i ], 'toarray' ):
550552 h [i ] = h [i ].toarray ()
551- except Exception as e :
552- print (f"Error eval Hx: { e } " )
553- h = None
553+ except Exception :
554+ h = None
554555 return h
0 commit comments