150 likes | 259 Views
number partitioning. Given a bag of numbers, can you partition this into 2 bags such that the sum of the integers in each bag is equal?. Recently featured in the Crystal Maze! (Thanks Zoe!). Try it!. Can you think of a 1st test to carry out to determine if there is no partition?.
E N D
Given a bag of numbers, can you partition this into 2 bags such that the sum of the integers in each bag is equal? Recently featured in the Crystal Maze! (Thanks Zoe!)
Can you think of a 1st test to carry out to determine if there is no partition?
Given a bag of numbers, can you partition this into 2 bags such that the sum of the integers in each bag is equal? Garey & Johnson “Computers and Intractability” [SP12] PARTITION INSTANCE: Finite set A and a size s(a) Z+ for each a A QUESTION: Is there a subset A’ A such that aA’ s(a) aA-A’ s(a)
Who cares? Imagine you have 2 machines on the shop floor You have n activities, of varying durations Place the activities on the machines to minimise makespan
Why just 2-partition? Why not m-way partitioning? Is there an optimisation problem?
A number of constraint encoding chocoExamples/numPart.cl
10 4 5 3 2 1 8 7 3 5 9
[numPart1(fname:string) : Problem -> let f := fopen(fname,"r"), n := read(f), p := makeProblem(fname,n + n), A := list<IntVar>(), B := list<IntVar>(), sum := 0 in (for i in (1 .. n) let m := read(f), mA := makeIntVar(p,"A" /+ string!(m),list(0,m)), mB := makeIntVar(p,"B" /+ string!(m),list(0,m)) in (add(A,mA), add(B,mB), post(p,mA !== mB), sum := sum + m), fclose(f), post(p,sumVars(A) == sumVars(B)), p)] // // is this okay? //
[numPart2(fname:string) : Problem -> let f := fopen(fname,"r"), n := read(f), p := makeProblem(fname,n + n), A := list<IntVar>(), B := list<IntVar>(), sum := 0 in (for i in (1 .. n) let m := read(f), mA := makeIntVar(p,"A" /+ string!(m),list(0,m)), mB := makeIntVar(p,"B" /+ string!(m),list(0,m)) in (add(A,mA), add(B,mB), post(p,mA !== mB), sum := sum + m), fclose(f), post(p,sumVars(A) == sum / 2), post(p,sumVars(B) == sum / 2), p)] // // is this better than numPart1? If so, why? //
[numPartMin(fname:string) : tuple -> let f := fopen(fname,"r"), n := read(f), p := makeProblem(fname,n + n + 3), A := list<IntVar>(), B := list<IntVar>(), sum := 0 in (for i in (1 .. n) let m := read(f), mA := makeIntVar(p,"A" /+ string!(m),list(0,m)), mB := makeIntVar(p,"B" /+ string!(m),list(0,m)) in (add(A,mA), add(B,mB), post(p,mA !== mB), sum := sum + m), fclose(f), let sumA := makeIntVar(p,"sumA",0,sum), sumB := makeIntVar(p,"sumB",0,sum), diff := makeIntVar(p,"diff",0,sum) in (post(p,sumVars(A) == sumA), post(p,sumVars(B) == sumB), post(p,ifOnlyIf(sumA >= sumB,diff == sumA - sumB)), post(p,ifOnlyIf(sumB > sumA,diff == sumB - sumA)), tuple(p,diff)))] // // minimise the difference between the two set // // t:tuple := numPartMin("np10") // minimize(t[1],t[2],true) //