TSP - location based
Previous  Top  Next

(continued from click here)

This shows how to calculate a route, which passes through several locations 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 location list
for i = 1 to theme.count
  RWnetBase1.Coordinate2location(x[i],y[i],link,percent,side,dist,xnew,ynew)
  if percent=0 then
    percent = 0.0001
  else if percent=1 then
    percent = 0.9999
  end if
  RWcalc1.locationlistset(i,link,percent)
next i

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

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

Create the route as "output.shp"
RWcalc1.RouteFileCreate("output",1)
RWcalc1.RouteFileFieldAdd("LinkID",2,0,0)
TotalTime = 0
for j = 1 to theme.count-1 
(in mode 1, use theme.count instead)
  RWcalc1.locationlistget(RWcalc1.locationlistgetnewpos(j),link1,percent1)
  if j<theme.count then
    RWcalc1.locationlistget(RWcalc1.locationlistgetnewpos(j+1),link2,percent2)
  else
    RWcalc1.locationlistget(RWcalc1.locationlistgetnewpos(1),link2,percent2)
  end if

Piecewise route calculation

  routetime = RWcalc1.RouteDyn(link1,link2,Percent1,Percent2,fromto1,fromto2,routelength,routedist)

  rwcalc1.RouteFileRecordAdd(1,str$(link1))
  if (link1=link2) and (routelength=0) then
    rwcalc1.RouteFileRecordAddSection(link1,percent1,percent2)
  else
    if fromto1=0 then
      rwcalc1.RouteFileRecordAddSection(link1,percent1,0)
    else
      rwcalc1.RouteFileRecordAddSection(link1,percent1,1)
    end if
    for i = routelength-1 to 1 step -1
      t = rwcalc1.RouteGetLink(i)
      rwcalc1.RouteFileRecordAdd(1,str$(abs(t)))
      if t>0 then
        rwcalc1.RouteFileRecordAddSection(t,0,1)
      else
        rwcalc1.RouteFileRecordAddSection(-t,1,0)
      end if
    next i
    rwcalc1.RouteFileRecordAdd(1,str$(link2))
    if fromto2=0 then
      rwcalc1.RouteFileRecordAddSection(link2,0,percent2)
    else
      rwcalc1.RouteFileRecordAddSection(link2,1,percent2)
    end if
  end if
next j
RWcalc1.RouteFileClose