Skip to main content

JavaScript Quick Start

Get OptalCP running in JavaScript/TypeScript in under 5 minutes.

Prerequisites

  • Bun 1.0 or later (recommended), or
  • Node.js 20 or later with npm

Why Bun? Bun is faster than Node.js and runs TypeScript files directly—no need to install a TypeScript compiler. You can run the TypeScript examples as-is.

Installation

Install the Preview edition (free for evaluation):

bun add scheduleopt/optalcp-js-bin-preview#latest
tip

For academic research or production use, see Editions for other options.

To verify the installation, run:

bunx optalcp --version

This prints the version of the JavaScript API, the solver version, and the path to the solver binary.

Your First Scheduling Model

Create a file test-optalcp.mjs:

import * as CP from '@scheduleopt/optalcp';

const model = new CP.Model();
const x = model.intervalVar({ length: 10, name: "x" });
const y = model.intervalVar({ length: 10, name: "y" });
model.endBeforeStart(x, y); // y starts after x ends
model.minimize(y.end());
const result = await model.solve();
console.log(`Objective: ${result.objective}`);

Run it:

bun test-optalcp.mjs

You'll see the solver log first, then the output from your program:

────────────────────────────────────────────────────────────────────────────────
                              ScheduleOpt OptalCP
                           Version 2025.12.1 (Linux)
                   CPU: AMD Ryzen 9 5950X (16 physical cores)
────────────────────────────────────────────────────────────────────────────────
Input parse time: 00:00
   nbWorkers = 16                      (auto: 16 physical cores)
   preset = Default                    (auto: < 100,000 variables)
   noOverlapPropagationLevel = 4       (preset: Default)
   cumulPropagationLevel = 3           (preset: Default)
     Workers 0-7: searchType = LNS     (preset: Default)
    Workers 8-13: searchType = FDS     (preset: Default)
   Workers 14-15: searchType = FDSDual (preset: Default)
Input:
   0 integer variables, 2 interval variables, 1 constraints, 9.38kB
   00:00 Presolving..
Presolved:
   0 integer variables, 2 interval variables, 1 constraints, 9.37kB
   00:00 Presolve complete, starting search.
────────────────────────────────────────────────────────────────────────────────
   00:00  Lower bound 20 Worker 8
   00:00 ↓ Solution 20 Worker 8: FDS
   00:00 The current best solution is optimal.
────────────────────────────────────────────────────────────────────────────────
   Objective value: 20 (optimal)
       Lower bound: 20
         Solutions: 1
         LNS steps: 0 (0.00 per second)
          Restarts: 0 (0.00 per second)
          Branches: 156 (8706.41 per second)
             Fails: 10 (558.10 per second)
    Total duration: 00:00.02
            Memory: 146MB
────────────────────────────────────────────────────────────────────────────────
Objective: 20

CommonJS vs ESM

OptalCP supports both module systems:

import * as CP from '@scheduleopt/optalcp';

TypeScript Setup

Setting up TypeScript compilation

While Bun runs TypeScript files directly, setting up the TypeScript compiler is useful for type checking and catching errors early. The example below is identical to the JavaScript version above—TypeScript types are inferred automatically from the OptalCP API.

Install TypeScript in your project:

bun add -d typescript
bunx tsc --init

Create a file test-optalcp.mts:

import * as CP from '@scheduleopt/optalcp';

async function main() {
const model = new CP.Model();
const x = model.intervalVar({ length: 10, name: "x" });
const y = model.intervalVar({ length: 10, name: "y" });
model.endBeforeStart(x, y); // y starts after x ends
model.minimize(y.end());
const result = await model.solve();
console.log(`Objective: ${result.objective}`);
}

main();

Type-check and compile:

bunx tsc                   # Type-check and compile
bun test-optalcp.mts # Run directly (no compilation needed)

Next Steps

  • Tutorial — Learn scheduling concepts step-by-step
  • Editions — Compare Preview, Academic, and Full editions
  • TypeScript API — Complete JavaScript/TypeScript API documentation

See also