HyperMu’NmGA

HyperMu’NmGA preview image

1 collaborator

Cosimo.leuci Cosimo Leuci (Author)

Tags

evolvability 

Tagged by Cosimo Leuci over 3 years ago

genetic algorithms 

Tagged by Cosimo Leuci over 3 years ago

hypermutation 

Tagged by Cosimo Leuci over 3 years ago

sex 

Tagged by Cosimo Leuci over 3 years ago

tumorigenesis 

Tagged by Cosimo Leuci over 3 years ago

Child of model Minimal Genetic Algorithm preview imageMinimal Genetic Algorithm
Visible to everyone | Changeable by the author
Model was written in NetLogo 6.2.0 • Viewed 400 times • Downloaded 22 times • Run 0 times
Download the 'HyperMu’NmGA' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Info tab cannot be displayed because of an encoding error

Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

;;  _______________________________________________________________________________________________________________
;;
;;  --------------------   HyperMu'NmGA   ---------------------------------------------------------   HyperMu'NmGA
;;  HyperMu'NmGA   ---------------------------------------------------------   HyperMu'NmGA   ---------------------
;;  _______________________________________________________________________________________________________________



globals     [best_result   ;; the numerical value of the chromosome  closer to the solution
             worst_result  ;; the numerical value of the chromosome more distant from the solution
             eureka-time   ;; the ticks elapsed to find the optimal solution
             eureka!       ;; binary variable turning on when the optimal solution has just reached
             reduction-time;; the ticks required to get a 30% reduction of the mutator genes values average
             donor         ;; the turtle having the best chromosome
             recipient     ;; a random turtle different from the donor
             chromcopy     ;; the copy of the chromosome carried by the donor turtle
             mutcopy       ;; the copy of the mutaror gene carried by the donor turtle
             mate_swcopy   ;; the copy of the mate_switch gene carried by the donor turtle
             mutator_swcopy;; the copy of the mutator_switch gene carried by the donor turtle
             max-mut.av    ;; the highest mutators average value recorded before the problem solution
             min-mut.av    ;; the lowest mutators average value recorded before the problem solution
             a-split       ;; the position of one extreme in a donor's chromosome fragment
             b-split       ;; the position of the second extreme  a in a donor's chromosome fragment
             counter       ;; a counter usefull in different blocks
             ]

turtles-own [chromosome    ;; a string of digits representing a candidate solution to the problem
             mutator       ;; a gene able to impact positively or negatively on the genetic stability of the
                           ;; chromosome and of itself; its strength is quantified by a whole number
             mutator_switch;; a gene able to determine the mutation mode;
             mate_switch   ;; a gene able to determine the reproduction mode;
                           ;; it harbours a binary variable: 0 for asexual reproduction, 1 for sexual reproduction
             fitness       ;; the numerical value of the chromosomal strings (if string-value fitness-function mode)
                           ;; or the Hamming distance complement (i.e. the proximity) to the best chromosomal
                           ;; string (if hamming fitness-function mode)
             ]



;;  ----------   SETUP PROCEDURES   -------------------------------------------------------------------------------
;;  ---------------------------------------------------------------------------------------------------------------

to setup                  ;; reset parameters and create a number of turtles set by the user
  clear-all
  set counter 0
  reset-ticks
  create-turtles turtles-number [
    set shape "circle 2"
    set size 2.3
    fd random 15
    genotype/fenotype-construction
   ]
  message0
  set eureka-time 0
end 

to genotype/fenotype-construction
  ;; the genotype (chromosome) and the corresponding phenotype (fitness) of the turtle are initially equal to the
  ;; first answer that is usually given by children to the problem: "what is the greatest number of n digits?"
  set fitness 10 ^ (genes-number - 1)
  ;; the initial formal fitness value is converted into a string: it will be the chromosome structure
  set chromosome word fitness ""
  set label chromosome    ;; chromosome is displayed as label

  ;; in the "hamming" mode, the fitness function is replaced by the complement of the Hamming distance
  ;; (i.e. the "Hamming proximity") to the optimal solution
  if fitness-function = "hamming" [compute-fitness]

  set mutator 1
  set max-mut.av 1
  set min-mut.av 1

  ;; defining mate_switch gene
  if reproduction = "asexual" [set mate_switch 0]
  if reproduction = "sexual" [set mate_switch 1]
  if reproduction = "sex/asex" [set mate_switch random 2]

  ;; defining mutator_switch gene
  if mutate_by = "simple mutagenesis" [set mutator_switch -1]
  if mutate_by = "additive mutator" [set mutator_switch 0]
  if mutate_by = "multiplicative mutator" [set mutator_switch 1]
  if mutate_by = "both mutators" [set mutator_switch random 2]
end 


;;  ----------   RUNTIME PROCEDURES   -----------------------------------------------------------------------------
;;  ---------------------------------------------------------------------------------------------------------------

to search
  ask turtles [move]
  ;; thew worse and the best fitness value are detected
  set worst_result min [fitness] of turtles
  set best_result max [fitness] of turtles

  ;; a message is sent to output when the optimal solution is reached  (the solutions are differently detected
  ;; depending on the chosen fitness function)
  if eureka-time = 0 and (best_result = 10 ^ genes-number - 1 or best_result = genes-number) [
      set eureka-time ticks
      set eureka! 1
      message1
    if mutate_by = "simple mutagenesis" [stop]
    ]
  ;; max-mut.av and min-mut.av are recorded before the optimal solution is reached
  if eureka-time = 0  [
    if max-mut.av < mean [mutator] of turtles [set max-mut.av  mean [mutator] of turtles]
    if min-mut.av > mean [mutator] of turtles [set min-mut.av  mean [mutator] of turtles]
    ]
  ;; other STOP conditions
  if eureka-time > 0 [set reduction-time ticks - eureka-time]
  if eureka-time > 0 and mean [mutator] of turtles < 0.7 [message2 stop]
  if eureka-time = 0 and ticks >= 750000 and max [mutator] of turtles < 1 [message3 stop]
  if eureka-time > 0 and min [mutator] of turtles >= 1.5 and reduction-time > 500000  [message4 stop]

  ;; two alternative kinds of selective reproduction are admitted: sexual and asexual
  ;; they take place if the diversity between all chromosomes is not null
  clear-links
  if best_result != worst_result [
    set donor [who] of one-of turtles with [fitness = best_result]
    set chromcopy [chromosome] of turtle donor
    set mutcopy [mutator] of turtle donor
    set mate_swcopy [mate_switch] of turtle donor
    set mutator_swcopy [mutator_switch] of turtle donor
    set recipient [who] of one-of turtles with [fitness != best_result]
    ask turtle donor [
      create-link-to turtle recipient
      if mate_switch = 0 [cloning]
      if mate_switch = 1 [recombination]
    ]
  ]
  ;; mutations occur randomly, the frequency is related to the basic mutation-rate (the mutagenicity
  ;; of the environment) and the total gene number of turtles population
  if random-float 1 < basic_mutation-rate * turtles-number * (genes-number + 3)
     [ask one-of turtles [mutation]
  ]
  tick
  set eureka! 0
end 

;; TALK TO ME! ---------------------------------------------------------------------------------------------------

to message0
  output-print "*** HyperMu'NmGA ***"
  output-print " "
end 

to message1
  output-print " OPTIMAL SOLUTION"
  output-print " reached on "
  output-print word " tick: " eureka-time
end 

to message2
  output-print " "

  output-print " Mutators mean went"
  output-print " down 30% under the"
  output-print  " starting value after"
  output-type " ticks: "
  output-type reduction-time
end 

to message3
  output-print " "
  output-print " Low evolutive"
  output-print " potential"
end 

to message4
  output-print " "
  output-print " No mutator < 1.5"
  output-print " possible endless"
  output-print " hypermutation status"
end 

;; GOOD VIBRATIONS -----------------------------------------------------------------------------------------------

to-report  patches-ahead [rad dis] ; reports to turtles a set of patches ahead
  report [patches in-radius rad] of patch-ahead dis
end 

to move
   ifelse (any? other turtles-on patches-ahead 1 1)
    [ bk 1 lt random-float 360]
    [fd 0.001]
end 


;; Operator 1.  FITNESS FUNCTIONS
;; ----------------------------------------------------------------------------------------------------------------

to compute-fitness
  set fitness 0
  set counter 0
  if fitness-function = "hamming" [hamming-proximity]
  if fitness-function = "string-value" [
    set fitness read-from-string chromosome]
  set label chromosome
end 

to hamming-proximity
  if counter = length chromosome [set counter 0 stop]
  if item counter chromosome = "9" [
    set fitness fitness + 1
    ]
  set counter counter + 1
  hamming-proximity
end 


;; Operator 2.  REPRODUCTION
;; ----------------------------------------------------------------------------------------------------------------

to cloning
  ask turtle recipient [
    set chromosome chromcopy
    set mutator mutcopy
    set mutator_switch mutator_swcopy
    set mate_switch mate_swcopy
    compute-fitness
  ]
end 

to recombination
  ;; homologous recombination occurs between the chromosome of a randomly chosen turtle (recipient) and the
  ;; chromosome of (one of) the most performing one (donor) that offers a code's fragment of its chromosome
  ;; to the first turtle; the two involved turtles will be highlighted by a link

  set counter 0
  set a-split random genes-number
  set b-split random genes-number
  ask turtle recipient [
    hybridization
    compute-fitness]
