Arpit-Bansal commited on
Commit
737c68a
·
1 Parent(s): 33c9f7f

adding or-tools interface

Browse files
Files changed (2) hide show
  1. greedyOptim/__init__.py +9 -0
  2. greedyOptim/balance.py +58 -1
greedyOptim/__init__.py CHANGED
@@ -75,3 +75,12 @@ __all__ = [
75
  'ConstraintViolationError',
76
  'ConfigurationError'
77
  ]
 
 
 
 
 
 
 
 
 
 
75
  'ConstraintViolationError',
76
  'ConfigurationError'
77
  ]
78
+
79
+ # Add OR-Tools to exports if available
80
+ if ORTOOLS_AVAILABLE:
81
+ __all__.extend([
82
+ 'optimize_with_ortools',
83
+ 'check_ortools_availability',
84
+ 'CPSATOptimizer',
85
+ 'MIPOptimizer'
86
+ ])
greedyOptim/balance.py CHANGED
@@ -1,2 +1,59 @@
1
- import ortools
 
 
 
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Load balancing for trainset optimization.
3
+ Simple interface for OR-Tools integration.
4
+ """
5
 
6
+ from .scheduler import optimize_trainset_schedule
7
+ from .models import OptimizationConfig
8
+
9
+ # Check for OR-Tools availability
10
+ try:
11
+ from .ortools_optimizers import optimize_with_ortools, check_ortools_availability
12
+ ORTOOLS_AVAILABLE = check_ortools_availability()
13
+ except ImportError:
14
+ ORTOOLS_AVAILABLE = False
15
+ optimize_with_ortools = None
16
+
17
+
18
+ def balance_trainset_load(data, method='cp-sat', time_limit=300):
19
+ """Balance trainset load using optimization methods."""
20
+ if ORTOOLS_AVAILABLE and method in ['cp-sat', 'mip'] and optimize_with_ortools is not None:
21
+ config = OptimizationConfig(required_service_trains=20, min_standby=3)
22
+ return optimize_with_ortools(data, method, config=config, time_limit_seconds=time_limit)
23
+ else:
24
+ print("Using genetic algorithm fallback")
25
+ config = OptimizationConfig(required_service_trains=20, min_standby=3)
26
+ return optimize_trainset_schedule(data, 'ga', config)
27
+
28
+
29
+ def compare_exact_vs_metaheuristic(data):
30
+ """Compare optimization methods."""
31
+ results = {}
32
+
33
+ # Try OR-Tools methods
34
+ if ORTOOLS_AVAILABLE:
35
+ for method in ['cp-sat', 'mip']:
36
+ try:
37
+ results[method] = balance_trainset_load(data, method, 120)
38
+ print(f"✅ {method.upper()} completed")
39
+ except Exception as e:
40
+ print(f"❌ {method.upper()} failed: {e}")
41
+
42
+ # Try metaheuristic methods
43
+ config = OptimizationConfig(generations=50, population_size=30)
44
+ for method in ['ga', 'pso']:
45
+ try:
46
+ results[method] = optimize_trainset_schedule(data, method, config)
47
+ print(f"✅ {method.upper()} completed")
48
+ except Exception as e:
49
+ print(f"❌ {method.upper()} failed: {e}")
50
+
51
+ return results
52
+
53
+
54
+ def get_ortools_status():
55
+ """Get OR-Tools status."""
56
+ return {
57
+ 'available': ORTOOLS_AVAILABLE,
58
+ 'methods': ['cp-sat', 'mip'] if ORTOOLS_AVAILABLE else []
59
+ }