Changeset 2862


Ignore:
Timestamp:
Jul 27, 2010 5:58:43 PM (9 years ago)
Author:
prsteel
Message:

Added tests for transformation of Constraint objects through NonNegativeTransformation? transformations.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/trunk/coopr/pyomo/tests/unit/test_transform.py

    r2860 r2862  
    1515from pyutilib.component.core import Plugin
    1616from pyutilib.services import registered_executable
    17 from coopr.pyomo.transform import *
     17from coopr.pyomo.transform import NonNegativeTransformation
    1818
    1919class Test(unittest.TestCase):
     
    268268            transformed_sol["Solution"][0]["Objective"]["f"]["value"]
    269269            )
     270
     271    @unittest.skipIf(registered_executable('glpsol') is None, "The 'glpsol' executable is not available")
     272    def test_nonnegative_transform_4(self):
     273        """ Same as #3, but adds constraints """
     274        self.model.S = RangeSet(0,10)
     275        self.model.T = Set(initialize=["foo", "bar"])
     276
     277        # Unindexed, singly indexed, and doubly indexed variables with
     278        # explicit bounds
     279        self.model.x1 = Var(bounds=(-3, 3))
     280        self.model.y1 = Var(self.model.S, bounds=(-3, 3))
     281        self.model.z1 = Var(self.model.S, self.model.T, bounds=(-3, 3))
     282
     283        # Unindexed, singly indexed, and doubly indexed variables with
     284        # rule-defined bounds
     285        def boundsRule(*args):
     286            return (-4, 4)
     287        self.model.x2 = Var(bounds=boundsRule)
     288        self.model.y2 = Var(self.model.S, bounds=boundsRule)
     289        self.model.z2 = Var(self.model.S, self.model.T, bounds=boundsRule)
     290
     291       
     292        # Unindexed, singly indexed, and doubly indexed variables with
     293        # explicit domains
     294        self.model.x3 = Var(domain=NegativeReals, bounds=(-10, 10))
     295        self.model.y3 = Var(self.model.S, domain = NegativeIntegers, bounds=(-10, 10))
     296        self.model.z3 = Var(self.model.S, self.model.T, domain = Reals, bounds=(-10, 10))
     297
     298        # Unindexed, singly indexed, and doubly indexed variables with
     299        # rule-defined domains
     300        def domainRule(*args):
     301            if len(args) == 1:
     302                arg = 0
     303            elif isinstance(args[0], tuple):
     304                arg = args[0][0]
     305            else:
     306                arg = args[0]
     307
     308            if len(args) == 1 or arg == 0:
     309                return NonNegativeReals
     310            elif arg == 1:
     311                return NonNegativeIntegers
     312            elif arg == 2:
     313                return NonPositiveReals
     314            elif arg == 3:
     315                return NonPositiveIntegers
     316            elif arg == 4:
     317                return NegativeReals
     318            elif arg == 5:
     319                return NegativeIntegers
     320            elif arg == 6:
     321                return PositiveReals
     322            elif arg == 7:
     323                return PositiveIntegers
     324            elif arg == 8:
     325                return Reals
     326            elif arg == 9:
     327                return Integers
     328            elif arg == 10:
     329                return Binary
     330            else:
     331                return Reals
     332
     333        self.model.x4 = Var(domain=domainRule, bounds=(-10, 10))
     334        self.model.y4 = Var(self.model.S, domain=domainRule, bounds=(-10, 10))
     335        self.model.z4 = Var(self.model.S, self.model.T, domain=domainRule, bounds=(-10, 10))
     336
     337        # Add some constraints
     338        def makeXConRule(var):
     339            def xConRule(var, model):
     340                return (-1, var, 1)
     341           
     342        def makeYConRule(var):
     343            def yConRule(var, s, model):
     344                return (-1, var[s], 1)
     345       
     346        def makeZConRule(var):
     347            def zConRule(var, s, t, model):
     348                return (-1, var[s, t], 1)
     349
     350        for n in ('1', '2', '3', '4'):
     351            self.model.__setattr__(
     352                "x" + n + "_constraint",
     353                Constraint(
     354                rule=makeXConRule(
     355                self.model.__getattribute__("x"+n))))
     356
     357            self.model.__setattr__(
     358                "y" + n + "_constraint",
     359                Constraint(
     360                rule=makeYConRule(
     361                self.model.__getattribute__("y"+n))))
     362
     363            self.model.__setattr__(
     364                "z" + n + "_constraint",
     365                Constraint(
     366                rule=makeZConRule(
     367                self.model.__getattribute__("z"+n))))
     368
     369        def objRule(model):
     370            return sum(5*summation(model.__getattribute__(c+n)) \
     371                       for c in ('x', 'y', 'z') for n in ('1', '2', '3', '4'))
     372           
     373        self.model.obj = Objective(rule=objRule)
     374
     375        transform = NonNegativeTransformation()
     376        instance=self.model.create()
     377        #transformed = apply_transformation("nonnegative_transform", instance)
     378        transformed = transform(instance)
     379       
     380        from coopr.opt.base import SolverFactory
     381
     382        solver = SolverFactory("glpk")
     383
     384        instance_sol = solver.solve(instance)
     385        transformed_sol = solver.solve(transformed)
     386
     387        self.failUnlessEqual(
     388            instance_sol["Solution"][0]["Objective"]["f"]["value"],
     389            transformed_sol["Solution"][0]["Objective"]["f"]["value"]
     390            )
    270391                             
    271392
Note: See TracChangeset for help on using the changeset viewer.