TSP
Previous  Top  Next

(continued from click here)

This shows how to calculate a route, which passes through several nodes in an optimum order. The result of the route is stored in an external file.

NOTE: From v2.24 this can also be done with function RouteList instead - see sample.

For simplicity, we assume access to the coordinates of the point objects in an array:

This first loop enters all points into the node list
for i = 1 to theme.count
  RWnetBase1.Coordinate2node(x[i],y[i],node,dist)
  RWcalc1.nodelistset(i,node)
next i

Enable ATSP mode (only available in RW Net Pro)
RWcalc1.ATSP = true

Actual TSP calculation (mode = 1, meaning round-trip and no time-limit on calculations)
routetime = RWcalc1.TSP2(theme.count,1,0)
routedist = RWcalc1.TSP2extra

Create the route as "output.shp"
RWcalc1.RouteFileCreate("output",2)
RWcalc1.RouteFileFieldAdd("LinkID",2,0,0)
RWcalc1.RouteFileFieldAdd("Minutes",5,0,0)
TotalTime = 0
for j = 1 to theme.count 
(in mode 0 or 2, use count-1)
  node1 = RWcalc1.nodelistget(RWcalc1.nodelistgetnewpos(j))
  if j<theme.count then
    node2 = RWcalc1.nodelistget(RWcalc1.nodelistgetnewpos(j+1))
  else
    node2 = RWcalc1.nodelistget(RWcalc1.nodelistgetnewpos(1))
  end if

Piecewise route calculation

  RWcalc1.Route(node1,node2)
  RWcalc1.GetNodeExtra(node2)

  for i = RWcalc1.RouteFind(node2)-1 to 1 step -1
    t = rwcalc1.RouteGetLink(i)
    Time = TotalTime + rwcalc1.GetNodeCost(rwcalc1.RouteGetNode(i))
    RWcalc1.RouteFileRecordAdd(1,str$(abs(t))+", "+str$(Time))
    if t>0 then
      rwcalc1.RouteFileRecordAddSection(t,0,1)
    else
      rwcalc1.RouteFileRecordAddSection(-t,1,0)
    end if
  next i
  TotalTime = Time
next j
RWcalc1.RouteFileClose