simulation - NetLogo 代理移动问题
问题描述
我实际上正在开发一个模拟来研究社会现象。我的想法是一群特工选择离他们最近的工厂,向前走并(当他们到达时)回到他们的初始位置。但是,我在编程时遇到了一些麻烦。一些特工只是经过他们的初始位置并到达我的模拟边界,停在那里。
这是我的代码:
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
如果有人可以帮助我,我将非常感激:)。
解决方案
亚瑟!
您的代码几乎是正确的!
为了保持一致性,我更改了名称,以修复一些错别字。
我重新排列了 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 ]
推荐阅读
- java - 从 Spring 初始化中删除 JNDI
- python - 从确实使用 BeautifulSoup python 中获取前 100 个工作结果
- javascript - javascript mysql插入多个值
- superclass - OptaPlanner 可以使用超类约束吗?
- java - 一个对象依赖gc根对象,比如一个线程字段,这个对象的gc是什么?
- android - 使用 screenSlider 和 Fragment 自动调整 TextView
- python-3.x - 在 tkinter 中启用命令菜单
- python - 如何将 100 个值插入每两个值?
- shell - 导出命令在 Jenkins groovy 中不起作用
- javascript - 获取 var 的数据时可以忽略吗?