still_learning
still_learning

Reputation: 806

The good and the bad turtles: how to update theirs actions?

I am trying to build a network with good and bad turtles, in order to study their actions (do-well, do-wrong, do-nothing). Good turtles may be susceptible, infected, or selfish; bad turtles only infected, instead. At each tick, other turtles (goods and bad) are created. When the simulation starts, each turtle can (randomly) decide to do something good, bad or just nothing based on its neighbours.

My approach to write the part of code regarding the turtles' actions is the following:

  1. check the links of a random chosen turtle G;
  2. if the turtle has no links, then it can choose between do-good or do-nothing; otherwise, if it has at least one link, then we need to check if its neighbors are good or bad turtles in order to decide which action the turtle should take. The turtle has to choose if follows a good or bad turtle and do some action (do-well, do-wrong, or do-nothing).
  3. if the turtle's neighbour is good (this means not infected), the actions to choose with a certain probability can be do-well, do-wrong or do-nothing. The turtle will continue to be good.
  4. if the turtle's neighbour is bad (this means infected), the actions to choose will be a certain probability can be do-well, do-wrong or do-nothing. The turtle will become infected, only if it chooses do-wrong.

This is the part of code that I have written trying to answer this question:

    ifelse one-of goods with [any? link-neighbors]  ;; check neighbours and decide action to take
     [ ifelse out-link-neighbors with [infected? true] ;; if neighbor is infected, it should have three options: do-well, do-wrong, do-nothing.
        [ 
           ifelse random-float 1 < probability
           [do-well][do-wrong]
        ] ;; but there should be another possible action, do-nothing... and maybe I should use an if condition with probability split in 0.3, 0.4, 0.3 for example
      [
        ifelse random-float 1 < probability ;; this if condition is for turtle linked with only good turtles 
            [do-well][do-nothing]
]

 ] [ifelse random-float 1 < probability [do-well][do-nothing]]

but I understand that this code cannot work due to errors, so I would like to ask you what I am doing wrong and, eventually, if you could explain me why.

(If you need to edit the probability, in order to show me how I could set three different options/actions in an if/ifelse condition, please do that. I used probability as an example on what I am trying to do)

Upvotes: 0

Views: 150

Answers (1)

JenB
JenB

Reputation: 17678

You have lots of problems.

  • ifelse one-of goods with [any? link-neighbors] makes no sense. Your description says that you want to choose a random turtle. This doesn't choose a turtle, it checks whether a random turtle has any neighbours. But it doesn't then use that same turtle for any of the code that follows.
  • Your description has a logic problem at step 2. What happens if the chosen turtle has two neighbours - one good and one bad? The way I have written the code below gets the turtle to randomly select one of its neighbours and then check whether that particular neighbour is good or bad. But you could also check whether there are any that are bad, the proportion of bad is above some threshold, or all are bad. You need to decide the implementation that you want.
  • If there are three possible actions, then you need some way of indicating the probability of each, you can't just use one probability.

So I made some guesses and here's one possibility. Of course, it is untested as I haven't built a model with 'goods' or a network etc. Hopefully it will help you work out what you want and how to do it.

ask one-of goods ; select a random turtle
[ ifelse [any? link-neighbors]  ; check if it has neighbours
  [ let choice one-of out-link-neighbors   ; pick one of the available neighbours
    [ let roll random-float 1     ; get a random number
      ifelse  [infected?] of choice
      ; condition if the chosen neighbour is infected
      [ ifelse roll < probability1
        [ do-well ]
        [ ifelse roll < probability1 + probability2 [do-wrong] [do-nothing] ]
      ]
      ; condition if the chosen neigbour not infected 
      [ ifelse roll < probability1   
        [do-well]
        [do-nothing]
      ]
    ]
  ]
  [ print "No neighbours to select" ]
]

Upvotes: 2

Related Questions