Dawkins Weasel Remix

Dawkins Weasel Remix preview image

1 collaborator

Patarakin_m Evgeny Patarakin (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by everyone
Model was written in NetLogo 6.2.0 • Viewed 39 times • Downloaded 4 times • Run 0 times
Download the 'Dawkins Weasel Remix' 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

globals
[
  uppercase
  lowercase
  parent-string
  generation
  all-done
]

to setup
  clear-all
  set uppercase "ABCDEFGHIJKLMNOPQRSTUVWXYZ "
  set lowercase "abcdefghijklmnopqrstuvwxyz"
  setup-intial-string
  initialize-string
  set generation 0
  set all-done false
  reset-ticks
end 

to go

  if all-done [ stop ] ; this line prevents the simulation from continuing to print out the following at every timestep after completion:

  if (with-selection and go-with-selection = 0 ) or (not with-selection and go-without-selection = 0) [ ; simulation found an exact matching phrase
    set all-done true
    print word "It took " word generation word " generations of " word number-of-offspring " offspring to evolve to the target phrase."
    stop ]

  tick
end 

to output-generation
  output-print (word generation "   " parent-string)
end 

to-report go-with-selection ; go function used when WITH-SELECTION is TRUE

  let offspring-string parent-string
  let top-offspring-string parent-string
  let top-offspring-score get-score parent-string
  let uppercase-length length uppercase
  let string-length length parent-string

  set generation generation + 1

  let i 0

  ; EACH OFFSPRING LOOP
  while [i < number-of-offspring]
  [
    set offspring-string parent-string;
    let j 0

    ; EACH CHARACTER LOOP
    while [j < string-length]
    [
      let mutation-probability random-float 1.0
      if mutation-probability < mutation-rate [
        let random-letter item (random uppercase-length) uppercase
        set offspring-string replace-item j offspring-string random-letter
      ]
      set j j + 1
    ]

    ; compares current offspring with top offspring
    let current-offspring-score get-score offspring-string
    if current-offspring-score < top-offspring-score [
      set top-offspring-string offspring-string
      set top-offspring-score current-offspring-score
    ]

    set i i + 1
  ]
  set parent-string top-offspring-string
  output-generation
  report top-offspring-score
end 

to-report go-without-selection ; go function used when WITH-SELECTION is FALSE
  let uppercase-length length uppercase
  set generation generation + 1

  let i 0
  while [i < length parent-string]
    [
      let mutation-probability random-float 1.0
      if mutation-probability < mutation-rate [
        let random-letter item (random uppercase-length) uppercase
        set parent-string replace-item i parent-string random-letter
      ]
      set i i + 1
    ]
  output-generation
  report get-score parent-string
end 

to setup-intial-string
  let string-length length target-phrase
  if string-length < 1 [ set target-phrase "SORRY DAVE I CANNOT ALLOW THAT" ]
  let i 0
  while [i < string-length] ; replace lowercase with uppercase:
  [
    if (not member? item i target-phrase uppercase) [
      ifelse (member? item i target-phrase lowercase)
      [ set target-phrase replace-item i target-phrase item (position (item i target-phrase) lowercase) uppercase ]
      [ set target-phrase replace-item i target-phrase " " ]
    ]
    set i i + 1
  ]
end 

to initialize-string
  let initial-string target-phrase
  let string-length length initial-string
  let uppercase-length length uppercase
  let index 0
  while [index < string-length]
  [
    let random-letter item (random uppercase-length) uppercase
    set initial-string replace-item index initial-string random-letter
    set index index + 1
  ]
  set parent-string initial-string;
  set parent-string initial-string
  output-generation
end 

to-report get-score [input-string] ; score used to determine how well the INPUT-STRING matches the TARGET-PHRASE

  let string-length length input-string
  let score string-length
  let index 0
  while [index < string-length]
  [
    let goal-letter item index target-phrase
    let input-letter item index input-string
    if goal-letter = input-letter [set score score - 1]
    set index index + 1
  ]

  report score
end 

There is only one version of this model, created 9 months ago by Evgeny Patarakin.

Attached files

File Type Description Last updated
Dawkins Weasel Remix.png preview Preview for 'Dawkins Weasel Remix' 9 months ago, by Evgeny Patarakin Download

This model does not have any ancestors.

This model does not have any descendants.