Using to-reports while creating agents from a csv file

前端 未结 1 692
闹比i
闹比i 2021-01-24 22:21

My question is a bit long. I really appreciate it if you could please read it all and I\'d be very grateful for any piece of advice.

I have data related to 2 consumers a

相关标签:
1条回答
  • 2021-01-24 22:38

    In response to your comment- got it, thought that might be the case. I will present an alternative approach that may or may not suit you, but it's probably how I would tackle this. The main difference is that I would store the lists for screen and battery ratings in a turtle variable, so you can easily access them after the fact without having to track counters. Given these variable declarations:

    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
    ]
    

    I would split headings of the csv file to the screen-headings and battery-headings globals.

    Then, I would iterate through the next lines of the csv, as you did below, and split the lines in a similar way but into the turtles-own variables turtle-screen-list and turtle-battery-list. That way, each turtle is aware of its own ratings for each screen and battery so you can modify your evaluated screen or battery as needed.

    Set the screen and battery of interest with screen-to-evaluate and battery-to-evaluate (or use a Chooser on the interface), then use a reporter (similar to what you had set up) that checks the position of the battery and screen of interest to return the current turtle's rating for each- this will set turtle-screen-eval and turtle-bat-eval. Finally, sum those last two values.

    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
    
      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
    

    Of course, you can condense some of this as needed. I tested this setup with 4 rows (patterned after your example data) and it seemed to work ok. If you have a huge number of rows it might not work so well.

    Edit:

    If you end up replacing screen-to-evaluate and battery-to-evaluate with interface choosers (which I recommend- it allows you to quickly see the different values), you can update to the new values with something like:

    to update-vals
      ask turtles [
        set turtle-screen-eval turtle-screen-rating scrn
        set turtle-bat-eval turtle-battery-rating batr
        set turtle-sum-eval turtle-screen-eval + turtle-bat-eval
      ]
    end
    

    where scrn and batr are the names of the choosers.

    Or, if you want them to dynamically update, you can make an interface monitor that reports the following reporter :

    to-report update
    ask turtles [
        set turtle-screen-eval turtle-screen-rating scrn
        set turtle-bat-eval turtle-battery-rating batr
        set turtle-sum-eval turtle-screen-eval + turtle-bat-eval
      ]
      report "Dynamically updating."
    end
    

    With that one, as soon as you change the Chooser value, the turtles should immediately update their turtle-bat-eval, turtle-screen-eval, and turtle-sum-eval. Fun!

    Edit 2

    If your csv includes a column for row numbers like this:

    row 12 13.5 14 15  5 12 24 30
    1   1  2.0  1  3  2  2  4  5
    2   4  3.0  1  2  1  1  2  3
    

    I would recommend creating a turtle variable to store the row number used. I've now included one called turtle-row-number. Then, you would just have to include a line to update that variable from the first item in the list, which is the row number, and then modify your sublist values accordingly, to something like:

    to setup-row-nums
      ca
      reset-ticks
      file-close-all
      file-open "turtle_details_2.csv"
      let headings csv:from-row file-read-line
      set screen-headings sublist headings 1 5
      set battery-headings sublist headings 5 length headings
    
      while [ not file-at-end? ] [
        let data csv:from-row file-read-line
        create-turtles 1 [
          set turtle-row-number first data 
          set turtle-screen-list sublist data 1 5
          set turtle-battery-list sublist data 5 length data
        ]
      ]
      update-vals
      file-close-all
    
    end
    

    Where update-vals is as shown above.

    0 讨论(0)
提交回复
热议问题