Views
No views yet
transformers。1pip install transformers>=4.37.0
2# 如果需要本地运行生成的优化代码,还需要安装求解器
3pip install gurobipy
4Warning: The generated code often importsgurobipy. Ensure you have a valid Gurobi license to execute the generated solver scripts in your local environment.
transformers 库加载并运行模型进行多目标建模的示例代码:1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_name = "YourOrg/MOP-RL-model"
4
5model = AutoModelForCausalLM.from_pretrained(
6 model_name,
7 torch_dtype="auto",
8 device_map="auto"
9)
10tokenizer = AutoTokenizer.from_pretrained(model_name)
11
12# 示例多目标调度问题描述
13prompt = """
14**[Task]**
15Based on the [Problem Description] and [Target JSON Format] below, please write a complete Python script.
16The script must use the `gurobipy` library to model and solve the multi-objective problem, and encapsulate the final solution results into JSON data, writing them to a file named `input.json`.
17
18**[Input Information]**
191. **Multi-objective Problem Description**:
20 \"\"\"
21 {problem_desc}
22 \"\"\"
232. **Target Output JSON Format Example**:
24 \"\"\"
25 {json_template}
26 \"\"\"
27
28**[Requirements]**
291. **Modeling Logic**:
30 - Clearly define decision variables and constraints.
31 - **Multi-objective Handling**: Choose an appropriate multi-objective processing method based on the context (e.g., weighted sum, hierarchical sequence/lexicographic, or Pareto frontier). If weights are not specified, assume equal weights or provide adjustable parameters.
322. **Code Standards**:
33 - The code must be a complete, runnable Python script.
34 - Include necessary comments explaining the mathematical model.
35 - Must include checks for model solution status (e.g., checking for `GRB.OPTIMAL`).
363. **Data Output**:
37 - After solving, extract variable values.
38 - **Format Matching**: Construct a Python dictionary that strictly matches the [Target Output JSON Format Example].
39 - **File Writing**: Use `json.dump` to save the result as `input.json`.
404. **Final Output**:
41 - Please wrap your code in a Python markdown block, i.e., starts with ```python and ends with ```.
42 - Provide only the Python code block."""
43
44messages = [
45 {"role": "system", "content": "You are an algorithm expert proficient in Operations Research and the Python Gurobi solver. You excel at translating complex business scenarios into mathematical models and writing robust code."},
46 {"role": "user", "content": prompt}
47]
48
49text = tokenizer.apply_chat_template(
50 messages,
51 tokenize=False,
52 add_generation_prompt=True
53)
54model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
55
56generated_ids = model.generate(
57 **model_inputs,
58 max_new_tokens=2048
59)
60generated_ids = [
61 output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
62]
63
64response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
65print(response)
66| Metric / Evaluation Level | Description | Score |
|---|---|---|
| 格式准确率 | 生成代码无格式错误 | 1.000 (100%) |
| 代码可执行率 | 无约束遗漏,成功提取变量并求得有效可行解,且求解器语法无报错的概率 | 88.01% |
| 条件帕累托率 | 在模型完全正确的前提下,解集通过支配性测试的概率 | 77.43% |
| 综合帕累托成功率 | parado解的总概率 | 68.15% |
| 模型 | 格式准确率 | 代码可执行率 | 条件帕累托率 | 整体帕累托率 |
|---|---|---|---|---|
| ChatGPT 5(闭源) | 0.985 | 0.615 | 0.732 | 45.0% |
| DeepSeek-R1(671B) | 0.968 | 0.821 | 0.582 | 47.8% |
| Qwen3-Max(1T) | 0.975 | 0.862 | 0.689 | 59.4% |
| MOP-RL(7B) (Ours) | 1.000 | 0.880 | 0.774 | 68.1% |