ian_688
ian_688

Reputation: 55

or-tools Maximize/Minimize OR / XOR

Simple example:

days = range(1,10)
for d in days:
   model.AddBoolXOr(a,b,c,d,e,f,g)

Above I can ensure only one of a...g is true every day. But it is not always possible to achieve this every day so I want to be able to maximize the number of times it is achieved. Something like...

array_bools = []
days = range(1,10)
for d in days:
   day_bool = NewBoolVar('name')
   model.Add(day_bool = XOr(a,b,c,d,e,f,g))
   array_bools.append(day_bool)

model.Maximize(sum(array_bool[i] for i in range(len(array_bool))))

Upvotes: 3

Views: 550

Answers (1)

Laurent Perron
Laurent Perron

Reputation: 11014

array_bools = []
days = range(1,10)
for d in days:
    day_bool = NewBoolVar('name')
    model.Add(sum([a,b,c,d,e,f,g]) == 1).OnlyEnforceIf(day_bool)
    model.Add(sum([a,b,c,d,e,f,g]) != 1).OnlyEnforceIf(day_bool.Not())
    array_bools.append(day_bool)

model.Maximize(sum(array_bool))

See this page of documentation

Upvotes: 2

Related Questions