Flibs'NLogo

Flibs'NLogo preview image

1 collaborator

Cosimo.leuci Cosimo Leuci (Author)

Tags

adaptive cognition 

Tagged by Cosimo Leuci about 6 years ago

artificial life 

Tagged by Cosimo Leuci about 6 years ago

finite automata 

Tagged by Cosimo Leuci about 6 years ago

genetic algorithms 

Tagged by Cosimo Leuci about 6 years ago

structural-coupling 

Tagged by Cosimo Leuci almost 6 years ago

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

;;  ----------   FLIBS'NLOGO   ----------------------------------------------------------------------------------
;;  -------------------------------------------------------------------------------------------------------------



breed   [flibs flib]     ;; FLiBs (finite living blobs) are the agents of the model: they are structured as
                         ;; finite automata (1, 2). They have "a finite number of states; an input signal
                         ;; causes it to change automatically from one state to another.
                         ;; The kind of automaton used in a flib also generates signals. Incoming and outgoing
                         ;; signals are represented within the automaton by symbols. When a signal is received,
                         ;; the automaton changes its state and emits a second signal" (2). A flib is considered
                         ;; perfect predictor i.e. very well adapted when its outgoing signals (previsions)
                         ;; is equal to the next incoming environmental signal. Signals are always binary digits.


flibs-own [chromosome    ;; every flib owns a chromosome that is a string coding its state transition table;
           state         ;; the current state of the flib
           fitness]      ;; a measure of its forestalling ability


globals [counter         ;; a counter useful in different procedures
         new-input       ;; the incoming environmental signal
         average         ;; mean of flibs population performances
         best            ;; the best flibs performance value
         worst           ;; the worst flibs performance value
         donor           ;; one very performing flib sharing part of its genes
         recipient       ;; one flib acquiring genes from a donor
         a-split         ;; a first cut in the recipient's chromosome
         b-split         ;; a second cut in the recipient's chromosome
         wild-type       ;; a natural state chromosome
         optimal]        ;; the flib's chromosome showing a perfect prevision ability



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

to setup                 ;; initializing the model
  clear-all
  reset-ticks
  set counter 0
  create-flibs num-flibs [
    set shape "face happy"
    set size 1.8
    set chromosome ""
    set label fitness
    fd 2 + random 7]
  ask flibs [chromosome-genesis]
end 

to chromosome-genesis   ;; chromosomes are strings randomly built through an iterative procedure: even string's
                        ;; positions are 0 or 1: they represent a possible outgoing signal; odd string's
                        ;; positions represent possible flib's states determined by NUM-STATES slider
  set chromosome word chromosome (word (random 2) (random num-states))
  set counter counter + 1
  ifelse counter < num-states * 2 [chromosome-genesis]
    [set counter 0]
end 



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

to search
  ;; testing flibs' prevision ability along 100 cycles and results are analyzed
  set counter 0
  ask flibs [set fitness 0 set state 0]
  ask flibs [chrom-test]
  performances-analysis

  ;; a new generation begins
  tick
  ask flibs [move]
  output-print ""
  output-print word "tick: " ticks

  ;; if flibs population harbours a perfect predictor, the goal of the genetic algorithm is reached
  if best = 100 [final-message stop]

  ;; if flibs population doesn't harbour any perfect predictor, genetic algorithm activates two operators:
  ;; genetic shuffling (as a consequence of mating processes) and mutagenesis

  ;; every generation one recombination event occurs by default, this frequency can be
  ;; lowered by the slider "MATE-RATE"
  ifelse random-float 1 < mate-rate [conjugation]
    [output-print "no mating event"]

  ;; every generation, one mutagenesis process occurs  by default, this frequency can be
  ;; lowered by the slider MUTATION-RATE
  ifelse random-float 1 < mutation-rate
    [set wild-type [who] of one-of flibs ask flib wild-type [mutate]]
    [output-print "no mutation event"]
end 

to final-message
  output-print "        EUREKA!!!"
  set optimal [who] of one-of flibs with [fitness = 100]
  ifelse best > 9  [output-type word "id: " optimal] [output-type word "id: 0" optimal]
  output-type word " optimal predictor:         " [chromosome] of flib optimal
  output-print word "     fitness: " [fitness] of flib optimal
  output-print ""
  output-print "--- no genetic variation (look at the previous generation) ---"
  output-print ""
end 

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 0.1 lt random-float 360]
    [fd 0.01]
end 


;; Operator 1: FITNESS EVALUATION
;; --------------------------------------------------------------------------------------------------------------

to chrom-test
  ;; an environmental input symbol is read from the given sequence
  set new-input read-from-string item (counter mod (length environmental-cycle)) environmental-cycle
  ;; only 0 or 1 symbols are accepted
  if new-input != 0 and new-input != 1 [
    show "error: invalid environmental value"
    stop]
  ;; each flibs prevision ability is tested and fitness is updated
  if read-from-string  item (4 * state + 2 * new-input) chromosome =
        read-from-string item ((counter + 1) mod (length environmental-cycle)) environmental-cycle
            [set fitness (fitness + 1)]
  ;; new flibs state is computed
  set state read-from-string item (4 * state + 2 * new-input + 1) chromosome
  set counter counter + 1
  ;; the cycle is repeated 100 times
  if counter < 100 [chrom-test]
  set counter 0
