Flibs'NLogo

Flibs'NLogo preview image

1 collaborator

F1 Cosimo Leuci (Author)

Tags

adaptive cognition 

Tagged by Cosimo Leuci 6 months ago

artificial life 

Tagged by Cosimo Leuci 7 months ago

finite automata 

Tagged by Cosimo Leuci 7 months ago

genetic algorithms 

Tagged by Cosimo Leuci 7 months ago

structural-coupling 

Tagged by Cosimo Leuci 4 months 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 everyone
Model was written in NetLogo 6.0.4 • Viewed 364 times • Downloaded 2 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

breed   [flibs flib]     ;; FLiBs (finite livig blobs) are the agents of the model: they are structured
                         ;; as finite automata. 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 reiceved,
                         ;; the automaton changes state and emits a second signal" (1). 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 conter useful in different blocks
         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           ;; a performing flib sharing part of its genes
         recipient       ;; a flib acquiring genes from a donor
         a-split         ;; a first cut in the flib's chromosome
         b-split         ;; a second cut in the flib's chromosome
         wild            ;; 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 state word 0 ""
    set label fitness
    chromosome-genesis
    fd 2 + random 7]
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
  ;; checking chromosome dimension
  ask flibs [if length chromosome != num-states * 4
    [show "error: invalid chromosome lenght" stop]]

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

  ;; a new generation begins
  tick
  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 consequence of mating processes) and mutagenesis

  ;; every generation one chromosomic genetic shuffling occurs by default, this frequency can be
  ;; lowered by slider "mate-rate"
  ifelse random-float 1 < mate-rate [mate]
    [output-print "no mating event"]

  ;; in every generation one mutagenesis process occurs  by default, this frequency can be
  ;; lowered by MUTATION-RATE slider
  ifelse random-float 1 < mutation-rate
    [set wild [who] of one-of flibs ask flib wild [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 (see to previous generation) ---"
  output-print ""
end 



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

to chrom-test
  ;; an environmental input symbol is read from the given sequence
  set new-input 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 item (4 * read-from-string state + 2 * read-from-string new-input) chromosome =
        item ((counter + 1) mod (length environmental-cycle)) environmental-cycle
            [set fitness (fitness + 1)]
  ;; new flibs state is computed
  set state item (4 * read-from-string state + 2 * read-from-string new-input + 1) chromosome
  ;; test is iterated 100 times
  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 is 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: GENETIC SHUFFLING
;; --------------------------------------------------------------------------------------------------------------

to mate
  clear-links
  select-flibs   ;; selection of the mating flibs
  ask flib donor [create-link-to flib recipient]
  ;; just a flibs' chromosome undergoes to genetic-shuffling
  ask flib recipient [genetic-shuffling]
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 selection
  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 genetic-shuffling
  ;; a genes sequence included between a-split and b-split restriction sites is randomly choosen
  set a-split random (num-states * 4)
  set b-split random (num-states * 4)
  set counter 0
  hybridization
  ;; generating 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 chromsome positions on a random
 ;; flib is replaced by the corresponding sequence on one of the most performing flibs;
 ;; chromosomes are treated as circular
 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]]
  if length chromosome != num-states * 4 [show "Invalid chromosome length"]
end 


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

to mutate
  ;; managing mutation and generating report
  ifelse wild > 9  [output-type word "id: " wild]  [output-type word "id: 0" wild]
  output-type word " wild chromosome:       " [chromosome] of flib wild
  output-print word "     fitness: " [fitness] of flib wild
  ;; 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 > 9  [output-type word "id: " wild] [output-type word "id: 0" wild]
  output-print word " mutated chromosome:    " [chromosome] of flib wild
end 

There are 6 versions of this model.

Uploaded by When Description Download
Cosimo Leuci 6 months ago Rev. 0.9.8 Download this version
Cosimo Leuci 6 months ago Rev. 0.9.7 Download this version
Cosimo Leuci 7 months ago Rev. 0.9.5 Download this version
Cosimo Leuci 7 months ago Rev. 0.9.4 Download this version
Cosimo Leuci 7 months ago Rev. 0.9 Download this version
Cosimo Leuci 7 months ago Initial upload Download this version

Attached files

File Type Description Last updated
f'nl_100.jpg jpeg screenshot 7 months ago, by Cosimo Leuci Download
f'nl_100_15.jpg jpeg screenshot 7 months ago, by Cosimo Leuci Download
Flibs'NLogo.png preview Preview for "Flibs'NLogo" 7 months ago, by Cosimo Leuci Download