Ejemplo n.º 1
0
def define_model(**kwds):

    model = ConcreteModel()

    model.x1 = Var(bounds=(0, 3))  # domain variable
    model.x2 = Var(bounds=(0, 3))  # domain variable
    model.x3 = Var(bounds=(0, 3))  # domain variable
    model.x4 = Var(bounds=(0, 3))  # domain variable

    model.Fx1 = Var()  # range variable
    model.Fx2 = Var()  # range variable
    model.Fx3 = Var()  # range variable
    model.Fx4 = Var()  # range variable
    model.p = Param(initialize=1.0)

    model.obj = Objective(expr=model.Fx1 + model.Fx2 + model.Fx3 + model.Fx4 +
                          model.x1 + model.x2 + model.x3 + model.x4,
                          sense=kwds.pop('sense', maximize))

    model.piecewise1 = Piecewise(model.Fx1,
                                 model.x1,
                                 pw_pts=DOMAIN_PTS,
                                 f_rule=F,
                                 **kwds)
    model.piecewise2 = Piecewise(model.Fx2,
                                 model.x2,
                                 pw_pts=DOMAIN_PTS,
                                 f_rule=F,
                                 **kwds)

    model.piecewise3 = Piecewise(model.Fx3,
                                 model.x3,
                                 pw_pts=DOMAIN_PTS,
                                 f_rule=F,
                                 **kwds)

    model.piecewise4 = Piecewise(model.Fx4,
                                 model.x4,
                                 pw_pts=DOMAIN_PTS,
                                 f_rule=F,
                                 **kwds)

    #Fix the answer for testing purposes
    model.set_answer_constraint1 = Constraint(
        expr=model.x1 == 0.5)  # Fx1 should solve to 0
    model.set_answer_constraint2 = Constraint(expr=model.x2 == 1.0)  #
    model.set_answer_constraint3 = Constraint(expr=model.Fx2 == 0.5)  #
    model.set_answer_constraint4 = Constraint(
        expr=model.x3 == 1.5)  # Fx3 should solve to 1
    model.set_answer_constraint5 = Constraint(
        expr=model.x4 == 2.5)  # Fx4 should solve to 1.5

    return model
Ejemplo n.º 2
0
def create_instance(scenario_name):
    cnt = d[scenario_name]

    model = ConcreteModel()
    # first stage
    model.x = Var(bounds=(0, 10))
    # first stage derived
    model.y = Expression(expr=model.x + 1)
    model.fx = Var()
    # second stage
    model.z = Var(bounds=(-10, 10))
    # second stage derived
    model.q = Expression(expr=model.z**2)
    model.fz = Var()
    model.r = Var()
    # stage costs
    model.StageCost = Expression([1, 2])
    model.StageCost.add(1, model.fx)
    model.StageCost.add(2, -model.fz + model.r - cnt)
    model.o = Objective(expr=sum_product(model.StageCost))

    model.ZERO = Param(initialize=0, mutable=True)
    if cnt == 0:
        cnt = model.ZERO

    model.c_first_stage = Constraint(expr=model.x >= 0)

    # test our handling of intermediate variables that
    # are created by Piecewise but can not necessarily
    # be declared on the scenario tree
    model.p_first_stage = Piecewise(model.fx,
                                    model.x,
                                    pw_pts=[0., 2., 5., 7., 10.],
                                    pw_constr_type='EQ',
                                    pw_repn='INC',
                                    f_rule=[10., 10., 9., 10., 10.],
                                    force_pw=True)

    model.c_second_stage = Constraint(expr=model.x + model.r * cnt >= -100)
    model.r_second_stage = Constraint(expr=inequality(-cnt, model.r, 0))
    # exercise more of the code by making this an indexed
    # block
    model.p_second_stage = Piecewise([1],
                                     model.fz,
                                     model.z,
                                     pw_pts=[-10, -5., 0., 5., 10.],
                                     pw_constr_type='EQ',
                                     pw_repn='INC',
                                     f_rule=[0., 0., -1., 2. + cnt, 1.],
                                     force_pw=True)

    return model
Ejemplo n.º 3
0
def define_model(**kwds):

    model = ConcreteModel()

    model.x = Var(INDEX_SET, bounds=(-5, 4))  # domain variable
    model.Fx = Var(INDEX_SET)  # range variable
    model.p = Param(INDEX_SET, initialize=1.0)

    model.obj = Objective(expr=sum_product(model.Fx),
                          sense=kwds.pop('sense', maximize))

    model.piecewise = Piecewise(INDEX_SET,
                                model.Fx,
                                model.x,
                                pw_pts=DOMAIN_PTS,
                                f_rule=F,
                                **kwds)

    #Fix the answer for testing purposes
    model.set_answer_constraint1 = Constraint(expr=model.x[1] == -5.0)
    model.set_answer_constraint2 = Constraint(expr=model.x[2] == -3.0)
    model.set_answer_constraint3 = Constraint(expr=model.x[3] == -2.5)
    model.set_answer_constraint4 = Constraint(expr=model.x[4] == -1.5)
    model.set_answer_constraint5 = Constraint(expr=model.x[5] == 2.0)
    model.set_answer_constraint6 = Constraint(expr=model.x[6] == 3.5)
    model.set_answer_constraint7 = Constraint(expr=model.x[7] == 4.0)

    return model
