Fractalmorphs

Fractalmorph is a NetLogo model that illustrates the principle of evolution by artificial selection. It is inspired by the Biomorphs of Richard Dawkins', "The Blind Watchmaker".

powered by NetLogo

view/download model file: FractalMorph.nlogo

 

HOW IT WORKS

After setup, you are presented with nine simple figures, based either on a branching structure, or fractal structure related to the Sierpinski gasket. These are the Fractalmorphs whose structure is determined by eight different variables or "genes". Each Fractalmorph differs from the central one by a single mutation in one of the genes. The user selects their favorite Fractalmorph and this becomes the new central morph in the next generation, with slightly mutated children surrounding it. As the user continues to select morphs, the shapes gradually change to new forms.

 

HOW TO USE IT

Choose a morph type (either branch or gasket), then click setup and then go. Keep selecting your favorite morph. The morphs are initially very simple, but as you continue selecting you will see that they can quickly become more complex. You have some scope to preselect the starting genes of your morphs also, although since the space of all morphs is huge, it is more satisfying exploring this space through evolution by artificial selection then by randomly picking values for genes.

 

THINGS TO NOTICE

After playing around for a while you should begin to notice how the different genes affect the morphs. For example, the top left morph is the one that has the "level" gene mutated. A higher value for this gene results in a more complex morph. By continuously selecting morphs in a particular location it is possible to mutate just one gene and see how it affects the form of the morphs.

 

THINGS TO TRY

If you don't like any of your choices select the central morph again. This has the effect of mutating the genes again. Some of the morphs will then look different. Also, try out the preselected morphs to see some interesting shapes. Try switching between the two different morph types. The gene names are the same, but they are used differently by the different procedures. What looks visually appealing for one type of morph may not look as good for the other type.

 

EXTENDING THE MODEL

It is possible to create more complicated morphs by adding more genes to the existing morph types. You would then need either create more seeds to show all the mutations, or keep the eight mutated morphs and randomly select which genes to morph. You might also invent a different morph type based on some other kind of fractal structure.

 

NETLOGO FEATURES

This model was created as a way to illustrate the use of lists in NetLogo. It also demonstrates one way to use multiple turtles to create fractals.

 

RELATED MODELS

See other Evolution based models in this series

 

CREDITS AND REFERENCES

Copyright 2006 David McAvity

This model was created at the Evergreen State College, in Olympia Washington
as part of a series of applets to illustrate principles in physics and biology.

Funding was provided by the Plato Royalty Grant.

The model may be freely used, modified and redistributed provided this copyright is included and the resulting models are not used for profit.

Contact David McAvity at mcavityd@evergreen.edu if you have questions about its use.

 

PROCEDURES

;Fractalmorphs is a model that illustrates the principle of evolution by artificial selsection. 
; It is inspired by the Biomorphs of Richard Dawkins', "Blind Watchmaker". 
globals [   
        mutation-rate  
        fittest   
        ]
 
breed [ seeds ]
breed [ branches ]
breed [ squares ]
 
turtles-own [
        level
        pen-factor
        edge
        edge-factor
        left-angle
        right-angle
        langle-factor
        rangle-factor  ]        
seeds-own [ genome  ]
branches-own [ right? ]
        
 
    
to setup
     ca
     set-default-shape squares "square"
; The mutation-rate is a list that determines by how much each gene mutates in each step. 
; The gene sequence is level pen-factor edge edge-factor left-angle right-angle langle-factor rangle-factor    
     
     set mutation-rate [1 0.05 10 0.2  5 5 0.1 0.1] 
     
