001 /* 002 * Created on Sep 21, 2004 003 * 004 */ 005 package aima.search.csp; 006 007 import java.util.ArrayList; 008 import java.util.Hashtable; 009 import java.util.List; 010 011 /** 012 * @author Ravi Mohan 013 * 014 */ 015 016 public class MapCSP extends CSP { 017 public static String WA = "WA"; 018 019 public static String NT = "NT"; 020 021 public static String SA = "SA"; 022 023 public static String Q = "Q"; 024 025 public static String NSW = "NSW"; 026 027 public static String V = "V"; 028 029 public static String T = "T"; 030 031 public static String RED = "RED"; 032 033 public static String BLUE = "BLUE"; 034 035 public static String GREEN = "GREEN"; 036 037 private MapCSP(List<String> variables, Constraint constraints) { 038 super(variables, constraints); 039 040 } 041 042 public static CSP getMap() { 043 List<String> variables = new ArrayList<String>(); 044 variables.add(WA); 045 variables.add(NT); 046 variables.add(SA); 047 variables.add(Q); 048 variables.add(NSW); 049 variables.add(V); 050 variables.add(T); 051 052 List<String> colors = new ArrayList<String>(); 053 colors.add(RED); 054 colors.add(BLUE); 055 colors.add(GREEN); 056 057 Domain domains = new Domain(variables); 058 for (int i = 0; i < variables.size(); i++) { 059 String variable = variables.get(i); 060 domains.addToDomain(variable, colors); 061 } 062 063 Hashtable<String, List<String>> neighbors = new Hashtable<String, List<String>>(); 064 addToNeighbors(neighbors, T); 065 addToNeighbors(neighbors, WA, NT, SA); 066 addToNeighbors(neighbors, NT, WA, SA, Q); 067 addToNeighbors(neighbors, SA, WA, NT, Q, NSW, V); 068 addToNeighbors(neighbors, Q, NT, SA, NSW); 069 addToNeighbors(neighbors, NSW, SA, Q, V); 070 addToNeighbors(neighbors, V, SA, NSW); 071 Constraint mapConstraints = new MapColoringConstraint(neighbors); 072 073 return new CSP(variables, mapConstraints, domains); 074 } 075 076 public static void addToNeighbors( 077 Hashtable<String, List<String>> neighbors, String whose) { 078 List<String> l = new ArrayList<String>(); 079 neighbors.put(whose, l); 080 } 081 082 public static void addToNeighbors( 083 Hashtable<String, List<String>> neighbors, String whose, String one) { 084 List<String> l = new ArrayList<String>(); 085 l.add(one); 086 neighbors.put(whose, l); 087 } 088 089 public static void addToNeighbors( 090 Hashtable<String, List<String>> neighbors, String whose, 091 String one, String two) { 092 List<String> l = new ArrayList<String>(); 093 l.add(one); 094 l.add(two); 095 neighbors.put(whose, l); 096 } 097 098 public static void addToNeighbors( 099 Hashtable<String, List<String>> neighbors, String whose, 100 String one, String two, String three) { 101 List<String> l = new ArrayList<String>(); 102 l.add(one); 103 l.add(two); 104 l.add(three); 105 neighbors.put(whose, l); 106 } 107 108 public static void addToNeighbors( 109 Hashtable<String, List<String>> neighbors, String whose, 110 String one, String two, String three, String four) { 111 List<String> l = new ArrayList<String>(); 112 l.add(one); 113 l.add(two); 114 l.add(three); 115 l.add(four); 116 neighbors.put(whose, l); 117 } 118 119 public static void addToNeighbors( 120 Hashtable<String, List<String>> neighbors, String whose, 121 String one, String two, String three, String four, String five) { 122 List<String> l = new ArrayList<String>(); 123 l.add(one); 124 l.add(two); 125 l.add(three); 126 l.add(four); 127 l.add(five); 128 neighbors.put(whose, l); 129 } 130 }