首次尝试NetLogo ABM – 遇到了关于程序的问题。

huangapple go评论86阅读模式
英文:

First time doing NetLogo ABM - encountering with procedures

问题

I can provide translations for the code-related parts of your text:

  1. 我正在建模一个社交环境,其中个体根据他们的自我效能水平选择目标。这些自我效能值然后根据他们管理目标的方式进行更新。如果他们没有成功或失去了继续的勇气,他们可以选择切换目标。
  2. 到目前为止,我有三个问题。
  3. 1) 我希望每个个体都有个人的自我效能值,因此一旦个体被初始化并经历了选择目标、更新其效能并可能选择新目标的过程,那么它的值就不再受到影响。也就是说,新代理的初始化不应改变这些值,新代理不应继承其前代代理的值。看起来是否确实发生了这种情况?
  4. 2) 我希望“go”过程保持运行,直到ticks > 599。我已经尝试实现这一点,但当我按下`GO`时,它只每次运行一次。这应该很容易实现,但我的所有尝试都没有奏效。
  5. 3) 我希望每个代理的默认颜色都为79,然后每次代理“切换”目标时都会减少1。目前似乎当前颜色是全局起点,然后随着所有代理的过程而逐渐减少,从而继承了其他代理影响它的数字。
  6. 以下是代码:

Please note that I've provided translations for the code-related parts only, as you requested.

英文:

I am modeling a social environment where individuals are choosing goals based on their self-efficacy levels.<br>
These Self-efficacy values are then updated based on how they manage the goal. They are then allowed to switch goals if they did not succeed or lost courage to continue.

So far I have three problems.

  1. I want personal se-values for each individual, so that once an individual is initialized and have gone through the process of choosing a goal, updating its efficacy and possibly selecting a new goal, then its values are no longer affected. I.e., the initialization of a new agent should not change these values, and a new agent should not inherit the values from it former agent. Does it seem like this is indeed what is happening?

  2. I want the "go" procedure to keep running until it's >599. I have tried to implement this, but when I press <kbd>GO</kbd> it only does 1 tick at a time. This should be easy to implement, but none of my attempts have worked.

  3. I want the default color to be 79 for each agent, and then it should deduct 1 for each time the agent "swaps" goals. <br>
    Right now it seems that current-color is the global starting point, and then it is decreasing gradually as all agents go through the process, thus inheriting the numbers from other agents influencing it.

