This model translates plain-English optimization problems into executable
MiniZinc code. It was fine-tuned with LoRA on the
learn2zinc dataset using the
Unsloth library.
1import torch
2from unsloth import FastLanguageModel
3
4# Load model — do NOT apply a chat template
5model, tokenizer = FastLanguageModel.from_pretrained(
6 model_name="skadio/learn2zinc-GPT-oss-20B"
7 max_seq_length=4096,
8 dtype=None,
9 load_in_4bit=True,
10)
11FastLanguageModel.for_inference(model)
12
13# Define the problem
14problem = """A farmer needs to decide how many cows, sheep, and chickens to raise in order to achieve maximum profit. The farmer can sell cows, sheep, and chickens for $500, $200, and $8 each, respectively. The feed costs for each cow, sheep, and chicken are $100, $80, and $5, respectively. The profit is the difference between the selling price and the feed cost. Each cow, sheep, and chicken produces 10, 5, and 3 units of manure per day, respectively. Due to the limited time the farm staff has for cleaning the farm each day, they can handle up to 800 units of manure. Additionally, because of the limited farm size, the farmer can raise at most 50 chickens. Furthermore, the farmer must have at least 10 cows to meet customer demand. The farmer must also raise at least 20 sheep. Finally, the total number of animals cannot exceed 100."""
15
16# Build Harmony-format prompt
17prompt = (
18 "<|start|>system<|message|>You are ChatGPT, a large language model trained by OpenAI.\n"
19 "Knowledge cutoff: 2024-06\n"
20 "Current date: 2026-03-04\n\n"
21 "Reasoning: medium\n\n"
22 "# Valid channels: analysis, commentary, final. "
23 "Channel must be included for every message.<|end|>"
24 "<|start|>developer<|message|># Instructions\n\n"
25 "Generate MiniZinc code for the following optimization problem.<|end|>"
26 f"<|start|>user<|message|>{problem}<|end|>"
27 "<|start|>assistant"
28)
29
30inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
31
32# Resolve stop-token IDs
33stop_token_ids = []
34for token in ["<|end|>", "<|return|>"]:
35 encoded = tokenizer.encode(token, add_special_tokens=False)
36 if encoded:
37 stop_token_ids.append(encoded[0])
38
39# Generate
40with torch.no_grad():
41 outputs = model.generate(
42 **inputs,
43 max_new_tokens=4096,
44 do_sample=False,
45 eos_token_id=stop_token_ids,
46 pad_token_id=tokenizer.pad_token_id,
47 )
48
49generated = tokenizer.decode(
50 outputs[0][inputs["input_ids"].shape[1]:],
51 skip_special_tokens=False,
52)
53
54# --- Extract content from the final channel ---
55if "<|channel|>final<|message|>" in generated:
56 content = generated.split("<|channel|>final<|message|>")[-1]
57 for stop in ["<|end|>", "<|return|>"]:
58 content = content.split(stop)[0]
59 response = content.strip()
60else:
61 # Fallback: strip stop tags
62 for stop in ["<|end|>", "<|return|>"]:
63 generated = generated.split(stop)[0]
64 response = generated.strip()
65
66print(response)
The model wraps its output in a fenced code block. To extract the code:
1import re
2
3def extract_minizinc_code(text):
4 match = re.search(r'```(?:\w+)?\n(.*?)\n```', text, re.DOTALL | re.IGNORECASE)
5 return match.group(1).strip() if match else None
6
7code = extract_minizinc_code(response)
Models were evaluated on the
IndustryOR subset of
learn2zinc (
cardinal_operations_industryor). Generated MiniZinc code was executed with the
HiGHS solver (120 s timeout). All generations used
temperature = 0 for reproducibility.
For full evaluation details, see
learn2zinc.
Training data comes from
skadio/learn2zinc-augmented, which pairs natural language optimization problem descriptions with corresponding MiniZinc code. For GPT-OSS, training examples were reformatted into Harmony format with automatic CoT detection: examples containing reasoning are routed to the
analysis channel, while direct answers use only the
final channel.