Question on min_max

From: Bryan (nox_at_yeah.net)
Date: 11/20/03

  • Next message: Bart Demoen: "Re: Efficiency of cuts vs. conditionals"
    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.


  • Next message: Bart Demoen: "Re: Efficiency of cuts vs. conditionals"

    Relevant Pages

    • Re: Need help with font selection
      ... display a chessboard with several queens on it. ... html table and the queens are represented by the character '*'. ... internet and found a set of free chess fonts. ...
      (alt.html)
    • Re: Question on min_max
      ... Bryan wrote: ... It shows every possible arrangement of the queens without ... it harder to express your constraints. ... Btw, if you are using a recent version of ECLiPSe, you should use the ...
      (comp.lang.prolog)
    • Re: Need help with font selection
      ... display a chessboard with several queens on it. ... html table and the queens are represented by the character '*'. ... My problem is that I don't know how to select the queen character from ... All I'm asking is for a code example of how do I display the queen ...
      (alt.html)
    • Need help with font selection
      ... For a chess puzzle that I'm working on at the moment, ... display a chessboard with several queens on it. ... html table and the queens are represented by the character '*'. ... internet and found a set of free chess fonts. ...
      (alt.html)
    • help me with the program
      ... You are given a N x N chessboard & you are required to place N queens ... A sample I/p and o/p is given below (where N will be ... Sample o/p: ...
      (comp.lang.c)