问题
I am trying to solve a MIP problem. I am trying to find the number of exams to be done by each tech on a date for a week by minimizing the total time used to finish demand.
I have demand, time taken by each tech, list of techs etc. in separate dataframes.
I am able to find the number of exams to minimize the objective, however I wish to add a constraint of maximum number of techs to be used as 8.
I added some binary variables to add the condition, however I am not able to relate it with the objective function.
Below is my code so far:
model = pulp.LpProblem("Time minimizing problem", pulp.LpMinimize)
capacity = pulp.LpVariable.dicts("capacity",
((examdate , techname, region) for examdate, techname, region in tech_data_new.index),
lowBound=0,
cat='Integer')
for examdate, techname,region in tech_data_new.index:
var = capacity[(examdate,techname,region)]
var.upBound = tech_data_new.loc[(examdate,techname,region), 'Max Capacity']
model += pulp.lpSum(capacity[examdate,techname, region] * tech_data_new.loc[(examdate,techname, region), 'Time taken'] for examdate,techname, region in tech_data_new.index)
for date in demand_data.index.get_level_values('Exam Date').unique():
for i in demand_data.loc[date].index.tolist():
model += pulp.lpSum([capacity[examdate,techname,region] for examdate, techname, region in tech_data_new.index
if (date == examdate and i == region)]) == demand_data.loc[(demand_data.index.get_level_values('Exam Date') == date) & (demand_data.index.get_level_values('Body Region') == i), shiftname].item()
And these are the binary variables, I tried adding but couldn't relate with the objective as multiplication will make it non-linear.
techs = pulp.LpVariable.dicts("techs",
(techname for techname in tech_data_new.index.get_level_values('Technologist Name').unique()),
cat='Binary')
days = pulp.LpVariable.dicts("day",
(examdate for examdate in tech_data_new.index.get_level_values('Exam Date').unique()),
cat='Binary')
Any lead would be appreciated. Thanks in advance. Please do let me know if any other support is needed.
回答1:
If you want to set a constraint for each examdate
, and the integer variables capacity
which have indexes (examdate, techname, region)
represent the number of exams each tech does on each date in each region, then I'd create a set of binary variables tech_used
which are indexed (examdate, techname)
which represent whether reach tech is used on each day.
After defining these vars you need to set constraints so that they behave as required... assuming lists examdates, technames, regions
have been appropriately declared:
for examdate in examdates:
for techname in technames:
model += Lp.sum([capacity[examdate, techname, region] for region in regions]) < max_capacity*tech_used(examdate, techname)
Note that in the above max_capacity
should be the capacity for each tech and these constraints can replace the max capacity constraints you've put elsewhere.
Then you just need to set your limit on No. Of techs to at most 8:
for examdate in examdates:
model += Lp.sum([tech_used[examdate, techname] for techname in technames]) <= 8
来源:https://stackoverflow.com/questions/53596196/relating-constraints-with-objective-in-pulp