同一补丁上的特工

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

在此模型中,船沿对角线移动穿过世界(不环绕,00 左下角,max-pycor 和 max-pxcor 49),20 条鱼沿相反的对角线移动。该模型的建立是为了让船对它遇到的鱼进行计数,并仔细检查每条鱼是否遇到了船。船遇不到所有的鱼也没关系

我的问题是,在某些(10-15%)运行中,尽管两个代理似乎都在同一补丁上,但相关记录不会增加,即 f 计数不增加的次数和 b 计数不增加的次数可能不会增加。我花了一些时间仔细观察代理,他们似乎处于同一补丁上,但记录并没有增加。作为 NetLogo 的初学者,我在“boats-here”、“fish-here”或“neighbors”的编码中是否存在与代理的 xcor/ycor 有关的内容?或者命令序列是问题的根源?感谢您考虑我的问题。

    breed [boats boat]
    breed [fish a-fish]

    boats-own [b-count fuel]            
    fish-own [f-count]                  

    to setup
    clear-all                          
    reset-ticks                        

    create-boats 1                
      [apply-boat-properties]           

     create-fish 20                 
      [apply-fish-properties]

    end

    to apply-boat-properties            
      ask boats
      [
        set color red
        set size 0.3
        set shape "star"
        setxy min-pxcor max-pycor
        set fuel 710
      ]
    end

    to apply-fish-properties                      
      ask fish
      [
        set color blue
        set size 0.3
        set shape "star"
        setxy random-xcor max-pycor
      ]
    end

    to go
     if (any? boats with [fuel <= 0 ])           
      [ stop ]

      ask boats                                  
      [
        follow-route1
        survey
      ]

      ask fish 
      [
        check-if-boat                            
        move
      ]

      tick

    end


    to follow-route1                             
      ask boats
    [
        facexy 49 0
        fd 0.05
        pen-down
        set fuel fuel - 1
      ]
    end


    to survey
      ifelse any? fish-here                        ;; if boat on the same patch as a fish
      [ask fish-here [                             ;; ask the fish on this patch
        if shape = "star"[                         ;; with a "star" shape
          set shape "circle"                       ;; change shape to "circle" shape
       ask boats-here                              ;; ask boat on same patch to increase b-                
      count by 1
            [set b-count b-count + 1]]
        ]
        ]
                                           ;; otherwise
          [ask fish-on neighbors [                   ;; ask fish on neighbouring 8 patches
            if shape = "circle"[                     ;; if the fish has a "circle" shape
              set shape "star" ]                     ;; change that shape to "star" shape
        ]]
      end

      to move
        ask fish
       [
          set heading 225
          fd 0.0025

        ]
      end

      to check-if-boat
        ifelse any? boats-here                             ;; if boats on same patch as the                                     
                                                               fish
        [
            ask fish-here with [color = blue]              ;; ask the blue fish to change                   
                                                            colour to yellow
          [ set color yellow
            set f-count f-count + 1]                       ;; and increase f-count by 1
        ]
        [ask fish-here with [color = yellow]               ;; otherwise ask fish with yellow 
                                                               colour to change color to blue
            [set color blue
      ] ]

      end

       to-report boat-b-count                                ;;Report count of boats with b- 
                                                             count more than 1
       report sum [b-count] of boats with [b-count > 0]
       end

       to-report fish-f-count                                ;;Report count of fish with f- 
                                                               count more than 1
       report sum [f-count] of fish with [f-count > 0]
       end

       to-report %fish-counted                               ;;Report count of fish with f- 
                                                                   count more than 1
       report (sum [f-count] of fish / count fish) * 100
       end
netlogo agent
1个回答
0
投票

游戏有点晚了,但遇到这个问题是因为我正在寻找自己问题的解决方案!

在不同代理集之间传递的逻辑是正确的,但它没有考虑到补丁上是否同时存在多条鱼。也就是说,您的代码询问是否有鱼与船位于同一块区域,但不包括 b 计数中有多少条鱼。我们用 star-fish-count 保存“星鱼”的数量,并将其添加到 b-count 而不是仅添加 1。(下面修改了调查代码)

针对初学者的 NetLogo 其他一般说明。

  • 从你的调查草稿来看,唯一的条件似乎是与船在同一块土地上是否确实存在星形鱼。前几行可以压缩为两个语句(ifany?并在这里问鱼)。

  • 每次迭代都会多次运行代理。例如,如果“go”命令具有“ask Fish [ move ]”,其中子例程“move”也要求相同的代理执行某些操作,则您将面临在一次迭代中多次运行该例程的风险。我建议要么使用 Ask 启动每个子例程,要么 您可以像以前一样在 go 中包含 Ask 命令。

事实证明,您为鱼和船设置的距离导致了“重复计算”,但如果您删除额外的循环,则通过将鱼和船向前移动相同的距离(例如,0.5),您应该获得相同的行为).

to survey
  ifelse any? fish-here with [shape = "star"][ ;;check if fish on same patch as given boat
    let star-fish-count count fish-here with [shape = "star"]
    ask boats-here [ set b-count b-count + star-fish-count]
    ask fish-here with [shape = "star"][set shape "circle"]
  ][ 
    ;; if no fish-here
    ;; change circle neighboring fish to stars
    ask fish-on neighbors [
      if (shape = "circle")[set shape "star"]
    ]
  ]
end
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.