Changeset 2210


Ignore:
Timestamp:
Jan 30, 2010 10:11:21 PM (10 years ago)
Author:
jwatson
Message:

Additional minor fixings to make new LP writer work with PH - which it now does.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/trunk/coopr/pyomo/io/cpxlp.py

    r2209 r2210  
    141141        return offset
    142142
     143    def _print_quadterm(self, x, is_minimizing, OUTPUT):
     144        # the LP format doesn't allow for expression of constant terms in the objective.
     145        # a work-around involves tracking the sum of constant terms in the quadratic
     146        # quadratic terms, and then writing that out with a dummy variable forced equal
     147        # to one.
     148        print >>OUTPUT, ""
     149        for arg in x._args:
     150            if isinstance(arg,expr._ProductExpression):
     151                # NOTE: we need to handle quadratics defined with the 'pow' term here.
     152                # WARNING: The following code is very specific to progressive hedging, with
     153                #          a very specific format assumed. we do need to handle more general
     154                #          expressions, but we'll worry about that at a latter time.
     155                blend = arg._numerator[0]()
     156
     157                if blend is 1:
     158
     159                   rho = arg._numerator[1]()
     160
     161                   pow_expression = arg._numerator[2]
     162               
     163                   base = pow_expression._args[0]
     164                   exponent = pow_expression._args[1]
     165
     166                   if not isinstance(base,expr._SumExpression):
     167                       raise ValueError, "Quadratic term base must be a _SumExpression"
     168                   if not isinstance(exponent,numvalue.NumericConstant):
     169                       raise ValueError, "Quadratic term exponent must be a NumericConstant"
     170                   variable = base._args[0]
     171                   offset = base._args[1]
     172                   if variable.status is not VarStatus.unused:
     173
     174                      if is_minimizing is True:
     175                         print >>OUTPUT, " + [" + str(rho) + " " + convert_name(variable.label) + "^2]/2",
     176                      else:
     177                         print >>OUTPUT, " - [" + str(rho) + " " + convert_name(variable.label) + "^2]/2",
     178
     179                      if (is_minimizing is True):
     180                         if offset.value < 0.0:
     181                            print >>OUTPUT, " + " + str(abs(rho*offset.value)) + " " + convert_name(variable.label),                   
     182                         else:
     183                            print >>OUTPUT, " - " + str(rho*offset.value) + " " + convert_name(variable.label),
     184                      else:
     185                         if offset.value < 0.0:
     186                            print >>OUTPUT, " - " + str(abs(rho*offset.value)) + " " + convert_name(variable.label),                   
     187                         else:
     188                            print >>OUTPUT, " + " + str(rho*offset.value) + " " + convert_name(variable.label),
     189
     190                      objective_offset = (rho * offset.value * offset.value / 2.0)
     191                      if is_minimizing is True:
     192                         print >>OUTPUT, " + " + str(objective_offset) + " ONE_VAR_CONSTANT"
     193                      else:
     194                         print >>OUTPUT, " - " + str(objective_offset) + " ONE_VAR_CONSTANT"
     195                     
     196            elif isinstance(arg,numvalue.NumericConstant):
     197                # this is the "0.0" element that forms the initial expression - the
     198                # quadratic sub-expressions aren't known to the presolve routines.
     199                # ideally unnecessary - hacked in for now.
     200                pass
     201
     202            else:
     203                print `arg`
     204                raise ValueError, "Unknown expression sub-type found in quadratic objective expression"   
     205   
     206
    143207    def _print_model_LP(self, model, OUTPUT):
    144208
     
    149213        #
    150214        if self._output_objectives is True:
    151            #printed_quadterm = False
     215           printed_quadterm = False
    152216           if len(_obj) == 0:
    153217              raise ValueError, "ERROR: No objectives defined for input model=" + str(model.name) + "; cannot write legal LP file"
     
    165229                    raise ValueError, "Cannot write legal LP file.  Objective %s[%s] has nonlinear terms that are not quadratic." % (str(obj),str(key))
    166230                self._print_expr(model, obj[key].repn, OUTPUT, print_offset=True)
     231           if obj._quad_subexpr is not None:
     232               self._print_quadterm(obj._quad_subexpr, (_obj[ _obj.keys()[0] ].sense == minimize), OUTPUT)
     233               printed_quadterm = True               
    167234           print >>OUTPUT, ""
    168235       
Note: See TracChangeset for help on using the changeset viewer.