Requested change:
Reserve the evening slot immediately before the final evening slot so the staff can begin arranging the auditorium for graduation events.
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.
solver.py
python
<<<<<<< SEARCH
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 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))
# Reserve the evening slot immediately before the final evening slot for auditorium setup
# The final evening slot is the last slot with time "7pm"
# We find all slots with time "7pm" and pick the last one, then reserve the previous slot cyclically
# This reservation is done by excluding that slot from assignment (reserved slot)
# We add this slot to reserved_slots and exclude it from the model by removing it from slots and related sets
# Determine the evening slots from the slots list and slot_times
# We need to get slot_times and slots_per_day from environment or parameters
# Since we don't have slot_times here, we must rely on the global instance data or environment
# Instead, we will assume the last evening slot is the max slot in eve_morn_start or other_b2b_start with time "7pm"
# But these are slot subsets, so better to get the last slot with time "7pm" from the slots list and slot_times
# We will get slot_times and slots_per_day from environment variables or from a global variable
# But since this is not available, we will do a workaround:
# The last evening slot is the max slot in slots where (slot - 1) % 3 == 2 (0-based indexing)
# Because slots_per_day=3, evening slot is slot index mod 3 == 2 (0-based)
# So evening slots are those where (slot - 1) % 3 == 2 (since slots are 1-based)
# The final evening slot is max of these, the reserved slot is the previous slot cyclically
# Compute evening slots
evening_slots = [slot for slot in slots if (slot - 1) % 3 == 2]
if evening_slots:
final_evening_slot = max(evening_slots)
# previous slot cyclically
idx = slots.index(final_evening_slot)
reserved_slot = slots[idx - 1] if idx > 0 else slots[-1]
# Remove reserved_slot from slots and related sets
slots = [s for s in slots if s != reserved_slot]
# Also remove reserved_slot from triple_slots if present
triple_slots = [s for s in triple_slots if s != reserved_slot]
# Also remove reserved_slot from eve_morn_start and other_b2b_start if present
eve_morn_start = [s for s in eve_morn_start if s != reserved_slot]
other_b2b_start = [s for s in other_b2b_start if s != reserved_slot]
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
solver.py
python
<<<<<<< SEARCH
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)
grb = build_exam_gurobi_model(
blocks=[int(block) for block in data["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=[int(slot) for slot in data.get("early_slots", [])],
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, [int(block) for block in data["blocks"]])
return float(grb.ObjVal), solution, solve_meta
=======
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)
# Reserve the evening slot immediately before the final evening slot for auditorium setup
# We add this slot to reserved_slots to exclude it from assignment
slot_times = data.get("slot_times", [])
slots_per_day = int(data.get("slots_per_day", 3))
slots = list(data.get("blocks", []))
# Find all evening slots (time == "7pm")
evening_slot_indices = [idx for idx, time in enumerate(slot_times) if time == "7pm"]
# Compute all slot ids that correspond to evening slots
evening_slots = []
for idx in evening_slot_indices:
# Slots are 1-based, so slot id = idx * slots_per_day + offset + 1
# But slot_times is length slots_per_day, repeated over days
# So evening slot offset is idx + 1 (1-based)
# Slots are sequential, so evening slots are those slots where (slot - 1) % slots_per_day == idx
evening_slots = [slot for slot in slots if (slot - 1) % slots_per_day == idx]
if evening_slots:
break # Use the first found evening slot index with "7pm"
if evening_slots:
final_evening_slot = max(evening_slots)
# previous slot cyclically
idx = slots.index(final_evening_slot)
reserved_slot = slots[idx - 1] if idx > 0 else slots[-1]
# Add reserved_slot to reserved_slots in data to exclude it
reserved_slots = set(data.get("reserved_slots", []))
reserved_slots.add(reserved_slot)
data["reserved_slots"] = list(sorted(reserved_slots))
grb = build_exam_gurobi_model(
blocks=[int(block) for block in data["blocks"]],
slots_per_day=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=[int(slot) for slot in data.get("early_slots", [])],
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, [int(block) for block in data["blocks"]])
return float(grb.ObjVal), solution, solve_meta