# SPDX-License-Identifier: LGPL-3.0-or-later ## @file # @brief Learn dispatching rules on a fixed panel of dynamic flexible scheduling instances. """Evolve and audit expression-tree scheduling rules on small job shops.""" import argparse from heapq import heapify, heappop, heappush import json from operator import add, mul, sub import numpy as np from deap import gp from qdpy.algorithms import DecisionProbe, QDGP def instance(rng): """Generate arrivals, due dates, and two machine options for each operation.""" arrivals = rng.integers(0, 9, 8) due = arrivals + rng.integers(8, 22, len(arrivals)) durations = rng.integers(1, 10, (len(arrivals), 2, 2)) return arrivals, due, durations def evaluate_jobs(problem, sequencing, routing): """Return total tardiness plus a small makespan cost.""" arrivals, due, durations = problem operations, machines = durations.shape[1:] events = [(int(time), job, 0) for job, time in enumerate(arrivals)] heapify(events) available = [0] * machines queues = [[] for _ in range(machines)] completed = np.zeros(len(arrivals), dtype=int) while events: clock = events[0][0] while events and events[0][0] == clock: _, job, operation = heappop(events) if operation == operations: completed[job] = clock continue times = durations[job, operation] loads = [max(0, available[m] - clock) + sum(entry[3] for entry in queue) for m, queue in enumerate(queues)] selected = min(range(machines), key=lambda machine: routing( float(times[machine]), float(loads[machine]), float(due[job] - clock))) queues[selected].append((job, operation, clock, int(times[selected]))) for machine, queue in enumerate(queues): if queue and available[machine] <= clock: slot = min(range(len(queue)), key=lambda i: sequencing( float(queue[i][3]), float(clock - queue[i][2]), float(due[queue[i][0]] - clock))) job, operation, _, duration = queue.pop(slot) available[machine] = clock + duration heappush(events, (clock + duration, job, operation + 1)) return float(np.maximum(0, completed - due).sum() + .1 * completed.max()) def make_search(seed, one_cell=False): """Use finite primitives and rank probes independent of the scoring instances.""" primitives = gp.PrimitiveSet("priority", 3) primitives.renameArguments(ARG0="duration", ARG1="waiting", ARG2="slack") for operation in (add, sub, mul, min, max): primitives.addPrimitive(operation, 2) primitives.addTerminal(1.) rng = np.random.default_rng(381) probes = [] for _ in range(6): candidates = rng.integers(0, 12, (3, 3)) if one_cell: candidates[:] = candidates[0] order = np.argsort(candidates[:, 0] + candidates[:, 1], kind="stable") probes.append(DecisionProbe(candidates, tuple(order))) return QDGP(primitives, primitives, sequencing_probes=probes[:3], routing_probes=probes[3:], max_depth=4, initial_samples=16, seed=seed) def run(method, seed, budget): """Train on the same cases and audit the training winner on unseen cases.""" rng = np.random.default_rng(677) training = [instance(rng) for _ in range(5)] held_out = [instance(rng) for _ in range(8)] search = make_search(seed, one_cell=method == "single-niche") best = search.run(lambda seq, route: np.mean([evaluate_jobs(case, seq, route) for case in training]), budget) seq, route = search.compile(best.rules) scores = [evaluate_jobs(case, seq, route) for case in held_out] return dict(method=method, seed=seed, attempts=search.evaluations, training_cases=budget * len(training), audit_cases=len(held_out), occupied=len(search.archive), training_cost=best.fitness, held_out_cost=float(np.mean(scores))) if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--budget", type=int, default=144) parser.add_argument("--seeds", type=int, nargs="+", default=[11, 22, 33]) args = parser.parse_args() for seed in args.seeds: for method in ("qdgp", "single-niche"): print(json.dumps(run(method, seed, args.budget), sort_keys=True, allow_nan=False))