@revolugo/ortools
A node.js wrapper for Google OR-Tools.
Current implementation only wraps integer programming necessary classes and methods. It exposes a Promise interface for the solver.solve()
method.
Supported features
The following OR-Tools C++ Library values, classes and methods are supported.
note : corresponding javascript class methods are camelCase-d (e.g. makeIntVar()
)
MPSolver
Supported optimization problem type:
MPSolver::CBC_MIXED_INTEGER_PROGRAMMING
Methods:
MPSolver::Name()
MPSolver::NumVariables()
MPSolver::NumConstraints()
MPSolver::MakeIntVar()
MPSolver::MakeRowConstraint()
MPSolver::MakeMutableObjective()
MPSolver::Solve()
MPSolver::infinity()
MPSolver::wall_time()
MPSolver::iterations()
MPSolver::nodes()
MPVariable
Methods:
MPVariable::Name()
MPVariable::solution_value()
MPConstraint
Methods:
MPConstraint::SetCoefficient()
MPObjective
Methods:
MPObjective::SetCoefficient()
MPObjective::SetMaximization()
MPObjective::SetMinimization()
MPObjective::Value()
Installation
npm install @revolugo/ortools --save
note: the package includes a script that detects your platform (OS/version) and downloads the right binaries and include files from Google OR-Tools repository releases. Building the module requires standard C/C++ build tools and zlib. Supported platforms are currently:
- Ubuntu 16.04, 18.04
- CentOS 7
- Debian 9
- Mac OS X
Usage
const ortools = require('@revolugo/ortools')
const solver = new ortools.MPSolver('My solver', ortools.MPSolver.CBC_MIXED_INTEGER_PROGRAMMING)
const x = solver.makeIntVar(0, solver.infinity(), 'x')
const y = solver.makeIntVar(0, solver.infinity(), 'y')
const c0 = solver.makeRowConstraint(-solver.infinity(), 17.5)
c0.setCoefficient(x, 1)
c0.setCoefficient(y, 7)
const c1 = solver.makeRowConstraint(-solver.infinity(), 3.5)
c1.setCoefficient(x, 1)
c1.setCoefficient(y, 0)
const objective = solver.mutableObjective()
objective.setCoefficient(x, 1)
objective.setCoefficient(y, 10)
objective.setMaximization()
;(async () => {
const result = await solver.solve()
if (result === ortools.MPSolver.RESULT_OPTIMAL) {
console.log('Solution:')
console.log(`x = ${ x.solution_value() }`)
console.log(`y = ${ y.solution_value() }`)
console.log(`objective = ${ objective.value() }`)
}
else {
console.log(`Non optimal result ${ result }!`)
}
})();