Traversal of an n-dimensional space

algorithm, java, multidimensional-array, recursion, search

Solution

Here is the general idea:

interface Callback {
   void visit(int[] p); // n-dimensional point
}

// bounds[] - each number the limits iteration on i'th axis from 0 to bounds[i]
// current - current dimension
// callback - point
void visit(int[] bounds, int currentDimension, int[] p, Callback c) {
   for (int i = 0; i < bounds[currentDimension]; i++) {
        p[currentDimension] = i;
        if (currentDimension == p.length - 1) c.visit(p);
        else visit(bounds, currentDimension + 1, p, c);
   }
}

/// now visiting
visit(new int[] {10, 10, 10}, 0, new int[3], new Callback() {
   public void visit(int[] p) {
        System.out.println(Arrays.toString(p));
   }
});

Problem

I'm trying to write an algorithm that will let me iterate over all desired points within an n-dimensional space to find the minimum of a function f(x) where x is a vector of size n. Obviously, searching a 2-d or 3-d space is fairly straightforward, you can simply do: ``` for(int i = 0; i < x; i++) { for(int j = 0; j < y; j++) { //and so on for however many dimensions you want ``` Unfortunately, for my problem, the dimensionality of the space is not fixed (I'm writing a generalised minimum finder for many functions in a statistical program) and so I'd have to write loops for each value of n I want to use - which might ultimately be rather large. I've been trying to get my head around how I could do this using recursion but can't quite see the solution - although I'm sure there is one there. The solution doesn't have to be recursive, but it must be general and efficient (the inner most line in that nested loop is going to get called an awful lot...). The way I'm representing the volume to search is a 2d array of double: ``` double[][] space = new double[2][4]; ``` This would represent a 4d space with the minimum and maximum bound in each dimension in position 0 or 1 of the array, respectively. Eg: ``` dim 0 1 2 3 min(0):-10 5 10 -0.5 max(1): 10 55 99 0.2 ``` Any ideas?

Original source

Related problems