如何使用CSV输入创建不同代理的人口规模?

huangapple go评论57阅读模式
英文:

How can I create the population size of different agents using csv input?

问题

我有一个包含7列数据的CSV文件,表示了我的模型中不同代理的人口规模。每一行代表模型中的不同天,但现在这不是重要的。

在第一行中,我应该有每个乌龟品种各1只。

以下是您可能需要的代理分配代码:

extensions [csv]
globals [ data ]

breed[ones one]
breed[twos two]
breed[threes three]
breed[fours four]
breed[fives five]
breed[sixes six]
breed[sevens seven]

to setup
  ca
  set data csv:from-file "data.csv"
  print data
  reset-ticks

  ; 这是我想象中应该根据CSV信息分配人口规模的地方
  create-ones item 0 data
  create-twos item 1 data
  create-threes item 2 data
  create-fours item 3 data
  create-fives item 4 data
  create-sixes item 5 data
  create-sevens item 6 data
end

请注意,上述代码中的item n data表示从CSV数据中获取第n列的值,然后将其用于创建相应数量的代理。您可以根据需要进一步调整代码。

英文:

I have a csv with 7 columns that has data that represents the population sizes of different agents in my model. Each row will represent a different day in the model but that's not important right now.

ones twos threes fours fives sixes sevens
1 1 1 1 1 1 1 1
0 0 1 0 1 1 1 2
2 4 5 6 1 1 1 1
4 5 6 7 1 1 2 3

I'm not sure how to assign the values to the agents to create the populations. If you look at the first row I should have 1 of each turtle breed.

 extensions [csv]
    globals [ data ]
    
    breed[ones one]
    breed[twos two]
    breed[threes three]
    breed[fours four]
    breed[fives five]
    breed[sixes six]
    breed[sevens seven]
    
    to setup
      ca
      set data csv:from-file "data.csv"
      print data
      reset-ticks

      ; this is where I imagine I should assign the population sizes by the csv info. 
      create-ones ???
      create-twos
      create-threes
      create-fours
      create-fives
      create-sixes
      create-sevens


    end

答案1

得分: 1

以下是翻译好的部分:

一旦使用 csv:from-file 原语读取了 csv 文件,您会得到一个标准的 NetLogo 列表,其中包括每一行的子列表。因此,您需要使用标准的 NetLogo 列表原语,如 itemforeach 循环来解析数据并创建龟。

您的 csv 文件包含太多列,所以我将提供一个较小数据集的示例。假设我们有一个名为 data.csv 的文件,内容如下:

ones,twos,threes
1,2,3
1,4,9

首先,让我们尝试打印 csv:from-file 的输出,以了解 NetLogo 如何表示您的数据:

observer > show csv:from-file "data.csv"
observer : [["ones" "twos" "threes"] [1 2 3] [1 4 9]]

这显示了 csv 文件中的行和列被表示为一个列表,其中包括三个项目,每个项目本身是一个包含三个项目的列表。因此,您只需将数据视为标准的嵌套 NetLogo 列表,并使用与列表相关的原语来操作数据和检索所需的信息。

以下是我编写您打算编写的代码的方式:

extensions[csv]

globals [ data ]

breed [ones one]
breed [twos two]
breed [threes three]

to setup
  clear-all
  
  set data but-first csv:from-file "data.csv"
 
  let first-row item 0 data
  
  let num-ones item 0 first-row
  let num-twos item 1 first-row
  let num-threes item 2 first-row
  
  create-ones num-ones [ set shape "triangle" fd 10 ]
  create-twos num-twos [ set shape "square" fd 10 ]
  create-threes num-threes [ set shape "circle" fd 10 ]
  
  reset-ticks
end
  • 请注意,我使用了 but-first 原语来摆脱包含列标题的数据集中的第一项,因为我们知道数据集中变量的顺序,所以不需要第一行。
  • 如果您有一个大型数据集,您还可以使用 foreach 原语来迭代行和列,而不是为每个单元格定义单独的变量。

注意:您可以参考 NetLogo 字典以获取有关所有与列表相关的原语的更多信息:https://ccl.northwestern.edu/netlogo/docs/dictionary.html#listsgroup

英文:

Once you read a csv file with the csv:from-file primitive, you get a standard NetLogo list, which itself includes a sublist for each row. So, you would need to use standard NetLogo list primitives such as item and a foreach loop to parse your data and create your turtles.

Your csv file has too many columns, so I will provide an example with a smaller dataset. Let's assume we have a file called data.csv which looks like this:

ones,twos,threes
1,2,3
1,4,9

First, let's try to print the output of the csv:from-file to get a sense of how NetLogo represents your data:

observer > show csv:from-file "data.csv"
observer : [["ones" "twos" "threes"] [1 2 3] [1 4 9]]

This shows that the rows and the columns in the csv file are represented as a single list that includes three items, each of them a list of three items themselves. Therefore, all you need to do is to treat your data as a standard nested NetLogo list and use the list-related primitives to manipulate your data and retrieve necessary information.

Here is how I would write the code you intended to write:

extensions[csv]

globals [ data ]

breed [ones one]
breed [twos two]
breed [threes three]

to setup
  clear-all
  
  set data but-first csv:from-file "data.csv"
 
  let first-row item 0 data
  
  let num-ones item 0 first-row
  let num-twos item 1 first-row
  let num-threes item 2 first-row
  
  create-ones num-ones [ set shape "triangle" fd 10 ]
  create-twos num-twos [ set shape "square"  fd 10 ]
  create-threes num-threes [ set shape "circle" fd 10 ]
  
  reset-ticks
end
  • Notice that I used the but-first primitive to get rid of the first item in the dataset that includes column headers because we know the order of variables in the dataset, so we do not need that first row.
  • If you have a large dataset, you can also use the foreach primitive to iterate over the rows and columns instead of defining a separate variable for each cell.

Note: You can refer to the NetLogo Dictionary for more information about all the list-related primitives: https://ccl.northwestern.edu/netlogo/docs/dictionary.html#listsgroup

huangapple
  • 本文由 发表于 2023年5月17日 18:58:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定