I am finding it hard to convert an Excel Solver model into python pulp syntax.
In my model, I am optimizing the HC and OT variables for each department, with an objective of minimizing the sum of the OT variables. The constraints require that the HC variables sum to no more than 92 and that the total production (=E2*C2*D2 + F2*C2
in the spreadsheet below) meets a per-department requirement (the "Input" column of the excel spreadsheet below). The Excel solver formulation shown below works perfectly.
Problem
- How can I write my objective function in pulp (in Excel F7 =SUM(F2:F6))?
- Constraints E7 <= 92
- Constraints G2:G6 >= B2:B6
- I have two decision variable HC and OT. In the below python code I only created one variable.
Before
After Solver
import pulp
import numpy as np
import pandas as pd
idx = [0, 1, 2, 3, 4]
d = {'Dept': pd.Series(['Receiving', 'Picking', 'PPicking', 'QC', 'Packing'], index=idx),
'Target': pd.Series([61,94,32,63,116], index=idx),
'Hrs/day': pd.Series([7.75, 7.75, 7.75, 7.75, 7.75], index=idx),
'Prod': pd.Series([11733, 13011, 2715, 13682, 14194], index=idx),
'HC': pd.Series([24,18,6,28,16], index=idx),
'OT': pd.Series([0,0,42,0,0], index=idx)}
df = pd.DataFrame(d)
# Create variables and model
x = pulp.LpVariable.dicts("x", df.index, lowBound=0)
mod = pulp.LpProblem("OTReduction", pulp.LpMinimize)
# Objective function
mod += sum(df['OT'])
# Lower and upper bounds:
for idx in df.index:
mod += x[idx] <= df['Input'][idx]
# Total HC value should be less than or equal to 92
mod += sum([x[idx] for idx in df.index]) <= 92
# Solve model
mod.solve()
# Output solution
for idx in df.index:
print idx, x[idx].value()
# Expected answer
# HC, OT
# 19, 35.795
# 18, 0
# 11, 0
# 28, 0
# ----------------
# 92, 35.795 -> **note:** SUM(HC), SUM(OT)