File size: 9,627 Bytes
a8ba5ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# Metro Train Scheduling System - DataService API

A comprehensive FastAPI-based service for generating synthetic metro train scheduling data and optimizing daily train operations.

## ๐ŸŽฏ Overview

This system generates realistic metro train schedules for a single-line metro network with:
- **25-40 trainsets** with varying health status
- **25 stations** on a bidirectional route
- **Operating hours**: 5:00 AM - 11:00 PM
- **Real-world constraints**: maintenance, fitness certificates, branding priorities, mileage balancing

## ๐Ÿš‡ Features

### Data Generation
- **Train Health Status**: Fully healthy, partially available, or under maintenance
- **Fitness Certificates**: Rolling stock, signalling, and telecom certificates with expiry tracking
- **Job Cards**: Open maintenance tasks with blocking status
- **Component Health**: IoT-style monitoring of brakes, HVAC, doors, bogies, etc.
- **Branding/Advertising**: Contract tracking with exposure priorities
- **Depot Layout**: Stabling bays, IBL bays, and washing bays

### Schedule Optimization
- **Multi-objective optimization** balancing:
  - Service readiness (35%)
  - Mileage balancing (25%)
  - Branding priority (20%)
  - Operational cost (20%)
- **Constraint satisfaction**: Fitness certificates, maintenance requirements, availability windows
- **Service block generation**: Optimal trip assignments throughout the day
- **Fleet allocation**: Revenue service, standby, maintenance, and cleaning assignments

### API Endpoints

#### Generate Complete Schedule
```bash
POST /api/v1/generate
Content-Type: application/json

{
  "date": "2025-10-25",
  "num_trains": 30,
  "num_stations": 25,
  "route_name": "Aluva-Pettah Line",
  "depot_name": "Muttom_Depot",
  "min_service_trains": 22,
  "min_standby_trains": 3,
  "max_daily_km_per_train": 300,
  "balance_mileage": true,
  "prioritize_branding": true
}
```

#### Quick Schedule Generation
```bash
POST /api/v1/generate/quick?date=2025-10-25&num_trains=30&num_stations=25
```

#### Get Example Schedule
```bash
GET /api/v1/schedule/example
```

#### Get Route Information
```bash
GET /api/v1/route/25
```

#### Get Train Health Status
```bash
GET /api/v1/trains/health/30
```

#### Get Depot Layout
```bash
GET /api/v1/depot/layout
```

## ๐Ÿ“ฆ Installation

### Prerequisites
- Python 3.8+
- pip

### Setup

1. **Clone the repository**
```bash
cd /home/arpbansal/code/sih2025/mlservice
```

2. **Install dependencies**
```bash
pip install -r requirements.txt
```

Requirements include:
- `fastapi>=0.104.1` - Web framework
- `uvicorn[standard]>=0.24.0` - ASGI server
- `pydantic>=2.5.0` - Data validation
- `ortools>=9.14.6206` - Optimization (optional)

## ๐Ÿš€ Usage

### Option 1: Run Demo Script

Test the system without starting the API:

```bash
python demo_schedule.py
```

This will:
- Generate synthetic metro data
- Optimize a daily schedule
- Display comprehensive results
- Save output to `sample_schedule.json`

### Option 2: Start FastAPI Server

```bash
python run_api.py
```

The API will be available at:
- **Base URL**: http://localhost:8000
- **Interactive Docs**: http://localhost:8000/docs
- **Alternative Docs**: http://localhost:8000/redoc

### Option 3: Use uvicorn directly

```bash
uvicorn DataService.api:app --reload --host 0.0.0.0 --port 8000
```

## ๐Ÿ“Š Schedule Output Structure

```json
{
  "schedule_id": "KMRL-2025-10-25-DAWN",
  "generated_at": "2025-10-24T23:45:00+05:30",
  "valid_from": "2025-10-25T05:00:00+05:30",
  "valid_until": "2025-10-25T23:00:00+05:30",
  "depot": "Muttom_Depot",
  
  "trainsets": [
    {
      "trainset_id": "TS-001",
      "status": "REVENUE_SERVICE",
      "priority_rank": 1,
      "assigned_duty": "DUTY-A1",
      "service_blocks": [
        {
          "block_id": "BLK-001",
          "departure_time": "05:30",
          "origin": "Aluva",
          "destination": "Pettah",
          "trip_count": 3,
          "estimated_km": 96
        }
      ],
      "daily_km_allocation": 224,
      "cumulative_km": 145620,
      "fitness_certificates": {
        "rolling_stock": {"valid_until": "2025-11-15", "status": "VALID"},
        "signalling": {"valid_until": "2025-10-30", "status": "VALID"},
        "telecom": {"valid_until": "2025-11-20", "status": "VALID"}
      },
      "job_cards": {"open": 0, "blocking": []},
      "branding": {
        "advertiser": "COCACOLA-2024",
        "contract_hours_remaining": 340,
        "exposure_priority": "HIGH"
      },
      "readiness_score": 0.98,
      "constraints_met": true
    }
  ],
  
  "fleet_summary": {
    "total_trainsets": 30,
    "revenue_service": 22,
    "standby": 4,
    "maintenance": 2,
    "cleaning": 2,
    "availability_percent": 93.3
  },
  
  "optimization_metrics": {
    "mileage_variance_coefficient": 0.042,
    "avg_readiness_score": 0.91,
    "branding_sla_compliance": 1.0,
    "shunting_movements_required": 8,
    "total_planned_km": 5280,
    "fitness_expiry_violations": 0
  },
  
  "conflicts_and_alerts": [...],
  "decision_rationale": {...}
}
```

