The constraint value of a variable must be an element of the specified list.A simple example is shown as follow,in which the value of thex.prod(flow_Restrictions[0])
expression can only be equal to an element in [0, 3, 5]
,and the value of thex.prod(flow_Restrictions[1])
expression can only be equal to an element in [0, 1, 3, 5]
and so on.
m = Model()
# Adding variables
flow_Restrictions = [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0],
[1.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 1.0, 0.0], [1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0],
[1.0, 1.0, 1.0, 1.0], [1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0], [1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0],
[1.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0]]
flow_c = [1.0, 1.0, 1.0, 1.0]
component_capacity = [[0, 3, 5], [0, 1, 3, 5], [0, 3, 5], [0, 2, 5], [0, 1, 2, 3, 5],[0, 3, 5], [0, 2, 3, 5],
[0, 1, 3, 5], [0, 3, 5], [0, 3, 4, 5], [0, 2, 5], [0, 2, 5], [0, 3, 5, 10, 15], [0, 5, 10],
[0, 3, 5], [0, 1, 3, 5], [0, 3, 5], [0, 1, 2, 3, 5], [0, 2, 5], [0, 3, 5, 10], [0, 3, 5],
[0, 3, 10, 15]]
d = 15
x = m.addVars(4, lb=0, ub=15, vtype=GRB.INTEGER)
for i in range(len(flow_Restrictions) + 1):
if i == len(flow_Restrictions):
m.addConstr(x.prod(flow_c) == d, name="")
else:
m.addConstr(x.prod(flow_Restrictions[i]) <= component_capacity[i], name="")
m.setObjective(x.prod(flow_c), sense=GRB.MAXIMIZE)
m.update()
The solution I want to obtain is shown below:
[[5, 5, 0, 5], [5, 0, 5, 5]]
and these solutions satisfy the following conditions: The elements of the list flow_Restrictions multiplied by the elements of the solution whose values must belong to the elements of the list component_capacity.
[[5, 5, 0, 5, 5, 5, 5, 5, 0, 5, 5, 5, 15, 5, 5, 0, 5, 5, 5, 5, 5, 15], [5, 0, 5, 5, 5, 5, 0, 5, 5, 5, 5, 5, 15, 5, 0, 5, 5, 5, 5, 5, 5, 15]]
Each element of the solution belongs to the element corresponding to the list named component_capacity. How do I achieve this?
flow_restrictions
andcomponent_capacity
occur multiple times. Is this intended to be? Assuming I understand you correctly, you want to model the constraint $$ \sum_{i=i}^{4} x_i f_{ij} \in C_j \quad \forall j, $$ where $f_{ij}$ corresponds toflow_restrictions[i][j]
and $C_j$ to the python listcomponent_capacity[j]
. IMHO, this is rather a modeling question than a programming one. $\endgroup$