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

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

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.

  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 :

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 &quot;circle&quot;
    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 &quot;&quot;
        set label-color white
        ask goal 0 [set name &quot;GOAL 1&quot;]
        ask goal 1 [set name &quot;GOAL 2&quot;]
        ask goal 2 [set name &quot;GOAL 3&quot;]
        ask goal 3 [set name &quot;GOAL 4&quot;]
        set label name
    ]
    reset-ticks
end

to go
    if ticks &gt; 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 [&quot;GOAL 1&quot;]
        ;;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 &gt;= 0) and (efficacy &lt;= 0.25) [
            set chosen-goal &quot;GOAL 1&quot;
            move-to goal 0
        ]   
        if (efficacy &gt; 0.25) and (efficacy &lt;= 0.5) [
            set chosen-goal &quot;GOAL 2&quot;
            move-to goal 1
        ]   
        if (efficacy &gt; 0.5) and (efficacy &lt;= 0.75) [
            set chosen-goal &quot;GOAL 3&quot;
            move-to goal 2
        ]  
        if (efficacy &gt; 0.75) and (efficacy &lt;= 1) [
            set chosen-goal &quot;GOAL 4&quot;
            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 = &quot;GOAL 4&quot;) and (pa-se &lt; 0.8) [
                    set ea-se (ea-se - (ea-se * (stringencyfactor / 100)))
                ]
                if (chosen-goal = &quot;GOAL 3&quot;) and (pa-se &lt; 0.6) [
                    set ea-se (ea-se - (ea-se * (stringencyfactor / 100)))
                ]
                if (chosen-goal =&quot;GOAL 2&quot;) and (pa-se &lt; 0.5) [
                    set ea-se (ea-se - (ea-se * (stringencyfactor / 100)))
                ]
                if ea-se &lt; 0 [
                    set ea-se 0
                ]
            ]
        ]

        ;; set a new goal if conditions are met
        ask individuals [
            set new-goal chosen-goal
            if (chosen-goal =  &quot;GOAL 1&quot;) and ((ea-se / (stringencyfactor / 100)) &gt; 0.2) [
                set new-goal &quot;GOAL 2&quot;
                move-to one-of goals with [name = &quot;GOAL 2&quot;]
            ]
            if (chosen-goal = &quot;GOAL 2&quot;) and ((ea-se / (stringencyfactor / 100)) &gt; 0.2) [
                set new-goal &quot;GOAL 3&quot;
                move-to one-of goals with [name = &quot;GOAL 3&quot;]
            ]
            if (chosen-goal = &quot;GOAL 3&quot;) and ((ea-se / (stringencyfactor / 100)) &gt; 0.2) [
                set new-goal &quot;GOAL 4&quot;
                move-to one-of goals with [name = &quot;GOAL 4&quot;]
            ] 
            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&#39;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&#39;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 &gt; limit [
        report limit
    ]
    if number &lt; (- 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

  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:

确定