Ejemplo n.º 4
0
def define_model(**kwds):

    model = ConcreteModel()

    model.x1 = Var(bounds=(-5,4)) # domain variable
    model.x2 = Var(bounds=(-5,4)) # domain variable    
    model.x3 = Var(bounds=(-5,4)) # domain variable
    model.x4 = Var(bounds=(-5,4)) # domain variable
    model.x5 = Var(bounds=(-5,4)) # domain variable
    model.x6 = Var(bounds=(-5,4)) # domain variable
    model.x7 = Var(bounds=(-5,4)) # domain variable

    model.Fx1 = Var() # range variable
    model.Fx2 = Var() # range variable
    model.Fx3 = Var() # range variable
    model.Fx4 = Var() # range variable
    model.Fx5 = Var() # range variable
    model.Fx6 = Var() # range variable
    model.Fx7 = Var() # range variable

    model.obj = Objective(expr=model.Fx1+model.Fx2+model.Fx3+model.Fx4+model.Fx5+model.Fx6+model.Fx7, sense=kwds.pop('sense',maximize))

    model.piecewise1 = Piecewise(model.Fx1,model.x1,
                                  pw_pts=DOMAIN_PTS,
                                  f_rule=F, **kwds)
    model.piecewise2 = Piecewise(model.Fx2,model.x2,
                                  pw_pts=DOMAIN_PTS,
                                  f_rule=F, **kwds)
    model.piecewise3 = Piecewise(model.Fx3,model.x3,
                                  pw_pts=DOMAIN_PTS,
                                  f_rule=F, **kwds)
    model.piecewise4 = Piecewise(model.Fx4,model.x4,
                                  pw_pts=DOMAIN_PTS,
                                  f_rule=F, **kwds)
    model.piecewise5 = Piecewise(model.Fx5,model.x5,
                                  pw_pts=DOMAIN_PTS,
                                  f_rule=F, **kwds)
    model.piecewise6 = Piecewise(model.Fx6,model.x6,
                                  pw_pts=DOMAIN_PTS,
                                  f_rule=F, **kwds)
    model.piecewise7 = Piecewise(model.Fx7,model.x7,
                                  pw_pts=DOMAIN_PTS,
                                  f_rule=F, **kwds)

    #Fix the answer for testing purposes
    model.set_answer_constraint1 = Constraint(expr= model.x1 == -5.0)
    model.set_answer_constraint2 = Constraint(expr= model.x2 == -3.0)
    model.set_answer_constraint3 = Constraint(expr= model.x3 == -2.5)
    model.set_answer_constraint4 = Constraint(expr= model.x4 == -1.5)
    model.set_answer_constraint5 = Constraint(expr= model.x5 == 2.0)
    model.set_answer_constraint6 = Constraint(expr= model.x6 == 3.5)
    model.set_answer_constraint7 = Constraint(expr= model.x7 == 4.0)    
    
    return model
Ejemplo n.º 5
0
    def _generate_model(self):
        self.model = ConcreteModel()
        model = self.model
        model._name = self.description

        model.x = Var()
        model.y = Var()

        model.obj = Objective(expr=model.y)
        model.p = Piecewise(model.y, model.x,
                            pw_pts=[-1,0,1],
                            f_rule=[1,0.5,1],
                            pw_repn='SOS2',
                            pw_constr_type='LB',
                            unbounded_domain_var=True)
Ejemplo n.º 6
0
def define_model(**kwds):

    model = ConcreteModel()

    model.x = Var(INDEX_SET1, INDEX_SET2, bounds=(0,6)) # domain variable
    model.Fx = Var(INDEX_SET1, INDEX_SET2) # range variable
    model.p = Param(INDEX_SET1, INDEX_SET2, initialize=1.0)

    model.obj = Objective(expr=sum_product(model.Fx), sense=kwds.pop('sense',maximize))

    model.piecewise = Piecewise(INDEX_SET1,INDEX_SET2,model.Fx,model.x,
                                  pw_pts=DOMAIN_PTS,
                                  f_rule=F, **kwds)

    #Fix the answer for testing purposes
    model.set_answer_constraint1 = Constraint(INDEX_SET2,rule= lambda model,t2,t3: model.x['1',t2,t3] == 0.0)
    model.set_answer_constraint2 = Constraint(INDEX_SET2,rule= lambda model,t2,t3: model.x['2',t2,t3] == 3.0)
    model.set_answer_constraint3 = Constraint(INDEX_SET2,rule= lambda model,t2,t3: model.x['3',t2,t3] == 5.5)
    model.set_answer_constraint4 = Constraint(INDEX_SET2,rule= lambda model,t2,t3: model.x['40',t2,t3] == 6.0)

    return model
Ejemplo n.º 7
0
def define_model(**kwds):

    model = ConcreteModel()

    model.x = Var(INDEX) # domain variable
    
    model.Fx = Var(INDEX) # range variable

    model.obj = Objective(expr=sum_product(model.Fx)+sum_product(model.x), sense=kwds.pop('sense',maximize))

    model.piecewise = Piecewise(INDEX,model.Fx,model.x,
                                  pw_pts=DOMAIN_PTS,
                                  f_rule=F,
                                  unbounded_domain_var=True,
                                  **kwds)

    #Fix the answer for testing purposes
    model.set_answer_constraint1 = Constraint(expr= model.x[1] == 0.5) # Fx1 should solve to 0
    model.set_answer_constraint2 = Constraint(expr= model.x[2] == 1.0)  # 
    model.set_answer_constraint3 = Constraint(expr= model.Fx[2] == 0.5) # 
    model.set_answer_constraint4 = Constraint(expr= model.x[3] == 1.5) # Fx3 should solve to 1
    model.set_answer_constraint5 = Constraint(expr= model.x[4] == 2.5) # Fx4 should solve to 1.5
    
    return model