【问题标题】:Problems with NetLogo agents movimentationNetLogo 代理移动问题
【发布时间】:2020-03-18 20:59:07
【问题描述】:

我实际上正在开发一个模拟来研究社会现象。 我的想法是一群特工选择离他们最近的工厂,向前走并(当他们到达时)回到他们的初始位置。 但是,我在编程时遇到了一些麻烦。一些特工只是经过他们的初始位置并到达我的模拟边界,停在那里。

这是我的代码:

breed [population person] ;create the population
breed [all-fac factory] ;create the factories

população-own [
  myneighbor ;defines which factory is closer
  myNND ;calculate the distance between the person and the closer factory
  home-x ;initial x position
  home-y ;initial y position
]

to setup
  clear-all
  reset-ticks
  create-population 83 ; creates the population with 83 people in it
  [
    setxy random-xcor random-ycor ; set people initial position randomly 
    set shape "person"
    set home-x xcor 
    set home-y ycor
  ]
  create-all-fac 9
  [
    ask all-fac [ set color white] 
    ask facture 83 [ setxy 0 0 ] ; defines the initial position of every factory
    ask facture 84 [ setxy 70 0] 
    ask facture 85 [ setxy -70 0 ]
    ask facture 86 [ setxy 70 70 ]
    ask facture 87 [ setxy 0 70 ]
    ask facture 88 [ setxy -70 70 ]
    ask facture 89 [ setxy 70 -70 ]
    ask facture 90 [ setxy 0 -70 ]
    ask facture 91 [ setxy -70 -70 ]
    set shape "house"
  ]
end

to go
    move
    tick
    choose-facture

end

to choose-facture
   ask population [
    set myneighbor min-one-of other fábricas [distance myself] ; choose my nearest neighbor based on distance
    set myNND distance myneighbor
  ]
end



to move
  ask population [
  if xcor = home-x and ycor = home-y [
      face myneighbor
      fd 1
    ]
   if any? all-fac in-radius 5 [
   facexy home-x home-y 
    fd 1 ] 
    fd 1
  ]
end 

如果有人可以帮助我,我将非常感激:)。

【问题讨论】:

    标签: simulation netlogo agent


    【解决方案1】:

    亚瑟!

    您的代码几乎是正确的!

    为了保持一致,我更改了名称,以修正一些拼写错误。

    我重新排列了 go 步骤中的命令,以便它们在移动之前而不是之后检查自己的位置。通常我们将“tick”作为 go 过程中的最后一个语句。

    我手动将视图重置为 max-xcor 和 max-ycor 100,并关闭包装,因此工厂在视图内,并将补丁大小设置为 3 而不是 13,因此视图适合我的笔记本电脑屏幕。

    最后,我将某人是否在家的测试改为他们是否在距离家 5 以内,而不是他们是否完全在家——就像你在工厂使用的测试一样。

    通过这些更改,代码似乎可以按照您的意愿行事——人们一遍又一遍地步行到工厂然后回家。没有人卡在视图的边界上。

    ;; NOTE -- the view has been changed to shut off horizontal and vertical wrapping
    ;; and max-pxcor = max-pycor = 100
    ;; and patch-size = 3 pixels, not 13
    ;; so population and factory size have been increased.
    
    
    breed [population person] ;create the population
    breed [all-fac factory] ;create the factories
    
    ;;população-own [
      population-own [
      myneighbor ;defines which factory is closer
      myNND ;calculate the distance between the person and the closer factory
      home-x ;initial x position
      home-y ;initial y position
    ]
    
    to setup
      clear-all
      reset-ticks
     create-population 83 ; creates the population with 83 people in it
    
      [
        setxy random-xcor random-ycor ; set people initial position randomly 
        set shape "person"
        set home-x xcor 
        set home-y ycor
        set size 5
      ]
      create-all-fac 9
      [
        ask all-fac [ set color white] 
        ask factory 83 [ setxy 0 0 ] ; defines the initial position of every factory
        ask factory 84 [ setxy 70 0] 
        ask factory 85 [ setxy -70 0 ]
        ask factory 86 [ setxy 70 70 ]
        ask factory 87 [ setxy 0 70 ]
        ask factory 88 [ setxy -70 70 ]
        ask factory 89 [ setxy 70 -70 ]
        ask factory 90 [ setxy 0 -70 ]
        ask factory 91 [ setxy -70 -70 ]
        set shape "house"
        set size 5
      ]
    end
    
    to go
        choose-factory  ;;  moved this command up from below tick
        move
        tick
       ;; choose-factory
    
    end
    
    to choose-factory
       ask population [
        set myneighbor min-one-of all-fac [distance myself] ; choose my nearest neighbor based on distance
        set myNND distance myneighbor
      ]
    end
    
    
    
    to move
      ask population [
     ;; if xcor = home-x and ycor = home-y [   ;;<<<<<<<<<<<<<<<<<<<< replaced this test
        if (distancexy home-x home-y) <= 5 [
          face myneighbor
          fd 1
        ]
       if any? all-fac in-radius 5 [
       facexy home-x home-y 
        fd 1 ] 
          fd 1
      ]
    end 
    

    最后一个建议。你写的代码很“脆弱”,因为如果你把人数改为5这样更小的数字,为了测试,任何地方都没有工厂#83设置,代码会崩溃。

    您可能希望有一些滑块来设置人数,并希望您的代码继续为任意人数的人工作。

    另外,定位所有工厂需要很多线路。有什么好办法做到时间短,但换人数也不会断?

    这里有一些代码可以做到这一点。它在创建工厂时会列出一个工厂列表,因此它会列出它刚刚创建的 9 个工厂的编号。

    然后,它会在列表中找到那些并使用数字来定位工厂。

    它有效。这是修改后的代码部分:

     let factory-list []  ;; make an empty list
      create-all-fac 9
      [ 
        set shape "house"
        set color white
        set size 5
        set factory-list fput who factory-list  ; add this factory to the growing list
      ]  
    
      ;; OK, now we have a list of all 9 factories.
      ;; Confirm that by printing it
    
      print "here is the list of who-numbers of the new factories:"
      show factory-list
    
      ;; now we need to insert those numbers into the following commands:
    
        ask factory item 0 factory-list [ setxy 0 0 ] ; defines the initial position of every factory
        ask factory item 1 factory-list [ setxy 70 0] 
        ask factory item 2 factory-list [ setxy -70 0 ]
        ask factory item 3 factory-list [ setxy 70 70 ]
        ask factory item 4 factory-list [ setxy 0 70 ]
        ask factory item 5 factory-list [ setxy -70 70 ]
        ask factory item 6 factory-list [ setxy 70 -70 ]
        ask factory item 7 factory-list [ setxy 0 -70 ]
        ask factory item 8 factory-list [ setxy -70 -70 ]
    

    【讨论】:

    • 我真的很感激。你帮了我很多!也谢谢你的提示。我还在学习如何使用 NetLogo,所以你的建议对我来说真的很有用! XD
    猜你喜欢
    • 1970-01-01
    • 2020-08-15
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多