/* Map coloring Australia in Picat. Example from Marriot & Stuckey "Programming with Constraints", page 86. Western Australia WA Northen Territory NT South Australia SA Queensland Q New South Wales NSW Victoria V Tasmania T Colors: red, green, blue (1..3) This shows the different labeling method's influences. This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. import trace_domains. import trace_domains_ar. main => go. go ?=> regions(Regions), println(regions=Regions), RegionMap = new_map([R=I : {R,I} in zip(Regions,1..Regions.len)]), println(regionMap=RegionMap), Neighbours = find_all([N1,N2],neighbour(N1,N2)), % println(Neighbours), Len = Neighbours.len, selection(Variables), choice(Values), member(Var,Variables), member(Val,Values), println(var=Var), println(val=Val), trace_domain_ar_reset(), once(color(Regions,RegionMap,Neighbours,Var,Val, Color)), % trace_domains_stat(), fail, nl. go => true. color(Regions,RegionMap,Neighbours,Var,Val, Color) => Color = new_list(Regions.len), Color :: 1..4, % NumColors #= max(Color), % trace_domains(Color,'Color',"Init"), trace_domain_ar(Color,"Color"), foreach([N1,N2] in Neighbours) Color[RegionMap.get(N1)] #!= Color[RegionMap.get(N2)] end, % println(color=Color), % println(degrees=[fd_degree(C) : C in Color]), solve($[Var,Val],Color), println(color=Color), nl. regions(Regions) => Regions = [wa,nt,sa,q,nsw,v,t]. index(-,-) neighbour(wa,nt). neighbour(wa,sa). neighbour(nt,wa). neighbour(nt,sa). neighbour(nt,q). neighbour(sa,wa). neighbour(sa,nt). neighbour(sa,q). neighbour(sa,nsw). neighbour(sa,v). neighbour(q,nt). neighbour(q,sa). neighbour(q,nsw). neighbour(nsw,sa). neighbour(nsw,q). neighbour(nsw,v). neighbour(v,sa). neighbour(v,nsw). neighbour(v,t). neighbour(t,v). % Variable selection selection(Variable) => Variable = [backward,constr,degree,ff,ffc,forward,inout,leftmost,max,min]. % Value selection choice(Value) => Value = [up,down,updown,split,reverse_split].