Skip to main content

Class: Solution

Remarks

Solution of a Model. When a model is solved, the solution is stored in this object. The solution contains values of all variables in the model (including optional variables) and the value of the objective (if the model specified one).

Preview version of OptalCP

Note that in the preview version of OptalCP, the values of variables in the solution are masked and replaced by value absent (null).

Constructors

new Solution()

new Solution(): Solution

Creates an empty solution.

Returns

Solution

Remarks

Creates a solution where all variables are absent, and the objective value is None/undefined.

Use this constructor to create an external solution that can be passed to the solver as a warm start (see Model.solve) or sent during solving using Solver.sendSolution.

import * as CP from "optalcp";

const model = new CP.Model();
const x = model.intervalVar({ length: 10, name: "x" });
model.minimize(x.end());

// Create an external solution
const solution = new CP.Solution();
solution.setValue(x, 0, 10); // x starts at 0, ends at 10

// Use it as a warm start
const result = await model.solve({ timeLimit: 60 }, solution);

Other

getEnd()

getEnd(variable: IntervalVar): null | number

Gets an interval variable's end time from the solution.

Parameters

ParameterTypeDescription
variableIntervalVarThe interval variable to query

Returns

null | number

The end value, or None if absent

Remarks

If the variable is absent in the solution, it returns null.

In the preview version of OptalCP, this function always returns null because real values of variables are masked and replaced by value absent.


getLength()

getLength(variable: IntervalVar): null | number

Gets an interval variable's length from the solution.

Parameters

ParameterTypeDescription
variableIntervalVarThe interval variable to query

Returns

null | number

The length value, or None if absent

Remarks

If the variable is absent in the solution, it returns null.

In the preview version of OptalCP, this function always returns null because real values of variables are masked and replaced by value absent.


getObjective()

getObjective(): ObjectiveValue

Returns the objective value of the solution.

Returns

ObjectiveValue

The objective value

Remarks

Returns null if no objective was specified or if the objective expression is absent (see optional IntExpr).

The correct value is reported even in the preview version of OptalCP.


getStart()

getStart(variable: IntervalVar): null | number

Gets an interval variable's start time from the solution.

Parameters

ParameterTypeDescription
variableIntervalVarThe interval variable to query

Returns

null | number

The start value, or None if absent

Remarks

If the variable is absent in the solution, it returns null.

In the preview version of OptalCP, this function always returns null because real values of variables are masked and replaced by value absent.


getValue()

Call Signature

getValue(variable: IntVar): null | number

Gets an integer variable's value from the solution.

Parameters
ParameterTypeDescription
variableIntVarThe integer variable to get the value of
Returns

null | number

The value, or None if the variable is absent

Call Signature

getValue(variable: BoolVar): null | boolean

Gets a boolean variable's value from the solution.

Parameters
ParameterTypeDescription
variableBoolVarThe boolean variable to get the value of
Returns

null | boolean

The value, or None if the variable is absent

Call Signature

getValue(variable: IntervalVar): null | { end: number; start: number; }

Gets an interval variable's start and end from the solution.

Parameters
ParameterTypeDescription
variableIntervalVarThe interval variable to get the value of
Returns

null | { end: number; start: number; }

An object with start and end properties, or null if the interval is absent


isAbsent()

isAbsent(variable: IntVar | BoolVar | IntervalVar): boolean

Returns true if the given variable is absent in the solution.

Parameters

ParameterTypeDescription
variableIntVar | BoolVar | IntervalVarThe variable to check

Returns

boolean

True if the variable is absent

Remarks

In the preview version of OptalCP, this method always returns true because real values of variables are masked and replaced by value absent.


setAbsent()

setAbsent(variable: IntVar | BoolVar | IntervalVar): void

Sets the given variable to be absent in the solution.

Parameters

ParameterTypeDescription
variableIntVar | BoolVar | IntervalVarThe variable to set as absent

Returns

void

Remarks

This function can be used for construction of an external solution that can be passed to the solver (see Model.solve and Solver.sendSolution).


setObjective()

setObjective(value: ObjectiveValue): void

Sets objective value of the solution.

Parameters

ParameterTypeDescription
valueObjectiveValueThe objective value to set

Returns

void

Remarks

This function can be used for construction of an external solution that can be passed to the solver (see Model.solve, Solver and Solver.sendSolution).


setValue()

Call Signature

setValue(variable: BoolVar, value: boolean): void

Sets the value of the given boolean variable in the solution.

Parameters
ParameterTypeDescription
variableBoolVarThe boolean variable
valuebooleanThe value to set (True or False)
Returns

void

Remarks

The variable will be present in the solution with the given value.

See

Solution.setAbsent to make the variable absent.

Call Signature

setValue(variable: IntVar, value: number): void

Sets the value of the given integer variable in the solution.

Parameters
ParameterTypeDescription
variableIntVarThe integer variable
valuenumberThe value to set
Returns

void

Remarks

The variable will be present in the solution with the given value.

See

Solution.setAbsent to make the variable absent.

Call Signature

setValue(variable: IntervalVar, start: number, end: number): void

Sets the start and end of the given interval variable in the solution.

Parameters
ParameterTypeDescription
variableIntervalVarThe interval variable
startnumberThe start time
endnumberThe end time
Returns

void

Remarks

The interval variable will be present in the solution with the given start and end.

See

Solution.setAbsent to make the variable absent.