@@ -609,6 +609,7 @@ def write(self, model):
609609 del suffix_data ['scaling_factor' ]
610610 else :
611611 scaling_factor = _NoScalingFactor ()
612+ scaling_cache = None
612613 scale_model = scaling_factor .scale
613614
614615 timer .toc ("Collected suffixes" , level = logging .DEBUG )
@@ -1360,7 +1361,7 @@ def write(self, model):
13601361 # Note: checking target_expr == 0 is equivalent to
13611362 # testing "(_con_id is not None and _obj_id is not None)
13621363 # or _con_id == 0 or _obj_id == 0"
1363- self ._write_v_line (_id , 0 )
1364+ self ._write_v_line (_id , 0 , scale_model , scaling_cache )
13641365 else :
13651366 if target_expr not in single_use_subexpressions :
13661367 single_use_subexpressions [target_expr ] = []
@@ -1395,7 +1396,7 @@ def write(self, model):
13951396 break
13961397 if single_use_subexpressions :
13971398 for _id in single_use_subexpressions .get (id (info [0 ]), ()):
1398- self ._write_v_line (_id , row_idx + 1 )
1399+ self ._write_v_line (_id , row_idx + 1 , scale_model , scaling_cache )
13991400 ostream .write (f'C{ row_idx } { row_comments [row_idx ]} \n ' )
14001401 self ._write_nl_expression (info [1 ], False )
14011402
@@ -1408,7 +1409,9 @@ def write(self, model):
14081409 # Note that "Writing .nl files" (2005) is incorrectly
14091410 # missing the "+ 1" in the description of V lines
14101411 # appearing in only Objectives (bottom of page 9).
1411- self ._write_v_line (_id , n_cons + n_lcons + obj_idx + 1 )
1412+ self ._write_v_line (
1413+ _id , n_cons + n_lcons + obj_idx + 1 , scale_model , scaling_cache
1414+ )
14121415 lbl = row_comments [n_cons + obj_idx ]
14131416 sense = 0 if info [0 ].sense == minimize else 1
14141417 ostream .write (f'O{ obj_idx } { sense } { lbl } \n ' )
@@ -1982,7 +1985,7 @@ def _write_nl_expression(self, repn, include_const):
19821985 else :
19831986 self .ostream .write (self .template .const % 0 )
19841987
1985- def _write_v_line (self , expr_id , k ):
1988+ def _write_v_line (self , expr_id , k , scale_model , scaling_cache ):
19861989 ostream = self .ostream
19871990 column_order = self .column_order
19881991 info = self .subexpression_cache [expr_id ]
@@ -1994,10 +1997,14 @@ def _write_v_line(self, expr_id, k):
19941997 # Do NOT write out 0 coefficients here: doing so fouls up the
19951998 # ASL's logic for calculating derivatives, leading to 'nan' in
19961999 # the Hessian results.
1997- linear = dict (item for item in info [1 ].linear .items () if item [1 ])
2000+ linear = info [1 ].linear
2001+ linear_ids = list (_id for _id , coef in linear .items () if coef )
2002+ if scale_model :
2003+ for _id in linear_ids :
2004+ linear [_id ] /= scaling_cache [_id ]
19982005 #
1999- ostream .write (f'V{ self .next_V_line_id } { len (linear )} { k } { lbl } \n ' )
2000- for _id in sorted (linear , key = column_order .__getitem__ ):
2006+ ostream .write (f'V{ self .next_V_line_id } { len (linear_ids )} { k } { lbl } \n ' )
2007+ for _id in sorted (linear_ids , key = column_order .__getitem__ ):
20012008 ostream .write (f'{ column_order [_id ]} { linear [_id ]!s} \n ' )
20022009 self ._write_nl_expression (info [1 ], True )
20032010 self .next_V_line_id += 1
0 commit comments