6
$\begingroup$

I'm trying to write this constraint in C++ and add it to my model (addLessOrEqual): $$\text{start}_{i}+\text{duration}_{i}\le\text{start}_{j}$$ for an $(i,j)$ arc of precedence.

Starts (IntVar) and durations (int) are part of an IntervalVar. The problem is that I can't operate $+$ with IntVar and Int.

I'm using C++ and i get this error:

    error: no match for ‘operator+’ (operand types are 
‘__gnu_cxx::__alloc_traits<std::allocator<operations_research::sat::IntVar> 
>::value_type {aka operations_research::sat::IntVar}’ and 
‘__gnu_cxx::__alloc_traits<std::allocator<int> >::value_type {aka int}’)
                 cp_model.AddLessOrEqual(starts[j]+durations[j],starts[succ[j][s]])

What can I do to sum and get the expression? Is there another predefined constraint that does the same? Thanks!

$\endgroup$
  • $\begingroup$ I'm not familiar with or-tools but have you tried bringing all of your expression to the left-hand side and your value to the right-hand side and see if the error goes away? $\endgroup$ – EhsanK Jan 20 at 15:12
  • $\begingroup$ I did this but now the problem is with the operator. $\endgroup$ – Diego R. Troncoso Jan 20 at 15:25
  • 1
    $\begingroup$ AddLessOrEqual(LinearExpr(starts[j]).AddConstant(durations[j]), starts[succ[j][s]]) $\endgroup$ – Stradivari Jan 20 at 15:28
  • $\begingroup$ thanks Stradivari, apparently it works! $\endgroup$ – Diego R. Troncoso Jan 20 at 15:32
6
$\begingroup$

What you want is:

AddLessOrEqual(LinearExpr(starts[j]).AddConstant(durations[j]), starts[succ[j][s]])

You might also want to take a look at the examples (ending with _sat.cc) to be more familiar with the c++ methods.

https://github.com/google/or-tools/tree/master/examples/cpp

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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