clpfd

reversible “binary to number” predicate

流过昼夜 提交于 2019-11-27 04:40:55
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 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_number(Bs, 5). Bs = [1, 0, 1] . ?- binary_number(Bs, N). Bs = [], N = 0 ; Bs = [N], N in 0..1 ; etc. Here is

Prolog Constraint Processing : Packing Squares

一世执手 提交于 2019-11-27 03:02:46
问题 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

Solving N-Queens Problem… How far can we go?

风格不统一 提交于 2019-11-27 01:49:17
问题 The N-Queens Problem: This problem states that given a chess board of size N by N, find the different permutations in which N queens can be placed on the board without any one threatening each other. My question is: What is the maximum value of N for which a program can calculate the answer in reasonable amount of time? Or what is the largest N we have seen so far? Here is my program in CLPFD(Prolog): generate([],_). generate([H|T],N) :- H in 1..N , generate(T,N). lenlist(L,N) :- lenlist(L,0

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

北战南征 提交于 2019-11-26 21:58:32
问题 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,

Reversible numerical calculations in Prolog

白昼怎懂夜的黑 提交于 2019-11-26 20:17:57
问题 While reading SICP I came across logic programming chapter 4.4. Then I started looking into the Prolog programming language and tried to understand some simple assignments in Prolog. I found that Prolog seems to have troubles with numerical calculations. Here is the computation of a factorial in standard Prolog: f(0, 1). f(A, B) :- A > 0, C is A-1, f(C, D), B is A*D. The issues I find is that I need to introduce two auxiliary variables ( C and D ), a new syntax ( is ) and that the problem is

Find powers of 2 in a list Prolog

自闭症网瘾萝莉.ら 提交于 2019-11-26 12:33:20
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 wrong and I only get "false" as an answer. Here's how you can find the "powers of two" in logically

Faster implementation of verbal arithmetic in Prolog

≡放荡痞女 提交于 2019-11-26 07:49:05
问题 I already made a working generalized verbal arithmetic solver in Prolog but it\'s too slow. It takes 8 minutes just to run the simple expression S E N D + M O R E = M O N E Y. Can someone help me to make it run faster? /* verbalArithmetic(List,Word1,Word2,Word3) where List is the list of all possible letters in the words. The SEND+MORE = MONEY expression would then be represented as verbalArithmetic([S,E,N,D,M,O,R,Y],[S,E,N,D],[M,O,R,E],[M,O,N,E,Y]). */ validDigit(X) :- member(X,[0,1,2,3,4,5

Using a constrained variable with `length/2`

半世苍凉 提交于 2019-11-26 03:45:33
问题 Here is the problem: $ swipl Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.3.6-5-g5aeabd5) Copyright (c) 1990-2015 University of Amsterdam, VU Amsterdam SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. Please visit http://www.swi-prolog.org for details. For help, use ?- help(Topic). or ?- apropos(Word). ?- use_module(library(clpfd)). true. ?- N in 1..3, length(L, N). N = 1, L = [_G1580] ; N = 2, L

Most general higher-order constraint describing a sequence of integers ordered with respect to a relation

*爱你&永不变心* 提交于 2019-11-25 22:36:01
问题 In CLP(FD), we frequently need to state: \"This is a list of integers and finite domain variables in (sometimes: strictly ) ascending/descending order.\" Is there any CLP(FD) system that provides a general (parametrisable) built-in constraint for this task? SWI-Prolog provides a constraint called chain/2 , which is similar to what I am looking for. However, the name is slightly too specific to encompass all relations that the constraint can describe (example: #< is not a partial order but