Constraint modeling
回答済みHello!
I have 4 variables in a list of objects i.e.,oysters_list.
My aim is to select just 1 binary variable of each year and face from the Main Objects created in the list and apply the constraint c1.
E.g. var_North_2020 == 1 and var_East_2021 == 1 (out of the 4 main objects in the oysters_list).
I'm not sure if adding the constraint 'c1' as I did below, fulfills my requirement. Please suggest on it.
Regards, Mohammed.
m = Model("sample")
class Sample:
def __init__(self, face: str, year:int, rate: float):
self.face = face
self.year = year
self.variable = m.addVar(0, 1, 0, GRB.BINARY, f'var_{face}_{year}', None)
self.rate = rate
class Oyster(Sample):
def __init__(self, face: str, year: int):
rate: float = 4 if face == 'North' else 8
super().__init__(face, year, rate)
face_list = ['North', 'East']
year_list = [2020, 2021]
oysters_list = [Oyster(i, j) for i in face_list for j in year_list]
m.addConstr(quicksum(i.variable for i in oysters_list)==1, 'c1')-
正式なコメント
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Mohammed,
Yes, the constraint \(\texttt{c1}\), as implemented, insures exactly one binary variable is assigned to one.
Best regards,
Maliheh
0 -
Hello Maliheh,
Thank you for the response.
I understand that constraint is applied to exactly one binary variable. But my concern is that I want to apply it to one of each type i.e., I have 2 North's and 2 East's binary variables. I wish to select 1 North and 1 East at random and apply the constraint to both of them. Is it possible to do so with 'c1'? If not, how do I go about it?
Regards,
Mohammed.
0 -
Hi Mohammed,
I am not sure I truly understand what you are trying to do. Given your example, do you want to ensure the following:
oyster("North", 2020).variable + oyster("North", 2021).variable = 1, and
oyster("East", 2020).variable + oyster("East", 2021).variable = 1The first constraint assigns exactly one of the \(\texttt{North}\) variables to 1 and the second constraint assigns exactly one of the \(\texttt{East}\) variables to 1.
If so, you can implement them as below:
North_expr, East_expr = 0, 0
for oyster in oyster_list:
if oyster.face == "North":
North_expr += oyster.varaible
elif oyster.face == "East":
East_expr += oyster.variable
m.addConstr(North_expr == 1, "c_north")
m.addConstr(East_expr == 1, "c_east")Of course, you can implement the constraints more compactly using list comprehension if you are willing to iterate through the same list twice. If this is not what you are thinking to implement, could you please write the constraint expressions that you would like to implement using the oyster variable notation?Best regards,Maliheh0 -
Hello Maliheh,
You understood it correctly. That's what I'm trying to achieve.
m.addConstr(quicksum(i.variable for i in oysters_list if i.face == 'North') == 1, 'oyster_north_const')
As you said, the above line filters with only the 'North' objects' variables. Similarly, it can give me 'East' objects' binary variables.
I have further questions after doing so:
1. The filtered variables have to further go through a few equations, two of which are as follows:
space = m.addVar(0, inf, 0, GRB.CONTINUOUS, 'space', None)
m.addConstr(space == quicksum(i.variable * i.area for i in oysters_list), 'space')
m.setObjective(i.price * i.variable, GRB.MINIMIZE)In the 'space' constraint I mentioned oysters_list which I feel is incorrect. My question is how do I make sure the variables processed in these equations are the ones which I filtered previously based on the 'face' attribute i.e. North and East. Do I filter them first in a list and then pass the 'space' constraint to that list?
2. Since I filtered North and East separately(separate lines), do I have to apply the above 'space' constraint to them separately? If I apply them separately, how do I know which binary variable is selected for my objective function, as it is mandatory to select one North and one East in the objective as well?
P.S. 'i.area' & 'i.price' are other attributes for oysters_list objects which I missed mentioning earlier in the class.
Regards, Mohammed!
0 -
Hi Mohammed,
It seems to me that you want to define the following constraints where space_north and space_east are decision variables.
space_north = oyster("North", 2020).variable * oyster("North", 2020).area +
oyster("North", 2021).variable * oyster("North", 2021).area
space_east = oyster("East", 2020).variable * oyster("East", 2020).area +
oyster("East", 2021).variable * oyster("East", 2021).areaYou can then implement it as bellow:
space = m.addVars(["North", "East"], name="space")
LP.addConstrs(
(
space[face]
== gp.quicksum(
oyster.variable * oyster.area
for oyster in oysters_list
if oyster.face == face
)
for face in ["North", "East"]
),
name="space_c",
)If I apply them separately, how do I know which binary variable is selected for my objective function, as it is mandatory to select one North and one East in the objective as well?
After solving the optimization problem, you can query the values assigned to decision variables using the code snippet below and you can then see which binary variable is assigned to 1.
model.getAttr("X", model.getVars())Best regards,
Maliheh
0 -
Hello Maliheh,
Thank you for answering my queries. I'll try these and see how it goes.
Best regards,
Mohammed.0
投稿コメントは受け付けていません。
コメント
7件のコメント