getting a prolog program to print out results instead of booleans -


imagine 2d map 6 regions r1-r6. each region should colored in 1 of 4 colors, no adjacent regions can same color.

here code:

% #1 initial facts next(red,green). next(red,blue). next(red,yellow). next(green,red). next(green,blue). next(green,yellow). next(blue,green). next(blue,yellow). next(blue,red). next(yellow,red). next(yellow,blue). next(yellow,green). % #1 actual program color(r1,r2,r3,r4,r5,r6). color(r1,r2,r3,r4,r5,r6):-     % region adjacency relations     next(r1,r2),     next(r1,r3),     next(r1,r4),     next(r2,r4),     next(r2,r5),     next(r3,r4),     next(r3,r6),     next(r4,r5),     next(r4,r6). 

expected output:

r1= red, r2= blue, r3= blue, r4= green, r5= red, r6= red 

my output:

true 

what doing wrong? wrong? if code finding right color configuration, how print out findings?

your program general due first clause of color/6. solution expect (as 1 among many different solutions) if remove first clause.

there more beautiful way write program:

regions(rs):-         rs = [r1,r2,r3,r4,r5,r6],         % neighbouring regions have different colors         dif(r1, r2),         dif(r1, r3),         dif(r1, r4),         dif(r2, r4),         dif(r2, r5),         dif(r3, r4),         dif(r3, r6),         dif(r4, r5),         dif(r4, r6),         maplist(color, rs).  color(red). color(green). color(blue). color(yellow). 

example query , sample solutions:

?- regions(rs). rs = [red, green, green, blue, red, red] ; rs = [red, green, green, blue, red, yellow] ; rs = [red, green, green, blue, yellow, red] ; etc. 

notice use of dif/2 () state 2 terms different.

for more serious map coloring tasks, consider using constraints.


Comments