Git Product home page Git Product logo

ai4co / llm-as-hh Goto Github PK

View Code? Open in Web Editor NEW
85.0 4.0 21.0 23.69 MB

Large Language Models as Hyper-Heuristics for Combinatorial Optimization (CO)

License: MIT License

Python 90.03% Jupyter Notebook 9.97%
ant-colony-optimization automatic-algorithm-generation bin-packing-problem evolutionary-algorithms genetic-algorithm hyper-heuristics large-language-models llm-agent multiple-knapsack-problem neural-combinatorial-optimization

llm-as-hh's Introduction

Large Language Models as Hyper-Heuristics for Combinatorial Optimization

🥳 Welcome! This is a codebase that accompanies the paper Large Language Models as Hyper-Heuristics for Combinatorial Optimization.

Give ReEvo 5 minutes, and get a state-of-the-art algorithm in return!

Table of Contents

1. News 📰

  • 2024.05: We release a new paper version.
  • 2024.04: Novel use cases for Neural Combinatorial Optimization (NCO) and Electronic Design Automation (EDA).
  • 2024.02: We are excited to release ReEvo! 🚀

2. Introduction 🚀

Diagram of ReEvo

We introduce Language Hyper-Heuristics (LHHs), an emerging variant of Hyper-Heuristics (HHs) that leverages LLMs for heuristic generation, featuring minimal manual intervention and open-ended heuristic spaces.

To empower LHHs, we present Reflective Evolution (ReEvo), a generic searching framework that emulates the reflective design approach of human experts while much surpassing human capabilities with its scalable LLM inference, Internet-scale domain knowledge, and powerful evolutionary search.

3. Exciting Highlights 🌟

We can improve the following types of algorithms:

  • Neural Combinatorial Optimization (NCO)
  • Genetic Algorithm (GA)
  • Ant Colony Optimization (ACO)
  • Guided Local Search (GLS)
  • Constructive Heuristics

on the following problems:

  • Traveling Salesman Problem (TSP)
  • Capacitated Vehicle Routing Problem (CVRP)
  • Orienteering Problem (OP)
  • Multiple Knapsack Problems (MKP)
  • Bin Packing Problem (BPP)
  • Decap Placement Problem (DPP)

with both black-box and white-box settings.

