A Prolog predicate result [closed] - prolog

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have written a Prolog relation remove(E,L,R) that is true if R is the list which results from removing one instance of E from list L. The relation is false if E isn't a member of L.
I want to know the result of the following query
remove(p(X),[a,p(a),p(p(a)),p(p(p(a)))],R).

So? Just ask. Not us, but ask Prolog. The result should be
?- remove(p(X),[a,p(a),p(p(a)),p(p(p(a)))],R).
X = a,
R = [a, p(p(a)), p(p(p(a)))]
or something along those lines, if you've written it right. If you allow backtracking (in other words, removing an element from your list, starting with the first), you'll get something like
?- remove(p(X),[a,p(a),p(p(a)),p(p(p(a)))],R).
X = a,
R = [a, p(p(a)), p(p(p(a)))] ;
X = p(a),
R = [a, p(a), p(p(p(a)))] ;
X = p(p(a)),
R = [a, p(a), p(p(a))] ;
false.

Related

Difficulty with propositional logic in prolog [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I need to convert the following propositions into prolog code and I have do not understand how the operators work. I usually use java.
"Jeans are only casual, dress pants are only formal, kakis are only semi-formal, belts are not casual, black socks are acceptable anytime.
Casual = C, Semi-Formal = SF, Formal = F, Jeans = J, Dress pants = DP, Kakis = K, Belts = B, Black socks = BS.
J -> C, DP -> F, K -> SF, B -> !C, BS -> (C v SF v F)"
Here is the code I already have:
casual(jeans).
formal(dress_pants, belt, black_socks).
semiFormal(khakis).
formal(belt).
semiFormal(belt).
casual(black_socks).
formal(black_socks).
semiFormal(black_socks).
Informally, a unary predicate p can be viewed as a set P and, consequently, a literal p(X) can be viewed as the set membership test X∈P. Therefore, a goal p(X) will be true or false (i.e., a proposition), depending on whether X belongs to the set P or not.
The sentences "Jeans are only casual, dress pants are only formal, kakis are only semi-formal, belts are not casual, black socks are acceptable anytime." can be represented as the sets:
casual = {jeans, black_socks}.
formal = {dress_pants, belt, black_socks}
semi_formal = {khakis, belt, black_socks}
And these sets can be represented in Prolog by the facts:
casual(jeans).
casual(black_socks).
formal(dress_pants).
formal(belt).
formal(black_socks).
semi_formal(khakis).
semi_formal(belt).
semi_formal(black_socks).
Here are some Prolog queries about these facts:
?- casual(jeans).
true.
?- casual(belt).
false.
?- casual(X).
X = jeans ;
X = black_socks.
?- formal(X).
X = dress_pants ;
X = belt ;
X = black_socks.
?- semi_formal(X).
X = khakis ;
X = belt ;
X = black_socks.

PROLOG store previous outputs/inputs and generate new input based on old

What im trying to do is:
getDiffAnswer/5
getDiffAnswer(Q,PQ,PR,CR,R) holds if R is the new response from the list containing the candidate answers CR for the the question Q.
Q is a Question.
PQ is a list of previous Questions
PR is a list of previous Replies
CR is a list of possible Replies
R is the different Reply
Expected output:
1. ?- getDiffAnswer([what,is,X],
[],[],
[1,2,3],R).
R = 1;
false.
2. ?- getDiffAnswer([what,is,X],
[[what,is,X]],
[[1]],
[1,2,3],R).
R = 2 ;
false.
3. ?- getDiffAnswer([what,is,X],
[[what,is,X],[what,is,X]],
[[1,2]],
[1,2,3],R)
R = 3 ;
4. ?- getDiffAnswer([what,is,X],
[[what,is,X],[what,is,X],[what,is,X]],
[[1,2,3]],
[1,2,3],R)
false;
Assuming all questions are always the same, then this implements your specification I believe:
getDiffAnswer(_,_,PR,CR,R) :-
append(PR,[R|_],CR).
Your examples:
?- getDiffAnswer([what,is,'X'],[],[],[1,2,3],R).
R = 1.
?- getDiffAnswer([what,is,'X'],[[what,is,'X']],[1],[1,2,3],R).
R = 2.
?- getDiffAnswer([what,is,'X'],[[what,is,'X'],[what,is,'X']],[1,2],[1,2,3],R).
R = 3.
?- getDiffAnswer([what,is,'X'],[[what,is,'X'],[what,is,'X'],[what,is,'X']],[1,2,3],[1,2,3],R).
false.
Here is edited answer, the first approach with select was wrong. This approach is to make recursion and check if another questions are equal to our question, if they are we are collecting answers to this questions. At the end we know that our answer R will be member of set CR but it also wont be member of collected set.
getDiffAnswer(Q,PQ,PR,CR,R) :-
possibleAnswers(Q,PQ,PR,[],Rs),
member(R,CR),not(member(R,Rs)).
possibleAnswers(_,[],_,RS,RS).
possibleAnswers(Q,[H|L],[H1|L1],Rs,Rs2):-
(Q = H -> append([H1],Rs,Rs3)),
possibleAnswers(Q,L,L1,Rs3,Rs2).
Rs is just a set of all answers already used for the current question.
I am treating PR like list of answers, so it's not [[1,2,3]] but [1,2,3].

Constraint - SWI- Prolog Queries

I have an exam coming up and I'm going through past papers to help my understanding.
I came across the following past paper question:
Consider the following queries and answers. Some answers coincide with
what SWI-Prolog would infer whereas others are erroneous. Indicate which
answers are genuine and which ones are fake (no explanation of your answer
is required).
(i) |?- [A, B, C] ins 0 .. 2, A #= B + C.
A = 0..2 B = 0..2 C = 0..2
(ii) |?- A in 0 .. 3, A * A #= A.
A = 0..2
(iii) |?- [A, B] ins -1 .. 1, A #= B.
A = 1 B = 1
(iv) |?- [A, B] ins 0 .. 3, A #= B + 1.
A = 1..3 B = 1..2
I'm struggling to see how each one is either true or false. Would someone be able to explain to me how to figure these out please.
Thank you, really appreciate the help.
The key principle for deciding which answers are admissible and which are not is to look whether the residual program is declaratively equivalent to the original query. If the residual constraints admit any solution that the original query does not, or the other way around, then the answer is fake (or you have found a mistake in the CLP(FD) solver). If the shown answer is not even syntactically valid, then the answer is definitely fake.
Let's do it:
(i) |?- [A, B, C] ins 0 .. 2, A #= B + C.
suggested answer: A = 0..2 B = 0..2 C = 0..2
WRONG! The original query constrains the variables to integers, but this answer is not even a syntactically valid Prolog program.
(ii) |?- A in 0 .. 3, A * A #= A.
suggested answer: A = 0..2
WRONG! The original query constrains A to integers, but according to this residual program, A = 0..2 is a valid solution. The term ..(0, 2) is not an integer.
(iii) |?- [A, B] ins -1 .. 1, A #= B.
suggested answer: A = 1 B = 1
WRONG! Not syntactically valid.
(iv) |?- [A, B] ins 0 .. 3, A #= B + 1.
suggested answer: A = 1..3 B = 1..2
WRONG! Not syntactically valid.
Note that even if all shown answers were syntactically valid and =/2 were replaced by in/2 in the residual goals of (i), (ii) and (iv), these answer would still be all wrong, because you can in each case find solutions that either are not admissible by the original query or the residual goals, but not both. I leave solving these cases as an exercise for you, for example, suppose the respective answers are:
A in 0..2, B in 0..2, C in 0..2.
A in 0..2.
A = 1, B = 1.
A in 1..3, B in 1..2.
and find a witness for each case to show that the residual goals are semantically different from the respective original query.
For example, in case (1), A = B = C = 2 would be a valid solution according to the residual constraints, but obviously the original constraints exclude this solution, because 2 #= 2 + 2 does not hold!
A variable is always restricted to get a value contained in its domain, and arithmetic constrains only reduce the domain of involved variables.
So, try to 'label' all variables - that is, assign values from answers reported domains. Of course, if the arithmetic relation is not satisfied, you can say the answer is faked. Take ii). Does it hold for A=0 ? What about A=2 ?
This 'test' of course doesn't suffice to answer all questions. Some reported domains are narrower. For instance, take iii). Can you see any reason that excludes -1, or 0. If you cannot, you should mark the answer as faked.

