Using Baidu Map API and swarm ant algorithm to simulate and solve TSP problem

Source: Internet
Author: User
Tags cos sin

Objective

Recently, because of the change of work, the period also has to reflect and summarize the company's gains and losses, always feel that there is something that can be completed or completed better, of which the TSP problem is one of them. At the time of the development of a warehouse system, there is a line scheduling demand, at that time, I simply drew ideas on paper, found that the precision solution algorithm complexity is n!, so to Baidu, found the concept of NPC problem, but has not been a good study on this issue, Finally just choose the greedy algorithm this is the solution to approximate solution, just this is my first blog, take this "regret" surgery.

1, using Baidu Map API to simulate the TSP of each city point

1.1, call Baidu Map API resolution longitude and latitude

Here first to Baidu Map API to apply for a apikey, address resolution interface will be used, where the parameters of the address resolution interface can be accessed Http://lbsyun.baidu.com/index.php?title=uri/api/web open view, Here I first put it:

The URL in the example (where output I use JSON) is entered in the browser, and the following results are obtained:

Here the HttpRequest method is used to invoke the address resolution interface, similar to the crawler, and finally the result is deserialized into the entity class, the code is as follows:

Entity classes for address resolution results

View Code

Invoking API interfaces with Httrequest

View Code

1.2, the use of latitude and longitude to calculate the distance between two addresses

In fact, this online has a large number of similar calculation methods, but this method is a long time ago I quoted, the original source has not been turned over, had to point out here this algorithm is not my original, if fortunate to let the original author see, Please notify me to indicate the source. Here I first briefly introduce the idea of the algorithm, with the Earth's spherical sphere as the coordinate system origin as a three-dimensional coordinate system,

where coordinates a (X1,Y1,Z1), coordinate B (x2,y2,z2), note, wherein x=r*sin?*cos?,y=r* R*sin?*sin?,z=r*cos?, where the angle is converted to π to calculate, so that latitude and longitude *π/180, then the coordinate point at the latitude: =π/2-latitude *π/180; coordinates point in south Latitude:? =π/2+ latitude *π/180; coordinates point in the west longitude:? =π*2-latitude *π/180; After this conversion is complete, it can be done by giving (X1-X2) ^2+ (y1-y2) ^2+ (z1-z2) ^ 2 The formula calculates the straight distance between two points D, and then the cosine theorem to find the angle between two points, and finally calculate the spherical distance between two points, the code is as follows:

View Code

2. Finding approximate solution of shortest path by swarm ant algorithm

On the group ant algorithm, the introduction of the internet there are many, but steal lazy, only focus on the implementation of C #, here is the garden of data on the top of the Swarm ant algorithm theory and practice of the whole strategy--travel business and other path optimization problems, I pointed out that I understand this article more laborious points.

(1) Here the roulette betting method is a genetic algorithm, that is, by generating a random number to select the next city to visit, can be 0.081, 0.74, 0.18 as [0,0.081],[0.081,0.821],[0.821,1] so three intervals, Then a random number of 0 to 1 is generated (two decimal digits are reserved), when the random decimal fraction belongs to the interval [0,0.081], select City B, which belongs to [0.081,0.821], select City C, which belongs to [0.821,1], select City d. Here is an optimization of swarm ant algorithm, there are many ways to optimize, you can access the data on your own.

(2) A deep copy of the list on the code

The original author did not give the implementation, I use the extension method to achieve a bit, the code is as follows:

View Code

(3) Implementation of greedy algorithm

The original author also did not give, so I myself realized a bit (may be in time and space complex call there is a great flaw, this optimization work I temporarily do not deal with, we are interested can achieve their own), the code is as follows:

View Code

The entire swarm ant algorithm code basically follows the original author's code, does not have any change, therefore does not stick the entire set of.

3. Test code:

View Code

Operation Result:

Here pay attention to adjust the parameters of the Basetspantsystem class construction method, the stronger the pheromone (parameter a), that is, the higher the visibility, the more convergence, the more the result path is more single, the accuracy will be worse, we can adjust their own parameters to observe different results.

4. Summary

Here is basically the collection of other people's Code and results to solve their own problems, the article cited most of the code is also someone else, but the point is that you must be a little bit of understanding and try.

Resources:

"1" Sina blog, Chao Wu, according to two points latitude and longitude calculation distance "turn";

"2" blog Park, the top of the data, swarm ant algorithm theory and practice of the whole strategy--travel business and other path optimization problems of new methods;

"3" NetEase blog, Ant colony optimization algorithm aco;

"4" Blog park, Lemon Rain, evolutionary Computing: Genetic Algorithm _ Roulette choice (reprint);

"5" blog Park, Learning hard, [Advanced series of C #] topic one: Deep analysis and shallow copy;

Using Baidu Map API and swarm ant algorithm to simulate and solve TSP problem

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.