Organizing data Handling--annealing algorithm annealing Java implementation __ Path planning

Source: Internet
Author: User

Directly on the code:

Package sa;
Import java.util.ArrayList;

Import java.util.List; Solving tsp traveling salesman problem by simulated annealing algorithm/public class Simulatedannealing {public static list<city> Allcitys = new Arraylist<c

    Ity> (); Calculate accepted probability public static double acceptanceprobability (int energy, int newenergy, double temperature) {//if the new
        Better solution, accept if (Newenergy < energy) {return 1.0;
    Return Math.exp ((energy-newenergy)/temperature);

        }//return approximate best travel path private static Tour SA () {//initialization temperature Double temp = 10000;

        Cooling probability double coolingrate = 0.003;
        The initialized solution Tour currentsolution = new Tour ();

        Currentsolution.generateindividual ();

        System.out.println ("Initial Solution Distance:" + currentsolution.getdistance ());

        Set the currently optimal scheme Tour best = new Tour (Currentsolution.gettour ()); Loop know system cooling while (Temp > 1) {//Generate a neighbor Tour newsolution = NEW Tour (Currentsolution.gettour ());
            Gets the random position int tourPos1 = (int) (Newsolution.toursize () * math.random ());

            int tourPos2 = (int) (Newsolution.toursize () * math.random ());
            City CitySwap1 = newsolution.getcity (TOURPOS1);

            City CITYSWAP2 = newsolution.getcity (TOURPOS2);
            Exchange Newsolution.setcity (TourPos2, CITYSWAP1);

            Newsolution.setcity (TOURPOS1, CITYSWAP2);
            Get the cost of the new solution int currentenergy = Currentsolution.getdistance ();

            int neighbourenergy = Newsolution.getdistance ();
                Decide whether to accept the new scenario if (Acceptanceprobability (Currentenergy, Neighbourenergy, temp) > Math.random ()) {
            Currentsolution = new Tour (Newsolution.gettour ()); }//records found optimal scheme if (currentsolution.getdistance () < Best.getdistance ()) {Best
            = New Tour (Currentsolution.gettour ());

           } Cooling temp *= 1-coolingrate;
	return best;
	    private static void Init () {City city = new City (60, 200);
	    Allcitys.add (city);
	    City City2 = new City (180, 200);
	    Allcitys.add (City2);
	    City City3 = new City (80, 180);
	    Allcitys.add (CITY3);
	    City City4 = new City (140, 180);
	    Allcitys.add (CITY4);
	    City city5 = new City (20, 160);
	    Allcitys.add (CITY5);
	    City City6 = new City (100, 160);
	    Allcitys.add (CITY6);
	    City City7 = new City (200, 160);
	    Allcitys.add (CITY7);
	    City City8 = new City (140, 140);
	    Allcitys.add (CITY8);
	    City City9 = new City (40, 120);
	    Allcitys.add (CITY9);
	    City City10 = new City (100, 120);
	    Allcitys.add (CITY10);
	    City City11 = new City (180, 100);
	    Allcitys.add (CITY11);
	    City City12 = new City (60, 80);
	    Allcitys.add (CITY12);
	    City CITY13 = new City (120, 80);
	    Allcitys.add (CITY13);
	    City CITY14 = new City (180, 60);
	    Allcitys.add (CITY14); City city15 = new City (20, 40);
	    Allcitys.add (CITY15);
	    City CITY16 = new City (100, 40);
	    Allcitys.add (CITY16);
	    City City17 = new City (200, 40);
	    Allcitys.add (CITY17);
	    City City18 = new City (20, 20);
	    Allcitys.add (CITY18);
	    City city19 = new City (60, 20);
	    Allcitys.add (CITY19);
	    City City20 = new City (160, 20);
	Allcitys.add (CITY20);
        
        public static void Main (string[] args) {//Create all City Cities list init ();
        Solve tsp problem Tour best = sa ();
        System.out.println ("Final Solution Distance:" + best.getdistance ());
    System.out.println ("Tour:" + best);

 }
}
package sa;
    public class City {int x;

    int y;
        Generates a random city () {this.x = (int) (Math.random () *200);
    THIS.Y = (int) (Math.random () *200);
        Public city (int x, int y) {this.x = x;
    This.y = y;
    public int GetX () {return this.x;
    public int GetY () {return this.y;
        }//Calculate the distance between two cities public double Distanceto {int xdistance = Math.Abs (GetX ()-city.getx ());
        int ydistance = Math.Abs (GetY ()-city.gety ());

        Double distance = math.sqrt ((xdistance*xdistance) + (ydistance*ydistance));
    return distance;
    @Override public String toString () {return GetX () + "," +gety (); }
}

Package sa;
Import java.util.ArrayList;

Import java.util.Collections;
    public class tour{//Keep List of cities private ArrayList Tour = new arraylist<city> ();

    Cache distance private int distance = 0; Generates an empty path public Tour () {for (int i = 0; i < SimulatedAnnealing.allCitys.size (); i++) {Tour.
        Add (null);
    }///Copy Path public Tour (ArrayList Tour) {this.tour = (ArrayList) tour.clone ();
    Public ArrayList Gettour () {return tour; }//Creates a random individual public void generateindividual () {//Loop through all our destination C Ities and add them to We tour for (int cityindex = 0; Cityindex < SimulatedAnnealing.allCitys.size (); Cityinde
        X + +) {setcity (Cityindex, SimulatedAnnealing.allCitys.get (Cityindex));
    }//Random scrambling Path Collections.shuffle (tour); //Get one of the city's getcity (int tourposition) {return (cities) tour.get(tourposition);
        public void setcity (int tourposition, city city) {Tour.set (tourposition, city);
    Recalculate distance distance = 0;
            ///Get the total cost of the current distance public int getdistance () {if (distance = = 0) {int tourdistance = 0;
                for (int cityindex=0; Cityindex < Toursize (); cityindex++) {City fromcity = getcity (Cityindex);
                City destinationcity;
                if (Cityindex+1 < Toursize ()) {destinationcity = Getcity (cityindex+1);
                } else{destinationcity = getcity (0);
            } tourdistance + = Fromcity.distanceto (destinationcity);
        } distance = Tourdistance;
    return distance;
    //Get the number of cities in the current path public int toursize () {return tour.size ();
        @Override public String toString () {string genestring = ' | '; for (int i = 0; I < toursize ();
        i++) {genestring + = getcity (i) + "|";}
    return genestring;
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.