zeeslag puzzel oplosser [ Geneties Algoritme ]

Algemene discussies en vragen omtrent Liberty BASIC programma's. Stuur zoveel mogelijk delen van listings (snippets) in als je hulp wilt.

Moderators: anthonio, Abcott

zeeslag puzzel oplosser [ Geneties Algoritme ]

Berichtdoor bluatigro » do apr 12, 2012 11:46 am

dit is n poging tot t maken van zeeslag puzzel oploser
daarbij gebruik ik n GGeneties Algoritme :
-evolutie in de computer

n GA werkt alsvolgt :
-1 maak n verzameling genen [ wilekuerig ]
-2 bereken de fitnes van elk gen
-3 sorteer de genen op fitnes
-4 maak met n paar ouder-genen kinder-genen
mbv kruising en mutaties
-5 als niet opgelost en generaties < getal dan ganaar 2

error :
-de oplossing wordt niet gevonden in 1000 generaties
-omdat t sorteer-algoritme niet goed werkt
Code: Selecteer alles
''this is a try at solving a seabatle-puzzle :
''find the ships in the grid
''then numbers at the end and bottom
''are the total parts in that row or colom
''the ships dont hit eatch other

''ships = #### ### ### ## ## ## # # # # = 20 parts
''0 = water : 1 = shippart
''.....0... 5
''......... 1
''......... 3
''......1.. 4
''......... 0
''.1....... 2
''......... 0
''......... 4
''......... 3
''411222314

dim gen$( 100 ) , fitnes( 100 )
seed$ = ".....0..." _
      + "........." _
      + "........." _
      + "......1.." _
      + "........." _
      + ".1......." _
      + "........." _
      + "........." _
      + "........."
ver$ = "513402043"
hor$ = "411222314"
ships = 20
''step 1 : create first generation
for i = 0 to 100
  for x = 1 to len( seed$ )
    if mid$( seed$ , x , 1 ) = "." then
      gen$( i ) = gen$( i ) + str$( int( rnd( 0 ) * 2 ) )
    else
      gen$( i ) = gen$( i ) + mid$( seed$ , x , 1 )
    end if
  next x
next i
done = 0
''let genes live
while not( done )
  ''step 2 : calculate fitnes of genes
  for i = 0 to 100
    sum = 0
    ''| fitnes
    for x = 0 to 8
      colom = 0
      for y = 0 to 8
        colom = colom + spot( gen$( i ) , x , y )
      next y
      sum = sum + abs( colom - val( mid$( ver$ , x + 1 , 1 ) ) )
    next x
    ''- fitnes
    for y = 0 to 8
      row = 0
      for x = 0 to 8
        row = row + spot( gen$( i ) , x , y )
      next x
      sum = sum + abs( row - val( mid$( hor$ , y + 1 , 1 ) ) )
    next y
    fitnes( i ) = sum
  next i
  ''step 3 : sort genes on there fitnes
  for h = 1 to 100
    for l = 0 to h - 1
      if fitnes( h ) < fitnes( l ) then
        hfit = fitnes( l )
        fitnes( l ) = fitnes( h )
        fitnes( h ) = hfit
        hgen$ = gen$( l )
        gen$( l ) = gen$( h )
        gen$( h ) = hgen$
      end if
    next l
  next h
  ''step 4 : crosover + mutate
  for i = 10 to 100
    a = int( rnd( 0 ) * 10 )
    b = int( rnd( 0 ) * 10 )
    new$ = ""
    for x = 1 to len( seed$ )
    ''crosover
      if rnd( 0 ) < .5 then
        n$ = mid$( gen$( a ) , x , 1 )
      else
        n$ = mid$( gen$( b ) , x , 1 )
      end if
      ''mutate
      if rnd( 0 ) < .1 and mid$( seed$ , x , 1 ) = "." then
        n$ = str$( int( rnd( 0 ) * 2 ) )
      end if
    next x
  next i
  ''step 5 : take care that living not lasts forever
  t = t + 1
  if not( t mod 10 ) then print t , fitnes( 0 )
  if t > 1000 or fitnes( 0 ) = 0 then done = not( false )
wend
''print results
for i = 0 to 9
  print mid$( gen$( 0 ) , i * 9 + 1 , 9 )
next i
end
function spot( a$ , x , y )
  spot = val( mid$( a$ , 1 + x + y * 9 , 1 ) )
end function
function setmid$( in$ , new$ , spot )
  l$ = left$( in$ , spot - 1 )
  r$ = right$( in$ , spot - len( new$ ) )
  setmid$ = l$ + new$ + r$
end function
bluatigro
 
Berichten: 306
Geregistreerd: za sep 27, 2008 6:01 pm

Keer terug naar Liberty BASIC Code

Wie is er online

Gebruikers op dit forum: Bing [Bot] en 1 gast

cron