[tsp_getcosts]

Description

Link: [tsp_getcosts]
Author: Jason Huck
Category: Math
Version: 8.x
License:
Posted: Jan. 22, 2006
Updated: Jan. 22, 2006
More by this author...
This tag accepts a nested array of coordinates and generates a cost matrix, using [tsp_getdistance] to calculate the distance from each point to every other point in the list. This matrix provides the data necessary for [tsp_gettour] to calculate a "next-closest tour" to solve basic TSP problems.

Requires [tsp_getdistance], available here.

Parameters

none


Sample Usage

// test data	
var('cities') = array(
	'Location A' = array(-82.521102,37.484227),
	'Location B' = array(-84.622564,37.059857),
	'Location C' = array(-84.622668,37.059893),
	'Location D' = array(-82.642757,38.480449),
	'Location E' = array(-84.600683,37.031884),
	'Location F' = array(-85.062656,36.982708),
	'Location G' = array(-84.623724,37.055167),
	'Location H' = array(-84.622564,37.059857),
	'Location I' = array(-84.072400,37.107600),
	'Location J' = array(-84.335426,36.841322)	
);

// cost example 	
// need an array of just the coordinate pairs	
var('points' = array);

'Raw Order<br>\n';

iterate($cities, local('i'));
	// just to show the original order
	loop_count + '. ' + #i->first + '<br>\n';

	// Flipping x/y values because my test data is backwards...
	$points->insert(array(#i->second->get(2),#i->second->get(1)));
/iterate;

var('costs') = tsp_getcosts($points);

// show the matrix
'<br><br>Cost Matrix:<br>\n';
'<table border="1">\n';

iterate($costs, local('i'));
	'<tr align="right">';
	
	iterate(#i, local('x'));
		local('x') = decimal(#x);
		#x->setformat( -precision=6);
	
		'<td>' + decimal(#x) + '</td>';
	/iterate;
	
	'</tr>\n';
/iterate;

'</table>\n';
						

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
define_tag(
	'getcosts',
	-namespace='tsp_',
	-priority='replace',
	-required='points',
	-type='array',
	-description='Generate a cost matrix for a set of points.'
);
	local('out' = array);
	
	iterate(#points, local('startSet'));
		local('tmp' = array);
	
		iterate(#points, local('costSet'));
			local('distance') = @tsp_getdistance(
				-startLat = #startSet->get(1),
				-startLong = #startSet->get(2),
				-endLat = #costSet->get(1),
				-endLong = #costSet->get(2)
			);
			
			#tmp->insert(#distance);
		/iterate;
		
		#out->insert(#tmp);
	/iterate;
	
	return(#out);
/define_tag;

 

Related Tags



Comments

none

Email:


Password:



Newest

Most Popular