I created OptionalIntervals for a number of tasks:

    std::vector<IntVar> starts;
    std::vector<IntVar> sizes;
    std::vector<IntVar> ends;
    std::vector<BoolVar> presences;
    std::vector<IntervalVar> Intervals;

    for (int j=0; j<nbTasks; j++) 
        const IntVar start = cp_model.NewIntVar(Domain(0, tmax)).WithName("start");       // generamos una variable intervalo
        const IntVar end = cp_model.NewIntVar(Domain(0, tmax)); 
        const BoolVar presence = cp_model.NewBoolVar();
        const IntVar size = cp_model.NewConstant(duration[j]); 
        const IntervalVar interval = cp_model.NewOptionalIntervalVar(start, size, end, presence);

and I want to maximize a function that depends on the start of each task (where some tasks could be unperformed to get a better solution).

For some reason, all the activities are unperformed in the results even if the start of a job is 53. I mean, the present variable is 0 but it has a start date (thus the constraint start+duration==end is not applied). Why?

I guess that what causes this is that the starts vector is independent from the Intervals vector, so when a task is unperformed the interval constraint is not applied but its start varies independently. Is there a way to avoid that? that if the task is not performed it doesn't have a start either.


1 Answer 1


In the Python API there is a pretty good docstring of what an OptionalIntervalVar is:

An optional interval variable is a constraint, that is itself used in other constraints like NoOverlap. This constraint is protected by an is_present literal that indicates if it is active or not.

Internally, it ensures that is_present implies start + size == end.

So you would have to constraint manually what happens to start, size and end when the presence literal is not active.

Don't forget to also constraint the presence boolean properly, otherwise it won't ever be active.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.