1
$\begingroup$

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?

$\endgroup$
2
  • $\begingroup$ First, note that some entries of your flow_restrictions and component_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 to flow_restrictions[i][j] and $C_j$ to the python list component_capacity[j]. IMHO, this is rather a modeling question than a programming one. $\endgroup$
    – joni
    Commented Jul 7, 2021 at 11:39
  • $\begingroup$ I may not be clear, but I think the modeling is fine.Your formula expresses the constraints that the solution must satisfy. $\endgroup$
    – Zying
    Commented Jul 8, 2021 at 2:12

1 Answer 1

1
$\begingroup$

I have added a more general question and answer that address the modeling aspect, independent of solver: How to linearize membership in a finite set

You can apply this with $S=\{0,3,5\}$, $S=\{0,1,3,5\}$, and so on.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.