clpfd

Getting an order into predicate resolution

十年热恋 提交于 2019-11-28 13:44:48
Look at the following goals (I am using swi-prolog with clpfd from Markus Triska): result(Input,Result) :- Input #> 10, Result=decline. result(Input,Result) :- Input in 0..20, Result=offer. A possible query looks like this: ?- result(15,B). B = decline ; B = offer. I want to add an order or some sort of solution priority. If "decline" is a valid response for Input=15 , then the second goal should not be considered anymore, so that only B=decline is a solution but not B=offer . I know that I could add a !/0 but then the other way round would not work. Give me all possible answers for this

Optimizing pathfinding in Constraint Logic Programming with Prolog

送分小仙女□ 提交于 2019-11-28 11:06:00
I am working on a small prolog application to solve the Skyscrapers and Fences puzzle. An unsolved puzzle: A solved puzzle: When I pass the program already solved puzzles it is quick, almost instantaneous, to validate it for me. When I pass the program really small puzzles (2x2, for example, with modified rules, of course), it is also quite fast to find a solution. The problem is on computing puzzles with the "native" size of 6x6. I've left it running for 5 or so hours before aborting it. Way too much time. I've found that the part that takes the longest is the "fences" one, not the

Prolog Beginner: How to make unique values for each Variable in a predicate

你说的曾经没有我的故事 提交于 2019-11-28 09:50:05
问题 I have a prolog predicate: Add( [A|B] , Answer ) :- ... ~ Add everything in the list to come up with answer ... I would now like to implement AddUnique that would return unique values for everything in the list except when I give it the variable twice. Here are somethings that are logically equivalent: ?- AddUnique([A, B, C], 10). is equivalent to: ?- Add([A, B, C], 10), A != B, B != C, A != C. And: ?- AddUnique([A, B, B], 10). is equivalent to: ?- Add([A, B, B], 10), A != B. Also: ?-

Prolog Constraint Processing : Packing Squares

怎甘沉沦 提交于 2019-11-28 09:36:03
I'm trying to solve a constraint processing problem in prolog. I need to pack 4 squares of 5x5,4x4,3x3 and 2x2 in a grid of 10x10. They may not overlap. My variables look like this: Name: SqX(i), i=1..10, domain: 1..10 Where X is either 5,4,3 or 2. The index i represents the row, the domain the column in the grid. My first constraints try to define the width and height of the squares. I formulate it as such: Constraint: SqX(i) > SqX(j)-X /\ i>j-X, range: i>0 /\ j>0 So that the possible points are constrained to be within X rows and columns from each other. Prolog however, stops on these

I want to count the occurrences of an element in a list

戏子无情 提交于 2019-11-28 09:02:18
问题 I want to count the occurrences of an element in a list, and if there is one then the predicate unique would be true, else false. However, if the element occurs more than once, Prolog finds it true. I don't know what to do... count([], X, 0). count([X|T], X, Y) :- count(T, X, Z), Y is 1+Z, write(Z). count([_|T], X, Z) :- count(T, X, Z). unique(St, [Y|RestList]) :- count([Y|RestList], St, N), N =:= 1. 回答1: The solution works as far as the first argument is a ground list. In some other cases,

Inverse factorial in Prolog

大兔子大兔子 提交于 2019-11-28 07:09:42
问题 Can someone helping me to find a way to get the inverse factorial in Prolog... For example inverse_factorial(6,X) ===> X = 3 . I have been working on it a lot of time. I currently have the factorial, but i have to make it reversible. Please help me. 回答1: Prolog's predicates are relations, so once you have defined factorial, you have implicitly defined the inverse too. However, regular arithmetics is moded in Prolog, that is, the entire expression in (is)/2 or (>)/2 has to be known at runtime,

How to enumerate combinations using DCGs with CLP(FD) and multiple constraints

醉酒当歌 提交于 2019-11-28 01:33:42
This question starts from Mat's answer to Algorithm improvement for enumerating binary trees which has only one input value that determines the number of all nodes for the binary tree, and the need to be able to have two input values with one being the number of unary nodes and the other being the number of binary nodes. While I was able to derive a solution by using listing/1 and threading extra state variables: e(t, B, B, U, U). e(u(E), B0, B1, [_|U0], U1) :- e(E, B0, B1, U0, U1). e(b(E0, E1), [_|B0], B2, U0, U2) :- e(E0, B0, B1, U0, U1), e(E1, B1, B2, U1, U2). e(U,B,Es) :- length(Bs, B),

List Length in Prolog

自作多情 提交于 2019-11-27 21:26:28
问题 I am beginner in Prolog programming. I wrote this program to calculate the length of a list. Why is below program wrong? length(0, []). length(L+l, H|T) :- length(L, T). I wrote below program and it works correctly. length([], 0). length([H|T], N) :- length(T, N1), N is N1+1. when I changed the order, I got an error. Why? length([], 0). length([H|T], N) :- N is N1+1, length(T, N1). 回答1: this code length_1(0,[]). length_1(L+1, [H|T]) :- length_1(L,T). works (please note the added square braces

Getting an order into predicate resolution

社会主义新天地 提交于 2019-11-27 07:54:54
问题 Look at the following goals (I am using swi-prolog with clpfd from Markus Triska): result(Input,Result) :- Input #> 10, Result=decline. result(Input,Result) :- Input in 0..20, Result=offer. A possible query looks like this: ?- result(15,B). B = decline ; B = offer. I want to add an order or some sort of solution priority. If "decline" is a valid response for Input=15 , then the second goal should not be considered anymore, so that only B=decline is a solution but not B=offer . I know that I

Optimizing pathfinding in Constraint Logic Programming with Prolog

吃可爱长大的小学妹 提交于 2019-11-27 05:54:34
问题 I am working on a small prolog application to solve the Skyscrapers and Fences puzzle. An unsolved puzzle: A solved puzzle: When I pass the program already solved puzzles it is quick, almost instantaneous, to validate it for me. When I pass the program really small puzzles (2x2, for example, with modified rules, of course), it is also quite fast to find a solution. The problem is on computing puzzles with the "native" size of 6x6. I've left it running for 5 or so hours before aborting it. Way