英文:
First time doing NetLogo ABM - encountering with procedures
问题
I can provide translations for the code-related parts of your text:
我正在建模一个社交环境,其中个体根据他们的自我效能水平选择目标。这些自我效能值然后根据他们管理目标的方式进行更新。如果他们没有成功或失去了继续的勇气,他们可以选择切换目标。
到目前为止,我有三个问题。
1) 我希望每个个体都有个人的自我效能值,因此一旦个体被初始化并经历了选择目标、更新其效能并可能选择新目标的过程,那么它的值就不再受到影响。也就是说,新代理的初始化不应改变这些值,新代理不应继承其前代代理的值。看起来是否确实发生了这种情况?
2) 我希望“go”过程保持运行,直到ticks > 599。我已经尝试实现这一点,但当我按下`GO`时,它只每次运行一次。这应该很容易实现,但我的所有尝试都没有奏效。
3) 我希望每个代理的默认颜色都为79,然后每次代理“切换”目标时都会减少1。目前似乎当前颜色是全局起点,然后随着所有代理的过程而逐渐减少,从而继承了其他代理影响它的数字。
以下是代码:
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.
-
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?
-
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. -
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 :
globals [current-color]
breed [individuals individual]
breed [goals goal]
individuals-own [
pa-se
ea-se
vl-se
sp-se
pa-se-initial
ea-se-initial
vl-se-initial
sp-se-initial
efficacy
efficacy-initial
chosen-goal
new-goal
;; setting efficacy levels
]
goals-own [
name
]
to setup
clear-all
set-default-shape turtles "circle"
set current-color 79
;; creating the four goals i the space
create-goals 4 [
set color blue
setxy random-xcor random-ycor
;; I want to move the goals towards the center, so I make them face the center patch and move towards it a bit
facexy 0 0
forward 15
set name ""
set label-color white
ask goal 0 [set name "GOAL 1"]
ask goal 1 [set name "GOAL 2"]
ask goal 2 [set name "GOAL 3"]
ask goal 3 [set name "GOAL 4"]
set label name
]
reset-ticks
end
to go
if ticks > 599 [stop]
initialize-individual
choose-goal
layout
tick
end
to initialize-individual
create-individuals 1 [
set color current-color
;; Assign variables randomly
set pa-se random-float 1
set ea-se random-float 1
set vl-se random-float 1
set sp-se random-float 1
set chosen-goal ["GOAL 1"]
;;record the initial value of each component
set pa-se-initial pa-se
set ea-se-initial ea-se
set vl-se-initial vl-se
set sp-se-initial sp-se
;; the efficacy in each individual is the sum of the four components
set efficacy ((pa-se + ea-se + vl-se + sp-se) / 4)
set efficacy-initial efficacy
]
end
to choose-goal
;; set initial goal based on SE
ask individuals [
if (efficacy >= 0) and (efficacy <= 0.25) [
set chosen-goal "GOAL 1"
move-to goal 0
]
if (efficacy > 0.25) and (efficacy <= 0.5) [
set chosen-goal "GOAL 2"
move-to goal 1
]
if (efficacy > 0.5) and (efficacy <= 0.75) [
set chosen-goal "GOAL 3"
move-to goal 2
]
if (efficacy > 0.75) and (efficacy <= 1) [
set chosen-goal "GOAL 4"
move-to goal 3
]
let goal-name chosen-goal
;; update efficacy values
ask individuals [
if chosen-goal != nobody [
;; Deduct from ea-se based on the selected goal and pa-se value
if (chosen-goal = "GOAL 4") and (pa-se < 0.8) [
set ea-se (ea-se - (ea-se * (stringencyfactor / 100)))
]
if (chosen-goal = "GOAL 3") and (pa-se < 0.6) [
set ea-se (ea-se - (ea-se * (stringencyfactor / 100)))
]
if (chosen-goal ="GOAL 2") and (pa-se < 0.5) [
set ea-se (ea-se - (ea-se * (stringencyfactor / 100)))
]
if ea-se < 0 [
set ea-se 0
]
]
]
;; set a new goal if conditions are met
ask individuals [
set new-goal chosen-goal
if (chosen-goal = "GOAL 1") and ((ea-se / (stringencyfactor / 100)) > 0.2) [
set new-goal "GOAL 2"
move-to one-of goals with [name = "GOAL 2"]
]
if (chosen-goal = "GOAL 2") and ((ea-se / (stringencyfactor / 100)) > 0.2) [
set new-goal "GOAL 3"
move-to one-of goals with [name = "GOAL 3"]
]
if (chosen-goal = "GOAL 3") and ((ea-se / (stringencyfactor / 100)) > 0.2) [
set new-goal "GOAL 4"
move-to one-of goals with [name = "GOAL 4"]
]
if new-goal != chosen-goal [
set current-color current-color - 1
set chosen-goal new-goal
;;to check final goal
]
]
]
end
to layout
;; the number 3 here is arbitrary; more repetitions slows down the
;; model, but too few gives poor layouts
repeat 3 [
;; the more turtles we have to fit into the same amount of space,
;; the smaller the inputs to layout-spring we'll need to use
let factor sqrt count turtles
;; numbers here are arbitrarily chosen for pleasing appearance
layout-spring turtles links (1 / factor) (7 / factor) (1 / factor)
display ;; for smooth animation
]
;; don't bump the edges of the world
let x-offset max [xcor] of turtles + min [xcor] of turtles
let y-offset max [ycor] of turtles + min [ycor] of turtles
;; big jumps look funny, so only adjust a little each time
set x-offset limit-magnitude x-offset 0.1
set y-offset limit-magnitude y-offset 0.1
ask goals [
setxy (xcor - x-offset / 2) (ycor - y-offset / 2)
]
end
to-report limit-magnitude [number limit]
if number > limit [
report limit
]
if number < (- limit) [
report (- limit)
]
report number
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
-
这从快速查看您的代码似乎没问题。我通常进行快速检查的方法是使用
print
语句。如果在时刻1和时刻100,乌龟0具有相同的变量,那么您可以合理地确定没有人在操作它。如果在时刻100时,乌龟0和乌龟7具有相同的变量,那么您就知道您的设置出了一些奇怪的问题,等等。如果要进行更严格的搜索,您可以尝试创建一个包含所有乌龟在每个时刻的所有变量的列表,也许将其输出到Excel以便更容易进行分析。但我认为这对于这种情况来说有点过分了。 -
确保
go
一直运行是在界面而不是在代码中完成的。当您创建按钮时,必须选中 "永久" 复选框。 -
由于
current-color
是一个全局变量,任何乌龟都可以访问它,任何乌龟都可以更改它。一般来说,我强烈建议不要让乌龟直接更改全局变量,除非这正是该变量的目的。在这种情况下,您可以使用set color color - 1
,因为color
是乌龟可以访问和更改的乌龟变量,而不会影响其他乌龟。您描述了您的一种尝试中,current-color 是一个乌龟变量,然后在go
运行期间进行设置。从中我可以推断出,它在每个tick
都被重置为79,这意味着即使在目标发生变化时它会减少,但之后会再次增加。
英文:
-
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. -
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 -
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 useset color color - 1
instead, sincecolor
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 duringgo
. From that I assume that it is being reset to 79 at everytick
, which means that even though it will decrease when a goal changes, it is then increased again.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论