PYOMO: How to use abstract models with internal data

痞子三分冷 提交于 2020-12-06 12:23:12

问题


Hei all,

I am trying to set up an abstract model for a very simple QP of the form

min (x-x0)^2

s.t.

A x = b

C x <= d

I would like to use an abstract model, as I need to resolve with changing parameters (mainly x0, but potentially also A, b, C, d). I am right now struggeling with simply setting the parameters in the model instance. I do not want to use an external data file, but rather internal python variables. All examples I find online use AMPL formatted data files.

This is the code I have right now

import pyomo.environ as pe

model = pe.AbstractModel()

# the sets
model.n = pe.Param(within=pe.NonNegativeIntegers)
model.m = pe.Param(initialize = 1)
model.ss = pe.RangeSet(1, model.n)
model.os = pe.RangeSet(1, model.m)

# the starting point and the constraint parameters
model.x_hat = pe.Param(model.ss)
model.A = pe.Param(model.os, model.ss)
model.b = pe.Param(model.os)
model.C = pe.Param(model.os, model.os)
model.d = pe.Param(model.ss, model.os)

# the decision variables
model.x_projected = pe.Var(model.ss)

# the cosntraints
# A x = b
def sum_of_elements_rule(model):
    value = model.A * model.x_projected
    return value == model.d
model.sumelem = pe.Constraint(model.os, rule=sum_of_elements_rule)

# C x <= d
def positivity_constraint(model):
    return model.C*model.x_projected <= model.d

model.bounds = pe.Constraint(model.ss, rule=positivity_constraint)

# the cost
def cost_rule(model):
    return sum((model.x_projected[i] - model.x[i])**2 for i in model.ss)
model.cost = pe.Objective(rule=cost_rule)

instance = model.create_instance()

And somehow here I am stuck. How do I set the parameters now?

Thanks and best, Theo


回答1:


I know this is an old post but a solution to this could have helped me so here is the solution to this problem:

## TEST
data_init= {None: dict(
        n = {None : 3},
        d = {0:0, 1:1, 2:2},
        x_hat = {0:10, 1:-1, 2:-100},
        b = {None: 10}
        )}
# create instance
instance = model.create_instance(data_init)

This creates the instance in an equivalent way than what you did but in a more formal way.




回答2:


Ok, I seemed to have figured out what the problem is. If I want to set a parameter after I create an instance, I need the

mutable=True 

flag. Then, I can set the parameter with something like

for i in range(model_dimension):
    getattr(instance, 'd')[i] = i

The model dimension I need to choose before i create an instance (which is ok for my case). The instance can be reused with different parameters for the constraints. The code below should work for the problem

min (x-x_hat)' * (x-x_hat)
s.t.
sum(x) = b
x[i] >= d[i]

with x_hat, b, d as parameters.

import pyomo.environ as pe
model = pe.AbstractModel()

# model dimension
model.n = pe.Param(default=2)

# state space set
model.ss = pe.RangeSet(0, model.n-1)

# equality
model.b = pe.Param(default=5, mutable=True)

# inequality
model.d = pe.Param(model.ss, default=0.0, mutable=True)

# decision var
model.x = pe.Var(model.ss)
model.x_hat = pe.Param(model.ss, default=0.0, mutable=True)

# the cost
def cost_rule(model):
    return sum((model.x[i] - model.x_hat[i])**2 for i in model.ss)
model.cost = pe.Objective(rule=cost_rule)

# CONSTRAINTS
# each x_i bigger than d_i
def lb_rule(model, i):
    return (model.x[i] >= model.d[i])
model.state_bound = pe.Constraint(model.ss, rule=lb_rule)

# sum of x == P_tot
def sum_rule(model):
    return (sum(model.x[i] for i in model.ss) == model.b)
model.state_sum = pe.Constraint(rule=sum_rule)

## TEST
# define model dimension
model_dimension = 3
model.n = model_dimension

# create instance
instance = model.create_instance()
# set d
for i in range(model_dimension):
    getattr(instance, 'd')[i] = i

# set x_hat
xh = (10,1,-100)
for i in range(model_dimension):
    getattr(instance, 'x_hat')[i] = xh[i]

# set b
instance.b = 10

# solve
solver = pe.SolverFactory('ipopt')
result = solver.solve(instance)

instance.display()


来源:https://stackoverflow.com/questions/48184893/pyomo-how-to-use-abstract-models-with-internal-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!