Prolog - finding min element in list - prolog

I'm trying to find the minimum element in the list with comparing the squares. I did the following below:
min_elem([Min],Min).
min_elem([Head | Tail], Min) :-
min_elem(Tail, Tail_min),
Min is min(Head ^ 2, Tail_min ^ 2).
It actually worked with a few tests, but I noticed when tracing the following test:
Tracing example
So after it checks the square, it'll use the previous. I.e., when 8^2 goes to 64 and then it does 64^2. How can I prevent this? 🤔 Thanks!!!

If I understand you correctly, you want min_elem(XS,X) to be true iff X is the minimum of the squares in XS.
Based on this understanding, there are two problems:
In your base case, you are not taking the square.
In your recursive case, you are taking the square of the minimum of the squares in the tail.
To fix these problems, square the minimum in the base case and remove the square in the recursive case:
min_elem([X],Min) :- Min is X ^ 2.
min_elem([Head | Tail], Min) :-
min_elem(Tail, Tail_min),
Min is min(Head ^ 2, Tail_min).

Related

Prolog lists inside the lists

I'm new to Prolog and what I'm trying to achieve is to define the tower-like type which contains lists of natural numbers. Each "floor" is supposed to have the same amount of "apartments" and I don't know how can I check it.
Assuming with "type" you mean a structure which can hold your data, I suggest using a list of lists, like this:
[[101,102,103],[201,202,203],[301,302,303]]
So now you need to check if each "floor" (sublist), has the same amount of elements. Counting the elements in a list is rather simple:
len([],0).
len([H|T],N1) :-
len(T,N),
N1 is N+1.
Which reads: an empty list [] has 0 elements. A list which can be splittet into a head element H and a rest list T has the length N1, if the length of T is N and N1 is calculated by N+1.
So what is missing? You need to go through all your sublists and get the length of the sublist. If the length of two sublists are different, then fail. If you reach the end with allways the same number, success. So the pattern would look like this:
isHotel(R):-
isHotel(R,_).
Add a placeholder for the counting, actual number of the second argument does not matter at this point.
isHotel([],_) .
If you got to a point where there are no floors left, accept for any number.
isHotel([CurrentFloor|T],N) :-
len(CurrentFloor,N),
isHotel(T,N).
If there are floors left (floorlist can be devided into the current floor CurrentFloor and all floors above T), count the rooms at this floor and go check with the left over floors T. The first time this is runs, the number N of rooms will be calculated and forwarded to every other call - therefore for the first time the calculation is just a calculation, in every other ecxecution it is a check as well. This leads to rejects if two lusts do not match in size. Please note this code does not check if the input is a list of lists of numbers.
Checked with SWISH:
?- isHotel([[101,102,103],[201,202,203],[301,302,303]]).
true.
?- isHotel([[101,102,103],[201,202,203],[301,302,3,03]]).
false.
?- isHotel([]).
true.

Heuristic function prolog

