Skip to content

Your Own Problem

Lemutisme edited this page Nov 7, 2026 · 1 revision

Your Own Problem

Two routes exist: a PyTorch module through the Python API, and the benchmark file format that the command line reads.

From PyTorch

Write the objective and the constraints as the outputs of one torch.nn.Module that maps a vector x of length n to a vector of k outputs, and tell gr.compile what each output is.

The module takes a batch of points x of shape [B, n] and returns a tensor of shape [B, k].

export PYTHONPATH=$PWD/solver:$PWD/solver/complete_verifier PYTHONNOUSERSITE=1; ./env/bin/python - <<'PY' 2>&1 | tail -n 8
import numpy as np, torch
import gear as gr

class Problem(torch.nn.Module):
    # outputs: [objective, g]; minimize (x0 - 1)^2 + sin(x1)  subject to  x0*x1 - 1 <= 0
    def forward(self, x):
        x0, x1 = x[:, 0:1], x[:, 1:2]
        return torch.cat([(x0 - 1.0) ** 2 + torch.sin(x1), x0 * x1 - 1.0], dim=1)

box = (np.array([-2.0, -3.0]), np.array([2.0, 3.0]))
c = gr.compile(Problem(), box=box, objective=0, le=[1])
r = gr.solve(c, gr.Options(preset='gear-f0', device='cuda', time_limit=60))
print(r.status, r.primal_bound, r.dual_bound, r.x, r.replay.ok)
PY

The last line printed is Status.OPTIMAL -1.0 -1.0 {'X_0': 0.9999999974696404, 'X_1': -1.570796318436919} True.

Rules:

  • box=(lo, hi) is required. Both are float64 arrays of length n with finite entries and lo <= hi.
  • objective is the index of the objective output, or None for a feasibility problem.
  • le lists the outputs that must be <= 0, eq the outputs that must equal 0. Every output must be listed exactly once, in objective, le or eq; otherwise ModelError names the unassigned and the doubly assigned outputs.
  • linear=(A, b) adds linear constraints A x <= b as float64 torch tensors.
  • Write -(expr) + 25.0 instead of 25.0 - expr; the second form is not supported and raises UnsupportedModelError.
  • Operators outside the supported set raise UnsupportedOperatorError before the search.

The solve returns the same Result as for a benchmark instance (Python API). The returned point is re-evaluated on your module before it is reported.

The file format of the command line

The command line reads benchmark instances in the layout of benchmarks/ (see Dataset). One instance consists of three files:

File Content
<name>_model.py A PyTorch module that evaluates the objective and the constraints from x
<name>_model_state.pt Its saved state
<name>.vnnlib The variable bounds, the objective and the constraint rows over the module outputs

The files are listed in benchmarks/MANIFEST.tsv with their checksums. The reader checks the checksums and accepts only the instances of the released benchmark; to solve your own problem, use the Python API above.

The AMPL and GAMS text files from which the benchmark was converted are in baselines/benchmark/. The current release does not read AMPL, GAMS or .nl files directly.

Clone this wiki locally