File size: 6,584 Bytes
9ce37b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# FairRelay β€” Complete Setup Guide

## Quick Start (30 seconds)

```bash
# Clone
git clone https://github.com/MUTHUKUMARAN-K-1/FairRelay.git
cd FairRelay

# Start Brain (AI Engine)
cd brain
pip install -r requirements.txt
uvicorn app.main:app --reload --port 8000
# β†’ http://localhost:8000/docs (Swagger UI)

# Start Backend (API Gateway) β€” new terminal
cd ops/backend-dm
npm install
echo "BRAIN_URL=http://localhost:8000" > .env
node index.js
# β†’ http://localhost:3000/health

# Start Dashboard β€” new terminal
cd ops/AIsupplychain/aisupply
npm install
echo "VITE_API_URL=http://localhost:3000" > .env
npm run dev
# β†’ http://localhost:5173
```

Open http://localhost:5173 β†’ Navigate to **Fair Dispatch** β†’ Click **Run Fair Allocation**

---

## Architecture

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Dashboard (React + Vite)  :5173        β”‚
β”‚  Pages: FairDispatch, LoadConsolidation β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                 β”‚ axios β†’ /api/*
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  API Gateway (Node.js + Express)  :3000 β”‚
β”‚  Auth, CRUD, Proxy to Brain             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                 β”‚ axios β†’ BRAIN_URL
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  AI Brain (FastAPI + LangGraph)   :8000 β”‚
β”‚  8 Dispatch Agents + 5 Consolidation    β”‚
β”‚  OR-Tools, scikit-learn, XGBoost        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                 β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Database: SQLite (auto) or PostgreSQL  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

---

## Environment Variables

### Brain (`brain/.env`)
```env
# Required: NONE (SQLite auto-created)
# Optional:
DATABASE_URL=postgresql+asyncpg://user:pass@host:5432/fairrelay
GOOGLE_API_KEY=your-gemini-key          # For LLM explanations
GEMINI_MODEL=gemini-2.5-flash           # Model to use
OLA_MAPS_API_KEY=your-ola-key           # For real-time traffic
LANGCHAIN_API_KEY=your-langsmith-key    # For tracing (optional)
APP_ENV=development                     # Shows /docs + /redoc
```

### Backend (`ops/backend-dm/.env`)
```env
BRAIN_URL=http://localhost:8000         # Required
DATABASE_URL=postgresql://...           # For auth/drivers/packages
JWT_SECRET=your-secret                  # For auth tokens
PORT=3000
```

### Dashboard (`ops/AIsupplychain/aisupply/.env`)
```env
VITE_API_URL=http://localhost:3000      # Points to API Gateway
```

---

## What Each Agent Does

### Fair Dispatch Pipeline (8 Agents)

| # | Agent | Algorithm | What it computes |
|---|-------|-----------|-----------------|
| 1 | ML Effort | Deterministic formula + traffic | NΓ—M effort matrix |
| 2 | Route Planner | OR-Tools SCIP/GLOP solver | Optimal 1-to-1 assignment |
| 3 | Fairness Manager | Gini coefficient | ACCEPT or REOPTIMIZE |
| 4 | Route Planner v2 | OR-Tools + penalties | Re-optimized assignment |
| 5 | Driver Liaison | Comfort band logic | Per-driver ACCEPT/COUNTER |
| 6 | Final Resolution | Metric-validated swaps | Resolve counter-proposals |
| 7 | Explainability | Template categorization | Natural language per driver |
| 8 | Gemini LLM | Gemini 2.5 Flash (optional) | Rich multilingual explanations |

### Load Consolidation Pipeline (5 Agents)

| # | Agent | Algorithm | What it computes |
|---|-------|-----------|-----------------|
| 1 | Geo Clustering | KMeans + Silhouette | Group nearby shipments |
| 2 | Time Window | Overlap filter | Split incompatible schedules |
| 3 | Capacity | OR-Tools CP-SAT | Bin-pack into trucks |
| 4 | Scoring | Multi-metric | Confidence per group |
| 5 | RL Insights | Q-Learning | Optimal parameters |

---

## API Endpoints

### Core Allocation
```bash
# Full LangGraph pipeline (8 agents)
POST /api/v1/allocate/langgraph

# Simple allocation (no LangGraph)
POST /api/v1/allocate

# Load consolidation (5 agents)
POST /api/v1/consolidate/optimize

# Compare scenarios
POST /api/v1/consolidate/simulate
```

### Monitoring
```bash
# Health check
GET /health

# SSE agent events (real-time)
GET /agent-events/stream?run_id=<uuid>

# Run history
GET /api/v1/runs
GET /api/v1/runs/<run_id>
```

### Driver Management
```bash
GET /api/v1/drivers
GET /api/v1/drivers/<id>
POST /api/v1/feedback
```

---

## Testing

```bash
cd brain

# Run all tests
pytest tests/ -v

# Run specific test
pytest tests/test_langgraph_workflow.py -v

# Test full workflow manually
python test_workflow.py
```

---

## Deployment

See [DEPLOYMENT.md](./DEPLOYMENT.md) for full production deployment guide with:
- Render (Brain + Backend)
- Vercel (Dashboard + Landing)
- LoRRI integration patterns

---

## Tech Stack

| Component | Technology |
|-----------|-----------|
| AI Engine | Python 3.11, FastAPI, LangGraph, SQLAlchemy |
| Optimization | OR-Tools (SCIP, CP-SAT), scikit-learn, XGBoost |
| Traffic | OLA Maps API (real-time Indian traffic) |
| LLM | Gemini 2.5 Flash (multilingual explanations) |
| API Gateway | Node.js, Express, Prisma, Socket.IO |
| Dashboard | React 19, Vite, TailwindCSS, Axios |
| Mobile | Flutter (driver app) |
| Database | PostgreSQL (prod) / SQLite (dev/demo) |
| Deployment | Render (Docker) + Vercel (static) |

---

## Key Differentiators

1. **Gini Coefficient Fairness** β€” mathematically provable workload equity (0.85 β†’ 0.12)
2. **EV-First Routing** β€” battery feasibility + charging penalty in OR-Tools cost matrix
3. **Night Safety** β€” gender-aware routing after 9 PM (avoid high-crime zones)
4. **Cognitive Load Index** β€” 6-factor driver mental fatigue scoring
5. **Explainable AI** β€” every allocation decision has a natural language explanation
6. **Real-Time Traffic** β€” OLA Maps API integration for Indian road conditions
7. **Self-Improving** β€” Q-Learning bandit auto-tunes fairness thresholds daily
8. **Production TMS Integration** β€” designed for LoRRI (logisticsnow.in) embedding