Ejemplo n.º 1
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))
Ejemplo 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.variable_dict(
            (i, pmo.variable()) for i in 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.)
    def __init__(
        self,
        interval_set,
        params: dict,
    ):
        super().__init__()

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

        # Need to reference interval set in coupling method, either need to add it to class or pass it to function.
        self.interval_set = interval_set

        self.component_element_ids = params[
            "component_element_names"]  # resources or other collections

        ## Parameters
        self.import_limit = aml.parameter(
            params["import_limit"])  # assume positive
        self.export_limit = aml.parameter(params["export_limit"])

        ## Expressions/Variables
        # Should we define net_export as a variable and set equal later - allows to define upper and lower bounds here
        # or should it be an expression here, and we reconstruct constraints later whenever we add elements - i.e. the coupling aspect of the problem
        # It really depends on how the model is "constructed" - one option is to define everything as constructor functions that are called when the model is constructed, but order of construction matters here!
        # If we define as a variable, then we decouple them.
        self.net_export = aml.variable_dict()
        for interval in interval_set:
            self.net_export[interval] = aml.variable(lb=-self.import_limit,
                                                     ub=self.export_limit)
Ejemplo n.º 4
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.)
Ejemplo n.º 5
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))
Ejemplo n.º 6
0
    def _generate_model(self):
        self.model = pmo.block()
        model = self.model
        model._name = self.description

        model.a = pmo.parameter(value=1.0)
        model.x = pmo.variable(domain=NonNegativeReals)
        model.y = pmo.variable(domain=Binary)

        model.obj = pmo.objective(model.x**2 + 3.0*model.y**2)
        model.c1 = pmo.constraint(model.a <= model.y)
        model.c2 = pmo.constraint((2.0, model.x/model.a - model.y, 10))
Ejemplo n.º 7
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)
Ejemplo n.º 8
0
    def _generate_model(self):
        self.model = pmo.block()
        model = self.model
        model._name = self.description

        model.a = pmo.parameter(value=1.0)
        model.x = pmo.variable(domain=NonNegativeReals)
        model.y = pmo.variable(domain=Binary)

        model.obj = pmo.objective(model.x + 3.0*model.y)
        model.c1 = pmo.constraint(model.a <= model.y)
        model.c2 = pmo.constraint((2.0, model.x/model.a - model.y, 10))
Ejemplo n.º 9
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([])
Ejemplo n.º 10
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)
Ejemplo n.º 11
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])
Ejemplo n.º 12
0
    def _generate_model(self):
        self.model = None
        self.model = pmo.block()
        model = self.model
        model._name = self.description

        model.a = pmo.parameter(value=1.0)
        model.x = pmo.variable(domain=NonNegativeReals)
        model.y = pmo.variable(domain=NonNegativeReals)

        model.inactive_obj = pmo.objective(model.y)
        model.inactive_obj.deactivate()
        model.obj = pmo.objective(model.x**2 + 3.0 * model.inactive_obj**2 +
                                  1.0)
        model.c1 = pmo.constraint(model.a <= model.y)
        model.c2 = pmo.constraint(2.0 <= model.x / model.a - model.y <= 10)
Ejemplo n.º 13
0
    def _generate_model(self):
        self.model = pmo.block()
        model = self.model
        model._name = self.description

        model.b = pmo.block()
        model.B = pmo.block_dict((i, pmo.block()) for i in range(1, 4))
        model.a = pmo.parameter(value=1.0)
        model.b.x = pmo.variable(lb=0)
        model.B[1].x = pmo.variable(lb=0)

        model.obj = pmo.objective(expr=model.b.x + 3.0 * model.B[1].x)
        model.obj.deactivate()
        model.B[2].c = pmo.constraint(expr=-model.B[1].x <= -model.a)
        model.B[2].obj = pmo.objective(expr=model.b.x + 3.0 * model.B[1].x + 2)
        model.B[3].c = pmo.constraint(
            expr=2.0 <= model.b.x / model.a - model.B[1].x <= 10)
