12.07.2015 Views

Programming in Eclipse

Programming in Eclipse

Programming in Eclipse

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Constra<strong>in</strong>t <strong>Programm<strong>in</strong>g</strong>Introduction to F<strong>in</strong>ite-Doma<strong>in</strong>Constra<strong>in</strong>t Logic <strong>Programm<strong>in</strong>g</strong>Alan M. FrischArtificial Intelligence GroupDepartment of Computer ScienceUniversity of YorkUpdated 27 Nov 2008


Reversable Constra<strong>in</strong>t Logic Program:Summ<strong>in</strong>g Elements of a ListProgramsumlist([],0).sumlist([H|T],Y) :- sumlist(T,X), Y #= H+X.Behaviour[eclipse 15]: sumlist([2,3],X).X = 5yes.[eclipse 16]: sumlist([2,X],5).X = 3yes.4


Reversable Constra<strong>in</strong>t Logic Program:Summ<strong>in</strong>g Elements of a ListMore Behaviour[eclipse 17]: sumlist(X,Y).X = []Y = 0 More? (;)X = [H ]Y = H More? (;)X = [H , H ]Y = YDelayed goals: (This says Y #= H1 + H2)0 - H - H + Y #=0 More? (;)X = [H , H, H]Y = YDelayed goals: (This says X #= H1+H2, Y#= H3+X)0 - H - H + X #= 00 - X - H + Y #= 0 More? (;)5


How Logic Programs Operate• While the goal is not empty do– Use selection function to select a literal L <strong>in</strong> thegoal.– Non-determ<strong>in</strong>istically chose a program clause,H :- B, such that H and L are unifiable. If there isno such clause, then Fail. Let θ be the mostgeneral unifier of H and L.– Modify the goal by replac<strong>in</strong>g L with B and thenapply<strong>in</strong>g θ to the goal• Succeed6


How Constra<strong>in</strong>t Logic Programs Operate• Set the constra<strong>in</strong>t store to empty• While goal is not empty do– Use selection function to select a literal L <strong>in</strong> thegoal.–If L is an ord<strong>in</strong>ary logical literal then– Else add L to the constra<strong>in</strong>t store– Simplify the constra<strong>in</strong>t store (perhaps by GAC).If unsatisfiability is detected, then Fail• Succeedas before7


How Logic Programs Operate• While the goal is not empty do– Use selection function to select a literal L <strong>in</strong> thegoal.– Non-determ<strong>in</strong>istically chose a program clausewhose head unifies with L. If there is no suchclause, then Fail. Let θ be the most generalunifier and B be the result of apply<strong>in</strong>g θ to thebody of the clause.– Modify the goal by replac<strong>in</strong>g L with B• Succeed8


Example: Send More Money Problem (1):- lib(ic).sendmore1(Digits) :-Digits = [S,E,N,D,M,O,R,Y],Digits :: [0..9],alldifferent(Digits),S #\= 0,M #\= 0,1000*S + 100*E + 10*N + D+ 1000*M + 100*O + 10*R + E#= 10000*M + 1000*O + 100*N + 10*E + Y,label<strong>in</strong>g(Digits).9


Label<strong>in</strong>g• The predicate label<strong>in</strong>g is def<strong>in</strong>ed <strong>in</strong> ic library• Could easily be written aslabel<strong>in</strong>g([]).label<strong>in</strong>g(H|T) :-<strong>in</strong>doma<strong>in</strong>(H),label<strong>in</strong>g(T).• <strong>in</strong>doma<strong>in</strong> is def<strong>in</strong>ed <strong>in</strong> the ic library.10


Example: Send More Money Problem (2)Idea: Use carries and set up a constra<strong>in</strong>t foreach column.C1 C2 C3 C4S E N DM O R E________________________M O N E YWhat are the doma<strong>in</strong>s of the carry variables?11


Example: Send More Money Problem (2):- lib(ic).sendmore2(Digits) :-Digits = [S,E,N,D,M,O,R,Y],Digits :: [0..9],Carries = [C1,C2,C3,C4],Carries :: [0..1],alldifferent(Digits),S #\= 0, M #\= 0, C1 #= M,C2 + S + M #= O + 10*C1,C3 + E + O #= N + 10*C2,C4 + N + R #= E + 10*C3,D + E #= Y + 10*C4,label<strong>in</strong>g(Carries),label<strong>in</strong>g(Digits).What wouldhappen if thesewere swapped ?12


Example: N-Queens:- lib(ic).nqueens(N,Rows) :-length(Rows,N), % def<strong>in</strong>e N variablesRows::1..N, % give doma<strong>in</strong>s to varssafe(Rows), % impose constra<strong>in</strong>tslabel<strong>in</strong>g(Rows). % search13


Example: N-Queens (cont<strong>in</strong>ued)/* ===== Set up the constra<strong>in</strong>ts ============ */safe(Rows) :-alldifferent(Rows),nondiagonal(Rows)./* no attacks on a diagonal */nondiagonal([]).nondiagonal([Column|Columns]) :-nonattack(Column,Columns,1),nondiagonal(Columns).nonattack(_,[],_).nonattack(Column1,[Column2|Columns],Offset) :-Column1 #\= Column2 + Offset,Column1 #\= Column2 - Offset,NewOffset is Offset + 1,nonattack(Column1, Columns, NewOffset).14


• In Department<strong>Eclipse</strong> System– Information (out of date) on Dept <strong>in</strong>stallation atwww.cs.york.ac.uk/support/l<strong>in</strong>ux/softcat.php?pkg=eclipse– Commands “eclipse” or “tkeclipse” or “jeclipe”starts version 6.0 #42• For your own computer– Download source and b<strong>in</strong>aries for L<strong>in</strong>ux, W<strong>in</strong>dowsNT or other systems fromhttp://www.eclipse-clp.org15


<strong>Eclipse</strong> Documentation• Available at www.eclipse-clp.org• To get sarted, the most useful documents are– ECLIPSE: A Tutorial Introduction.– ECLIPSE Constra<strong>in</strong>t Library Manual• Later use the on-l<strong>in</strong>e Reference Manual– Provides list of all libraries and a list of all built-<strong>in</strong>predicate16


Introduction to Us<strong>in</strong>g <strong>Eclipse</strong>• To load the ic library for f<strong>in</strong>ite doma<strong>in</strong>s use either:-use_module(library(ic)).:-lib(ic).• To enter “<strong>in</strong>put mode” use the command[user].and then exit with a control-d• To compile code <strong>in</strong> a file, use the command[filename].• To get history of command l<strong>in</strong>e:h.• To repeat a previous command:7.-1.17


Example: Magic Squares(from Joachim Schimpf):- lib(ic).magic(N) :-NN is N*N,% number of cellsSum is N*(NN+1)//2, % the magical sumpr<strong>in</strong>tf("Sum = %d%n", [Sum]),dim(Square, [N,N]), % make the variablesSquare[1..N,1..N] :: 1..NN,% set the doma<strong>in</strong>sRows is Square[1..N,1..N],flatten(Rows, Vars),alldifferent(Vars), % cells are all different18


Example: Magic Squares (cont<strong>in</strong>ued)( for(I,1,N),foreach(U,UpDiag),foreach(D,DownDiag),param(N,Square,Sum)doSum #= sum(Square[I,1..N]), % sum of row ISum #= sum(Square[1..N,I]), % sum of col IU is Square[I,I],D is Square[I,N+1-I]),Sum #= sum(UpDiag),% diagonal sumsSum #= sum(DownDiag),19


Example: Magic Squares (cont<strong>in</strong>ued)Square[1,1] #< Square[1,N],Square[1,1] #< Square[N,N],Square[1,1] #< Square[N,1],Square[1,N] #< Square[N,1],mylabel<strong>in</strong>g(Vars),pr<strong>in</strong>t_square(Square).% symmetry removal% pr<strong>in</strong>t resultpr<strong>in</strong>t_square(Square) :-dim(Square, [N,N]),( for(I,1,N), param(N,Square) do( for(J,1,N), param(I,Square) doField is Square[I,J],pr<strong>in</strong>tf("%3d", Field)), nl), nl.20


Example: Magic Squares (cont<strong>in</strong>ued)mylabel<strong>in</strong>g(AllVars) :-( fromto(AllVars, Vars, Rest, []) dodelete(X,Vars,Rest,0,first_fail),<strong>in</strong>doma<strong>in</strong>(X,m<strong>in</strong>)).An alternative:mylabel<strong>in</strong>g(AllVars,UpDiag,DownDiag) :-label<strong>in</strong>g(UpDiag),label<strong>in</strong>g(DownDiag),label<strong>in</strong>g(AllVars),21

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!