IBM ILOG Solver User's Manual > Extending the Library > Writing a Goal: Car Sequencing > Understanding goals > Subgoals

A goal can be defined in terms of other goals, called its subgoals. A subgoal is not executed immediately. In fact, it is added to the goal stack, and the execute member function of the current goal terminates before the subgoal is executed. When the execution of the goal itself is complete, the subgoal will be returned.

A goal can be defined as a choice between subgoals. Only one of the subgoals can succeed.This choice point is implemented by the function IlcOr.

A goal can also be defined as a sequence of subgoals that must all succeed. This sequence is implemented by the function IlcAnd.

Creating choice points using IlcOr

A function that creates a goal takes a constrained variable as its argument (or an array of variables) and binds a value to it (or them). Solver automatically propagates the constraints posted on this constrained variable. There is a hidden problem in that description: in general, Solver does not know which value in the domain is consistent with the constraints. In other words, that binding of the constrained variable must be seen as a guess: if it leads to inconsistencies, it should be undone, and another value should be tried. Solver implement these guesses and "undos" by using choice points.

Choice points are implemented in Solver by the function IlcOr. A choice point defines a goal in terms of a choice between subgoals. Solver executes a choice point between two subgoals like this:

When the function IloSolver::fail is called, goal execution resumes at the most recent choice point with untried subgoals. It is possible to make goal execution resume at an earlier choice point if you associate labels with choice points. Then the function IloSolver::fail can be called with a label. In such a case, goal execution resumes at the most recent choice point having the same label.

You can create a choice point with up to five subgoals by means of the function IlcOr. For example, in order to define a choice point with eight subgoals g1, . . . , g8, you can do this:

IlcOr(IlcOr(g1, g2, g3, g4, g5),
      IlcOr(g6, g7, g8));


The member function IloSolver::next searches for one successful execution of a given. To search for all of them, put next within a loop.

Creating sequences of subgoals using IlcAnd

A goal can also be defined as a sequence of subgoals that must all succeed. This sequence is implemented by the function IlcAnd. This function defines a goal composed of several subgoals (up to five subgoals). The subgoals are executed from left to right.

You can overcome the apparent limitation to five subgoals by calling the function IlcAnd several times. For example, in order to define a choice point with eight subgoals g1, . . . , g8, it is sufficient to call:

IlcAnd(IlcAnd(g1, g2, g3, g4, g5),
       IlcAnd(g6, g7, g8));

Logical properties of IlcOr and IlcAnd

The functions IlcOr and IlcAnd are associative. For example, if g1, g2, and g3 are goals, the execution of

IlcAnd(IlcAnd(g1, g2), g3);

is equivalent to the execution of

IlcAnd(g1, IlcAnd(g2, g3));

You can use this associativity to define goals with more than five subgoals. IlcOr is also associative. Thus the execution of

IlcOr(IlcOr(g1, g2), g3);

is equivalent to the execution of

IlcOr(g1, IlcOr(g2, g3));

Again, you can use this associativity for defining choice points with more than five subgoals. IlcAnd and IlcOr are distributive over each other. For example, the execution of

IlcAnd(IlcOr(g1, g2), g3);

is equivalent to the execution of

IlcOr(IlcAnd(g1, g3), IlcAnd(g2, g3));

To create a conjunction (logical AND) of goals at the model level, use IloAndGoal. To create a disjunction (logical OR) of goals at the model level, use IloOrGoal.