I am trying to add constraints to a maximization problem. Let’s say I have a dataframe like this:
df = pd.DataFrame({'id':[1,2,3,4,5,6,7,8,9,10], 'somethingElse':['a','b','c','d','e','f','g','h','i','j'], 'car_type':['honda','honda','bmw','honda','vw','vw','kia','ferrari','mercedes','honda']
df = df.set_index(['id','somethingElse'])
In this problem, either a row gets picked or not. I have created flags:
flag = LpVariable.dicts('flag', ((id, somethingElse) for id, somethingElse in df.idnex), cat = 'Binary')
But I’m confused how to add constraints for something like “The % of Hondas selected should be 20% (or 18-22%)”. I’ve tried starting with something like:
lpSum([(flag[i[0], i[1])]*df.loc[(i[0], i[1]), 'car_type'] for i in df.index])
but it’s not working. Any help would be appreciated!
In this problem, either a row gets picked or not. I have created flags:
flag = LpVariable.dicts('flag', ((id, somethingElse) for id, somethingElse in df.idnex), cat = 'Binary')
But I’m confused how to add constraints for something like “The % of Hondas selected should be 20% (or 18-22%)”. I’ve tried starting with something like:
lpSum([(flag[i[0], i[1])]*df.loc[(i[0], i[1]), 'car_type'] for i in df.index])
but it’s not working. Any help would be appreciated!
formicaman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.