end 

to performances-analysis
  ask flibs [
  set average mean [fitness] of flibs
  set best max [fitness] of flibs
  set worst min [fitness] of flibs
  set label fitness
  ;; flibs performances are visually represented through emoticons language
  ifelse fitness < 55
    [set shape "face sad"] [ifelse fitness < 85
    [set shape "face neutral"]
    [set shape "face happy"]]
  ]
end 


;; Operator 2: RECOMBINATION
;; --------------------------------------------------------------------------------------------------------------

to conjugation
  clear-links
  ;; the conjugation process requires two flibs' chromosomes: the donor and the recipient
  ;; just the second one undergoes to recombination
  select-flibs
  ;; a link highlighted the two mating flibs
  ask flib donor [create-link-to flib recipient]
  ask flib recipient [recombination]
end 

to select-flibs
  set donor [who] of one-of flibs with [fitness = best]
  set recipient [who] of one-of flibs
  if donor = recipient [select-flibs]     ;; self conjugation is forbidden
  ;; generating report about the selection process
  ifelse donor > 9  [output-type word "id: " donor] [output-type word "id: 0" donor]
  output-type word " donor chromosome:      " [chromosome] of flib donor
  output-print word "     fitness: " [fitness] of flib donor
  ifelse recipient > 9  [output-type word "id: " recipient]  [output-type word "id: 0" recipient]
  output-type word " recipient chromosome:  " [chromosome] of flib recipient
  output-print word "     fitness: " [fitness] of flib recipient
end 

to recombination
  ;; a gene's sequence included between a-split and b-split restriction sites is randomly chosen
  set a-split random (num-states * 4)
  set b-split random (num-states * 4)
  if a-split = b-split [recombination]
  set counter 0
  hybridization
  ;; generating a report about genetic shuffling
  ifelse recipient > 9  [output-type word "id: " recipient]  [output-type word "id: 0" recipient]
  output-print word " hybridized chromosome: " chromosome
  output-type "- flanking positions of replaced fragment:  [ "
  output-type word a-split " - "
  output-print word b-split " [ "
end 

to hybridization
 ;; the genes' sequence included between a-split and b-split restriction sites on a random
 ;; flib is replaced by the homologous sequence of one of the most performing flibs;
 ;; chromosomes are treated as circle-shaped
 if a-split < b-split [
    set chromosome replace-item (a-split + counter)
    chromosome (item (a-split + counter) [chromosome] of flib donor)
    set counter (counter + 1) if counter < b-split - a-split
    [hybridization]]
 if a-split > b-split [
    set chromosome replace-item ((a-split + counter) mod (num-states * 4))
    chromosome (item ((a-split + counter) mod (num-states * 4)) [chromosome] of flib donor)
    set counter (counter + 1) if counter < (num-states * 4) - (a-split - b-split)
    [hybridization]]
  set counter 0
end 


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

to mutate
  ;; managing mutation and generating a report
  ifelse wild-type > 9  [output-type word "id: " wild-type]  [output-type word "id: 0" wild-type]
  output-type word " wild-type chromosome:  " [chromosome] of flib wild-type
  output-print word "     fitness: " [fitness] of flib wild-type
  ;; mutations occur randomly at a given frequency on just one locus
  let dice random length chromosome
  let muton read-from-string item dice chromosome
  ifelse dice mod 2 = 0
    [set chromosome replace-item dice chromosome word ((muton + 1) mod 2) ""]
    [set chromosome replace-item dice chromosome word ((muton + 1) mod num-states) ""]
  ifelse wild-type > 9  [output-type word "id: " wild-type] [output-type word "id: 0" wild-type]
  output-print word " mutant chromosome:     " [chromosome] of flib wild-type
end 

There are 8 versions of this model.

Uploaded by When Description Download
Cosimo Leuci over 4 years ago Rev. 1.0.1 Download this version
Cosimo Leuci almost 5 years ago Rev. 1.0.0 Download this version
Cosimo Leuci about 6 years ago Rev. 0.9.8 Download this version
Cosimo Leuci about 6 years ago Rev. 0.9.7 Download this version
Cosimo Leuci about 6 years ago Rev. 0.9.5 Download this version
Cosimo Leuci about 6 years ago Rev. 0.9.4 Download this version
Cosimo Leuci about 6 years ago Rev. 0.9 Download this version
Cosimo Leuci about 6 years ago Initial upload Download this version

Attached files

File Type Description Last updated
Flibs'NLogo.png preview Preview for "Flibs'NLogo" about 6 years ago, by Cosimo Leuci Download
READ_ME.txt data attachment about 1 year ago, by Cosimo Leuci Download