Skip to main content

Class: BoolVar

Remarks

Boolean variable represents an unknown truth value (True or False) that the solver must find.

Boolean variables are useful for modeling decisions, choices, or logical conditions in your problem. For example, you can use boolean variables to represent whether a machine is used, whether a task is assigned to a particular worker, or whether a constraint should be enforced.

Boolean variables can be created using the function Model.boolVar. By default, boolean variables are present (not optional). To create an optional boolean variable, specify optional=True in the arguments of the function.

Logical operators

Boolean variables support the following logical operators:

These operators can be used to create complex boolean expressions and constraints.

Boolean variables as integer expressions

Class BoolVar derives from BoolExpr, which derives from IntExpr. Therefore, boolean variables can be used as integer expressions: True is equal to 1, False is equal to 0, and absent remains absent.

This is useful for counting how many conditions are satisfied or for weighted sums.

Optional boolean variables

A boolean variable can be optional. In this case, the solver can decide to make the variable absent, which means the variable doesn't participate in the solution. When a boolean variable is absent, its value is neither True nor False — it is absent.

Most expressions that depend on an absent variable are also absent. For example, if x is an absent boolean variable, then ~x, x | y, and x & y are all absent, regardless of the value of y. However, some functions handle absent values specially, such as IntExpr.presence or Model.sum.

When a boolean expression is added as a constraint using Model.enforce, the constraint requires that the expression is not false in the solution. The expression can be true or absent. This means that constraints involving optional variables are automatically satisfied when the underlying variables are absent.

Functions Model.presence and IntExpr.presence can constrain the presence of the variable.

See

  • Model.boolVar to create boolean variables.
  • BoolExpr for boolean expressions and their operations.
  • IntVar for integer decision variables.
  • IntervalVar for the primary variable type for scheduling problems.

Examples

In the following example, we create two boolean variables representing whether to use each of two machines. We require that at least one machine is used, but not both:

import * as CP from "optalcp";

const model = new CP.Model();
const useMachineA = model.boolVar({ name: "use_machine_a" });
const useMachineB = model.boolVar({ name: "use_machine_b" });

// Constraint: must use at least one machine
model.enforce(useMachineA.or(useMachineB));

// Constraint: cannot use both machines (exclusive choice)
model.enforce(useMachineA.and(useMachineB).not());

const result = await model.solve();

Boolean variables can be used in arithmetic expressions by treating True as 1 and False as 0:

import * as CP from "optalcp";

const model = new CP.Model();
const options = Array.from({length: 5}, (_, i) => model.boolVar({ name: `option_${i}` }));

// Constraint: select exactly 2 options
model.enforce(model.sum(options).eq(2));

const result = await model.solve();

Extends

Accessors

max

Get Signature

get max(): null | boolean

The maximum value of the boolean variable's domain.

Remarks

Gets or sets the maximum value of the boolean variable's domain.

For a free (unconstrained) boolean variable, returns True. If set to False, the variable is fixed to False. If the variable is absent, the getter returns None.

Note: This property reflects the variable's domain in the model (before the solve), not in the solution.

See

BoolVar.min, BoolVar.optional.

Example
import * as CP from "optalcp";

const model = new CP.Model();
const x = model.boolVar({ name: "x" });

console.log(x.max); // true (default maximum)

x.max = false;
console.log(x.max); // false (variable is now fixed to false)
Returns

null | boolean

Set Signature

set max(value: boolean): void

Parameters
ParameterType
valueboolean
Returns

void


min

Get Signature

get min(): null | boolean

The minimum value of the boolean variable's domain.

Remarks

Gets or sets the minimum value of the boolean variable's domain.

For a free (unconstrained) boolean variable, returns False. If set to True, the variable is fixed to True. If the variable is absent, the getter returns None.

Note: This property reflects the variable's domain in the model (before the solve), not in the solution.

See

BoolVar.max, BoolVar.optional.

Example
import * as CP from "optalcp";

const model = new CP.Model();
const x = model.boolVar({ name: "x" });

console.log(x.min); // false (default minimum)

x.min = true;
console.log(x.min); // true (variable is now fixed to true)
Returns

null | boolean

Set Signature

set min(value: boolean): void

Parameters
ParameterType
valueboolean
Returns

void


name

Get Signature

get name(): undefined | string

The name assigned to this model element.

Remarks

The name is optional and primarily useful for debugging purposes. When set, it helps identify the element in solver logs, error messages, and when inspecting solutions.

Names can be assigned to any model element including variables, expressions, and constraints.

Example
import * as CP from "optalcp";

const model = new CP.Model();

// Name a variable at creation
const task = model.intervalVar({ length: 10, name: "assembly" });

// Or set name later
const x = model.intVar({ min: 0, max: 100 });
x.name = "quantity";

console.log(task.name); // "assembly"
console.log(x.name); // "quantity"
Returns

undefined | string

Set Signature

set name(value: string): void

Parameters
ParameterType
valuestring
Returns

void

Inherited from

BoolExpr.name


optional

Get Signature

get optional(): null | boolean

The presence status of the boolean variable.

Remarks

Gets or sets the presence status of the boolean variable using a tri-state value:

  • True / true: The variable is optional - the solver decides whether it is present or absent in the solution.
  • False / false: The variable is present - it must have a value in the solution.
  • None / null: The variable is absent - it will be omitted from the solution.

Note: This property reflects the presence status in the model (before the solve), not in the solution.

See

BoolVar.min, BoolVar.max.

Example
import * as CP from "optalcp";

const model = new CP.Model();
const x = model.boolVar({ name: "x" });
const y = model.boolVar({ optional: true, name: "y" });

console.log(x.optional); // false (present by default)
console.log(y.optional); // true (optional)

// Make x optional
x.optional = true;
console.log(x.optional); // true

// Make y absent
y.optional = null;
console.log(y.optional); // null
Returns

null | boolean

Set Signature

set optional(value: null | boolean): void

Parameters
ParameterType
valuenull | boolean
Returns

void

Constructors

new BoolVar()

new BoolVar(): BoolVar

Returns

BoolVar

Inherited from

BoolExpr.constructor

Methods

abs()

abs(): IntExpr

Creates an integer expression which is absolute value of the expression.

Returns

IntExpr

The resulting integer expression

Remarks

If the expression has value absent, the resulting expression also has value absent.

Same as Model.abs.

Inherited from

BoolExpr.abs


and()

and(rhs: boolean | BoolExpr): BoolExpr

Returns logical AND of the expression and arg.

Parameters

ParameterTypeDescription
rhsboolean | BoolExprThe second boolean expression.

Returns

BoolExpr

The resulting Boolean expression

Remarks

If the expression or arg has value absent, then the resulting expression has also value absent.

Same as Model.and.

Inherited from

BoolExpr.and


div()

div(rhs: number | IntExpr): IntExpr

Returns integer division of the expression arg. The division rounds towards zero.

Parameters

ParameterTypeDescription
rhsnumber | IntExprThe second integer expression.

Returns

IntExpr

The resulting integer expression

Remarks

If the expression or arg has value absent, then the resulting expression has also value absent.

Same as Model.div.

Inherited from

BoolExpr.div


enforce()

enforce(): void

Adds this boolean expression as a constraint to the model.

Returns

void

Remarks

This method adds the boolean expression as a constraint to the model. It provides a fluent-style alternative to Model.enforce.

A constraint is satisfied if it is not false. In other words, a constraint is satisfied if it is true or absent.

A boolean expression that is not added as a constraint can have arbitrary value in a solution (true, false, or absent). Once added as a constraint, it can only be true or absent in the solution.

See

  • Model.enforce for the Model-centric style of adding constraints.
  • BoolExpr for more about boolean expressions.

Example

import * as CP from "optalcp";

const model = new CP.Model();
const x = model.intVar({ min: 0, max: 10, name: "x" });
const y = model.intVar({ min: 0, max: 10, name: "y" });

// Enforce constraint using fluent style
x.plus(y).le(15).enforce();

// Equivalent to:
// model.enforce(x.plus(y).le(15));

const result = await model.solve();

Inherited from

BoolExpr.enforce


eq()

eq(rhs: number | IntExpr): BoolExpr

Create an equality constraint.

Parameters

ParameterTypeDescription
rhsnumber | IntExprThe expression or constant to compare against.

Returns

BoolExpr

A boolean expression that is true when self equals rhs.

Remarks

Returns a BoolExpr representing self == other.

If either operand has value absent, the result is also absent.

See

import * as CP from "optalcp";

const model = new CP.Model();
const x = model.intVar({ min: 0, max: 10, name: "x" });
const y = model.intVar({ min: 0, max: 10, name: "y" });

// IntExpr.eq(IntExpr)
model.enforce(x.eq(y));

// IntExpr.eq(constant)
model.enforce(x.eq(5));

Inherited from

BoolExpr.eq


ge()

ge(rhs: number | IntExpr): BoolExpr

Create a greater-than-or-equal constraint.

Parameters

ParameterTypeDescription
rhsnumber | IntExprThe expression or constant to compare against.

Returns

BoolExpr

A boolean expression that is true when self is greater than or equal to rhs.

Remarks

Returns a BoolExpr representing self >= other.

If either operand has value absent, the result is also absent.

See

import * as CP from "optalcp";

const model = new CP.Model();
const x = model.intVar({ min: 0, max: 10, name: "x" });
const y = model.intVar({ min: 0, max: 10, name: "y" });

// IntExpr.ge(IntExpr)
model.enforce(x.ge(y));

// IntExpr.ge(constant)
model.enforce(x.ge(5));

Inherited from

BoolExpr.ge


gt()

gt(rhs: number | IntExpr): BoolExpr

Create a greater-than constraint.

Parameters

ParameterTypeDescription
rhsnumber | IntExprThe expression or constant to compare against.

Returns

BoolExpr

A boolean expression that is true when self is greater than rhs.

Remarks

Returns a BoolExpr representing self > other.

If either operand has value absent, the result is also absent.

See

import * as CP from "optalcp";

const model = new CP.Model();
const x = model.intVar({ min: 0, max: 10, name: "x" });
const y = model.intVar({ min: 0, max: 10, name: "y" });

// IntExpr.gt(IntExpr)
model.enforce(x.gt(y));

// IntExpr.gt(constant)
model.enforce(x.gt(5));

Inherited from

BoolExpr.gt


guard()

guard(absentValue: number): IntExpr

Creates an expression that replaces value absent by a constant.

Parameters

ParameterTypeDefault valueDescription
absentValuenumber0The value to use when the expression is absent.

Returns

IntExpr

The resulting integer expression

Remarks

The resulting expression is:

  • equal to the expression if the expression is present
  • and equal to absentValue otherwise (i.e. when the expression is absent).

The default value of absentValue is 0.

The resulting expression is never absent.

Same as Model.guard.

Inherited from

BoolExpr.guard


identity()

identity(rhs: number | IntExpr): Constraint

Constrains two expressions to be identical, including their presence status.

Parameters

ParameterTypeDescription
rhsnumber | IntExprThe second integer expression.

Returns

Constraint

The constraint object

Remarks

Identity is different than equality. For example, if x is absent, then x == 0 is absent, but x.identity(0) is false.

Same as Model.identity.

Inherited from

BoolExpr.identity


implies()

implies(rhs: boolean | BoolExpr): BoolExpr

Returns implication between the expression and arg.

Parameters

ParameterTypeDescription
rhsboolean | BoolExprThe second boolean expression.

Returns

BoolExpr

The resulting Boolean expression

Remarks

If the expression or arg has value absent, then the resulting expression has also value absent.

Same as Model.implies.

Inherited from

BoolExpr.implies


inRange()

inRange(lb: number, ub: number): BoolExpr

Creates Boolean expression lbthisub.

Parameters

ParameterTypeDescription
lbnumberThe lower bound of the range.
ubnumberThe upper bound of the range.

Returns

BoolExpr

The resulting Boolean expression

Remarks

If the expression has value absent, then the resulting expression has also value absent.

Use Model.enforce to add this expression as a constraint to the model.

Same as Model.inRange.

Inherited from

BoolExpr.inRange


le()

le(rhs: number | IntExpr): BoolExpr

Create a less-than-or-equal constraint.

Parameters

ParameterTypeDescription
rhsnumber | IntExprThe expression or constant to compare against.

Returns

BoolExpr

A boolean expression that is true when self is less than or equal to rhs.

Remarks

Returns a BoolExpr representing self <= other.

If either operand has value absent, the result is also absent.

See

import * as CP from "optalcp";

const model = new CP.Model();
const x = model.intVar({ min: 0, max: 10, name: "x" });
const y = model.intVar({ min: 0, max: 10, name: "y" });

// IntExpr.le(IntExpr)
model.enforce(x.le(y));

// IntExpr.le(constant)
model.enforce(x.le(5));

Inherited from

BoolExpr.le


lt()

lt(rhs: number | IntExpr): BoolExpr

Create a less-than constraint.

Parameters

ParameterTypeDescription
rhsnumber | IntExprThe expression or constant to compare against.

Returns

BoolExpr

A boolean expression that is true when self is less than rhs.

Remarks

Returns a BoolExpr representing self < other.

If either operand has value absent, the result is also absent.

See

import * as CP from "optalcp";

const model = new CP.Model();
const x = model.intVar({ min: 0, max: 10, name: "x" });
const y = model.intVar({ min: 0, max: 10, name: "y" });

// IntExpr.lt(IntExpr)
model.enforce(x.lt(y));

// IntExpr.lt(constant)
model.enforce(x.lt(5));

Inherited from

BoolExpr.lt


max2()

max2(rhs: number | IntExpr): IntExpr

Creates an integer expression which is the maximum of the expression and arg.

Parameters

ParameterTypeDescription
rhsnumber | IntExprThe second integer expression.

Returns

IntExpr

The resulting integer expression

Remarks

If the expression or arg has value absent, then the resulting expression has also value absent.

Same as Model.max2. See Model.max for n-ary maximum.

Inherited from

BoolExpr.max2


maximize()

maximize(): Objective

Creates a maximization objective for this expression.

Returns

Objective

An Objective that maximizes this expression.

Remarks

Creates an objective to maximize the value of this integer expression. A model can have at most one objective. New objective replaces the old one.

This is a fluent-style alternative to Model.maximize that allows creating objectives directly from expressions.

Example

import * as CP from "optalcp";

const model = new CP.Model();
const x = model.intervalVar({start: [0, 100], length: 10, name: "x"});

// Fluent style - maximize the start of x
x.start().maximize();

// Equivalent Model.maximize() style:
model.maximize(x.start());

See

Inherited from

BoolExpr.maximize


min2()

min2(rhs: number | IntExpr): IntExpr

Creates an integer expression which is the minimum of the expression and arg.

Parameters

ParameterTypeDescription
rhsnumber | IntExprThe second integer expression.

Returns

IntExpr

The resulting integer expression

Remarks

If the expression or arg has value absent, then the resulting expression has also value absent.

Same as Model.min2. See Model.min for the n-ary minimum.

Inherited from

BoolExpr.min2


minimize()

minimize(): Objective

Creates a minimization objective for this expression.

Returns

Objective

An Objective that minimizes this expression.

Remarks

Creates an objective to minimize the value of this integer expression. A model can have at most one objective. New objective replaces the old one.

This is a fluent-style alternative to Model.minimize that allows creating objectives directly from expressions.

Example

import * as CP from "optalcp";

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

// Fluent style - minimize the end of y
y.end().minimize();

// Equivalent Model.minimize() style:
model.minimize(y.end());

See

Inherited from

BoolExpr.minimize


minus()

minus(rhs: number | IntExpr): IntExpr

Returns subtraction of the expression and arg.

Parameters

ParameterTypeDescription
rhsnumber | IntExprThe second integer expression.

Returns

IntExpr

The resulting integer expression

Remarks

If the expression or arg has value absent, then the resulting expression has also value absent.

Same as Model.minus.

Inherited from

BoolExpr.minus


ne()

ne(rhs: number | IntExpr): BoolExpr

Create an inequality constraint.

Parameters

ParameterTypeDescription
rhsnumber | IntExprThe expression or constant to compare against.

Returns

BoolExpr

A boolean expression that is true when self does not equal rhs.

Remarks

Returns a BoolExpr representing self != other.

If either operand has value absent, the result is also absent.

See

import * as CP from "optalcp";

const model = new CP.Model();
const x = model.intVar({ min: 0, max: 10, name: "x" });
const y = model.intVar({ min: 0, max: 10, name: "y" });

// IntExpr.ne(IntExpr)
model.enforce(x.ne(y));

// IntExpr.ne(constant)
model.enforce(x.ne(5));

Inherited from

BoolExpr.ne


neg()

neg(): IntExpr

Returns negation of the expression.

Returns

IntExpr

The resulting integer expression

Remarks

If the expression has value absent then the resulting expression has also value absent.

Same as Model.neg.

Inherited from

BoolExpr.neg


not()

not(): BoolExpr

Returns negation of the expression.

Returns

BoolExpr

The resulting Boolean expression

Remarks

If the expression has value absent then the resulting expression has also value absent.

Same as Model.not.

Inherited from

BoolExpr.not


or()

or(rhs: boolean | BoolExpr): BoolExpr

Returns logical OR of the expression and arg.

Parameters

ParameterTypeDescription
rhsboolean | BoolExprThe second boolean expression.

Returns

BoolExpr

The resulting Boolean expression

Remarks

If the expression or arg has value absent then the resulting expression has also value absent.

Same as Model.or.

Inherited from

BoolExpr.or


plus()

plus(rhs: number | IntExpr): IntExpr

Returns addition of the expression and the argument.

Parameters

ParameterTypeDescription
rhsnumber | IntExprThe second integer expression.

Returns

IntExpr

The resulting integer expression

Remarks

If the expression or arg has value absent, then the resulting expression has also value absent.

Same as Model.plus.

Inherited from

BoolExpr.plus


presence()

presence(): BoolExpr

Returns an expression which is true if the expression is present and false when it is absent.

Returns

BoolExpr

The resulting Boolean expression

Remarks

The resulting expression is never absent.

Same as Model.presence.

Inherited from

BoolExpr.presence


times()

times(rhs: number | IntExpr): IntExpr

Returns multiplication of the expression and arg.

Parameters

ParameterTypeDescription
rhsnumber | IntExprThe second integer expression.

Returns

IntExpr

The resulting integer expression

Remarks

If the expression or arg has value absent, then the resulting expression has also value absent.

Same as Model.times.

Inherited from

BoolExpr.times

Properties

PropertyModifierTypeInherited from
__boolExprBrandreadonly"BoolExpr"BoolExpr.__boolExprBrand
__floatExprBrandreadonly"FloatExpr"BoolExpr.__floatExprBrand
__intExprBrandreadonly"IntExpr"BoolExpr.__intExprBrand