Exemplo n.º 1
0
    def test_stats2(self):
        model = ConcreteModel()
        #
        model.x = Var([1, 2])

        def obj_rule(model, i):
            return sum_product(model.x)

        model.y = VarList()
        model.y.add()
        model.y.add()
        #
        model.obj = Objective([1, 2], rule=obj_rule)
        model.o = ObjectiveList()
        model.o.add(model.y[1])
        model.o.add(model.y[2])

        #
        def c_rule(model, i):
            expr = 0
            for j in [1, 2]:
                expr += j * model.x[j]
            return expr == 0

        model.c = Constraint([1, 2], rule=c_rule)
        model.C = ConstraintList()
        model.C.add(model.y[1] == 0)
        model.C.add(model.y[2] == 0)
        #
        self.assertEqual(model.nvariables(), 4)
        self.assertEqual(model.nobjectives(), 4)
        self.assertEqual(model.nconstraints(), 4)
Exemplo n.º 2
0
    def test_expr5(self):
        model = ConcreteModel()
        model.A = Set(initialize=[1, 2, 3], doc='set A')
        model.B = Param(model.A,
                        initialize={
                            1: 100,
                            2: 200,
                            3: 300
                        },
                        doc='param B',
                        mutable=True)
        model.C = Param(initialize=3, doc='param C', mutable=True)
        model.x = Var(model.A, doc='var x')
        model.y = Var(doc='var y')
        model.o = Objective(expr=model.y, doc='obj o')
        model.c1 = Constraint(expr=model.x[1] >= 0, doc='con c1')

        def c2_rule(model, a):
            return model.B[a] * model.x[a] <= 1

        model.c2 = Constraint(model.A, doc='con c2', rule=c2_rule)
        model.c3 = ConstraintList(doc='con c3')
        model.c3.add(model.y <= 0)
        #
        OUTPUT = open(join(currdir, "test_expr5.out"), "w")
        model.pprint(ostream=OUTPUT)
        OUTPUT.close()
        _out, _txt = join(currdir,
                          "test_expr5.out"), join(currdir, "test_expr5.txt")
        self.assertTrue(cmp(_out, _txt),
                        msg="Files %s and %s differ" % (_out, _txt))
Exemplo n.º 3
0
    def test_add_single_constraint(self):
        model = ConcreteModel()
        model.X = Var(within=Binary)

        opt = SolverFactory("cplex", solver_io="python")
        opt._set_instance(model)

        self.assertEqual(opt._solver_model.linear_constraints.get_num(), 0)

        model.C = Constraint(expr=model.X == 1)

        con_interface = opt._solver_model.linear_constraints
        with unittest.mock.patch.object(
                con_interface, "add",
                wraps=con_interface.add) as wrapped_add_call:
            opt._add_constraint(model.C)

            self.assertEqual(wrapped_add_call.call_count, 1)
            self.assertEqual(
                wrapped_add_call.call_args,
                ({
                    "lin_expr": [[[0], (1, )]],
                    "names": ["x2"],
                    "range_values": [0.0],
                    "rhs": [1.0],
                    "senses": ["E"],
                }, ),
            )

        self.assertEqual(opt._solver_model.linear_constraints.get_num(), 1)
Exemplo n.º 4
0
#  ___________________________________________________________________________
#
#  Pyomo: Python Optimization Modeling Objects
#  Copyright (c) 2008-2022
#  National Technology and Engineering Solutions of Sandia, LLC
#  Under the terms of Contract DE-NA0003525 with National Technology and
#  Engineering Solutions of Sandia, LLC, the U.S. Government retains certain
#  rights in this software.
#  This software is distributed under the 3-clause BSD License.
#  ___________________________________________________________________________

from pyomo.environ import ConcreteModel, Var, Constraint

model = ConcreteModel()
model.X = Var()


def c_rule(m):
    try:
        return model.X >= 10.0
    except Exception:
        return model.X >= 20.0


model.C = Constraint(rule=c_rule)
Exemplo n.º 5
0
model.Pi_p = Param(model.N, initialize=square(network.maximum_pressure_bounds))

Psi_m = network.minimum_nodal_injections
Psi_p = network.maximum_nodal_injections

for i in model.N:
    if model.d[i] != 0:
        Psi_m[i] = -model.d[i]
        Psi_p[i] = 0

model.Psi_m = Param(model.N, initialize=Psi_m)
model.Psi_p = Param(model.N, initialize=Psi_p)

## Edges
model.P = Set(initialize=network.ref_pipes.keys())
model.C = Set(initialize=network.ref_compressors.keys())
model.E = model.P | model.C

model.c = Param(model.E, initialize=network.friction_coefficients)
model.rho_m = Param(model.E, initialize=square(network.minimum_pressure_ratio))
model.rho_p = Param(model.E, initialize=square(network.maximum_pressure_ratio))
model.kappa = Param(model.E, initialize=network.compression_costs)

## Incidence

temp = {}
for (i, j), value in np.ndenumerate(network.incidence_matrix):
    temp[i + 1, j + 1] = value

model.delta = Param(model.N, model.E, initialize=temp, mutable=True)