Ejemplo n.º 14
0
    def _generate_model(self):
        self.model = pmo.block()
        model = self.model
        model._name = self.description

        model.b = pmo.block()
        model.B = pmo.block_dict((i, pmo.block())
                                 for i in range(1,4))
        model.a = pmo.parameter(value=1.0)
        model.b.x = pmo.variable(lb=0)
        model.B[1].x = pmo.variable(lb=0)

        model.obj = pmo.objective(expr=model.b.x + 3.0*model.B[1].x)
        model.obj.deactivate()
        model.B[2].c = pmo.constraint(expr=-model.B[1].x <= -model.a)
        model.B[2].obj = pmo.objective(expr=model.b.x + 3.0*model.B[1].x + 2)
        model.B[3].c = pmo.constraint(expr=(2.0, model.b.x/model.a - model.B[1].x, 10))
Ejemplo n.º 15
0
    def _generate_model(self):
        self.model = pmo.block()
        model = self.model
        model._name = self.description

        model.a = pmo.parameter(value=0.1)
        model.x = pmo.variable(domain=NonNegativeReals)
        model.y = pmo.variable_dict()
        model.y[1] = pmo.variable(domain=NonNegativeReals)
        model.y[2] = pmo.variable(domain=NonNegativeReals)

        model.obj = pmo.objective(model.x + model.y[1]+2*model.y[2])
        model.c1 = pmo.constraint(model.a <= model.y[2])
        model.c2 = pmo.constraint((2.0, model.x, 10.0))
        model.c3 = pmo.sos1(model.y.values())
        model.c4 = pmo.constraint(sum(model.y.values()) == 1)

        # Make an empty SOS constraint
        model.c5 = pmo.sos1([])
Ejemplo n.º 16
0
    def _generate_model(self):
        self.model = pmo.block()
        model = self.model
        model._name = self.description

        model.a = pmo.parameter(value=0.1)
        model.x = pmo.variable(domain=NonNegativeReals)
        model.y = pmo.variable_dict()
        model.y[1] = pmo.variable(domain=NonNegativeReals)
        model.y[2] = pmo.variable(domain=NonNegativeReals)

        model.obj = pmo.objective(model.x + model.y[1] + 2 * model.y[2])
        model.c1 = pmo.constraint(model.a <= model.y[2])
        model.c2 = pmo.constraint(2.0 <= model.x <= 10.0)
        model.c3 = pmo.sos1(model.y.values())
        model.c4 = pmo.constraint(sum(model.y.values()) == 1)

        # Make an empty SOS constraint
        model.c5 = pmo.sos1([])
Ejemplo n.º 17
0
# re-validate the function after changing inputs
# (will raise PiecewiseValidationError when validation fails)
p.validate()

# evaluate the function
assert p(1) == 1
assert p(1.5) == 1.5
assert p(2) == 2
assert p(2.5) == 1.5
assert p(3) == 1
assert p(2.5) == 1.5
assert p(4) == 2

breakpoints = [
    pmo.parameter(1),
    pmo.parameter(2),
    pmo.parameter(3),
    pmo.parameter(None)
]
values = [
    pmo.parameter(1),
    pmo.parameter(2),
    pmo.parameter(1),
    pmo.parameter(None)
]
p = pmo.piecewise(breakpoints,
                  values,
                  input=x,
                  output=y,
                  repn='sos2',
Ejemplo n.º 18
0
p.output.expr = q

# re-validate the function after changing inputs
# (will raise PiecewiseValidationError when validation fails)
p.validate()

# evaluate the function
assert p(1) == 1
assert p(1.5) == 1.5
assert p(2) == 2
assert p(2.5) == 1.5
assert p(3) == 1
assert p(2.5) == 1.5
assert p(4) == 2

breakpoints = [pmo.parameter(1),
               pmo.parameter(2),
               pmo.parameter(3),
               pmo.parameter(None)]
values = [pmo.parameter(1),
          pmo.parameter(2),
          pmo.parameter(1),
          pmo.parameter(None)]
p = pmo.piecewise(breakpoints,
                  values,
                  input=x,
                  output=y,
                  repn='sos2',
                  bound='eq',
                  validate=False)
Ejemplo n.º 19
0
NUM_WORKERS = len(costs)
NUM_TASKS = len(costs[0])

# Define the model
model = pmo.block()

# Create the sets
model.set_workers = range(NUM_WORKERS)
model.set_tasks = range(NUM_TASKS)

# Create the parameters
model.param_cost = pmo.parameter_dict()
for _worker in model.set_workers:
    for _task in model.set_tasks:
        model.param_cost[(_worker,
                          _task)] = pmo.parameter(costs[_worker][_task])

# Create the variables
model.var_x = pmo.variable_dict()
for _worker in model.set_workers:
    for _task in model.set_tasks:
        model.var_x[(_worker, _task)] = pmo.variable(domain=pmo.Binary)

# Create the constraints
## Each worker is assigned to at most 1 task
model.con_worker = pmo.constraint_list()
for _worker in model.set_workers:
    model.con_worker.append(
        pmo.constraint(
            sum(model.var_x[(_worker, _task)]
                for _task in model.set_tasks) <= 1))
    7, 0, 30, 22, 80, 94, 11, 81, 70, 64, 59, 18, 0, 36, 3, 8, 15, 42, 9, 0,
    42, 47, 52, 32, 26, 48, 55, 6, 29, 84, 2, 4, 18, 56, 7, 29, 93, 44, 71,
    3, 86, 66, 31, 65, 0, 79, 20, 65, 52, 13]

