Exemplo n.º 1
0
def MeetShortage_rule(model):
    lt_q = pyo.sum_product(model.LT_QF, model.LT_ACTION)
    mt_q = pyo.quicksum(model.LT_QF[i] * model.LT_ACTION[i] * model.MT_EXP[i]
                        for i in model.LT)
    st_q = pyo.quicksum(model.ST_Q[k] for k in model.ST)
    tot_q = lt_q + mt_q + st_q
    return tot_q >= model.SHORTAGE_Q['SH']
Exemplo n.º 2
0
    def test_quicksum(self):
        m = ConcreteModel()
        m.y = Var(domain=Binary)
        m.c = Constraint(expr=quicksum([m.y, m.y], linear=True) == 1)

        lbl = NumericLabeler('x')
        smap = SymbolMap(lbl)
        tc = StorageTreeChecker(m)
        self.assertEqual(("x1 + x1", False),
                         expression_to_string(m.c.body, tc, smap=smap))
        m.x = Var()
        m.c2 = Constraint(expr=quicksum([m.x, m.y], linear=True) == 1)
        self.assertEqual(("x2 + x1", False),
                         expression_to_string(m.c2.body, tc, smap=smap))

        m.y.fix(1)
        lbl = NumericLabeler('x')
        smap = SymbolMap(lbl)
        tc = StorageTreeChecker(m)
        self.assertEqual(("1 + 1", False),
                         expression_to_string(m.c.body, tc, smap=smap))
        m.x = Var()
        m.c2 = Constraint(expr=quicksum([m.x, m.y], linear=True) == 1)
        self.assertEqual(("x1 + 1", False),
                         expression_to_string(m.c2.body, tc, smap=smap))
Exemplo n.º 3
0
 def minimumDownTime2(pyM, loc, compName, p, t):
     downTimeMin = getattr(compDict[compName], 'downTimeMin')
     if t >= downTimeMin:
         return opVarBin[loc, compName, p, t] <= 1 -pyomo.quicksum(opVarStopBin[loc, compName, p, t_down] for t_down in range(t-downTimeMin+1, t))
     else:
         return opVarBin[loc, compName, p, t] <= 1 -pyomo.quicksum(opVarStopBin[loc, compName, p, t_down] for t_down in range(0, t)) \
             - pyomo.quicksum(opVarStopBin[loc, compName, p, t_down] for t_down in range(numberOfTimeSteps-(downTimeMin-t), numberOfTimeSteps))
Exemplo n.º 4
0
def test_convex_relaxation_with_quadratic_only():
    m = pe.ConcreteModel()
    m.I = range(10)
    m.x = pe.Var(m.I)
    m.obj = pe.Objective(expr=pe.quicksum(m.x[i]*m.x[i] for i in m.I))
    m.c0 = pe.Constraint(expr=pe.quicksum(2 * m.x[i] * m.x[i] for i in m.I) >= 0)
    dag = problem_from_pyomo_model(m)

    relaxation = _convex_relaxation(dag)
    relaxed = relaxation.relax(dag)

    assert relaxed.objective
    # original constraint + 10 for disaggregated squares
    assert len(relaxed.constraints) == 1 + 10

    objective = relaxed.objective
    constraint = relaxed.constraints[0]

    assert isinstance(objective.root_expr, LinearExpression)
    assert len(objective.root_expr.children) == 10

    for constraint in relaxed.constraints[:-1]:
        assert isinstance(constraint.root_expr, SumExpression)
        children = constraint.root_expr.children
        assert len(children) == 2
        assert isinstance(children[0], LinearExpression)
        assert isinstance(children[1], QuadraticExpression)

    constraint = relaxed.constraints[-1]
    assert len(constraint.root_expr.children) == 10
    assert isinstance(constraint.root_expr, LinearExpression)
