Esempio n. 1
0
    def _generate_model(self):
        self.model = None
        self.model = pmo.block()
        model = self.model
        model._name = self.description

        model.s = list(range(1, 13))
        model.x = pmo.variable_dict((i, pmo.variable()) for i in model.s)
        model.x[1].lb = -1
        model.x[1].ub = 1
        model.x[2].lb = -1
        model.x[2].ub = 1
        model.obj = pmo.objective(
            sum(model.x[i] * ((-1)**(i + 1)) for i in model.s))
        model.c = pmo.constraint_dict()
        # to make the variable used in the constraint match the name
        model.c[3] = pmo.constraint(model.x[3] >= -1.)
        model.c[4] = pmo.constraint(model.x[4] <= 1.)
        model.c[5] = pmo.constraint(model.x[5] == -1.)
        model.c[6] = pmo.constraint(model.x[6] == -1.)
        model.c[7] = pmo.constraint(model.x[7] == 1.)
        model.c[8] = pmo.constraint(model.x[8] == 1.)
        model.c[9] = pmo.constraint((-1., model.x[9], -1.))
        model.c[10] = pmo.constraint((-1., model.x[10], -1.))
        model.c[11] = pmo.constraint((1., model.x[11], 1.))
        model.c[12] = pmo.constraint((1., model.x[12], 1.))

        model.c_inactive = pmo.constraint_dict()
        # to make the variable used in the constraint match the name
        model.c_inactive[3] = pmo.constraint(model.x[3] >= -2.)
        model.c_inactive[4] = pmo.constraint(model.x[4] <= 2.)
Esempio n. 2
0
    def _generate_model(self):
        self.model = pmo.block()
        model = self.model
        model._name = self.description

        model.neg1 = pmo.parameter(value=-1.0)
        model.pos1 = pmo.parameter(value=1.0)

        model.s = list(range(1, 13))
        model.x = pmo.create_variable_dict(keys=model.s)
        model.x[1].lb = model.neg1
        model.x[1].ub = model.pos1
        model.x[2].lb = model.neg1
        model.x[2].ub = model.pos1
        model.obj = pmo.objective(sum(model.x[i] * ((-1)**(i))
                                      for i in model.s),
                                  sense=pmo.maximize)
        model.c = pmo.constraint_dict()
        model.c[3] = pmo.constraint(model.x[3] >= -1.)
        model.c[4] = pmo.constraint(model.x[4] <= 1.)
        model.c[5] = pmo.constraint(model.x[5] == -1.)
        model.c[6] = pmo.constraint(model.x[6] == -1.)
        model.c[7] = pmo.constraint(model.x[7] == 1.)
        model.c[8] = pmo.constraint(model.x[8] == 1.)
        model.c[9] = pmo.constraint((model.neg1, model.x[9], model.neg1))
        model.c[10] = pmo.constraint((-1., model.x[10], -1.))
        model.c[11] = pmo.constraint((1., model.x[11], 1.))
        model.c[12] = pmo.constraint((1., model.x[12], 1.))

        model.c_inactive = pmo.constraint_dict()
        # to make the variable used in the constraint match the name
        model.c_inactive[3] = pmo.constraint(model.x[3] >= -2.)
        model.c_inactive[4] = pmo.constraint(model.x[4] <= 2.)
