Skip to content

OR-Tools

Optimisation with Google OR-Tools β€” routing, scheduling, assignment, flows and bin-packing

Connector software
Free and open source
Service credentials
Not needed
Use with an agent

Install the Orchard skill in your project to help your agent find connectors, configure them, and verify SQL results.

npx skills add Query-farm/vgi-skills --skill orchard

Copy this request into your coding agent to get started.

Use https://query.farm/products/orchard/connectors/ortools/index.md to help me query OR-Tools with DuckDB or Haybarn 2.0.1 and VGI protocol 3.4. Ask only for missing details about the result I need or whether I want local execution, self-hosting or Query Farm hosting. Check the connector's actual hosting availability, credentials and input requirements. Read one format of each needed reference and the exact object declarations before writing SQL. Use a released connector, and distinguish a saved example result from anything you actually execute. If you cannot run the query, tell me what remains to verify.

Assign tasks at the lowest cost

Example output Β· captured
Connector release
v0.5.0
Engine
2.0.1
VGI protocol
3.4.0
SELECT worker, task, cost
FROM ortools.main.solve_assignment(cost := [[0,0,90],[0,1,76],[0,2,75],[1,0,35],[1,1,85],[1,2,55],[2,0,125],[2,1,95],[2,2,90]]::BIGINT[][], maximize := false)
ORDER BY worker
Result
worker task cost
0 1 76
1 0 35
2 2 90

Quick start

Choose where to run OR-Tools, then follow the steps below.

The connector runs alongside DuckDB or Haybarn. No Query Farm account is needed.

1. Install the vgi extension

Run this in your SQL client. On DuckDB, keep FROM query_farm on LOAD in later sessions.

setup.sql
SET allow_extension_repositories = 'allowed';

CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
    WITH PREFIX 'https://duckdb-extensions.query.farm';

INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;

2. Attach the connector

Attach with uvx

Needs uv. uvx builds the worker from its release tag on first attach and reuses the cached environment after that.

ATTACH 'ortools' AS ortools (
  TYPE vgi,
  LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-ortools'
);

3. Run a query

query.sql
SELECT worker, task, cost
FROM ortools.main.solve_assignment(cost := [[0,0,90],[0,1,76],[0,2,75],[1,0,35],[1,1,85],[1,2,55],[2,0,125],[2,1,95],[2,2,90]]::BIGINT[][], maximize := false)
ORDER BY worker
More example queries β†’
Technical details
Verified connector release
v0.5.0
Engine
2.0.1
VGI protocol
3.4.0
Verified on
December 1, 2026
Release notes on GitHub
Pin a version for production

When running the connector yourself, keep a specific release until you’re ready to upgrade. Replace the ATTACH statement above with this one and follow the same extension and credential setup.

Attach with uvx

Needs uv. uvx builds the worker from its release tag on first attach and reuses the cached environment after that.

ATTACH 'ortools' AS ortools (
  TYPE vgi,
  LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-ortools'
);

Overview

Google OR-Tools optimization / constraint solving as SQL functions.

Catalog

Everything ortools exposes once attached β€” read straight from the worker's released build.

ortools 1 schema
main 2 tables Β· 12 functions

OR-Tools solvers: CP-SAT, routing, assignment, scheduling, flow, packing.

Schema examples (5)
Examples (5)

