'METHINKS IT IS LIKE A WEASEL by QWERKEY 15/8/19
'128 Males + 128 Females
'128*Birthrate Male Offspring + 128*Birthrate Female Offspring: Male Odd-recessive, Female Even-recessive
'A certain percentage of mutations in the offspring
'Only the most adapted 128 Male and 128 Female Offspring survive
'As Methinks2 (tidied up) but with immovable correct gene removed, mutation rate much reduced, and errors corrected
CONST True
= -1, False
= 0 CONST NoMates%
= 128, NoIssue%
= 141, JumpStart%
= 256, MutationRate!
= 2 / 1000 CONST Elite!
= 3 / 7, Kappa!
= 4, Prob0!
= 0.23, GenLimit%
= 30000 DIM Mates$
(NoMates%
- 1, 1), MatesDat%
(NoMates%
- 1, 1), IssueDat%
(NoIssue%
- 1, 1) DIM Mating%%
(NoMates%
- 1)
_TITLE "Methinks It Is Like A Weasel" Weasel$ = "METHINKS[IT[IS[LIKE[A[WEASEL"
Alphabet$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ["
'Initialise 1st Generation
Generation% = 1
FOR N%%
= 0 TO NoMates%
- 1 Mates$
(N%%
, R%%
) = MID$(Alphabet$
, INT(RND * 26) + 1, 1) Mates$
(N%%
, R%%
) = Mates$
(N%%
, R%%
) + MID$(Alphabet$
, INT(RND * 27) + 1, 1) MatesDat%(N%%, R%%) = 0
MatesDat%
(N%%
, R%%
) = MatesDat%
(N%%
, R%%
) + ABS(ASC(MID$(Weasel$
, M%%
, 1)) - ASC(MID$(Mates$
(N%%
, R%%
), M%%
, 1)))'Sort & Display 1st Generation
CALL Sort1
(MatesDat%
(), Mates$
())
'Cycle through generations:
REDIM Papa%%
(NoIssue%
- 1, 1) Dawkins%% = True
OneByOne%% = True
WHILE Dawkins%%
AND Generation%
< GenLimit%
'Display Current Generation
FOR N%%
= 0 TO NoMates%
- 1 W$ = Mates$(N%%, R%%) 'W$ gets modified by Spaced$()
_PRINTSTRING (4 + 250 + R%%
* 500, 15 + (N%%
- 64) * 11), Spaced$
(W$
) 'Find partners (monogamous, brother & sister not allowed to mate)
PartnersAvailable%% = False
PartnersAvailable%% = True
REDIM Mated%%
(NoMates%
- 1) FOR N%%
= 0 TO NoMates%
- 1 CanMate%% = False
NoTries% = 0
N1%%
= INT(RND * NoMates%
/ 2) NoTries% = NoTries% + 1
IF NOT Mated%%
(N1%%
) AND (Papa%%
(N%%
, 0) <> Papa%%
(N1%%
, 1) OR Generation%
= 1) THEN 'By this time, Papa%% has been re-ordered Mated%%(N1%%) = True
Mating%%(N%%) = N1%%
CanMate%% = True
CanMate%% = True
PartnersAvailable%% = False
'Produce Children (always 1 male & 1 female offspring at the same time)
REDIM Issue$
(NoIssue%
- 1, 1), Papa%%
(NoIssue%
- 1, 1) NoKidsLess1% = 0
N%% = 0
WHILE NoKidsLess1%
<= NoIssue%
- 1 IF RND <= (Prob0!
+ ((Prob0!
* N%%
* (1 - Kappa!
)) / ((NoMates%%
- 1) * Kappa!
))) THEN FOR R%%
= 0 TO 1 'Male/Female Offspring Issue$
(NoKidsLess1%
, R%%
) = MID$(Alphabet$
, INT(RND * 26) + 1, 1) Issue$
(NoKidsLess1%
, R%%
) = Issue$
(NoKidsLess1%
, R%%
) + MID$(Alphabet$
, INT(RND * 27) + 1, 1) Issue$
(NoKidsLess1%
, R%%
) = Issue$
(NoKidsLess1%
, R%%
) + MID$(Mates$
(N%%
, 0), M%%
, 1) 'From father Issue$
(NoKidsLess1%
, R%%
) = Issue$
(NoKidsLess1%
, R%%
) + MID$(Mates$
(Mating%%
(N%%
), 1), M%%
, 1) 'From mother IssueDat%(NoKidsLess1%, R%%) = 0
IssueDat%
(NoKidsLess1%
, R%%
) = IssueDat%
(NoKidsLess1%
, R%%
) + ABS(ASC(MID$(Weasel$
, M%%
, 1)) - ASC(MID$(Issue$
(NoKidsLess1%
, R%%
), M%%
, 1))) Papa%%(NoKidsLess1%, R%%) = N%%
NoKidsLess1% = NoKidsLess1% + 1
N%% = 0
N%% = N%% + 1
'Delay
'Display Childless Parents
FOR N%%
= 0 TO NoMates%
- 1 'N%% is the father Progeny%% = False
T% = 0 'T% is the child
IF Papa%%
(T%
, 0) = N%%
THEN Progeny%%
= True
T% = T% + 1
W$ = Mates$(N%%, 0) 'W$ gets modified by Spaced$() - don't actually need this substitution here, as updated below
N2%% = Mating%%(N%%)
W$ = Mates$(N2%%, 1) 'W$ gets modified by Spaced$() - don't actually need this substitution here, as updated below
_PRINTSTRING (4 + 250 + 500, 15 + (N2%%
- 64) * 11), Spaced$
(W$
) 'Use up keypresses & wait for keypress
SetAwhile%% = True
SetAwhile%% = False
SetAwhile%% = False
Dawkins%% = False
OneByOne%% = False
SetAwhile%% = False
OneByOne%% = True
Dawkins%% = False
' Order children & set new generation
CALL Sort2
(IssueDat%
(), Issue$
(), Papa%%
()) FOR N%%
= 0 TO NoMates%%
- 1 Mates$(N%%, R%%) = Issue$(N%%, R%%)
MatesDat%(N%%, R%%) = 0
MatesDat%
(N%%
, R%%
) = MatesDat%
(N%%
, R%%
) + ABS(ASC(MID$(Weasel$
, M%%
, 1)) - ASC(MID$(Mates$
(N%%
, R%%
), M%%
, 1))) Generation% = Generation% + 1
'IF NOT OneByOne%% THEN _DISPLAY
IF Generation%
= GenLimit%
THEN PRINT "Generation Limit Reached"
Spaced$ = X$
SUB Sort1
(Numbers%
(), Names$
()) Jump% = JumpStart%
Jump% = (Jump% - 1) \ 2
Finished% = False
Finished% = True
FOR Upper%
= 1 TO NoMates%
- Jump%
Lower% = Upper% + Jump%
IF Numbers%
(Upper%
- 1, R%%
) > Numbers%
(Lower%
- 1, R%%
) THEN SWAP Names$
(Upper%
- 1, R%%
), Names$
(Lower%
- 1, R%%
) SWAP Numbers%
(Upper%
- 1, R%%
), Numbers%
(Lower%
- 1, R%%
) Finished% = False
SUB Sort2
(Numbers%
(), Names$
(), WhosTheDaddy%%
()) Jump% = JumpStart%
Jump% = (Jump% - 1) \ 2
Finished% = False
Finished% = True
FOR Upper%
= 1 TO NoIssue%
- Jump%
Lower% = Upper% + Jump%
IF Numbers%
(Upper%
- 1, R%%
) > Numbers%
(Lower%
- 1, R%%
) THEN SWAP Names$
(Upper%
- 1, R%%
), Names$
(Lower%
- 1, R%%
) SWAP Numbers%
(Upper%
- 1, R%%
), Numbers%
(Lower%
- 1, R%%
) SWAP WhosTheDaddy%%
(Upper%
- 1, R%%
), WhosTheDaddy%%
(Lower%
- 1, R%%
) Finished% = False