簡體   English   中英

模擬退火方法無法執行

[英]Simulated Annealing Method won't execute

目前,我正在嘗試創建一種刺激退火算法,以解決旅行商問題並為其創建GUI。 最初的城市(點)和線會顯示,但是我無法使doSA()方法成功運行。 任何想法,都有些困難。

class SAPanel extends JPanel implements ActionListener{
    private static final long serialVersionUID = 1L;
    private City city,city2,city3,city4,city5,city6,city7,city8,city9,city10,city11,city12,city13,city14,city15,city16,city17,city18,city19,city20;
    private int temp;
    private Tour best, currentSolution;
    int delay  = 10;
    static int POINTWIDTH = 8;
    static Color POINTCOLOR = Color.MAGENTA;
    static Color LINECOLOR = Color.CYAN;
    Timer timer = new Timer(delay, this);
    private static double coolingRate = .006; 


public void start(){
    temp = 10000;
    timer.start();
}


public void intiSA(){
    city = new City(60, 200);
    TourManager.addCity(city);
    city2 = new City(180, 200);
    TourManager.addCity(city2);
    city3 = new City(80, 180);
    TourManager.addCity(city3);
    city4 = new City(140, 180);
    TourManager.addCity(city4);
    city5 = new City(20, 160);
    TourManager.addCity(city5);
    city6 = new City(100, 160);
    TourManager.addCity(city6);
    city7 = new City(200, 160);
    TourManager.addCity(city7);
    city8 = new City(140, 140);
    TourManager.addCity(city8);
    city9 = new City(40, 120);
    TourManager.addCity(city9);
    city10 = new City(100, 120);
    TourManager.addCity(city10);
    city11 = new City(180, 100);
    TourManager.addCity(city11);
    city12 = new City(60, 80);
    TourManager.addCity(city12);
    city13 = new City(120, 80);
    TourManager.addCity(city13);
    city14 = new City(180, 60);
    TourManager.addCity(city14);
    city15 = new City(20, 40);
    TourManager.addCity(city15);
    city16 = new City(100, 40);
    TourManager.addCity(city16);
    city17 = new City(200, 40);
    TourManager.addCity(city17);
    city18 = new City(20, 20);
    TourManager.addCity(city18);
    city19 = new City(60, 20);
    TourManager.addCity(city19);
    city20 = new City(160, 20);
    TourManager.addCity(city20);

    //Initialize initial solution
    currentSolution = new Tour();
    currentSolution.generateIndividual();
    best = currentSolution;
    System.out.println("Initial solution distance: " + currentSolution.getDistance());
}

//energy represents the total distance of each tour
public static double acceptanceProbability(int energy, int newEnergy, double temp){
    //if currentSolution energy is larger than newEnergy, return 1.0
    if(newEnergy < energy){
        return 1.0;
    }

    //as temp decreases, acceptance of the new solution becomes more selective
    return Math.exp((energy-newEnergy) / temp);

}

public void doSA(){
    //set new solution as the current solution
    Tour newSolution = new Tour(currentSolution.getTour());
    //get two random points on the solution
    int tourPos1  = (int)(newSolution.tourSize() * Math.random());
    int tourPos2 = (int)(newSolution.tourSize() * Math.random());

    //get two cities depending on the selected points
    City citySwap1 = newSolution.getCity(tourPos1);
    City citySwap2 = newSolution.getCity(tourPos2);

    //swap the cities on the selected points
    newSolution.setCity(tourPos1, citySwap2);
    newSolution.setCity(tourPos2, citySwap1);

    int currentEnergy = currentSolution.getDistance();
    int neighborEnergy = currentSolution.getDistance();

    //accept new solution as the current solution if greater than random number
    if(acceptanceProbability(currentEnergy, neighborEnergy, temp) > Math.random()){
        currentSolution = new Tour(newSolution.getTour());
    }

    //keep the current solution as best if distance is greater
    if(currentSolution.getDistance() > newSolution.getDistance()){
        best = new Tour(currentSolution.getTour());
    }
    repaint();

}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2= (Graphics2D)(g);
    g2.setStroke(new BasicStroke(3));
    for(int x =0;x<19;x++){
        //draw lines connecting each city in solution
        g2.setColor(LINECOLOR);
        g2.drawLine(best.getCity(x).getX(), best.getCity(x).getY(), best.getCity(x+1).getX(), best.getCity(x+1).getY());
        g2.setColor(POINTCOLOR);
        //draw all points
        g2.fillOval(best.getCity(x).getX() - POINTWIDTH/2, best.getCity(x).getY() - POINTWIDTH/2, POINTWIDTH, POINTWIDTH);
    }

    //draw last reminding line, connecting last city to first city.
    g2.setColor(LINECOLOR);
    g2.drawLine(best.getCity(19).getX(), best.getCity(19).getY(), best.getCity(0).getX(), best.getCity(0).getY());
    g2.setColor(POINTCOLOR);
    g2.fillOval(best.getCity(0).getX() - POINTWIDTH/2, best.getCity(0).getY() - POINTWIDTH/2, POINTWIDTH, POINTWIDTH);
}




public void actionPerformed(ActionEvent e){
    temp *= coolingRate -1;
    if(temp >1){
        doSA();
        System.out.println("Final solution distance: " + best.getDistance());
        System.out.println("Tour: " + best);
    }
    else{
        ((Timer)e.getSource()).stop();

    }
}   

}

可能是您的初始參數:模擬退火對參數非常敏感,它需要2個參數(初始溫度和冷卻時間表),因此很難對其進行調整。 我的實現中 ,我根據仍然可用的時間將冷卻時間減少到1個參數。

也可能是您的acceptanceProbability方法中的錯誤。 編寫一個單元測試,以覆蓋所有極端情況,以證明它是正確的。 這是我的測試提示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM