Repository navigation
Your Own Problem
Two routes exist: a PyTorch module through the Python API, and the benchmark file format that the command line reads.
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)
PYThe 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 lengthnwith finite entries andlo <= hi. -
objectiveis the index of the objective output, orNonefor a feasibility problem. -
lelists the outputs that must be<= 0,eqthe outputs that must equal0. Every output must be listed exactly once, inobjective,leoreq; otherwiseModelErrornames the unassigned and the doubly assigned outputs. -
linear=(A, b)adds linear constraintsA x <= bas float64 torch tensors. - Write
-(expr) + 25.0instead of25.0 - expr; the second form is not supported and raisesUnsupportedModelError. - Operators outside the supported set raise
UnsupportedOperatorErrorbefore 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 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.