capacities = 850

model = pmo.block()

# Create the sets
model.set_labels = range(len(values))

# Create the parameters
model.param_label_value = pmo.parameter_dict()
model.param_label_weight = pmo.parameter_dict()
for i in model.set_labels:
    model.param_label_value[i] = pmo.parameter(values[i])
    model.param_label_weight[i] = pmo.parameter(weights[i])

# Create the variables
model.var_pack = pmo.variable_dict()
for i in model.set_labels:
    model.var_pack[i] = pmo.variable(domain=pmo.Binary)

# Create the constraints
model.con_capacity = pmo.constraint(sum(model.var_pack[i] * weights[i] for i in model.set_labels) <= capacities)

# Create the objective
model.obj = pmo.objective(sum(model.var_pack[i] * values[i] for i in model.set_labels), sense=pmo.maximize)

# Solve
opt = pmo.SolverFactory('cplex')
Ejemplo n.º 21
0
import pyomo.kernel as pmo

#
# Blocks
#

# define a simple optimization model
b = pmo.block()
b.x = pmo.variable()
b.c = pmo.constraint(expr=b.x >= 1)
b.o = pmo.objective(expr=b.x)

# define an optimization model with indexed containers
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))
Ejemplo n.º 22
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)
p.output.expr = q

# re-validate the function after changing inputs
# (will raise PiecewiseValidationError when validation fails)
p.validate()

# evaluate the function
assert p(1) == 1
assert p(1.5) == 1.5
assert p(2) == 2
assert p(2.5) == 1.5
assert p(3) == 1
assert p(2.5) == 1.5
assert p(4) == 2

breakpoints = [pmo.parameter(1),
               pmo.parameter(2),
               pmo.parameter(3),
               pmo.parameter(None)]
values = [pmo.parameter(1),
          pmo.parameter(2),
          pmo.parameter(1),
          pmo.parameter(None)]
p = pmo.piecewise(breakpoints,
                  values,
                  input=x,
                  output=y,
                  repn='sos2',
                  bound='eq',
                  validate=False)
Ejemplo n.º 24
0
import pyomo.kernel as pmo

#
# Mutable parameters
#

p = pmo.parameter()
assert p.value == None

p = pmo.parameter(value=2)
assert p.value == 2

p.value = 4
assert p.value == 4
assert pmo.value(p**2) == 16
assert pmo.value(p - 1) == 3

v = pmo.variable()
c = pmo.constraint(p-1 <= v <= p+1)
assert pmo.value(c.lb) == 3
assert pmo.value(c.ub) == 5

p.value = -1
assert pmo.value(c.lb) == -2
assert pmo.value(c.ub) == 0
    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)