The code :

  1. globals [current-color]
  2. breed [individuals individual]
  3. breed [goals goal]
  4. individuals-own [
  5. pa-se
  6. ea-se
  7. vl-se
  8. sp-se
  9. pa-se-initial
  10. ea-se-initial
  11. vl-se-initial
  12. sp-se-initial
  13. efficacy
  14. efficacy-initial
  15. chosen-goal
  16. new-goal
  17. ;; setting efficacy levels
  18. ]
  19. goals-own [
  20. name
  21. ]

  1. to setup
  2. clear-all
  3. set-default-shape turtles &quot;circle&quot;
  4. set current-color 79
  5. ;; creating the four goals i the space
  6. create-goals 4 [
  7. set color blue
  8. setxy random-xcor random-ycor
  9. ;; I want to move the goals towards the center, so I make them face the center patch and move towards it a bit
  10. facexy 0 0
  11. forward 15
  12. set name &quot;&quot;
  13. set label-color white
  14. ask goal 0 [set name &quot;GOAL 1&quot;]
  15. ask goal 1 [set name &quot;GOAL 2&quot;]
  16. ask goal 2 [set name &quot;GOAL 3&quot;]
  17. ask goal 3 [set name &quot;GOAL 4&quot;]
  18. set label name
  19. ]
  20. reset-ticks
  21. end

  1. to go
  2. if ticks &gt; 599 [stop]
  3. initialize-individual
  4. choose-goal
  5. layout
  6. tick
  7. end

  1. to initialize-individual
  2. create-individuals 1 [
  3. set color current-color
  4. ;; Assign variables randomly
  5. set pa-se random-float 1
  6. set ea-se random-float 1
  7. set vl-se random-float 1
  8. set sp-se random-float 1
  9. set chosen-goal [&quot;GOAL 1&quot;]
  10. ;;record the initial value of each component
  11. set pa-se-initial pa-se
  12. set ea-se-initial ea-se
  13. set vl-se-initial vl-se
  14. set sp-se-initial sp-se
  15. ;; the efficacy in each individual is the sum of the four components
  16. set efficacy ((pa-se + ea-se + vl-se + sp-se) / 4)
  17. set efficacy-initial efficacy
  18. ]
  19. end

  1. to choose-goal
  2. ;; set initial goal based on SE
  3. ask individuals [
  4. if (efficacy &gt;= 0) and (efficacy &lt;= 0.25) [
  5. set chosen-goal &quot;GOAL 1&quot;
  6. move-to goal 0
  7. ]
  8. if (efficacy &gt; 0.25) and (efficacy &lt;= 0.5) [
  9. set chosen-goal &quot;GOAL 2&quot;
  10. move-to goal 1
  11. ]
  12. if (efficacy &gt; 0.5) and (efficacy &lt;= 0.75) [
  13. set chosen-goal &quot;GOAL 3&quot;
  14. move-to goal 2
  15. ]
  16. if (efficacy &gt; 0.75) and (efficacy &lt;= 1) [
  17. set chosen-goal &quot;GOAL 4&quot;
  18. move-to goal 3
  19. ]
  20. let goal-name chosen-goal
  21. ;; update efficacy values
  22. ask individuals [
  23. if chosen-goal != nobody [
  24. ;; Deduct from ea-se based on the selected goal and pa-se value
  25. if (chosen-goal = &quot;GOAL 4&quot;) and (pa-se &lt; 0.8) [
  26. set ea-se (ea-se - (ea-se * (stringencyfactor / 100)))
  27. ]
  28. if (chosen-goal = &quot;GOAL 3&quot;) and (pa-se &lt; 0.6) [
  29. set ea-se (ea-se - (ea-se * (stringencyfactor / 100)))
  30. ]
  31. if (chosen-goal =&quot;GOAL 2&quot;) and (pa-se &lt; 0.5) [
  32. set ea-se (ea-se - (ea-se * (stringencyfactor / 100)))
  33. ]
  34. if ea-se &lt; 0 [
  35. set ea-se 0
  36. ]
  37. ]
  38. ]
  39. ;; set a new goal if conditions are met
  40. ask individuals [
  41. set new-goal chosen-goal
  42. if (chosen-goal = &quot;GOAL 1&quot;) and ((ea-se / (stringencyfactor / 100)) &gt; 0.2) [
  43. set new-goal &quot;GOAL 2&quot;
  44. move-to one-of goals with [name = &quot;GOAL 2&quot;]
  45. ]
  46. if (chosen-goal = &quot;GOAL 2&quot;) and ((ea-se / (stringencyfactor / 100)) &gt; 0.2) [
  47. set new-goal &quot;GOAL 3&quot;
  48. move-to one-of goals with [name = &quot;GOAL 3&quot;]
  49. ]
  50. if (chosen-goal = &quot;GOAL 3&quot;) and ((ea-se / (stringencyfactor / 100)) &gt; 0.2) [
  51. set new-goal &quot;GOAL 4&quot;
  52. move-to one-of goals with [name = &quot;GOAL 4&quot;]
  53. ]
  54. if new-goal != chosen-goal [
  55. set current-color current-color - 1
  56. set chosen-goal new-goal
  57. ;;to check final goal
  58. ]
  59. ]
  60. ]
  61. end

  1. to layout
  2. ;; the number 3 here is arbitrary; more repetitions slows down the
  3. ;; model, but too few gives poor layouts
  4. repeat 3 [
  5. ;; the more turtles we have to fit into the same amount of space,
  6. ;; the smaller the inputs to layout-spring we&#39;ll need to use
  7. let factor sqrt count turtles
  8. ;; numbers here are arbitrarily chosen for pleasing appearance
  9. layout-spring turtles links (1 / factor) (7 / factor) (1 / factor)
  10. display ;; for smooth animation
  11. ]
  12. ;; don&#39;t bump the edges of the world
  13. let x-offset max [xcor] of turtles + min [xcor] of turtles
  14. let y-offset max [ycor] of turtles + min [ycor] of turtles
  15. ;; big jumps look funny, so only adjust a little each time
  16. set x-offset limit-magnitude x-offset 0.1
  17. set y-offset limit-magnitude y-offset 0.1
  18. ask goals [
  19. setxy (xcor - x-offset / 2) (ycor - y-offset / 2)
  20. ]
  21. end

  1. to-report limit-magnitude [number limit]
  2. if number &gt; limit [
  3. report limit
  4. ]
  5. if number &lt; (- limit) [
  6. report (- limit)
  7. ]
  8. report number
  9. end

