Flibs'NFarol

No preview image

1 collaborator

Cosimo.leuci Cosimo Leuci (Author)

Tags

adaptive cognition 

Tagged by Cosimo Leuci over 4 years ago

artificial life 

Tagged by Cosimo Leuci over 4 years ago

el farol 

Tagged by Cosimo Leuci almost 4 years ago

finite automata 

Tagged by Cosimo Leuci over 4 years ago

genetic algorithms 

Tagged by Cosimo Leuci over 4 years ago

self-organisation 

Tagged by Cosimo Leuci over 4 years ago

social science 

Tagged by Cosimo Leuci over 4 years 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.3.0 • Viewed 547 times • Downloaded 62 times • 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

;;  _________________________________________________________________________________________________________________
;;
;;  --------------------   FlibsN'Farol   ---------------------------------------------------------   FlibsN'Farol
;;  FlibsN'Farol   ---------------------------------------------------------   FlibsN'Farol   -----------------------
;;  _________________________________________________________________________________________________________________



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)


flibs-own [chromosome      ;; every flib owns a chromosome, that is a string codifying its state transitions schema
           chrom-10        ;; the corresponding decimal number of a binary chomosome string (when the flibs possible
                           ;; states are less than three)
           state           ;; the current inner state of the flib
           choice          ;; choice/prevision expressed by the flib regarding 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  [tot_attend        ;; total of attendances during one season to "El Farol" bar (i.e. 100 cycles)
          sigma_attend      ;; an accumulator for the previous variable
          crowded           ;; a switch recording the state of the bar: 1 if overcrowded, 0 if not
          lorenz-points     ;; list of the Lorenz curve ordinates
          gini-index-reserve;;
          sigma-gini        ;; an accumulator for the previous variable
          diversity         ;; the different chromosomes number in the flibs population
          best              ;; the best flibs fitness value
          worst             ;; the worst flibs fitness value
          donor             ;; one of best performing flibs sharing part of its genes
          recipient         ;; one flib acquiring genes from the donor flib
          a-split           ;; the first cut in the recipient's chromosome
          b-split           ;; the second cut in the recipient's chromosome
          locus             ;; pointer/counter of chromosome loci
          ]



;;  ----------   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"]]

  ;; flibs and their chromosomes are generated
  ask n-of num-flibs patches [sprout-flibs 1 [
    set shape "flib"
    set color white
    set size 2
    set chromosome ""
    set state random num-states]
  ]
  set locus 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.
                        ;; The content of even string's positions are 1 or 0: they are the
                        ;; flibs outgoing signals representing respectively the intention to go or not to go
                        ;; to the bar. Odd string's positions represent a possible flib's states.
  set chromosome word chromosome (word (random 2) (random num-states))
  set locus locus + 1
  if locus < num-states * 2 [chromosome-genesis]
  set locus 0
end 

to notice
  output-print "Sequences separation takes"
  output-print "place for binary chromosomes"
  output-print "only. The analysis is"
  output-print "unavailable when the number"
  output-print "of flibs' states are higher"
  output-print "than two."
end 



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

to go
  set tot_attend 0
  ask flibs [set fitness 0 set state 0]

  repeat 100 [el-farol] ;; a 100 cycles tournement will correspond to a season to "El Farol" bar (i.e. 100 nights)

  if sum [fitness] of flibs = 0 [  ;; no fitness no progress
    show "fitness null for every flibs"
    stop
  ]
  analyse   ;; some relevant "El Farol" seasonal results are picked and processed

  ;; after every season, one breeding and one mutational event can occur
  if random-float 1 < mate-rate [conjugation]
  if random-float 1 < mutation-rate [ask one-of flibs [mutate]
  ]
  tick
end 


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

to el-farol
  let attendance 0    ;; the variable records the fraction of agents attending the bar during one night
  flibs-behaviour
  ;; bar attendance is the result of every 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, who have chosen 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, who have chosen to go there are rewarded
    ask flibs with [choice = 1] [set fitness fitness + 1] ]
end 

to flibs-behaviour
  ask flibs [
    ;; the flibs state is updated
    set state read-from-string item (4 * state + 2 * crowded + 1) chromosome
    ;; each flib processes its choice (to go or not to go)
    set choice read-from-string item (4 * state + 2 * crowded) chromosome
    ;; flibs behaviour is displayed
    ifelse choice = 0
      [move-to one-of patches with [pcolor = blue]]
    [move-to one-of patches with [pcolor = green]]
  ]
end 

to analyse
  set best max [fitness] of flibs
  set worst min [fitness] of flibs
  ask flibs [set color scale-color red fitness 101 0 set label fitness]
  set sigma_attend sigma_attend + tot_attend / 100
  update-lorenz-and-gini
  diversity-assesment
  if num-states < 3 [chrom-analysis]
end 

