# How exactly does OptionalIntervalVar work in OR-Tools?

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);
starts.push_back(start);
sizes.push_back(size);
ends.push_back(end);
presences.push_back(presence);
Intervals.push_back(interval);
}


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.

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.