示例#1
0
    def _generate_model(self):
        self.model = ConcreteModel()
        model = self.model
        model._name = self.description

        model.f = Var()
        model.x = Var(bounds=(1,3))
        model.fi = Param([1,2,3],mutable=True)
        model.fi[1] = 1.0
        model.fi[2] = 2.0
        model.fi[3] = 0.0
        model.xi = Param([1,2,3],mutable=True)
        model.xi[1] = 1.0
        model.xi[2] = 2.0
        model.xi[3] = 3.0
        model.p = Var(within=NonNegativeReals)
        model.n = Var(within=NonNegativeReals)
        model.lmbda = Var([1,2,3])
        model.obj = Objective(expr=model.p+model.n)
        model.c1 = ConstraintList()
        model.c1.add((0.0, model.lmbda[1], 1.0))
        model.c1.add((0.0, model.lmbda[2], 1.0))
        model.c1.add(0.0 <= model.lmbda[3])
        model.c2 = SOSConstraint(var=model.lmbda, index=[1,2,3], sos=2)
        model.c3 = Constraint(expr=sum_product(model.lmbda) == 1)
        model.c4 = Constraint(expr=model.f==sum_product(model.fi,model.lmbda))
        model.c5 = Constraint(expr=model.x==sum_product(model.xi,model.lmbda))
        model.x = 2.75
        model.x.fixed = True

        # Make an empty SOSConstraint
        model.c6 = SOSConstraint(var=model.lmbda, index=[1,2,3], sos=2)
        model.c6.set_items([],[])
        assert len(list(model.c6.get_items())) == 0
示例#2
0
    def test_EXPORT_suffixes_no_datatype(self):
        model = ConcreteModel()
        model.sosno = Suffix(direction=Suffix.EXPORT,datatype=None)
        model.y = Var([1,2,3])
        model.obj = Objective(expr=sum_product(model.y))

        # The NL writer will convert this constraint to ref and sosno
        # suffixes on model.y
        model.sos_con = SOSConstraint(var=model.y, index=[1,2,3], sos=1)

        for i in [1,2,3]:
            model.sosno.set_value(model.y[i],-1)
        
        try:
            model.write(filename=join(currdir,"junk.nl"),
                        format=ProblemFormat.nl,
                        io_options={"symbolic_solver_labels" : False})
        except RuntimeError:
            pass
        else:
            os.remove(join(currdir,"junk.nl"))
            self.fail("The NL writer should have thrown an exception "\
                      "when using an EXPORT suffix with datatype=None")
        try:
            os.remove(join(currdir,"junk.nl"))
        except:
            pass
示例#3
0
    def test_EXPORT_suffixes_with_SOSConstraint_duplicateref(self):
        model = ConcreteModel()
        model.ref = Suffix(direction=Suffix.EXPORT,datatype=Suffix.INT)
        model.y = Var([1,2,3])
        model.obj = Objective(expr=sum_product(model.y))

        # The NL writer will convert this constraint to ref and sosno
        # suffixes on model.y
        model.sos_con = SOSConstraint(var=model.y, index=[1,2,3], sos=1)

        for i,val in zip([1,2,3],[11,12,13]):
            model.ref.set_value(model.y[i],val)
        
        try:
            model.write(filename=join(currdir,"junk.nl"),
                        format=ProblemFormat.nl,
                        io_options={"symbolic_solver_labels" : False})
        except RuntimeError:
            pass
        else:
            os.remove(join(currdir,"junk.nl"))
            self.fail("The NL writer should have thrown an exception "\
                      "when overlap of SOSConstraint generated suffixes "\
                      "and user declared suffixes occurs.")
        
        try:
            os.remove(join(currdir,"junk.nl"))
        except:
            pass
示例#4
0
    def _generate_model(self):
        self.model = ConcreteModel()
        model = self.model
        model._name = self.description

        model.a = Param(initialize=0.1)
        model.x = Var(within=NonNegativeReals)
        model.y = Var([1, 2], within=NonNegativeReals)

        model.obj = Objective(expr=model.x + model.y[1] + 2 * model.y[2])
        model.c1 = Constraint(expr=model.a <= model.y[2])
        model.c2 = Constraint(expr=2.0 <= model.x <= 10.0)
        model.c3 = SOSConstraint(var=model.y, index=[1, 2], sos=1)
        model.c4 = Constraint(expr=summation(model.y) == 1)

        # Make an empty SOSConstraint
        model.c5 = SOSConstraint(var=model.y, index=[1, 2], sos=1)
        model.c5.set_items([], [])
        assert len(list(model.c5.get_items())) == 0
示例#5
0

def constraint2_rule(model, t1, t2):
    return model.Fx[t1,
                    t2] == sum(model.y[t1, t2, i] * F(PIECEWISE_PTS[t1, t2][i])
                               for i in xrange(len(PIECEWISE_PTS[t1, t2])))


def constraint3_rule(model, t1, t2):
    return sum(model.y[t1, t2, j]
               for j in xrange(len(PIECEWISE_PTS[t1, t2]))) == 1


model.obj = Objective(expr=sum_product(model.Fx), sense=maximize)
model.constraint1 = Constraint(INDEX_SET1, INDEX_SET2, rule=constraint1_rule)
model.constraint2 = Constraint(INDEX_SET1, INDEX_SET2, rule=constraint2_rule)
model.constraint3 = Constraint(INDEX_SET1, INDEX_SET2, rule=constraint3_rule)
model.SOS_set_constraint = SOSConstraint(INDEX_SET1,
                                         INDEX_SET2,
                                         var=model.y,
                                         index=model.SOS_indices,
                                         sos=2)

#Fix the answer for testing purposes
model.set_answer_constraint1 = Constraint(expr=model.x[1, 1] == 2.5)
model.set_answer_constraint2 = Constraint(expr=model.x[2, 1] == 1.5)
model.set_answer_constraint3 = Constraint(expr=model.x[1, 2] == 2.5)
model.set_answer_constraint4 = Constraint(expr=model.x[2, 2] == 1.5)
model.set_answer_constraint5 = Constraint(expr=model.x[1, 3] == 2.5)
model.set_answer_constraint6 = Constraint(expr=model.x[2, 3] == 1.5)