Hill climbing with 2d array

41 Views Asked by At

This is a continuation of the online qualification round, hash code 2017.

I have a 2d array, each row is a cache, each column is a file, for example:

int[][] solution = {
    {0, 0, 1, 0, 0},
    {0, 1, 0, 1, 0},
    {1, 1, 0, 0, 0}
};

The array shows how files are allocated to cache servers, [0][2] = 1 means file2 has been allocated to cache 0. Each file has a size, and the only constraint is that each cache must not exceed its capacity. The fitness score is then calculated for this solution.

I want to perform hill climbing, in order to find the best solution. These are the high level instructions:

• generate all the neighbouring solutions, by making simple "moves"

• keep the best (fittest) solution. And iterate.

public int[][] hillClimbing(int[][] solution, int n) {
    double currentScore = fitness(solution);
    for (int i = 0; i < n; i++) {
        int[][] neighbourSolution = generateNeighbourSolution(solution);
        double neighborScore = fitness(neighborSolution);
        if (neighborScore > currentScore) {
            solution = neighborSolution;
            currentScore = neighborScore;
        }
    }
    return solution;
}

I think this is correct so far, but how do you make the neighbouring solution?

0

There are 0 best solutions below