Ejemplo n.º 26
0
    def test_call(self):

        g = PiecewiseLinearFunction([1], [0])
        f = TransformedPiecewiseLinearFunction(
            g, require_bounded_input_variable=False)
        self.assertTrue(f.parent is None)
        self.assertEqual(f.ctype, IBlock)
        self.assertEqual(f(1), 0)
        self.assertIs(type(f(1)), float)
        with self.assertRaises(ValueError):
            f(0.9)
        with self.assertRaises(ValueError):
            f(1.1)

        g = PiecewiseLinearFunction([1, 2], [0, 4])
        f = TransformedPiecewiseLinearFunction(
            g, require_bounded_input_variable=False)
        self.assertTrue(f.parent is None)
        self.assertEqual(f.ctype, IBlock)
        self.assertEqual(f(1), 0)
        self.assertIs(type(f(1)), float)
        self.assertEqual(f(1.5), 2)
        self.assertIs(type(f(1.5)), float)
        self.assertEqual(f(2), 4)
        self.assertIs(type(f(2)), float)
        with self.assertRaises(ValueError):
            f(0.9)
        with self.assertRaises(ValueError):
            f(2.1)

        # step function
        g = PiecewiseLinearFunction([1, 1], [0, 1])
        f = TransformedPiecewiseLinearFunction(
            g, require_bounded_input_variable=False)
        self.assertTrue(f.parent is None)
        self.assertEqual(f.ctype, IBlock)
        self.assertEqual(f(1), 0)
        self.assertIs(type(f(1)), float)
        with self.assertRaises(ValueError):
            f(0.9)
        with self.assertRaises(ValueError):
            f(1.1)

        g = PiecewiseLinearFunction([1, 2, 3], [1, 2, 1])
        f = TransformedPiecewiseLinearFunction(
            g, require_bounded_input_variable=False)
        self.assertTrue(f.parent is None)
        self.assertEqual(f.ctype, IBlock)
        self.assertEqual(f(1), 1)
        self.assertIs(type(f(1)), float)
        self.assertEqual(f(1.5), 1.5)
        self.assertIs(type(f(1.5)), float)
        self.assertEqual(f(2), 2)
        self.assertIs(type(f(2)), float)
        self.assertEqual(f(2.5), 1.5)
        self.assertIs(type(f(2.5)), float)
        self.assertEqual(f(3), 1)
        self.assertIs(type(f(3)), float)
        with self.assertRaises(ValueError):
            f(0.9)
        with self.assertRaises(ValueError):
            f(3.1)

        # step function
        g = PiecewiseLinearFunction([1, 2, 2, 3], [1, 2, 3, 4])
        f = TransformedPiecewiseLinearFunction(
            g, require_bounded_input_variable=False)
        self.assertTrue(f.parent is None)
        self.assertEqual(f.ctype, IBlock)
        self.assertEqual(f(1), 1)
        self.assertIs(type(f(1)), float)
        self.assertEqual(f(1.5), 1.5)
        self.assertIs(type(f(1.5)), float)
        self.assertEqual(f(2), 2)  # lower semicontinuous
        self.assertIs(type(f(2)), float)
        self.assertEqual(f(2.5), 3.5)
        self.assertIs(type(f(2.5)), float)
        self.assertEqual(f(3), 4)
        self.assertIs(type(f(3)), float)
        with self.assertRaises(ValueError):
            f(0.9)
        with self.assertRaises(ValueError):
            f(3.1)

        # another step function
        g = PiecewiseLinearFunction([1, 1, 2, 3], [1, 2, 3, 4],
                                    equal_slopes_tolerance=-1)
        f = TransformedPiecewiseLinearFunction(
            g, require_bounded_input_variable=False, equal_slopes_tolerance=-1)
        self.assertTrue(f.parent is None)
        self.assertEqual(f.ctype, IBlock)
        self.assertEqual(f(1), 1)
        self.assertEqual(f(1.5), 2.5)
        self.assertEqual(f(2), 3)  # lower semicontinuous
        self.assertEqual(f(2.5), 3.5)
        self.assertEqual(f(3), 4)
        with self.assertRaises(ValueError):
            f(0.9)
        with self.assertRaises(ValueError):
            f(3.1)

        # another step function
        g = PiecewiseLinearFunction([1, 2, 3, 3], [1, 2, 3, 4],
                                    equal_slopes_tolerance=-1)
        f = TransformedPiecewiseLinearFunction(
            g, require_bounded_input_variable=False, equal_slopes_tolerance=-1)
        self.assertTrue(f.parent is None)
        self.assertEqual(f.ctype, IBlock)
        self.assertEqual(f(1), 1)
        self.assertEqual(f(1.5), 1.5)
        self.assertEqual(f(2), 2)
        self.assertEqual(f(2.5), 2.5)
        self.assertEqual(f(3), 3)  # lower semicontinuous
        with self.assertRaises(ValueError):
            f(0.9)
        with self.assertRaises(ValueError):
            f(3.1)

        # another step function using parameters
        g = PiecewiseLinearFunction([
            pmo.parameter(1),
            pmo.parameter(1),
            pmo.parameter(2),
            pmo.parameter(3)
        ], [
            pmo.parameter(1),
            pmo.parameter(2),
            pmo.parameter(3),
            pmo.parameter(4)
        ],
                                    equal_slopes_tolerance=-1)
        f = TransformedPiecewiseLinearFunction(
            g, require_bounded_input_variable=False, equal_slopes_tolerance=-1)
        self.assertTrue(f.parent is None)
        self.assertEqual(f.ctype, IBlock)
        self.assertEqual(f(1), 1)
        self.assertEqual(f(1.5), 2.5)
        self.assertEqual(f(2), 3)  # lower semicontinuous
        self.assertEqual(f(2.5), 3.5)
        self.assertEqual(f(3), 4)
        with self.assertRaises(ValueError):
            f(0.9)
        with self.assertRaises(ValueError):
            f(3.1)

        # another step function
        g = PiecewiseLinearFunction([1, 1, 2, 3, 4], [1, 2, 3, 4, 5],
                                    equal_slopes_tolerance=-1)
        f = TransformedPiecewiseLinearFunction(
            g, require_bounded_input_variable=False, equal_slopes_tolerance=-1)
        self.assertTrue(f.parent is None)
        self.assertEqual(f.ctype, IBlock)
        self.assertEqual(f(1), 1)  # lower semicontinuous
        self.assertEqual(f(1.5), 2.5)
        self.assertEqual(f(2), 3)
        self.assertEqual(f(2.5), 3.5)
        self.assertEqual(f(3), 4)
        self.assertEqual(f(3.5), 4.5)
        self.assertEqual(f(4), 5)
        with self.assertRaises(ValueError):
            f(0.9)
        with self.assertRaises(ValueError):
            f(4.1)

        # another step function
        g = PiecewiseLinearFunction([1, 2, 2, 3, 4], [1, 2, 3, 4, 5],
                                    equal_slopes_tolerance=-1)
        f = TransformedPiecewiseLinearFunction(
            g, require_bounded_input_variable=False, equal_slopes_tolerance=-1)
        self.assertTrue(f.parent is None)
        self.assertEqual(f.ctype, IBlock)
        self.assertEqual(f(1), 1)
        self.assertEqual(f(1.5), 1.5)
        self.assertEqual(f(2), 2)  # lower semicontinuous
        self.assertEqual(f(2.5), 3.5)
        self.assertEqual(f(3), 4)
        self.assertEqual(f(3.5), 4.5)
        self.assertEqual(f(4), 5)
        with self.assertRaises(ValueError):
            f(0.9)
        with self.assertRaises(ValueError):
            f(4.1)

        # another step function
        g = PiecewiseLinearFunction([1, 2, 3, 3, 4], [1, 2, 3, 4, 5],
                                    equal_slopes_tolerance=-1)
        f = TransformedPiecewiseLinearFunction(
            g, require_bounded_input_variable=False, equal_slopes_tolerance=-1)
        self.assertTrue(f.parent is None)
        self.assertEqual(f.ctype, IBlock)
        self.assertEqual(f(1), 1)
        self.assertEqual(f(1.5), 1.5)
        self.assertEqual(f(2), 2)
        self.assertEqual(f(2.5), 2.5)
        self.assertEqual(f(3), 3)  # lower semicontinuous
        self.assertEqual(f(3.5), 4.5)
        self.assertEqual(f(4), 5)
        with self.assertRaises(ValueError):
            f(0.9)
        with self.assertRaises(ValueError):
            f(4.1)

        # another step function
        g = PiecewiseLinearFunction([1, 2, 3, 4, 4], [1, 2, 3, 4, 5],
                                    equal_slopes_tolerance=-1)
        f = TransformedPiecewiseLinearFunction(
            g, require_bounded_input_variable=False, equal_slopes_tolerance=-1)
        self.assertTrue(f.parent is None)
        self.assertEqual(f.ctype, IBlock)
        self.assertEqual(f(1), 1)
        self.assertEqual(f(1.5), 1.5)
        self.assertEqual(f(2), 2)
        self.assertEqual(f(2.5), 2.5)
        self.assertEqual(f(3), 3)
        self.assertEqual(f(3.5), 3.5)
        self.assertEqual(f(4), 4)  # lower semicontinuous
        with self.assertRaises(ValueError):
            f(0.9)
        with self.assertRaises(ValueError):
            f(4.1)

        # another step function
        g = PiecewiseLinearFunction([1, 2, 3, 4, 5], [1, 2, 3, 4, 5],
                                    equal_slopes_tolerance=-1)
        f = TransformedPiecewiseLinearFunction(
            g, require_bounded_input_variable=False, equal_slopes_tolerance=-1)
        self.assertTrue(f.parent is None)
        self.assertEqual(f.ctype, IBlock)
        self.assertEqual(f(1), 1)
        self.assertEqual(f(1.5), 1.5)
        self.assertEqual(f(2), 2)
        self.assertEqual(f(2.5), 2.5)
        self.assertEqual(f(3), 3)
        self.assertEqual(f(3.5), 3.5)
        self.assertEqual(f(4), 4)
        self.assertEqual(f(4.5), 4.5)
        self.assertEqual(f(5), 5)
        with self.assertRaises(ValueError):
            f(0.9)
        with self.assertRaises(ValueError):
            f(5.1)
