def test_construct2(self): model = AbstractModel() model.a = Set(initialize=[1,2,3]) model.A = Param(initialize=1) model.B = Param(model.a) model.x = Var(initialize=1, within=Reals, dense=True) model.y = Var(model.a, initialize=1, within=Reals, dense=True) model.obj = Objective(rule=lambda model: model.x+model.y[1]) model.obj2 = Objective(model.a,rule=lambda model, i: i+model.x+model.y[1]) model.con = Constraint(rule=rule1) model.con2 = Constraint(model.a, rule=rule2) instance = model.create_instance() expr = instance.x + 1 OUTPUT = open(join(currdir, "display2.out"), "w") display(instance,ostream=OUTPUT) display(instance.obj,ostream=OUTPUT) display(instance.x,ostream=OUTPUT) display(instance.con,ostream=OUTPUT) OUTPUT.write(expr.to_string()) model = AbstractModel() instance = model.create_instance() display(instance,ostream=OUTPUT) OUTPUT.close() try: display(None) self.fail("test_construct - expected TypeError") except TypeError: pass _out, _txt = join(currdir, "display2.out"), join(currdir, "display2.txt") self.assertTrue(cmp(_out, _txt), msg="Files %s and %s differ" % (_out, _txt))
def test_construct(self): model = AbstractModel() model.a = Set(initialize=[1, 2, 3]) model.A = Param(initialize=1) model.B = Param(model.a) model.x = Var(initialize=1, within=Reals, dense=False) model.y = Var(model.a, initialize=1, within=Reals, dense=False) model.obj = Objective(rule=lambda model: model.x + model.y[1]) model.obj2 = Objective(model.a, rule=lambda model, i: i + model.x + model.y[1]) model.con = Constraint(rule=rule1) model.con2 = Constraint(model.a, rule=rule2) instance = model.create_instance() expr = instance.x + 1 OUTPUT = open(currdir + "/display.out", "w") display(instance, ostream=OUTPUT) display(instance.obj, ostream=OUTPUT) display(instance.x, ostream=OUTPUT) display(instance.con, ostream=OUTPUT) OUTPUT.write(expr.to_string()) model = AbstractModel() instance = model.create_instance() display(instance, ostream=OUTPUT) OUTPUT.close() try: display(None) self.fail("test_construct - expected TypeError") except TypeError: pass self.assertFileEqualsBaseline(currdir + "/display.out", currdir + "/display.txt")
def CreateAbstractScenarioTreeModel(): from pyomo.core import (AbstractModel, Set, Param, Boolean) model = AbstractModel() # all set/parameter values are strings, representing the # names of various entities/variables. model.Stages = Set(ordered=True) model.Nodes = Set(ordered=True) model.NodeStage = Param(model.Nodes, within=model.Stages, mutable=True) model.Children = Set(model.Nodes, within=model.Nodes, initialize=[], ordered=True) model.ConditionalProbability = Param(model.Nodes, mutable=True) model.Scenarios = Set(ordered=True) model.ScenarioLeafNode = Param(model.Scenarios, within=model.Nodes, mutable=True) model.StageVariables = Set(model.Stages, initialize=[], ordered=True) model.NodeVariables = Set(model.Nodes, initialize=[], ordered=True) model.StageCost = Param(model.Stages, mutable=True) # DEPRECATED model.StageCostVariable = Param(model.Stages, mutable=True) # it is often the case that a subset of the stage variables are strictly "derived" # variables, in that their values are computable once the values of other variables # in that stage are known. it generally useful to know which variables these are, # as it is unnecessary to post non-anticipativity constraints for these variables. # further, attempting to force non-anticipativity - either implicitly or explicitly - # on these variables can cause issues with decomposition algorithms. model.StageDerivedVariables = Set(model.Stages, initialize=[], ordered=True) model.NodeDerivedVariables = Set(model.Nodes, initialize=[], ordered=True) # scenario data can be populated in one of two ways. the first is "scenario-based", # in which a single .dat file contains all of the data for each scenario. the .dat # file prefix must correspond to the scenario name. the second is "node-based", # in which a single .dat file contains only the data for each node in the scenario # tree. the node-based method is more compact, but the scenario-based method is # often more natural when parameter data is generated via simulation. the default # is scenario-based. model.ScenarioBasedData = Param(within=Boolean, default=True, mutable=True) # do we bundle, and if so, how? model.Bundling = Param(within=Boolean, default=False, mutable=True) # bundle names model.Bundles = Set(ordered=True) model.BundleScenarios = Set(model.Bundles, ordered=True) return model
def create_abstract_model(self): """Build the model <b>object</b>.""" if not self.built: def jk_init(m): return [(j, k) for j in m.J for k in m.K[j]] model = AbstractModel() model.J = Set() model.K = Set(model.J) model.JK = Set(initialize=jk_init, dimen=None) model.y_pred = Param() model.epsilon = Param() model.max_cost = Var() model.w = Param(model.J) model.a = Param(model.JK) model.c = Param(model.JK) model.u = Var(model.JK, within=Binary) # Make sure only one action is on at a time. def c1Rule(m, j): return sum([m.u[j, k] for k in m.K[j]]) == 1 # 2.b: Action sets must flip the prediction of a linear classifier. def c2Rule(m): return sum((m.u[j, k] * m.a[j, k] * m.w[j]) for j, k in m.JK) >= -m.y_pred # instantiate max cost def maxcost_rule(m, j, k): return m.max_cost >= (m.u[j, k] * m.c[j, k]) # Set up objective for total sum. def obj_rule_percentile(m): return sum(m.u[j, k] * m.c[j, k] for j, k in m.JK) # Set up objective for max cost. def obj_rule_max(m): return sum(m.epsilon * m.u[j, k] * m.c[j, k] for j, k in m.JK) + (1 - m.epsilon) * m.max_cost ## set up objective function. if self.mip_cost_type == "max": model.g = Objective(rule=obj_rule_max, sense=minimize) model.c3 = Constraint(model.JK, rule=maxcost_rule) else: model.g = Objective(rule=obj_rule_percentile, sense=minimize) ## model.c1 = Constraint(model.J, rule=c1Rule) model.c2 = Constraint(rule=c2Rule) self.model = model self.built = True return self.model
""" Created on Thu Dec 19 16:12:21 2019 @author: Silvia """ from __future__ import division from pyomo.opt import SolverFactory from pyomo.core import AbstractModel from pyomo.dataportal.DataPortal import DataPortal from pyomo.environ import * import pandas as pd from datetime import datetime ############ Create abstract model ########### model = AbstractModel() data = DataPortal() # ####################Define sets##################### #Name of all the nodes (primary and secondary substations) model.N = Set() data.load(filename='nodes.csv', set=model.N) #first row is not read #Allowed connections model.links = Set(dimen=2) #in the csv the values must be delimited by commas data.load(filename='links.csv', set=model.links) #Nodes are divided into two sets, as suggested in https://pyomo.readthedocs.io/en/stable/pyomo_modeling_components/Sets.html: # NodesOut[nodes] gives for each node all nodes that are connected to it via outgoing links # NodesIn[nodes] gives for each node all nodes that are connected to it via ingoing links
def _create_using(self, model, **kwds): """ Tranform a model to its Lagrangian dual. """ # Optional naming schemes for dual variables and constraints constraint_suffix = kwds.pop("dual_constraint_suffix", "_constraint") variable_prefix = kwds.pop("dual_variable_prefix", "p_") # Optional naming schemes to pass to StandardForm sf_kwds = {} sf_kwds["slack_names"] = kwds.pop("slack_names", "auxiliary_slack") sf_kwds["excess_names"] = kwds.pop("excess_names", "auxiliary_excess") sf_kwds["lb_names"] = kwds.pop("lb_names", "_lower_bound") sf_kwds["ub_names"] = kwds.pop("ub_names", "_upper_bound") sf_kwds["pos_suffix"] = kwds.pop("pos_suffix", "_plus") sf_kwds["neg_suffix"] = kwds.pop("neg_suffix", "_minus") # Get the standard form model sf_transform = StandardForm() sf = sf_transform(model, **sf_kwds) # Roughly, parse the objectives and constraints to form A, b, and c of # # min c'x # s.t. Ax = b # x >= 0 # # and create a new model from them. # We use sparse matrix representations # {constraint_name: {variable_name: coefficient}} A = _sparse(lambda: _sparse(0)) # {constraint_name: coefficient} b = _sparse(0) # {variable_name: coefficient} c = _sparse(0) # Walk constaints for (con_name, con_array) in sf.component_map(Constraint, active=True).items(): for con in (con_array[ndx] for ndx in con_array._index): # The qualified constraint name cname = "%s%s" % (variable_prefix, con.local_name) # Process the body of the constraint body_terms = process_canonical_repn( generate_standard_repn(con.body)) # Add a numeric constant to the 'b' vector, if present b[cname] -= body_terms.pop(None, 0) # Add variable coefficients to the 'A' matrix row = _sparse(0) for (vname, coef) in body_terms.items(): row["%s%s" % (vname, constraint_suffix)] += coef # Process the upper bound of the constraint. We rely on # StandardForm to produce equality constraints, thus # requiring us only to check the lower bounds. lower_terms = process_canonical_repn( generate_standard_repn(con.lower)) # Add a numeric constant to the 'b' matrix, if present b[cname] += lower_terms.pop(None, 0) # Add any variables to the 'A' matrix, if present for (vname, coef) in lower_terms.items(): row["%s%s" % (vname, constraint_suffix)] -= coef A[cname] = row # Walk objectives. Multiply all coefficients by the objective's 'sense' # to convert maximizing objectives to minimizing ones. for (obj_name, obj_array) in sf.component_map(Objective, active=True).items(): for obj in (obj_array[ndx] for ndx in obj_array._index): # The qualified objective name # Process the objective terms = process_canonical_repn(generate_standard_repn( obj.expr)) # Add coefficients for (name, coef) in terms.items(): c["%s%s" % (name, constraint_suffix)] += coef * obj_array.sense # Form the dual dual = AbstractModel() # Make constraint index set constraint_set_init = [] for (var_name, var_array) in sf.component_map(Var, active=True).items(): for var in (var_array[ndx] for ndx in var_array._index): constraint_set_init.append("%s%s" % (var.local_name, constraint_suffix)) # Make variable index set variable_set_init = [] dual_variable_roots = [] for (con_name, con_array) in sf.component_map(Constraint, active=True).items(): for con in (con_array[ndx] for ndx in con_array._index): dual_variable_roots.append(con.local_name) variable_set_init.append("%s%s" % (variable_prefix, con.local_name)) # Create the dual Set and Var objects dual.var_set = Set(initialize=variable_set_init) dual.con_set = Set(initialize=constraint_set_init) dual.vars = Var(dual.var_set) # Make the dual constraints def constraintRule(A, c, ndx, model): return sum(A[v][ndx] * model.vars[v] for v in model.var_set) <= \ c[ndx] dual.cons = Constraint(dual.con_set, rule=partial(constraintRule, A, c)) # Make the dual objective (maximizing) def objectiveRule(b, model): return sum(b[v] * model.vars[v] for v in model.var_set) dual.obj = Objective(rule=partial(objectiveRule, b), sense=maximize) return dual.create()