prolog basket(L,Smin,Smax,S) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
here have a problem with Prolog. Consider a set of products, each with a given price. For simplicity we think of one item of each product. Assume you have a list of pairs, (Item,Price), representing names of items in a store and their corresponding price. The name is a constant and the price is a positive integer number.
For example:
[(radio,130),(tv,940),(laptop,400),(bicycle,330)]
Write a predicate basket(L,Smin,Smax,S) that finds a subset S of the items available in L such that the sum of the prices in S is higher that Smin and lower than Smax.
All possible subsets that meet the specification of the problem should be presented one by one, through backtracking!
A practice in Prolog that eases things most of the time is using an accumulator. An accumulator is a variable you pass recursively and update in each recursive step. Each recursive step can inspect the value of the variable and decide accordingly. Here the accumulator is the sum of the price of the items this far added to S. Evidently at the start, that sum is zero, so we first ease things a bit by introducing an accumulator:
basket(L,Smin,Smax,S) :-
basket(L,Smin,Smax,0,S).
Now we need to work out basket/5. There is a base case in which all items are enumerated. Therefore L is empty. What we need to do then, is check whether the Sum is between the given bounds:
basket([],Smin,Smax,Sum,[]) :-
Smin < S,
S < Smax.
This is the only base case. Note that S (the last argument) is the empty list as well. Since all items have already been inspected, this means we will not add any elements to the tail of the subset.
Now there are two recursive cases. In the recursive case, L contains at least one element, and we can do two things:
either we accept that item, and add it to our subset;
or we discard the item, and continue further.
In case we accept the item, the predicate looks like:
basket([(I,V)|T],Smin,Smax,Sum,[(I,V)|S]) :-
Sum1 is Sum+V,
Sum1 < Smax,
basket(T,Smin,Smax,Sum1,S).
We thus decide to add the element, we calculate the new sum Sum1 by adding the value V to the original Sum, we can (optionally) already perform a bounds check on the upper bound of the value. This can be efficient if there is a large amount of items: we cut off search from the moment our bag will start to run in overflow.
Finally we have the other recursive case, where we simply decide to discard the item. This is an easy one:
basket([_|T],Smin,Smax,Sum,S) :-
basket(T,Smin,Smax,Sum,S).
We don't even look at the head, we simply take the tail of the list feeded to basket, we do not have to update Sum, since nothing changed to the subset.
Putting it all together, the following program should do the trick:
basket(L,Smin,Smax,S) :-
basket(L,Smin,Smax,0,S).
basket([],Smin,Smax,Sum,[]) :-
Smin < S,
S < Smax.
basket([(I,V)|T],Smin,Smax,Sum,[(I,V)|S]) :-
Sum1 is Sum+V,
Sum1 < Smax,
basket(T,Smin,Smax,Sum1,S).
basket([_|T],Smin,Smax,Sum,S) :-
basket(T,Smin,Smax,Sum,S).
Note however that using dynamic programming one can generate solutions way more efficiently.
when we have sublist/2 from this answer
sublist([], []).
sublist([_|XS], YS) :-
sublist(XS, YS).
sublist([X|XS], [X|YS]) :-
sublist(XS, YS).
library(aggregate) can be used to enforce the constraint:
basket(L,Smin,Smax,S) :-
sublist(L,S),
aggregate(sum(P), I^member((I,P),S), T),
T>=Smin,T=<Smax.
?- basket([(radio,130),(tv,940),(laptop,400),(bicycle,330)],1600,2000,S).
S = [(tv, 940), (laptop, 400), (bicycle, 330)] ;
S = [(radio, 130), (tv, 940), (laptop, 400), (bicycle, 330)].