assert s.weights == (1, 2, 3)
assert len(s.variables) == 3
assert v1 in s
assert v2 in s
assert v3 in s

#
# Specifying weights
#

# using known values
s = pmo.sos([v1, v2], weights=[1.2, 2.5])
assert s.weights == (1.2, 2.5)

# using paramters
p = pmo.parameter_list(pmo.parameter() for i in range(2))
s = pmo.sos([v1, v2], weights=[p[0]**2, p[1]**2])
assert len(s.weights) == 2
p[0].value = 1
p[1].value = 2
assert tuple(pmo.value(w) for w in s.weights) == (1, 4)

# using data expressions
d = pmo.expression_list(pmo.data_expression() for i in range(2))
s = pmo.sos([v1, v2], weights=d)
assert len(s.weights) == 2
d[0].expr = p[0] + 1
d[1].expr = p[0] + p[1]
assert tuple(pmo.value(w) for w in s.weights) == (2, 3)

#
Ejemplo n.º 28
0
# @ConcreteModels
m = pmo.block()
m.b = pmo.block()
# @ConcreteModels

# @Sets_1
m.s = [1, 2]

# @Sets_1
# @Sets_2
# [0,1,2]
m.q = range(3)
# @Sets_2

# @Parameters_single
m.p = pmo.parameter(0)

