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

时间:2017-07-14 03:39:07

标签: csv netlogo consumer agent-based-modeling

我有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函数,在设置中称为一个函数。重命名该功能并将其他功能放在一边。为了不创造过多的消费者,我用create-turtles 1 [替换ask consumers [,希望现在再次读取csv,但当我说询问消费者时,会逐行读取,所以我可以找到不同的来自数据集的值。然而,它正在执行奇怪。我不知道如何修改它以避免创建新的消费者并丢失以前的消费者?

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

1 个答案:

答案 0 :(得分:3)

要举例说明我在上述评论中的含义,请查看我建议的here设置的修改版本。

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”与我在上一个问题中用于示例的那个相同。