; Now assign the initial "genetic code" to nine "seed" turtles. The genetic code consists of 
; all the variables associated with creating different two different types of fractalmorphs (branching
; sierpinski gasket like fractals). The seeds are then mutated, planted around the screen and then grown
; according to the type of fractal chosen on the interface.
 
     create-seeds 9 [ 
         set heading 0
         set color red
         set level 2 + random 5
         set pen-factor 1.0
         set edge 10 + random 10
         set edge-factor 0.6 + random-float 0.7
         set left-angle random 360
         set right-angle random 360
         set langle-factor 0.5 + random-float 3
         set rangle-factor 0.5 + random-float 3
         set genome (list level pen-factor edge edge-factor left-angle right-angle langle-factor rangle-factor) ]  
     mutate
     assign-genes
     plant-seeds
     if morph-type = "Branch" [branch]
     if morph-type = "Gasket" [gasket]                  
end
 
; When this procedure is running it is possible to select one of the Fractalmorphs with the mouse. The
; genome of the "fittest" Fractalmorph is assigned to all the seeds, then the seeds are mutated as in the 
; setup procedure.
to go
    if mouse-down? [ 
            set fittest min-one-of seeds [distancexy mouse-xcor mouse-ycor]
            ask seeds [set genome [genome] of fittest 
            set pen-size [pen-size] of fittest]
            clear-drawing
            mutate
            assign-genes
            if morph-type = "Branch" [branch]
            if morph-type = "Gasket" [gasket]
            wait 0.1 ]
end
 
; Place "seed" turtles evenly around the screen, with turtle 0 at the center.
to plant-seeds
  ask turtle 0 [  setxy  ( 2 * max-pxcor / (- 3)) (2 * max-pycor / 3) ]
  ask turtle 1 [  setxy  0 (2 * max-pycor / 3) ]
  ask turtle 2 [  setxy  ( 2 * max-pxcor / 3) (2 * max-pycor / 3) ]
  ask turtle 3 [  setxy  ( 2 * max-pxcor / (- 3)) 0 ]
  ask turtle 4 [  setxy  ( 2 * max-pxcor / 3) 0 ]
  ask turtle 5 [  setxy  ( 2 * max-pxcor / (- 3)) (2 * max-pycor / (- 3)) ]
  ask turtle 6 [  setxy  0 (2 * max-pycor / (- 3)) ]
  ask turtle 7 [  setxy  ( 2 * max-pxcor / 3) (2 * max-pycor / (- 3)) ]
end
 
; Each seed, with the exception of the turtle at the center of the screen (the 9th seed), 
; has the gene gene corresponding to ts who number in the genome list mutated by adding
; or subracting the value of the mutation rate from the corresponding gene in the genome. 
; (Note: We only subtract if the result is a postive value for the gene.)
to mutate
  ask seeds with [who < 8 ][
      let i who
      ifelse (random 2) = 1
      [ set  genome replace-item i genome ( (item i genome) + (item i mutation-rate) )]
      [ if (item i genome) > (item i mutation-rate)
              [  set  genome replace-item i genome ( (item i genome) - (item i mutation-rate) ) ] ] ]
end
 
; Update the genes after mutation
to assign-genes
    ask seeds [    
      set level item 0 genome
      set pen-factor item 1 genome
      set edge item 2 genome
      set edge-factor item 3 genome
      set left-angle item 4 genome
      set right-angle item 5 genome
      set langle-factor item 6 genome
      set rangle-factor item 7 genome   ]
end    
 
; These are predetermined genetic codes where the Fractal-Morphs have a particular shape. 
to choose-creature
   set morph-type "Branch"
   ask seeds [
       if creature = "spider" [set genome [6 1.1 20 0.8 40 52 2.18 1.93] ]
       if creature = "fly" [ set genome  [6 1.1 10 0.9 37 49 0.9 2.1] ] 
       if creature = "flowers" [ set genome [8 0.9 20 0.8 46 44 2.49 2.51]  ]
       if creature = "moth" [ set genome [6 1 30 0.8 108 36 1.2 0.9] ]]
       clear-drawing
       mutate
       assign-genes
       branch
end
 
