Requested change:
Ensure all large exams with over 300 students are completed before the 15th time slot to allow teaching assistants sufficient grading time.
Editable files:
solver.py
Read-only context files:
runtime_snapshot.json
Preferred edit targets:
solver.py :: build_exam_gurobi_model, solve_exam_direct (Direct Gurobi model builder and solve surface for code editing.)
Use the existing exam solver mechanisms rather than inventing new interfaces or rewriting the model structure. For slot exclusion requests, resolve the concrete slot from slot_times and slots_per_day, update reserved_slots, and preserve the virtual-block reservation mechanism that materializes the slot exclusion. For pairwise co-enrollment changes between two blocks, keep pair-count updates symmetric across both ordered keys unless the request explicitly distinguishes direction. For large-exam frontloading requests, reuse early_slots and the existing large_blocks logic instead of adding a new frontload formulation. For day-level load caps, ground the limit with block_enrollment over the affected day slots and preserve the existing slot-load-cap semantics. For stress-penalty changes, update the existing objective weight parameters rather than rewriting the objective structure.
Problem-specific edit notes:
The main solver code surfaces are build_exam_gurobi_model(...) for the base Gurobi formulation and solve_exam_direct(...) for runtime data preparation, warm start application, and solve orchestration.
In solve_exam_direct(...), data["blocks"] is the ordered slot-id list and data["slots_per_day"] is the day width; if data["slots"] is present it is a scalar count, not an iterable slot list.
Do not invent new runtime_data or solver_params keys; only pass real Gurobi parameters through _apply_solver_params(...).
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Prefer editing weights, pair_counts, reserved_slots/large_blocks/early_slots derivation, or the direct model constraints in build_exam_gurobi_model(...).
Preserve additive semantics from the request: wording like 'increase by' should usually modify existing values rather than replace them.
When the request refers to a relationship between two blocks, keep the update symmetric unless the request explicitly distinguishes direction.
Slot reservation requests should ground through slot_times and slots_per_day, then reserve the resolved slot via the virtual-block mechanism.
Day-level load changes should use data["block_enrollment"] with first-index occupancy over the relevant slots.
If a combined request specifies an order, preserve that order in the implementation.
Problem context:
problem_context
Exam Block Sequencing Context
You are helping the University Registrar evaluate changes to the final-exam schedule.
This is the block-sequencing stage of a Group-then-Sequence workflow: exams have already been grouped into blocks, and this model places those blocks into exam slots.
The model is about exam timing only; room assignment is handled separately.
Delta requests usually describe policy, comfort, or operational changes to the exam calendar.
Calendar And Basic Assumptions
The exam period is a fixed ordered sequence of slots.
The standard interpretation is three slots per day: morning, afternoon, and evening. In the packaged instances these are usually 9am, 2pm, and 7pm.
Requests such as “Day 2”, “morning”, “afternoon”, “evening”, or “the evening slot immediately before the final evening slot” should be grounded through slots_per_day, slot_times, and slot ids, not through guessed LP names.
Some slots may be intentionally excluded from use. In practice, the Registrar may want to keep certain late slots empty for setup, cleanup, or special events.
In final patch proposals, relative calendar phrases should be resolved to explicit slot ids when the instance data makes that possible.
Objective
The sequencing objective penalizes stressful student exam patterns:
alpha: triples within one day
beta: triples within 24 hours
gamma1: evening-to-morning back-to-backs
gamma2: other back-to-backs
delta: three exams in four consecutive slots
Here, a triple within one day (alpha) means three exams assigned to three consecutive slots that all fall on the same day. A triple within 24 hours (beta) means three exams assigned to three consecutive slots within a 24-hour window, which may cross an overnight boundary. A back-to-back means two exams assigned to consecutive slots; in this model, back-to-backs are split into evening-to-morning (gamma1) and other adjacent-slot pairs (gamma2). These events are not double-counted: exam pairs that are already part of a triple should not also be counted again as back-to-backs.
Core Model View
The sequencing formulation is cyclic over the slot set.
x[i,j,k,s] = 1 means block i is placed at slot s, block j at slot s+1, and block k at slot s+2.
The block occupying slot s is identified by the first index of x[i,j,k,s], so assignment-like policy rules should be expressed by summing x[i,*,*,s] terms over the relevant slots.
y[...] and z[...] are linkage variables used to score triple and four-slot patterns.
Assignment and continuity constraints enforce a valid cyclic schedule in which each block is placed once and each slot receives one block.
Grounding Data
The rendered model representation already exposes the concrete instance data needed to ground requests, including virtual_blocks, large_blocks, early_slots, reserved_slots, block_enrollment, pair_counts, triplet_counts, slots_per_day, and slot_times.
Use those rendered values directly instead of inventing symbolic placeholders, and use the exact numeric slot cutoff stated in the prompt when front-loading requests are instance-specific.
Canonical Interpretations
Slot reservation requests should use reserved_virtual_slot; reserved_slots is the canonical slot list feeding that mechanism.
Front-loading requests should be interpreted through large_blocks and early_slots.
Day-load requests should use block_enrollment and the first-index occupancy view of x[i,j,k,s].
Co-enrollment changes usually affect pair_counts / p or triplet_counts / t; pairwise block relationships are unordered in the policy language, so update both ordered pair keys unless the request explicitly distinguishes direction.
Weight or comfort tradeoff requests usually affect alpha, beta, gamma1, gamma2, or delta.
If the instance data resolves a slot or day directly, emit explicit slot ids rather than symbolic formulas.
Combined Requests
Preserve the requested order when one request bundles several changes.
Combined Registrar edits may mix weight changes, co-enrollment edits, front-loading, slot reservations, and day-load restrictions.
I understand the request and will implement it by tightening the front-loading cutoff so that any block flagged as “large” is forced into slots 1–14. This uses the existing large_blocks and early_slots mechanism without introducing new interfaces.
Changes needed:
Update solve_exam_direct to override early_slots to the explicit list of slots prior to slot 15 (slots 1–14) when building the model. We continue to rely on the provided large_blocks list to identify which blocks contain large exams, consistent with the existing pipeline.
solver.py
code
"""Direct Gurobi build and solve helpers for exam block sequencing."""
from __future__ import annotations
import os
from typing import Any, Dict, Mapping, Tuple
import gurobipy as gp
from gurobipy import GRB
from framework.core.solver_utils import SolveFailureError
from problems.exam_block_seq.solution import extract_block_assignments_from_model
from problems.exam_block_seq.warm_start import apply_exam_warm_start_payload
def build_exam_gurobi_model(
*,
blocks: list[int],
slots_per_day: int,
triple_24_start: list[int],
triple_day_start: list[int],
eve_morn_start: list[int],
other_b2b_start: list[int],
weights: Dict[str, float],
p: Dict[tuple[int, int], float] | None = None,
t: Dict[tuple[int, int, int], float] | None = None,
large_blocks: list[int] | None = None,
early_slots: list[int] | None = None,
time_limit: float | None = None,
) -> "gp.Model":
"""Build the upstream block-sequencing model directly in gurobipy."""
del slots_per_day
slots = list(blocks)
block_sequence_trip = [(i, j, k) for i in blocks for j in blocks for k in blocks]
block_sequence_quad = [(i, j, k, l) for i in blocks for j in blocks for k in blocks for l in blocks]
block_sequence_slot = [(i, j, k, s) for i in blocks for j in blocks for k in blocks for s in slots]
next_slot = {slot: slots[(idx + 1) % len(slots)] for idx, slot in enumerate(slots)}
triple_slots = sorted(list(triple_day_start) + list(triple_24_start))
pair_penalties = dict(p or {})
triplet_penalties = dict(t or {})
alpha = float(weights.get("alpha", 10.0))
beta = float(weights.get("beta", 10.0))
gamma1 = float(weights.get("gamma1", 1.0))
gamma2 = float(weights.get("gamma2", 1.0))
delta = float(weights.get("delta", 5.0))
m = gp.Model("BlockSequencing")
m.setParam("OutputFlag", 1 if _show_solver_log() else 0)
if time_limit:
m.setParam("TimeLimit", float(time_limit))
x = m.addVars(block_sequence_slot, vtype=GRB.BINARY, name="x")
y = m.addVars(block_sequence_trip, vtype=GRB.BINARY, name="y")
z = m.addVars(block_sequence_quad, vtype=GRB.BINARY, name="z")
m.addConstrs(
(
gp.quicksum(x[i, j, k, s] for j in blocks for k in blocks for s in slots) == 1
for i in blocks
),
name="each_i",
)
m.addConstrs(
(
gp.quicksum(x[i, j, k, s] for i in blocks for k in blocks for s in slots) == 1
for j in blocks
),
name="each_j",
)
m.addConstrs(
(
gp.quicksum(x[i, j, k, s] for i in blocks for j in blocks for s in slots) == 1
for k in blocks
),
name="each_k",
)
m.addConstrs(
(
gp.quicksum(x[i, j, k, s] for i in blocks for j in blocks for k in blocks) == 1
for s in slots
),
name="each_slot",
)
m.addConstrs((x[i, i, k, s] == 0 for i in blocks for k in blocks for s in slots), name="no_ii")
m.addConstrs((x[i, j, i, s] == 0 for i in blocks for j in blocks for s in slots), name="no_ik")
m.addConstrs((x[i, j, j, s] == 0 for i in blocks for j in blocks for s in slots), name="no_jj")
m.addConstrs(
(
gp.quicksum(x[i, j, k, s] for i in blocks)
== gp.quicksum(x[j, k, l, next_slot[s]] for l in blocks)
for j in blocks
for k in blocks
for s in slots
),
name="continuity",
)
m.addConstrs(
(
y[i, j, k] == gp.quicksum(x[i, j, k, s] for s in triple_slots)
for i, j, k in block_sequence_trip
),
name="define_y",
)
m.addConstrs(
(
z[i, j, k, l] >= y[i, j, k] + y[j, k, l] - 1
for i, j, k, l in block_sequence_quad
),
name="define_z",
)
frontload_blocks = sorted(
{
int(block)
for block in (large_blocks or [])
if isinstance(block, (int, float)) and int(block) in blocks
}
)
early_slot_values = [int(slot) for slot in (early_slots or [])]
if frontload_blocks:
m.addConstrs(
(
gp.quicksum(
x[i, j, k, s]
for j in blocks
for k in blocks
for s in early_slot_values
)
== 1
for i in frontload_blocks
),
name="frontload",
)
objective = (
gp.quicksum(
gamma1 * float(pair_penalties.get((i, j), 0.0)) * x[i, j, k, s]
for i in blocks
for j in blocks
for k in blocks
for s in eve_morn_start
)
+ gp.quicksum(
gamma2 * float(pair_penalties.get((i, j), 0.0)) * x[i, j, k, s]
for i in blocks
for j in blocks
for k in blocks
for s in other_b2b_start
)
+ gp.quicksum(
alpha * float(triplet_penalties.get((i, j, k), 0.0)) * x[i, j, k, s]
for i in blocks
for j in blocks
for k in blocks
for s in triple_day_start
)
+ gp.quicksum(
beta * float(triplet_penalties.get((i, j, k), 0.0)) * x[i, j, k, s]
for i in blocks
for j in blocks
for k in blocks
for s in triple_24_start
)
+ gp.quicksum(
delta
* (
float(triplet_penalties.get((i, j, k), 0.0))
+ float(triplet_penalties.get((i, k, l), 0.0))
)
* z[i, j, k, l]
for i in blocks
for j in blocks
for k in blocks
for l in blocks
)
)
m.setObjective(objective, GRB.MINIMIZE)
m.update()
return m
def solve_exam_direct(
runtime_data: Mapping[str, Any],
*,
warm_start: Any | None = None,
solver_params: dict[str, Any] | None = None,
time_limit: int | float | None = None,
) -> Tuple[float, Dict[int, int], Dict[str, float | int]]:
"""Solve the direct solver model used by the codeedit pipeline."""
data = dict(runtime_data)
# Blocks are the ordered slot-ids.
blocks = [int(block) for block in data["blocks"]]
# Policy change: ensure all large exams are completed before the 15th slot.
# We enforce this by front-loading large blocks into slots 1..14 using the existing early_slots mechanism.
early_slot_cutoff = 15 # slots strictly before 15
early_slots_override = [slot for slot in blocks if slot < early_slot_cutoff]
grb = build_exam_gurobi_model(
blocks=blocks,
slots_per_day=int(data["slots_per_day"]),
triple_24_start=[int(slot) for slot in data["triple_24_start"]],
triple_day_start=[int(slot) for slot in data["triple_day_start"]],
eve_morn_start=[int(slot) for slot in data["eve_morn_start"]],
other_b2b_start=[int(slot) for slot in data["other_b2b_start"]],
weights=dict(data["weights"]),
p=dict(data.get("pair_counts") or {}),
t=dict(data.get("triplet_counts") or {}),
large_blocks=[int(block) for block in data.get("large_blocks", [])],
early_slots=early_slots_override,
time_limit=float(time_limit if time_limit is not None else data.get("time_limit", 600)),
)
apply_solver_params(grb, solver_params)
apply_exam_warm_start_payload(grb, warm_start)
grb.update()
grb.optimize()
solve_meta = extract_solve_meta(grb)
solve_meta["warm_start_applied"] = warm_start is not None
solve_meta["warm_start_source"] = "provided" if warm_start is not None else "none"
solve_meta["warm_start_mode"] = solve_meta["warm_start_source"]
if grb.Status not in (GRB.OPTIMAL, GRB.TIME_LIMIT):
raise SolveFailureError(
f"Block sequencing solve failed with status {grb.Status}",
solve_meta=solve_meta,
)
if grb.SolCount == 0:
raise SolveFailureError(
f"No incumbent solution available at status {grb.Status}",
solve_meta=solve_meta,
)
solution = extract_block_assignments_from_model(grb, blocks)
return float(grb.ObjVal), solution, solve_meta
def _show_solver_log() -> bool:
return os.getenv("REOPT_DEBUG_PROMPTS", "").strip().lower() in {"1", "true", "yes", "on"}
def apply_solver_params(model: gp.Model, solver_params: Any) -> None:
if not isinstance(solver_params, dict):
return
for name, value in solver_params.items():
if value is None:
continue
try:
model.setParam(str(name), value)
except Exception as exc:
raise RuntimeError(f"Failed to set Gurobi parameter {name}={value!r}") from exc
def extract_solve_meta(model: gp.Model) -> Dict[str, float | int]:
meta: Dict[str, float | int] = {
"status": int(model.Status),
"runtime": float(model.Runtime),
"sol_count": int(model.SolCount),
}
try:
meta["mip_gap"] = float(model.MIPGap)
except Exception:
pass
try:
meta["obj_bound"] = float(model.ObjBound)
except Exception:
pass
if model.SolCount > 0:
try:
meta["objective"] = float(model.ObjVal)
except Exception:
pass
return meta