[gecode-users] Customised static variable ordering

Chris Mears cmears at infotech.monash.edu.au
Sun Sep 27 13:04:56 CEST 2009


Hello Gecoders,

I'd like to perform a depth first search with a specific static variable
ordering.  I have a matrix of integer variables and I want to label the
top row first, then the left column, then the rest of the matrix in
standard top-to-bottom, left-to-right order.

The naive search shown here gives a standard top-to-bottom,
left-to-right ordering:

    class Problem : public Space {
        IntVarArray cells;
        Problem(int n) {
            Matrix<IntVarArray> m(cells, n, n);
            // ...
            // set up constraints on cells via m
            // ...
            branch(*this, cells, INT_VAR_NONE, INT_VAR_MIN);
        }
    }

I have managed to achieve the ordering I want, like this (with the new
bits marked with comments):

    class Problem : public Space {
        IntVarArray cells;
        IntVarArray ordering;   // *** added this array ***
        Problem(int n) {
            Matrix<IntVarArray> m(cells, n, n);
            // ...
            // set up constraints on cells via m
            // ...
            // *** added this block ***
            int index = 0;
            for (int c = 0 ; c < n ; c++)
                ordering[index++] = m(c,0);
            for (int r = 1 ; r < n ; r++)
                ordering[index++] = m(0,r);
            for (int r = 1 ; r < n ; r++)
                for (int c = 1 ; c < n ; c++)
                    ordering[index++] = m(c,r);
            // *** search on "ordering" instead of "cells" ***
            branch(*this, cells, INT_VAR_NONE, INT_VAR_MIN);
        }
    }

This seems to work.  My question: is this the best way to do it?  (It
just occurred to me that maybe it could be done using Slices -- is that
right?)

Thanks,
Chris




More information about the gecode-users mailing list