Exemplo n.º 5
0
def test_linear_relaxation_with_quadratic_and_linear():
    m = pe.ConcreteModel()
    m.I = range(10)
    m.x = pe.Var(m.I, bounds=(0, 1))
    m.obj = pe.Objective(
        expr=pe.quicksum(m.x[i]*m.x[i] for i in m.I) + pe.quicksum(m.x[i] for i in m.I)
    )
    m.c0 = pe.Constraint(
        expr=pe.quicksum(2 * m.x[i] * m.x[i] for i in m.I) + pe.quicksum(m.x[i] for i in m.I) >= 0
    )
    dag = problem_from_pyomo_model(m)

    relaxation = _linear_relaxation(dag)
    relaxed = relaxation.relax(dag)
    print_problem(relaxed)
    assert relaxed.objective
    # 1 objective, 1 c0, 4 * 10 x^2 (3 mccormick, 1 midpoint)
    assert len(relaxed.constraints) == 1 + 1 + 4*10

    objective = relaxed.objective
    constraint = relaxed.constraint('c0')

    assert isinstance(objective.root_expr, LinearExpression)
    assert isinstance(constraint.root_expr, SumExpression)

    # Root is only objvar
    assert len(objective.root_expr.children) == 1

    c0, c1 = constraint.root_expr.children

    assert isinstance(c0, LinearExpression)
    assert isinstance(c1, LinearExpression)
    assert len(c0.children) == 10
    assert len(c1.children) == 10
Exemplo n.º 6
0
 def minimumUpTime2(pyM, loc, compName, p, t):
     upTimeMin = getattr(compDict[compName], 'upTimeMin')
     if t >= upTimeMin:
         return opVarBin[loc, compName, p, t] >= pyomo.quicksum(opVarStartBin[loc, compName, p, t_up] for t_up in range(t-upTimeMin+1, t))
     else:
         return opVarBin[loc, compName, p, t] >= pyomo.quicksum(opVarStartBin[loc, compName, p, t_up] for t_up in range(0, t)) \
             + pyomo.quicksum(opVarStartBin[loc, compName, p, t_up] for t_up in range(numberOfTimeSteps-(upTimeMin-t), numberOfTimeSteps))
Exemplo n.º 7
0
 def rule(x, compute_values=False):
     if compute_values:
         return (quicksum(value(index_coef_dict[i]) * x[i]
                          for i in x) + value(constant))
     else:
         return quicksum(index_coef_dict[i] * x[i]
                         for i in x) + constant
Exemplo n.º 8
0
def MeetShortage_rule(model):
    lt_q = model.LT_ACTION['RETRO']
    exp_vop_q = pyo.quicksum(model.EXP_VOP_ACTION[i] *
                             model.LT_EXP_PERMIT_ACTION[i]
                             for i in model.LT_EXP)
    st_q = pyo.quicksum(model.ST_ACTION[i] for i in model.ST)
    short_q = pyo.quicksum(model.SHORT_ACTION[i] for i in model.SHORT_ACTION)
    return lt_q + st_q + exp_vop_q + short_q >= model.SHORTAGE_Q['SH']
Exemplo n.º 9
0
def ComputeSecondStageCost_rule(model):

    expansion_cost = pyo.quicksum(model.EXP_ACTION[i] * model.LT_EXP_COST[i]
                                  for i in model.LT_EXP)

    baseline_op_cost = pyo.quicksum(model.EXP_BOP_ACTION[i] *
                                    data['LT_EXP'][i]['baseline_op_cost']
                                    for i in model.LT_EXP)

    return expansion_cost + baseline_op_cost
Exemplo n.º 10
0
def ComputeThirdStageCost_rule(model):

    st_actions = pyo.sum_product(model.C_ST, model.ST_ACTION)

    short_cost = pyo.quicksum(model.SHORT_ACTION[i] * model.SHORT_COST[i]
                              for i in model.SHORT_ACTION)

    exp_op_cost = pyo.quicksum(model.EXP_VOP_ACTION[i] *
                               data['LT_EXP'][i]['variable_op_cost']
                               for i in model.LT_EXP)

    return st_actions + short_cost + exp_op_cost
Exemplo n.º 11
0
 def test_fixed_linear_expr(self):
     # Note that this checks both that a fixed variable is fixed, and
     # that the resulting model type is correctly classified (in this
     # case, fixing a binary makes this an LP)
     m = ConcreteModel()
     m.y = Var(within=Binary)
     m.y.fix(0)
     m.x = Var(bounds=(0,None))
     m.c1 = Constraint(expr=quicksum([m.y, m.y], linear=True) >= 0)
     m.c2 = Constraint(expr=quicksum([m.x, m.y], linear=True) == 1)
     m.obj = Objective(expr=m.x)
     self._check_baseline(m)
