-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.py
More file actions
36 lines (29 loc) · 1.49 KB
/
solver.py
File metadata and controls
36 lines (29 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from pulp import LpProblem, LpVariable, LpMinimize, LpMaximize, lpSum, value
def solve_lp(objective_dict, constraints_list, minimize=True):
"""Solve LP problem given an objective and constraints."""
# Choose minimize or maximize
sense = LpMinimize if minimize else LpMaximize
lp_prob = LpProblem("LP_Problem", sense)
# Create variables with non-negativity
variables = {name: LpVariable(name, lowBound=0) for name in objective_dict}
# Objective function
lp_prob += lpSum(coef * variables[var] for var, coef in objective_dict.items()), "Objective"
# Constraints
for c in constraints_list:
lhs = lpSum(c["coefficients"].get(var, 0) * variables[var] for var in variables)
# case for different types of inequalities
if c["ineq"] == "≤" or c["ineq"] == "<=":
lp_prob += lhs <= c["rhs"], c["name"]
elif c["ineq"] == "≥" or c["ineq"] == ">=":
lhs_expr = lpSum(c["coefficients"].get(var, 0) * variables[var] for var in variables)
lp_prob += lhs >= c["rhs"], c["name"]
elif c["ineq"] == "=" or c["ineq"] == "==":
lp_prob += lhs == c["rhs"], c["name"]
status = lp_prob.solve()
if status != 1:
raise Exception("No optimal solution found")
results = {} # store results in a dictionary
results["Z"] = round(value(lp_prob.objective), 2)
for v in lp_prob.variables():
results[v.name] = round(v.varValue, 2)
return results, list(variables.keys())