Exemplo n.º 1
0
    def __init__(self, xL, xU, yL, yU):
        assert xL <= xU
        assert yL <= yU
        self._model = pmo.block()
        x = self._model.x = pmo.variable(lb=xL, ub=xU)
        y = self._model.y = pmo.variable(lb=yL, ub=yU)
        x2 = self._model.x2 = pmo.variable(lb=-pybnb.inf, ub=pybnb.inf)
        x2y = self._model.x2y = pmo.variable(lb=-pybnb.inf, ub=pybnb.inf)
        self._model.x2_c = SquaredEnvelope(x, x2)
        # Temporarily provide bounds to the x2 variable so
        # they can be used to build the McCormick
        # constraints for x2y. After that, they are no
        # longer needed as they are enforced by the
        # McCormickEnvelope constraints.
        x2.bounds = self._model.x2_c.derived_output_bounds()
        self._model.x2y_c = McCormickEnvelope(x2, y, x2y)
        x2.bounds = (-pybnb.inf, pybnb.inf)

        # original objective
        self._model.f = pmo.expression(
            (x**4 - 2 * (x**2) * y + 0.5 * (x**2) - x + (y**2) + 0.5))
        # convex relaxation
        self._model.f_convex = pmo.expression(
            (x**4 - 2 * x2y + 0.5 * (x**2) - x + (y**2) + 0.5))
        self._model.objective = pmo.objective(sense=pmo.minimize)
        self._ipopt = pmo.SolverFactory("ipopt")
        self._ipopt.options["tol"] = 1e-9
        self._last_bound_was_feasible = False

        # make sure the PyomoProblem initializer is called
        # after the model is built
        super(Rosenbrock2D, self).__init__()
Exemplo n.º 2
0
    def _generate_model(self):
        self.model = pmo.block()
        model = self.model
        model._name = self.description

        model.a1 = pmo.parameter(value=1.0)
        model.a2 = pmo.parameter_dict({1: pmo.parameter(value=1.0)})
        model.a3 = pmo.parameter(value=1.0)
        model.a4 = pmo.parameter_dict({1: pmo.parameter(value=1.0)})
        model.x = pmo.variable(domain=NonNegativeReals)
        model.y = pmo.variable(domain=NonNegativeReals)
        model.z1 = pmo.variable()
        model.z2 = pmo.variable()
        model.dummy_expr1 = pmo.expression(model.a1 * model.a2[1])
        model.dummy_expr2 = pmo.expression(model.y / model.a3 * model.a4[1])

        model.inactive_obj = pmo.objective(model.x + 3.0 * model.y + 1.0 +
                                           model.z1 - model.z2)
        model.inactive_obj.deactivate()
        model.p = pmo.parameter(value=0.0)
        model.obj = pmo.objective(model.p + model.inactive_obj)

        model.c1 = pmo.constraint(
            model.dummy_expr1 <= pmo.noclone(model.dummy_expr2))
        model.c2 = pmo.constraint((2.0, model.x / model.a3 - model.y, 10))
        model.c3 = pmo.constraint((0, model.z1 + 1, 10))
        model.c4 = pmo.constraint((-10, model.z2 + 1, 0))
Exemplo n.º 3
0
    def _generate_model(self):
        self.model = pmo.block()
        model = self.model
        model._name = self.description

        model.a1 = pmo.parameter(value=1.0)
        model.a2 = pmo.parameter_dict(
            {1: pmo.parameter(value=1.0)})
        model.a3 = pmo.parameter(value=1.0)
        model.a4 = pmo.parameter_dict(
            {1: pmo.parameter(value=1.0)})
        model.x = pmo.variable(domain=NonNegativeReals)
        model.y = pmo.variable(domain=NonNegativeReals)
        model.z1 = pmo.variable()
        model.z2 = pmo.variable()
        model.dummy_expr1 = pmo.expression(model.a1*model.a2[1])
        model.dummy_expr2 = pmo.expression(model.y/model.a3*model.a4[1])

        model.inactive_obj = pmo.objective(
            model.x + 3.0*model.y + 1.0 + model.z1 - model.z2)
        model.inactive_obj.deactivate()
        model.p = pmo.parameter(value=0.0)
        model.obj = pmo.objective(model.p + model.inactive_obj)

        model.c1 = pmo.constraint(model.dummy_expr1 <= pmo.noclone(model.dummy_expr2))
        model.c2 = pmo.constraint((2.0, model.x/model.a3 - model.y, 10))
        model.c3 = pmo.constraint((0, model.z1 + 1, 10))
        model.c4 = pmo.constraint((-10, model.z2 + 1, 0))
Exemplo n.º 4
0
 def test_type_hack(self):
     for obj in [pmo.variable(),
                 pmo.constraint(),
                 pmo.objective(),
                 pmo.expression(),
                 pmo.parameter(),
                 pmo.suffix(),
                 pmo.sos([]),
                 pmo.block()]:
         ctype = obj.ctype
         self.assertIs(obj.__class__._ctype, ctype)
         self.assertIs(obj.type(), ctype)
