-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_ga.sh
More file actions
executable file
·297 lines (262 loc) · 10.5 KB
/
Copy pathrun_ga.sh
File metadata and controls
executable file
·297 lines (262 loc) · 10.5 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
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
#!/usr/bin/env bash
# =============================================================================
# run_ga.sh — Launch a full GA optimisation run with N headless Go workers
# =============================================================================
#
# Usage
# -----
# chmod +x run_ga.sh
# ./run_ga.sh --scene simopt-scene.json [OPTIONS]
# ./run_ga.sh --heightmap terrain.png [OPTIONS] # legacy
#
# Options
# -n, --workers <N> Number of simultaneous workers (default: 20)
# -p, --population <N> GA population size (default: 20)
# -g, --generations <N> Number of GA generations (default: 50)
# -s, --scene <path> Path to simopt-scene.json from deVision (preferred)
# -r, --road-network <path> Path to road-network.json from deVision (optional)
# -m, --heightmap <path> Path to heightmap PNG (legacy, use --scene instead)
# --replay Run replay after GA to record vehicle paths
# -h, --help Show this help and exit
#
# What it does
# ------------
# 1. Builds the orchestrator and worker binaries if not present.
# 2. Starts the Go orchestrator in the background.
# 3. Launches N workers in --fast (headless) mode, each logging to logs/.
# 4. Waits for all workers to finish (they exit when the GA is complete).
# 5. If --replay is set, runs one final simulation to record vehicle paths.
# 6. Cleans up the orchestrator on exit (including Ctrl-C).
#
# Output (written to ga_results/)
# --------------------------------
# best_schedule.json — Best spawn schedule found by the GA
# best_vehicle_paths.json — Per-vehicle movement paths (--replay only)
# generation_history.json — Per-generation best/avg/worst fitness
# orchestrator.log — Full orchestrator log
#
# Worker logs are written to logs/worker_<NN>.log
# =============================================================================
set -euo pipefail
# ---------------------------------------------------------------------------
# Defaults
# ---------------------------------------------------------------------------
WORKER_COUNT=20
POPULATION=20
GENERATIONS=50
HEIGHTMAP=""
SCENE=""
ROAD_NETWORK=""
RUN_REPLAY=false
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PARENT_DIR="$(dirname "${SCRIPT_DIR}")"
LOG_DIR="${SCRIPT_DIR}/logs"
ORCH_LOG="${LOG_DIR}/orchestrator_stdout.log"
ORCH_ADDR="127.0.0.1:7050"
# Auto-discover the deVision scratch-project assets directory
DEVISION_ASSETS=""
_scratch=$(find "${PARENT_DIR}/deVision/.devision-dev" -maxdepth 1 -name "kit-scratch-*" -type d 2>/dev/null | head -1)
if [[ -n "$_scratch" && -d "${_scratch}/assets" ]]; then
DEVISION_ASSETS="${_scratch}/assets"
fi
# ---------------------------------------------------------------------------
# Argument parsing
# ---------------------------------------------------------------------------
print_help() {
sed -n '/^# Usage/,/^# ====*/p' "$0" | sed 's/^# \?//'
exit 0
}
while [[ $# -gt 0 ]]; do
case "$1" in
-n|--workers) WORKER_COUNT="$2"; shift 2 ;;
-p|--population) POPULATION="$2"; shift 2 ;;
-g|--generations) GENERATIONS="$2"; shift 2 ;;
-s|--scene) SCENE="$2"; shift 2 ;;
-r|--road-network) ROAD_NETWORK="$2"; shift 2 ;;
-m|--heightmap) HEIGHTMAP="$2"; shift 2 ;;
--replay) RUN_REPLAY=true; shift ;;
-h|--help) print_help ;;
*) echo "[ERROR] Unknown option: $1"; print_help ;;
esac
done
if [[ -z "$SCENE" && -z "$HEIGHTMAP" ]]; then
echo "[ERROR] Either --scene or --heightmap is required."
echo " Usage: $0 --scene <simopt-scene.json> [OPTIONS]"
exit 1
fi
# Resolve relative paths against the deVision assets directory
resolve_path() {
local p="$1"
if [[ -z "$p" ]]; then echo ""; return; fi
if [[ "$p" = /* ]]; then echo "$p"; return; fi
if [[ -n "$DEVISION_ASSETS" ]]; then echo "${DEVISION_ASSETS}/${p}"; return; fi
echo "$p"
}
SCENE="$(resolve_path "$SCENE")"
HEIGHTMAP="$(resolve_path "$HEIGHTMAP")"
ROAD_NETWORK="$(resolve_path "$ROAD_NETWORK")"
if [[ -n "$DEVISION_ASSETS" ]]; then
echo "[assets] deVision assets: ${DEVISION_ASSETS}"
else
echo "[assets] deVision assets dir not found — using paths as-is"
fi
if [[ -n "$SCENE" && ! -f "$SCENE" ]]; then
echo "[ERROR] Scene file not found: ${SCENE}"
exit 1
fi
if [[ -n "$HEIGHTMAP" && ! -f "$HEIGHTMAP" ]]; then
echo "[ERROR] Heightmap not found: ${HEIGHTMAP}"
exit 1
fi
if [[ -n "$ROAD_NETWORK" && ! -f "$ROAD_NETWORK" ]]; then
echo "[ERROR] Road network file not found: ${ROAD_NETWORK}"
exit 1
fi
# Build worker scene/heightmap flag string
if [[ -n "$SCENE" ]]; then
WORKER_SCENE_FLAGS="--simopt-scene ${SCENE}"
else
WORKER_SCENE_FLAGS="--heightmap ${HEIGHTMAP}"
fi
if [[ -n "$ROAD_NETWORK" ]]; then
WORKER_SCENE_FLAGS="${WORKER_SCENE_FLAGS} --road-network ${ROAD_NETWORK}"
fi
# ---------------------------------------------------------------------------
# Build binaries if needed
# ---------------------------------------------------------------------------
ORCH_BIN="${SCRIPT_DIR}/orchestrator"
WORKER_BIN="${SCRIPT_DIR}/worker"
echo "[build] Building binaries..."
cd "${SCRIPT_DIR}"
go build -o orchestrator ./cmd/orchestrator
go build -o worker ./cmd/worker
echo "[build] Binaries ready."
# ---------------------------------------------------------------------------
# Setup
# ---------------------------------------------------------------------------
mkdir -p "${LOG_DIR}"
mkdir -p "${SCRIPT_DIR}/ga_results"
echo "============================================================"
echo " GA Run"
echo " Workers : ${WORKER_COUNT}"
echo " Population : ${POPULATION}"
echo " Generations : ${GENERATIONS}"
if [[ -n "$SCENE" ]]; then
echo " Scene : ${SCENE}"
else
echo " Heightmap : ${HEIGHTMAP}"
fi
[[ -n "$ROAD_NETWORK" ]] && echo " Road network : ${ROAD_NETWORK}"
echo " Replay : ${RUN_REPLAY}"
echo " Log dir : ${LOG_DIR}"
echo " Results dir : ${SCRIPT_DIR}/ga_results"
echo "============================================================"
# ---------------------------------------------------------------------------
# Process management
# ---------------------------------------------------------------------------
WORKER_PIDS=()
ORCH_PID=""
cleanup() {
echo ""
echo "[cleanup] Stopping all processes..."
if [[ -n "$ORCH_PID" ]] && kill -0 "$ORCH_PID" 2>/dev/null; then
kill "$ORCH_PID" 2>/dev/null && echo "[cleanup] Killed orchestrator PID ${ORCH_PID}"
fi
for pid in "${WORKER_PIDS[@]}"; do
if kill -0 "$pid" 2>/dev/null; then
kill "$pid" 2>/dev/null && echo "[cleanup] Killed worker PID ${pid}"
fi
done
local remaining
remaining=$(jobs -p 2>/dev/null || true)
if [[ -n "$remaining" ]]; then
echo "$remaining" | xargs -r kill 2>/dev/null || true
fi
echo "[cleanup] Done."
}
trap cleanup EXIT INT TERM
# ---------------------------------------------------------------------------
# Start orchestrator
# ---------------------------------------------------------------------------
echo "[start] Launching orchestrator..."
"${ORCH_BIN}" \
-addr "${ORCH_ADDR}" \
-population "${POPULATION}" \
-generations "${GENERATIONS}" \
> "${ORCH_LOG}" 2>&1 &
ORCH_PID=$!
echo "[start] Orchestrator PID: ${ORCH_PID} (log: ${ORCH_LOG})"
# Stream orchestrator log to terminal with prefix
tail -f "${ORCH_LOG}" | awk '{print "[ORCH] " $0; fflush()}' &
# Wait for orchestrator to become ready (up to 30s)
echo "[start] Waiting for orchestrator to be ready..."
MAX_WAIT=30
WAITED=0
until curl -sf "http://${ORCH_ADDR}/" > /dev/null 2>&1; do
sleep 0.1
WAITED=$((WAITED + 1))
if [[ $WAITED -ge $MAX_WAIT ]]; then
echo "[ERROR] Orchestrator did not start within ${MAX_WAIT}s."
echo " Check ${ORCH_LOG} for details."
exit 1
fi
done
echo "[start] Orchestrator ready after ${WAITED}s."
# ---------------------------------------------------------------------------
# Launch workers
# ---------------------------------------------------------------------------
echo "[start] Launching ${WORKER_COUNT} workers..."
for i in $(seq 1 "${WORKER_COUNT}"); do
WORKER_LOG="${LOG_DIR}/worker_$(printf '%02d' "$i").log"
# shellcheck disable=SC2086
"${WORKER_BIN}" \
${WORKER_SCENE_FLAGS} \
--fast \
> "${WORKER_LOG}" 2>&1 &
WORKER_PIDS+=($!)
echo "[start] Worker ${i} PID: ${WORKER_PIDS[-1]} (log: ${WORKER_LOG})"
done
echo "[start] All ${WORKER_COUNT} workers launched."
echo ""
echo "Follow progress:"
echo " tail -f ${ORCH_LOG}"
echo " tail -f ${LOG_DIR}/worker_01.log"
echo ""
# ---------------------------------------------------------------------------
# Wait for all workers to finish
# Workers exit cleanly when the orchestrator sends "end" (all generations done)
# ---------------------------------------------------------------------------
echo "[wait] Waiting for all workers to finish..."
for pid in "${WORKER_PIDS[@]}"; do
wait "$pid" 2>/dev/null || true
done
if [[ "$RUN_REPLAY" == true ]]; then
if [[ -z "$SCENE" ]]; then
echo "[replay] Skipped — --replay requires --scene (heightmap has no coordinate transform)."
else
echo ""
echo "[replay] Recording vehicle paths from best schedule..."
# shellcheck disable=SC2086
"${WORKER_BIN}" \
${WORKER_SCENE_FLAGS} \
--replay \
--schedule "${SCRIPT_DIR}/ga_results/best_schedule.json" \
--output "${SCRIPT_DIR}/ga_results/best_vehicle_paths.json"
echo "[replay] Done — ga_results/best_vehicle_paths.json written."
if [[ -n "$DEVISION_ASSETS" ]]; then
cp "${SCRIPT_DIR}/ga_results/best_vehicle_paths.json" "${DEVISION_ASSETS}/best_vehicle_paths.json"
cp "${SCRIPT_DIR}/ga_results/best_schedule.json" "${DEVISION_ASSETS}/best_schedule.json"
echo "[replay] Copied → ${DEVISION_ASSETS}/best_vehicle_paths.json"
echo "[replay] Copied → ${DEVISION_ASSETS}/best_schedule.json"
fi
fi
fi
echo ""
echo "============================================================"
echo " GA optimisation complete!"
echo " Best schedule : ${SCRIPT_DIR}/ga_results/best_schedule.json"
[[ "$RUN_REPLAY" == true && -n "$SCENE" ]] && \
echo " Vehicle paths : ${SCRIPT_DIR}/ga_results/best_vehicle_paths.json"
echo " History : ${SCRIPT_DIR}/ga_results/generation_history.json"
echo " Full log : ${ORCH_LOG}"
echo "============================================================"