end 

to hybridization
  ;; the two selected chromosomal strings give place to hybridization according to a mechanism
  ;; similar to the crossing-over following to bacterial conjugation; strings are looped,
  ;; as occur usually in bacterial chromosomes or plasmids
  ifelse a-split < b-split [set chromosome
            replace-item (a-split + counter) chromosome (item (a-split + counter) chromcopy)
            set counter (counter + 1)
            if counter < b-split - a-split [hybridization]]
       [if b-split < a-split [set chromosome
              replace-item ((a-split + counter) mod genes-number)
                  chromosome (item ((a-split + counter) mod genes-number) chromcopy)
              set counter (counter + 1)
              if counter < genes-number - a-split + b-split [hybridization]]
    ]
  ;; if required, the three donor's regulative genes are transferred into the recipient turtle
  if reproduction = "sex/asex" [set mate_switch 1]
  if mutate_by != "simple mutagenesis" [
    if random-float 1 < hym-ratio [set mutator mutcopy]
    if mutate_by = "both mutators" [if random-float 1 < 0.5 [
      set mutator_switch mutator_swcopy]
    ]
  ]
end 


;; Operator 3: MUTAGENESIS
;; ----------------------------------------------------------------------------------------------------------------

to mutation
  ;; simple mutagenesis doesn't involve mutator genes
  ifelse mutate_by = "simple mutagenesis" [point-mutation compute-fitness
    if reproduction = "sex/asex" [ ;; mutation of the reproduction mode
      if random-float 1 < 0.5
       [set mate_switch ((mate_switch + 1) mod 2)]
    ]
  ]
  ;; hypermutation can produce a mutation of a structural gene as well as a mutation on the mutator gene;
  ;; the last one determines the mutation number along the chromosome: it can be 0, 1 or more (multimutation)
  [if random-float 1 > hym-ratio [multimutation compute-fitness]
   if random-float 1 < hym-ratio [hypermutation]
   ;; when the population is reproductively heterogeneous, also the mate_switch gene can mutate
   if reproduction = "sex/asex" [
     if random-float 1 * mutator * mu-expressivity > hym-ratio [
       set mate_switch ((mate_switch + 1) mod 2)]  ;; mutation of the reproduction mode
    ]
   ;; when the population is mutationally heterogeneous, also the mutator_switch gene can mutate
   if mutate_by = "both mutators" [
    if random-float 1 * mutator * mu-expressivity > hym-ratio [
      set mutator_switch ((mutator_switch + 1) mod 2)]  ;; mutation of the mutator mode
    ]
  ]
end 

to point-mutation
  ;; point-mutations hit randomly only one gene
  set chromosome replace-item random genes-number chromosome word random 10 ""
end 

to multimutation
  ;; the multimutations imply different point-mutations in a unique mutation event
  ;;  the number of contemporary point-mutations is determined by the variable stored in the mutator-gene
  ifelse mutator < genes-number * 4
    [repeat mutator [point-mutation]]
    ;; if the mutator value is too high (more than four times the number of the chromosome genes) in order to avoid
    ;; overflow events, the number of point-mutations is cut to four times the number of chromosome genes
    [repeat genes-number * 4 [point-mutation]
  ]
end 

to hypermutation
  ;; mutator gene can mutate itself  in two alternative ways (additive or multiplicative, decided by
  ;; mutator_switch gene), the self-mutation effect can be modulated also by the mutator-expressivity parameter
  set mutator mutator + (random 3 - 1) * mutator ^ mutator_switch * mu-expressivity
end 

There are 7 versions of this model.

Uploaded by When Description Download
Cosimo Leuci over 1 year ago Rev. 1.2.1 Download this version
Cosimo Leuci over 1 year ago Rev. 1.2.0 Download this version
Cosimo Leuci over 1 year ago Rev. 1.0.3 Download this version
Cosimo Leuci over 1 year ago Rev. 1.0.2 Download this version
Cosimo Leuci over 2 years ago Rev. 1.0.1 Download this version
Cosimo Leuci over 3 years ago Rev. 1.0.0 Download this version
Cosimo Leuci over 3 years ago Rev. 1.0 Download this version

Attached files

File Type Description Last updated
HyperMu’NmGA.png preview HyperMu-Reflection over 3 years ago, by Cosimo Leuci Download
READ_ME.txt data attachment 5 months ago, by Cosimo Leuci Download

Parent: Minimal Genetic Algorithm

This model does not have any descendants.

Graph of models related to 'HyperMu’NmGA'