我实际上是在开发一种模拟方法来研究社会现象。我的想法是一群代理商选择他们附近的工厂,向前走,然后(到达时)回到初始位置。但是,我在编程时遇到了一些麻烦。一些特工刚经过其初始位置并到达了我的模拟边界,然后在此处停止。
这是我的代码:
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”作为最后一个语句。
我手动将视图重置为具有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 ]