Any recommendations are welcomed as this is my very first time doing an ABM.

Problem #1<br>
I have tried "checking" whether agents are indeed having their own personal values by inspecting the color changes. But then I realized that the problem was in the way the colors change, and not necessarily in this part (having individual SE values)

Problem #2<br>
I have tried moving the statement to the bottom, I have tried making it an if else statement. Nothing has worked.

Problem #3<br>
I have tried making current-color a turtle variable, but when I then set it in either the "to go" procedure or in the "initializing individual" procedure, it just set the color to 79 and it never changes from that.

答案1

得分: 2

  1. 这从快速查看您的代码似乎没问题。我通常进行快速检查的方法是使用 print 语句。如果在时刻1和时刻100,乌龟0具有相同的变量,那么您可以合理地确定没有人在操作它。如果在时刻100时,乌龟0和乌龟7具有相同的变量,那么您就知道您的设置出了一些奇怪的问题,等等。如果要进行更严格的搜索,您可以尝试创建一个包含所有乌龟在每个时刻的所有变量的列表,也许将其输出到Excel以便更容易进行分析。但我认为这对于这种情况来说有点过分了。

  2. 确保 go 一直运行是在界面而不是在代码中完成的。当您创建按钮时,必须选中 "永久" 复选框。

  3. 由于 current-color 是一个全局变量,任何乌龟都可以访问它,任何乌龟都可以更改它。一般来说,我强烈建议不要让乌龟直接更改全局变量,除非这正是该变量的目的。在这种情况下,您可以使用 set color color - 1,因为 color 是乌龟可以访问和更改的乌龟变量,而不会影响其他乌龟。您描述了您的一种尝试中,current-color 是一个乌龟变量,然后在 go 运行期间进行设置。从中我可以推断出,它在每个 tick 都被重置为79,这意味着即使在目标发生变化时它会减少,但之后会再次增加。

英文:
  1. This seems to be fine from a cursory look at your code. A way in which I generally do quick checks is with print statements. If at tick 1 and at tick 100 turtle 0 has the same variables, you can be reasonable sure that nobody was tinkering with it. If turtle 0 and turtle 7 have the same variables at tick 100, you know something weird happened with your setup, etc. For a more rigorous search you could try creating a list of all the variables of all turtles at each tick, maybe outputting it to excel for easier analysis. But I think that's overkill for this situation.

  2. Making sure that go keeps on running is something you do in the interface rather than in the code. When you create the button, you have to tick the "Forever" box 首次尝试NetLogo ABM – 遇到了关于程序的问题。

  3. With current-color being a global variable, any turtle can access it and any turtle can change it. In general I strongly advise against letting turtles directly change global variables, unless that is specifically the purpose of the variable. In this case, you can use set color color - 1 instead, since color is a turtle variable that they can access and change without changing anything for the other turtles.
    You describe one of your attempts had current-color a turtle variable and then setting it during go. From that I assume that it is being reset to 79 at every tick, which means that even though it will decrease when a goal changes, it is then increased again.

huangapple
  • 本文由 发表于 2023年5月25日 17:34:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76330833.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定