Mathematica NonlinearModelFit strange output [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I am trying to run NonlinearModelFit in mathematica on some data for a project. It is a fairly complicated model having 9 coefficients, and 5 independent variables. I created the data the way that the model needs, and I think I called the function correctly.
Here is a sample of the data:
{{16.5892, 0.631, 1.7707, 1258.21, 580.271, 14.02}, {16.2855, 0.813,
1.76756, 2098.41, 745.624, 13.59}, {16.036, 0.58, 1.77311, 846.994, 718.092, 13.41}}
Of course the total amount of data is about 30 data sets for the function to use.
Here is my function call:
nlm = NonlinearModelFit[data, a + c*b + d*bminv + e*M + f*bminv*M + g*x + h*x^2 + i*y + j*y^2,
{a, c, d, e, f, g, h, i, j}, {b, bminv, M, x, y}];
I think that this should work, but I get a really weird output:
FittedModel[38592.8+0.811612b+<<9>>+3.06099*10^-7*y^2]^3
The numbers for this model might be right, but I do not understand the part with <<9>>, and even more so I do not understand how the FittedModel function, which is what NonlinearModelFit always returns, is cubed.
Besides the weirdness of the output, it is also an unusuable output, I have tried functions that should work with FittedModel objects, and none of them work.
If anybody has some advice as to what the output means, how to fix it, or if I am simply doing something wrong with my function call or data it would be much appreciated.
Thanks.
It seems to work and I don't get the ^3 :
data = {{16.5892, 0.631, 1.7707, 1258.21, 580.271, 14.02}, {16.2855, 0.813, 1.76756, 2098.41, 745.624, 13.59}, {16.036, 0.58, 1.77311, 846.994, 718.092, 13.41}}
nlm = NonlinearModelFit[data, a + c*b + d*bminv + e*M + f*bminv*M + g*x + h*x^2 + i*y + j*y^2, {a, c, d, e, f, g, h, i, j}, {b, bminv, M, x, y}] ;
nlm[Sequence ## Most[#]] & /# data - data[[All, -1]]
(* {-1.77636*10^-15, 0., -3.55271*10^-15} *)

Resources