I couldn't have i+1 value for the last value of the RangeSet, therefore I want to ignore it in the constraint. However, I couldn't do that because i value always comes as default value. So, it does not enter to if statement. How can I solve the problem ?
import pyomo.environ as pyo
from pyomo.opt import SolverFactory
model = pyo.AbstractModel()
number_of_lane=2
number_of_vehicle=2
model.i = pyo.Param(within=pyo.NonNegativeIntegers, default=number_of_lane)
model.j = pyo.Param(within=pyo.NonNegativeIntegers, default=number_of_vehicle)
model.I = pyo.RangeSet(1, model.i)
model.J = pyo.RangeSet(1, model.j)
model.R = pyo.Param(default=0.5) #CAV's reaction (s)
model.D = pyo.Param(default=1.5) #Safety Distance (m)
model.lv = pyo.Param(default=4) #Length of vehicle (m)
model.xr = pyo.Param(model.I, model.J, within=pyo.NonNegativeIntegers, initialize=xr_cons)
model.x = pyo.Var(model.I, model.J, domain=pyo.NonNegativeReals, initialize=(0))
def lane_crossing_constraint_rule(m, i, j): #lane should be 2, vehicles will be the first one which is close to the intersection
m.i.pprint() #There is a problem about taking i and j value
if(m.i.value<number_of_vehicle): #Always comes equal!!!!
return (m.x[i,j]-m.xr[i,j])**2+(m.x[i+1,j]-m.xr[i+1,j])**2>=(m.lv+m.D)
else:
return pyo.Constraint.Skip
# the next line creates one constraint for each member of the set model.I
model.l1Constraint = pyo.Constraint(model.I, model.J, rule=lane_crossing_constraint_rule)
It isn't super clear what you are trying to do with the model setup you've chosen. Why did you select
AbstractModel()? Your example isn't reproducible because it is missing data and you aren't building a model instance. Further, I'm not sure what you are trying to accomplish by initializing your range set size from the value of a parameter?I think you should start with a
ConcreteModel(), get something working, and build out from there, if needed. Build a little bit at a time, print the model and verify it, then add to it.Here is a small example that I think shows some of the things you are trying to do. It is a
ConcreteModelthat builds a range set, a variable, and shows how to make a constraint that is dependent on the value of the index from the setLanesOutput