#!/bin/bash
set -u
cd /home/dtourolle/Development/scene-actor-extraction
mkdir -p experiments/trajectories experiments/results
MODELS=(arcface_w600k_r50 arcface_r18 arcface_w600k_mbf LVFace-B_Glint360K)
MODES=(full restricted); EXPAND=(1 0)
for model in "${MODELS[@]}"; do
  for mode in "${MODES[@]}"; do
    for exp in "${EXPAND[@]}"; do
      xtag=$([ "$exp" = 1 ] && echo exp || echo noexp); tag="${model}_${mode}_${xtag}"
      best="experiments/results/rep4_best_${tag}.json"; traj="experiments/trajectories/rep4_${tag}.jsonl"
      [ -f "$best" ] && { echo "skip $tag"; continue; }
      : > "$traj"; echo "=== $tag START $(date '+%H:%M') ==="
      SAE_EXPAND=$exp REPLAY_WORKERS=4 DE_WORKERS=2 python3 scripts/optimizer/optimize.py \
        --manifest "experiments/manifests/rep4_${model}_${mode}.json" \
        --gallery "experiments/galleries/gallery_${model}.h5" \
        --params prob_threshold:0.5:0.999 anneal_sec:1:60 extinction_sec:1:60 \
        --popsize 10 --maxiter 15 --seed 0 \
        --trajectory "$traj" --out "experiments/results/opt_rep4_${tag}.json" \
        > "experiments/results/rep4_de_${tag}.log" 2>&1
      python3 -c "import json;r=[json.loads(l) for l in open('$traj')];b=max(r,key=lambda x:x['f1']);json.dump({'best':b,'n_evals':len(r)},open('$best','w'))" 2>/dev/null
      echo "done $tag $(date '+%H:%M'): $(python3 -c "import json;d=json.load(open('$best'));print('F1=%.1f%% misID=%d'%(d['best']['f1']*100,d['best'].get('FPI_misid',-1)))" 2>/dev/null)"
    done
  done
done
echo "=== REP4 MATRIX COMPLETE $(date '+%F %H:%M') ==="