Esempio n. 3
0
    def _generate_model(self):
        self.model = None
        self.model = pmo.block()
        model = self.model
        model._name = self.description

        model.s = list(range(1,13))
        model.x = pmo.variable_dict(
            (i, pmo.variable()) for i in model.s)
        model.x[1].lb = -1
        model.x[1].ub = 1
        model.x[2].lb = -1
        model.x[2].ub = 1
        model.obj = pmo.objective(sum(model.x[i]*((-1)**(i+1))
                                      for i in model.s))
        model.c = pmo.constraint_dict()
        # to make the variable used in the constraint match the name
        model.c[3] = pmo.constraint(model.x[3]>=-1.)
        model.c[4] = pmo.constraint(model.x[4]<=1.)
        model.c[5] = pmo.constraint(model.x[5]==-1.)
        model.c[6] = pmo.constraint(model.x[6]==-1.)
        model.c[7] = pmo.constraint(model.x[7]==1.)
        model.c[8] = pmo.constraint(model.x[8]==1.)
        model.c[9] = pmo.constraint((-1.,model.x[9],-1.))
        model.c[10] = pmo.constraint((-1.,model.x[10],-1.))
        model.c[11] = pmo.constraint((1.,model.x[11],1.))
        model.c[12] = pmo.constraint((1.,model.x[12],1.))

        model.c_inactive = pmo.constraint_dict()
        # to make the variable used in the constraint match the name
        model.c_inactive[3] = pmo.constraint(model.x[3]>=-2.)
        model.c_inactive[4] = pmo.constraint(model.x[4]<=2.)
Esempio n. 4
0
def build_constraint_dict():
    """Build a constraint_dict with no references to external
    objects so its size can be computed."""
    return constraint_dict(
        ((i, constraint(rhs=1, body=sum(A_data[p]*X_kernel[A_indices[p]]
                                        for p in range(A_indptr[i], A_indptr[i+1]))))
         for i in range(N)))
Esempio n. 5
0
    def _generate_model(self):
        self.model = pmo.block()
        model = self.model
        model._name = self.description

        model.f = pmo.variable()
        model.x = pmo.variable(lb=1,ub=3)
        model.fi = pmo.parameter_dict()
        model.fi[1] = pmo.parameter(value=1.0)
        model.fi[2] = pmo.parameter(value=2.0)
        model.fi[3] = pmo.parameter(value=0.0)
        model.xi = pmo.parameter_dict()
        model.xi[1] = pmo.parameter(value=1.0)
        model.xi[2] = pmo.parameter(value=2.0)
        model.xi[3] = pmo.parameter(value=3.0)
        model.p = pmo.variable(domain=NonNegativeReals)
        model.n = pmo.variable(domain=NonNegativeReals)
        model.lmbda = pmo.variable_dict(
            (i, pmo.variable()) for i in range(1,4))
        model.obj = pmo.objective(model.p+model.n)
        model.c1 = pmo.constraint_dict()
        model.c1[1] = pmo.constraint((0.0, model.lmbda[1], 1.0))
        model.c1[2] = pmo.constraint((0.0, model.lmbda[2], 1.0))
        model.c1[3] = pmo.constraint(0.0 <= model.lmbda[3])
        model.c2 = pmo.sos2(model.lmbda.values())
        model.c3 = pmo.constraint(sum(model.lmbda.values()) == 1)
        model.c4 = pmo.constraint(model.f==sum(model.fi[i]*model.lmbda[i]
                                               for i in model.lmbda))
        model.c5 = pmo.constraint(model.x==sum(model.xi[i]*model.lmbda[i]
                                               for i in model.lmbda))
        model.x.fix(2.75)

        # Make an empty SOS constraint
        model.c6 = pmo.sos2([])
Esempio n. 6
0
    def __init__(self, V, W,
                 pyomo_solver="ipopt",
                 pyomo_solver_io="nl",
                 integer_tolerance=1e-4):
        assert V > 0
        assert integer_tolerance > 0
        self.V = V
        self.W = W
        self._integer_tolerance = integer_tolerance
        N = range(len(self.W))
        m = self.model = pmo.block()
        x = m.x = pmo.variable_dict()
        y = m.y = pmo.variable_dict()
        for i in N:
            y[i] = pmo.variable(domain=pmo.Binary)
            for j in N:
                x[i,j] = pmo.variable(domain=pmo.Binary)

        m.B = pmo.expression(sum(y.values()))
        m.objective = pmo.objective(m.B, sense=pmo.minimize)

        m.B_nontrivial = pmo.constraint(m.B >= 1)

        m.capacity = pmo.constraint_dict()
        for i in N:
            m.capacity[i] = pmo.constraint(
                sum(x[i,j]*self.W[j] for j in N) <= self.V*y[i])

        m.assign_1 = pmo.constraint_dict()
        for j in N:
            m.assign_1[j] = pmo.constraint(
                sum(x[i,j] for i in N) == 1)

        # relax everything for the bound solves,
        # since the objective uses a simple heuristic
        self.true_domain_type = pmo.ComponentMap()
        for xij in self.model.x.components():
            self.true_domain_type[xij] = xij.domain_type
            xij.domain_type = pmo.RealSet
        for yi in self.model.y.components():
            self.true_domain_type[yi] = yi.domain_type
            yi.domain_type = pmo.RealSet

        self.opt = pmo.SolverFactory(
            pyomo_solver,
            solver_io=pyomo_solver_io)
