I'm trying to implement the Hill Climbing algorithm and this algorithm should return the sequence of cities considered good (with the smallest distance between cities) from the traveling salesman problem based on an adjacency array, however, I'm having difficulty implementing the avaliar()
and sucessor()
methods, I do not know what approach I should take to make these methods work as expected.
The implementation is in JavaScript:
let matrizAdjacente = [
[0, 1, 2, 3, 4, 5],
[2, 0, 5, 2, 1, 1],
[1, 8, 0, 2, 3, 4],
[2, 2, 1, 0, 6, 3],
[4, 2, 3, 1, 0, 4],
[3, 1, 3, 2, 2, 0]
];
let sequenciaCidades = [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6];
let solucaoInicial = function() {
return [4, 5, 1, 6, 3, 2];
}
let avaliar = function(estado) {
}
let sucessor = function(atual, novo) {
}
let hillClimbing = function() {
let novo = null;
let atual = solucaoInicial();
let valorAvaliadoAtual = avaliar(atual);
let valorAvaliadoNovo = 0;
while (1) {
novo = sucessor(atual, novo);
valorAvaliadoNovo = avaliar(novo);
if (valorAvaliadoNovo < valorAvaliadoAtual) {
atual = novo;
valorAvaliadoAtual = valorAvaliadoAtual;
} else break;
}
return atual;
}
This algorithm consists of examining the successors of the current state and moving to the first state that is larger than the current one. However, it is necessary to implement the avaliar()
and sucessor()
methods, the successor method will make a switch between states and the evaluate method will return a given value a sequence.
Question
So, how could I implement the avaliar()
and sucessor()
method of the Slope Climb algorithm?