Flibs'NFarol

Flibs'NFarol preview image

1 collaborator

F1 Cosimo Leuci (Author)

Tags

adaptive cognition 

Tagged by Cosimo Leuci 3 months ago

artificial life 

Tagged by Cosimo Leuci 3 months ago

finite automata 

Tagged by Cosimo Leuci 3 months ago

genetic algorithms 

Tagged by Cosimo Leuci 3 months ago

self-organisation 

Tagged by Cosimo Leuci 3 months ago

social science 

Tagged by Cosimo Leuci 3 months ago

Part of project 'Starfish_Planet' Child of model Flibs'NLogo preview imageFlibs'NLogo
Visible to everyone | Changeable by the author
Model was written in NetLogo 6.0.4 • Viewed 148 times • Downloaded 1 time • Run 0 times
Download the 'Flibs'NFarol' 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" (A. K. Dewdney) A flib is considered perfect
                         ;; predictor i.e. very well adapted, when its outgoing signals (prevision/choice)
                         ;; is equal to the next incoming environmental signal. Signals are always binary
                         ;; digits and in this model they are the sum of the signals produced by all flibs.


flibs-own [chromosome    ;; Every flib owns a chromosome that is a string coding its state transition table;
           chrom-10      ;; chromosome transposition into decimal number when it is composed by binary digits only
                         ;; (i.e. flibs states are less than three)
           state         ;; the current inner state of the flib
           choice        ;; the binary prevision/choice expressed by the flib about the problem to go or
                         ;; not to go to the bar
           fitness       ;; a measure of flibs prevision ability to forestall if the bar will be crowded or not
           ]


globals [id              ;; a counter to recall flibs one by one
         bases-replaced  ;; counts the number of chromosome bases replacement during generation
                         ;; and hybridization processes
         attendance      ;; counts the number of agents attending the bar
         tot_attend      ;; sum of attendances during one El Farol evening
         sum_attend      ;; summation of all attendances gotten during one El Farol season (i.e. 100 cycles)
         sigma           ;; variance in attendances referred to threshold level
         sum_sigma       ;; summation of all variances gotten during one El Farol season
         sum_fit_dev     ;; summation of flibs fitness standard deviations
         crowded         ;; a switch recording the state of the bar: 1 if overcrowded, 0 if not
         average         ;; mean of flibs population fitness
         best            ;; the best flibs fitness value
         worst           ;; the worst flibs fitness value
         donor           ;; a best performing flib sharing part of its genes
         recipient       ;; a 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 (before mutation)
         ]



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

to setup                 ;; initializing the model
  clear-all
  ;; create the bar area (green) and an elsewhere (blue)
  ask patches  [set pcolor blue]
  ask patches with [abs pxcor < 10 and abs pycor < 7] [set pcolor green]
  ask patches with [pxcor = 9 and pycor = 7] [set plabel ["El Farol Bar"]]
  ask patches with [pxcor = 21 and pycor = -21] [set plabel ["Somewhere else"]]

 ;; fifty flibs and their chromosomes are generated
  ask n-of num-flibs patches with [pcolor = blue] [sprout-flibs 1 [
    set shape "person"
    set color white
    set size 1.8
    set chromosome ""
    set state 0]   ;; flibs are initially all out of the bar and their state is conventionally 0
  ]
  ask flibs [chromosome-genesis]
  if num-states > 2 [notice]
  reset-ticks
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 bases-replaced bases-replaced + 1
  if bases-replaced < num-states * 2 [chromosome-genesis]
  set bases-replaced 0
end 

to notice
  output-print "Chromosomes analysis "
  output-print "takes place for binary "
  output-print "string only, "
  output-print "so analysis is disabled "
  output-print "when number of flibs "
  output-print "states are higher than two"
end 



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

to go
  ;; every generation one genetic shuffling occurs by default,
  ;; this frequency can be lowered by slider conjugation-RATE
  if random-float 1 < mate-rate [conjugation]
  ;; every season, a mutagenesis process can occur, this frequency can be adjusted by slider MUTATION-RATE

  ;; the mutational event is preceded by the random choice of a wild-type candidate
  if random 10000000 / 1000000 < mutation-rate
    [set wild-type [who] of one-of flibs ask flib wild-type [mutate]
  ]
  ;; variables are resetted before another season to El Farol Bar begins
  ask flibs [set fitness 0 set state 0]
  set crowded 0
  set attendance 0
  set tot_attend 0
  repeat 100 [el-farol]
  results-analysis
  tick
end 

to results-analysis
  ;; the El Farol seasonal results are summarized and displayed
  set best max [fitness] of flibs
  set worst min [fitness] of flibs
  set average mean [fitness] of flibs
  ask flibs [set color scale-color red fitness 101 0]
  set sigma abs (tot_attend / 100 - threshold)
  set sum_attend sum_attend + tot_attend / 100
  set sum_sigma sum_sigma + sigma
  set sum_fit_dev sum_fit_dev + standard-deviation [fitness] of flibs
  if num-states < 3 [chrom-analysis]
