# Changeset 2214

Ignore:
Timestamp:
Jan 31, 2010 4:44:12 PM (10 years ago)
Message:

Reworking the canonical expression definition to include a
dictionary from variable ID -> _VarValue object.

Location:
coopr.pyomo/trunk/coopr/pyomo
Files:
4 edited

Unmodified
Removed
• ## coopr.pyomo/trunk/coopr/pyomo/expr/canonical_repn.py

 r2201 def as_expr(rep, model, ignore_other=False): def as_expr(rep, vars=None, model=None, ignore_other=False): """ Convert a canonical representation into an expression. """ if isinstance(model, Model): vars = model._var id_offset=0 else: vars = model elif not vars is None: id_offset=1 expr = 0.0 if not ignore_other: expr += rep[d] continue if d is -1: continue for v in rep[d]: for id in v: for i in xrange(v[id]): e *= vars[id+id_offset] if vars is None: e *= rep[-1][id] else: e *= vars[id[1]+id_offset] expr += e return expr rep[d] = r2[d] continue if d < 0: continue if not d in rep: rep[d] = {} for var in r2[d]: rep[d][var] = coef*r2[d][var] + rep[d].get(var,0.0) # # Merge the VarValue maps # if -1 in r2: if -1 in rep: rep[-1].update(r2[-1]) else: rep[-1] = r2[-1] return rep def repn_mult(r1, r2, model, coef=1.0): def repn_mult(r1, r2, coef=1.0): rep = {} for d1 in r1: for d2 in r2: if d1 == None or d2 == None: if d1 == None or d2 == None or d1 < 0 or d2 < 0: pass else: # if None in r1: rep[None] = as_expr(r2, model, ignore_other=True) * copy.deepcopy(r1[None]) #print "YY" #r1[None].pprint() #as_expr(r2, model, ignore_other=True).pprint() #print "YY" rep[None] = as_expr(r2, ignore_other=True) * copy.deepcopy(r1[None]) if None in r2: rep[None] += copy.deepcopy(r1[None])*copy.deepcopy(r2[None]) if None in r2: if None in rep: rep[None] += as_expr(r1, model, ignore_other=True) * copy.deepcopy(r2[None]) else: rep[None]  = as_expr(r1, model, ignore_other=True) * copy.deepcopy(r2[None]) rep[None] += as_expr(r1, ignore_other=True) * copy.deepcopy(r2[None]) else: rep[None]  = as_expr(r1, ignore_other=True) * copy.deepcopy(r2[None]) # # Merge the VarValue maps # if -1 in r1: rep[-1] = r1[-1] if -1 in r2: if -1 in rep: rep[-1].update(r2[-1]) else: rep[-1] = r2[-1] # # Return the canonical repn # called recursively. # def collect_canonical_repn(exp, model): def collect_canonical_repn(exp): global temp_const global temp_var repn = {} for i in range(len(exp._args)): repn = repn_add(repn, collect_canonical_repn(exp._args[i], model), coef=exp._coef[i] ) repn = repn_add(repn, collect_canonical_repn(exp._args[i]), coef=exp._coef[i] ) return repn # #print "Y",exp.coef/denom for e in exp._numerator: repn = repn_mult(repn, collect_canonical_repn(e, model), model) repn = repn_mult(repn, collect_canonical_repn(e)) return repn # # elif type(exp) is _VarValue or exp.type() is Var: temp_var = { 1: {frozendict({exp.id:1}):1.0} } if type(exp) is _VarValue: mid = id(exp.var.model) else: mid = id(exp.model) temp_var = { -1: {(mid,exp.id):exp}, 1: {frozendict({(mid,exp.id):1}):1.0} } return temp_var # # log(y[1]) + x[1]*x[1]         {2:{{0:2}:1.0}, None:log(y[1])} # def generate_canonical_repn(expr, model): return collect_canonical_repn(expr, model) def generate_canonical_repn(expr): return collect_canonical_repn(expr) def is_constant(repn): """Return True if the canonical representation is a constant expression""" return (0 in repn) and (len(repn) == 1) for key in repn: if key is None or key > 0: return False return True def is_nonlinear(repn): """Return True if the canonical representation is a nonlinear expression""" for key in repn: if not key in [0,1]: if not key in [-1,0,1]: return True return False
