-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.py
More file actions
65 lines (53 loc) · 2.18 KB
/
Copy pathsolver.py
File metadata and controls
65 lines (53 loc) · 2.18 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from pulp import LpProblem, LpStatus
from typing import Dict, Any
import time
class MILPSolver:
def __init__(self):
self.solution_time = 0
self.status = None
self.objective_value = None
self.variable_values = {}
def solve(self, problem: LpProblem) -> Dict[str, Any]:
"""Solve the MILP problem and return the results."""
start_time = time.time()
# Solve the problem
status = problem.solve()
# Record solution time
self.solution_time = time.time() - start_time
# Get solution status
self.status = LpStatus[status]
# Get objective value
self.objective_value = problem.objective.value()
# Get variable values
for var in problem.variables():
self.variable_values[var.name] = var.value()
return self._generate_report()
def _generate_report(self) -> Dict[str, Any]:
"""Generate a detailed report of the solution."""
return {
'status': self.status,
'objective_value': self.objective_value,
'solution_time': self.solution_time,
'variable_values': self.variable_values,
'is_optimal': self.status == 'Optimal',
'is_infeasible': self.status == 'Infeasible',
'is_unbounded': self.status == 'Unbounded'
}
def print_solution(self):
"""Print a formatted solution report."""
print("\n=== MILP Solution Report ===")
print(f"Status: {self.status}")
print(f"Objective Value: {self.objective_value}")
print(f"Solution Time: {self.solution_time:.2f} seconds")
print("\nVariable Values:")
for var_name, value in self.variable_values.items():
print(f"{var_name} = {value}")
print("\nSolution Analysis:")
if self.status == 'Optimal':
print("✓ Optimal solution found")
elif self.status == 'Infeasible':
print("✗ No feasible solution exists")
elif self.status == 'Unbounded':
print("✗ Problem is unbounded")
else:
print("? Solution status unknown")