Documentation

Closyr finds an equation that explains a table of numbers. Use it in the browser, run it as a server, call it from the command line or Python, or embed it as WebAssembly.

Using the solver

  1. Choose data. Press the dataset card to open the gallery: every built-in dataset with a picture of its data, from physics laws to measured tables. Or upload a CSV: every column except the last is an input, and the last column is the value to fit. A header row is optional, and its names become the variable names in your equation. You can also drop a file onto the plot.
  2. Shape it, if you like. With one input, Draw sketches a whole curve in one stroke: every point takes the height of the line you draw. Drag moves one point; Smooth, Bump and Flatten reshape the points under the brush. Two inputs are drawn as a surface you can reshape the same way.
  3. Find the formula. Press Find formula (or Ctrl ↵, ⌘ ↵ on a Mac). The best equation so far appears on the board within seconds, the plot bends toward the data as the search improves, and Discoveries lists every better equation as it was found. Pause or stop at any time.
  4. Choose an equation. When the run ends you see the recommended equation: the best balance of fit and simplicity. An equation that reproduces the data to numerical precision is stamped Exact fit. The candidates list the most accurate equation at each size, each with a small plot of its fit; click one to put it on the board. Copy any equation as text, LaTeX or a Python function, or export everything as JSON or CSV.

The keys under Search choose which functions an equation may use; arithmetic and powers are always on. Effort sets how long the search runs, and Advanced settings hold everything else (see Settings).

Run in browser runs the search on your own device with the WebAssembly build, so your data never leaves it. With it off, the search runs on the server, which uses every core.

Finished runs collect under History. Continue starts a new search seeded with a past run’s equations, which often improves a result that ran out of time; Open result brings back every candidate.

Leaving the page loses nothing. A run on the server keeps going without it, and the solver picks it up again when you come back. History is kept too: the server keeps its runs until it restarts, and runs in your browser stay on your device. A run in the browser stops when you leave the page, so the solver asks first, and History keeps the best equation it had found.

Running the server

Closyr is written in Rust. With a Rust toolchain installed:

Terminal
git clone https://github.com/closyr/closyr.git
cd closyr
cargo run --release                 # http://localhost:3001
cargo run --release -- --port 8080  # another port
cargo run --release -- --threads 8  # cap the worker threads

The server hosts this site, the solver, and the HTTP API. Build with --release: a debug build works but is many times slower.

Command line

The srbench binary reads a JSON request on stdin and writes JSON on stdout. Only xs and ys are required.

Terminal
cargo build --release --bin srbench

echo '{"xs": [1, 2, 3, 4, 5], "ys": [1, 4, 9, 16, 25], "timeout_secs": 10}' \
  | ./target/release/srbench

xs is column-major: a flat array for one input, or one array per input. Name the inputs with var_names.

Two inputs, five rows
{
  "xs": [[1, 2, 3, 4, 5],
         [2, 1, 0, 1, 2]],
  "ys": [5, 5, 3, 9, 13],
  "var_names": ["mass", "speed"],
  "timeout_secs": 30
}

The reply carries the equation, its score and size, a model object for predictions, the configuration the run used, and the accuracy/size frontier:

Output (abridged)
{
  "formula": "x ^ 2",
  "score": 0.0,
  "complexity": 3,
  "model": { … },
  "config": { "timeout_secs": 10, "populations": 64, … },
  "frontier": [ { "nodes": 3, "formula": "x ^ 2", "score": 0.0, "shipped": true } ]
}

To predict on new data, send the model back with new xs:

Terminal
echo '{"model": { … }, "xs": [[6, 7]]}' | ./target/release/srbench predict
# → {"predictions": [36.0, 49.0], "n_nan": 0, "n_clipped": 0}

Runs are bounded by timeout_secs (60 by default); a search that finds an exact law ends early. Every other field overrides one default; srbench --help lists them.

Python

A scikit-learn estimator wraps the srbench binary, so it works anywhere an sklearn regressor does.

Terminal
cargo build --release --bin srbench
python3 -m venv .venv && source .venv/bin/activate
pip install -e srbench_wrapper
Python
import numpy as np
from srbench_wrapper import ClosyrRegressor

