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(...).
Prefer editing weights, pair_counts, reserved_slots/large_blocks/early_slots derivation, or the direct model constraints in build_exam_gurobi_model(...).
Assistant
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.
Repair guidance:
This is a fresh repair attempt for the same user request from a fresh copy of the source tree.
Preserve the user's intent, not the previous implementation details.
Use the runtime feedback below only to avoid the previous failure mode.
The items below are runtime feedback from earlier attempts in this same run.
Failure class: no_model_effective_edit
Observed failure: Code-edit diff did not change the optimization model.
Keep the requested change, but ensure the edit changes the actual optimization model rather than only dead runtime preparation code.
Recent failed attempts:
attempt 1: Edit Generation | no_model_effective_edit | Code-edit diff did not change the optimization model.
attempt 2: Edit Generation | no_model_effective_edit | Code-edit diff did not change the optimization model.
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 {})
# Increase pairwise co-enrollment count between Block 4 and Block 9 by 120 students symmetrically
key_49 = (4, 9)
key_94 = (9, 4)
pair_penalties[key_49] = pair_penalties.get(key_49, 0.0) + 120.0
pair_penalties[key_94] = pair_penalties.get(key_94, 0.0) + 120.0
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