我根据从文件中读取的值设置补丁。值位于列表列表中:
[[10001 53 1 2 160 4 4 4 1] [10004 69 1 2 143 4 4 4 2] [10005 70 2 2 135 3 3 4 2] [10006 51 2 2 132 4 4 3 3] ... ]
每个"内部" list具有补丁属性的值
位置2中的值(I' m从0开始计算第一个值)表示性别:
1
=男性,2
=女性。
我的程序:
to setup-patches-as-agents [ patches-data ]
set male-count 0
set female-count 0
set total-rows 0
foreach patches-data [
[ one-row ] ->
ask patches with [ pxcor = random-pxcor and pycor = random-pycor]
[ set total-rows total-rows + 1
;; add values to patch properties
;; **** id ****
set p-midus_id item 0 one-row
;; **** age *****
set p-age item 1 one-row
;; gender 1= MALE; 2= FEMALE; 7= DON'T KNOW; 8= REFUSE
set p-gender item 2 one-row
if item 2 one-row = 1 ;; male
[ set pcolor blue
set male-count male-count + 1 ] ;; male
if item 2 one-row = 2 ;; female
[ set pcolor orange
set female-count female-count + 1 ] ;; female
;; **** lose10lb ****
set p-lose10lb item 3 one-row
;; **** weightYrAgo ****
set p-weightYrAgo item 4 one-row
;; **** limitLiftCarryGroceries ****
set p-limitLiftCarryGroceries item 5 one-row
;; **** limitClimbingStairs ****
set p-limitClimbingStairs item 6 one-row
;; **** limitWalkingSeveralBlocks ****
set p-limitWalkingSeveralBlocks item 7 one-row
;; **** limitWalkingSeveralBlocks ****
set p-limitWalkingSeveralBlocks item 8 one-row
]
]
type "male-count: "
print male-count
type "female-count: "
print female-count
type "total-count: "
print total-rows
end
读取的总行数为:6325(读取文件后输出,与文件中的行数相匹配) 男性总人数应为:3004,女性总数:3321。我对这些变量的计数变化,包括"行的总数"
知道为什么吗?
答案 0 :(得分:2)
很高兴你找到了解决方法!只是为了得到我的两分钱,我认为你的解决方案指出了你的第一个代码可能存在的问题 - 你的
ask patches with [ pxcor = random-pxcor and pycor = random-pycor]
没有从补丁池中删除具有指定值的补丁 - 也就是说您可以多次选择补丁,因此每次选择补丁时都会覆盖其初始值。因为在你的turtles
解决方案中,你为每一行创建了一只新的乌龟,所以没有覆盖发生。
如果您需要将其作为补丁而不是海龟,则可以将with语句更改为仅选择尚未分配值的补丁。例如(假设您的设置之前没有设置p-gender),您可以利用以下事实:所有声明的变量默认设置为0,直到它们在代码中更改并将原始代码更改为类似
...
foreach patches-data [
[ one-row ] ->
ask one-of patches with [ p-gender = 0 ] [
...
在同一过程中你可以使用one-of
而不是手动选择随机pxcor
和pycor
- 我认为这更简单。
此外,这显然取决于您,而不是手动递增male
和female
的计数器,您可能只想设置一个to-report
过程来监控这些您。例如,如果您的代码中包含此内容:
to-report male-turtle-count
report count turtles with [ gender = 1 ]
end
然后你print male-count
(或在界面上使用显示器),它会显示当前性别为1的海龟数量。这会动态更新,因此您无需跟踪一个柜台。
答案 1 :(得分:0)
我切换到海龟并设置所有属性,包括初始坐标,现在我得到正确的计数。我怀疑坐标选择补丁的条件可能是一个问题,但我不确定。无论如何,这是我的解决方案:
to setup-turtles [ turtles-data ]
foreach turtles-data [
[ one-row ] ->
crt 1 [
set shape "square"
setxy random-xcor random-ycor
set size 2
;;(item 0 one-row) and ycor = (item 1 one-row) ] [
;; **** id ****
set midus_id item 0 one-row
;; **** age *****
set age item 1 one-row
;; gender 1= MALE; 2= FEMALE; 7= DON'T KNOW; 8= REFUSE
set gender item 2 one-row
if item 2 one-row = 1 ;; male
[ set color blue ] ;; male
if item 2 one-row = 2 ;; female
[ set color orange ] ;; female
;; **** lose10lb ****
set lose10lb item 3 one-row
;; **** weightYrAgo ****
set weightYrAgo item 4 one-row
;; **** limitLiftCarryGroceries ****
set limitLiftCarryGroceries item 5 one-row
;; **** limitClimbingStairs ****
set limitClimbingStairs item 6 one-row
;; **** limitWalkingSeveralBlocks ****
set limitWalkingSeveralBlocks item 7 one-row
;; **** physicalFitnessCompared5YrAgo ****
set physicalFitnessCompared5YrAgo item 8 one-row
]
]
end