# @Parameters_single
# @Parameters_dict
# pd[1] = 0, pd[2] = 1
m.pd = pmo.parameter_dict()
for k, i in enumerate(m.s):
    m.pd[i] = pmo.parameter(k)

# @Parameters_dict
# @Parameters_list
# uses 0-based indexing
# pl[0] = 0, pl[0] = 1, ...
m.pl = pmo.parameter_list()
for j in m.q:
    m.pl.append(pmo.parameter(j))
Ejemplo n.º 29
0
assert len(s.variables) == 3
assert v1 in s
assert v2 in s
assert v3 in s

#
# Specifying weights
#

# using known values
s = pmo.sos([v1,v2], weights=[1.2,2.5])
assert s.weights == (1.2,2.5)

# using paramters
p = pmo.parameter_list(
    pmo.parameter() for i in range(2))
s = pmo.sos([v1,v2], weights=[p[0]**2, p[1]**2])
assert len(s.weights) == 2
p[0].value = 1
p[1].value = 2
assert tuple(pmo.value(w) for w in s.weights) == (1, 4)

# using data expressions
d = pmo.expression_list(
    pmo.data_expression() for i in range(2))
s = pmo.sos([v1,v2], weights=d)
assert len(s.weights) == 2
d[0].expr = p[0] + 1
d[1].expr = p[0] + p[1]
assert tuple(pmo.value(w) for w in s.weights) == (2, 3)
Ejemplo n.º 30
0


# @Sets_1
m.s = [1,2]

# @Sets_1
# @Sets_2
# [0,1,2]
m.q = range(3)
# @Sets_2



# @Parameters_single
m.p = pmo.parameter(0)

# @Parameters_single
# @Parameters_dict
# pd[1] = 0, pd[2] = 1
m.pd = pmo.parameter_dict()
for k,i in enumerate(m.s):
    m.pd[i] = pmo.parameter(k)


# @Parameters_dict
# @Parameters_list
# uses 0-based indexing
# pl[0] = 0, pl[0] = 1, ...
m.pl = pmo.parameter_list()
for j in m.q: