Changeset 2656


Ignore:
Timestamp:
Jun 11, 2010 10:12:12 AM (11 years ago)
Author:
khunter
Message:

Add readable error message for incorrect arguments to objectives

After spending an egregious amount of time tracking down that we had
passed a model instead of a model attribute (like set). Error
messages to the rescue.

Also add associated test.

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

Legend:

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

    r2655 r2656  
    6464        if args == ():
    6565            self._data[None] = ObjectiveData()
     66        else:
     67            for i in args:
     68                if not ( isinstance(i, _SetContainer) ):
     69                    msg  = 'Invalid argument.  Objective expects arguments of '
     70                    msg += "type 'Set', not type '%s'"
     71                    raise TypeError, msg % str( type(i).__name__ )
    6672
    6773        # this is a hack for now; we eventually want a
  • coopr.pyomo/trunk/coopr/pyomo/tests/unit/test_obj.py

    r2422 r2656  
    5858        self.failUnlessEqual(self.instance.obj(), 8)
    5959        self.failUnlessEqual(value(self.instance.obj), 8)
     60
     61    def test_arguments(self):
     62        """Test that arguments notare of type _SetContainer"""
     63        def rule(model):
     64            return 1
     65        try:
     66            self.model.obj = Objective(self.model, rule=rule)
     67        except TypeError:
     68            pass
     69        else:
     70            self.fail("Objective should only accept '_SetContainter's")
     71       
    6072
    6173    def test_sense_option(self):
Note: See TracChangeset for help on using the changeset viewer.