quaquel / pyNetLogo

BSD 3-Clause "New" or "Revised" License
81 stars 22 forks source link

Running a Netlogo file using Pynetlogo several times #77

Closed zargol7419 closed 10 months ago

zargol7419 commented 1 year ago

Hi everybody, I am using Genetic algorithm in Python to calibrate some social coefficients in NETlogo model using Pynetlogo library. But I've got an error while I was running my fitness function in python. It seems that a function involving pynetlogo modules can not be run several times. Am I right? I got this error in netlogo: image and this one in python: Traceback (most recent call last):

File NetLogoLink.java:180 in NetLogoLinkV61.NetLogoLink.command

Exception: Java Exception

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

Traceback (most recent call last):

File C:\Python27\anaconda\lib\site-packages\pyNetLogo\core.py:336 in command self.link.command(netlogo_command)

java.lang.NullPointerException: java.lang.NullPointerException

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File E:\zargolooo\Thesis\python-Thesis\4202-Transient-calib8_MODFLOW\Out_Mf2k\Fitness function.py:103 in test1 = fitness_func(solution)

File E:\zargolooo\Thesis\python-Thesis\4202-Transient-calib8_MODFLOW\Out_Mf2k\Fitness function.py:24 in fitness_func netlogo.command('go-historical-Memory-1')

File C:\Python27\anaconda\lib\site-packages\pyNetLogo\core.py:339 in command raise NetLogoException(str(ex))

NetLogoException: java.lang.NullPointerException

What should I do to solve this problem? This part is of high siginicance in my project I need to run fitness function in Genetic algorithm to calibrate my coefficients!! Thanks in advance for your wise solutions.

quaquel commented 1 year ago

This exception is identical to the one reported in #76 in quite a different context, so it seems the problem is not directly related to running an optimization. Without seeing a minimum working example of your problem, I can't judge what is going on.

Note that, as evidenced by the examples in the documentation, you can run a model multiple times without any problem.

zargol7419 commented 1 year ago

def fitness_func(solution): netlogo = pyNetLogo.NetLogoLink(gui=True) netlogo.load_model("E\farmers behavior model2.nlogo") netlogo.command('go-historical-Memory-1') subprocess.run([transter to modflow.py"]) netlogo.command('go-historical-Memory-2') netlogo.command('Go-decision-making-year1-1') subprocess.run(["python", "Modflow-Q1.py"]) netlogo.command('Go-decision-making-year1-2') netlogo.command('simulate-social-behavior') netlogo.command('Go-decision-making-year2-1') subprocess.run(["python", "E:\zargolooo\Thesis\python-Thesis\Year2.py"]) subprocess.run(["python", "Modflow-Q2.py"]) netlogo.command('Go-decision-making-year2-2') netlogo.command('simulate-social-behavior') netlogo.command('Go-decision-making-year3-1') subprocess.run(["python", "E:\zargolooo\Thesis\python-Thesis\Year3.py"]) netlogo.command('Go-decision-making-year3-2') netlogo.command('simulate-social-behavior') netlogo.command('Go-decision-making-year4-1') subprocess.run(["python", "E:\zargolooo\Thesis\python-Thesis\Year4.py"]) subprocess.run(["python", "Modflow-Q4.py"]) netlogo.command('Go-decision-making-year4-2')

RMSE calculation

netlogo.kill_workspace()

... Whenever I run this function once, it works well but when it comes to running it for the second time I get error in both python and netlogo in spite of using kill-workspace() to close current netlogo model. You know I need to implement this function several times and get RMSE for various solutions. I am sorry if there was any ambiguity in my previous comment. Could you now help me in this regard? What is the problem if I can run a netlogo file several times!

quaquel commented 1 year ago

This is hopelessly inefficient. You want to instantiate the link and load the model only once instead of for every function call. You also only want to kill the workspace at the end of all your calculations. Again, look at the examples here: https://pynetlogo.readthedocs.io/en/latest/_docs/SALib_multiprocessing.html

zargol7419 commented 1 year ago

I got the point and modified my code. Thank you so much for your help. Hopefully, It worked.