LinearInteger_Programming / rd_projects.py
zpetrea's picture
Update rd_projects.py
74ef7ff verified
Raw
History Blame Contribute Delete
1.82 kB
from pyomo.environ import *
def solve_rd_projects():
model = ConcreteModel()
projects = list(range(1, 8)) # Projects 1 through 7
NPV = {
1: 600000, 2: 580000, 3: 550000, 4: 400000,
5: 650000, 6: 725000, 7: 340000
}
Eng = {
1: 9, 2: 4, 3: 7, 4: 12,
5: 8, 6: 10, 7: 8
}
Cost = {
1: 196000, 2: 400000, 3: 70000, 4: 180000,
5: 225000, 6: 200000, 7: 130000
}
model.y = Var(projects, domain=Binary)
# Maximize NPV
model.objective = Objective(
expr=sum(NPV[i] * model.y[i] for i in projects),
sense=maximize
)
# Budget constraint
model.budget = Constraint(expr=sum(Cost[i] * model.y[i] for i in projects) <= 1300000)
# Engineer constraint
model.engineers = Constraint(expr=sum(Eng[i] * model.y[i] for i in projects) <= 35)
# Project dependencies
model.max_one_of_12 = Constraint(expr=model.y[1] + model.y[2] <= 1)
model.project4_depends_on_2 = Constraint(expr=model.y[4] <= model.y[2])
solver = SolverFactory("cbc")
result = solver.solve(model)
if result.solver.termination_condition != TerminationCondition.optimal:
return "❌ Solver failed to find an optimal solution."
selected_projects = [i for i in projects if model.y[i]() >= 0.5]
total_npv = sum(NPV[i] for i in selected_projects)
total_cost = sum(Cost[i] for i in selected_projects)
total_eng = sum(Eng[i] for i in selected_projects)
summary = (
"βœ… **Optimal Project Selection Found**\n\n"
f"πŸ“Œ **Selected Projects:** {selected_projects}\n"
f"πŸ’° **Total Cost:** ${total_cost:,.0f} / $1,300,000\n"
f"πŸ‘· **Engineers Used:** {total_eng} / 35\n"
f"πŸ’Ή **Total Net Present Value (NPV):** ${total_npv:,.0f}"
)
return summary