Loading solver…
MPSolver - frontend sample
Problem: solve one small linear model through MPSolver while switching between available LP and integer backends.
Show code
import { MPSolver } from 'or-tools-wasm/mp-solver';
const solver = MPSolver.createSolver('GLOP');
if (!solver) throw new Error('GLOP is unavailable');
solver.setNumThreads(4);
const x = solver.addNumVariable(0, 1, 'x');
const y = solver.addNumVariable(0, 1, 'y');
const c0 = solver.addConstraint(-solver.infinity(), 1);
c0.setCoefficient(x, 1);
c0.setCoefficient(y, 1);
const objective = solver.objective();
objective.setCoefficient(x, 2);
objective.setCoefficient(y, 1);
objective.setMaximization();
await solver.solveWithProto({ executor: 'worker' });
console.log(x.solutionValue(), y.solutionValue());
MPSolver routes one tiny linear model through selectable backends. GLOP, CLP, and GLPK_LP solve it as a continuous LP; SAT, GLPK, SCIP, CBC, BOP, and Knapsack solve the same model with integral variables.
Model
- The model has two variables, one capacity constraint, and one linear objective.
- The solver menu changes which MPSolver backend receives the model.
- LP backends solve continuous variables; integer backends solve integral variables.
- The result compares objective value, variable values, iterations, and branch-and-bound nodes.
Run the solver to view the solution.
Status / Response: