Table of Contents

Class BoolVar

Namespace
OptalCP
Assembly
OptalCP.dll

Boolean decision variable whose value is determined by the solver.

public class BoolVar : BoolExpr
Inheritance
BoolVar
Inherited Members

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:

  • !x for logical NOT
  • x | y for logical OR
  • x & y for logical AND

Note: use & and | (bitwise operators), not && and || (short-circuit operators), as C# does not allow overloading && and ||.

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.

Example:

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:
var model = new Model();
var useMachineA = model.BoolVar(name: "use_machine_a");
var useMachineB = model.BoolVar(name: "use_machine_b");

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

// Constraint: cannot use both machines (exclusive choice)
model.Enforce(!(useMachineA & useMachineB));

var result = model.Solve();

Example:

Boolean variables can be used in arithmetic expressions by treating True as 1 and False as 0:
var model = new Model();
var options = Enumerable.Range(0, 5).Select(i => model.BoolVar(name: $"option_{i}")).ToArray();

// Constraint: select exactly 2 options
model.Enforce(model.Sum(options) == 2);

var result = model.Solve();

See also:

  • 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.

Properties

Max

The maximum value of the boolean variable's domain.

public bool Max { get; set; }

Property Value

bool

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.

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

Example:

var model = new Model();
var x = model.BoolVar(name: "x");

Console.WriteLine(x.Max); // True (default maximum)

x.Max = false; Console.WriteLine(x.Max); // False (variable is now fixed to False)

See also:

Min

The minimum value of the boolean variable's domain.

public bool Min { get; set; }

Property Value

bool

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.

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

Example:

var model = new Model();
var x = model.BoolVar(name: "x");

Console.WriteLine(x.Min); // False (default minimum)

x.Min = true; Console.WriteLine(x.Min); // True (variable is now fixed to True)

See also:

Optional

Whether the boolean variable is optional.

public bool Optional { get; set; }

Property Value

bool

Remarks

Gets or sets whether the boolean variable is optional:

  • 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.

To force a variable to be absent, make it optional and constrain its presence to be false: model.enforce(~model.presence(x)) (Python), model.enforce(model.presence(x).not()) (TypeScript), or model.Enforce(!model.Presence(x)) (C#).

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

Example:

var model = new Model();
var x = model.BoolVar(name: "x");
var y = model.BoolVar(optional: true, name: "y");

Console.WriteLine(x.Optional); // False (present by default) Console.WriteLine(y.Optional); // True (optional)

// Make x optional x.Optional = true; Console.WriteLine(x.Optional); // True

See also: