If I have an IntVar
s[j]
stored in a vector s
, is it possible to iterate for (a period of time) t
and create a BoolVar
b
such that b[t]
is true if and only if s[j]==t
? I tried setting some restrictions:
for (int j = 0; j < nbTasks; ++j)
{
for (int t = 0; t < tmax; ++t)
{
const IntVar expr = cp_model.NewIntVar(Domain(-1000000000,100000000000));
const IntVar actual_t = cp_model.NewConstant(t).WithName("actual_t");
const BoolVar b = cp_model.NewBoolVar().WithName("b");
const IntVar value = cp_model.NewConstant(w[j][t]);
obj_final.push_back(expr);
actuals[j].push_back(actual_t);
expressions[j].push_back(expr);
values[j].push_back(value);
cp_model.AddEquality(b, starts[j]==actual_t);
cp_model.AddProductEquality(expr, {value, b});
}
}
I created the BoolVar
b
and then set an equality to the bool value of s[j]==t
, then I used the constraint expr == value*b
and added the expression to the vector obj_final
, and finally set the objective: cp_model.Maximize(LinearExpr::Sum(obj_final));
The problem is that my solution is optimal but the objective is zero, so I guess b
is always false.
My goal is to assign a value to an expression if and only if s[j]==t
. Enforce constraints isn't what i need because the other expressions must be zero.
I'll be thankful for any help.
OnlyEnforceIf
takes onlyBoolVar
as argument, and the condition i need (s[j]==t
) is justbool
, i tried but the function don't match the argument. $\endgroup$cp_model.AddEquality(s[j], t).OnlyEnforceIf(b)
andcp_model.AddEquality(s[j], 0).OnlyEnforceIf(Not(b))
? Which is more or less the same as the example linked $\endgroup$cp_model.AddEquality(expr, value).OnlyEnforceIf(s[j]==t)
butOnlyEnforceIf
takes onlyBoolVar
arguments and notbool
(as s[j]==t). So i want to create aBoolVar
that is true ifs[j]==t
, else false. $\endgroup$