Esempio n. 7
0
    def _generate_model(self):
        self.model = pmo.block()
        model = self.model
        model._name = self.description

        model.s = [1,2]
        model.x = pmo.variable()
        model.y = pmo.variable()
        model.z = pmo.variable(lb=0)

        model.obj = pmo.objective_dict()
        for i in model.s:
            model.obj[i] = pmo.objective(
                inactive_index_LP_obj_rule(model,i))

        model.OBJ = pmo.objective(model.x+model.y)
        model.obj[1].deactivate()
        model.OBJ.deactivate()
        model.c1 = pmo.constraint_dict()
        model.c1[1] = pmo.constraint(model.x<=1)
        model.c1[2] = pmo.constraint(model.x>=-1)
        model.c1[3] = pmo.constraint(model.y<=1)
        model.c1[4] = pmo.constraint(model.y>=-1)
        model.c1[1].deactivate()
        model.c1[4].deactivate()
        model.c2 = pmo.constraint_dict()
        for i in model.s:
            model.c2[i] = pmo.constraint(
                inactive_index_LP_c2_rule(model, i))

        model.b = pmo.block()
        model.b.c = pmo.constraint(model.z >= 2)
        model.B = pmo.block_dict()
        model.B[1] = pmo.block()
        model.B[1].c = pmo.constraint(model.z >= 3)
        model.B[2] = pmo.block()
        model.B[2].c = pmo.constraint(model.z >= 1)

        model.b.deactivate()
        model.B.deactivate()
        model.B[2].activate()
    def update_coupling_constraints(self):
        self.con_coupling_net_export = aml.constraint_dict()
        for interval in self.interval_set:
            sum_of_component_element_net_exports_interval = sum(
                getattr(self.parent, component_element_id).net_export[interval]
                for component_element_id in self.component_element_ids)
            # body_expr =  self.net_export[interval] - sum_of_component_element_net_exports_interval
            # self.con_coupling_net_export[interval] = aml.constraint(body=body_expr, rhs=0)

            self.con_coupling_net_export[interval] = aml.constraint(
                self.net_export[interval] -
                sum_of_component_element_net_exports_interval == 0)
Esempio n. 9
0
    def _generate_model(self):
        self.model = pmo.block()
        model = self.model
        model._name = self.description

        model.s = [1,2]
        model.x = pmo.variable()
        model.y = pmo.variable()
        model.z = pmo.variable(lb=0)

        model.obj = pmo.objective_dict()
        for i in model.s:
            model.obj[i] = pmo.objective(
                inactive_index_LP_obj_rule(model,i))

        model.OBJ = pmo.objective(model.x+model.y)
        model.obj[1].deactivate()
        model.OBJ.deactivate()
        model.c1 = pmo.constraint_dict()
        model.c1[1] = pmo.constraint(model.x<=1)
        model.c1[2] = pmo.constraint(model.x>=-1)
        model.c1[3] = pmo.constraint(model.y<=1)
        model.c1[4] = pmo.constraint(model.y>=-1)
        model.c1[1].deactivate()
        model.c1[4].deactivate()
        model.c2 = pmo.constraint_dict()
        for i in model.s:
            model.c2[i] = pmo.constraint(
                inactive_index_LP_c2_rule(model, i))

        model.b = pmo.block()
        model.b.c = pmo.constraint(model.z >= 2)
        model.B = pmo.block_dict()
        model.B[1] = pmo.block()
        model.B[1].c = pmo.constraint(model.z >= 3)
        model.B[2] = pmo.block()
        model.B[2].c = pmo.constraint(model.z >= 1)

        model.b.deactivate()
        model.B[1].deactivate()
