clpfd

Prolog Constraint Programing finding even and odd numbers

拟墨画扇 提交于 2020-01-02 15:24:06
问题 I need to create a predicate: applyConstraints(L) That applies constraints to the variables in L such that no two contiguous elements in L are both odd or even how can I do that? With a fixed size L it's simple but what about a variable size L? I need that to be done using sicstus-prolog clpfd library. 回答1: Inspired by @MatsCarlsson's version, I tried to minimize the number of constrained variables involved: applyConstraints(Xs) :- S #\= R, applyConstraints(Xs, S, R). applyConstraints([], _,

Generalizing Fibonacci sequence with SICStus Prolog

我的未来我决定 提交于 2019-12-31 01:00:15
问题 I'm trying to find a solution for a query on a generalized Fibonacci sequence (GFS). The query is: are there any GFS that have 885 as their 12th number? The initial 2 numbers may be restricted between 1 and 10. I already found the solution to find the Nth number in a sequence that starts at (1, 1) in which I explicitly define the initial numbers. Here is what I have for this: fib(1, 1). fib(2, 1). fib(N, X) :- N #> 1, Nmin1 #= N - 1, Nmin2 #= N - 2, fib(Nmin1, Xmin1), fib(Nmin2, Xmin2), X #=

Simple nth1 predicate in Prolog

限于喜欢 提交于 2019-12-29 08:48:09
问题 With SWI Prolog, there's a predicate that finds the nth item in a list called nth1. I want to implement my own version of the predicate but SWI's is so complicated if you look at the listing(nth1) code. Is there a simpler way of doing it? Thank you :). 回答1: The SWI code is a bit complex because the predicate can be used to generate from a variable index: ?- nth1(Idx,[a,b,c],X). Idx = 1, X = a ; Idx = 2, X = b ; Idx = 3, X = c ; false. If you don't want that behavior, nth1/3 can be implemented

Simple nth1 predicate in Prolog

夙愿已清 提交于 2019-12-29 08:48:07
问题 With SWI Prolog, there's a predicate that finds the nth item in a list called nth1. I want to implement my own version of the predicate but SWI's is so complicated if you look at the listing(nth1) code. Is there a simpler way of doing it? Thank you :). 回答1: The SWI code is a bit complex because the predicate can be used to generate from a variable index: ?- nth1(Idx,[a,b,c],X). Idx = 1, X = a ; Idx = 2, X = b ; Idx = 3, X = c ; false. If you don't want that behavior, nth1/3 can be implemented

Understanding CLP(FD) Prolog code of N-queens problem

亡梦爱人 提交于 2019-12-29 07:18:16
问题 I am trying to understand N-queens problem's solution as given below: :- use_module(library(clpfd)). n_queens(N, Qs) :- length(Qs, N), Qs ins 1..N, safe_queens(Qs). safe_queens([]). safe_queens([Q|Qs]) :- safe_queens(Qs, Q, 1), safe_queens(Qs). safe_queens([], _, _). safe_queens([Q|Qs], Q0, D0) :- Q0 #\= Q, abs(Q0 - Q) #\= D0, D1 #= D0 + 1, safe_queens(Qs, Q0, D1). I am not able to understand the below snippet: safe_queens([]). safe_queens([Q|Qs]) :- safe_queens(Qs, Q, 1), safe_queens(Qs).

reversible “binary to number” predicate

我只是一个虾纸丫 提交于 2019-12-28 02:06:38
问题 What is the best way to convert binary bits (it might be a list of 0/1, for example) into numbers in a reversible way. I've written a native predicate in swi, but is there better solution ? Best regards 回答1: Use CLP(FD) constraints, for example: :- use_module(library(clpfd)). binary_number(Bs0, N) :- reverse(Bs0, Bs), foldl(binary_number_, Bs, 0-0, _-N). binary_number_(B, I0-N0, I-N) :- B in 0..1, N #= N0 + B*2^I0, I #= I0 + 1. Example queries: ?- binary_number([1,0,1], N). N = 5. ?- binary

Splitting a list of integers into a list of positive integers and a list of negative integers

柔情痞子 提交于 2019-12-28 01:35:27
问题 I've been trying to create a predicate in Prolog which splits a list of integers into a list of positive integers and into a list of negative integers. Sample query with expected result: ?- split([1,-2,3,4,-8],X,Y). X = [1,3,4], Y = [-2,-8]. This is the code I got so far: split([], [], []). split([Head|Tail], List1, List2) :- split(Tail, [Head|List1], List2), Head>=0. split([Head|Tail], List1, List2) :- split(Tail, List1, [Head|List2]), Head<0. I can't seem to figure out what I'm doing wrong.

SWI Prolog CLP(FD) scheduling

巧了我就是萌 提交于 2019-12-24 13:26:32
问题 I am solving a scheduling task in SWI Prolog using the CLPFD library. Since it is the first time I solve something more serious than was the sendmory I probably need some good advices from more experienced users. Let me briefly describe the domain/the task. Domain I have a "calendar" for a month. Everyday there are 2 for the whole day, 2 for the whole night (long 12h service). There are also, only Mon-Fri 10 more workers for 8 hours (short service). The domain constraints are, obviously:

How to write kind of Conditional Planning in Prolog?

☆樱花仙子☆ 提交于 2019-12-24 10:02:16
问题 I tried to write a prolog code that can understand student program written in C#. Now I'm stuck in the process of recognizing the 'if' statement in student's program. For example: The following is the code that I expect from the student. int d = int.Parse(Console.ReadLine()); // value d is inputted by user int s = 0; if (d>0) s = 2; else if (d==0) s = 1; else s = 0; I defined the goal of this expected code as: goal:- hasVarName(Vid_s, s), hasVarName(Vid_d, d), hasVarValue(Vid_d, Vd), ((not(gt

Prolog, testing labeling heuristics

最后都变了- 提交于 2019-12-24 07:26:03
问题 I'm working on some experiments for comparing different labeling heuristics in Sicstus Prolog. But I keep getting into 'Resource error: insufficient memory'. I'm pretty sure I'm doing something wrong in my testcode. The following code will replicate my problem: :- use_module(library(clpfd)). :- use_module(library(lists)). atest( R, C ):- X is R * C, length( M, X), domain( M, 0, X), all_distinct( M ), statistics(walltime, [_,_SinceLast]), labeling( [],M ), statistics(walltime, [_,SinceLast]),