I have a problem which is [1,2,3,0,4,5,6] and the goal is [4,5,6,0,1,2,3] and the Heuristic function
is to calculate misplaced for 4,5,6 tile in position of 1,2,3 tile so when I try to add the condition for head >3 it always false
getHeuristic([], 0, []):-!.
% here it is calculated as number of misplaced numbers for 4,5,6 only
% ( so if Head = Head and value greater than 3 its correct so
% don't count it)
getHeuristic([H|T1],V,[H|T2]):-!,
H>3,
getHeuristic(T1,V, T2).
getHeuristic([_|T1],H,[_|T2]):-
getHeuristic(T1,TH, T2),
H is TH + 1.
A heuristic is a quick way to estimate how close the current state is to the goal state (in the state space).
Let h*(S) be the cost of an optimal path from current state S to a goal state G. Then, a heuristic function h(S) is admissible if, and only, if:
0 ≤ h(S) ≤ h*(S), and
h(G) = 0.
In other words, an admissible heuristic function must be always optimistic!
For example, from the following current state, you need at least two "moves" to reach the goal state:
Current state = [6,4,5,0,1,2,3]
Swap 6 and 4 : [4,6,5,0,1,2,3]
Swap 5 and 6 : [4,5,6,0,1,2,3] = Goal state
Notice that, for example, a heuristic function that estimates the cost of an optimal path to reach the goal state [4,5,6,0,1,2,3] from the current state [5,4,6,0,1,2,3] as at least 2 is not admissible (since a unique move - swap 5 and 4 - is sufficient to correct both positions).
Thus, I think you can do something like this:
heuristic(State, Goal, Value) :-
heuristic(State, Goal, 0, Value).
heuristic([], [], A, A).
heuristic([X|Xs], [Y|Ys], A, V) :-
( X < Y
-> A1 is A + 1
; A1 is A ),
heuristic(Xs, Ys, A1, V).
Examples:
?- heuristic([1,2,3,0,4,5,6], [4,5,6,0,1,2,3], V).
V = 3.
?- heuristic([6,4,5,0,1,2,3], [4,5,6,0,1,2,3], V).
V = 2.
?- heuristic([5,4,6,0,1,2,3], [4,5,6,0,1,2,3], V).
V = 1.
?- heuristic([4,5,6,0,1,2,3], [4,5,6,0,1,2,3], V).
V = 0.
Remark: The "moves" counted by the heuristic function do not necessarily correspond to the actual moves that cause transitions from states to their successors in the state space (i.e., they are relaxed moves).
If you want to compute a "distance" from the "current state" to the "target state" by counting the number of nonmatching positions:
% nmpcount(+CurrentState,+TargetState,?Count)
nmpcount(CurrentState,TargetState,Count) :-
nmpcount2(CurrentState,TargetState,0,Count).
% nmpcount2(+CurrentState,+TargetState,+CountSoFar,?CountFinal)
nmpcount2([],[],Count,Count).
nmpcount2([X|MoreCS],[X|MoreTS],CountIn,FinalCount) :-
nmpcount2(MoreCS,MoreTS,CountIn,FinalCount).
nmpcount2([X|MoreCS],[Y|MoreTS],CountIn,FinalCount) :-
dif(X,Y),
CountNext is CountIn+1,
nmpcount2(MoreCS,MoreTS,CountNext,FinalCount).

Understanding this bubble sort solution in Prolog

How does this bubble sort solution work in Prolog?
bubblesort([], []).
bubblesort([H], [H]).
bubblesort([H|D], R) :-
bubblesort(D, E),
[B|G] = E,
( (H =< B, R = [H|E])
; (H > B, bubblesort([B,H|G], R))
).
Here is an example trace: https://pastebin.com/T0DLsmAV
I understand the the line bubblesort(D,E), is responsible for sorting it down to one element, but I don't understand how this works. I understand the basics of lists in prolog, but still cannot work out how this solution operates.
The main difficulty with this code is that bad variable names were chosen and are making the logic harder to follow than it needs to be.
The first two cases are obviously base cases. The first says "the empty list is already sorted" and the second says "a singleton list is already sorted." This should make sense. The third case is where things get interesting.
Let's examine the first part.
bubblesort([H|D], R) :-
bubblesort(D, E),
All that's happened so far is we've named our result R and broken our inputs into a first element H and a tail D. From there, we have said, let's bubblesort the tail of our input and call that E. Maybe this would be a little easier to follow?
bubblesort([H|T], Result) :-
bubblesort(T, TSorted),
Next up,
[B|G] = E,
Again, bad names, but what the author is intending to do here is straightforward: take apart the result of sorting the tail so we can talk about whether the next item in the sorted tail is the right element for that position, or if it needs to switch places with the head of our input. Let's rename:
[HeadOfTSorted|RestOfTSorted] = TSorted,
Now we have a condition. Think of it in terms of prepending onto a sorted list. Say you have some element, like 3, and I hand you a sorted list. You want to determine if your 3 goes at the front or somewhere else. Well, suppose I gave you a sorted list that looked like [5,7,19,23,...]. You'd know that your 3 is right where it needs to be, and you'd hand back [3,5,7,19,23,...]. That's exactly the first case of the condition:
( (H =< HeadOfTSorted, Result = [H|TSorted])
Now consider another case, where I hand you a list that starts with [1,2,...]. You know you can't just put the three at the start and give me back [3,1,2,...]. But you don't really know where the 3 goes; it just doesn't go at the start. So what you have to do is resort the rest of the list with the 3 at the start, after the 1: [1 | resorted([3,2,...])]. That's effectively the other branch of the condition:
; (H > HeadOfTSorted, bubblesort([HeadOfTSorted,H|RestOfTSorted], R))
).
Hope this helps!
note: the key to recursive problem solving is exactly not to think about the minutiae of our code's operations. Imagine you already have the solution, then just use it to solve a smaller subproblem, thus arriving at the full problem's solution.
Your code, with more suggestive variable names so I could follow it, reads:
bubblesort([], []). % empty list is already sorted
bubblesort([H], [H]). % singleton list is already sorted
bubblesort([H|T], S) :- % `[H|T]` sorted is `S`, *if*
bubblesort(T, [M|R]), % `T` sorted is `[M|R]`, *and*
( % *either*,
H =< M, % in case `H` is not greater than `M`,
S = [H,M|R] % `S` is `[H,M|R]`,
; % *or*
H > M, % in case `H` is greater than `M`,
bubblesort([M,H|R], S) % `S` is `[M,H|R]` sorted by the same algorithm
).
(H is for "head", T is for "tail", S is "sorted", R "rest" and M is "minimum" -- see below for that).
We prove its correctness by structural induction. The induction hypothesis (IH) is that this definition is correct for shorter lists. We need to prove it is then also correct for a longer list. Indeed T is one-element shorter than [H|T]. Thus IH says [M|R] is sorted. This means M is the minimum element in T. It also means T is non-empty (sorting doesn't change the number of elements), so the clauses are indeed mutually-exclusive.
If H is not larger than the minimum element in T, [H,M|R] is obviously sorted.
Otherwise, we sort [M,H|R]. M is the minimum element and thus guaranteed to be the first in the result. What's actually sorted is [H|R], which is one element shorter, thus by IH sorting it works. QED.
If the last step sounds fishy to you, consider replacing the second alternative with the equivalent
; H > M, % in case `H` is greater then `M`,
bubblesort([H|R], S1), % `S1` is `[H|R]` sorted by the same algorithm
S = [M|S1]
).
where the applicability of the induction step is even clearer.
I'm not so sure it's a bubble sort though.
update: Indeed, measuring the empirical orders of growth, its number of inferences grows as ~ n3 (or slower), but true bubble sort clocked at ~ n2.1 (close enough to the theoretical ~ n2), where n is the list's length:
tbs([], []). % 'true' bubble sort
tbs([H],[H]).
tbs(L,S):- bubble(L,B),
( L==B -> S=L ; tbs(B,S) ).
bubble([],[]).
bubble([A],[A]).
bubble([A,B|C],R):-
( A =< B -> bubble([B|C],X), R=[A|X]
; bubble([A|C],X), R=[B|X] ).

How to maximize the goal in prolog?

I am trying to solve the knapsack problem in prolog. Following is my implementation.
% 'ks' is compound term which has 4 argumets
% 1 - List of items to be chosen from.
% 2 - Maximum weight a knapsack can carry.
% 3 - Selected items which sum of weights is less than or equal to knapsack capacity.
% 4 - The gain after choosing the selected item.
% base conditions where input list contains only one items and
% it is either selected or excluded.
ks([item(W1, V1)], W, [item(W1, V1)], V1):- W1 =< W.
ks([item(W1, _)], W, [], 0):- W1 > W.
% An item from the input list is chosen in the knapsack.
% In that case, we recurse with smaller list with reduced weight constraint.
ks(ItemList, MaxWeight, SelectItems, Gain) :-
append(Prefix, [item(W1, V1)|Suffix], ItemList),
append(Prefix, Suffix, RemList),
NewWeight is MaxWeight - W1,
W1 =< MaxWeight,
append([item(W1, V1)], SelectItems1, SelectItems),
ks(RemList, NewWeight, SelectItems1, Gain1),
Gain is V1 + Gain1.
% An item from the input list is not chosen in the knapsack.
% In that case, we recurse with smaller list but with the same weight constraint.
ks(ItemList, MaxWeight, SelectItems, Gain) :-
append([P1|Prefix], [item(W1, V1)|Suffix], ItemList),
append([P1|Prefix], Suffix, RemList),
not(member(item(W1, V1), SelectItems)),
ks(RemList, MaxWeight, SelectItems, Gain).
The input to the program will be list of items as below. in term item(W, V) W is weight of the item while V is value of the item. Goal to maximize the value for the given weight constraint.
ks([item(2,3), item(3,4), item(4,5), item(5,8), item(9,10)], 20, List, Gain).
List = [item(2, 3), item(3, 4), item(4, 5), item(5, 8)],
Gain = 20 ;
While I am able to generate all the combinations of items with above program, I am not able to code to find out the maximum gain only.
Could any one please point me the right direction?
Thanks.
I think that to find reusable abstractions it's an important point of studying programming. If we have a subset_set/2 that yields on backtracking all subsets, ks/4 becomes really simple:
subset_set([], _).
subset_set([H|T], Set) :-
append(_, [H|Rest], Set),
subset_set(T, Rest).
ks(Set, Limit, Choice, Gain) :-
subset_set(Choice, Set),
aggregate((sum(W), sum(G)), member(item(W, G), Choice), (TotWeight, Gain)),
TotWeight =< Limit.
and then
ks_max(Items, Limit, Sel, WMax) :-
aggregate(max(W,I), ks(Items,Limit,I,W), max(WMax,Sel)).
despite its simplicity, subset_set/2 is not really easy to code, and library available alternatives (subset/2, ord_subset/2) don't enumerate, but only check for the relation.
There are at least two things you can do, depending on how you want to approach this.
You could simply collect all solutions and find the maximum. Something along the lines of:
?- Items = [item(2,3), item(3,4), item(4,5), item(5,8), item(9,10)],
findall(Gain-List, ks(Items, 20, List, Gain), Solutions),
sort(Solutions, Sorted),
reverse(Sorted, [MaxGain-MaxList|_]).
% ...
MaxGain = 26,
MaxList = [item(9, 10), item(5, 8), item(4, 5), item(2, 3)].
So you find all solutions, sort them by Gain, and take the last. This is just one way to do it: if you don't mind collecting all solutions, it is up to you how you want to pick out the solution you need from the list. You might also want to find all maximum solutions: see this question and answers for ideas how to do that.
The cleaner approach would be to use constraints. As the comment to your questions points out, it is not very clear what you are actually doing, but the way to go would be to use a library like CLP(FD). With it, you could simply tell labeling/2 to look for the maximum Gain first (once you have expressed your problem in terms of constraints).
greedy Approximation algorithm :
pw((P,W),Res) :- PW is P/W, Res=(PW,P,W).
pws(Ps_Ws,PWs) :- maplist(pw,Ps_Ws,PWs).
sort_desc(List,Desc_list) :-
sort(List,Slist),
reverse(Slist,Desc_list).
ransack_([],_,_,[]).
ransack_([(_,P,W)|PWs],Const,Sum,Res) :-
Sum1 is W+Sum,
Sum1 < Const ->
Res=[(P,W)|Res1],
ransack_(PWs,Const,Sum1,Res1)
;ransack_(PWs,Const,Sum,Res).
% ransack(+[(P,W)|..],+W,,Res)
ransack(L_PWs,W,Res) :-
pws(L_PWs,Aux),
sort_desc(Aux,PWs),
ransack_(PWs,W,0,Res).
Test
item(W, V)-->(V,W)
| ?- ransack([(3,2),(4,3),(5,4),(8,5),(10,9)],20,Res).
Res = [(8,5),(3,2),(4,3),(5,4)] ? ;
no

Transforming recursion into tail recursion?

I am trying to write a predicate that recursively finds the nth power of some number
[A^n = A * A^(n-1)] and uses the shortcut A^(2n) = A^n * A^n.
Here is the solution so far.
p(_,0,1):-!.
p(A,N,R):-N mod 2=0,!,N1=N/2,p(A,N1,R1),R=R1*R1.
p(A,N,R):-N1=N-1,p(A,N1,R1),R=R1*A.
Now I want to make this tail recursive. I can do tail for simple cases, such as factorials and power without the shortcut (by adding an accumulator), but this one is hard.
Any help is much appreciated!
It seems it is sort of possible after all, just start it from the other end:
pow(A,N,R) :-
pow(A,N,A,1,R).
pow(_,N,R,N,R) :- !.
pow(A,N,Acc,M,R) :-
M =< N div 2, !,
M1 is M*2,
NewAcc is Acc * Acc,
pow(A,N,NewAcc,M1,R).
pow(A,N,Acc,M,R) :-
M < N,
M1 is M+1,
NewAcc is A * Acc,
pow(A,N,NewAcc,M1,R).
It applies the shortcut up to the highest power of 2 smaller than N, which is admittedly not the same as what your algorithm is doing.
Boris is right in that what his algorithm does is not the same as the original one. But here is how you can reproduce it, if you really want to:
Observe that you can determine the order of the operations from the binary representation of the number. Let N=7, then binary N=111, denoted as N=7~111.
Now you see the scheme in your original algorithm:
N Op N'
7~111 Mul 6~110 (= zero last bit)
6~110 Squ 3~011 (= shift right)
3~011 Mul 2~010
2~010 Squ 1~001
1~001 Base
Considering that due to the recursive nature of the algorithm, these steps are carried out top-to-bottom, you get Base - Squ - Mul - Squ - Mul = ((A*A)*A)*((A*A)*A))*A = A**7
Contrast this to Boris' algorithm:
N Op N'
1~001 Squ 2~010 (=shift left)
2~010 Squ 4~100 (=shift left)
4~100 Mul 5~101 (=add one)
5~101 Mul 6~110 (=add one)
6~110 Mul 7~111 (=add one)
So this one does all the shifting first, while the original considers each bit except for the first of N, right to left, in turn, "queuing" (because of bottom-up) Mul, Squ if the bit is set or just Squ if it is unset.
To reproduce this behavior (which is more efficient, as you will never do more simple multiplications than squares), you could start with N in binary and do the following (here in general pseudocode, easy for you to translate into prolog):
Acc=A
for i in (reverse(tail(bits(N)))):
Acc*=Acc
if i==1:
Acc*=A
This is for N>=1. N=0 is a special case and must be treated separately.
I'm pretty sure this is correct. If you have doubts, then just think about your original algorithm: testing for mod 2 == 0 is the same as testing if the last bit is zero. And if it is not, then substracting one is the same as zeroing out the last bit while doubling and halving is just shifting left or right in binary.

Resources