4. Usage 🔑

  • Set your LLM API key (OpenAI API, ZhiPu API, Llama API) here or as an environment variable.
  • Running logs and intermediate results are saved in ./outputs/main/ by default.
  • Datasets are generated on the fly.
  • Some test notebooks are provided in ./problems/*/test.ipynb.

4.1. Dependency

  • Python >= 3.11
  • openai >= 1.0.0
  • hydra-core
  • scipy

You may install the dependencies above via pip install -r requirements.txt.

Problem-specific dependencies:

  • tsp_aco(_black_box): pytorch, scikit-learn
  • cvrp_aco(_black_box) / mkp_aco(_black_box) / op_aco(_black_box) / NCO: pytorch
  • tsp_gls: numba==0.58

4.2. To run ReEvo

# e.g., for tsp_aco
python main.py problem=tsp_aco

Check out ./cfg/ for more options.

4.3. Available problems

  • Traveling Salesman Problem (TSP): tsp_aco, tsp_aco_black_box, tsp_constructive, tsp_gls, tsp_pomo, tsp_lehd
  • Capacitated Vehicle Routing Problem (CVRP): cvrp_aco, cvrp_aco_black_box, cvrp_pomo, cvrp_lehd
  • Bin Packing Problem (BPP): bpp_offline_aco, bpp_offline_aco_black_box, bpp_online
  • Multiple Knapsack Problems (MKP): mkp_aco, mkp_aco_black_box
  • Orienteering Problem (OP): op_aco, op_aco_black_box
  • Decap Placement Problem (DPP): dpp_ga

4.4. Simple steps to apply ReEvo to your problem

  • Define your problem in ./cfg/problem/.
  • Generate problem instances and implement the evaluation pipeline in ./problems/.
  • Add function_description, function_signature, and seed_function in ./prompts/.

4.5. Use Alternative LLMs

Use the cli parameter llm_client to designate an LLM API provider, and llm_client.model to determine the model to use. For example,

$ export LLAMA_API_KEY=xxxxxxxxxxxxxxxxxxxx
$ python main.py llm_client=llama_api llm_client.model=gemma2-9b

Supported LLM API providers and models including (to be noted that only chat models are supported):

5. Citation 🤩

If you encounter any difficulty using our code, please do not hesitate to submit an issue or directly contact us! If you find our work helpful (or if you are so kind as to offer us some encouragement), please consider giving us a star, and citing our paper.

@article{ye2024large,
      title={Large Language Models as Hyper-Heuristics for Combinatorial Optimization}, 
      author={Haoran Ye and Jiarui Wang and Zhiguang Cao and Federico Berto and Chuanbo Hua and Haeyeon Kim and Jinkyoo Park and Guojie Song},
      year={2024},
      journal={arXiv preprint arXiv:2402.01145},
      note={\url{https://github.com/ai4co/LLM-as-HH}}
}

6. Acknowledgments 🫡

We are very grateful to Yuan Jiang, Yining Ma, Yifan Yang, and AI4CO community for valuable discussions and feedback.

Also, our work is built upon the following projects, among others:

llm-as-hh's People

Contributors

dependabot[bot] avatar fedebotu avatar furffico avatar henry-yeh avatar wuyuesong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

llm-as-hh's Issues

请教一些实验输出质量的问题

我运行的结果发现,seed_func对于结果的影响很大。reevo所生成的算法,很多程度上受到seed_func的影响,会偏向于生成相似的函数结构和变量,在探索能力上有所欠缺。不知道是否存在避免这种影响的设计,让框架尽可能多的探索

Seed function is invalid exception

Hello ReEvo team!

I've been trying to use ziphuAi instead of Gpt4 to run the prompts. I've provided the api key for ziphuAi, and I've done some changes in the config.yaml to enable that.

# LLM parameters
model: GLM-4  # LLM model (other options include gpt-4-turbo-preview, GLM-3-Turbo, GLM-4)
temperature: 1  # temperature for chat completion
suffix: GLM  # suffix for generated files (indicates LLM model)

but when I run the code an exception happens due to seed function. Can you please help with that?

Here is the full error:

python main.py problem=tsp_constructive algorithm=reevo
[2024-04-08 13:05:18,818][root][INFO] - Workspace: C:\Users\kjkj0\LLM-as-HH\outputs\main\2024-04-08_13-05-18
[2024-04-08 13:05:18,819][root][INFO] - Project Root: C:\Users\kjkj0\LLM-as-HH
[2024-04-08 13:05:18,819][root][INFO] - Using LLM: GLM-4
[2024-04-08 13:05:18,819][root][INFO] - Using Algorithm: reevo
[2024-04-08 13:05:19,378][root][INFO] - Problem: tsp_constructive
[2024-04-08 13:05:19,378][root][INFO] - Problem description: Solving Traveling Salesman Problem (TSP) with constructive heuristics. TSP requires finding the shortest path that visits all given nodes and returns to the starting node.
[2024-04-08 13:05:19,378][root][INFO] - Function name: select_next_node
[2024-04-08 13:05:19,379][root][INFO] - Evaluating seed function...
[2024-04-08 13:05:19,389][root][INFO] - Seed function code:
import numpy as np
def select_next_node_v2(current_node: int, destination_node: int, unvisited_nodes: set, distance_matrix: np.ndarray) -> int:
    """Select the next node to visit from the unvisited nodes."""
    threshold = 0.7
    c1, c2, c3, c4 = 0.4, 0.3, 0.2, 0.1
    scores = {}
    for node in unvisited_nodes:
        all_distances = [distance_matrix[node][i] for i in unvisited_nodes if i != node]
        average_distance_to_unvisited = np.mean(all_distances)
        std_dev_distance_to_unvisited = np.std(all_distances)
        score = c1 * distance_matrix[current_node][node] - c2 * average_distance_to_unvisited + c3 * std_dev_distance_to_unvisited - c4 * distance_matrix[destination_node][node]
        scores[node] = score
    next_node = min(scores, key=scores.get)
    return next_node
[2024-04-08 13:05:19,389][root][INFO] - Iteration 0: Running Code 0
[2024-04-08 13:05:19,553][root][INFO] - Iteration 0: Code Run 0 execution error!
[2024-04-08 13:05:19,572][root][INFO] - Iteration 0, response_id 0: Objective value: inf
Error executing job with overrides: ['problem=tsp_constructive', 'algorithm=reevo']
Traceback (most recent call last):
  File "C:\Users\kjkj0\LLM-as-HH\main.py", line 52, in <module>
    main()
  File "C:\Users\kjkj0\LLM-as-HH\revo-venv-ziphu\Lib\site-packages\hydra\main.py", line 94, in decorated_main
    _run_hydra(
  File "C:\Users\kjkj0\LLM-as-HH\revo-venv-ziphu\Lib\site-packages\hydra\_internal\utils.py", line 394, in _run_hydra
    _run_app(
  File "C:\Users\kjkj0\LLM-as-HH\revo-venv-ziphu\Lib\site-packages\hydra\_internal\utils.py", line 457, in _run_app
    run_and_report(
  File "C:\Users\kjkj0\LLM-as-HH\revo-venv-ziphu\Lib\site-packages\hydra\_internal\utils.py", line 223, in run_and_report
    raise ex
  File "C:\Users\kjkj0\LLM-as-HH\revo-venv-ziphu\Lib\site-packages\hydra\_internal\utils.py", line 220, in run_and_report
    return func()
           ^^^^^^
  File "C:\Users\kjkj0\LLM-as-HH\revo-venv-ziphu\Lib\site-packages\hydra\_internal\utils.py", line 458, in <lambda>
    lambda: hydra.run(
            ^^^^^^^^^^
  File "C:\Users\kjkj0\LLM-as-HH\revo-venv-ziphu\Lib\site-packages\hydra\_internal\hydra.py", line 132, in run
    _ = ret.return_value
        ^^^^^^^^^^^^^^^^
  File "C:\Users\kjkj0\LLM-as-HH\revo-venv-ziphu\Lib\site-packages\hydra\core\utils.py", line 260, in return_value
    raise self._return_value
  File "C:\Users\kjkj0\LLM-as-HH\revo-venv-ziphu\Lib\site-packages\hydra\core\utils.py", line 186, in run_job
    ret.return_value = task_function(task_cfg)
                       ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\kjkj0\LLM-as-HH\main.py", line 31, in main
    reevo = ga(cfg, ROOT_DIR)
            ^^^^^^^^^^^^^^^^^
  File "C:\Users\kjkj0\LLM-as-HH\reevo.py", line 26, in __init__
    self.init_population()
  File "C:\Users\kjkj0\LLM-as-HH\reevo.py", line 98, in init_population
    raise RuntimeError(f"Seed function is invalid. Please check the stdout file in {os.getcwd()}.")
RuntimeError: Seed function is invalid. Please check the stdout file in C:\Users\kjkj0\LLM-as-HH\outputs\main\2024-04-08_13-05-18.

关于api_key传入一些问题

1、想请问这个项目api_key具体如何传入,试过很多方式始终无法顺利运行

2、关于适配api问题,能否像eoh一样支持传入endpoint(baseurl),方便使用一些非openai官方的key

希望版主有空能够解答疑惑,十分感谢。

RuntimeError due to Invalid Seed Function Evaluation

When running ReEvo with the command python main.py problem=tsp_aco, an error occurs during the evaluation of the seed function, leading to a RuntimeError. The error message suggests that the seed function is invalid, but it doesn't provide specific details about what caused the invalid evaluation.

Steps to Reproduce:

  1. Set up the environment as described in the README.
  2. Run the command python main.py problem=tsp_aco.
  3. Observe the RuntimeError related to the invalid seed function evaluation.

Error Logs:

[2024-05-16 12:38:34,259][root][INFO] - Workspace: /remote-home/iot_liuguangyi/llm/LLM-as-HH/outputs/main/2024-05-16_12-38-34
[2024-05-16 12:38:34,259][root][INFO] - Project Root: /remote-home/iot_liuguangyi/llm/LLM-as-HH
[2024-05-16 12:38:34,259][root][INFO] - Using LLM: gpt-3.5-turbo
[2024-05-16 12:38:34,259][root][INFO] - Using Algorithm: reevo
[2024-05-16 12:38:34,668][root][INFO] - Problem: tsp_aco
[2024-05-16 12:38:34,668][root][INFO] - Problem description: Solving Traveling Salesman Problem (TSP) via stochastic solution sampling following "heuristics". TSP requires finding the shortest path that visits all given nodes and returns to the starting node.
[2024-05-16 12:38:34,668][root][INFO] - Function name: heuristics
[2024-05-16 12:38:34,668][root][INFO] - Evaluating seed function...
[2024-05-16 12:38:34,669][root][INFO] - Seed function code:
import numpy as np
def heuristics_v2(distance_matrix: np.ndarray) -> np.ndarray:
return 1 / distance_matrix
[2024-05-16 12:38:34,669][root][INFO] - Iteration 0: Running Code 0
[2024-05-16 12:38:36,349][root][INFO] - Iteration 0: Code Run 0 successful!
[2024-05-16 12:38:56,351][root][INFO] - Error for response_id 0: Command '['python', '-u', '/remote-home/iot_liuguangyi/llm/LLM-as-HH/problems/tsp_aco/eval.py', '50', '/remote-home/iot_liuguangyi/llm/LLM-as-HH', 'train']' timed out after 19.99986495007761 seconds

Please let us know if you need further information or assistance in resolving this issue. Thank you!

Attempt 1 failed with error: Request timed out.

How to solve this problem?

[2024-05-27 11:03:36,897][root][INFO] - Using LLM: gpt-3.5-turbo
[2024-05-27 11:03:36,899][root][INFO] - Using Algorithm: reevo
[2024-05-27 11:03:38,178][root][INFO] - Problem: tsp_aco
[2024-05-27 11:03:38,178][root][INFO] - Problem description: Solving Traveling Salesman Problem (TSP) via stochastic solution sampling following "heuristics". TSP requires finding the shortest path that visits all given nodes and returns to the starting node.
[2024-05-27 11:03:38,178][root][INFO] - Function name: heuristics
[2024-05-27 11:03:38,191][root][INFO] - Evaluating seed function...
[2024-05-27 11:03:38,191][root][INFO] - Seed function code:
import numpy as np
def heuristics_v2(distance_matrix: np.ndarray) -> np.ndarray:
return 1 / distance_matrix
[2024-05-27 11:03:38,191][root][INFO] - Iteration 0: Running Code 0
[2024-05-27 11:03:40,944][root][INFO] - Iteration 0: Code Run 0 successful!
[2024-05-27 11:03:49,394][root][INFO] - Iteration 0, response_id 0: Objective value: 6.737054565194424
[2024-05-27 11:03:49,394][root][INFO] - Iteration 0: Elitist: 6.737054565194424
[2024-05-27 11:03:49,394][root][INFO] - Iteration 0 finished...
[2024-05-27 11:03:49,396][root][INFO] - Best obj: 6.737054565194424, Best Code Path: problem_iter0_code0.py
[2024-05-27 11:03:49,396][root][INFO] - Function Evals: 1
[2024-05-27 11:03:49,396][root][INFO] - Initial Population Prompt:
System Prompt:
You are an expert in the domain of optimization heuristics. Your task is to design heuristics that can effectively solve optimization problems.
Your response outputs Python code and nothing else. Format your code as a Python code string: "python ... ".
User Prompt:
Write a heuristics function for Solving Traveling Salesman Problem (TSP) via stochastic solution sampling following "heuristics". TSP requires finding the shortest path that visits all given nodes and returns to the starting node.
The heuristics function takes as input a distance matrix, and returns prior indicators of how promising it is to include each edge in a solution. The return is of the same shape as the input.

def heuristics_v1(distance_matrix: np.ndarray) -> np.ndarray:
return 1 / distance_matrix
Refer to the format of a trivial design above. Be very creative and give heuristics_v2. Output code only and enclose your code with Python code block: python ... .

  • Try combining various factors to determine how promising it is to select an edge.
  • Try sparsifying the matrix by setting unpromising elements to zero.
    [2024-05-27 11:03:59,454][openai._base_client][INFO] - Retrying request to /chat/completions in 0.760825 seconds
    [2024-05-27 11:04:10,234][openai._base_client][INFO] - Retrying request to /chat/completions in 1.547540 seconds
    [2024-05-27 11:04:21,796][root][INFO] - Attempt 1 failed with error: Request timed out.
    [2024-05-27 11:04:32,819][openai._base_client][INFO] - Retrying request to /chat/completions in 0.760375 seconds
    [2024-05-27 11:04:43,588][openai._base_client][INFO] - Retrying request to /chat/completions in 1.944483 seconds
    [2024-05-27 11:04:55,558][root][INFO] - Attempt 2 failed with error: Request timed out.

RuntimeError: All individuals are invalid

[2024-07-22 19:57:06,333][root][INFO] - Workspace: D:\deeplearning\LLM-as-HH\outputs\main\2024-07-22_19-57-06
[2024-07-22 19:57:06,333][root][INFO] - Project Root: D:\deeplearning\LLM-as-HH
[2024-07-22 19:57:06,333][root][INFO] - Using LLM: llama3-70b
[2024-07-22 19:57:06,333][root][INFO] - Using Algorithm: reevo
[2024-07-22 19:57:07,393][root][INFO] - Problem: tsp_aco
[2024-07-22 19:57:07,393][root][INFO] - Problem description: Solving Traveling Salesman Problem (TSP) via stochastic solution sampling following "heuristics". TSP requires finding the shortest path that visits all given nodes and returns to the starting node.
[2024-07-22 19:57:07,393][root][INFO] - Function name: heuristics
[2024-07-22 19:57:07,394][root][INFO] - Evaluating seed function...
[2024-07-22 19:57:07,395][root][INFO] - Seed function code:
import numpy as np
def heuristics_v2(distance_matrix: np.ndarray) -> np.ndarray:
return 1 / distance_matrix
[2024-07-22 19:57:07,395][root][INFO] - Iteration 0: Running Code 0
[2024-07-22 19:57:09,359][root][INFO] - Iteration 0: Code Run 0 successful!
[2024-07-22 19:57:16,735][root][INFO] - Iteration 0, response_id 0: Objective value: 6.469335357675092
[2024-07-22 19:57:16,735][root][INFO] - Iteration 0: Elitist: 6.469335357675092
[2024-07-22 19:57:16,735][root][INFO] - Iteration 0 finished...
[2024-07-22 19:57:16,735][root][INFO] - Best obj: 6.469335357675092, Best Code Path: problem_iter0_code0.py
[2024-07-22 19:57:16,735][root][INFO] - Function Evals: 1
[2024-07-22 19:57:16,735][root][INFO] - Initial Population Prompt:
System Prompt:
You are an expert in the domain of optimization heuristics. Your task is to design heuristics that can effectively solve optimization problems.
Your response outputs Python code and nothing else. Format your code as a Python code string: "python ... ".

User Prompt:
Write a heuristics function for Solving Traveling Salesman Problem (TSP) via stochastic solution sampling following "heuristics". TSP requires finding the shortest path that visits all given nodes and returns to the starting node.
The heuristics function takes as input a distance matrix, and returns prior indicators of how promising it is to include each edge in a solution. The return is of the same shape as the input.

def heuristics_v1(distance_matrix: np.ndarray) -> np.ndarray:
return 1 / distance_matrix

Refer to the format of a trivial design above. Be very creative and give heuristics_v2. Output code only and enclose your code with Python code block: python ... .

How to solve the problem?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.