DIM rabbits(100,2)
FOR i = 1 TO 100
FOR j = 1 TO 2
IF INT(RND * 2) = 1 THEN
rabbits(i,j) = "a"
ELSE
rabbits(i,j) = "b"
END IF
NEXT j
NEXT i
FOR gen = 1 TO 50
DIM fitness(100)
FOR i = 1 TO 100
fitness(i) = 0
FOR j = 1 TO 2
IF rabbits(i,j) = "a" THEN
fitness(i) = fitness(i) + 1
END IF
NEXT j
NEXT i
DIM parent1(2)
DIM parent2(2)
total_fitness = 0
FOR i = 1 TO 100
total_fitness = total_fitness + fitness(i)
NEXT i
FOR j = 1 TO 2
r = RND * total_fitness
count = 0
FOR i = 1 TO 100
count = count + fitness(i)
IF count >= r THEN
IF j = 1 THEN
parent1(j) = rabbits(i,j)
ELSE
parent2(j) = rabbits(i,j)
END IF
EXIT FOR
END IF
NEXT i
NEXT j
FOR i = 1 TO 100
FOR j = 1 TO 2
r = RND
IF r < 0.5 THEN
rabbits(i,j) = parent1(j)
ELSE
rabbits(i,j) = parent2(j)
END IF
NEXT j
r = RND
IF r < 0.1 THEN
gene = INT(RND * 2) + 1
IF rabbits(i,gene) = "a" THEN
rabbits(i,gene) = "b"
ELSE
rabbits(i,gene) = "a"
END IF
END IF
NEXT i
NEXT gen
FOR i = 1 TO 100
PRINT "Rabbit #" + STR$(i) + ": " + rabbits(i,1) + rabbits(i,2) + " (" + STR$(LEN(STRING$(INSTR(1, rabbits(i,1) + rabbits(i,2), "a") - 1, "1") + STRING$(INSTR(1, rabbits(i,1) + rabbits(i,2), "a") - 1, "2"))) + " fit genes"
NEXT i