'Formulating Constraints for an optimization Problem
Good day
I would be very glad for some help.
I am currently writing my master thesis. I have the following mixed integer linear optimization:
Finally, I want to minimize the start time of the last activity (dummy variable, end). Each activity has different modes. The activities in the different modes can take different time.
Now the task is to find a start solution with simple constraints, which does not have to be optimal yet. The parameters and variables are already defined and approved.
My idea would be for now the following constraints:
- for each activity the mode with the shortest time duration is used.
- activity i must be finished before i starts
PROBLEM 1 - 1st constraint:
A list created with the activities each in the mode with the minimum duration and sorted by it:
p_im_min = {i: np.min([p[i,m] for m in M_i[i]]) for i in V}
p_im_min[0] = 0
p_im_min[n+1] = 0
p_sort = list(sorted(p_im_min.items(), key = lambda kv: kv[1]))
p_sort = [(3, 1), (4, 1), (5, 1), (7, 1), (13, 1), (14, 1), (15, 1), (19, 1), (1, 2), (2, 2), (8, 2), (16, 2), (17, 2), (18, 2), (20, 2), (6, 3), (10, 3), (9, 4), (12, 4), (11, 5)]
with (i,m) -> i for the activity and m for the mode.
The variable x is already defined in my code as:
x = mdl.addVars([(i,m)
for i in V_ext
for m in M_i[i]],
vtype = grb.GRB.BINARY)
so =1, if activity i is executed in mode m / = 0, otherwise
Then I tried to add the constraint:
mdl.addConstrs(x[i,m] == 1
for (i,m) in p_sort)
But in doing so, I get the error message "Variable not in model". But i defined x, didn't I?
PROBLEM 2 - 2nd constraint:
The variable y is already defined in my code as:
y = mdl.addVars([(i,j)
for i in V_ext
for j in V_ext
if i != j],
vtype = grb.GRB.BINARY)
so =1, if activity i must be completed before the start of activity j / = 0, otherwise
Admittedly a bit unimaginative I created the list, for the activities (couldn't figure out a better way):
order_act = list[(3,4), (4,5), (5,7), (7, 13), (13,14), (14, 15), (15, 19), (19, 1), (1,2), (2,8), (8,16), (16,17), (17,18), (18,20), (20,6), (6, 10), (10,9), (9,12), (12,11)]
So, for example, (3,4) -> 3 (i) must be finished before 4 (j) starts.
My idea for the constraint would have been the following:
mdl.addConstrs(y[i,j] == 1 for (i,j) in order_act)
Again, I get an error message: types.GenericAlias' object is not iterable. Why is the object not iterable?
Can anyone help me with the problems? Where are my thinking errors? Probably it is totally easy for all of you, but unfortunately I am still a Python beginner, so I'd be really thankful for some help.
Solution 1:[1]
I'm not familiar with the package you are using, but I will allow myself to suggest you PulP, wich is another milp package, that in my opinion is easier to use. Also PulP has options to use specific solvers (Gurobi, Cplex and others). Given that, the code would look like this:
pairs = [(i, m) for i in V_ext for m in M_i[i]]]
x = pulp.LpVariable.dicts('x', pairs, cat='Binary')
In that order, you may want to check if changing the package may give you additional clues, like the need of a linking constraint or alike.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ivan Calderon |
