Changeset 2027


Ignore:
Timestamp:
Dec 22, 2009 9:24:38 AM (10 years ago)
Author:
wehart
Message:

Simplified the syntax for adding a constraint to a
Constraint object.

Location:
coopr.pyomo/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/trunk/coopr/pyomo/base/constraint.py

    r2025 r2027  
    281281        # Local variables for code optimization
    282282        #
    283         _self_name=self.name
    284283        _self_rule = self.rule
    285284        #
     
    292291                    raise IndexError, "The index "+str(val)+" generated by the constructor rule is not valid"
    293292                ##constructed_indices.append(val)
    294                 self._initialize_constraint(val, constraints[val], _self_name)
     293                self.add(val, constraints[val])
    295294        else:
    296295            for val in self._index:
     
    312311                        continue
    313312                ##constructed_indices.append(val)
    314                 self._initialize_constraint(val, expr, _self_name)
     313                self.add(val, expr)
    315314        #self._index=constructed_indices
    316315
    317     def _initialize_constraint(self, val, expr, _name):
    318         # val: the constraint index (should probably be renamed)
     316    def add(self, index, expr):
     317        # index: the constraint index (should probably be renamed)
    319318        # expr: the constraint expression
    320319        # model: the parent model
    321         # _name: the name of the constraint
    322320
    323321        #
     
    331329        #
    332330        if isinstance(expr,Expression) is False and isinstance(expr,tuple) is False and isinstance(expr, list) is False:
    333            raise ValueError, "Non-expression object of "+str(type(expr))+" supplied to initialize constraint="+_name+", index="+str(val)
     331           raise ValueError, "Non-expression object of "+str(type(expr))+" supplied to initialize constraint="+self.name+", index="+str(index)
    334332           
    335333        #
     
    338336        #
    339337        _self_data = self._data
    340         _self_data[val] = ConstraintData(name=create_name(_name,val))
     338        _self_data[index] = ConstraintData(name=create_name(self.name,index))
    341339        if not type(expr) in [tuple,list]:
    342340            _expr_args0 = expr._args[0]
     
    352350        #
    353351        if isinstance(expr,_EqualToExpression):
    354             _self_data[val]._equality = True
     352            _self_data[index]._equality = True
    355353            if is_constant(_expr_args1):
    356354                tpl = [ _expr_args1,_expr_args0,_expr_args1 ]
     
    440438            #
    441439            if len(expr) == 2:
    442                 _self_data[val]._equality = True
     440                _self_data[index]._equality = True
    443441                if is_constant(expr[1]):
    444442                    tpl = [ expr[1],expr[0],expr[1] ]
     
    470468        if type(tpl[1]) in [bool,int,long,float]:
    471469            raise ValueError, "Constraint \""+self.name+"\" has a numeric body.  Expecting Expression instance."
    472         if _self_data[val]._equality:
     470        if _self_data[index]._equality:
    473471            tpl[2] = tpl[0]
    474472        elif type(tpl[2]) in [bool,int,long,float]:
     
    483481        # 'infinite' RHS
    484482        #
    485         if (tpl[0] is None or tpl[2] is None) and _self_data[val]._equality:
     483        if (tpl[0] is None or tpl[2] is None) and _self_data[index]._equality:
    486484            raise ValueError, "Equality constraint "+self.name+" defined with infinity RHS"
    487485        #
     
    503501        # Finally, setup the data
    504502        #
    505         _self_data[val].lower = tpl[0]
    506         _self_data[val].body = tpl[1]
    507         _self_data[val].upper = tpl[2]
     503        _self_data[index].lower = tpl[0]
     504        _self_data[index].body = tpl[1]
     505        _self_data[index].upper = tpl[2]
    508506
    509507
  • coopr.pyomo/trunk/examples/pyomo/benders/runbenders

    r2023 r2027  
    8282                    sum([master_instance.sell_lim_price[p, t, s, i] * master_instance.market[p, t] for p in master_instance.PROD for t in master_instance.TWOPLUSWEEKS for s in master_instance.SCEN]) - \
    8383                    master_instance.Min_Stage2_Profit
    84    master_instance.Cut_Defn._initialize_constraint(i, (0.0, cut_expression, None), master_instance, "Cut_Defn")
     84   master_instance.Cut_Defn.add(i, cut_expression >= 0.0)
    8585     
    8686   # compute expected second-stage profit
Note: See TracChangeset for help on using the changeset viewer.