Esempio n. 10
0
    def _generate_model(self):
        self.model = pmo.block()
        model = self.model
        model._name = self.description

        model.x = pmo.variable(domain=NonNegativeReals)
        model.y = pmo.variable(domain=NonNegativeReals)
        model.z = pmo.variable(domain=NonNegativeReals)
        model.fixed_var = pmo.variable()
        model.fixed_var.fix(0.2)
        model.q1 = pmo.variable(ub=0.2)
        model.q2 = pmo.variable(lb=-2)
        model.obj = pmo.objective(model.x+model.q1-model.q2,sense=maximize)
        model.c0 = pmo.constraint(model.x+model.y+model.z == 1)
        model.qc0 = pmo.constraint(model.x**2 + model.y**2 + model.fixed_var <= model.z**2)
        model.qc1 = pmo.constraint(model.x**2 <= model.y*model.z)
        model.c = pmo.constraint_dict()
        model.c[1] = pmo.constraint(lb=0, body=-model.q1**2 + model.fixed_var)
        model.c[2] = pmo.constraint(body=model.q2**2 + model.fixed_var, ub=5)
Esempio n. 11
0
    def _generate_model(self):
        self.model = pmo.block()
        model = self.model
        model._name = self.description

        model.x = pmo.variable(domain=NonNegativeReals)
        model.y = pmo.variable(domain=NonNegativeReals)
        model.z = pmo.variable(domain=NonNegativeReals)
        model.fixed_var = pmo.variable()
        model.fixed_var.fix(0.2)
        model.q1 = pmo.variable(ub=0.2)
        model.q2 = pmo.variable(lb=-2)
        model.obj = pmo.objective(model.x + model.q1 - model.q2,
                                  sense=maximize)
        model.c0 = pmo.constraint(model.x + model.y + model.z == 1)
        model.qc0 = pmo.constraint(
            model.x**2 + model.y**2 + model.fixed_var <= model.z**2)
        model.qc1 = pmo.constraint(model.x**2 <= model.y * model.z)
        model.c = pmo.constraint_dict()
        model.c[1] = pmo.constraint(lb=0, body=-model.q1**2 + model.fixed_var)
        model.c[2] = pmo.constraint(body=model.q2**2 + model.fixed_var, ub=5)
Esempio n. 12
0
    def _generate_model(self):
        self.model = None
        self.model = pmo.block()
        model = self.model
        model._name = self.description

        model.x = pmo.variable(domain=RealInterval(bounds=(float('-inf'),
                                                           None)))
        model.y = pmo.variable(ub=float('inf'))
        model.obj = pmo.objective(model.x - model.y)
        model.c = pmo.constraint_dict()
        model.c[1] = pmo.constraint(model.x >= -2)
        model.c[2] = pmo.constraint(model.y <= 3)
        cdata = model.c[3] = pmo.constraint((0, 1, 3))
        assert cdata.lb == 0
        assert cdata.ub == 3
        assert cdata.body() == 1
        assert not cdata.equality
        cdata = model.c[4] = pmo.constraint((0, 2, 3))
        assert cdata.lb == 0
        assert cdata.ub == 3
        assert cdata.body() == 2
        assert not cdata.equality
        cdata = model.c[5] = pmo.constraint((0, 1, None))
        assert cdata.lb == 0
        assert cdata.ub is None
        assert cdata.body() == 1
        assert not cdata.equality
        cdata = model.c[6] = pmo.constraint((None, 0, 1))
        assert cdata.lb is None
        assert cdata.ub == 1
        assert cdata.body() == 0
        assert not cdata.equality
        cdata = model.c[7] = pmo.constraint((1, 1))
        assert cdata.lb == 1
        assert cdata.ub == 1
        assert cdata.body() == 1
        assert cdata.equality