X = np.array([[1], [2], [3], [4], [5]])
y = np.array([1, 4, 9, 16, 25])

est = ClosyrRegressor(timeout_secs=10).fit(X, y)
print(est.formula_)        # x ^ 2
print(est.predict(np.array([[6]])))  # [36.]

After fit, formula_ holds the equation, frontier_ the accuracy/size trade-off, and effective_config_ the settings the run used. Pass binary_path if srbench lives elsewhere.

WebAssembly

The closyr-wasm crate compiles the engine for the browser. Two builds are available:

Terminal
bash build_wasm.sh           # single-threaded, stable Rust
bash build_wasm_parallel.sh  # Web Worker threads, nightly Rust
JavaScript
import init, { solve } from './wasm/closyr_wasm.js';

await init();
const result = JSON.parse(solve(JSON.stringify({
  xs: [[1, 2, 3, 4, 5]],   // column-major, even for one input
  ys: [1, 4, 9, 16, 25],
  iterations: 100,
})));

console.log(result['best-solution'].formula);
console.log(result.predictions);

solve blocks until the search ends, so call it from a Web Worker. For progress updates, use WasmSolver, which advances the search one step() at a time. The threaded build needs a page served with Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Embedder-Policy: require-corp.

HTTP API

Start a run with POST /api/solve, then follow it as Server-Sent Events. The server keeps every run in memory until it restarts. Requests that send an X-Closyr-Client header see only the runs started with the same value in GET /api/jobs; the solver page sends one, so people sharing a server don’t see each other’s runs.

Terminal
curl -s localhost:3001/api/solve -H 'Content-Type: application/json' -d '{
  "xs": [1, 2, 3, 4, 5],
  "ys": [1, 4, 9, 16, 25],
  "config": { "iterations": 100, "population": 3200, "numLagoons": 32 }
}'
# → {"jobId": "…", "eventsUrl": "/api/jobs/…/events"}

curl -N localhost:3001/api/jobs/<jobId>/events
POST /api/solve
Start a run. Body: xs, ys, and an optional config; optionally a label, and inputNames and targetName for display.
GET /api/jobs/{id}/events
Stream progress events, then one complete or stopped event with every candidate.
POST /api/jobs/{id}/pause, /resume, /stop
Control a running search. A stopped run still returns its best equations.
POST /api/jobs/{id}/continue
Start a new run seeded with this run’s equations.
GET /api/jobs
Your runs, newest first: each one’s status, settings and recommended equation, or its latest progress while it runs.
GET /api/jobs/{id}
One run in full: its data, latest progress, and the result once finished.
PATCH /api/jobs/{id}, DELETE /api/jobs/{id}
Rename a run ({"label": "…"}), or forget it, stopping it first if it is still running.
GET /api/datasets
The built-in example datasets.

Settings

The solver’s Advanced settings, with their API config names. The web solver starts from the values shown; the command line instead sizes the search to the machine and stops on timeout_secs.

SettingFieldSolver defaultWhat it does
Effort / Iterationsiterations200How many generations the search runs. Quick is 50, Thorough is 1,000.
Populationpopulation6,400Equations alive at once, across all islands.
IslandsnumLagoons64Independent populations that evolve separately and trade their best equations.
Cycles per iterationncyclesPerIteration200Mutations tried per island in each iteration.
Max sizemaxLeafs30The largest equation the search may build.
SeedseedRandomSet it to make a run repeatable.
FunctionsmutationsBlacklistsin, cos, tan, exp, logWhich functions equations may use; the API field lists the ones to leave out. Arithmetic and powers are always on.
Error measurescoringMethodMean squaredmse, mae, mae-max, log-cosh or r-squared.
Prefer shortsimplicityBiasOffExtra pressure toward shorter equations during the search.
Measure size bycomplexityMetricNodesnode_count counts every operator and value; leaf_count counts only values.
Constant fittingoptimizerLevenberg–MarquardtHow numeric constants are fitted: levenberg-marquardt, bfgs or nelder-mead.
Fast mathuseF32EvalOn32-bit arithmetic: about twice as fast, slightly less precise.