Traveling Salesmen Problem
op te lossen of een goede benadering te bedenken
TSP :
je wilt reizen langs n reeks steden
wat is de kortste weg langs alle steden
als je elke 1 keer bezoekt
- Code: Selecteer alles
WindowWidth = DisplayWidth
WindowHeight = DisplayHeight
global citymax , routemax , YN$
citymax = 100
routemax = 100
dim x( citymax ) , y( citymax ) , hh( citymax )
dim r( routemax + 1 , citymax ) , d( routemax )
dim rij( routemax )
for i = 0 to routemax
rij( i ) = i
next i
nomainwin
timer 1000 , [tmr]
for i = 1 to citymax - 1
x( i ) = 20 + rnd( 0 ) * ( WindowWidth - 40 )
y( i ) = 20 + rnd( 0 ) * ( WindowHeight - 40 )
next i
confirm "TSP fast ?" ; YN$
open "TSP" for graphics as #m
#m , "trapclose [quit]"
wait
[tmr]
for i = 0 to routemax
d( i ) = afstand( i )
next i
for h = 1 to routemax
for l = 0 to h
if d( rij( h ) ) < d( rij( l ) ) then
i = rij( h )
rij( h ) = rij( l )
rij( l ) = i
end if
next l
next h
for i = 10 to routemax
a = rnd( 0 ) * ( citymax - 1 ) + 1
b = rnd( 0 ) * ( citymax - 1 ) + 1
if a > b then
h = a : a = b : b = h
end if
if YN$ = "yes" then
for t = a to b
hh( t - a ) = r( rij( i ) , t )
next t
for t = a to b
r( rij( i ) , t ) = hh( b - t )
next t
else
h = r( rij( i ) , a )
r( rij( i ) , b ) = r( rij( i ) , a )
r( rij( i ) , a ) = h
end if
next i
#m , "cls"
#m , "down"
#m , "goto 0 0"
for i = 0 to citymax
a = x( r( rij( 0 ) , i ) )
b = y( r( rij( 0 ) , i ) )
#m , "color black"
#m , "goto " ; a ; " " ; b
#m , "backcolor red"
#m , "circlefilled 10"
next i
#m , "up"
#m , "flush"
wait
function afstand( no )
som = 0
for i = 1 to citymax
dx = x( r( no , i ) ) - x( r( no , i - 1 ) )
dy = y( r( no , i ) ) - y( r( no , i - 1 ) )
som = som + sqr( dx ^ 2 + dy ^ 2 + 1e-10 )
next i
afstand = som
end function
[quit]
#m , "close"
end
probleem : de [tmr] sub tekend niet alle steden