end 

to chrom-analysis
  output-print ""
  output-print ""
  output-type word "------ season " ticks
  output-print " ------------"
  output-print ""
  ask flibs [set chrom-10 0]
  binary2decimal
  output-print "id.  bin<-chrom.->dec   fitness"
end 

to binary2decimal
  ask flibs
    [set chrom-10 chrom-10 + (read-from-string item bases-replaced reverse chromosome) * 2 ^ bases-replaced]
  set bases-replaced bases-replaced + 1
  ifelse bases-replaced < num-states * 4 [binary2decimal]  [
    set bases-replaced 0
    ask flibs [ifelse who <= 9 [output-type word "0"who] [output-type who]
      output-type word "   " chromosome
      ifelse chrom-10 < 10 [output-type word "       " chrom-10]
        [ifelse chrom-10 < 100 [output-type word "      " chrom-10]
          [output-type word "     " chrom-10]
      ]
    output-print word "   " fitness]
  ]
end 



;; Operator 1: A SEASON TO EL FAROL BAR
;; --------------------------------------------------------------------------------------------------------------

to el-farol
  set id 0
  previsions&decisions
  ;; the attendance is the result of the flibs choices
  set attendance sum [choice] of flibs / num-flibs
  set tot_attend tot_attend + attendance
  ;; comparing the attendance and the threshold value, the (over)crowded state of the bar is determined
  if attendance >= threshold [set crowded 1
    ;; if the bar is crowded, the flibs that have choiced to stay elsewhere are rewarded
    ask flibs with [choice = 0] [set fitness fitness + 1] ]
  if attendance < threshold [set crowded 0
    ;; if the bar is not crowded, the flibs that have choiced to go there are rewarded
    ask flibs with [choice = 1] [set fitness fitness + 1] ]
end 

to previsions&decisions
  ask flib id [
    ;; new flibs state is computed
    set state read-from-string item (4 * state + 2 * crowded + 1) chromosome
    ;; each flibs prevision is tested and fitness updated
    set choice read-from-string item (4 * state + 2 * crowded) chromosome
    ;; flib choice is displayed
    ifelse choice = 0 [move-to one-of patches with [pcolor = blue]]
      [move-to one-of patches with [pcolor = green]]
  ]
  set id id + 1
  if id < num-flibs [previsions&decisions]
end 


;; Operator 2: GENETIC SHUFFLING
;; --------------------------------------------------------------------------------------------------------------

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

to select-flibs
  ;; there are three selection method to select the "donor" chromosome:
  ;; it can be the fittest, the misfittest or it can be randomly chosen
  ;; the MATE-SELECTION chooser allows to set it
  if mate-selection = "random" [set donor [who] of one-of flibs]
  if mate-selection = "fittest" [set donor [who] of one-of flibs with [fitness = best]]
  if mate-selection = "misfittest" [set donor [who] of one-of flibs with [fitness = worst]]
  ;; the "recipient" chromosome is always randomly chosen
  set recipient [who] of one-of flibs
  ;; self conjugation is forbidden
  if donor = recipient [select-flibs]
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 bases-replaced 0
  hybridization
  set fitness 0
end 

to hybridization
 ;; the genes' sequence included between a-split and b-split restriction sites 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 + bases-replaced)
    chromosome (item (a-split + bases-replaced) [chromosome] of flib donor)
    set bases-replaced (bases-replaced + 1) if bases-replaced < b-split - a-split
    [hybridization]
  ]
 if a-split > b-split [
    set chromosome replace-item ((a-split + bases-replaced) mod (num-states * 4))
    chromosome (item ((a-split + bases-replaced) mod (num-states * 4)) [chromosome] of flib donor)
    set bases-replaced (bases-replaced + 1) if bases-replaced < (num-states * 4) - (a-split - b-split)
    [hybridization]
  ]
  set bases-replaced 0
end 


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

to mutate
  ;; mutations occur randomly at a given frequency on one locus only
  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) ""]
  set fitness 0
end 


; Copyright 2019 Cosimo Leuci.
; See Info tab for full copyright and license.

There are 6 versions of this model.

Uploaded by When Description Download
Cosimo Leuci 2 months ago Rev. 0.3.4.7 Download this version
Cosimo Leuci 3 months ago Rev. 0.3.4.1 Download this version
Cosimo Leuci 3 months ago Rev. 0.3.4.1 Download this version
Cosimo Leuci 3 months ago Rev. 0.3.4 Download this version
Cosimo Leuci 3 months ago Rev. 0..3.3 Download this version
Cosimo Leuci 3 months ago Initial upload Download this version

Attached files

File Type Description Last updated
Flibs'NFarol.png preview Preview for "Flibs'NFarol" 3 months ago, by Cosimo Leuci Download

Parent: Flibs'NLogo

This model does not have any descendants.

Graph of models related to 'Flibs'NFarol'