For a project, I have to write a basic maze in Prolog. The only problem is that I do not know how to write a KB that represents the maze in the below picture.
This is what I currently have, but I should be able to find pats from letter to letter.
% size of maze, including barriers.
mazeSize(7,7)
barrier(1,6).
barrier(2,2).
barrier(2,3).
barrier(3,2).
barrier(3,6).
barrier(4,1).
barrier(5,4).
barrier(5,6).
barrier(6,1).
barrier(7,4).
And this is what it should look like:
Hopefully someone can help me! Thanks in advance!
Your approach is reasonable, but you would also need facts something like node_name(1, 1, a) to map between node names and coordinates. Also, a 7x7 maze will clearly not correspond to the shown 4x4 maze.
A simpler solution that would not need coordinates at all would be one that only enumerates connected nodes, but not barriers:
connection(a, b).
connection(b, f).
and so on, but not connection(b, c) for example. Note that you will probably need to express that connections are two-way, so b and a are also connected.
Related
So I've been learning Prolog, and to test myself I wanted to solve a certain puzzle. I think I'm pretty close to solving it, but I seem to be doing something wrong. It seems my predicate block/3 is not called.
maybe it's a syntax error, or I'm doing something that doesn't work in Prolog. I can't see it.
block/3 is supposed to give all possible combinations of sets in block/2.
I'm not sure if it's entirely relevant but I'll include the goal of the puzzle:
There's four cubes, with different combination of four images on their faces. (Kim,Lab,Hail and Com)
The goal is to align the cubes in such a way that if you put them together, all the sides next to each other should be the same. So it's four of the same rows going around each cube, and then two sides that should also be matching.
I wrote the program to just solve the rows, and disregarding the orientation of the images and the two sides. Should that give more than one answer it shouldn't give too many to manually try.
anyway, somehow solve(X) completely ignores my predicate block/3. I've been staring at it for a long time and I can't find the issue.
member( block(1, _, Row), X )
is equivalent to
E = block(1, _, Row), member( E, X )
so it does not in fact call block/3 as a predicate, it just uses it as a compound term, symbolically.
Sorry by the first ask, I'm new at the hood... I did a cleaning on the code. The problem is:
I have a square grid with paths and obstacles. I want to find the shortest path from a point to another. This is part of an artificial intelligence. When the path is too large, I can not see the whole list of points on the bash, but in the game, the character who travels this path, it does not at shortest path. So, my question is, how I can change this code to solve the shortest path. Thank you so much!
mov(X1,Y1,X2,Y2):-
pos(X1,Y1), X2 is X1 , Y2 is Y1+1 ,pos(X2,Y2).
mov(X1,Y1,X2,Y2):-
pos(X1,Y1), X2 is X1 , Y2 is Y1-1 ,pos(X2,Y2).
mov(X1,Y1,X2,Y2):-
pos(X1,Y1), X2 is X1+1 , Y2 is Y1 , pos(X2,Y2).
mov(X1,Y1,X2,Y2):-
pos(X1,Y1), X2 is X1 -1 , Y2 is Y1 , pos(X2,Y2).
path(X1,Y1,X2,Y2,Path) :-
travel(pos(X1,Y1),pos(X2,Y2),[pos(X1,Y1)],Q),
reverse(Q,Path).
travel(pos(X1,Y1),pos(X2,Y2),P,[pos(X2,Y2)|P]) :-
mov(X1,Y1,X2,Y2).
travel(pos(X1,Y1),pos(X2,Y2),Visited,Path) :-
mov(X1,Y1,X,Y),
pos(X,Y) \== pos(X2,Y2),
\+member(pos(X,Y),Visited),
travel(pos(X,Y),pos(X2,Y2),[pos(X,Y)|Visited],Path).
First some Prolog advice.
member/2 is a built-in, you should not have to define it.
ISO negation is \+, not not/1.
For performance, memberchk/2 beats member/2.
I see a lot of foo(X,Y) :- X == Y, ... in your code. It's much better if you just say foo(X,X) and save yourself the trouble of making explicit tests like this, unless you're going to do a conditional expression to avoid a choice point or something.
Lots of cuts in this code. Cuts and bugs tend to be great friends because the cut can undermine reasonable-looking code by preventing it from being executed.
If I had to solve this problem, I would want to separate the shortest-path logic from the grid traversal logic. You'll never be able to debug this, and even if you do, what you'll have is a one of those unreadable blocks of code that cannot be modified. It's clear that you have an explosion of terms because you're embedding the traversal logic in the path finding logic. Break them out into two separate steps and you will probably find that you get smaller pieces you can meaningfully test and debug. This is a good way of life with programming, regardless of the language: what would you do if you needed to change the grid structure or make the pathfinding more intelligent or complex? Keeping pieces granular always helps for managing change.
As for S.O. etiquette, this isn't great: you should talk about what doesn't work and what you've tried and you want to supply a minimum, complete, verifiable example. I suspect in producing such a thing you'd probably solve the problem yourself.
I'm trying to re-familiarize myself with Prolog and I thought this could be the type of problem with an elegant solution in Prolog.
I'm following along this example:
http://home.deib.polimi.it/matteucc/Clustering/tutorial_html/hierarchical.html
I've tried a variety of data formats:
dist('BA','FI',662).
dist(0,'BA','FI',662).
dist(['BA'],['FI'],662).
but I haven't found any particular one most suitable.
Here's all the data in the first format:
%% Graph distances
dist('BA','FI',662).
dist('BA','MI',877).
dist('BA','NA',255).
dist('BA','RM',412).
dist('BA','TO',996).
dist('FI','MI',295).
dist('FI','NA',468).
dist('FI','RM',268).
dist('FI','TO',400).
dist('MI','NA',754).
dist('MI','RM',564).
dist('MI','TO',138).
dist('NA','RM',219).
dist('NA','TO',869).
dist('RM','TO',669).
Now, there seems to be some awesome structure to this problem to exploit, but I'm really struggling to get a grasp of it. I think I've got the first cluster here (thought it may not be the most elegant way of doing it ;)
minDist(A,B,D) :- dist(A,B,D), dist(X,Y,Z), A \= X, A \= Y, B \= X, B \= Y, D < Z.
min(A,B,B) :- B < A
min(A,B,A) :- A < B
dist([A,B],C, D) :- minDist(A,B,D), dist(A,C,Q), dist(B,C,W), min(Q,W,D)
The problem I have here is the concept of "replacing" the dist statements involving A and B with the cluster.
This just quickly become a brainteaser for me and I'm stuck. Any ideas on how to formulate this? Or is this perhaps just not the kind of problem elegantly solved with Prolog?
Your table is actually perfect! The problem is that you don't have an intermediate data structure. I'm guessing you'll find the following code pretty surprising. In Prolog, you can simply use whatever structures you want, and it will actually work. First let's get the preliminary we need for calculating distance without regard for argument order:
distance(X, Y, Dist) :- dist(X, Y, Dist) ; dist(Y, X, Dist).
This just swaps the order if it doesn't get a distance on the first try.
Another utility we'll need: the list of cities:
all_cities(['BA','FI','MI','NA','RM','TO']).
This is just helpful; we could compute it, but it would be tedious and weird looking.
OK, so the end of the linked article makes it clear that what is actually being created is a tree structure. The article doesn't show you the tree at all until you get to the end, so it isn't obvious that's what's going on in the merges. In Prolog, we can simply use the structure we want and there it is, and it will work. To demonstrate, let's enumerate the items in a tree with something like member/2 for lists:
% Our clustering forms a tree. So we need to be able to do some basic
% operations on the tree, like get all of the cities in the tree. This
% predicate shows how that is done, and shows what the structure of
% the cluster is going to look like.
cluster_member(X, leaf(X)).
cluster_member(X, cluster(Left, Right)) :-
cluster_member(X, Left) ; cluster_member(X, Right).
So you can see we're going to be making use of trees using leaf('FI') for instance, to represent a leaf-node, a cluster of N=1, and cluster(X,Y) to represent a cluster tree with two branches. The code above lets you enumerate all the cities within a cluster, which we'll need to compute the minimum distance between them.
% To calculate the minimum distance between two cluster positions we
% need to basically pair up each city from each side of the cluster
% and find the minimum.
cluster_distance(X, Y, Distance) :-
setof(D,
XCity^YCity^(
cluster_member(XCity, X),
cluster_member(YCity, Y),
distance(XCity, YCity, D)),
[Distance|_]).
This probably looks pretty weird. I'm cheating here. The setof/3 metapredicate finds solutions for a particular goal. The calling pattern is something like setof(Template, Goal, Result) where the Result will become a list of Template for each Goal success. This is just like bagof/3 except that setof/3 gives you unique results. How does it do that? By sorting! My third argument is [Distance|_], saying just give me the first item in the result list. Because the result is sorted, the first item in the list will be the smallest. It's a big cheat!
The XCity^YCity^ notation says to setof/3: I don't care what these variables actually are. It marks them as "existential variables." This means Prolog will not provide multiple solutions for each city combination; they will all be thrown together and sorted once.
This is all we need to perform the clustering!
From the article, the base case is when you have two clusters left: just combine them:
% OK, the base case for clustering is that we have two items left, so
% we cluster them together.
cluster([Left,Right], cluster(Left,Right)).
The inductive case takes the list of results and finds the two which are nearest and combines them. Hold on!
% The inductive case is: pair up each cluster and find the minimum distance.
cluster(CityClusters, FinalCityClusters) :-
CityClusters = [_,_,_|_], % ensure we have >2 clusters
setof(result(D, cluster(N1,N2), CC2),
CC1^(select(N1, CityClusters, CC1),
select(N2, CC1, CC2),
cluster_distance(N1, N2, D)),
[result(_, NewCluster, Remainder)|_]),
cluster([NewCluster|Remainder], FinalCityClusters).
Prolog's built-in sorting is to sort a structure on the first argument. We cheat again here by creating a new structure, result/3, which will contain the distance, the cluster with that distance, and the remaining items to be considered. select/3 is extremely handy. It works by pulling an item out of the list and then giving you back the list without that item. We use it twice here to select two items from the list (I don't have to worry about comparing a place to itself as a result!). CC1 is marked as a free variable. The result structures will be created for considering each possible cluster with the items we were given. Again, setof/3 will sort the list to make it unique, so the first item in the list will happen to be the one with the shortest distance. It's a lot of work for one setof/3 call, but I like to cheat!
The last line says, take the new cluster and append it to the remaining items, and forward it on recursively to ourself. The result of that invocation will eventually be the base case.
Now does it work? Let's make a quick-n-dirty main procedure to test it:
main :-
setof(leaf(X), (all_cities(Cities), member(X, Cities)), Basis),
cluster(Basis, Result),
write(Result), nl.
Line one is a cheesy way to construct the initial conditions (all cities in their own cluster of one). Line two calls our predicate to cluster things. Then we write it out. What do we get? (Output manually indented for readability.)
cluster(
cluster(
leaf(FI),
cluster(
leaf(BA),
cluster(
leaf(NA),
leaf(RM)))),
cluster(
leaf(MI),
leaf(TO)))
The order is slightly different, but the result is the same!
If you're perplexed by my use of setof/3 (I would be!) then consider rewriting those predicates using the aggregate library or with simple recursive procedures that aggregate and find the minimum by hand.
From http://www.cse.ohio-state.edu/~gurari/theory-bk/theory-bk-twoli1.html#30007-23021r2.2.4:
Let M = <Q, Σ, Δ, δ, q0, F> be the deterministic finite-state transducer whose transition diagram is given in Figure 2.E.2.
For each of the following relations find a finite-state transducer that computes the relation.
a. { (x, y) | x is in L(M), and y is in Δ* }.
b. { (x, y) | x is in L(M), y is in Δ*, and (x, y) is not in R(M) }.
Yes, this is HW, but I have been struggling with these questions and could at least use pointers. If you want to create your own c. and/or d. examples just to show me HOW to do it rather than lead me to the answers for a. and b. then obviously I'm fine with that.
Thanks in advance!
Since you don't indicate what progress you've made so far, I'm going to assume that you've made no progress at all, and will give overall guidance for how you can approach this sort of problem.
First of all, examine the transition diagram. Do you understand what all the notations mean? Note that the transducer is described as deterministic. Do you understand what that means? Convince yourself that the transducer depicted in the transition diagram is, in fact, deterministic. Trace through it; try to get a sense for what inputs are accepted by the transducer, and what outputs it gives.
Next, figure out what L(M), Δ, and R(M) are for this transducer, since the questions refer to them. Do you know what those notations mean?
Do you know what it means for a transducer to compute a certain relation? Do you understand the { (x, y) | ... } notation for describing the relation?
Can you modify the transition diagram to eliminate the ε/0 transition and merge it into adjacent transitions (which then might output multiple symbols at a single transition)? (This can help, IMHO, with creating other transducers that accept the same input language. More so with part b, in this case, than part a.)
Describe for yourself the transducers you need to create, in a way that's independent of the original transducer. Will these transducers be deterministic?
Create the transition diagrams for these transducers.
I have the following problem: there are some towers build with cubes on a table.
a
b d
c e
------------------- <- table
Now I want to move the cubes to another situation, like this one:
c e
a b d
-------------------
The Prolog program should print the steps to get to this situation, for example: move cube a onto the table, and so on. I have the first situation represented in Prolog:
clean(t). % t is the table, you can always put things there
clean(X) :- \+ on(_,X). % X is the top element, if there is nothing above it
on(a,b). % a is on b
on(b,c). % b on c
on(d,e). % d on e
on(c,t). % c on the table
on(e,t). % and e on the table
Now my problem is to find an solution to make Prolog print the steps to the new situation. My first problem is, how to tell Prolog how the new situation looks like. I tried it with some lists, but I didn´t succeed until now.
Does anyone have an idea how to solve this?
If you encode the problem state by having facts of the on/2 predicate in your database, then you can only change that state by using assert and retract to change the database (and your predicates probably have to be declared dynamic as well). This is unwieldy. A nicer solution is to pass around the problem state as a parameter to your solving predicates (and maybe hide that structure behind a wrapper predicate). This works much better with the backtracking that you're probably going to need to implement the search for the solution.