## ๐Ÿ—๏ธ Architecture

```
mlservice/
โ”œโ”€โ”€ DataService/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ api.py                    # FastAPI application
โ”‚   โ”œโ”€โ”€ metro_models.py           # Pydantic data models
โ”‚   โ”œโ”€โ”€ metro_data_generator.py   # Synthetic data generation
โ”‚   โ”œโ”€โ”€ schedule_optimizer.py     # Schedule optimization logic
โ”‚   โ”œโ”€โ”€ enhanced_generator.py     # (existing)
โ”‚   โ”œโ”€โ”€ synthetic_base.py         # (existing)
โ”‚   โ””โ”€โ”€ synthetic_extend.py       # (existing)
โ”œโ”€โ”€ greedyOptim/                  # Optimization algorithms
โ”œโ”€โ”€ demo_schedule.py              # Demo/test script
โ”œโ”€โ”€ run_api.py                    # API startup script
โ”œโ”€โ”€ requirements.txt
โ””โ”€โ”€ README.md
```

## ๐Ÿ”ง Configuration

### Train Health Categories

- **Fully Healthy** (65%): Available entire operational day
- **Partially Healthy** (20%): Available for limited hours
- **Unavailable** (15%): Not available for service (maintenance/repairs)

### Train Status Types

- `REVENUE_SERVICE`: Active passenger service
- `STANDBY`: Ready for deployment
- `MAINTENANCE`: Under repair/inspection
- `CLEANING`: Washing/interior cleaning
- `OUT_OF_SERVICE`: Long-term unavailable

### Optimization Weights

Default objective weights (configurable):
```python
{
  "service_readiness": 0.35,
  "mileage_balancing": 0.25,
  "branding_priority": 0.20,
  "operational_cost": 0.20
}
```

## ๐Ÿ“ API Examples

### cURL Examples

**Generate schedule:**
```bash
curl -X POST "http://localhost:8000/api/v1/generate" \
  -H "Content-Type: application/json" \
  -d '{
    "date": "2025-10-25",
    "num_trains": 30,
    "num_stations": 25,
    "min_service_trains": 22
  }'
```

**Quick generation:**
```bash
curl "http://localhost:8000/api/v1/generate/quick?date=2025-10-25&num_trains=30"
```

**Health check:**
```bash
curl "http://localhost:8000/health"
```

### Python Client Example

```python
import requests

# Generate schedule
response = requests.post(
    "http://localhost:8000/api/v1/generate",
    json={
        "date": "2025-10-25",
        "num_trains": 30,
        "num_stations": 25,
        "min_service_trains": 22,
        "min_standby_trains": 3
    }
)

schedule = response.json()
print(f"Schedule ID: {schedule['schedule_id']}")
print(f"Trains in service: {schedule['fleet_summary']['revenue_service']}")
```

## ๐Ÿงช Testing

Run the demo script to test all functionality:

```bash
python demo_schedule.py
```

Expected output:
- โœ“ Data generation statistics
- โœ“ Route information
- โœ“ Train health summary
- โœ“ Optimization results
- โœ“ Fleet status breakdown
- โœ“ Sample train details
- โœ“ JSON export

## ๐ŸŽจ Key Concepts

### Service Blocks
Continuous operating periods with specific origin/destination and trip counts. Each block represents a trainset's assignment for part of the day.

### Readiness Score
Computed metric (0.0-1.0) considering:
- Fitness certificate validity
- Open/blocking job cards
- Component health scores
- Days since maintenance

### Mileage Balancing
Distributes daily kilometers to equalize cumulative mileage across the fleet, extending overall fleet life.

### Branding Priority
Trains with active advertising contracts get preferential assignment to maximize exposure (revenue optimization).

## ๐Ÿ” Monitoring & Alerts

The system generates alerts for:
- Certificate expirations (EXPIRING_SOON, EXPIRED)
- Blocking maintenance (job cards preventing service)
- Fitness violations
- Constraint conflicts

## ๐Ÿค Integration

### With Existing Optimizer

The DataService can integrate with existing `greedyOptim` algorithms:

```python
from greedyOptim.scheduler import TrainsetSchedulingOptimizer
from DataService.metro_data_generator import MetroDataGenerator

# Generate synthetic data
generator = MetroDataGenerator(num_trains=30)
# ... use with existing optimizer
```

### As Microservice

Deploy as standalone microservice:
```bash
docker build -t metro-scheduler .
docker run -p 8000:8000 metro-scheduler
```

## ๐Ÿ“ˆ Future Enhancements

- [ ] Real-time schedule adjustments
- [ ] Machine learning for demand prediction
- [ ] Driver/crew scheduling integration
- [ ] Energy consumption optimization
- [ ] Passenger flow simulation
- [ ] Weather impact modeling
- [ ] Multi-line network support

## ๐Ÿ“„ License

[Add your license information]

## ๐Ÿ‘ฅ Contributors

[Add contributor information]

## ๐Ÿ“ž Support

For issues, questions, or contributions:
- GitHub Issues: [repository URL]
- Email: [contact email]

---

**Built for Smart India Hackathon 2025** ๐Ÿ‡ฎ๐Ÿ‡ณ