pyNetLogo icon indicating copy to clipboard operation
pyNetLogo copied to clipboard

reps in repeat_report not matching the ticks

Open feroi35 opened this issue 3 years ago • 6 comments

Hello, I have to measure the time in Netlogo and got this problem. I summed it up to keep the essential:

In Netlogo.

globals [
  time
  step-time
]

to setup
  clear-all
  reset-ticks
  set time 0
  set step-time 0.1
end

to go
  every step-time [
    set time (time + step-time)
    tick
  ]
end

So I have one tick every 0.1s. But when I use pynetlogo with this code:

import pyNetLogo
import matplotlib.pyplot as plt

netlogo = pyNetLogo.NetLogoLink(gui=True)
netlogo_file = "pb_pynetlogo.nlogo"
netlogo.load_model(netlogo_file)
netlogo.command('setup')

t = netlogo.repeat_report(['time'], 10000, go='go') 
plt.figure()
plt.plot(t)
plt.show()

netlogo.kill_workspace()

I get this graph. I'm stuck at tick 2, I don't know what does 10000 correspond to exactly.

step_time

Maybe I should put what follows in an another issue, but I tried to use another function to bypass this problem. I used :

t = netlogo.report_while(['time'], 'True', command='go')

And got the error message :

 File "C:\Users\...\lib\site-packages\pyNetLogo\core.py", line 389, in report_while
    result = self.link.doReportWhile(command, netlogo_reporter, condition, max_seconds)
AttributeError: 'NetLogoLinkV61.NetLogoLink' object has no attribute 'doReportWhile'

I thought of updating pynetlogo with the current version on Github but I got another Java error (52 can't run 59 or something like that, I don't seem it is relevant.)

feroi35 avatar Mar 28 '22 15:03 feroi35

what version of pynetlogo are you using?

Does the simple netlogo model behave as expected when run within NetLogo?

quaquel avatar Mar 28 '22 17:03 quaquel

I use pynetlogo version '0.4.2'. Yes it works as expected on Netlogo

feroi35 avatar Mar 28 '22 18:03 feroi35

Hmm... my hunch is that the every command is the problem. See http://ccl.northwestern.edu/netlogo/docs/dict/every.html. Basically, my hypothesis is that pynetlogo invokes the go function much more often than once every 0.1. second. Is there a possiblity for a sleep in NetLogo (I have never used netlogo myself)?

quaquel avatar Mar 28 '22 19:03 quaquel

Yes, it looks that you're right because I tried this on Netlogo:

to go
  every step-time [
    set time (time + step-time)
    tick
  ]
  set test (test + 1)
end

and if I plot the "test" variable, I get the line y = x. That's what the reps are. Even if I try to change the value of the tick, it keeps in memory the initial value somehow. Nevermind, I can still extract the time and the other variables I need and plot them, it should be fine. There is also a wait function indeed. I didn't try yet it yet, I'll see what's best, thanks.

However, I've seen multiple issues about the other error

AttributeError: 'NetLogoLinkV61.NetLogoLink' object has no attribute 'doReportWhile'

but I still get the error, so I'm not sure what to do about it.

feroi35 avatar Mar 28 '22 19:03 feroi35

no, it does not keep it in memory. It just skips the body of the code. every is like an if statement if I read the NetLogo docs correctly.

The AttributeError was fixed with #41 but I apparently still have to update the version on pypi.

quaquel avatar Mar 28 '22 20:03 quaquel

Ok thanks. I tried to correct it by hand by copy pasting the pyNetLogo files in the environment but got this error.

Traceback (most recent call last):
  File "org.jpype.manager.TypeManager.java", line -1, in org.jpype.manager.TypeManager.findClassByName
  File "org.jpype.manager.TypeManager.java", line -1, in org.jpype.manager.TypeManager.lookupByName
  File "java.lang.Class.java", line -1, in java.lang.Class.forName
  File "java.lang.Class.java", line -2, in java.lang.Class.forName0
  File "java.lang.ClassLoader.java", line -1, in java.lang.ClassLoader.loadClass
  File "java.lang.ClassLoader.java", line -1, in java.lang.ClassLoader.loadClass
  File "java.lang.ClassLoader.java", line -1, in java.lang.ClassLoader.loadClass
  File "sun.misc.Launcher$AppClassLoader.java", line -1, in sun.misc.Launcher$AppClassLoader.loadClass
  File "java.lang.ClassLoader.java", line -1, in java.lang.ClassLoader.loadClass
  File "java.net.URLClassLoader.java", line -1, in java.net.URLClassLoader.findClass
  File "java.security.AccessController.java", line -2, in java.security.AccessController.doPrivileged
  File "java.net.URLClassLoader$1.java", line -1, in java.net.URLClassLoader$1.run
  File "java.net.URLClassLoader$1.java", line -1, in java.net.URLClassLoader$1.run
  File "java.net.URLClassLoader.java", line -1, in java.net.URLClassLoader.access$100
  File "java.net.URLClassLoader.java", line -1, in java.net.URLClassLoader.defineClass
  File "java.security.SecureClassLoader.java", line -1, in java.security.SecureClassLoader.defineClass
  File "java.lang.ClassLoader.java", line -1, in java.lang.ClassLoader.defineClass
  File "java.lang.ClassLoader.java", line -2, in java.lang.ClassLoader.defineClass1
Exception: Java Exception

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:/Users/.../pb_pynetlogo.py", line 4, in <module>
    netlogo = pyNetLogo.NetLogoLink(gui=True)
  File "C:\Users\...\lib\site-packages\pyNetLogo\core.py", line 275, in __init__
    link = jpype.JClass(class_name[self.netlogo_version])
  File "C:\Users\...\lib\site-packages\jpype\_jclass.py", line 99, in __new__
    return _jpype._getClass(jc)
java.lang.UnsupportedClassVersionError: java.lang.UnsupportedClassVersionError: NetLogoLinkV61/NetLogoLink has been compiled by a more recent version of the Java Runtime (class file version 59.0), this version of the Java Runtime only recognizes class file versions up to 52.0

feroi35 avatar Mar 28 '22 20:03 feroi35