JavaScript/WebAssembly port of GLPK (GNU Linear Programming Kit) for browser & node. Rather than porting the complete GLPK library (including GLPSOL) this project aims at creating a simple JSON interface to setup and solve LP/MILP with JavaScript.
npm install glpk.jsMinimal live example: https://jvail.github.io/glpk.js/examples/lp.html
import GLPK from 'glpk.js';
const glpk = await GLPK();
const options = {
msglev: glpk.GLP_MSG_ALL,
presol: true
};
const result = await glpk.solve({
name: 'LP',
objective: {
direction: glpk.GLP_MAX,
name: 'obj',
vars: [
{ name: 'x1', coef: 0.6 },
{ name: 'x2', coef: 0.5 }
]
},
subjectTo: [
{
name: 'cons1',
vars: [
{ name: 'x1', coef: 1.0 },
{ name: 'x2', coef: 2.0 }
],
bnds: { type: glpk.GLP_UP, ub: 1.0, lb: 0.0 }
},
{
name: 'cons2',
vars: [
{ name: 'x1', coef: 3.0 },
{ name: 'x2', coef: 1.0 }
],
bnds: { type: glpk.GLP_UP, ub: 2.0, lb: 0.0 }
}
]
}, options);glpk.js and Mixed-Integer Programming with a lot of background information:
Simple LP in the browser:
Large LP with simplex callback (600x600 transportation problem):
Some (slightly outdated) examples using glpk.js:
interface LP {
name: string,
objective: {
direction: number,
name: string,
vars: { name: string, coef: number }[]
},
subjectTo: {
name: string,
vars: { name: string, coef: number }[],
bnds: { type: number, ub: number, lb: number }
}[],
bounds?: {
name: string,
type: number,
ub: number,
lb: number
}[],
binaries?: string[],
generals?: string[],
options?: Options
}Optionally the "kind of structural variable"
- continuous variable (default)
- integer variable
- binary variable
may be specified with an array of variable names:
/* integer */
lp.generals = ['x1', 'x2'];
/* binary */
lp.binaries = ['x3', 'x4'];For full TypeScript definitions, see:
- src/types.d.ts - shared types (
LP,Options,Result) - src/index.d.ts - browser async API (default export)
- src/glpk.d.ts - Node.js sync API (
glpk.js/node)
Built with emsdk 4.0.x.
git clone https://github.com/jvail/glpk.js.git
cd glpk.js
git submodule update --init --recursive
npm install
source ~/emsdk/emsdk_env.sh
npm run build && npm run testUses official docker images for emscripten/emsdk.
docker build . -t glpk.js
docker run -v $PWD:/app glpk.js