Exemplo n.º 12
0
def Objective_mls(mod):
    '''
    multivariate response least squares objective
    '''
    M = mod.M
    P = mod.P
    T = mod.T

    return (pyo.quicksum(mod.Q[m][p1, p2] * mod.beta[m, p1] * mod.beta[m, p2]
                         for m in range(M) for p1 in range(P)
                         for p2 in range(P)) +
            pyo.quicksum(mod.R[m][p] * mod.beta[m, p] for m in range(M)
                         for p in range(P)) + pyo.quicksum(mod.C))
Exemplo n.º 13
0
def Objective_mls_shrinkl1(mod):
    M = mod.M
    P = mod.P
    T = mod.T

    return (pyo.quicksum(mod.Q[m][p1, p2] * mod.beta[m, p1] * mod.beta[m, p2]
                         for m in range(M) for p1 in range(P)
                         for p2 in range(P)) +
            pyo.quicksum(mod.R[m][p] * mod.beta[m, p] for m in range(M)
                         for p in range(P)) + pyo.quicksum(mod.C) +
            pyo.quicksum(mod.param_gamma *
                         (mod.beta_tilde[m, p, 0] + mod.beta_tilde[m, p, 1])
                         for p in range(P) for m in range(M)))
Exemplo n.º 14
0
 def test_sum2(self):
     model = ConcreteModel()
     model.A = Set(initialize=[1,2,3], doc='set A')
     model.x = Var(model.A)
     expr = quicksum(model.x[i] for i in model.x)
     baseline = "x[1] + x[2] + x[3]"
     self.assertEqual( str(expr), baseline )
Exemplo n.º 15
0
 def conv_rule(model, scenario_name, var_name):
     return model.x[var_name] \
         + model.slack_plus[scenario_name, var_name] \
         - model.slack_minus[scenario_name, var_name] \
         == pyo.quicksum(model.conv[scenario_name, ix] *
             points[scenario_name][ix][var_name]
             for ix in range(len(points[scenario_name])))
Exemplo n.º 16
0
def Portfolio():
    m = pe.ConcreteModel()
    m.cons = pe.ConstraintList()

    N = 5
    index = list(range(N))
    mean = [0.1, 0.3, 0.5, 0.7, 0.4]

    m.x = pe.Var(index, bounds=(0, 1))
    m.z = pe.Var(within=pe.PositiveReals)

    m.U = ro.UncSet()
    m.r = ro.UncParam(index, uncset=m.U, nominal=mean)
    r = m.r
    expr = 0
    for i in index:
        expr += (m.r[i] - mean[i])**2
    m.U.cons = pe.Constraint(expr=expr <= 0.0005)
    m.Elib = ro.uncset.EllipsoidalSet(mean,
                                      [[0.0005, 0, 0, 0, 0],
                                       [0, 0.0005, 0, 0, 0],
                                       [0, 0, 0.0005, 0, 0],
                                       [0, 0, 0, 0.0005, 0],
                                       [0, 0, 0, 0, 0.0005]])

    P = [[1, 1, 0, 0, 0],
         [-1, 1, 0, 0, 0],
         [1, -1, 0, 0, 0],
         [-1, -1, 0, 0, 0],
         [0, 0, 1, 0, 0],
         [0, 0, -1, 0, 0],
         [0, 0, 0, 1, 0],
         [0, 0, 0, -1, 0],
         [0, 0, 0, 0, 1],
         [0, 0, 0, 0, -1]]
    rhs = [0.001 + mean[0] + mean[1],
           0.001 - mean[0] + mean[1],
           0.001 + mean[0] - mean[1],
           0.001 - mean[0] - mean[1],
           0.001 + mean[2],
           0.001 - mean[2],
           0.001 + mean[3],
           0.001 - mean[3],
           0.001 + mean[4],
           0.001 - mean[4]]
    m.Plib = ro.uncset.PolyhedralSet(P, rhs)

    m.P = ro.UncSet()
    m.P.cons = pe.ConstraintList()
    for i, row in enumerate(P):
        m.P.cons.add(expr=pe.quicksum(row[j]*m.r[j] for j in m.r) <= rhs[i])

    m.Obj = pe.Objective(expr=m.z, sense=pe.maximize)
    # x0 = m.x[0]
    # expr = x0*3
    expr = sum([m.x[i] for i in index]) == 1
    m.cons.add(expr)
    m.cons.add(sum([r[i]*m.x[i] for i in index]) >= m.z)

    return m
