====== Archive eFLP 2011: Prolog assignment 2: Vector formula ====== Do you believe Prolog is only good for boring list manipulation and complicated recursion? Not any more! Your task is to create an artificial intelligence for the [[http://en.wikipedia.org/wiki/Racetrack_(game) | “Vector formula”]] game. Your car will go through the track as fast as possible, respecting physical limits of the traction. As a crash course into the basic principle of vector formula, spend a minute playing [[http://www.harmmade.com/vectorracer | Vector racer]] online. The game is played on a squared sheet of paper (a matrix in our case). There is only one car in the race (which is yours) and at the beginning, it is placed on a predefined position “checkpoint 0”. As the game is “real-time”, at each step your car’s position (x,y) moves according to the speed vector
--00--------[] --[][][]----11 --22----------The symbol “00” is the starting position, “11” is the first checkpoint and “22” is the last checkpoint. Cells “--” represent the track and “[]” is a wall (the car can not go through a wall). Start inspecting the given code by printing this map by executing the query: ''?- [basic,race]. ?- map(Map), plot(Map).'' By inspecting the source code, you will find that the map is a matrix (list of lists of equal length), that the track has the symbol ‘-’ in the Map matrix and the wall has the symbol ‘x’. The ultimate goal of this assignment is to produce the list of //waypoints//, which is a list of coordinates (= a list of 2 numbers [x,y]) of your car at each time step. Assuming that the top-left corner is [1,1], a valid set of waypoints for the ''basic'' map is
--<0<>--<><>[] --[][][]----<1 <>22--<>--<><>===== Question 1: Low-level tools ===== Define a predicate similar to ''member'': ''nth(X,List,N)'' holds iff ''X'' is the ''N''-th member of ''List''. E.g. the query ''nth(X,[a,b],Y)'' must answer ''X=a,Y=1'' and then ''X=b,Y=2''. While searching, you will need to retrieve the position at specific coordinates in the map. Formally given a matrix, return the cell value at the specified coordinate. Define ''cell(Matrix,[X,Y],Value)'' s.t. cell([[a,b],[c,d]], [2,1], X) gives ''X=b''. It is highly recommended to use ''nth'' for the definition of ''cell''. **//2 points, one for each predicate.//** ===== Question 2: Find the path ===== In order to check for obstacles and checkpoints, one needs to compute the path from a set of waypoints by finding coordinates of a straight line between two arbitrary points in the map. Firstly define the Euclidean metric euclid([X1,Y1], [X2,Y2], D) to compute the distance between 2 points. As an example euclid([1,6],[4,2],D) should return D=sqrt((1-4)^2+(6-2)^2)=sqrt(25)=5. **//1 point//** Given two points [x1,y1] and [x2,y2], find all intermediate points on a line between these two. Use the the [[http://en.wikipedia.org/wiki/Beam_search | beam search]] strategy, which is a greedy (//beam width// = 1) heuristic search. Start by enumerating all points adjacent to [x1,y1]:
Adj=[[x1+1,y1], [x1-1,y1], [x1,y1+1], [x1,y1-1], [x1+1,y1+1],[x1+1,y1-1],[x1-1,y1+1],[x1-1,y1-1]].Then pick the closest point to [x2,y2] from the set Adj as the first intermediate point. Reapply the same procedure from the intermediate point until you reach [x2,y2]. Your predicate will give answers such as: * beam([1,1],[5,5],X) => X=[[2,2],[3,3],[4,4]] * beam([1,1],[5,3],X) => X=[[2,2],[3,3],[4,3]] Please note that the predicate should produce only one exact answer and is should not allow backtracking (this property is caused by the //beam width// being equal to 1). You may find the predicate sort(List,Sorted) very useful for finding the “closest” points. It works as follows: sort([[2,x],[3,y],[1,z]],X) gives X=[[1,z],[2,x],[3,y]]. **//3 points//** ===== Question 3: Finish the track ===== In this question, you will find a way through the whole track. Your task is to produce a list of waypoints. Firstly define the terminating condition. Write ''finished(Map,Way)'', which succeeds if ''Way'' is a valid list of waypoints for ''Map'': * **The car stays on the map:** All coordinates in the ''Way'' must be greater or equal to [1,1] and smaller or equal to the dimensions of the map. * **The physical limits of the traction are satisfied:** The second derivative of the ''Way'' is not greater than 1. E.g. the