Skip to main content

Setting up your project with OptalCP

This page describes how to set up your project with OptalCP. It is assumed that you have already installed Node.js and OptalCP. If not, see Installation.

Create a project

Let's say you want to create my-optalcp-project with OptalCP. Let's start the same way you would start any Node.js project:

mkdir my-optalcp-project
cd my-optalcp-project
npm init

You will be asked a few questions about your project. You can just press enter to accept the default values, your answers can be changed later in the generated file package.json.

Add OptalCP to your project

To be able to use @scheduleopt/optalcp module in your project, you need to add it as a dependency. OptalCP installation includes the module in a file scheduleopt-optalcp-<version>.tgz (where <version> is the version of OptalCP).

To add the dependency (install the module) run:

npm install OPTALCP_INSTALL_DIR/scheduleopt-optalcp-<version>.tgz --save-dev

where OPTALCP_INSTALL_DIR is the directory where OptalCP is installed (see Installation). The module will install to the directory node_modules in your project.

In addition, your project needs to know where optalcp executable is. There are multiple ways:

  1. Add OPTALCP_INSTALL_DIR/bin to your PATH environment variable.
  2. Create environment variable OPTALCP_SOLVER with the path to optalcp executable, including the executable name.
  3. Specify parameter solverPath in your code.
  4. Let users set the path on the command line using the --solverPath parameter (see e.g. function parseParameters).

Write your JavaScript code

If you want to develop in JavaScript then 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();
note

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 should 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 then you need to install TypeScript in your project first. 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

You should get again the same output as before in section Write your JavaScript code.