• ## coopr.pyomo/trunk/coopr/pyomo/io/cpxlp.py

 r2211 return a[0] def _print_expr(self, model, x, OUTPUT, print_offset=False): def _print_expr(self, x, OUTPUT, print_offset=False): max_terms_on_line=5 # this is the line-limit hack terms_output = 0 for id in keys: coef = x[1][id] name = convert_name(model._var[id.keys()[0]].label) name = convert_name(x[-1][id.keys()[0]].label) print >>OUTPUT, ('- %f' % math.fabs(coef) if coef < 0.0 else '+ %f' % coef), name, terms_output += 1 print >>OUTPUT, ('- %f' % math.fabs(coef) if coef < 0.0 else '+ %f' % coef), for var in id: name = convert_name(model._var[var].label) name = convert_name(x[-1][var].label) if id[var] > 1: print "%s^%d" % (name,id[var]), if is_nonlinear(obj[key].repn) and not is_quadratic(obj[key].repn): raise ValueError, "Cannot write legal LP file.  Objective %s[%s] has nonlinear terms that are not quadratic." % (str(obj),str(key)) self._print_expr(model, obj[key].repn, OUTPUT, print_offset=True) self._print_expr(obj[key].repn, OUTPUT, print_offset=True) if obj._quad_subexpr is not None: self._print_quadterm(obj._quad_subexpr, (_obj[ _obj.keys()[0] ].sense == minimize), OUTPUT) # print >>OUTPUT, prefix + "c_e_" + convert_name(C._data[cndx].label) + "_: ", offset = self._print_expr(model, C[cndx].repn, OUTPUT) offset = self._print_expr(C[cndx].repn, OUTPUT) print >>OUTPUT, "=", self._print_bound(C._data[cndx].lower, OUTPUT, -offset) if C._data[cndx].lower is not None: print >>OUTPUT, prefix + "c_l_" + convert_name(C._data[cndx].label) + "_: ", offset = self._print_expr(model, C[cndx].repn, OUTPUT) offset = self._print_expr(C[cndx].repn, OUTPUT) print >>OUTPUT, ">=", self._print_bound(C._data[cndx].lower, OUTPUT, -offset) if C._data[cndx].upper is not None: print >>OUTPUT, prefix + "c_u_" + convert_name(C._data[cndx].label) + "_: ", offset = self._print_expr(model, C[cndx].repn, OUTPUT) offset = self._print_expr(C[cndx].repn, OUTPUT) print >>OUTPUT, "<=", self._print_bound(C._data[cndx].upper, OUTPUT, -offset)
• ## coopr.pyomo/trunk/coopr/pyomo/presolve/compute_canonical_repn.py

 r2201 raise ValueError, "No expression has been defined for objective %s" % str(key) try: obj._data[ondx].repn = coopr.pyomo.expr.generate_canonical_repn(obj._data[ondx].expr, model) obj._data[ondx].repn = coopr.pyomo.expr.generate_canonical_repn(obj._data[ondx].expr) except Exception, e: print "Error generating a canonical representation for objective %s (index %s)" % (str(key), str(ondx)) raise ValueError, "No expression has been defined for the body of constraint %s" % str(key) try: con._data[cndx].repn = coopr.pyomo.expr.generate_canonical_repn(con._data[cndx].body, model) con._data[cndx].repn = coopr.pyomo.expr.generate_canonical_repn(con._data[cndx].body) except Exception, e: print "Error generating a canonical representation for constraint %s (index %s)" % (str(key), str(cndx))
