Courses/CS 460/Fall 2006/Nadeem, Hassan/Spy Girls

From CSWiki

< Courses | CS 460 | Fall 2006 | Nadeem, Hassan
Revision as of 04:12, 11 October 2006 by Hassan (Talk | contribs)
(diff) ←Older revision | Current revision (diff) | Newer revision→ (diff)
Jump to: navigation, search
import java.util.Arrays;

import JaCoP.*;

/*
                                       SPY GIRLS
The Spy Girls Agency, the all-female spy agency, consists of Jane and four other agents, 
one of whom is Agent Brookes. Each agent is ranked by her secret agent number, from 
Agent 1 (highest) to Agent 5 (lowest). 
From this information and the following clues, determine each agent's rank, 
first name, and last name.

   1. Sylvia is ranked higher than Agent Adams, but is not the highest.
   2. Hillary's rank is higher than Agent Darling, but not as high as Agent Brookes.
   3. Amanda is ranked higher than either Kate or Hillary, but is not the highest.
   4. Agent Adams is ranked higher than Agent Miller, but is ranked lower than Agent Cooper.
   5. Agent Miller is not ranked the lowest.
*/

public class SpyGirls {

  protected static FDStore store = new FDStore();

  protected static int min = 1, max = 5;

  protected static enum Fname implements FDVEnum {
    Jane, Silvia, Hillary, Amanda, Kate;
    public static FDV[] getFDVs() {return fdvEnumsToFDVs(values());}
    static {store.impose(new Alldiff(getFDVs()));}
    private FDV fdv;
    Fname() {fdv = store.newFDV(this, min, max);}
    public FDV getFD() {return fdv;}
  }

  protected static enum Lname implements FDVEnum {
    Brooke, Adam, Darling, Miller, Cooper;
    public static FDV[] getFDVs() {return fdvEnumsToFDVs(values());}
    static {store.impose(new Alldiff(getFDVs()));}
    private FDV fdv;
    Lname() {fdv = store.newFDV(this, min, max);}
    public FDV getFD() {return fdv;}
  }

    protected static enum Rank implements FDVEnum {
    First, Second, Third, Fourth, Fifth;
    public static FDV[] getFDVs() {return fdvEnumsToFDVs(values());}
    static {store.impose(new Alldiff(getFDVs()));}
    private FDV fdv;
    Rank() {fdv = store.newFDV(this, min, max);}
    public FDV getFD() {return fdv;}
  }

  public static void main (String args[]) {

    System.out.println("This is a Program to solve the Spy Girls puzzle.");

  store.imposeXeqC(Rank.First, 5);
  store.imposeXeqC(Rank.Second, 4);
  store.imposeXeqC(Rank.Third, 3);
  store.imposeXeqC(Rank.Fourth, 2);
  store.imposeXeqC(Rank.Fifth, 1);

  //clue 1
  store.impose(new XgtY(Fname.Silvia.getFD(), Lname.Adam.getFD()));
  store.impose(new XneqY(Fname.Silvia.getFD(), Rank.First.getFD()));

  //clue 2
  store.impose(new XgtY(Fname.Hillary.getFD(), Lname.Darling.getFD()));
  store.impose(new XltY(Fname.Hillary.getFD(), Lname.Brooke.getFD()));

  //clue 3
  store.impose(new XgtY(Fname.Amanda.getFD(), Fname.Kate.getFD()));
  store.impose(new XgtY(Fname.Amanda.getFD(), Fname.Hillary.getFD()));

  //clue 4
  store.impose(new XgtY(Lname.Adam.getFD(), Lname.Miller.getFD()));
  store.impose(new XltY(Lname.Adam.getFD(), Lname.Cooper.getFD()));

  //clue 5
  store.impose(new XneqY(Lname.Miller.getFD(), Rank.Fifth.getFD()));

 FDStore.printFDVEnumArrays("\nBefore consistency check:",
            new FDVEnum[][] {Rank.values(),
                             Fname.values(),
                             Lname.values()});

    Solver.consistency(store);
    FDStore.printFDVEnumArrays("\nAfter consistency check:",
            new FDVEnum[][] {Rank.values(),
                             Fname.values(),
                             Lname.values()});


    long T1 = System.currentTimeMillis();
    boolean result =
      Solver.searchOne(store, store, new SearchOne(), new IndomainMin(),
                       new Delete(new MostConstrainedDynamic()));
    long T2 = System.currentTimeMillis();
    if (result) {
      System.out.println();

      FDStore.printFDVEnumArrays("\nAfter searchOne:",
              new FDVEnum[][] {Rank.values(),Fname.values(), Lname.values()});
      System.out.println();

   for (int i = 1; i <=5 ; i++)
{

                 System.out.println("  " + i + ": " +
                           getFDVByValue(Rank.values(), i) + ", " +
                           getFDVByValue(Fname.values(), i) + ", " +
                           getFDVByValue(Lname.values(), i));
      }
    } else System.out.println("No solution.");
    System.out.println("\n\t*** Execution time = " + (T2 - T1) + " ms");

  }

 static FDV[] fdvEnumsToFDVs(FDVEnum[] fdvsEnums) {
    FDV[] fdvs = new FDV[fdvsEnums.length];
    int i = 0;
    for (FDVEnum fdvE: fdvsEnums) fdvs[i++] = fdvE.getFD();
    return fdvs;
  }

  private static FDVEnum getFDVByValue(FDVEnum[] fdvsEnums, int value) {
    for (FDVEnum fdvE: fdvsEnums) {
      FDV fdv = fdvE.getFD();
      if (fdv.singleton() && fdv.value() == value) return fdvE;
    }
    System.out.println("No matching singleton matching " + value +
                       " in: " + Arrays.asList(fdvsEnums));
    return null;
  }

}