Chris Mears wrote: > 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. [...] > 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);
You can use m.row and m.col here, like this: branch(*this, m.row(0), ...); branch(*this, m.col(0), ...); > 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); Here you could just branch over the whole thing, as at this point the first row and column will already be assigned and thus ignored in the branching: branch(*this, cells, ...); > 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?) It's merely a matter of taste, not of efficiency, really. Cheers, Guido _______________________________________________ Gecode users mailing list us...@gecode.org https://www.gecode.org/mailman/listinfo/gecode-users