• ## coopr.pyomo/trunk/coopr/pyomo/tests/expr/test_canonical.py

 r2201 self.instance = self.model.create() #self.instance.obj[None].expr.pprint() self.failUnlessEqual(generate_canonical_repn(self.instance.obj[None].expr, self.instance), {1: {frozendict({2: 1}): 6.0, frozendict({1: 1}): 4.0, frozendict({3: 1}): 8.0, frozendict({0: 1}): 2.0}}) mid=id(self.instance) rep = generate_canonical_repn(self.instance.obj[None].expr) del rep[-1] self.failUnlessEqual(rep, {1: {frozendict({(mid,2): 1}): 6.0, frozendict({(mid,1): 1}): 4.0, frozendict({(mid,3): 1}): 8.0, frozendict({(mid,0): 1}): 2.0}}) def test_expr2(self): self.model.obj = Objective(rule=obj_rule) self.instance = self.model.create() #self.instance.obj[None].expr.pprint() self.failUnlessEqual(generate_canonical_repn(self.instance.obj[None].expr, self.instance), {1: {frozendict({2: 1}): 6.0, frozendict({1: 1}): 4.0, frozendict({3: 1}): 8.0, frozendict({0: 1}): 2.0}}) mid=id(self.instance) #self.instance.obj[None].expr.pprint() rep = generate_canonical_repn(self.instance.obj[None].expr) del rep[-1] self.failUnlessEqual(rep, {1: {frozendict({(mid,2): 1}): 6.0, frozendict({(mid,1): 1}): 4.0, frozendict({(mid,3): 1}): 8.0, frozendict({(mid,0): 1}): 2.0}}) def test_expr3(self): self.model.obj = Objective(rule=obj_rule) self.instance = self.model.create() #self.instance.obj[None].expr.pprint() self.failUnlessEqual(generate_canonical_repn(self.instance.obj[None].expr, self.instance), {1: {frozendict({0: 1}):9.0, frozendict({1: 1}):4.0}}) mid=id(self.instance) #self.instance.obj[None].expr.pprint() rep = generate_canonical_repn(self.instance.obj[None].expr) del rep[-1] self.failUnlessEqual(rep, {1: {frozendict({(mid,0): 1}):9.0, frozendict({(mid,1): 1}):4.0}}) def test_expr4(self): self.model.obj = Objective(rule=obj_rule) self.instance = self.model.create() #self.instance.obj[None].expr.pprint() self.failUnlessEqual(generate_canonical_repn(self.instance.obj[None].expr, self.instance), {0:frozendict({None:1.2}), 1: {frozendict({0: 1}): 5.0}}) mid=id(self.instance) #self.instance.obj[None].expr.pprint() rep = generate_canonical_repn(self.instance.obj[None].expr) del rep[-1] self.failUnlessEqual(rep, {0:frozendict({None:1.2}), 1: {frozendict({(mid,0): 1}): 5.0}}) def test_expr5(self): self.model.obj = Objective(rule=obj_rule) self.instance = self.model.create() #self.instance.obj[None].expr.pprint() self.failUnlessEqual(generate_canonical_repn(self.instance.obj[None].expr, self.instance), {2: {frozendict({0:2}):1.0, frozendict({0:1, 1:1}):2.0}, 3:{frozendict({1:1,2:2}):3.0}}) mid=id(self.instance) #self.instance.obj[None].expr.pprint() rep = generate_canonical_repn(self.instance.obj[None].expr) del rep[-1] self.failUnlessEqual(rep, {2: {frozendict({(mid,0):2}):1.0, frozendict({(mid,0):1, (mid,1):1}):2.0}, 3:{frozendict({(mid,1):1, (mid,2):2}):3.0}}) def test_expr6(self): self.model.obj = Objective(rule=obj_rule) self.instance = self.model.create() #self.instance.obj[None].expr.pprint() self.failUnlessEqual(generate_canonical_repn(self.instance.obj[None].expr, self.instance), {0:frozendict({None:5.4}), 1: {frozendict({0: 1}): 13.0}}) mid=id(self.instance) #self.instance.obj[None].expr.pprint() rep = generate_canonical_repn(self.instance.obj[None].expr) del rep[-1] self.failUnlessEqual(rep, {0:frozendict({None:5.4}), 1: {frozendict({(mid,0): 1}): 13.0}}) def test_expr7(self): self.model.obj = Objective(rule=obj_rule) self.instance = self.model.create() #self.instance.obj[None].expr.pprint() self.failUnlessEqual(generate_canonical_repn(self.instance.obj[None].expr, self.instance), {1: {frozendict({2: 1}): 3.0, frozendict({1: 1}): 2.0, frozendict({3: 1}): 4.0, frozendict({0: 1}): 1.0}}) mid=id(self.instance) #self.instance.obj[None].expr.pprint() rep = generate_canonical_repn(self.instance.obj[None].expr) del rep[-1] self.failUnlessEqual(rep, {1: {frozendict({(mid,2): 1}): 3.0, frozendict({(mid,1): 1}): 2.0, frozendict({(mid,3): 1}): 4.0, frozendict({(mid,0): 1}): 1.0}}) def test_expr8(self): self.instance.y.fixed = True self.instance.y.reset() #self.instance.obj[None].expr.pprint() self.failUnlessEqual(generate_canonical_repn(self.instance.obj[None].expr, self.instance), {1: {frozendict({2: 1}): 3.0, frozendict({1: 1}): 2.0, frozendict({3: 1}): 4.0, frozendict({0: 1}): 1.0}}) mid=id(self.instance) #self.instance.obj[None].expr.pprint() rep = generate_canonical_repn(self.instance.obj[None].expr) del rep[-1] self.failUnlessEqual(rep, {1: {frozendict({(mid,2): 1}): 3.0, frozendict({(mid,1): 1}): 2.0, frozendict({(mid,3): 1}): 4.0, frozendict({(mid,0): 1}): 1.0}}) def test_expr9(self): self.instance.y.fixed = True self.instance.y.reset() #self.instance.obj[None].expr.pprint() self.failUnlessEqual(generate_canonical_repn(self.instance.obj[None].expr, self.instance), {1: {frozendict({2: 1}): 3.0, frozendict({1: 1}): 2.0, frozendict({3: 1}): 4.0, frozendict({0: 1}): 1.0}}) mid=id(self.instance) #self.instance.obj[None].expr.pprint() rep = generate_canonical_repn(self.instance.obj[None].expr) del rep[-1] self.failUnlessEqual(rep, {1: {frozendict({(mid,2): 1}): 3.0, frozendict({(mid,1): 1}): 2.0, frozendict({(mid,3): 1}): 4.0, frozendict({(mid,0): 1}): 1.0}}) def test_expr10(self): self.instance = self.model.create() #self.instance.obj[None].expr.pprint() rep = generate_canonical_repn(self.instance.obj[None].expr, self.instance) rep = generate_canonical_repn(self.instance.obj[None].expr) self.failUnless(len(rep) == 1 and None in rep) self.instance = self.model.create() #self.instance.obj[None].expr.pprint() rep = generate_canonical_repn(self.instance.obj[None].expr, self.instance) rep = generate_canonical_repn(self.instance.obj[None].expr) rep[None].pprint() del rep[None] self.failUnlessEqual(rep, {0: {None: 3.0}, 1: {frozendict({1: 1}): 1.0, frozendict({0: 1}): 1.0}}) del rep[-1] mid=id(self.instance) self.failUnlessEqual(rep, {0: {None: 3.0}, 1: {frozendict({(mid,1): 1}): 1.0, frozendict({(mid,0): 1}): 1.0}}) def test_expr12(self): self.model.obj = Objective(rule=obj_rule) self.instance = self.model.create() #self.instance.obj[None].expr.pprint() rep = generate_canonical_repn(self.instance.obj[None].expr, self.instance) mid=id(self.instance) #self.instance.obj[None].expr.pprint() rep = generate_canonical_repn(self.instance.obj[None].expr) rep[None].pprint() del rep[None] self.failUnlessEqual(rep, {0: {None: 2.0}, 1: {frozendict({1: 1}): 1.0, frozendict({0: 1}): 2.0}, 2: {frozendict({0: 1, 1: 1}): 1.0}}) del rep[-1] self.failUnlessEqual(rep, {0: {None: 2.0}, 1: {frozendict({(mid,1): 1}): 1.0, frozendict({(mid,0): 1}): 2.0}, 2: {frozendict({(mid,0): 1, (mid,1): 1}): 1.0}}) if __name__ == "__main__":
Note: See TracChangeset for help on using the changeset viewer.