您如何为 netlogo 中的代理创造转换概率机会? [英] How do you go about making a convert probability chance for an agent in netlogo?

查看:74
本文介绍了您如何为 netlogo 中的代理创造转换概率机会?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在 NetLogo 2D 中进行僵尸启示录模拟.在我目前的模拟中,当僵尸与人类发生碰撞时,人类有 100% 的机会变成僵尸.我想改变这一点,以便我可以使用滑块来设置转化率——例如,如果我的滑块上的转化率为 50%,那么当僵尸与人类发生碰撞时,它们有 50% 的几率会变成僵尸,否则杀死攻击僵尸.

I'm currently working on a zombie apocalypse simulation in NetLogo 2D. In my current simulation, when the zombies collide with humans the humans have a 100% chance to turn into a zombie. I would like to change this so that I can use a slider to set the conversion rate - for example if I have a rate of 50% on my slider, then when the zombies collide with humans there's a 50% chance they would turn into a zombie, otherwise kill the attacking zombie.

这就是我目前设置我的项目的方式,到目前为止我所做的方法是检测它们何时发生碰撞,并将人类健康设置为 -1,然后做出一个 if 语句,说明健康是否为低于 1,使他们成为僵尸.

This is how I have currently setup my project, the way I've done this so far is to detect when they collide, and set the humans health to -1, and then made an if statement that says if the health is below 1, to make them a zombie.

如果我在正确的方向上提供任何帮助,我将不胜感激,因为我花了很多时间思考这个问题,但还没有想出任何解决方案.

I would appreciate any help in the right direction as I've spent time pondering about this and haven't come up with any solution.

breed[humans person]
breed[zombies zombie]
globals[rad]

humans-own[zombies_seen zombies_hit humans_speed per_vis_rad per_vis_ang health]
zombies-own[zombies_speed humans_around_me closest_humans]

patches-own[solid]

to setup_world
  clear-all
  reset-ticks

  set rad 1

  ask patches[
    set pcolor green
  ]

  create-humans number_of_humans [
  setxy random-xcor random-ycor
    set color blue
    set size 5
    set shape "person"
    set humans_speed 1 + random-float 0.1
    set health 100
    adjust_vision_cone
  ]

 ask humans[
   ask patches in-radius 5[
     set pcolor yellow
    ]
  ]

  create-zombies number_of_zombies [
  setxy random-xcor random-ycor
    set color red
    set size 4
    set shape "person"
    set zombies_speed 0.5
  ]

  ask zombies[
    move-to one-of patches with [pcolor = green]
  ]

  ask humans[
    ask patches in-radius 5[
      set pcolor green
    ]
  ]

  draw_solid_patches
end

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

to detect_wall
  if[solid] of patch-ahead 1 = true[
    right 90
    left 90
  ]
end

to run_model
  Every 0.01 [
    make_humans_move

    make_zombies_move
    tick
    reset_patch_colour

    if not any? humans [stop]
    if ticks = 500000 [stop]
  ]

end

to make_humans_move
  ask humans[
    if health > 1 [
    show_visualisations
    right 45
    left 45
    let have_seen_zombies human_function
    detect_wall
    forward humans_speed
    ]
    if health < 1 [ 
; ;   ; this is where it looks at the health of the humans, 
; ;   ; and checks to see if they have been hit by a zombie, 
; ;   ; because their health would be below 1.
      set breed zombies
      set color red
      set size 4
      set shape "person"
      set zombies_speed 0.5
    ]
  ]
end



to make_zombies_move
  ask zombies[
    detect_wall
    right 45
    left 45
    smell_humans
    detect_wall
    forward zombies_speed
  ]
end
to smell_humans
  if any? humans in-radius 10 [
    set heading towards one-of humans in-radius 10]
  detect_wall
end



to show_visualisations
  if show_vis_cone = true [
    ask patches in-cone per_vis_rad per_vis_ang with [pcolor = green] [
      set pcolor orange
    ]
  ]
end

to reset_patch_colour
  ask patches with [pcolor = orange] [
    set pcolor green
  ]
end


to adjust_vision_cone
    set per_vis_rad 10 + random 10

    set per_vis_ang 90
end

to-report human_function
  let seen [false]
  let hit [false]
  ask zombies in-cone per_vis_rad per_vis_ang [
    set seen true
  ]

  ask zombies in-radius rad [
    set hit true
  ]

  ifelse seen = true [
    set zombies_seen zombies_seen + 1
    right 180

  ][
    right (random zwr - (zwr / 2))
  ]

  if hit = true [
    set zombies_hit zombies_hit + 1
    set color black
    set health -1 
; ;   ; this is where I make the human into a zombie, 
; ;   ; I need to have a method here that will refer to 
; ;   ; the convert_probability variable.
  ]
  report seen

end

推荐答案

对代码进行最小的调整,您可以将 set health -1 替换为

Making minimal adjustments to your code you could substitute the set health -1 to

if random 100 < conversion_probability [ set health 0 ]

其中 conversion_probability 是一个从 0 到 100 的滑块,正如您在问题中所说.

where conversion_probability is a slider going from 0 to 100, as you said in the question.

我还想指出,您可以像这样检测墙壁

I also want to point out that you could detect walls like this

to detect_wall
  if [solid] of patch-ahead 2
  [
    face one-of patches in-radius 2 with [not solid]
  ]
end

left x然后right x只会让海龟向左转然后面向它原来面向的地方,除非它看到僵尸它不会'完全改变的行为.

doing left x and then right x only makes the turtle turn to the left and then face back where it was originally facing, unless it sees a zombie it won't change the behavior of the humans all that much.

这篇关于您如何为 netlogo 中的代理创造转换概率机会?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