Esempio n. 13
0
    def _generate_model(self):
        self.model = None
        self.model = pmo.block()
        model = self.model
        model._name = self.description

        model.x = pmo.variable(domain=RealInterval(bounds=(float('-inf'), None)))
        model.y = pmo.variable(ub=float('inf'))
        model.obj = pmo.objective(model.x - model.y)
        model.c = pmo.constraint_dict()
        model.c[1] = pmo.constraint(model.x >= -2)
        model.c[2] = pmo.constraint(model.y <= 3)
        cdata = model.c[3] = pmo.constraint((0, 1, 3))
        assert cdata.lb == 0
        assert cdata.ub == 3
        assert cdata.body() == 1
        assert not cdata.equality
        cdata = model.c[4] = pmo.constraint((0, 2, 3))
        assert cdata.lb == 0
        assert cdata.ub == 3
        assert cdata.body() == 2
        assert not cdata.equality
        cdata = model.c[5] = pmo.constraint((0, 1, None))
        assert cdata.lb == 0
        assert cdata.ub is None
        assert cdata.body() == 1
        assert not cdata.equality
        cdata = model.c[6] = pmo.constraint((None, 0, 1))
        assert cdata.lb is None
        assert cdata.ub == 1
        assert cdata.body() == 0
        assert not cdata.equality
        cdata = model.c[7] = pmo.constraint((1,1))
        assert cdata.lb == 1
        assert cdata.ub == 1
        assert cdata.body() == 1
        assert cdata.equality
Esempio n. 14
0
    def test_iter_component_kernel(self):
        model = pmo.block()
        model.x = pmo.variable_list(pmo.variable(value=0) for _ in [1, 2, 3])
        model.z = pmo.variable(value=0)

        model.con = pmo.constraint_dict(
            (i, pmo.constraint(expr=model.x[i - 1] + model.z == i))
            for i in [1, 2, 3])
        model.zcon = pmo.constraint(expr=model.z >= model.x[2])

        model.param_t = pmo.parameter_tuple(
            pmo.parameter(value=36) for _ in [1, 2, 3])
        model.param = pmo.parameter(value=42)

        self.assertSameComponents(list(iter_component(model.x)), list(model.x))
        self.assertSameComponents(list(iter_component(model.z)), [model.z])
        self.assertSameComponents(list(iter_component(model.con)),
                                  list(model.con.values()))
        self.assertSameComponents(list(iter_component(model.zcon)),
                                  [model.zcon])
        self.assertSameComponents(list(iter_component(model.param_t)),
                                  list(model.param_t))
        self.assertSameComponents(list(iter_component(model.param)),
                                  [model.param])
Esempio n. 15
0
# used 0-based indexing
m.vl = pmo.variable_list()
for j in m.q:
    m.vl.append(
        pmo.variable(lb=i))

# @Variables_list



# @Constraints_single
m.c = pmo.constraint(
    sum(m.vd.values()) <= 9)
# @Constraints_single
# @Constraints_dict
m.cd = pmo.constraint_dict()
for i in m.s:
    for j in m.q:
        m.cd[i,j] = \
            pmo.constraint(
                body=m.vd[i],
                rhs=j)
