I have the following code, where I am trying to create a 2xN
variable called x
and initialize all columns in the first row to x0_init
, and all columns in the second row to x1_init
. After that I want to add initial and final boundary constraints to each row:
model = pyo.ConcreteModel()
N = 100
num_rows = range(2)
num_cols = range(N)
x0_init = 0.0
x1_init = 0.0
x0_final = 1.0
x1_final = 0.0
# Declaring and initializing 2xN state variable
model.x = pyo.Var(num_rows, num_cols, domain=pyo.Reals, initialize=[x0_init, x1_init])
# Declaring initial boundary constraints
model.initial_boundary_constraint_x0 = pyo.Constraint(expr=model.x[0,0] == x0_init)
model.initial_boundary_constraint_x1 = pyo.Constraint(expr=model.x[1,0] == x1_init)
# Declaring final boundary constraints
model.final_boundary_constraint_x0 = pyo.Constraint(expr=model.x[0,N-1] == x0_final)
model.final_boundary_constraint_x1 = pyo.Constraint(expr=model.x[1,N-1] == x1_final)
The above code of course does not work. However, I was hoping someone would be able to help me achieve the abovementioned goals. Looking through the pyomo documentation, I have unfortunately been unable to find a solution to this problem.
Here's a fix for you....
A couple notes:
import pyomo.environ as pyo
model = pyo.ConcreteModel()
num_rows = 2
num_cols = 10
x0_init = 0.0
x1_init = 2.0
x0_final = 4.0
x1_final = 5.0
# SETS
model.R = pyo.Set(initialize=range(num_rows), doc='row')
model.C = pyo.Set(initialize=range(num_cols), doc='col')
# VARIABLES
# NOTE: INITIALIZING VARIABLES IS ALMOST NEVER NECESSARY. Just
# showing how to do it as it is the same for parameters
# and was requested...
def x_initializer(model, r, c):
if r==0:
return x0_init
return x1_init
# Declaring and initializing 2xN state variable
model.x = pyo.Var(model.R, model.C, domain=pyo.Reals, initialize=x_initializer)
# CONSTRAINTS
# Declaring initial boundary constraints
model.initial_boundary_constraint_x0 = pyo.Constraint(expr=model.x[0, 0] == x0_init)
model.initial_boundary_constraint_x1 = pyo.Constraint(expr=model.x[1, 0] == x1_init)
# Declaring final boundary constraints
model.final_boundary_constraint_x0 = pyo.Constraint(expr=model.x[0, model.C.last()] == x0_final)
model.final_boundary_constraint_x1 = pyo.Constraint(expr=model.x[1, model.C.last()] == x1_final)
model.pprint()