Giving a set of points and the distance each pair of points, found the roite which go through the points minimizing the distance traveled.

More information about the TSP, the used algorithm belongs to the family of the genetic algorithms.

These do not ensure to find the optimal solution, but they find suboptimal solutions in a reasonable time.

 

1 geo.tspSolver

<geo.tspSolver
    lat_col='lat_col'
    lng_col='lng_col'
    id_col='id_col'
    origin_city_code='origin_city_code'
    end_city_code='end_city_code'
    is_closed='is_closed'
    population='population'
    generations='generations'
    mutation_rate='mutation_rate'
>
    <v_cities /> +
    <v_distances /> +
</geo.tspSolver>
Example

Define a StringBuffer with a string.

Copy
<xsql-script name='stringbuffer_sample'>
    <body>	
 <println><v_cities /></println>	
 	    	
+-------+-------+---------------+
|gps_lat|gps_lon|assign_customer|
|decimal|decimal|char           |
+-------+-------+---------------+
|41,3712| 2,1114|10018          |
|41,3816| 2,1625|10006          |
|41,3819| 2,1578|10007          |
|41,3835| 2,1475|10004          |
|41,3895| 2,1499|10003          |
|41,3946| 2,1545|10009          |
|41,3946| 2,1525|10002          |
|41,3963| 2,1578|10011          |
|41,4045| 2,1571|10010          |
|41,4182| 2,1811|10012          |
|41,3941| 2,1923|T1000          |
+-------+-------+---------------+
    
 
  <println><v_distances /></println>	
   
+--------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+
|codcli_1|pivot11   |pivot1    |pivot2    |pivot3    |pivot4    |pivot5    |pivot6    |pivot7    |pivot8    |pivot9    |pivot10   |
|char    |decimal   |decimal   |decimal   |decimal   |decimal   |decimal   |decimal   |decimal   |decimal   |decimal   |decimal   |
|<null>  |10002     |10003     |10004     |10006     |10007     |10009     |10010     |10011     |10012     |10018     |T1000     |
|        |route_dist|route_dist|route_dist|route_dist|route_dist|route_dist|route_dist|route_dist|route_dist|route_dist|route_dist|
+--------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+
|10002   |          |   1110,00|   2031,00|   2457,00|   2463,00|    965,00|   2143,00|    776,00|   4651,00|   6573,00|   1240,00|
|10003   |    984,00|          |   1388,00|   2423,00|   1904,00|    931,00|   2796,00|   2272,00|   6147,00|   5938,00|   1693,00|
|10004   |   2234,00|   1575,00|          |   1765,00|   1234,00|   1964,00|   4362,00|   2995,00|   6870,00|   4689,00|   1506,00|
|10006   |   1974,00|   1653,00|   2031,00|          |   1358,00|   1921,00|   3848,00|   2537,00|   5945,00|   5664,00|   1349,00|
|10007   |   2063,00|   1629,00|   1466,00|   1074,00|          |   2011,00|   3824,00|   2513,00|   5921,00|   5375,00|   1877,00|
|10009   |    851,00|    944,00|   1865,00|   2291,00|   2297,00|          |   2329,00|   1018,00|   4328,00|   6407,00|   1074,00|
|10010   |   2346,00|   2805,00|   3247,00|   3677,00|   3684,00|   2666,00|          |   1342,00|   3908,00|   7565,00|   2461,00|
|10011   |   1004,00|   1463,00|   2708,00|   2815,00|   2692,00|   1324,00|   1756,00|          |   4492,00|   6223,00|   1470,00|
|10012   |   5047,00|   5506,00|   6751,00|   5941,00|   5947,00|   5367,00|   3269,00|   3532,00|          |  10265,00|   4724,00|
|10018   |   6134,00|   4513,00|   4091,00|   5373,00|   4843,00|   4902,00|   7822,00|   6895,00|   9960,00|          |   6720,00|
|T1000   |   1108,00|   1205,00|   1583,00|   1209,00|   1216,00|   1055,00|   2850,00|   1539,00|   4845,00|   6037,00|          |
+--------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+    

    	
        <!-- SOLVE THE PROBLEM -->
        <vtable name='v_tsp_result'>
            <in>
                <geo.tspSolver  lat_col="1" lng_col="2" id_col="3" population="5000" generations="1000" mutation_rate="0.025" origin_city_code="T1000">
                    <cities />
                    <pivoted />
                </geo.tspSolver>
            </in>
        </vtable>    	

<println><v_tsp_result /></pritnnln>
+-------+------+-----+
|lat    |lng   |name |
|double |double|char |
+-------+------+-----+
|41,3941|2,1923|T1000|
|41,3819|2,1578|10007|
|41,3816|2,1625|10006|
|41,3946|2,1545|10009|
|41,4182|2,1811|10012|
|41,4045|2,1571|10010|
|41,3963|2,1578|10011|
|41,3946|2,1525|10002|
|41,3895|2,1499|10003|
|41,3835|2,1475|10004|
|41,3712|2,1114|10018|
+-------+------+-----+

    </body>
</xsql-script>