1

私が解決しようとしたTSPに基づいて、Rubyにシミュレートされたアニーリングを実装しようとしています(このコードをJavaから変換しました)。しかし、アニーリングが私の結果を最悪にしていることが判明しました! (PlayerPath は、シミュレートされたアニーリングを実行するパスを提供します - 貪欲なアルゴリズム 1 を実行してパスを取得しました)。誰かがコードをチェックして、何か問題があるかどうかを確認するのを手伝ってもらえますか?それとも、シミュレートされたアニーリングが常に改善するとは限らないのですか?

#BEGIN ANNEALING
    for i in 1..k
        temp = 10000000
        cooling = 0.003


    if (playerPath[i].length > 2) # if path is larger than 2

      bestPath = playerPath[i]
        while temp > 1
          newSolution = playerPath[i];

          firstPosition = rand(newSolution.length)
          secondPosition = rand(newSolution.length)
      if(firstPosition == 0 || firstPosition == newSolution.length-1)
        next
      end
      if(secondPosition == 0 || secondPosition == newSolution.length-1 )
        next
      end

      # swap cities
      tempStore = newSolution[firstPosition]
      newSolution[firstPosition] = newSolution[secondPosition]
      newSolution[secondPosition] = tempStore

      # Tabulation
      currentEnergy = calculate_distance(playerPath[i])
      neighbourEnergy = calculate_distance(newSolution)

      if(acceptanceProbability(currentEnergy,neighbourEnergy,temp) > rand)
          playerPath[i] = newSolution
          end

      if(calculate_distance(playerPath[i])< calculate_distance(bestPath))
        bestPath = playerPath[i];
      end

      temp *= (1-cooling);


        end

    end

  end


  #END ANNEALING

  #acceptanceProbability
def acceptanceProbability(energy, newEnergy,temperature) 
        # If the new solution is better, accept it
        if (newEnergy < energy)
            return 1.0
        end

        # If the new solution is worse, calculate an acceptance probability
        return Math.exp((energy - newEnergy) / temperature)

end
4

0 に答える 0