第一次做NetLogo ABM - 遇到程序

问题描述 投票:0回答:1

我正在模拟一个社会环境,其中个人根据自我效能水平选择目标。
然后,这些自我效能感值会根据他们管理目标的方式进行更新。如果他们没有成功或失去继续下去的勇气,他们可以改变目标。

到目前为止我遇到了三个问题。

  1. 我希望每个人都有个人的自我价值观,这样一旦一个人被初始化并经历了选择目标、更新其功效并可能选择新目标的过程,那么它的价值观就不再受到影响。即,新代理的初始化不应更改这些值,并且新代理不应继承其前代理的值。看起来这确实是正在发生的事情吗?

  2. 我希望“go”程序继续运行,直到> 599。我尝试过实现这一点,但是当我按

    GO
    时,它一次只执行 1 个刻度。这应该很容易实现,但我的尝试都没有成功。

  3. 我希望每个智能体的默认颜色为79,然后每次智能体“交换”目标时应该扣除1。
    目前看来,current-color 是全局起点,然后随着所有智能体经历这个过程,它逐渐减少,从而继承了影响它的其他智能体的数字。

代码:

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

欢迎任何建议,因为这是我第一次进行 ABM。

问题#1
我尝试通过检查颜色变化来“检查”特工是否确实拥有自己的个人价值观。但后来我意识到问题在于颜色变化的方式,而不一定是在这部分(具有单独的 SE 值)

问题#2
我尝试将语句移到底部,我尝试将其设为 if else 语句。没有任何效果。

问题#3
我尝试将当前颜色设置为海龟变量,但是当我在“执行”过程或“初始化个体”过程中设置它时,它只是将颜色设置为 79 并且永远不会改变。

parallel-processing simulation netlogo procedure agent
1个回答
2
投票
  1. 粗略地看一下你的代码,这似乎没问题。我通常进行快速检查的一种方法是使用

    print
    语句。如果在刻度 1 和刻度 100 时,turtle 0 具有相同的变量,那么您可以合理地确定没有人在修改它。如果海龟 0 和海龟 7 在刻度 100 处具有相同的变量,则您知道您的设置等发生了一些奇怪的事情。为了进行更严格的搜索,您可以尝试创建一个包含所有海龟在每个刻度处的所有变量的列表,也许将其输出以便更轻松地进行分析。但我认为这对于这种情况来说有点过分了。

  2. 确保

    go
    继续运行是您在界面中而不是在代码中执行的操作。创建按钮时,必须勾选“永久”框

  3. 由于

    current-color
    是全局变量,任何海龟都可以访问它并且任何海龟都可以更改它。一般来说,我强烈建议不要让海龟直接更改全局变量,除非这是变量的具体用途。在这种情况下,您可以使用
    set color color - 1
    来代替,因为
    color
    是一个海龟变量,它们可以访问和更改它,而无需更改其他海龟的任何内容。 您描述了您的一次尝试,其中有一个乌龟变量的当前颜色,然后在
    go
    期间设置它。由此,我假设它会在每个
    tick
    时重置为 79,这意味着即使目标改变时它会减少,但随后会再次增加。

© www.soinside.com 2019 - 2024. All rights reserved.