0

currently I'm working on a zombie apocalypse simulation on Netlogo, and I'm almost done except that the convertion function is not working. when the zombies collide with humans the humans they either die or convert the human to a zombie according to the global variable convert_probability. For example, if convert probability is set to 80 the human would have an 80% chance of winning the fight and killing the zombie and the zombie would have a 20% chance of converting the human to a zombie. Also, zombies and humans are not allowed to pass over the brown patches and the function I wrote for that is change_direction, but had to comment those parts because I run to an error. I have spent hours but couldnt come up with a solution so I would really appreciate any help;

breed [humans human]
humans-own [  humans_speed]
turtles-own [
  infected?
  
  
]

patches-own [
  block
]


breed [zombies zombie]
zombies-own [ zombies_speed]

globals [
  student_id
  username
  nzomb
  %infected  
]



to setup_world
  clear-all
  reset-ticks
  
  set student_id 19009670
    
  ask patches [set pcolor green]
   
  set-default-shape zombies "person"
  set-default-shape humans "person"
  
  
  create-zombies 5 [
    set color red 
    set size 4 
    set zombies_speed 0.5
    setxy random-pxcor random-pycor
    
  ]
  
  
   ask zombies[
    move-to one-of patches with [pcolor = green]
  ]
  
  
  create-humans 15 [
    set color blue 
    set size 4  
    set humans_speed 1 + random-float 0.1
    setxy random-pxcor random-pycor
    
    
  ]
  
 
  ask humans[
    move-to one-of patches with [pcolor = green]
  ]
  
  
  set %infected (count turtles with [color = red ] / 20 ) * 100
  
  set nzomb 0
  
  draw_blocks

end


to draw_blocks
  ask patches [
    set block false
  ]
  ask n-of 100 patches with [pcolor = green][
    set pcolor brown
    set block true    
  ]
end



to change_direction
  if[block] of patch-ahead 2 = true [
    right 180
    face one-of patches in-radius 2 with [not block]
  ]
end


to run_model
  if ticks >= 10000 [ stop ]
  tick
  move-turtles
  convert
  
  
  if not any? humans [stop]
  
end

to move-turtles
  ask humans [move-humans]
  ask zombies [move-zombies] 
end

to move-humans
  
  ask humans [
    if any? zombies in-radius (10 + random 20)  [rt 180]
    set infected? false
    set heading (heading + 45 - (random 90))
    forward 1 + random-float 1.1
    
    zombiedeath
    convert
    ;;change_direction    
  ] 
 
end

to move-zombies
  ask zombies [
    
    chasehuman
    convert
    zombiedeath
        
    set %infected (count turtles with [color = red ] / 20 ) * 100
    set infected? true
    set heading (heading + 45 - (random 90))
    
    forward 0.5
   ; change-direction    
  ]
  
end


to chasehuman
  if any? humans in-radius 10 [
    set heading towards one-of humans in-radius 10]
   ; change_direction
  
  
end

to runaway
  face min-one-of zombies [distance myself]
  rt 180
  ;change_direction
  
end
  

to convert
  ask turtles with [ breed = zombies ]

[
    ask humans-on patch-here 
    [
      if random 100 < convert_probability [set breed zombies]]
   
    ask humans-on patch-here [if random 100 < convert_probability
        [set color red]]
  ]
    

end


to zombiedeath
  
  ask zombies-on patch-here [if random 100 > convert_probability
    [die]]
end

1 Answers1

1

Okay, the only procedure relevant to this question is this one (same code as above, formatted so you can see the structure logic}:

to convert
  ask turtles with [ breed = zombies ]
  [ ask humans-on patch-here 
    [ if random 100 < convert_probability
      [ set breed zombies
      ]
    ]
    ask humans-on patch-here
    [ if random 100 < convert_probability
      [ set color red
      ]
    ]
  ]
end

You haven't actually told us what the problem is - "convertion function is not working" does not say whether it is generating an error, or what behaviour it is doing compared to what is expected for example. But, look at what this code actually does. First it finds all the zombies then, for each zombie, it:

  1. gets the humans on the same patch to convert to zombies with some probability
  2. gets the humans on the same patch to change colour to red with some probability

Your first problem is that you haven't linked the turning red with becoming a zombie. Is that the problem you were trying to solve? If so, here is some cleaned up code that links zombie and red:

to convert
  ask zombies              ; note, you can just directly use the breed name
  [ ask humans-here        ; again, can use the breed name
    [ if random 100 < convert_probability
      [ set breed zombies
        set color red
      ]
    ]
  ]
end
JenB
  • 17,620
  • 2
  • 17
  • 45
  • Sorry for not being clear enough previously, with my previous code although i would set convert_probability to 100% thats when all humans must convert to a zombie, none of them would, and at 0% no zombies where dying. I have tried your code and it great and I have modified zombiedeath function looking at it, now I the simulation is working as its supposed to be, except for the change_direction part. But thank you very much for your help, all the best – Sevde Aksoy Apr 20 '21 at 20:30