# Tune the schedule solve

You set the instance's solve budget, protect it against back-to-back solves, size the memory and choose the search engine. The solver is **one for the whole instance**: one solve at a time, all editions together. It uses a single core: a faster machine saves time ([Solve on another machine](calcul-sur-une-autre-machine.md)).

## Set the instance's solve budget

What the solve does with this time is explained in [how long it takes](/comment-marche-le-calcul/). The budget is an **instance setting**, the same for every edition; *Settings › Calculation* shows it without changing it.

| Variable | Default | Role |
| --- | --- | --- |
| `SOLVER_SECONDS_LIMIT` | `900` | Maximum duration of a solve, in seconds |
| `SOLVER_UNIMPROVED_SECONDS_LIMIT` | `180` | Stop if the plan, already feasible, has not improved for this long, in seconds; `0`: never |
| `SOLVER_SECONDS_LIMIT_MAX` | `3600` | Maximum duration a launch may request (`?seconds=`, MCP), in seconds |

A duration requested above `SOLVER_SECONDS_LIMIT_MAX` is **refused** with a message quoting the ceiling, never trimmed silently. **Startup is refused** if `SOLVER_SECONDS_LIMIT_MAX` is below `SOLVER_SECONDS_LIMIT`. `SOLVER_UNIMPROVED_SECONDS_LIMIT_MAX` is removed in 4.0: setting it does not prevent startup and, since 5.0, is no longer flagged.

## Limit the number of solves on a public instance

The budget bounds **one** solve. An instance open to strangers, or that shares its machine (a public demo next to a production), must also bound how many can be chained. Two guards, off by default, apply to the whole instance **and only if it declares itself public**:

| Variable | Default | Role |
| --- | --- | --- |
| `PUBLIC_INSTANCE` | `false` | `true`: the instance is public. It applies the two guards below and refuses to start without [legal notices](mentions-legales.md) |
| `SOLVER_MAX_SOLVES_PER_HOUR` | `0` (no limit) | Solves accepted over the last 60 rolling minutes |
| `SOLVER_MAX_QUEUED_JOBS` | `0` (no limit) | Jobs that may wait behind the one running ("Schedule"), the latter not included |

The quota counts every solve **accepted at launch**, whatever the path: full or incremental solve, screen, API (`POST /api/solve` included) or MCP assistant, and the staffing check by a solve (the default of *Diagnostic › Staffing need*), which is a real solve. A launch refused for another reason does not count; a job scheduled and then removed from the queue does. Beyond the limit, the launch is refused (`409`) with a sentence the screen shows as is ("This instance limits solving to 12 solves per hour. Next solve possible at 14:32.", the time in the `NOTIFICATIONS_TIMEZONE` zone; a full queue is refused the same way). On an instance that is **not** public, the guards do nothing and the application says at startup that they are ignored if you set them.

The counter and the queue live **in memory**: a restart resets them and **loses the scheduled solves**. At startup, those a previous version left queued or running are marked **interrupted** in the history, with the reason on the job and their number in the log; nothing restarts by itself, start the solve again. A solve that went to a remote machine is followed again. For a demo, an order of magnitude: a dozen solves per hour, a queue of two, and `SOLVER_SECONDS_LIMIT_MAX` lowered to what a visitor is patient enough to wait.

## The application's memory

The application container needs **2 GB** (`mem_limit: 2g` in `docker-compose.prod.yml`), PostgreSQL 512 MB: the host must be able to give them. Solving is the only big consumer, its local search allocating relentlessly. The image sets the JVM through `JDK_JAVA_OPTIONS`:

| Option | JVM default | Why |
| --- | --- | --- |
| `-XX:MaxRAMPercentage=75` | 25% of the container limit | At 25%, the heap tops out at 512 MB under 2 GB and the solver spends its time collecting. Three quarters leave the rest (metaspace, compiled code, stacks, about 200 MB measured during a solve) outside the heap |
| `-XX:+UseParallelGC` | G1, or Serial under two cores or about 1.8 GB | A collector built for throughput: a solve that allocates a lot gains, for pauses of about a tenth of a second with no effect on the screens |

On a small machine (Raspberry Pi 4, three cores, image built on site for arm64: the registry's targets `linux/amd64` only), these options make the search evaluate **more than twice as many moves per second** as the defaults under 1 GB. Three consequences:

- 75% assumes a memory limit on the container. Without one (a bare `docker run`, a manifest without `resources.limits`), the JVM takes three quarters of the host's memory at the expense of PostgreSQL, and the parallel collector rarely gives back what it took: always set a limit.
- Do not keep 1 GB with this image: the container risks being killed mid-solve. A host that cannot give 2 GB redefines `JDK_JAVA_OPTIONS` with a lower percentage and accepts a slower solve.
- To change these options, redefine `JDK_JAVA_OPTIONS` in `environment:`; the value replaces the image's entirely, repeat what you keep. `JAVA_TOOL_OPTIONS` does not work: the JVM reads it before the command line, the image's value wins on the heap, and another collector chosen there (`-XX:+UseG1GC`, which some monitoring agents inject) **prevents the JVM from starting** ("Multiple garbage collectors selected"), the container restarting in a loop.

## Choose the search engine

| Variable | Default | Role |
| --- | --- | --- |
| `SOLVER_ENGINE` | `hand` | `hand`: solves search their plan with the direct engine; `timefold`: with Timefold's search, as before 3.0.0 ([decision 0088](repo:docs/decisions/0088-le-moteur-direct-devient-le-moteur-par-defaut.md)) |

This is an **instance** setting: a compute machine follows that of the application that sends it the problem. The staffing check by a solve follows the instance's engine; "until feasibility" solves stay on Timefold.

The direct engine ([decision 0087](repo:docs/decisions/0087-un-second-moteur-de-recherche-arbitre-par-timefold.md), which gives the measurements and the command) reaches a plan with no hard breach three to four times sooner than Timefold's search on `festival-hivernal`, asking for no more memory on a small machine. It is not taken on trust: every plan it returns is scored again by the application's constraints before being saved.

- A solve that restarts from the plan in place prefers to stay there: at equal score, it keeps the holder it found instead of swapping staff members the rules do not tell apart. This is a preference of the search, not a seat-by-seat guarantee; a solve from scratch is not concerned, and a published plan stays protected by its own rule.
- A gap between the two scores fails the solve, with a message giving both; the saved plan is untouched and nothing switches to the other engine by itself. It is a defect to report; meanwhile, `SOLVER_ENGINE=timefold` and a restart give back the original search.
- The engine refuses by itself, with a message on the job, what it cannot score; the remedy is the same, `SOLVER_ENGINE=timefold`.

  | Refusal | When |
  | --- | --- |
  | "the staff member … is rated on … game categories offered by a stand, for 64 at most" | A record carries more than 64 ratings on categories a stand offers |
  | "the seat … has no stand or no complete schedule" | A timeslot with no end time |
  | "the seat … is held by a staff member absent from the problem" | A starting plan that names someone the edition no longer knows |
  | "… carries two states", "… carries two ceilings", "not exactly one set of parameters" | A duplicated fact, which no table allows |
  | "… seat × staff member pairs" | More than 15 million pairs (15,000 seats for 1,000 staff members): the engine's table no longer fits on a small machine. A usual edition has fewer than a million. Under the `hand` default, the job **fails** with no fallback: an instance of that size sets `SOLVER_ENGINE=timefold` before solving |
  | "the score … is beyond what the engine can order" | More than 500 million soft points, or a medium of the same order: absurd weights |