to update-lorenz-and-gini
  let sorted-comfort sort [fitness] of flibs
  let total-comfort sum sorted-comfort
  let comfort-sum-so-far 0
  let index 0
  set gini-index-reserve 0
  set lorenz-points []
  repeat num-flibs [
    set comfort-sum-so-far (comfort-sum-so-far + item index sorted-comfort)
    set lorenz-points lput ((comfort-sum-so-far / total-comfort) * 100) lorenz-points
    set index (index + 1)
    set gini-index-reserve
      gini-index-reserve +
      (index / num-flibs) -
      (comfort-sum-so-far / total-comfort)
  ]
    set sigma-gini sigma-gini + ((gini-index-reserve / num-flibs) * 2)
end 

to diversity-assesment
  let sort-chrom sort [chromosome] of flibs
  let index 1
  set diversity 1
  repeat num-flibs - 1 [
    if item index sort-chrom != item (index - 1) sort-chrom [set diversity diversity + 1]
    set index (index + 1)
  ]
end 

to chrom-analysis
  output-print ""
  output-print ""
  output-type word "------ season " ticks
  output-print " ------------"
  output-print ""
  ask flibs [set chrom-10 0]
  binary2decimal
  if num-states = 1 [output-print " id.  bindec   fitness"]
  if num-states = 2 [output-print " id.  bin  dec  fitness"]
end 

to binary2decimal
  ask flibs
    [set chrom-10 chrom-10 + (read-from-string item locus reverse chromosome) * 2 ^ locus]
  set locus locus + 1
  ifelse locus < num-states * 4 [binary2decimal]
    [set locus 0
     ask flibs [ifelse who <= 9 [output-type word "00" who]
        [ifelse who <= 99 [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]
  ]
  set locus 0
end 



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

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

to select-flibs
  ;; there are three selection modes to choose the "donor" and "recipient" chromosomes: they can be the fittest,
  ;; the misfittest, or they can be randomly chosen
  if donor-selection = "random"     [set donor [who] of one-of flibs]
  if donor-selection = "fittest"    [set donor [who] of one-of flibs with [fitness = best]]
  if donor-selection = "misfittest" [set donor [who] of one-of flibs with [fitness = worst]]
  if recipient-selection = "random"     [set recipient [who] of one-of flibs]
  if recipient-selection = "fittest"    [set recipient [who] of one-of flibs with [fitness = best]]
  if recipient-selection = "misfittest" [set recipient [who] of one-of flibs with [fitness = worst]]
  ;; 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 locus 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 + locus)
      chromosome (item (a-split + locus) [chromosome] of flib donor)
    set locus (locus + 1) if locus < b-split - a-split
      [hybridization]
  ]
 if a-split > b-split [
    set chromosome replace-item ((a-split + locus) mod (num-states * 4))
      chromosome (item ((a-split + locus) mod (num-states * 4)) [chromosome] of flib donor)
    set locus (locus + 1) if locus < (num-states * 4) - (a-split - b-split)
      [hybridization]
  ]
  set locus 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 2022 Cosimo Leuci.
; See Info tab for full copyright and license.

There are 23 versions of this model.

Uploaded by When Description Download
Cosimo Leuci 8 months ago Rev. 0.5.5 Download this version
Cosimo Leuci 8 months ago Rev. 0.5.5 Download this version
Cosimo Leuci 8 months ago Rev. 0.5.5 Download this version
Cosimo Leuci 8 months ago Rev. 0.5.5 Download this version
Cosimo Leuci 8 months ago Rev. 0.5.5 Download this version
Cosimo Leuci 8 months ago Rev. 0.5.5 Download this version
Cosimo Leuci 8 months ago Rev. 0.5.5 Download this version
Cosimo Leuci 8 months ago Rev. 0.5.5 Download this version
Cosimo Leuci 8 months ago Rev. 0.5.5 Download this version
Cosimo Leuci 8 months ago Rev. 0.5.5 Download this version
Cosimo Leuci 11 months ago Rev. 0.5.3 Download this version
Cosimo Leuci 11 months ago Rev. 0.5.2 Download this version
Cosimo Leuci 11 months ago Rev. 0.5.1 Download this version
Cosimo Leuci 11 months ago Rev. 0.5 Download this version
Cosimo Leuci almost 4 years ago Rev. 0.3.5.1 Download this version
Cosimo Leuci almost 4 years ago Rev. 0.3.5 Download this version
Cosimo Leuci almost 4 years ago Rev. 0.3.5 Download this version
Cosimo Leuci over 4 years ago Rev. 0.3.4.7 Download this version
Cosimo Leuci over 4 years ago Rev. 0.3.4.1 Download this version
Cosimo Leuci over 4 years ago Rev. 0.3.4.1 Download this version
Cosimo Leuci over 4 years ago Rev. 0.3.4 Download this version
Cosimo Leuci over 4 years ago Rev. 0..3.3 Download this version
Cosimo Leuci over 4 years ago Initial upload Download this version

Attached files

No files

Parent: Flibs'NLogo

This model does not have any descendants.

Graph of models related to 'Flibs'NFarol'