colinsheppard / time

A NetLogo extension that brings date/time utilities and discrete event scheduling to NetLogo
12 stars 13 forks source link

Problems with scheduling several events through a list with "foreach" #40

Closed annaklabunde closed 9 years ago

annaklabunde commented 9 years ago

Hi Colin, first of all, thank you for this great extension; it has been very helpful so far. However, for several days now I have been stuck with an issue that I can't resolve. What I want to do is to read from a .csv file, which I read into Netlogo as a time-series object (this part is working fine) a schedule of which agent does what at what time. The time series-object in Netlogo looks like this:

observer: {{time:LogoTimeSeries TIMESTAMP,ID,newstate 2000-02-19 01:01:53.697,79.0,1.0 2000-04-06 03:00:20.023,95.0,4.0 2000-09-12 00:37:29.114,29.0,5.0 2001-01-22 22:05:39.811,52.0,4.0...

The first column is the time t at which an event happens, ID is an agent variable (not equal to the who number) used to identify the agent, and newstate is also an agent variable which contains the type of new state that the agent should adopt at time T.

When I just want to schedule an event for one agent, that works fine:

time:schedule-event one-of turtles with [id = time:ts-get transitions ( time:create "2008-10-21 21:45:41.889") "ID"] task [set newtransition time:ts-get transitions ( time:create "2008-10-21 21:45:41.889" ) "newstate" perform-transition ] time:create "2008-10-21 21:45:41.889"

I can also run the model with time:go; the event happens to the individual at the desired time.

Now, however, I want to do this for all the agents, so that it is automatically read from the time series at what time which agent does what. I tried doing this with a list in all kinds of ways. First I tried this: set micsimlist [] set micsimlist time:ts-get-range transitions time:create "2000-01-01 00:00:00" time:create "2010-12-31 23:59:00" "ALL"

This is the error I get: error (IllegalArgumentException) while observer running SET called by procedure SCHEDULE-EVENTS called by procedure GO called by Button 'go'

NetLogo is unable to supply you with more details about this error. Please report the problem at https://github.com/NetLogo/NetLogo/issues, or to bugs@ccl.northwestern.edu, and paste the contents of this window into your report.

java.lang.IllegalArgumentException: fromIndex(85) > toIndex(48) at java.util.SubList.(AbstractList.java:604) at java.util.RandomAccessSubList.(AbstractList.java:758) at java.util.AbstractList.subList(AbstractList.java:468) at TimeExtension$LogoTimeSeries.getRangeByTime(TimeExtension.java:366) at TimeExtension$TimeSeriesGetRange.report(TimeExtension.java:1645) at org.nlogo.prim._externreport.report(_externreport.java:50) at org.nlogo.prim._asm_procedurescheduleevents_setobservervariable_1.perform() at org.nlogo.nvm.Context.stepConcurrent(Context.java:91) at org.nlogo.nvm.ConcurrentJob.step(ConcurrentJob.java:82) at org.nlogo.job.JobThread.org$nlogo$job$JobThread$$runPrimaryJobs(JobThread.scala:143) at org.nlogo.job.JobThread$$anonfun$run$1.apply$mcV$sp(JobThread.scala:78) at org.nlogo.job.JobThread$$anonfun$run$1.apply(JobThread.scala:76) at org.nlogo.job.JobThread$$anonfun$run$1.apply(JobThread.scala:76) at scala.util.control.Exception$Catch.apply(Exception.scala:88) at org.nlogo.util.Exceptions$.handling(Exceptions.scala:41) at org.nlogo.job.JobThread.run(JobThread.scala:75)

NetLogo 5.0.4 main: org.nlogo.app.AppFrame thread: JobThread Java HotSpot(TM) Server VM 1.6.0_41 (Sun Microsystems Inc.; 1.6.0_41-b02) operating system: Windows 7 6.1 (x86 processor) Scala version 2.9.2 JOGL: (3D View not initialized) OpenGL Graphics: (3D View not initialized) model: MicsimKombi5

10:49:21.748 JobRemovedEvent (org.nlogo.app.App$$anon$1 (org.nlogo.window.GUIWorkspace)) JobThread 10:49:21.748 SwitchedTabsEvent (org.nlogo.app.Tabs) AWT-EventQueue-0 10:49:21.748 RuntimeErrorEvent (org.nlogo.app.App$$anon$1 (org.nlogo.window.GUIWorkspace)) AWT-EventQueue-0 10:49:21.670 AddJobEvent (org.nlogo.window.ButtonWidget) AWT-EventQueue-0 10:49:21.577 InterfaceGlobalEvent (org.nlogo.app.InterfacePanel$2 (org.nlogo.window.SliderWidget)) AWT-EventQueue-0 10:49:21.577 PeriodicUpdateEvent (org.nlogo.app.App$$anon$1 (org.nlogo.window.GUIWorkspace)) AWT-EventQueue-0 10:49:21.561 InputBoxLoseFocusEvent (org.nlogo.window.ButtonWidget) AWT-EventQueue-0 10:49:21.372 InterfaceGlobalEvent (org.nlogo.app.InterfacePanel$2 (org.nlogo.window.SliderWidget)) AWT-EventQueue-0 10:49:21.372 PeriodicUpdateEvent (org.nlogo.app.App$$anon$1 (org.nlogo.window.GUIWorkspace)) AWT-EventQueue-0 10:49:21.169 InterfaceGlobalEvent (org.nlogo.app.InterfacePanel$2 (org.nlogo.window.SliderWidget)) AWT-EventQueue-0

Next, I tried "LOGOTIME" instead of "ALL", which works fine: set micsimlist [] set micsimlist time:ts-get-range transitions time:create "2000-01-01 00:00:00" time:create "2010-12-31 23:59:00" "LOGOTIME" This gives me the list observer: [{{time:logotime 2000-02-19 01:01:53.697}} {{time:logotime 2000-04-06 03:00:20.023}} {{time:logotime 2000-09-12 00:37:29.114}}...

I now tried to go over this list with foreach:

foreach micsimlist [time:schedule-event one-of turtles with [id = time:ts-get transitions (time:create ? ) "ID" ] task [set newtransition time:ts-get transitions ( time:create ? ) "newstate" perform-transition ] time:create ?]

This is the error I get: Extension exception: Expected this input to be a string but got the TimeExtension$LogoTime {{time:logotime 2000-02-19 01:01:53.697}} instead. error while turtle 59 running TIME:CREATE called by (command task from: procedure SCHEDULE-EVENTS) called by procedure SCHEDULE-EVENTS called by procedure GO called by Button 'go'

Next, I tried leaving out the "time:create" before the "?". foreach micsimlist [time:schedule-event one-of turtles with [id = time:ts-get transitions ? "ID" ] task [set newtransition time:ts-get transitions ? "newstate" perform-transition ] ?]

The scheduling seems to work, at least I do not get an error. However, when I try to run the model with time:go, the error I get is:

error (ArrayIndexOutOfBoundsException) while observer running TIME:GO called by procedure GO called by Button 'go'

NetLogo is unable to supply you with more details about this error. Please report the problem at https://github.com/NetLogo/NetLogo/issues, or to bugs@ccl.northwestern.edu, and paste the contents of this window into your report.

java.lang.ArrayIndexOutOfBoundsException: 0 at org.nlogo.nvm.Task$class.bindArgs(Task.scala:27) at org.nlogo.nvm.CommandTask.bindArgs(Task.scala:65) at org.nlogo.nvm.CommandTask.perform(Task.scala:74) at TimeExtension$LogoSchedule.performScheduledTasks(TimeExtension.java:588) at TimeExtension$LogoSchedule.performScheduledTasks(TimeExtension.java:548) at TimeExtension$Go.perform(TimeExtension.java:1544) at org.nlogo.prim._extern.perform(_extern.java:54) at org.nlogo.nvm.Context.stepConcurrent(Context.java:91) at org.nlogo.nvm.ConcurrentJob.step(ConcurrentJob.java:82) at org.nlogo.job.JobThread.org$nlogo$job$JobThread$$runPrimaryJobs(JobThread.scala:143) at org.nlogo.job.JobThread$$anonfun$run$1.apply$mcV$sp(JobThread.scala:78) at org.nlogo.job.JobThread$$anonfun$run$1.apply(JobThread.scala:76) at org.nlogo.job.JobThread$$anonfun$run$1.apply(JobThread.scala:76) at scala.util.control.Exception$Catch.apply(Exception.scala:88) at org.nlogo.util.Exceptions$.handling(Exceptions.scala:41) at org.nlogo.job.JobThread.run(JobThread.scala:75)

NetLogo 5.0.4 main: org.nlogo.app.AppFrame thread: JobThread Java HotSpot(TM) Server VM 1.6.0_41 (Sun Microsystems Inc.; 1.6.0_41-b02) operating system: Windows 7 6.1 (x86 processor) Scala version 2.9.2 JOGL: (3D View not initialized) OpenGL Graphics: (3D View not initialized) model: MicsimKombi5

11:21:14.656 SwitchedTabsEvent (org.nlogo.app.Tabs) AWT-EventQueue-0 11:21:14.640 RuntimeErrorEvent (org.nlogo.app.App$$anon$1 (org.nlogo.window.GUIWorkspace)) AWT-EventQueue-0 11:21:14.640 AddJobEvent (org.nlogo.window.ButtonWidget) AWT-EventQueue-0 11:21:14.515 InputBoxLoseFocusEvent (org.nlogo.window.ButtonWidget) AWT-EventQueue-0 11:21:14.515 InterfaceGlobalEvent (org.nlogo.app.InterfacePanel$2 (org.nlogo.window.SliderWidget)) AWT-EventQueue-0 11:21:14.515 PeriodicUpdateEvent (org.nlogo.app.App$$anon$1 (org.nlogo.window.GUIWorkspace)) AWT-EventQueue-0 11:21:14.313 InterfaceGlobalEvent (org.nlogo.app.InterfacePanel$2 (org.nlogo.window.SliderWidget)) AWT-EventQueue-0 11:21:14.313 PeriodicUpdateEvent (org.nlogo.app.App$$anon$1 (org.nlogo.window.GUIWorkspace)) AWT-EventQueue-0 11:21:14.110 InterfaceGlobalEvent (org.nlogo.app.InterfacePanel$2 (org.nlogo.window.SliderWidget)) AWT-EventQueue-0 11:21:14.110 PeriodicUpdateEvent (org.nlogo.app.App$$anon$1 (org.nlogo.window.GUIWorkspace)) AWT-EventQueue-0

I also tried "time:create[0,?]" and "[0,?]"; both did not work, either.

I really don't know what I'm doing wrong. I would be extremely grateful for any kind of help. I have Netlogo 5.0.4. and Windows 7.

Best wishes Anna

colinsheppard commented 9 years ago

Hi Anna, I think I got things to run in the way you were hoping, try using this code. Note that I created a global "current-time" which I anchor to ticks and use as the way to extract the new state from the transitions time series. This is key to making things work. The way you were attempting before was breaking because the actual value of your index into "transitions" was holding a different value when the schedule was actually executed than when the events were put into the schedule.

annaklabunde commented 9 years ago

Hi Colin, thank you for answering so quickly! I tried your code and it does indeed work :) I think I also understand now what the problem was with my solution. Thank you so much!! You saved me lots of time and worry. By having created this extension and doing such great trouble-shooting you help immensely in bridging the gap between agent-based modeling and discrete-event microsimulation, which I think is awesome.
Cheers Anna

colinsheppard commented 9 years ago

Thanks Anna, I appreciate the positive feedback and I'm glad to know that this solution worked.

Best of luck with your model! Colin

On Tue Feb 17 2015 at 2:11:11 AM annaklabunde notifications@github.com wrote:

Hi Colin, thank you for answering so quickly! I tried your code and it does indeed work :) I think I also understand now what the problem was with my solution. Thank you so much!! You saved me lots of time and worry. By having created this extension and doing such great trouble-shooting you help immensely in bridging the gap between agent-based modeling and discrete-event microsimulation, which I think is awesome.

Cheers Anna

— Reply to this email directly or view it on GitHub https://github.com/colinsheppard/time/issues/40#issuecomment-74644616.