从 CSV 文件读取代理特征时如何避免创建过多代理

How to avoid creating an excess of agents when reading their characteristics from a CSV file

我有 100 位消费者的相关数据,这些消费者作为海龟对笔记本电脑的功能进行了评级。笔记本电脑有两种特性:屏幕尺寸和电池寿命。每个都有一些级别。例如电池寿命有5小时、12小时、24小时、30小时。数据存储在 csv 文件中。为简单起见,这里您看到 2 个消费者。

   size12  size13.5  size14  size15  Battery5  Battery12 Battery24 Battery30 
1  1        *2*         1      3        2         2           *4*       5
2  4        3           3      2        1          1           2        3

我们访问数据集以对 2 个特征级别的比率求和。例如对于消费者 1 ,什么是:

The sum of rates of screen size of 13.5 + rate of battery life 24

使用下面的代码,这是实现的:

to CalculateSumRates
  ca
  reset-ticks
  file-close-all
  file-open "turtle_details.csv"
  let headings csv:from-row file-read-line
  set screen-headings sublist headings 0 4
  set battery-headings sublist headings 4 length headings

  let screen-to-evaluate 13.5
  let battery-to-evaluate 24

  while [ not file-at-end? ] [
    let data csv:from-row file-read-line
    create-turtles 1 [
      set turtle-screen-list sublist data 0 4
      set turtle-battery-list sublist data 4 length data
      set turtle-screen-eval turtle-screen-rating screen-to-evaluate
      set turtle-bat-eval turtle-battery-rating battery-to-evaluate
      set turtle-sum-eval turtle-screen-eval + turtle-bat-eval
    ]
  ]
  file-close-all

end

to-report turtle-screen-rating [sc]
  let pos position sc screen-headings
  let turt-screen-rate-value item pos turtle-screen-list
  report turt-screen-rate-value
end

to-report turtle-battery-rating [bc]
  let pos position bc battery-headings
  let turt-bat-rate-value item pos turtle-battery-list
  report turt-bat-rate-value
end

现在我想做更多的事情。我需要考虑一个时间间隔。例如,20 年来,消费者如何改变他们对某些笔记本电脑功能的评价。为了说明更多,消费者 1 表示她的总排名为 13.5 和电池 24,在第 2 年(刻度 = 2)改进了她的笔记本电脑,所以现在我们想知道:

The sum of rates of screen size of 13.5 + rate of battery life **30**

我第一次创建我的 go 是这样的:

 to setup
      CalculateSumRates
    end


to go
repeat 20 [

   { screen-to-evaluate changes and is no longer 13.5}
   { battery-to-evaluate also changes and is no longer 24}

; 编辑

set turtle-screen-eval turtle-screen-rating screen-to-evaluate
  set turtle-bat-eval turtle-battery-rating battery-to-evaluate
  set turtle-sum-eval turtle-screen-eval + turtle-bat-eval

; 编辑

  tick
]
end

这里麻烦的是,每次调用 CalculateSumRates 时,都会转到这一行:

 create-turtles 1 [

所以每年都会从头创建 100 个消费者,而我需要在开始时监控这 100 个消费者的行为。

然后我写了 2 个 CalculateSumRates 函数,在设置中调用了一个。重命名函数并将另一个放在一边。为了不造成过多的消费者,我用 ask consumers [ 替换了 create-turtles 1 [,希望现在可以再次读取 csv,但是当我说询问消费者时会逐行读取,这样我就可以找到不同的来自数据集的值。但是,它执行得很奇怪。我不知道如何修改它以避免创建新的消费者并失去以前的消费者?

通过在编辑中添加这些行,我遇到了一个错误,告诉我不能在观察者上下文中使用 go; go 只是乌龟!! 谢谢,

为了举例说明我在上面的评论中的意思,请查看我建议的设置的修改版本

extensions [ csv ]

globals [ screen-headings battery-headings ]

turtles-own [
  turtle-screen-list
  turtle-battery-list
  turtle-screen-eval
  turtle-bat-eval
  turtle-sum-eval
  turtle-row-number
  ;; New:
  rating-each-year 
]

to setup
  ca
  reset-ticks
  file-close-all
  file-open "turtle_details.csv"
  let headings csv:from-row file-read-line
  set screen-headings sublist headings 0 4
  set battery-headings sublist headings 4 length headings

  while [ not file-at-end? ] [
    let data csv:from-row file-read-line
    create-turtles 1 [
      set turtle-screen-list sublist data 0 4
      set turtle-battery-list sublist data 4 length data
      set rating-each-year []

    ]
  ]

  file-close-all

  ask turtles [
    update-vals 12 5
    set rating-each-year lput turtle-sum-eval rating-each-year
  ]

end

它或多或少是一样的,但有一些重要的变化,比如一个名为 rating-each-year 的新列表,旨在让海龟在每个刻度中跟踪它们的评级。

报告器也基本没有变化,只是 update-vals 现在是一个特定于海龟的过程,因此它必须由 ask turtles(或类似的)调用。此外,它需要两个变量,一个称为 screen?,一个称为 battery?。然后你可以通过让一只乌龟来调用记者:update-vals 12 24,然后那只乌龟将更新它的值,屏幕尺寸为 12,电池寿命为 24。为了完整性,我包括了所有三个记者,但另一个我对你的另一个问题的回答有两个没有改变:

to update-vals [ screen? battery? ]
    set turtle-screen-eval turtle-screen-rating screen?
    set turtle-bat-eval turtle-battery-rating battery?
    set turtle-sum-eval turtle-screen-eval + turtle-bat-eval
end

to-report turtle-screen-rating [sc]
  let pos position sc screen-headings
  let turt-screen-rate-value item pos turtle-screen-list
  report turt-screen-rate-value
end

to-report turtle-battery-rating [bc]
  let pos position bc battery-headings
  let turt-bat-rate-value item pos turtle-battery-list
  report turt-bat-rate-value
end

所以现在,您的海龟可以随时根据您分配给它们或它们购买的屏幕和电池组合更新它们的总评分值,无论您是如何设置的。这是一个示例 go 过程,每个滴答让他们选择一个随机的可能屏幕尺寸和电池寿命来评估,然后他们将该总评分值添加到他们的 rating-each-year 列表中。当经过 20 个刻度时,程序停止,海龟在命令中心显示它们的列表(21 项长,因为它们也包括来自 setup 的值)。

to go
  ifelse ticks < 20 [
    ask turtles [
      let screen-this-year one-of screen-headings
      let battery-this-year one-of battery-headings
      update-vals screen-this-year battery-this-year
      set rating-each-year lput turtle-sum-eval rating-each-year
    ]
  ]
  [
    ask turtles [
      show rating-each-year
    ]
    stop
  ]
  tick 
end

在您的模型中,您当然可能不会让他们随机选择值 - 这更多是为了展示他们实际在做什么。我还应该提到 "turtle_details.csv" 与我在上一个问题中用作示例的相同。