Exemplo n.º 17
0
    def _extract_objective(self, mip):
        ''' Extract the original part of the provided MIP's objective function
            (no dual or prox terms), and create a copy containing the QP
            variables in place of the MIP variables.

            Args:
                mip (Pyomo ConcreteModel): MIP model for a scenario or bundle.

            Returns:
                obj (Pyomo Objective): objective function extracted
                    from the MIP
                new (Pyomo Expression): expression from the MIP model
                    objective with the MIP variables replaced by QP variables.
                    Does not inculde dual or prox terms.

            Notes:
                Acts on either a single-scenario model or a bundle
        '''
        mip_to_qp = mip._mpisppy_data.mip_to_qp
        obj = find_active_objective(mip)
        repn = generate_standard_repn(obj.expr, quadratic=True)
        if len(repn.nonlinear_vars) > 0:
            raise ValueError("FWPH does not support models with nonlinear objective functions")
        linear_vars = [mip_to_qp[id(var)] for var in repn.linear_vars]
        new = LinearExpression(
            constant=repn.constant, linear_coefs=repn.linear_coefs, linear_vars=linear_vars
        )
        if repn.quadratic_vars:
            quadratic_vars = (
                (mip_to_qp[id(x)], mip_to_qp[id(y)]) for x,y in repn.quadratic_vars
            )
            new += pyo.quicksum(
                (coef*x*y for coef,(x,y) in zip(repn.quadratic_coefs, quadratic_vars))
            )
        return obj, new
Exemplo n.º 18
0
 def distribution_system_pumping_power_rule(
     problem,
     time_step
 ):
     # Distributed Secondary Pumping
     if distributed_secondary_pumping:
         rule = (
             problem.distribution_system_total_power[time_step]
             == py.quicksum(
                 (1 / self.parameters.distribution_system["pump efficiency secondary pump [-]"])
                 * self.parameters.physics["water density [kg/m^3]"]
                 * self.parameters.physics["gravitational acceleration [m^2/s]"]
                 * ds_head_differences_time_array[str(time_step)][building_id]
                 * problem.ets_flows_var[time_step, building_id]
                 for building_id in problem.building_ids
             )
         )
         return rule
     # Central Secondary Pumping
     else:
         rule = (
             problem.distribution_system_total_power[time_step]
             == (
                     (1 / self.parameters.distribution_system["pump efficiency secondary pump [-]"])
                     * self.parameters.physics["water density [kg/m^3]"]
                     * self.parameters.physics["gravitational acceleration [m^2/s]"]
                     * ds_head_differences_time_array[str(time_step)].max()
                     * problem.total_flow_demand[time_step]
             )
         )
         return rule
Exemplo n.º 19
0
 def __prevent_excessive_township_supply_constraint(self):
     """
     Prevent excessive supply from warehouses to townships, especially during profit maximisation.
     """
     for t in self.model.T:
         self.model.prevent_excessive_township_supply_constraint.add(
             pyo.quicksum(self.model.x_assign[w, t]
                          for w in self.model.W) <= self.model.t_demand[t])
Exemplo n.º 20
0
 def __township_demand_fulfillment_constraint(self):
     """
     All township demands must be fulfilled.
     """
     for t in self.model.T:
         self.model.township_demand_fulfillment_constraint.add(
             pyo.quicksum(self.model.x_assign[w, t]
                          for w in self.model.W) >= self.model.t_demand[t])
