Skip to content

Commit 7a8a0fa

Browse files
committed
Modify tools
1 parent 267fe0e commit 7a8a0fa

2 files changed

Lines changed: 36 additions & 29 deletions

File tree

s2mpj_tools.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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):
481482
def _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

494497
def _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

507511
def _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

521524
def _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

src/s2mpjlib.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,14 +374,14 @@ def evalgrsum( self, isobj, glist, x, nargout ):
374374

375375
Htimesx = self.H .dot(x)
376376
if nargout == 1:
377-
fx += 0.5 * x.T .dot(Htimesx)
377+
fx += float(np.array(0.5 * x.T .dot(Htimesx)).item())
378378
elif nargout == 2:
379379
gx += Htimesx
380-
fx += 0.5 * x.T .dot(Htimesx)
380+
fx += float(np.array(0.5 * x.T .dot(Htimesx)).item())
381381
elif nargout == 3:
382382
Htimesx = self.H .dot(x);
383383
gx += Htimesx
384-
fx += 0.5 * x.T .dot(Htimesx)
384+
fx += float(np.array(0.5 * x.T .dot(Htimesx)).item())
385385
Hx += self.H
386386

387387
if debug: #D
@@ -1005,6 +1005,12 @@ def find( lst, condition ):
10051005

10061006
def arrset( arr, index, value ):
10071007

1008+
if hasattr(value, 'item') and not isinstance(value, (float, int, complex, np.number)):
1009+
try:
1010+
if value.size == 1:
1011+
value = value.item()
1012+
except:
1013+
pass
10081014
if isinstance( index, np.ndarray):
10091015
maxind = np.max( index )
10101016
else:

0 commit comments

Comments
 (0)