Skip to main content

Installation

This page describes the installation of OptalCP.

Prerequisites

You need Node.js and npm installed (npm is bundled with Node.js). You can check if you have them installed by running:

node --version
npm --version

Start a new JavaScript project

If you don't have a JavaScript project yet, you can create a new one by running:

mkdir myproject
cd myproject
npm init

This will create a new directory myproject and initialize a new Node.js project in it. You will be asked a few questions, and a package.json file will be created in the directory.

Install OptalCP

You can install the preview version of OptalCP in your project by running:

npm install 'scheduleopt/optalcp-js-bin-preview#latest'

Or, if you have access to the full version, then you can install it by running:

npm install 'scheduleopt/optalcp-js-bin#latest'

Those commands install the following JavaScript packages into your project (into the directory myproject/node_modules):

  • @scheduleopt/optalcp: JavaScript API for OptalCP.
  • @scheduleopt/optalcp-bin-preview or @scheduleopt/optalcp-bin: binary (executable) of OptalCP.

The packages are installed from the following GitHub repositories:

  • optalcp-js: JavaScript API for OptalCP (module @scheduleopt/optalcp).
  • optalcp-js-bin-preview: OptalCP binaries for JavaScript( preview version, module @scheduleopt/optalcp-bin-preview).
  • optalcp-js-bin: OptalCP binaries for JavaScript (module @scheduleopt/optalcp-bin). Restricted access.

Testing the installation

To test the installation, run:

npx optalcp --version

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

Writing the first program

Now is the time you can start writing your code. For a TypeScript project, see Install TypeScript below.

Create a file index.mjs with the following content:

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

async function main() {
let model = new CP.Model();
let x = model.intervalVar({ name: 'x', length : 10});
let y = model.intervalVar({ name: 'y', length : 10});
model.endBeforeStart(x, y);
model.minimize(y.end());

let result = await CP.solve(model, { nbWorkers: 1 });
if (result.bestSolution) {
console.log("x start: " + result.bestSolution.getStart(x));
console.log("y start: " + result.bestSolution.getStart(y));
} else
console.log("No solution found");
}

main();
info

The file above uses the new import syntax (ECMAScript 2015) instead of the old require (CommonJS). OptalCP can be used with both. If you prefer require syntax, then replace the first line with:

const CP = require('@scheduleopt/optalcp');

and use .js extension for the file instead of .mjs.

In this documentation, we always use import syntax.

To run the code, use:

node index.mjs

You will get an output similar to this one:

--------------------------------------------------------------------------------                             ScheduleOpt OptalCP                          version 0.9.2.1 (gd75977e6)                   CPU: AMD Ryzen 9 5950X (16 physical cores) -------------------------------------------------------------------------------- Input parse time: 00:00 Parameters:   NbWorkers = 1 Solver input:   0 integer variables, 2 interval variables, 2 constraints, 9.1kB   00:00 Presolving..   00:00 Starting search using 1 worker (nbWorkers parameter). -------------------------------------------------------------------------------- Presolved:   0 integer variables, 2 interval variables, 2 constraints, 9.21kB   00:00 Lower bound 20 Worker 0   00:00 Solution 20 [Verified] Worker 0   00:00 Current best solution is optimal. --------------------------------------------------------------------------------   Objective value: 20 (optimal)       Lower bound: 20         Solutions: 1         LNS steps: 9 (5646.23 per second)          Restarts: 0 (0.00 per second)          Branches: 19 (11919.83 per second)             Fails: 9 (5646.23 per second)    Total duration: 00:00.00            Memory: 565kB -------------------------------------------------------------------------------- x start: 0 y start: 10

Developing in TypeScript

If you want to develop in TypeScript, you must install TypeScript in your project. You can do it by running:

npm install typescript --save-dev
npx tsc --init

Now, we can develop in TypeScript. Let's create a file index.ts with the following content:

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

async function main() {
let model = new CP.Model();
let x = model.intervalVar({ name: 'x', length : 10});
let y = model.intervalVar({ name: 'y', length : 10});
model.endBeforeStart(x, y);
model.minimize(y.end());

let result = await CP.solve(model, { nbWorkers: 1, color: "Always" });
if (result.bestSolution) {
console.log("x start: " + result.bestSolution.getStart(x));
console.log("y start: " + result.bestSolution.getStart(y));
} else
console.log("No solution found");
}

main();

TypeScript code needs to be compiled into JavaScript before it can be run. To compile the code, run:

npx tsc

This will create a file index.js with the compiled code. Now you can run it:

node index.js

The output will be the same as before with the pure JavaScript code.