clpfd

Convert peano number s(N) to integer in Prolog

邮差的信 提交于 2019-12-19 17:47:13
问题 I came across this natural number evaluation of logical numbers in a tutorial and it's been giving me some headache: natural_number(0). natural_number(s(N)) :- natural_number(N). The rule roughly states that: if N is 0 it's natural, if not we try to send the contents of s/1 back recursively to the rule until the content is 0 , then it's a natural number if not then it's not. So I tested the above logic implementation, thought to myself, well this works if I want to represent s(0) as 1 and s(s

Prolog arithmetic syntax

荒凉一梦 提交于 2019-12-19 09:15:13
问题 How to define a as a integer/float number ? I want to find the results of a+b+c+d=10 where a,b,c,d is integer and >=0 . 回答1: Here is a simple, modern, pure Prolog, non-CLP-library solution: range(X):- member(X,[0,1,2,3,4,5,6,7,8,9,10]). ten(A,B,C,D):- range(A), range(B), range(C), range(D), 10 =:= A + B + C + D. 回答2: with SWI-Prolog you can use CLP(FD) library 1 ?- use_module(library(clpfd)). % library(error) compiled into error 0.00 sec, 9,764 bytes % library(clpfd) compiled into clpfd 0.05

Prevent backtracking after first solution to Fibonacci pair

天大地大妈咪最大 提交于 2019-12-18 13:36:31
问题 The term fib(N,F) is true when F is the N th Fibonacci number. The following Prolog code is generally working for me: :-use_module(library(clpfd)). fib(0,0). fib(1,1). fib(N,F) :- N #> 1, N #=< F + 1, F #>= N - 1, F #> 0, N1 #= N - 1, N2 #= N - 2, F1 #=< F, F2 #=< F, F #= F1 + F2, fib(N1,F1), fib(N2,F2). When executing this query (in SICStus Prolog), the first (and correct) match is found for N (rather instantly): | ?- fib(X,377). X = 14 ? When proceeding (by entering ";") to see if there are

Prolog manual or custom labeling

亡梦爱人 提交于 2019-12-18 06:55:13
问题 I am currently writing a solver for a floor planning problem in Prolog and have some issues with the labeling part. The current problem is my constraints are posted but when I launch the labeling, it takes forever to find a solution. I would like to bring in some heuristics. My question is, how do I manually label my variables ? I am afraid that after defining a clpfd variable like this : X in Xinf..Xsup and constraining it, If I do something like : fd_sup(X, Xmax), X = Xmax, ... in my custom

Tennis match scheduling

妖精的绣舞 提交于 2019-12-17 21:47:53
问题 There are a limited number of players and a limited number of tennis courts. At each round, there can be at most as many matches as there are courts. Nobody plays 2 rounds without a break. Everyone plays a match against everyone else. Produce the schedule that takes as few rounds as possible. (Because of the rule that there must a break between rounds for everyone, there can be a round without matches.) The output for 5 players and 2 courts could be: | 1 2 3 4 5 -|------------------- 2| 1 - 3

Find powers of 2 in a list Prolog

浪尽此生 提交于 2019-12-17 02:32:28
问题 I'm trying to create a list in Prolog (SWI Prolog) and check which numbers are powers of 2 and second find how many times a specific number is in the list (in this example I'm trying to find how many times the number 3 is in the list). For a example, if you ask ?- check([0,2,3,-5,-2,1,8,7,4], MULT2, THREE). you should see MULT2=[2,8,4] THREE=1 My first try to find a solution is to search the list with head and doing head mod 2 = 0 to find all numbers which are powers of 2, but something went

Prolog converting integer to a list of digit

蓝咒 提交于 2019-12-14 01:37:14
问题 I want to write a predicate that an integer and a list of digits, and succeed if Digits contain the digits of the integer in the proper order, i.e: ?-digit_lists( Num, [1,2,3,4] ). [Num == 1234]. Here is what I have so far: my_digits( 0, [] ). my_digits(N,[A|As]) :- N1 is floor(N/10), A is N mod 10, my_digits(N1, As). 回答1: As already suggested, consider using finite domain constraints: :- use_module(library(clpfd)). number_digits(Number, 0, [Number]) :- Number in 0..9. number_digits(Number, N

Checking equal digits of numbers between lists?

烂漫一生 提交于 2019-12-13 10:25:41
问题 What would be an efficient way to check how many equal ending digits have numbers between lists in prolog? For example we have Lista = [4432,2345,3243] and Listb = [3345,3232] . In these two lists we have 4432 and 3232 which have 2 same ending digits and 3345 , 2345 which have 3 same ending digits . 3243 , 3232 have the same 2 starting digits but I don't count this as valid. I have solved this problem in the slowest way , by checking each number of Lista with each number of Listb , but this

How does `random_variable `random_value` work in SWI-Prolog's labeling/2?

南楼画角 提交于 2019-12-13 03:01:34
问题 I've seen it's possible to label cplfd variables using a random method by adding the following options to labeling/2 : labeling([random_variable(N),random_value(M)],List). Where M and N are supposed to be integers, I think. However I am not able to find any information about those options in SWI-Prolog's documentation page. How can they be used? 回答1: In the CLP(FD) library there's this: selection(random_variable(Seed)) :- must_be(integer, Seed), set_random(seed(Seed)). % TODO: random_variable

Eclipse CLP labeling: exclude permutations

会有一股神秘感。 提交于 2019-12-12 19:21:58
问题 I am solving a scheduling problem (briefly described here: SWI Prolog CLP(FD) scheduling switched to ECLP). I am able to get some solution quickly, but now I want to incorporate some optimization task. A part of the problem/schedule row looks like D1,D2,N1,N2,A0,A1,A2,..,A9 where some cost for this variables is C1,C1,C1,C1,C2,C2,C2,...,C2 . So from this point of view any permutation of assignments to A0..A9 has the same cost. But, obviously, during the labeling process the solver backtracks