Exemplo n.º 21
0
 def __warehouse_supply_constraint(self):
     """
     Warehouse supply to townships must not exceed warehouse's capacity (volume).
     """
     for w in self.model.W:
         self.model.warehouse_supply_constraint.add(
             pyo.quicksum(self.model.x_assign[w, t]
                          for t in self.model.T) <= self.model.w_volume[w])
Exemplo n.º 22
0
    def scenario_creator_callback(self, scenario_name, **kwargs):
        ## fist, get the model out
        model = self._scenario_tree_instance_factory.construct_scenario_instance(
            scenario_name, self._pysp_scenario_tree)

        tree_scenario = self._pysp_scenario_tree.get_scenario(scenario_name)

        non_leaf_nodes = tree_scenario.node_list[:-1]

        for node in non_leaf_nodes:
            if node.is_leaf_node():
                raise Exception("Unexpected leaf node")

            node._mpisppy_cost_expression = _get_cost_expression(
                model, node._cost_variable)
            node._mpisppy_nonant_list = _get_nonant_list(model, node)
            node._mpisppy_nonant_ef_suppl_list = _get_derived_nonant_list(
                model, node)

        ## add the things mpisppy expects to the model
        model._mpisppy_probability = tree_scenario.probability

        model._mpisppy_node_list = [
            mpisppyScenarioNode(
                name=node._mpisppy_name,
                cond_prob=node.conditional_probability,
                stage=node._mpisppy_stage,
                cost_expression=node._mpisppy_cost_expression,
                scen_name_list=None,
                nonant_list=node._mpisppy_nonant_list,
                scen_model=None,
                nonant_ef_suppl_list=node._mpisppy_nonant_ef_suppl_list,
                parent_name=node._mpisppy_parent_name,
            ) for node in non_leaf_nodes
        ]

        for _ in model.component_data_objects(pyo.Objective,
                                              active=True,
                                              descend_into=True):
            break
        else:  # no break
            print(
                "Provided model has no objective; using PySP auto-generated objective"
            )

            # attach PySP objective
            leaf_node = tree_scenario.node_list[-1]
            leaf_node._mpisppy_cost_expression = _get_cost_expression(
                model, leaf_node._cost_variable)

            if hasattr(model, "_PySPModel_objective"):
                raise RuntimeError(
                    "provided model has attribute _PySPModel_objective")

            model._PySPModel_objective = pyo.Objective(expr=\
                    pyo.quicksum(node._mpisppy_cost_expression for node in tree_scenario.node_list))

        return model
Exemplo n.º 23
0
 def test_uncparam_has_no_uncset(self):
     m = pe.ConcreteModel()
     m.w = ro.UncParam(range(3), nominal=(1, 2, 3))
     m.x = pe.Var(range(3))
     expr = pe.quicksum(m.w[i] * m.x[i] for i in range(3))
     m.cons = pe.Constraint(expr=expr <= 5)
     m.obj = pe.Objective(expr=m.x[0], sense=pe.maximize)
     solver = pe.SolverFactory('romodel.reformulation')
     self.assertRaises(AssertionError, lambda: solver.solve(m))
Exemplo n.º 24
0
 def __warehouse_selection_constraint(self):
     """
     Warehouse selection constraint to apply fixed monthly cost if any supply is provided from a warehouse.
     """
     for w in self.model.W:
         self.model.warehouse_selection_constraint.add(
             pyo.quicksum(
                 self.model.x_assign[w, t]
                 for t in self.model.T) <= 9_999_999 * self.model.x[w])
Exemplo n.º 25
0
 def test_quicksum_integer_var_fixed(self):
     m = ConcreteModel()
     m.x = Var()
     m.y = Var(domain=Binary)
     m.c = Constraint(expr=quicksum([m.y, m.y], linear=True) == 1)
     m.o = Objective(expr=m.x ** 2)
     m.y.fix(1)
     outs = StringIO()
     m.write(outs, format='gams')
     self.assertIn("USING nlp", outs.getvalue())
Exemplo n.º 26
0
def e_negentropy(model):
    """
    Calculation of the negative Shannon entropy for minimization problem

    Parameters
    ----------
    model : InvPBaseModel

    """
    return quicksum(model.PEST[n] * log(model.PEST[n]) for n in model.NZPSET)
