I am trying to understand the pulp library in python, specifically in defining constraints that don't involve lpSum and are just straight bounds on one of the variables.
orders_dict_test = {"Orders": [11, 22, 33, 44, 55, 66, 77],
"Num_Days": [1, 2, 2, 3, 3, 5, 6]}
How would one create a pulp constraint using "Num_Days", where we only want to include "Orders" where "Num_Days" is >= 1 and <= 4?
I am just having a hard time understanding constraints that doesn't involve a lpSum as many of the examples I have seen are focused on that.
Any resources or help would be great.
Thank you.
I don't fully understand the question, or what you are trying to achieve. As a general point though when creating constraints there tend to be two things you can do with 'sets' of variables (a set is just a collection of variables which you could define in any way you like).
You can either sum over that set of variables, or you can apply a constraint for each of the variables in that set.
So for your example it would be quite straightforward to create the subset of variables which satisfy orders_dict_test['Num_Days'] >= 1 and <= 4
, and you could then sum over that subset or apply a constraint for each variables in that subset.
The code below will create a list of tuples of ('orders', 'ndays') which satisfy that constraint. Extracting the actual variables will depend on how you have defined them. I would suggest that you probably want a unique key of some description.
orders_dict_test = {"Orders": [11, 22, 33, 44, 55, 66, 77],
"Num_Days": [1, 2, 2, 3, 3, 5, 6]}
print([(o, nd) for o, nd in zip(orders_dict_test['Orders'], orders_dict_test['Num_Days']) if nd >= 1 and nd<= 4])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.