Question on min_max
From: Bryan (nox_at_yeah.net)
Date: 11/20/03
- Previous message: Jens Kilian: "Re: Efficiency of cuts vs. conditionals"
- Next in thread: Joachim Schimpf: "Re: Question on min_max"
- Reply: Joachim Schimpf: "Re: Question on min_max"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 20 Nov 2003 04:37:53 -0800
Hi all,
I'm new on this language and am now writing a little program under
Eclipse, trying to put n queens on a chessboard and maximize the
unattacked positions. The min_max build-in doesn't work the way I
supposed. It shows every possible arrangement of the queens without
actually trying to find the ones with the maximal "Cost", the number
of safe positions. Could someone please take a look at my code and
tell me what I have done wrong?
Thanks in advance!
Bryan
=================
:- lib(fd).
queens(N, Cb,Qs):-
declareDomains(N, Square),
%generateConstraints(N, Square, Qs),
length(Qs,N),
Rows is Square[1..N,1..N],
flatten(Rows, Cb),
count_queens(Cb,Q,Cost,Qs,Square),
Cost #>=11, % I have to add this now to
% keep the program from giving incorrect answers.
min_max(labeling(Cb),Cost).
declareDomains(N,Square):-
dim(Square, [N,N]), % make the variables
Square[1..N,1..N] :: [0..2]. % their range
%0safe, 1attacked 2queenlocated
count_queens([],0,0,[],Square).
count_queens([2|Cb],N+1,S,[P|Qs],Square):-
dim(Square,[M,M]),
length(Cb,OP),
P is M*M - OP -1,
Row is P//M+1,
mod(P,M, Co),
Col is Co+1,
( for(I,1,M), param(M,Square,Row,Col) do
Square[Row,I] #>0,
Square[I,Col] #>0,
Tmpa is I+Col-Row,
confine(Square,I,Tmpa,M),
Tmpb is Col+Row-I,
confine(Square,I,Tmpb,M)
),
count_queens(Cb,N,S,Qs,Square).
count_queens([0|Cb],N,S+1,Qs,Square):-
count_queens(Cb,N,S,Qs,Square).
count_queens([1|Cb],N,S,Qs,Square):-
count_queens(Cb,N,S,Qs,Square).
confine(Square,Row,Col,M):- Row =<0.
confine(Square,Row,Col,M):- Row > M.
confine(Square,Row,Col,M):- Col =<0.
confine(Square,Row,Col,M):- Col > M.
confine(Square,Row,Col,M):-
Row =< M,
Row > 0,
Col =< M,
Col > 0,
Square[Row,Col] #>0.
- Previous message: Jens Kilian: "Re: Efficiency of cuts vs. conditionals"
- Next in thread: Joachim Schimpf: "Re: Question on min_max"
- Reply: Joachim Schimpf: "Re: Question on min_max"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|