---
title: "SaveSolution"
description: "Keeps every final solution, to look at later or to check again without running anything."
---

> Documentation Index
> Fetch the complete documentation index at: https://docs.cpbenchy.com/llms.txt
> Use this file to discover all available pages before exploring further.

# SaveSolution

## What it does

Writes the final solution of every run that has one to `logs/<run_id>.solution.json`:

```json
{"status": "optimal", "objective": -9, "solution": {"x1": true, "x2": true, "x3": false, "x4": true}}
```

Optionally, it also puts the solution in the result itself. Saved solutions can be checked later with
[`cpbenchy check`](/library/solution-checker/), without running anything again.

## Use it

```python
import cpbenchy
from cpbenchy import observers

cpbenchy.run("instances/", solvers=["ortools"], time_limit=60, plugins=[observers.SaveSolution()])
cpbenchy.run("instances/", solvers=["ortools"], time_limit=60, plugins=[observers.SaveSolution(record=True)])
```

```sh
cpbenchy run instances/ -s ortools -t 60 -p cpbenchy.observers:SaveSolution
cpbenchy run instances/ -s ortools -t 60 -p "cpbenchy.observers:SaveSolution(record=True)"
```

## Options

| Argument | |
|---|---|
| `record` | also store the solution in the result, as `extra["solution"]`. Handy for small models, but it makes `results.jsonl` large. Default `False` |

## What it records

| | |
|---|---|
| `logs/<run_id>.solution.json` | status, objective, and `{name: value}` of the instance's variables |
| `extra["solution"]` | with `record=True`: the same `{name: value}` |

In the parent, the file is `run.artifact("solution.json")`.

## Implementation

Source: https://docs.cpbenchy.com/library/save-solution/index.mdx