Exemplo n.º 27
0
 def objective_cost_minimum(
     problem
 ):
     rule = (
         py.quicksum(
             (
                 problem.costs[time_step]
             ) for time_step in problem.time_set
         )
     )
     return rule
Exemplo n.º 28
0
def ComputeFirstStageCost_rule(model):

    lt_actions = pyo.sum_product(model.C_LT, model.LT_ACTION)

    lt_exp_actions = pyo.sum_product(model.LT_EXP_ACTION, model.LT_EXP_COST)

    lt_exp_permit_action = pyo.quicksum(model.LT_EXP_PERMIT_ACTION[i] *
                                        model.LT_EXP_PERMIT_COST[i]
                                        for i in model.LT_EXP)

    return lt_actions + lt_exp_actions + lt_exp_permit_action
Exemplo n.º 29
0
    def solve_pyomo_kernel(self, solver='cplex'):
        """
    Solve the problem using the kernel library of the Pyomo package.

    Parameters
    ----------
    solver (str, default:'cplex'): defines the solver used to solve the optimization problem

    Returns
    -------
    obj_fun (real): objective function
    sol (list): optimal solution of variables

    """
        # Model
        m = pk.block()
        # Sets
        m.i = range(self.nvar)
        m.j = range(self.ncon)
        # Variables
        m.x = pk.variable_list()
        for _ in m.i:
            m.x.append(pk.variable(domain=pk.Reals, lb=0))
        # Objective function
        m.obj = pk.objective(pe.quicksum(self.c[i] * m.x[i] for i in m.i),
                             sense=pk.minimize)
        # Constraints
        m.con = pk.constraint_list()
        for j in m.j:
            m.con.append(
                pk.constraint(body=pe.quicksum(self.A[j][i] * m.x[i]
                                               for i in m.i),
                              lb=None,
                              ub=self.b[j]))
        # Solve problem
        res = pe.SolverFactory(solver).solve(m,
                                             symbolic_solver_labels=True,
                                             tee=True)
        print(res['Solver'][0])
        # Output
        return round(m.obj(), 2), [round(m.x[i].value, 2) for i in m.i]
Exemplo n.º 30
0
def build_scenario_model(fname, scenario_ix):
    ''' Multiple threads could be parsing the same file at the same
        time but they're all read-only, should this should not present any
        problems (famous last words)
    '''
    data = parse(fname, scenario_ix=scenario_ix)
    num_nodes = data['N']
    adj = data['A']  # Adjacency matrix
    edges = data['el']  # Edge list
    c = data['c']  # First-stage  cost matrix (per edge)
    d = data['d']  # Second-stage cost matrix (per edge)
    u = data['u']  # Capacity of each arc
    b = data['b']  # Demand of each node
    p = data['p']  # Probability of scenario

    model = pyo.ConcreteModel()
    model.x = pyo.Var(edges, domain=pyo.Binary)  # First stage vars
    model.y = pyo.Var(edges, domain=pyo.NonNegativeReals)  # Second stage vars

    model.edges = edges
    model.PySP_prob = p
    ''' Objective '''
    model.FirstStageCost = pyo.quicksum(c[e] * model.x[e] for e in edges)
    model.SecondStageCost = pyo.quicksum(d[e] * model.y[e] for e in edges)
    obj_expr = model.FirstStageCost + model.SecondStageCost
    model.MinCost = pyo.Objective(expr=obj_expr, sense=pyo.minimize)
    ''' Variable upper bound constraints on each edge '''
    model.vubs = pyo.ConstraintList()
    for e in edges:
        expr = model.y[e] - u[e] * model.x[e]
        model.vubs.add(expr <= 0)
    ''' Flow balance constraints for each node '''
    model.bals = pyo.ConstraintList()
    for i in range(num_nodes):
        in_nbs = np.where(adj[:, i] > 0)[0]
        out_nbs = np.where(adj[i, :] > 0)[0]
        lhs = pyo.quicksum(model.y[i,j] for j in out_nbs) - \
              pyo.quicksum(model.y[j,i] for j in in_nbs)
        model.bals.add(lhs == b[i])

    return model