Commit ce4e99b9 by Leon Schmidtchen

### Merge branch 'master' of https://gl.kwarc.info/teaching/AI

parents 5327cab8 18d5853c
 % HW3 Uninformed Search: Definitions and Hints % First, we define trees as in the exercise sheet: subtrees([]). subtrees([(Cost,T)|Rest]) :- number(Cost),istree(T), subtrees(Rest). istree(tree(Value,Children)) :- string(Value),subtrees(Children). % We'll add a couple of test trees: testTree1(tree("A",[])). testTree2(tree("A",[(2,tree("B",[(5,tree("C",[]))])),(3,tree("B",[(10,tree("D",[]))]))])). testTree3(tree("A",[(2,tree("B",[(5,tree("C",[])),(60,tree("D",[])),(42,tree("H",[]))])),(3,tree("C",[(10,tree("E",[(60,tree("J",[(60,tree("D",[(60,tree("I",[]))]))]))]))]))])). testTree4(tree("A",[(2,tree("B",[(5,tree("C",[]))])),(3,tree("C",[(10,tree("D",[]))]))])). treeZ(tree("Z",[])). treeY(tree("Y",[])). treeX(tree("X",[])). treeW(tree("W",[])). treeV(tree("V",[])). treeU(tree("U",[])). treeT(tree("T",[])). treeS(tree("S",[])). treeR(tree("R",[(5,TreeZ)])) :- treeZ(TreeZ). treeQ(tree("Q",[])). treeP(tree("P",[])). treeO(tree("O",[])). treeN(tree("N",[])). treeM(tree("M",[(21,TreeY)])) :- treeY(TreeY). treeL(tree("L",[(30,TreeX)])) :- treeX(TreeX). treeK(tree("K",[(28,TreeW)])) :- treeW(TreeW). treeJ(tree("J",[(14,TreeV)])) :- treeV(TreeV). treeI(tree("I",[(8,TreeT),(7,TreeU)])) :- treeT(TreeT), treeU(TreeU). treeH(tree("H",[(6,TreeS)])) :- treeS(TreeS). treeG(tree("G",[(14,TreeQ),(3,TreeR)])) :- treeQ(TreeQ), treeR(TreeR). treeF(tree("F",[(18,TreeP)])) :- treeP(TreeP). treeE(tree("E",[(3,TreeN),(12,TreeO)])) :- treeN(TreeN), treeO(TreeO). treeD(tree("D",[(3,TreeK),(17,TreeL),(20,TreeM)])) :- treeK(TreeK),treeL(TreeL),treeM(TreeM). treeC(tree("C",[(14,TreeH),(8,TreeI),(11,TreeJ)])) :- treeH(TreeH),treeI(TreeI),treeJ(TreeJ). treeB(tree("B",[(3,TreeE),(1,TreeF),(2,TreeG)])) :- treeE(TreeE),treeF(TreeF),treeG(TreeG). treeA(tree("A",[(5,TreeB),(6,TreeC),(4,TreeD)])) :- treeB(TreeB),treeC(TreeC),treeD(TreeD). % Check that they work % ?- testTree1(A), istree(A), testTree2(B), istree(B), testTree3(C), istree(C), treeA(NA), istree(NA). % - - - - - - - - - - - - - - - - - - - - - - - - % The following might be useful % - - - - - - - - - - - - - - - - - - - - - - - - % Unifying terms treeRoot(Tree) :- Tree = tree(Root,_), % unify Term1 with Term2 write(Root). % ?- testTree1(T), treeRoot(T). % String concatenation % ?- string_concat("a","b",S). % Semicolon: an "or" in Prolog tomJerry(X) :- X = "Tom"; X = "Jerry". % ?- findall(X,tomJerry(X),Y). % Appending % ?- append(["a","b"],["c"],L). % Sorting % ?- sort([3,5,2,6,4,1],L) \ No newline at end of file
 % Map Coloring % Original problem: https://www.tjhsst.edu/~rlatimer/assignments2004/colors.txt % It's known that only 4 colors are needed to paint any map so that no two neighboring states have the same color. % Write a Prolog program that receives a map and a list of 4 colors and produces a colored map. % The map is represented by a list of states, each of which is a state name and a list of neighboring states. % For example, the maps of Australia and Western Europe are represented by the following maps. % Maps of Australia and West Europe, colors, define getcolor map('Australia', [wa: [nt,sa], nt: [wa,sa,qld], qld:[nt,sa,nsw], nsw: [qld,sa,vic], vic: [sa,nsw], tas:[], sa: [wa,nt,qld,nsw,vic]]). map('West Europe', [portugal: [spain], spain: [portugal, france], belgium: [france, holland, luxembrg, germany], holland: [belgium, germany], luxembrg: [france, belgium, germany], switzerld: [france,germany,austria, italy], italy: [france,switzerld, austria], austria: [germany, switzerld, italy], france: [spain,belgium,luxembrg, germany,switzerld, italy], germany:[holland,belgium,luxembrg,france,switzerld, austria]]). colors([red,green,blue,yellow]). % - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - % Edit section arrangecolors([],_,ColoredMap,ColoredMap). % Tracing: % :- write('Colormap='), write(ColoredMap),nl. arrangecolors([Country:Neighbors|Rest], ColorList, TempList, ColoredMap) :- member(Color,ColorList), % Tracing: % write('Color:'), write(Color),write(' TempList:'), write(TempList),nl, different(Color,Neighbors,TempList) arrangecolors(Rest,ColorList,[Country:Color|TempList], ColoredMap). % TempList is in the format [country1:color1,country2:color2,...] restrictedness(Neighbors,TempList,R) :- length(Neighbors,1), length(TempList,1), R = 1. % - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - % Maybe useful things findneighbors(X, Map, Who) :- map(Map, Countries), neighbors(X,Countries,Who). neighbors(_,[],[]). neighbors(X,[X:Neighbors|_], Neighbors) :- !. % Try this without the ! neighbors(X,[_|RestofCountries], Neighbors) :- neighbors(X,RestofCountries, Neighbors). adjacent(X,Y,Map) :- findneighbors(X, Map, Countries), member(Y, Countries). % - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - % This you will most likely use different(C, Nbs, [S1:C1|Rest]) :- not((member(S1,Nbs), C = C1)), different(C, Nbs,Rest). different(_,_,[]). availablecolors(_,[],Colors,Colors). availablecolors(Neighbors,[NB:Color|TempListTail],TempColors,Colors) :- member(NB,Neighbors) -> availablecolors(Neighbors,TempListTail,[Color|TempColors],Colors); availablecolors(Neighbors,TempListTail,TempColors,Colors). countrieswithconstraints([],_,L,L). countrieswithconstraints([Country:Neighbors|Rest],TempList,Accumulator,CountriesWithConstraints) :- countrieswithconstraints(Rest,TempList,[R-(Country:Neighbors)|Accumulator],CountriesWithConstraints), restrictedness(Neighbors,TempList,R). sortedcountries(Countries,TempList,SortedCountries) :- countrieswithconstraints(Countries,TempList,[],CountriesWithConstraints), keysort(CountriesWithConstraints,SortedCountries). % - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - % Main paint(Map,ColoredMap) :- map(Map, Countries), colors(ColorList), % Tracing: % write('ColoredMap is '), write(ColoredMap), nl. arrangecolors(Countries,ColorList,[], ColoredMap). % paint('Australia',ColoredMap). \ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!