Courses/CS 460/Fall 2006/Regin filtering

From CSWiki

Jump to: navigation, search

[edit] Code

package examples;

import java.util.Arrays;

import choco.*;
import choco.integer.IntVar;

public class ReginFiltering {
  
  public static void main(String[] args) {
    
    Problem regin = new Problem();
    
    IntVar[] vars =
      new IntVar[] {
            regin.makeEnumIntVar("a", 1, 3),
            regin.makeEnumIntVar("b", 2, 3),
            regin.makeEnumIntVar("c", 2, 3),
            regin.makeEnumIntVar("d", 1, 4),
            regin.makeEnumIntVar("e", 1, 5)};
    
    // Require all the FDV's to be different.
    regin.post(regin.allDifferent(vars));
    
    System.out.print("Initially: ");
    for (IntVar v: vars) System.out.print(v.pretty() + " ");
    System.out.println();
    // Output: a:?{1, 2, 3} b:?{2, 3} c:?{2, 3} d:?{1, 2, 3, 4} e:?{1, 2, 3, 4, 5}
    
    // The constraint propagator is clever enough to
    // know that b and c exhaust the possibilities 
    // for 2 and 3 (even though it doesn't know which
    // is which) leaving only 1 for a. That leaves only 
    // 4 for d, which leaves only 5 for e.
    try {regin.propagate();}
    catch (ContradictionException e) {e.printStackTrace();}

    System.out.print("After regin.propagate(): ");
    for (IntVar v: vars) System.out.print(v.pretty() + " ");
    System.out.println();
    // Output: a:1{1} b:?{2, 3} c:?{2, 3} d:4{4} e:5{5}

    regin.solve();
    
    System.out.println("After regin.solve(): " + Arrays.asList(vars));
    // Output: [a:1, b:3, c:2, d:4, e:5]
  }

}

[edit] Output

Initially: a:?{1, 2, 3} b:?{2, 3} c:?{2, 3} d:?{1, 2, 3, 4} e:?{1, 2, 3, 4, 5} 
After regin.propagate(): a:1{1} b:?{2, 3} c:?{2, 3} d:4{4} e:5{5} 
After regin.solve(): [a:1, b:2, c:3, d:4, e:5]