Minimal Genetic Algorithm

Minimal Genetic Algorithm preview image

1 collaborator

F1 Cosimo Leuci (Author)

Tags

arithmetic 

Tagged by Cosimo Leuci 8 months ago

genetic algorithms 

Tagged by Cosimo Leuci 8 months ago

Part of project 'Starfish_Planet' Parent of 1 model: Flibs'NLogo preview imageFlibs'NLogo
Visible to everyone | Changeable by the author
Model was written in NetLogo 6.0.4 • Viewed 317 times • Downloaded 4 times • Run 0 times
Download the 'Minimal Genetic Algorithm' 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

turtles-own [chromosome  ;; a string of digits representing a possible answer to the problem
             fitness     ;; the numerical value of the chromosomal string
             ]

globals [best            ;; the numerical value of the chromosome  closer to the goal
         worst           ;; the numerical value of the chromosome more distant from the goal
         donor           ;; the agent having the best chromosome
         recipient       ;; an agent not having the best chromosome
         chromcopy       ;; the copy of the best chromosome
         a-split         ;; the position of one extreme of a chromosome's fragment
         b-split         ;; the position of the second extreme of a chromosome's fragment
         counter         ;; a conter usefull in different blocks
         ]



;;  SETUP PROCEDURES
;;  _________________________________________________________________________________________________

to setup                 ;; reset parameters and create a number of agents given by the user
  clear-all
  set counter 0
  reset-ticks
  create-turtles turtles-number [
    set shape "circle 2"
    set size 1.8
    fd 7
    genotype/fenotype-construction
   ]
end 

to genotype/fenotype-construction
  ;; the genotype (chromosome) and the corresponding fenotype (fitness) of the agent
  ;; is initially equal to the worst response to the problem; it is shown as label
  set fitness 10 ^ (genes-number - 1)
  ;; lowest fitness value is converted into string:
  ;; it will be the initial chromosome structure
  set chromosome word fitness ""
  ;; chromosome is displayed as label
  set label chromosome
end 



;;  RUNTIME PROCEDURES
;;  _________________________________________________________________________________________________

to search
  ;; worse and best fitness value are detected
  set worst min [fitness] of turtles
  set best max [fitness] of turtles
  ;; the problem is resolved when an agent gain the higher value of fitness having a user
  ;; specified number of digits
  if best = 10 ^ genes-number - 1 [show "DONE!!!" stop]
  ;; when  diversity between chromosomes is null, genetic-shuffling is skipped
  if best != worst [genetic-shuffling]
  if random-float 1 < mutation-rate [mutate]
  tick
end 

to genetic-shuffling
  ;; hybridization occur between the chromosome of a randomly choosen turtle (recipient)
  ;; and the chromosome of the most performing one (donor) that offer a fragment of its
  ;; chromosome to first turtle; the two involved agents are highlighted by a link
  clear-links
  set donor [who] of one-of turtles with [fitness = best]
  set recipient [who] of one-of turtles with [fitness != best]
  ask turtle donor [create-link-to turtle recipient ]
  set counter 0
  set a-split random genes-number
  set b-split random genes-number
  set chromcopy [chromosome] of turtle donor
  ask turtle recipient [
     hybridization
     set fitness read-from-string chromosome
     set label fitness
    ]
end 

to hybridization
  ;; the two selected chromosomosomal strings give place to hybridization with a mechanism inspired both
  ;; to crossing-over and bacterial conjugation: the recipient string will be hybridized with a fragment
  ;; of the donor one; strings are treated as circular, as occur 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 length chromosome != genes-number [show "Invalid chromosome length"]
end 

to mutate
  ;; mutations happen randomly with a given frequency on just one digit place
  ask turtle random turtles-number
       [set chromosome replace-item random genes-number chromosome word random 10 ""
        set fitness read-from-string chromosome
        set label fitness
        if length chromosome != genes-number [show "Invalid chromosome length"]
    ]
end 

There are 11 versions of this model.

Uploaded by When Description Download
Cosimo Leuci 7 months ago adjustments in Download this version
Cosimo Leuci 7 months ago user interface update Download this version
Cosimo Leuci 7 months ago simplifications Download this version
Cosimo Leuci 7 months ago simplifications Download this version
Cosimo Leuci 7 months ago flux bug fixed Download this version
Cosimo Leuci 8 months ago Info tab new revision Download this version
Cosimo Leuci 8 months ago Info tab new revision Download this version
Cosimo Leuci 8 months ago Info tab new revision Download this version
Cosimo Leuci 8 months ago user interface and info tab update Download this version
Cosimo Leuci 8 months ago Info tab revised Download this version
Cosimo Leuci 8 months ago Initial upload Download this version

Attached files

File Type Description Last updated
Minimal Genetic Algorithm.png preview Preview for 'Minimal Genetic Algorithm' 8 months ago, by Cosimo Leuci Download

This model does not have any ancestors.

Children:

Graph of models related to 'Minimal Genetic Algorithm'