For Programmers: Free Programming Magazines  


Home > Archive > Prolog > June 2005 > How to counting solutions ?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to counting solutions ?
Pabis

2005-06-06, 8:57 pm

Hello!

I try to counting solutions in my programs but it doesn't work... Now i have
no idea. Could you help me ?
I'll be very happy ;-)

Thanks from the mountain - Pabis.

My program:

domains
K1,K2,K3,K4 = Integer

predicates

nondeterm kod.
nondeterm k(K1,K2,K3,K4).

goal
kod.

clauses
k(0,0,0,1).
k(1,0,0,0).
k(0,1,0,0).
k(0,0,1,0).
k(1,1,1,0).
k(1,1,0,1).
k(1,0,1,1).
k(0,1,1,1).

kod:-
k(A1,B1,C1,D1),
k(A2,B2,C2,D2),
k(A3,B3,C3,D3),
k(A4,B4,C4,D4),
k(A5,B5,C5,D5),
k(A6,B6,C6,D6),
A1+B1+C1+D1+A2+B2+C2+D2+A3+B3+C3+D3+A4+B
4+C4+D4+A5+B5+C5+D5+A6+B6+C6+D6=10,
(A1+A2+A3+A4+A5+A6) mod 2 = 1,
(B1+B2+B3+B4+B5+B6) mod 2 = 1,
(C1+C2+C3+C4+C5+C6) mod 2 = 1,
(D1+D2+D3+D4+D5+D6) mod 2 = 1,

write (A1," ",A2," ",A3," ",A4," ",A5," ",A6),nl,
write (B1," ",B2," ",B3," ",B4," ",B5," ",B6),nl,
write (C1," ",C2," ",C3," ",C4," ",C5," ",C6),nl,
write (D1," ",D2," ",D3," ",D4," ",D5," ",D6),nl,nl,
fail.


Piotr Wilkin

2005-06-07, 4:02 pm

Pabis wrote:

> Hello!
>
> I try to counting solutions in my programs but it doesn't work... Now i have
> no idea. Could you help me ?
> I'll be very happy ;-)


Generally, a good naive and general method is setof(solution(X1, X2...,
XN), predicate(X1, X2..., XN), Solutions), length(Solutions, X).

> My program:

[cut]
> A1+B1+C1+D1+A2+B2+C2+D2+A3+B3+C3+D3+A4+B
4+C4+D4+A5+B5+C5+D5+A6+B6+C6+D6=10,
> (A1+A2+A3+A4+A5+A6) mod 2 = 1,
> (B1+B2+B3+B4+B5+B6) mod 2 = 1,
> (C1+C2+C3+C4+C5+C6) mod 2 = 1,
> (D1+D2+D3+D4+D5+D6) mod 2 = 1,


This is not valid Prolog code (unless you're using some sort of wrapper
library). In Prolog, + as well as mod is a term constructor, so 1 as an
atomic value (integer) will never unify with a compound term whose head
is "mod", because = is not an equality operator like in non-logic
programming languages, but rather a unification operator. For
arithmetical equality, you'll have to read about the operator "is".


Greetings,
Piotr Wilkin

A.L.

2005-06-07, 4:02 pm

On Tue, 07 Jun 2005 14:05:57 +0200, Piotr Wilkin <pwl@pwl.pl> wrote:

>programming languages, but rather a unification operator. For
>arithmetical equality, you'll have to read about the operator "is".


???... Equality?...

A.L.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com