clpfd

Binary to decimal - prolog

泄露秘密 提交于 2019-12-24 01:47:29
问题 I found this on stack: reversible "binary to number" predicate But I don't understand :- use_module(library(clpfd)). binary_number(Bs0, N) :- reverse(Bs0, Bs), binary_number(Bs, 0, 0, N). binary_number([], _, N, N). binary_number([B|Bs], I0, N0, N) :- B in 0..1, N1 #= N0 + (2^I0)*B, I1 #= I0 + 1, binary_number(Bs, I1, N1, N). Example queries: ?- binary_number([1,0,1], N). N = 5. ?- binary_number(Bs, 5). Bs = [1, 0, 1] . Could somebody explain me the code Especialy this : binary_number([], _,

How to prevent duplicates in generated sequences by using dif/2?

血红的双手。 提交于 2019-12-24 00:15:55
问题 This question came up while answering another question on StackOverflow on (generalizing a bit) generating all sequences formed out of a finite set of elements with no duplicate occurrences. As Boris rightly indicated in the comments, there are many existing solutions to this problem. However, I am interested in a solution that does not use an accumulator (i.e., a list of already picked elements against which a newly selected element is to be compared) but that uses dif/2 statements instead.

Compute a list of distinct odd numbers (if one exists), such that their sum is equal to a given number

混江龙づ霸主 提交于 2019-12-23 18:04:17
问题 :- use_module(library(clpfd)). % load constraint library % [constraint] Compute a list of distinct odd numbers (if one exists), such that their sum is equal to a given number. odd(Num) :- Num mod 2 #= 1. sumOfList([],N,N) :- !. sumOfList([H|T],Counter,N) :- NewN #= H + Counter, sumOfList(T,NewN,N). buildOddList(N,InputList,L) :- %return list when sum of list is N V in 1..N, odd(V), append(InputList,[V],TempL), sumOfList(TempL,0,N)-> L = TempL; buildOddList(N,TempL,L). computeOddList(N) :-

Solve Instant Insanity in PROLOG with CLP

不羁的心 提交于 2019-12-23 15:51:54
问题 This is the game I've managed to generate the problem with 4 colours and 4 cubes randomly mixed and following the colour scheme suggested in the link. So, the goal is to generate the possible solutions to the problem using clpfd . The main principle is basic, the same face for all 4 cubes must be different. Used all_different/2 on 4 lists, each of them containing the respective side of the "tower" composed by 4 faces. So far, so good. Now I must assure that the final result is a composition

Expressing setup time with cumulatives

主宰稳场 提交于 2019-12-23 09:23:27
问题 There are many families of scheduling problems. I'm looking into a problem where I have families of jobs/tasks where the transition from one family to another family require reconfiguring the machine (setup time). I'm using cumulatives[2/3] to solve this problem, but I feel unsure how the setup time could be expressed. In this small example I have 10 tasks belonging to 3 different families. Any task can run on any machine, but a switch from one task in one family to another task in another

How to do arithmetic expression evaluation in prolog?

£可爱£侵袭症+ 提交于 2019-12-23 03:46:14
问题 I am trying to solve an arithmetic expression in prolog (implementation - eclipse prolog). The arithmetic expression to be solved is like this: A * (C + B * X) + D * X = E X is the value to be computed, and all others (A,B,C,D,E) are all numbers. For example: 5 * (3 + 2*X) + 2*X = 39, on computing should assign X with the value 2. The query(goal) that would be entered into Prolog would take the form: ?- compute( 5*(3+2*X)+2*X = 39, Result). The 'Result' and value of 'X' should be tied

Logic Dots puzzle problems with SWI-Prolog

霸气de小男生 提交于 2019-12-23 03:39:14
问题 I am working on a logic game named "Logic Dots". The game is a 3*3 grid with numbers surrounding each row and column. The rule is easy that the sum of each row and column should equal to those numbers. Below is my code, it is changed from clpfd sokoku. But result is something wrong. Code: :- use_module(library(clpfd)). kakuro(Rows) :- length(Rows, 3), maplist(length_list(3), Rows), append(Rows, Vs), Vs ins 0..1, Rows = [A,B,C], sum(A,#=,1), sum(B,#=,1), sum(C,#=,1), transpose(Rows, Columns),

what's the difference between #= and =:= in SWI prolog

馋奶兔 提交于 2019-12-22 10:44:23
问题 What is the difference between #= and =:= in SWI prolog. I have found the definition from SWI prolog, but still confused about it. http://www.swi-prolog.org/pldoc/man?section=arithpreds http://www.swi-prolog.org/pldoc/man?section=clpfd-arith-constraints ?- 3=:=3. true. ?- (3-2) =:= (9-8). true. ?- 3 #= 3. true. ?- (3-2) #= (9-8). true. 回答1: What's the difference between #= and =:= in SWI prolog ? The difference is that #=/2 is a CLPFD library operator (you need to execute: use_module(library

Simple prolog program. Getting error: >/2: Arguments are not sufficiently instantiated

此生再无相见时 提交于 2019-12-21 08:01:10
问题 I made a Prolog predicate posAt(List1,P,List2) that tests whether the element at position P of List1 and List2 are equal: posAt([X|Z], 1, [Y|W]) :- X = Y. posAt([Z|X], K, [W|Y]) :- K > 1, Kr is K - 1, posAt(X, Kr, Y). When testing: ?- posAt([1,2,3], X, [a,2,b]). I expected an output of X = 2 but instead I got the following error: ERROR: >/2: Arguments are not sufficiently instantiated Why am I getting this error? 回答1: A Prolog predicate is a relation between arguments, and your statement the

Clojure core.logic CLP(FD) projecting FD variables

拜拜、爱过 提交于 2019-12-20 01:59:20
问题 I'm working on a naive square-packing algorithm using Clojure's core.logic CLP(FD) library (core.logic version 0.8.3). Squares are represented like so: [[[x11 y11] [x12 y12]] [[x21 y21] [x22 y22] ...]] with each square represented as the coordinates of its top-left and bottom-right corners. Coordinates are FD variables, within a certain interval. I want to define the size of a solution as the distance between the top-right corner and bottom-right corners of the closest and farthest squares to