Modalizing Mechanisms

Modalizing Mechanisms preview image

1 collaborator

Default-person Manolo Martínez (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by everyone
Model was written in NetLogo 5.1.0 • Viewed 154 times • Downloaded 14 times • Run 0 times
Download the 'Modalizing Mechanisms' 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

;;This file is part of the Modalizing Mechanisms model.
;;
;;the Modalizing Mechanisms model is free software: you can redistribute it and/or modify
;;it under the terms of the GNU General Public License as published by
;;the Free Software Foundation, either version 3 of the License, or
;;(at your option) any later version.
;;the Modalizing Mechanisms model is distributed in the hope that it will be useful,
;;but WITHOUT ANY WARRANTY; without even the implied warranty of
;;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;GNU General Public License for more details.
;;
;;You should have received a copy of the GNU General Public License
;;along with the Modalizing Mechanisms model.  If not, see .

extensions [matrix]

__includes ["utils.nls"]

breed [monsters monster]
breed [hunters hunter]

globals [should-stop old-number-of-hunters hunters-list states messages acts hunter-pure-strats]

turtles-own [energy huntstage]
monsters-own [monstertype myhunter stagecolors]
hunters-own [mymonster strategy investmentpolicy]

to setup
  clear-all
  initialize-utils
  initialize-monsters
  initialize-hunters
  reset-ticks
end 

to initialize-monsters
 create-monsters number-of-monsters [
    set stagecolors [yellow red blue]
    set energy monster-starting-energy
    set monstertype random 3
    set color item monstertype stagecolors
    set huntstage 0
    set myhunter nobody
    setxy random-xcor random-ycor]
end 

to initialize-hunters
 create-hunters number-of-hunters [
    set energy hunter-starting-energy
    set strategy random-hunter-strat
    ;; set strategy matrix:make-identity 3
    set investmentpolicy random-hunter-investment-policy
    set shape "square"
    set color orange
    set huntstage 0
    set mymonster nobody
    setxy random-xcor random-ycor]
end 

to go
  check-progress
  if should-stop [
    stop
  ]
  monsters-go
  hunters-go
  tick
end 

to check-progress
  ;; calculates an average of the number of hunters over the last 10000 ticks. It it doesn't grow, we stop
  set hunters-list lput count hunters hunters-list
  if ticks / 10000 = floor (ticks / 10000) [
    let new-number-of-hunters mean hunters-list
    set hunters-list []
    if old-number-of-hunters >= new-number-of-hunters [
      set should-stop true
    ]
    set old-number-of-hunters new-number-of-hunters
  ]
end 

to monsters-go
  monsters-time-passes  
end 

to hunters-go
  hunters-find-monster
  hunters-hunt
  hunters-time-passes
end 

to monsters-time-passes
  ask monsters [
    set energy energy - 1
    if energy <= 0 [
      if-else monstertype = 0 [
        set monstertype (weighted-random-choice list prob-air (1 - prob-air)) + 1
        set energy monster-starting-energy
        set color item monstertype stagecolors
      ]
      [
        if huntstage > 0 [
          ;; show mysender
          dismantle-hunt self myhunter
        ]
        monster-die self  
      ]
    ]
  ]
end 

to monster-die [thismonster]
  ask thismonster [
    hatch 1 [
    set color yellow
    set monstertype 0       
    set energy monster-starting-energy
    setxy random-xcor random-ycor
    ]
    die
  ]
end 

to hunters-time-passes
  ask hunters [
    set energy energy - 1
    if energy <= 0 [
      if huntstage > 0 [
      dismantle-hunt mymonster self
      ]
      die
    ]
    if energy >= hunter-reproducing-energy [
        hatch 1 [
          set energy ([ energy ] of myself) / 2
          setxy random-xcor random-ycor
          if random-float 1 < mutation-probability [
            if-else random-float 1 < mutation-mix [
              set strategy perturb-matrix perturbation-ratio [ strategy ] of myself
              set investmentpolicy perturb-vector perturbation-ratio [ investmentpolicy ] of myself
            ] [
              set strategy random-hunter-strat
              set investmentpolicy random-hunter-investment-policy
            ]
          ]
        ]
        set energy energy / 2
    ]
  ]
end 

to hunters-find-monster
  ask hunters [
    if mymonster = nobody [
      let nearby-idle-monsters monsters in-radius hunter-radius with [myhunter = nobody]
      set mymonster one-of nearby-idle-monsters
      if mymonster != nobody [
        ;; show "found a monster"
        ask mymonster [ set myhunter myself ]
        ask (turtle-set self mymonster) [set huntstage 1 ]
      ]
    ]
  ]
end 

to hunters-hunt
  ask hunters [
    if-else huntstage = 0
      [ stop ]
      [if mymonster = nobody [
        dismantle-hunt mymonster self
        ;; show "i'm outta here"
        relocate self
        stop
      ]
    ]
    if huntstage = 1 [ first-round mymonster self ]
    if huntstage = 2 [ second-round mymonster self ]
  ]
end 

to first-round [monster hunter]
  ;; show "first round"
  let act get-act monster hunter
  if-else act > 0 or [monstertype] of monster > 0 [
    let payoff unprepared-attack [monstertype] of monster act
    ;; show "1st round payoff"
    ;; show payoff
    ask hunter [ set energy (energy + payoff) ]
    dismantle-hunt monster hunter
    monster-die monster
    relocate hunter
  ] [
    ask (turtle-set hunter monster) [ set huntstage 2 ]
  ]
end 

to second-round [monster hunter]
  ;; show "second round"
  ;; show [ monstertype ] of monster
  if ([ monstertype ] of monster) > 0 [
    let act get-act monster hunter
    let policy [ investmentpolicy] of hunter
    let payoff prepared-attack [monstertype] of monster act policy
    ;; show payoff
    ask hunter [ set energy (energy + payoff) ]
    dismantle-hunt monster hunter
    monster-die monster
    relocate hunter
  ]
end 

to-report unprepared-attack [thismonstertype act]
  if thismonstertype = 0 [ report energy-of-undecided ]
  if-else thismonstertype = act
    [ report 10 ]
    [ report investment-to-payoff 0 ]
end 

to-report prepared-attack [thismonstertype act policy]
  ;; show "prepared attack"
  if-else thismonstertype = act [
    let investment item (act - 1) policy
    report (investment-to-payoff investment) - 1
  ] [
    report -1
  ]
end 

to dismantle-hunt [monster hunter]
  if monster != nobody [
    ask monster [
      set myhunter nobody
      set huntstage 0
    ]
  ]
  if hunter != nobody [
    ask hunter [
      set mymonster nobody
      set huntstage 0
    ]
  ]
end 

to-report get-act [monster hunter]
  let hunterstrat [strategy] of hunter
  let thismonstertype [monstertype] of monster
  let stratpertype matrix:get-row hunterstrat thismonstertype
  let act weighted-random-choice stratpertype
  report act
end 

to relocate [agent]
  if agent != nobody [
    ask agent [ setxy random-xcor random-ycor ]
  ]
end 

There is only one version of this model, created over 5 years ago by Manolo Martínez.

Attached files

File Type Description Last updated
Modalizing Mechanisms.png preview Preview for 'Modalizing Mechanisms' over 5 years ago, by Manolo Martínez Download

This model does not have any ancestors.

This model does not have any descendants.