@@ -298,52 +298,41 @@ def get_inequality_constraint_indices(self, constraints):
298
298
299
299
# overloaded from NLP
300
300
def get_obj_scaling (self ):
301
- obj = self .get_pyomo_objective ()
302
- val = SuffixFinder ('scaling_factor' , context = self ._pyomo_model ).find (obj )
303
- # maintain backwards compatibility
304
- scaling_suffix = self ._pyomo_model .component ('scaling_factor' )
305
- if scaling_suffix and scaling_suffix .ctype is pyo .Suffix :
306
- return 1.0 if val is None else val
307
- else :
308
- return val
301
+ scaling_finder = SuffixFinder (
302
+ 'scaling_factor' , default = 1.0 , context = self ._pyomo_model
303
+ )
304
+ val = scaling_finder .find (self .get_pyomo_objective ())
305
+ if not scaling_finder .all_suffixes :
306
+ return None
307
+ return val
309
308
310
309
# overloaded from NLP
311
310
def get_primals_scaling (self ):
312
- scaling_suffix_finder = SuffixFinder (
313
- 'scaling_factor' , context = self ._pyomo_model
311
+ scaling_finder = SuffixFinder (
312
+ 'scaling_factor' , default = 1.0 , context = self ._pyomo_model
314
313
)
315
- primals_scaling = np .ones (self .n_primals ())
316
- ret = None
317
- for i , v in enumerate (self .get_pyomo_variables ()):
318
- val = scaling_suffix_finder .find (v )
319
- if val is not None :
320
- primals_scaling [i ] = val
321
- ret = primals_scaling
322
- # maintain backwards compatibility
323
- scaling_suffix = self ._pyomo_model .component ('scaling_factor' )
324
- if scaling_suffix and scaling_suffix .ctype is pyo .Suffix :
325
- return primals_scaling
326
- else :
327
- return ret
314
+ primals_scaling = np .fromiter (
315
+ (scaling_finder .find (v ) for v in self .get_pyomo_variables ()),
316
+ count = self .n_primals (),
317
+ dtype = float ,
318
+ )
319
+ if not scaling_finder .all_suffixes :
320
+ return None
321
+ return primals_scaling
328
322
329
323
# overloaded from NLP
330
324
def get_constraints_scaling (self ):
331
- scaling_suffix_finder = SuffixFinder (
332
- 'scaling_factor' , context = self ._pyomo_model
325
+ scaling_finder = SuffixFinder (
326
+ 'scaling_factor' , default = 1.0 , context = self ._pyomo_model
333
327
)
334
- constraints_scaling = np .ones (self .n_constraints ())
335
- ret = None
336
- for i , c in enumerate (self .get_pyomo_constraints ()):
337
- val = scaling_suffix_finder .find (c )
338
- if val is not None :
339
- constraints_scaling [i ] = val
340
- ret = constraints_scaling
341
- # maintain backwards compatibility
342
- scaling_suffix = self ._pyomo_model .component ('scaling_factor' )
343
- if scaling_suffix and scaling_suffix .ctype is pyo .Suffix :
344
- return constraints_scaling
345
- else :
346
- return ret
328
+ constraints_scaling = np .fromiter (
329
+ (scaling_finder .find (v ) for v in self .get_pyomo_constraints ()),
330
+ count = self .n_constraints (),
331
+ dtype = float ,
332
+ )
333
+ if not scaling_finder .all_suffixes :
334
+ return None
335
+ return constraints_scaling
347
336
348
337
def extract_subvector_grad_objective (self , pyomo_variables ):
349
338
"""Compute the gradient of the objective and return the entries
@@ -624,19 +613,15 @@ def __init__(self, pyomo_model):
624
613
else :
625
614
need_scaling = True
626
615
627
- self ._primals_scaling = np .ones (self .n_primals ())
628
- scaling_suffix_finder = SuffixFinder (
629
- 'scaling_factor' , context = self ._pyomo_model
616
+ scaling_finder = SuffixFinder (
617
+ 'scaling_factor' , default = 1.0 , context = self ._pyomo_model
630
618
)
631
- for i , v in enumerate (self .get_pyomo_variables ()):
632
- v_scaling = scaling_suffix_finder .find (v )
633
- if v_scaling is not None :
634
- need_scaling = True
635
- self ._primals_scaling [i ] = v_scaling
636
- # maintain backwards compatibility
637
- scaling_suffix = self ._pyomo_model .component ('scaling_factor' )
638
- if scaling_suffix and scaling_suffix .ctype is pyo .Suffix :
639
- need_scaling = True
619
+ self ._primals_scaling = np .fromiter (
620
+ (scaling_finder .find (v ) for v in self .get_pyomo_variables ()),
621
+ count = self .n_primals (),
622
+ dtype = float ,
623
+ )
624
+ need_scaling = bool (scaling_finder .all_suffixes )
640
625
641
626
self ._constraints_scaling = []
642
627
pyomo_nlp_scaling = self ._pyomo_nlp .get_constraints_scaling ()
0 commit comments