Changeset 2999


Ignore:
Timestamp:
Sep 6, 2010 4:40:46 AM (9 years ago)
Author:
wehart
Message:

Changes to allow a single variable to be used in a
constraint or objective ... along with associated tests to
verify this. This resolves #4110.

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

Legend:

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

    r2906 r2999  
    173173        self._constructed=True
    174174        #
    175         if isinstance(self.rule,Expression):
    176             if None in self._index or len(self._index) == 0:
     175        if isinstance(self.rule,Expression) or isinstance(self.rule,_VarValue):
     176            if None in self._index or len(self._index) == 0 or self._index[None] is None:
    177177                self._data[None].expr = self.rule
    178178            else:
  • coopr.pyomo/trunk/coopr/pyomo/base/var.py

    r2977 r2999  
    572572        self._varval[None].index = None
    573573
     574    def __call__(self, exception=True):
     575        if None in self._varval:
     576            return self._varval[None].value
     577        return None
     578
    574579    def is_constant(self):
    575580        return _VarValue.is_constant(self)
  • coopr.pyomo/trunk/coopr/pyomo/preprocess/identify_vars.py

    r2914 r2999  
    4949                msg += "\n    Do all constraints and objectives return a " + \
    5050                       "value?"
     51            msg += " type="+str(type(exp))
    5152            raise AttributeError, msg
    5253               
  • coopr.pyomo/trunk/coopr/pyomo/tests/unit/test_con.py

    r2826 r2999  
    3939        pass
    4040
     41    def test_expr1(self):
     42        """Test expr option"""
     43        self.model = ConcreteModel()
     44        self.model.x = Var(RangeSet(1,4),initialize=2)
     45        ans=0
     46        for i in self.model.x.keys():
     47            ans = ans + self.model.x[i]
     48        ans = ans >= 0
     49        ans = ans <= 1
     50        self.model.c = Constraint(expr=ans)
     51        self.instance = self.model.create()
     52        try:
     53            self.failUnlessEqual(self.instance.c(), 8)
     54        except ValueError:
     55            pass
     56        else:
     57            self.fail("test_expr_option")
     58        self.instance.x.reset()
     59        self.failUnlessEqual(self.instance.c(), 8)
     60        self.failUnlessEqual(value(self.instance.c), 8)
     61
     62    def test_expr2(self):
     63        """Test expr option"""
     64        self.model = ConcreteModel()
     65        self.model.x = Var(initialize=2)
     66        ans = self.model.x >= 0
     67        ans = ans <= 1
     68        self.model.c = Constraint(expr=ans)
     69        self.instance = self.model.create()
     70        self.instance.x.reset()
     71        self.failUnlessEqual(self.instance.c(), 2)
     72        self.failUnlessEqual(value(self.instance.c), 2)
     73
    4174    def test_rule1(self):
    4275        """Test rule option"""
  • coopr.pyomo/trunk/coopr/pyomo/tests/unit/test_obj.py

    r2656 r2999  
    3939        pass
    4040
     41    def test_expr1_option(self):
     42        """Test expr option"""
     43        self.model = ConcreteModel()
     44        self.model.x = Var(RangeSet(1,4),initialize=2)
     45        ans=0
     46        for i in self.model.x.keys():
     47            ans = ans + self.model.x[i]
     48        self.model.obj = Objective(expr=ans)
     49        self.instance = self.model.create()
     50        try:
     51          self.failUnlessEqual(self.instance.obj(), 8)
     52        except ValueError:
     53          pass
     54        else:
     55          self.fail("test_rule_option")
     56        self.instance.x.reset()
     57        self.failUnlessEqual(self.instance.obj(), 8)
     58        self.failUnlessEqual(value(self.instance.obj), 8)
     59
     60    def test_expr2_option(self):
     61        """Test expr option"""
     62        self.model = ConcreteModel()
     63        self.model.x = Var(initialize=2)
     64        self.model.obj = Objective(expr=self.model.x)
     65        self.instance = self.model.create()
     66        self.instance.x.reset()
     67        print 'X',type(self.instance.obj.rule)
     68        self.failUnlessEqual(self.instance.obj(), 2)
     69        self.failUnlessEqual(value(self.instance.obj), 2)
     70
    4171    def test_rule_option(self):
    4272        """Test rule option"""
Note: See TracChangeset for help on using the changeset viewer.