Search code examples
pythonsolveror-toolscp-sat

Setting values for IntVar for CP-SAT solver from OR-Tools


I am currently using a google OR-tools CP-SAT solver for a planification problem. I am using IntVars as a representation of the date. All those IntVars are in a dictionary. I have a few constraints that work correctly but I would like to force the solver to have about 2/3 of my Intvars inferior to 400.

I've tried to solve the problem using BoolVars but didn't succeed and I ran out of ideas of how to force 2/3 of the values under 400.

Can anyone think of a solution?


Solution

  • I am not sure this is a good business rule,

    anyway

      int_vars = [...]
      bool_vars = []
      for v in int_vars:
        is_less_than_400 = model.new_bool_var('')
        model.add(v <= 400).only_enforce_if(is_less_than_400)
        model.add(v > 400).only_enforce_if(~is_less_than_400)
        bool_vars.append(is_less_than_400)
    
      threshold = len(bool_var) * 2 // 3
      model.add(sum(bool_vars) <= threshold)