my tree's [ 'T' ] are not visable in the 'solotion'
i dont think this is the solution my code gives
Code: Select all
''bluatigro 24 jul 2018
''tree and tent puzle.
''on a farm there are some tree's
''every tree has a tent near
''tent's do not near tent's also diagonal
''the number's at the end of a
''colom or row are the number
''of tent's in that row or colom
function in( no as integer , x as integer , y as integer ) as integer
return no * 100 + x * 9 + y
end function
dim shared as integer gen( in( 200 , 10 , 8 ) ) , t( 10 , 8 )
dim shared as integer ryy( 200 ) , fout( 200 )
dim shared as integer xx( 10 ) , yy( 8 )
const as integer notent = 0
const as integer empty = 1
const as integer tent = 2
const as integer tree = 3
function neartree( x as integer , y as integer ) as integer
dim as integer uit = 0
if x + 1 <= 10 then
if t( x + 1 , y ) = tree then uit = 1
end if
if x - 1 >= 1 then
if t( x - 1 , y ) = tree then uit = 1
end if
if y + 1 <= 8 then
if t( x , y + 1 ) = tree then uit = 1
end if
if y - 1 >= 0 then
if t( x , y - 1 ) = tree then uit = 1
end if
return uit
end function
function fitness( no as integer ) as integer
dim as integer uit = 0 , tel , x , y
for x = 1 to 10
tel = 0
for y = 0 to 8
if gen( in( no , x , y ) ) = tent then
tel = tel + 1
end if
next y
uit = uit + abs( tel - xx( x ) )
next x
for y = 0 to 8
tel = 0
for x = 1 to 10
if gen( in( no , x , y ) ) = tent then
tel = tel + 1
end if
next x
uit = uit + abs( tel - yy( y ) )
next y
return uit
end function
function rnd_int( l as integer , h as integer ) as integer
return int( rnd * ( h - l + 1 ) + l )
end function
sub crossover( a as integer , b as integer , uit as integer )
dim as integer x , y , z
for x = 1 to 10
for y = 0 to 8
if rnd < .5 then
z = gen( in( a , x , y ) )
else
z = gen( in( b , x , y ) )
end if
gen( in( uit , x , y ) ) = z
next y
next x
end sub
sub mutate( no as integer )
dim as integer x , y
x = rnd_int( 1 , 10 )
y = rnd_int( 0 , 8 )
while gen( in( no , x , y ) ) = tree _
or gen( in( no , x , y ) ) = notent
x = rnd_int( 1 , 10 )
y = rnd_int( 0 , 8 )
wend
if gen( in( no , x , y ) ) = empty then
gen( in( no , x , y ) ) = tent
else
gen( in( no , x , y ) ) = empty
end if
end sub
dim as integer x , y , no
dim as string p , px
read px
for y = 0 to 8
read p
for x = 1 to 10
if mid( p , x , 1 ) = "T" then
t( x , y ) = tree
end if
next x
yy( y ) = val( right( p , 1 ) )
next y
for x = 1 to 10
xx( x ) = val( mid( px , x , 1 ) )
next x
for x = 1 to 10
for y = 0 to 8
if neartree( x , y ) then
t( x , y ) = empty
end if
next y
next x
restore
data " 2 2 1 2 3 2 2 1 3 2"
data "T T T 2"
data " T T2"
data " 1"
data " T T 3"
data " TT1"
data " 2"
data " T T T T1"
data " T T 3"
data " T T 1"
data " T T T4"
for no = 0 to 200
for x = 1 to 10
for y = 0 to 8
if t( x , y ) = empty then
if rnd < .5 then
gen( in( no , x , y ) ) = tent
end if
end if
next y
next x
next no
for no = 0 to 200
ryy( no ) = no
next no
dim as integer done = 0 , a , b
dim as integer tel = 0 , h , l , help
while tel < 1000 and fitness( ryy( 0 ) ) <> 0
tel += 1
for no = 0 to 200
fout( no ) = fitness( no )
next no
for h = 1 to 200
for l = 0 to h - 1
if fout( ryy( h ) ) < fout( ryy( l ) ) then
help = ryy( h )
ryy( h ) = ryy( l )
ryy( l ) = help
end if
next l
next h
for no = 20 to 200
a = rnd_int( 0 , 20 )
b = rnd_int( 0 , 20 )
crossover ryy( a ) , ryy( b ) , ryy( no )
if rnd < .1 then
mutate ryy( no )
end if
next no
print tel , fout( ryy( 0 ) )
wend
for y = 0 to 8
print " " ;
for x = 1 to 10
select case gen( in( ryy(0) , x , y ) )
case tree
print "T " ;
case tent
print "A " ;
case empty
print " " ;
case else
print ". " ;
end select
next x
print yy( y )
next
print
print px
print "[ game over ]"
sleep