; This is the main branching procedure. It is based on a simple dichotomous tree. Each seed produces 
; two branches at a particular angle. Each of these produces branches in turn, repeating the process 
; a number of times determined by a gene called "level". The length of the branches (edge) changes
; at each level of branching by a particular factor (edge-factor). The left and right branching angles
; also change at each level by factors langle-factor and rangle-factor. To preserve bilateral symmetry 
; the main right branch is a mirror image of the main left branch. The pen-size variable is changed
; by a pen-factor gene.  Color is not changed by a separate gene, but is instead linked to the angle genes.
; Two important points. After producing new branches, old branches must die, otherwise they will 
; keep branching and grow out of control. Also, without-interruption is crucial here. Without it seeds would
; be asking the branches of other seeds to branch.
 
to branch 
 without-interruption [
     ask seeds [
         pendown
         hatch-branches 1 [rt right-angle
                           set right? true]
         hatch-branches 1 [lt right-angle 
                           set right? false]
         let n  level
         repeat level [ set n n - 1       
                        ask branches [
                               set edge edge * edge-factor 
                               set right-angle right-angle * rangle-factor
                               set left-angle left-angle * langle-factor
                               set pen-size pen-size * pen-factor
                               fd edge                         
                               ifelse right? [
                               hatch 1 [rt right-angle           
                                        set color right-angle * n]
                               hatch 1 [lt left-angle
                                        set color left-angle * n] 
                                        ]
                                         [
                               hatch 1 [lt right-angle           
                                        set color right-angle * n]
                               hatch 1 [rt left-angle
                                        set color left-angle * n] 
                                       ]  
                               die
                         ]
                     ]
           ask branches [die]  
        ]
      ]         
end
 
; This procedure is based on the Sierpinski Gasket type of fractal. The Sierpinski Gasket is
; created by repeated iteration of the following step: Given a square, replace it with three  
; squares that are reduced in size by a factor of a half and arranged in a trianglular shape 
; with two on the base and one on the top. When this step is repeated with the smaller squares
; the Sierpinski gastket is created. Different fractals can be created by rotating the squares
; by particular angles. In this procedure the elements (squares) of the fractals "branches" so 
; that the same procedures can be used, but really they do not branch. To create the fractal,
; the seed produces a new square which then is reduced in size by a factor. It then makes two 
; copies of itself, one which moves down, turns left moves forward a step and then to the right.
; The other does the mirror images of this manouever. The original square then moves forward a step.
; This then creates the desired triangular arrangment. 
; The process is repeat a number of times determined by the gene "level".
; The steps, rotating angles and the scale factors by which they changed are determined by genes.
; Color is linked to the angle gene, but is not a gene itself.
 
to gasket  
      clear-drawing
      ask seeds [   
          pen-up
          set size edge * 5
          set hidden? true ]
      without-interruption [
          ask seeds [
                hatch-squares 1 []
                let n level - 2
                repeat (level - 1) [ 
                      ask squares [
                          set size size * (edge-factor * 0.5)
                          set right-angle right-angle * rangle-factor
                          set left-angle left-angle * langle-factor
                          let step size * (edge-factor * 0.5)
                          let angle pen-factor * 90
                          hatch 1 [ 
                              bk step lt left-angle fd step rt right-angle  
                              set color  1.2 * left-angle * n
                              rt angle  ]
                          hatch 1 [ bk step rt left-angle fd step lt right-angle  
                              set color  1.2 * left-angle * n 
                              lt angle]  
                          fd step   ]  ]  
                      ask squares [stamp die]  
                      set n n - 1]  ]
                     
end
 
;;  ***  Copyright notice  ***
;;
;;   Copyright 2006 David McAvity
;;
;; This model was created at the Evergeen State College, in Olympia Washington
;; as part of a series of applets to illustrate principles in physics and biology. Funding was provided
;; the Plato Royalty Grant
;; 
;; The model may be freely used, modified and redistribued provided this copyright is included and
;; it not used for profit.
;;
;; Contact David McAvity at mcavityd@evergreen.edu if you have questions about its use.