Least-cost assignment of three workers to three tasks (returns each worker's chosen task and its cost).

SELECT worker, task, cost
FROM ortools.main.solve_assignment(cost := [[0,0,90],[0,1,76],[0,2,75],[1,0,35],[1,1,85],[1,2,55],[2,0,125],[2,1,95],[2,2,90]]::BIGINT[][], maximize := false)
ORDER BY worker;
Result
worker task cost
0 1 76
1 0 35
2 2 90

Optimal travelling-salesperson tour over a 5-node distance matrix, in visit order.

SELECT seq, node, arrival
FROM ortools.main.solve_tsp(distance := [[0,2,9,10,1],[1,0,6,4,8],[15,7,0,8,3],[6,3,12,0,5],[9,8,4,7,0]]::BIGINT[][], start := 0)
ORDER BY seq;

List the bundled OR-Tools / solver component versions.

SELECT component, version FROM ortools.main.solver_info ORDER BY component;

Arc criticality: for each arc carrying flow in the 0->3 max-flow solution, re-solve with that arc removed (max_flow_value) and report the throughput lost. Losing arc 2->3 drops the max flow from 30 to 10; each of the other flow-carrying arcs costs 10.

SELECT f."from", f."to", f.flow,
  ortools.main.max_flow_value(list_filter([[0,1,20],[0,2,30],[1,2,10],[1,3,10],[2,3,20]]::BIGINT[][], lambda a: NOT (a[1] = f."from" AND a[2] = f."to")), 0, 3) AS flow_without_arc,
  ortools.main.max_flow_value([[0,1,20],[0,2,30],[1,2,10],[1,3,10],[2,3,20]]::BIGINT[][], 0, 3) - flow_without_arc AS flow_lost
FROM ortools.main.max_flow(arcs := [[0,1,20],[0,2,30],[1,2,10],[1,3,10],[2,3,20]]::BIGINT[][], source := 0, sink := 3) AS f
ORDER BY flow_lost DESC, f."from", f."to";
1 more example

Ship 12 units from node 0 to node 3 at minimum cost (68), and compare against the network's maximum 0->3 throughput from max_flow_value over the same arcs' capacities (17), leaving 5 units of headroom.

SELECT sum(flow) FILTER (WHERE "from" = 0) AS shipped,
  sum(cost) AS total_cost,
  ortools.main.max_flow_value(list_transform([[0,1,10,2],[0,2,8,5],[1,2,6,1],[1,3,5,4],[2,3,12,2]]::BIGINT[][], lambda a: a[1:3]), 0, 3) AS max_shippable,
  max_shippable - shipped AS headroom
FROM ortools.main.min_cost_flow(arcs := [[0,1,10,2],[0,2,8,5],[1,2,6,1],[1,3,5,4],[2,3,12,2]]::BIGINT[][], supplies := [[0,12],[3,-12]]::BIGINT[][])
ORDER BY total_cost;
Tables
last_solve_stats 2 examples2 cols

Statistics of the most recent solve on this worker process, one row per metric (NULL values until the first solve).

Columns
metric VARCHAR
Metric name from the most recent solve.
value VARCHAR
Metric value as a string (NULL string when not yet set).
ConstraintsPRIMARY KEY (metric)
Examples (2)

Read every metric of the most recent solve on this worker.

SELECT metric, value FROM ortools.main.last_solve_stats ORDER BY metric;

Fetch just the status of the most recent solve.

SELECT value FROM ortools.main.last_solve_stats WHERE metric = 'status';
Link
solver_info 2 examples2 cols

Bundled OR-Tools / worker / dialect versions, one row per component (no arguments).

Columns
component VARCHAR
Component name (ortools, cp_sat, worker, dialect, ...).
versionnot null VARCHAR
Component version string.
ConstraintsPRIMARY KEY (component)
Examples (2)

List every bundled component and its version.

SELECT component, version FROM ortools.main.solver_info ORDER BY component;

Fetch just the OR-Tools engine version.

SELECT version FROM ortools.main.solver_info WHERE component = 'ortools';
Link
Scalar functions
max_flow_value (arcs BIGINT[][], source BIGINT, sink BIGINT) 1 example

The scalar maximum-flow value from source to sink over a capacitated network (arcs as a [[from, to, capacity], ...] LIST). Backed by OR-Tools SimpleMaxFlow.

Arguments
arcs BIGINT[][]
[from, to, capacity] arcs, one network per row.
source BIGINT
Source node id.
sink BIGINT
Sink node id.
ReturnsBIGINT
Example

Max-flow value from node 0 to node 3 (= 30).

SELECT ortools.main.max_flow_value([[0,1,20],[0,2,30],[1,2,10],[1,3,10],[2,3,20]]::BIGINT[][], 0, 3);
Link
Table functions
max_flow (arcs := BIGINT[][], sink := BIGINT, source := BIGINT) 2 examples

Maximum flow from a source to a sink over a capacitated network. Backed by OR-Tools SimpleMaxFlow. Returns one row per arc with positive flow; see max_flow_value for the scalar total.

Named arguments
arcs := BIGINT[][]
[from, to, capacity] arcs of the network. Β· default null
sink := BIGINT
Sink node id. Β· default 0
source := BIGINT
Source node id. Β· default 0
Returns columns
from BIGINT
Arc tail node.
to BIGINT
Arc head node.
flow BIGINT
Flow sent on this arc (rows with zero flow are omitted).
status VARCHAR
OPTIMAL / INFEASIBLE / UNKNOWN.
Examples (2)

Max flow from node 0 to node 3 (value 30).

SELECT sum(flow) FILTER (WHERE "from" = 0) AS out_of_source
FROM ortools.main.max_flow(arcs := [[0,1,20],[0,2,30],[1,2,10],[1,3,10],[2,3,20]]::BIGINT[][], source := 0, sink := 3);

Arc criticality: for each arc carrying flow in the 0->3 max-flow solution, re-solve with that arc removed (max_flow_value) and report the throughput lost. Losing arc 2->3 drops the max flow from 30 to 10; each of the other flow-carrying arcs costs 10.

SELECT f."from", f."to", f.flow,
  ortools.main.max_flow_value(list_filter([[0,1,20],[0,2,30],[1,2,10],[1,3,10],[2,3,20]]::BIGINT[][], lambda a: NOT (a[1] = f."from" AND a[2] = f."to")), 0, 3) AS flow_without_arc,
  ortools.main.max_flow_value([[0,1,20],[0,2,30],[1,2,10],[1,3,10],[2,3,20]]::BIGINT[][], 0, 3) - flow_without_arc AS flow_lost
FROM ortools.main.max_flow(arcs := [[0,1,20],[0,2,30],[1,2,10],[1,3,10],[2,3,20]]::BIGINT[][], source := 0, sink := 3) AS f
ORDER BY flow_lost DESC, f."from", f."to";
Link
min_cost_flow (arcs := BIGINT[][], supplies := BIGINT[][]) 2 examples

Minimum-cost flow: route supply to demand over a capacitated, costed network at minimum total cost. Backed by OR-Tools SimpleMinCostFlow. Returns one row per arc with positive flow.

Named arguments
arcs := BIGINT[][]
[from, to, capacity, unit_cost] arcs of the network. Β· default null
supplies := BIGINT[][]
[node, supply] pairs (positive = source, negative = sink; must sum to 0). Β· default null
Returns columns
from BIGINT
Arc tail node.
to BIGINT
Arc head node.
flow BIGINT
Flow sent on this arc (rows with zero flow are omitted).
cost BIGINT
Flow times unit cost on this arc.
status VARCHAR
OPTIMAL / INFEASIBLE / UNKNOWN.
Examples (2)

Min-cost flow of 20 units out of node 0 to sinks 3 (5 units) and 4 (15 units) -- the canonical OR-Tools instance, optimal total cost 150.

SELECT "from", "to", flow, cost
FROM ortools.main.min_cost_flow(arcs := [[0,1,15,4],[0,2,8,4],[1,2,20,2],[1,3,4,2],[1,4,10,6],[2,3,15,1],[2,4,4,3],[3,4,20,2],[4,2,5,3]]::BIGINT[][], supplies := [[0,20],[3,-5],[4,-15]]::BIGINT[][])
ORDER BY "from", "to";

Ship 12 units from node 0 to node 3 at minimum cost (68), and compare against the network's maximum 0->3 throughput from max_flow_value over the same arcs' capacities (17), leaving 5 units of headroom.

SELECT sum(flow) FILTER (WHERE "from" = 0) AS shipped,
  sum(cost) AS total_cost,
  ortools.main.max_flow_value(list_transform([[0,1,10,2],[0,2,8,5],[1,2,6,1],[1,3,5,4],[2,3,12,2]]::BIGINT[][], lambda a: a[1:3]), 0, 3) AS max_shippable,
  max_shippable - shipped AS headroom
FROM ortools.main.min_cost_flow(arcs := [[0,1,10,2],[0,2,8,5],[1,2,6,1],[1,3,5,4],[2,3,12,2]]::BIGINT[][], supplies := [[0,12],[3,-12]]::BIGINT[][])
ORDER BY total_cost;
Link
solve_assignment (cost := BIGINT[][], maximize := BOOLEAN) 2 examples

Linear sum assignment: match workers to tasks over sparse [worker, task, cost] arcs, minimizing (or maximizing) total cost. Backed by OR-Tools SimpleLinearSumAssignment.

Named arguments
cost := BIGINT[][]
Sparse [worker, task, cost] triples (array_agg([w, t, c])); a missing pair is a disallowed match. Β· default null
maximize := BOOLEAN
Maximize total cost instead of minimizing. Β· default false
Returns columns
worker BIGINT
Worker id from the input arcs.
task BIGINT
Task this worker is assigned to.
cost BIGINT
Arc cost of this match.
status VARCHAR
OPTIMAL, or INFEASIBLE when no perfect matching exists.
Examples (2)

Assign 3 workers to 3 tasks minimizing total cost (unique optimum 0->1, 1->0, 2->2).

SELECT worker, task, cost
FROM ortools.main.solve_assignment(cost := [[0,0,90],[0,1,76],[0,2,75],[1,0,35],[1,1,85],[1,2,55],[2,0,125],[2,1,95],[2,2,90]]::BIGINT[][], maximize := false)
ORDER BY worker;
Result
worker task cost
0 1 76
1 0 35
2 2 90

Maximize total value instead of minimizing cost.

SELECT worker, task, cost, status
FROM ortools.main.solve_assignment(cost := [[0,0,8],[0,1,7],[1,0,6],[1,1,9]]::BIGINT[][], maximize := true)
ORDER BY worker;
Link
solve_bin_packing (bin_capacity := BIGINT, random_seed := BIGINT, sizes := BIGINT[], time_limit_s := DOUBLE) 1 example

1-D bin packing: assign items to the fewest identical fixed-capacity bins. CP-SAT model minimizing the number of open bins. Returns the item -> bin assignment.

Named arguments
bin_capacity := BIGINT
Capacity of each identical bin. Β· default 0
random_seed := BIGINT
Random seed for determinism. Β· default 1
sizes := BIGINT[]
Per-item sizes [s0, s1, ...]. Β· default null
time_limit_s := DOUBLE
Wall-clock cap in seconds. Β· default 10.0
Returns columns
item BIGINT
Item index (0-based).
bin BIGINT
Bin the item is assigned to (0-based).
status VARCHAR
OPTIMAL / FEASIBLE / INFEASIBLE.
Example

Pack six items into the fewest bins of capacity 10 (optimum 3 bins).

SELECT count(DISTINCT bin) AS bins
FROM ortools.main.solve_bin_packing(sizes := [3,5,2,7,1,4]::BIGINT[], bin_capacity := 10, time_limit_s := 5.0);
Link
solve_bin_packing_bins (bin_capacity := BIGINT, random_seed := BIGINT, sizes := BIGINT[], time_limit_s := DOUBLE) 1 example

Per-bin rollup of the bin-packing solution: one row per open bin with the count of items it holds. Same inputs as solve_bin_packing.

Named arguments
bin_capacity := BIGINT
Capacity of each identical bin. Β· default 0
random_seed := BIGINT
Random seed for determinism. Β· default 1
sizes := BIGINT[]
Per-item sizes [s0, s1, ...]. Β· default null
time_limit_s := DOUBLE
Wall-clock cap in seconds. Β· default 10.0
Returns columns
bin BIGINT
Bin index (only bins holding >= 1 item).
used BIGINT
Number of items packed into the bin.
status VARCHAR
OPTIMAL / FEASIBLE / INFEASIBLE.
Example

Per-bin item counts for a six-item pack into capacity-10 bins.

SELECT bin, used
FROM ortools.main.solve_bin_packing_bins(sizes := [3,5,2,7,1,4]::BIGINT[], bin_capacity := 10)
ORDER BY bin;
Link
solve_cpsat (enumerate_all := BOOLEAN, model := VARCHAR, num_workers := BIGINT, project := VARCHAR, random_seed := BIGINT, time_limit_s := DOUBLE) 2 examples

Solve an arbitrary CP-SAT constraint-programming model expressed as a versioned JSON document (variables, constraints, objective). Returns one row per variable plus solve metadata. The general escape hatch for constraints highs/LP cannot express.

Named arguments
enumerate_all := BOOLEAN
Reserved: enumerate feasible solutions (solution-pool streaming is roadmap). Β· default false
model := VARCHAR
The CP-SAT model as a JSON document (the versioned dialect, README section A). Β· default ""
num_workers := BIGINT
Parallel CP-SAT search workers (0 = single-threaded deterministic). Β· default 0
project := VARCHAR
Optional comma-separated subset of variable names to emit (others dropped). Β· default ""
random_seed := BIGINT
Random seed for a deterministic objective value. Β· default 1
time_limit_s := DOUBLE
Wall-clock cap in seconds (clamped to the worker hard cap). Β· default 10.0
Returns columns
var VARCHAR
Variable name; intervals expand to <name>.start/.size/.end/.presence.
value BIGINT
Assigned integer value (NULL on a non-solution status row).
status VARCHAR
OPTIMAL/FEASIBLE/INFEASIBLE/MODEL_INVALID/UNKNOWN.
objective DOUBLE
Objective value (NULL if no objective / no solution).
best_bound DOUBLE
Best objective bound (the other end of the gap).
wall_time DOUBLE
Solver wall-clock time in seconds.
num_branches BIGINT
Number of search branches explored.
Examples (2)

0/1 knapsack as a CP-SAT model: maximize value within a weight budget (optimum 40).

SELECT var, value, status, objective
FROM ortools.main.solve_cpsat(model := '{"version":1,"variables":[{"kind":"bool","name":"a"},{"kind":"bool","name":"b"},{"kind":"bool","name":"c"}],"constraints":[{"type":"linear","expr":{"sum":[{"coef":5,"var":"a"},{"coef":10,"var":"b"},{"coef":15,"var":"c"}]},"op":"<=","rhs":20}],"objective":{"sense":"maximize","terms":[{"coef":10,"var":"a"},{"coef":20,"var":"b"},{"coef":30,"var":"c"}]}}')
ORDER BY var;

Single-machine no-overlap schedule minimizing makespan (optimum 10).

SELECT objective, status
FROM ortools.main.solve_cpsat(model := '{"version":1,"variables":[{"kind":"int","name":"s0","lb":0,"ub":20},{"kind":"int","name":"e0","lb":0,"ub":20},{"kind":"int","name":"s1","lb":0,"ub":20},{"kind":"int","name":"e1","lb":0,"ub":20},{"kind":"int","name":"s2","lb":0,"ub":20},{"kind":"int","name":"e2","lb":0,"ub":20},{"kind":"interval","name":"i0","start":{"var":"s0"},"size":{"const":3},"end":{"var":"e0"}},{"kind":"interval","name":"i1","start":{"var":"s1"},"size":{"const":5},"end":{"var":"e1"}},{"kind":"interval","name":"i2","start":{"var":"s2"},"size":{"const":2},"end":{"var":"e2"}},{"kind":"int","name":"makespan","lb":0,"ub":20}],"constraints":[{"type":"no_overlap","intervals":["i0","i1","i2"]},{"type":"max_equality","target":{"var":"makespan"},"exprs":[{"var":"e0"},{"var":"e1"},{"var":"e2"}]}],"objective":{"sense":"minimize","terms":[{"coef":1,"var":"makespan"}]}}', time_limit_s := 5.0)
LIMIT 1;
Link
solve_jobshop (horizon := BIGINT, random_seed := BIGINT, tasks := BIGINT[][], time_limit_s := DOUBLE) 1 example

Job-shop scheduling: schedule the tasks of several jobs on shared machines (tasks of a job run in order, one task per machine at a time) to minimize the makespan. CP-SAT.

Named arguments
horizon := BIGINT
Scheduling horizon upper bound (0 = sum of durations). Β· default 0
random_seed := BIGINT
Random seed for a deterministic makespan. Β· default 1
tasks := BIGINT[][]
[job, task, machine, duration] operation rows. Β· default null
time_limit_s := DOUBLE
Wall-clock cap in seconds. Β· default 30.0
Returns columns
job BIGINT
Job id.
task BIGINT
Task index within the job.
machine BIGINT
Machine the task runs on.
start BIGINT
Scheduled start time.
end BIGINT
Scheduled end time (start + duration).
makespan BIGINT
Optimal makespan (repeated on every row).
status VARCHAR
OPTIMAL / FEASIBLE / INFEASIBLE / UNKNOWN.
Example

Two-job, two-machine job shop minimizing makespan (optimum 7).

SELECT job, task, machine, start, "end", makespan
FROM ortools.main.solve_jobshop(tasks := [[0,0,0,3],[0,1,1,2],[1,0,1,2],[1,1,0,4]]::BIGINT[][], time_limit_s := 5.0)
ORDER BY job, task;
Link
solve_knapsack (capacities := BIGINT[], item_values := BIGINT[], weights := BIGINT[][]) 2 examples

Multi-dimensional 0/1 knapsack: choose items to maximize total value subject to one or more capacity dimensions. Backed by OR-Tools KnapsackSolver (branch and bound).

Named arguments
capacities := BIGINT[]
Per-dimension capacities [c_d0, ...]. Β· default null
item_values := BIGINT[]
Per-item values [v0, v1, ...]. Β· default null
weights := BIGINT[][]
Per-item per-dimension weights [[w0_d0, w0_d1, ...], ...]. Β· default null
Returns columns
item BIGINT
Item index (0-based).
chosen BOOLEAN
True if the item is packed into the knapsack.
status VARCHAR
OPTIMAL / FEASIBLE / INFEASIBLE.
Examples (2)

Single-dimension 0/1 knapsack: pick items maximizing value within capacity 40 (optimum 80).

SELECT item, chosen
FROM ortools.main.solve_knapsack(item_values := [10,20,30,40,50]::BIGINT[], weights := [[5],[10],[15],[20],[25]]::BIGINT[][], capacities := [40]::BIGINT[])
ORDER BY item;

Two-dimensional knapsack: each item has a weight and a volume, packed under both a weight budget (50) and a volume budget (5) at once -- one `weights` column per dimension.

SELECT item, chosen, status
FROM ortools.main.solve_knapsack(item_values := [60,100,120]::BIGINT[], weights := [[10,2],[20,3],[30,4]]::BIGINT[][], capacities := [50,5]::BIGINT[])
ORDER BY item;
Link
solve_rcpsp (precedences := BIGINT[][], random_seed := BIGINT, resources := BIGINT[][], tasks := BIGINT[][], time_limit_s := DOUBLE) 1 example

Resource-constrained project scheduling (RCPSP): schedule tasks with durations, precedences, and per-resource capacity (renewable) to minimize makespan. CP-SAT cumulative.

Named arguments
precedences := BIGINT[][]
[pred, succ] finish-to-start precedences. Β· default null
random_seed := BIGINT
Random seed for a deterministic makespan. Β· default 1
resources := BIGINT[][]
[resource, capacity] rows. Β· default null
tasks := BIGINT[][]
[task, duration, r0_demand, r1_demand, ...] rows (one demand column per resource). Β· default null
time_limit_s := DOUBLE
Wall-clock cap in seconds. Β· default 30.0
Returns columns
task BIGINT
Task id.
start BIGINT
Scheduled start time.
end BIGINT
Scheduled end time (start + duration).
makespan BIGINT
Optimal makespan (repeated on every row).
status VARCHAR
OPTIMAL / FEASIBLE / INFEASIBLE / UNKNOWN.
Example

Four-task project, one resource of capacity 2, with two precedences.

SELECT task, start, "end", makespan, status
FROM ortools.main.solve_rcpsp(tasks := [[0,3,2],[1,2,1],[2,4,2],[3,2,1]]::BIGINT[][], precedences := [[0,2],[1,3]]::BIGINT[][], resources := [[0,2]]::BIGINT[][], time_limit_s := 5.0)
ORDER BY task;
Link
solve_tsp (distance := BIGINT[][], first_solution_strategy := VARCHAR, local_search_metaheuristic := VARCHAR, random_seed := BIGINT, start := BIGINT, time_limit_s := DOUBLE) 2 examples

Travelling salesperson: shortest single-vehicle tour visiting every node once and returning to the start, over a distance matrix. OR-Tools routing library.

Named arguments
distance := BIGINT[][]
Distance matrix: sparse [from, to, meters] triples, or a dense n x n matrix. Β· default null
first_solution_strategy := VARCHAR
OR-Tools first-solution strategy name. Β· default ""
local_search_metaheuristic := VARCHAR
OR-Tools local-search metaheuristic name. Β· default ""
random_seed := BIGINT
Reserved (routing determinism is the fixed strategy). Β· default 1
start := BIGINT
Start (and return) node of the tour. Β· default 0
time_limit_s := DOUBLE
Wall-clock cap in seconds. Β· default 5.0
Returns columns
seq INTEGER
0-based position in the tour.
node BIGINT
Node id visited at this position.
arrival BIGINT
Cumulative distance at arrival.
status VARCHAR
OPTIMAL / INFEASIBLE / UNKNOWN / MODEL_INVALID.
Examples (2)

5-node TSP from a dense distance matrix; shortest tour length 17.

SELECT seq, node, arrival
FROM ortools.main.solve_tsp(distance := [[0,2,9,10,1],[1,0,6,4,8],[15,7,0,8,3],[6,3,12,0,5],[9,8,4,7,0]]::BIGINT[][], start := 0, time_limit_s := 1.0)
ORDER BY seq;

Same problem from the sparse `[from, to, meters]` edge-list form (build it with `array_agg([f, t, m])`) instead of a dense matrix -- here a complete 4-node graph.

SELECT seq, node, arrival
FROM ortools.main.solve_tsp(distance := [[0,1,1],[0,2,4],[0,3,2],[1,0,1],[1,2,1],[1,3,5],[2,0,4],[2,1,1],[2,3,1],[3,0,2],[3,1,5],[3,2,1]]::BIGINT[][], start := 0, time_limit_s := 1.0)
ORDER BY seq;
Link
solve_vrp (allow_dropping := BOOLEAN, demand := BIGINT[][], depot := BIGINT, distance := BIGINT[][], drop_penalty := BIGINT, first_solution_strategy := VARCHAR, lns_time_limit_s := DOUBLE, local_search_metaheuristic := VARCHAR, num_vehicles := BIGINT, pickup_delivery := BIGINT[][], random_seed := BIGINT, service_time := BIGINT[][], settings_json := VARCHAR, solution_limit := BIGINT, span_cost_coefficient := BIGINT, time_limit_s := DOUBLE, time_windows := BIGINT[][], travel_time := BIGINT[][], vehicle_capacity := BIGINT) 1 example

Vehicle routing problem: route a fleet from a depot to serve stops at minimum total distance, with optional capacity dimensions, time windows, service/travel time, pickup-delivery pairs, droppable stops, and route balancing. OR-Tools routing library.

Named arguments
allow_dropping := BOOLEAN
Allow skipping infeasible stops (with a penalty). Β· default false
demand := BIGINT[][]
[node, d0, d1, ...] one demand column per capacity dimension. Β· default null
depot := BIGINT
Shared start/end depot node. Β· default 0
distance := BIGINT[][]
Distance matrix: sparse [from, to, meters] or dense n x n. Β· default null
drop_penalty := BIGINT
Uniform per-node drop penalty (required if allow_dropping). Β· default 0
first_solution_strategy := VARCHAR
OR-Tools first-solution strategy name. Β· default ""
lns_time_limit_s := DOUBLE
LNS sub-solve cap (0 = unset). Β· default 0.0
local_search_metaheuristic := VARCHAR
OR-Tools local-search metaheuristic name. Β· default ""
num_vehicles := BIGINT
Fleet size. Β· default 1
pickup_delivery := BIGINT[][]
[pickup_node, delivery_node] same-vehicle pairs. Β· default null
random_seed := BIGINT
Reserved (routing determinism is the fixed strategy). Β· default 1
service_time := BIGINT[][]
[node, secs] dwell time per node. Β· default null
settings_json := VARCHAR
JSON escape for advanced config: per-vehicle/per-dimension capacities, per-vehicle depots. Β· default ""
solution_limit := BIGINT
Stop after N improving solutions (0 = unset). Β· default 0
span_cost_coefficient := BIGINT
Per-vehicle route-span penalty (balances routes). Β· default 0
time_limit_s := DOUBLE
Wall-clock cap in seconds. Β· default 10.0
time_windows := BIGINT[][]
[node, open, close] arrival windows. Β· default null
travel_time := BIGINT[][]
[from, to, secs] travel time; defaults to distance. Β· default null
vehicle_capacity := BIGINT
Shared capacity for dimension 0 (-1 = unset). Β· default -1
Returns columns
route_id INTEGER
Vehicle index (NULL on a dropped-node row).
seq INTEGER
0-based stop order on the vehicle.
node BIGINT
Node id (depot at seq 0 and last).
arrival BIGINT
Cumulative time/distance at arrival.
departure BIGINT
Arrival + service time.
load_0 BIGINT
Cumulative load for capacity dimension 0.
load_k BIGINT
Cumulative load for capacity dimension k (one load_d column per dimension).
is_dropped BOOLEAN
True on the row emitted for a dropped node.
status VARCHAR
OPTIMAL / FEASIBLE / INFEASIBLE / UNKNOWN / MODEL_INVALID.
Returns columns Β· depends on the arguments
route_id INTEGER
Vehicle index (NULL on a dropped-node row).
seq INTEGER
0-based stop order on the vehicle.
node BIGINT
Node id (depot at seq 0 and last).
arrival BIGINT
Cumulative time/distance at arrival.
departure BIGINT
Arrival + service time.
load_0 BIGINT
Cumulative load for capacity dimension 0.
load_k BIGINT
Cumulative load for capacity dimension k (one load_d column per dimension).
is_dropped BOOLEAN
True on the row emitted for a dropped node.
status VARCHAR
OPTIMAL / FEASIBLE / INFEASIBLE / UNKNOWN / MODEL_INVALID.
Example

Capacitated VRP: 1 depot, 3 stops of demand 1, 2 vehicles of capacity 2 (optimal distance 6).

SELECT route_id, seq, node, arrival, load_0
FROM ortools.main.solve_vrp(distance := [[0,1,1],[0,2,1],[0,3,2],[1,0,1],[1,2,1],[1,3,2],[2,0,1],[2,1,1],[2,3,1],[3,0,2],[3,1,2],[3,2,1]]::BIGINT[][], demand := [[1,1],[2,1],[3,1]]::BIGINT[][], num_vehicles := 2, vehicle_capacity := 2, depot := 0, time_limit_s := 1.0)
ORDER BY route_id, seq;
Link

Try these queries

Optimal travelling-salesperson tour over a 5-node distance matrix, in visit order.

SELECT seq, node, arrival
FROM ortools.main.solve_tsp(distance := [[0,2,9,10,1],[1,0,6,4,8],[15,7,0,8,3],[6,3,12,0,5],[9,8,4,7,0]]::BIGINT[][], start := 0)
ORDER BY seq

Uses solve_tsp

List the bundled OR-Tools / solver component versions.

SELECT component, version FROM ortools.main.solver_info ORDER BY component

Uses solver_info

Arc criticality: for each arc carrying flow in the 0->3 max-flow solution, re-solve with that arc removed (max_flow_value) and report the throughput lost. Losing arc 2->3 drops the max flow from 30 to 10; each of the other flow-carrying arcs costs 10.

SELECT f."from", f."to", f.flow,
  ortools.main.max_flow_value(list_filter([[0,1,20],[0,2,30],[1,2,10],[1,3,10],[2,3,20]]::BIGINT[][], lambda a: NOT (a[1] = f."from" AND a[2] = f."to")), 0, 3) AS flow_without_arc,
  ortools.main.max_flow_value([[0,1,20],[0,2,30],[1,2,10],[1,3,10],[2,3,20]]::BIGINT[][], 0, 3) - flow_without_arc AS flow_lost
FROM ortools.main.max_flow(arcs := [[0,1,20],[0,2,30],[1,2,10],[1,3,10],[2,3,20]]::BIGINT[][], source := 0, sink := 3) AS f
ORDER BY flow_lost DESC, f."from", f."to"

Uses max_flow_valuemax_flow

Read every metric of the most recent solve on this worker.

SELECT metric, value FROM ortools.main.last_solve_stats ORDER BY metric

Uses last_solve_stats

Run a solve, then read back the statistics of that solve.

SELECT count(*) FROM ortools.main.solve_assignment(cost := [[0,0,1],[1,1,1]]::BIGINT[][], maximize := false);
SELECT metric, value FROM ortools.main.last_solve_stats ORDER BY metric;

Uses solve_assignmentlast_solve_stats

Capacitated VRP: 1 depot, 3 stops of demand 1, 2 vehicles of capacity 2 (optimal distance 6).

SELECT route_id, seq, node, arrival, load_0
FROM ortools.main.solve_vrp(distance := [[0,1,1],[0,2,1],[0,3,2],[1,0,1],[1,2,1],[1,3,2],[2,0,1],[2,1,1],[2,3,1],[3,0,2],[3,1,2],[3,2,1]]::BIGINT[][], demand := [[1,1],[2,1],[3,1]]::BIGINT[][], num_vehicles := 2, vehicle_capacity := 2, depot := 0, time_limit_s := 1.0)
ORDER BY route_id, seq

Uses solve_vrp

17 more examples in the Catalog, each listed under the table or function it uses.

Questions it can answer

Plain-English questions from the worker's agent test tasks. An AI agent with this catalog attached should answer each one with SQL.

  • Pack items of sizes [3,5,2,7,1,4] into bins that each hold at most 10. What is the fewest number of bins needed? Return a single number.
  • Pack items of sizes [3,5,2,7,1,4] into bins of capacity 10. Using the per-bin rollup, how many bins end up holding at least one item? Return a single number.
  • Which version of Google OR-Tools does this worker bundle? Find the version string it reports for its OR-Tools engine.
  • Run any solve (for example a small assignment), then inspect the statistics of the most recent solve this worker handled and report that solve's status.

OR-Tools is an independent open-source connector from Query.Farm. It is not affiliated with, endorsed by or sponsored by the services, projects or organisations it works with. Product names, logos and trademarks belong to their respective owners.