WindowWidth = 700''DisplayWidth
WindowHeight = 500''DisplayHeight
global citymax , routemax , frame
global fastYN$ , crosoverYN$
citymax = 40
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
for j = 0 to citymax
r( i , j ) = j
next j
next i
nomainwin
for i = 1 to citymax - 1
x( i ) = 20 + rnd( 0 ) * ( WindowWidth - 40 )
y( i ) = 20 + rnd( 0 ) * ( WindowHeight - 100 )
next i
confirm "TSP fast ?" ; fastYN$
confirm "TSP crossover ?" ; crosoverYN$
timer 1000 , [tmr]
open "TSP" for graphics as #m
#m , "trapclose [quit]"
#m , "when characterInput [key]"
wait
[tmr]
for q = 0 to 9
for i = 0 to routemax
d( i ) = afstand( i )
next i
for h = 1 to routemax
for l = 0 to h - 1
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 = 0 to citymax
r( 0 , i ) = r( rij( 0 ) , i )
next i
''crosover
if crosoverYN$ = "yes" then
for i = 20 to routemax
a = int( rnd( 0 ) * 20 )
b = int( rnd( 0 ) * 20 )
for q = 1 to citymax - 1
r( i , q ) = r( a , r( b , q ) )
next q
next i
end if
''mutatons
for i = 20 to routemax
a = int( rnd( 0 ) * ( citymax - 2 ) + 1 )
b = int( rnd( 0 ) * ( citymax - 2 ) + 1 )
if a > b then
h = a : a = b : b = h
end if
if fastYN$ = "yes" then
for t = a to b
hh( t - a ) = r( i , t )
next t
for t = a to b
r( i , t ) = hh( b - t )
next t
else
h = r( i , a )
r( i , a ) = r( i , b )
r( i , b ) = h
end if
next i
next q
#m , "cls"
#m , "down"
#m , "size 5"
#m , "goto 0 0"
for i = 1 to citymax - 1
a = x( r( 0 , i ) )
b = y( r( 0 , i ) )
#m , "color black"
#m , "goto " ; a ; " " ; b
#m , "backcolor red"
#m , "circlefilled 10"
next i
#m , "goto 0 0"
#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
[key]
if inkey$ = chr$( 27 ) then goto [quit]
wait
[quit]
close #m
end