Exemplo n.º 5
0
 def test_type_hack(self):
     for obj in [
             pmo.variable(),
             pmo.constraint(),
             pmo.objective(),
             pmo.expression(),
             pmo.parameter(),
             pmo.suffix(),
             pmo.sos([]),
             pmo.block()
     ]:
         ctype = obj.ctype
         self.assertIs(obj.__class__._ctype, ctype)
         self.assertIs(obj.type(), ctype)
Exemplo 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)
    def __init__(
        self,
        interval_set,
        params: dict,
    ):
        super().__init__()

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

        ## Params
        # Decide if we want load as mutable param - need to use param object
        # not sure what difference is with raw numpy - may be pyomo mutability - check this
        # self.load = aml.parameter_dict()
        self.load = params["load"]
        # for interval in interval_set:
        #     self.load[interval] = params["load"][interval]

        ## Expressions
        self.net_export = aml.expression_dict()
        for interval in interval_set:
            self.net_export[interval] = aml.expression(-self.load[interval])
Exemplo n.º 8
0
import pyomo.kernel as pmo

v = pmo.variable(value=2)

#
# Expressions
#

e = pmo.expression()
assert e() == None
assert e.expr == None

e = pmo.expression(expr=v**2 + 1)
assert e() == 5
assert pmo.value(e) == 5
assert pmo.value(e.expr) == 5

e = pmo.expression()
e.expr = v - 1
assert pmo.value(e) == 1

esub = pmo.expression(expr=v + 1)
e = pmo.expression(expr=esub + 1)
assert pmo.value(esub) == 3
assert pmo.value(e) == 4

esub.expr = v - 1
assert pmo.value(esub) == 1
assert pmo.value(e) == 2

c = pmo.constraint()
Exemplo n.º 9
0
            sum(model.var_x[(_worker, _task)]
                for _task in model.set_tasks) <= 1))
## Each task is assigned to exactly 1 worker
model.con_task = pmo.constraint_list()
for _task in model.set_tasks:
    model.con_task.append(
        pmo.constraint(
            sum(model.var_x[(_worker, _task)]
                for _worker in model.set_workers) == 1))

# Create the objective
expr = pmo.expression_list()
for _worker in model.set_workers:
    for _task in model.set_tasks:
        expr.append(
            pmo.expression(model.param_cost[(_worker, _task)] *
                           model.var_x[(_worker, _task)]))
model.obj = pmo.objective(sum(expr), sense=pmo.minimize)

# Solve
opt = pmo.SolverFactory('cplex')
result = opt.solve(model)

# Print the solution
if result.solver.termination_condition == TerminationCondition.optimal or result.solver.status == SolverStatus.ok:
    print('Total cost = ', pmo.value(model.obj), '\n')
    for i in range(NUM_WORKERS):
        for j in range(NUM_TASKS):
            # Test if x[i,j] is 1 (with tolerance for floating point arithmetic).
            if model.var_x[(i, j)].value > 0.5:
                print('Worker %d assigned to task %d.  Cost = %d' %
                      (i, j, costs[i][j]))
Exemplo n.º 10
0
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
m.e = pmo.expression(-m.v)
# @Expressions_single
# @Expressions_dict
m.ed = pmo.expression_dict()
for i in m.s:
    m.ed[i] = \
        pmo.expression(-m.vd[i])
# @Expressions_dict
# @Expressions_list
# uses 0-based indexed
m.el = pmo.expression_list()
for j in m.q:
    m.el.append(pmo.expression(-m.vl[j]))
# @Expressions_list

# @Objectives_single
Exemplo n.º 11
0
 def __init__(self, p, input=None):
     super(Widget, self).__init__()
     self.p = pmo.parameter(value=p)
     self.input = pmo.expression(expr=input)
     self.output = pmo.variable()
     self.c = pmo.constraint(self.output == self.input**2 / self.p)
    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)
Exemplo n.º 13
0
import pyomo.kernel as pmo

v = pmo.variable(value=2)

#
# Expressions
#

e = pmo.expression()
assert e() == None
assert e.expr == None

e = pmo.expression(expr= v**2 + 1)
assert e() == 5
assert pmo.value(e) == 5
assert pmo.value(e.expr) == 5

e = pmo.expression()
e.expr = v - 1
assert pmo.value(e) == 1

esub = pmo.expression(expr= v + 1)
e = pmo.expression(expr= esub + 1)
assert pmo.value(esub) == 3
assert pmo.value(e) == 4

esub.expr = v - 1
assert pmo.value(esub) == 1
assert pmo.value(e) == 2

c = pmo.constraint()
Exemplo n.º 14
0
# @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
m.e = pmo.expression(-m.v)
# @Expressions_single
# @Expressions_dict
m.ed = pmo.expression_dict()
for i in m.s:
    m.ed[i] = \
        pmo.expression(-m.vd[i])
# @Expressions_dict
# @Expressions_list
# uses 0-based indexed
m.el = pmo.expression_list()
for j in m.q:
    m.el.append(
        pmo.expression(-m.vl[j]))
# @Expressions_list