# @Constraints_dict
# @Constraints_list
# uses 0-based indexing
m.cl = pmo.constraint_list()
for j in m.q:
    m.cl.append(
        pmo.constraint(
            lb=-5,
            body=m.vl[j]-m.v,
Esempio n. 16
0
    def _generate_model(self):
        self.model = pmo.block()
        model = self.model
        model._name = self.description

        model.s = [1, 2]

        model.x_unused = pmo.variable(domain=pmo.IntegerSet)
        model.x_unused.stale = False

        model.x_unused_initialy_stale = pmo.variable(domain=pmo.IntegerSet)
        model.x_unused_initialy_stale.stale = True

        model.X_unused = pmo.variable_dict(
            (i, pmo.variable(domain=pmo.IntegerSet)) for i in model.s)
        model.X_unused_initialy_stale = pmo.variable_dict(
            (i, pmo.variable(domain=pmo.IntegerSet)) for i in model.s)
        for i in model.s:
            model.X_unused[i].stale = False
            model.X_unused_initialy_stale[i].stale = True

        model.x = pmo.variable(domain=RangeSet(None, None))
        model.x.stale = False

        model.x_initialy_stale = pmo.variable(domain=pmo.IntegerSet)
        model.x_initialy_stale.stale = True

        model.X = pmo.variable_dict(
            (i, pmo.variable(domain=pmo.IntegerSet)) for i in model.s)
        model.X_initialy_stale = pmo.variable_dict(
            (i, pmo.variable(domain=pmo.IntegerSet)) for i in model.s)
        for i in model.s:
            model.X[i].stale = False
            model.X_initialy_stale[i].stale = True

        model.obj = pmo.objective(model.x + \
                                  model.x_initialy_stale + \
                                  sum(model.X.values()) + \
                                  sum(model.X_initialy_stale.values()))

        model.c = pmo.constraint_dict()
        model.c[1] = pmo.constraint(model.x >= 1)
        model.c[2] = pmo.constraint(model.x_initialy_stale >= 1)
        model.c[3] = pmo.constraint(model.X[1] >= 0)
        model.c[4] = pmo.constraint(model.X[2] >= 1)
        model.c[5] = pmo.constraint(model.X_initialy_stale[1] >= 0)
        model.c[6] = pmo.constraint(model.X_initialy_stale[2] >= 1)

        # Test that stale flags do not get updated
        # on inactive blocks (where "inactive blocks" mean blocks
        # that do NOT follow a path of all active parent blocks
        # up to the top-level model)
        flat_model = model.clone()
        model.b = pmo.block()
        model.B = pmo.block_dict()
        model.b.b = flat_model.clone()
        model.B[1] = pmo.block()
        model.B[1].b = flat_model.clone()
        model.B[2] = pmo.block()
        model.B[2].b = flat_model.clone()

        model.b.deactivate()
        model.B.deactivate(shallow=False)
        model.b.b.activate()
        model.B[1].b.activate()
        model.B[2].b.deactivate()
        assert model.b.active is False
        assert model.B[1].active is False
        assert model.B[1].active is False
        assert model.b.b.active is True
        assert model.B[1].b.active is True
        assert model.B[2].b.active is False
    def __init__(
        self,
        interval_set,
        params: dict,
    ):
        super().__init__()

        ## Setup
        # self.id = uuid4()
        self.id = params["name"]
        self.objective_terms = {}

        ## Parameters
        self.maxMW = aml.parameter(params["maxMW"])
        self.minMW = aml.parameter(params["minMW"])
        self.max_ramp = aml.parameter(params["max_ramp"])
        self.marginal_cost = aml.parameter(params["marginal_cost"])
        self.initial_commit = aml.parameter(params["initial_commit"])

        ## Variables
        self.output = aml.variable_dict()
        for interval in interval_set:
            self.output[interval] = aml.variable(lb=0, ub=self.maxMW)

        self.commit = aml.variable_dict()
        for interval in interval_set:
            self.commit[interval] = aml.variable(domain=aml.Binary)

        ## Expressions - this is where we define a common interface for model resource elements
        # Note in Optopy we have to have a generic and dynamic common interface for market products, different time indices, etc.
        # Will just use net_export as an interface for now
        self.net_export = aml.expression_dict()
        for interval in interval_set:
            self.net_export[interval] = aml.expression(expr=self.output)

        ## Constraints
        # Can express constraints abstractly (lhs, rhs, body, ub, lb, etc.) or with interpreted syntax - see below
        self.con_output_commit_upper_bound = aml.constraint_dict()
        for interval in interval_set:
            # body_expr = self.output[interval] - self.commit[interval] * self.maxMW
            # self.con_output_commit_upper_bound[interval] = aml.constraint(body=body_expr, ub=0)

            self.con_output_commit_upper_bound[interval] = aml.constraint(
                self.output[interval] -
                self.commit[interval] * self.maxMW <= 0)

        self.con_output_commit_lower_bound = aml.constraint_dict()
        for interval in interval_set:
            # body_expr =  self.commit[interval] * self.minMW - self.output[interval]
            # self.con_output_commit_lower_bound[interval] = aml.constraint(body=body_expr, ub=0)

            self.con_output_commit_lower_bound[interval] = aml.constraint(
                self.commit[interval] * self.minMW -
                self.output[interval] <= 0)

        # todo Add constraints/costs for ramping, min up, min down, startup/shutdown costs, etc.

        ## Objective Terms
        # Unclear whether this expression object needs to be added to block/model - may be enough just to have it in the objective
        self.interval_cost = aml.expression_dict()
        for interval in interval_set:
            self.interval_cost[interval] = aml.expression(
                self.marginal_cost * self.output[interval])
        self.objective_terms["total_cost"] = sum(self.interval_cost[interval]
                                                 for interval in interval_set)
Esempio n. 18
0
cl = pmo.constraint_list()
for i in range(10):
    cl.append(pmo.constraint(vl[-1] == 1))

cl.insert(0, pmo.constraint(vl[0]**2 >= 1))

del cl[0]

#
# Dict containers
#

vd = pmo.variable_dict(
    ((str(i), pmo.variable()) for i in range(10)))

cd = pmo.constraint_dict(
    (i, pmo.constraint(v == 1)) for i,v in vd.items())

cd = pmo.constraint_dict()
for i, v in vd.items():
    cd[i] = pmo.constraint(v == 1)

cd = pmo.constraint_dict()
cd.update((i, pmo.constraint()) for i,v in vd.items())

cd[None] = pmo.constraint()

del cd[None]

#
# Nesting containers
#
Esempio n. 19
0
for i in range(10):
    cl.append(pmo.constraint(vl[-1] == 1))

cl.insert(0, pmo.constraint(vl[0]**2 >= 1))

del cl[0]

#
# Dict containers
#

# uses OrderedDict when ordered=True
vd = pmo.variable_dict(((str(i), pmo.variable()) for i in range(10)),
                       ordered=True)

cd = pmo.constraint_dict((i, pmo.constraint(v == 1)) for i, v in vd.items())

cd = pmo.constraint_dict()
for i, v in vd.items():
    cd[i] = pmo.constraint(v == 1)

cd = pmo.constraint_dict()
cd.update((i, pmo.constraint()) for i, v in vd.items())

cd[None] = pmo.constraint()

del cd[None]

#
# Nesting containers
#
Esempio n. 20
0
b = pmo.block()

b.p = pmo.parameter()
b.plist = pmo.parameter_list(pmo.parameter() for i in range(10))
b.pdict = pmo.parameter_dict(
    ((i, j), pmo.parameter()) for i in range(10) for j in range(10))

b.x = pmo.variable()
b.xlist = pmo.variable_list(pmo.variable() for i in range(10))
b.xdict = pmo.variable_dict(
    ((i, j), pmo.variable()) for i in range(10) for j in range(10))

b.c = pmo.constraint(b.x >= 1)
b.clist = pmo.constraint_list(
    pmo.constraint(b.xlist[i] >= i) for i in range(10))
b.cdict = pmo.constraint_dict(((i, j), pmo.constraint(b.xdict[i, j] >= i * j))
                              for i in range(10) for j in range(10))

b.o = pmo.objective(b.x + sum(b.xlist) + sum(b.xdict.values()))

#
# Define a custom tiny_block
#


# The tiny_block class uses more efficient storage for the
# case when a small number of objects are placed on the block.
class Widget(pmo.tiny_block):
    def __init__(self, p, input=None):
        super(Widget, self).__init__()
        self.p = pmo.parameter(value=p)
        self.input = pmo.expression(expr=input)
Esempio n. 21
0
for i in m.s:
    m.vd[i] = pmo.variable(ub=9)
# @Variables_dict
# @Variables_list
# used 0-based indexing
m.vl = pmo.variable_list()
for j in m.q:
    m.vl.append(pmo.variable(lb=i))

# @Variables_list

# @Constraints_single
m.c = pmo.constraint(sum(m.vd.values()) <= 9)
# @Constraints_single
# @Constraints_dict
m.cd = pmo.constraint_dict()
for i in m.s:
    for j in m.q:
        m.cd[i,j] = \
            pmo.constraint(
                body=m.vd[i],
                rhs=j)
# @Constraints_dict
# @Constraints_list
# uses 0-based indexing
m.cl = pmo.constraint_list()
for j in m.q:
    m.cl.append(pmo.constraint(lb=-5, body=m.vl[j] - m.v, ub=5))
# @Constraints_list

# @Expressions_single
Esempio n. 22
0
    def _generate_model(self):
        self.model = None
        self.model = pmo.block()
        model = self.model
        model._name = self.description

        model.s = [1,2]
        model.x_unused = pmo.variable()
        model.x_unused.stale = False

        model.x_unused_initialy_stale = pmo.variable()
        model.x_unused_initialy_stale.stale = True

        model.X_unused = pmo.variable_dict(
            (i, pmo.variable()) for i in model.s)
        model.X_unused_initialy_stale = pmo.variable_dict(
            (i, pmo.variable()) for i in model.s)

        for i in model.X_unused:
            model.X_unused[i].stale = False
            model.X_unused_initialy_stale[i].stale = True

        model.x = pmo.variable()
        model.x.stale = False

        model.x_initialy_stale = pmo.variable()
        model.x_initialy_stale.stale = True

        model.X = pmo.variable_dict(
            (i, pmo.variable()) for i in model.s)
        model.X_initialy_stale = pmo.variable_dict(
            (i, pmo.variable()) for i in model.s)
        for i in model.X:
            model.X[i].stale = False
            model.X_initialy_stale[i].stale = True

        model.obj = pmo.objective(model.x + \
                                  model.x_initialy_stale + \
                                  sum(model.X.values()) + \
                                  sum(model.X_initialy_stale.values()))

        model.c = pmo.constraint_dict()
        model.c[1] = pmo.constraint(model.x          >= 1)
        model.c[2] = pmo.constraint(model.x_initialy_stale    >= 1)
        model.c[3] = pmo.constraint(model.X[1]       >= 0)
        model.c[4] = pmo.constraint(model.X[2]       >= 1)
        model.c[5] = pmo.constraint(model.X_initialy_stale[1] >= 0)
        model.c[6] = pmo.constraint(model.X_initialy_stale[2] >= 1)

        # Test that stale flags do not get updated
        # on inactive blocks (where "inactive blocks" mean blocks
        # that do NOT follow a path of all active parent blocks
        # up to the top-level model)
        flat_model = model.clone()
        model.b = pmo.block()
        model.B = pmo.block_dict()
        model.B[1] = pmo.block()
        model.B[2] = pmo.block()
        model.b.b = flat_model.clone()
        model.B[1].b = flat_model.clone()
        model.B[2].b = flat_model.clone()

        model.b.deactivate()
        model.B.deactivate(shallow=False)
        model.b.b.activate()
        model.B[1].b.activate()
        model.B[2].b.deactivate()
        assert model.b.active is False
        assert model.B[1].active is False
        assert model.B[1].active is False
        assert model.b.b.active is True
        assert model.B[1].b.active is True
        assert model.B[2].b.active is False