Implementation for the paper: "Latent Heuristic Search: Continuous Optimization for Automated Algorithm Design".
This repository implements a latent-space pipeline for automated heuristic design:
- Encode heuristic programs into latent vectors (
z). - Train a unified normalizing flow to map
z <-> u(prior space). - Train a ranking predictor in
u-space. - Optimize in continuous
u-space with gradient ascent. - Decode optimized latents back into executable heuristic code and evaluate.
The framework supports multiple combinatorial optimization tasks under task/.
train_unified_flow.py: trains one normalizing flow across all tasks.train_unified_mapper.py: trains the latent-to-soft-prompt mapper.ranking_score_predictor.py: trains task-specificu-space ranking predictor.gradient_search.py: main search loop (gradient ascent in prior space + generation + evaluation).normalizing_flow.py: RealNVP-style flow implementation.mapper.py: mapper architectures (MLPlMapper,LowRankMapper).load_encoder_decoder.py: model loading helpers (encoder/decoder).model_config.py: default model names and embedding settings.utils.py: shared code utilities.base/: core execution/evaluation framework (from LLM4AD base components).task/: task-specific datasets, templates, and evaluators.
tsp_constructcvrp_constructvrptw_constructjssp_constructknapsack_constructonline_bin_packingqap_constructcflp_constructset_cover_constructadmissible_set
Recommended:
- Python 3.10+
- CUDA GPU
- PyTorch with CUDA
Install common dependencies:
pip install torch transformers sentence-transformers accelerate flash-attn numpy pandas scipy tqdm matplotlibNotes:
- Decoder loading uses Flash Attention 2 (
attn_implementation="flash_attention_2"). - Update environment/model stack if your hardware or CUDA version differs.
From model_config.py:
- Encoder:
Qwen/Qwen3-Embedding-0.6B - Decoder:
Qwen/Qwen3-4B-Instruct-2507 - Matryoshka embedding dim:
128
Run from repository root.
python3 train_unified_flow.py \
--encoder Qwen/Qwen3-Embedding-0.6B \
--embedding-dim 128python3 train_unified_mapper.py \
--encoder Qwen/Qwen3-Embedding-0.6B \
--decoder Qwen/Qwen3-4B-Instruct-2507 \
--embedding-dim 128Example for TSP:
python3 ranking_score_predictor.py \
--task tsp_construct \
--flow_path Flow_Checkpoints/unified_flow_final.pth \
--output_dir Predictor_Checkpointspython3 gradient_search.py \
--task tsp_construct \
--predictor Predictor_Checkpoints/ranking_predictor_u_tsp_construct.pth \
--flow Flow_Checkpoints/unified_flow_final.pth \
--mapper Mapper_Checkpoints/unified_mapper_optimized.pth \
--output_dir gradient_search_u_resultsOptional:
--llm_init --llm_init_count 20to initialize search with freshly generated programs.
The commands above show only the core arguments. Additional hyperparameters and runtime options are available in:
train_unified_flow.pytrain_unified_mapper.pyranking_score_predictor.pygradient_search.pymodel_config.py(default model names and embedding dimension)
You can inspect all CLI options with:
python3 train_unified_flow.py --help
python3 train_unified_mapper.py --help
python3 ranking_score_predictor.py --help
python3 gradient_search.py --helpTypical generated directories/files:
Flow_Checkpoints/Mapper_Checkpoints/Predictor_Checkpoints/gradient_search_u_results/
Search exports include discovered programs, per-program scores, and full program database snapshots.
Parts of the evaluation/runtime infrastructure in base/ and task/ are adapted from the LLM4AD ecosystem: https://github.com/Optima-CityU/LLM4AD/tree/main
