the-anylogic-company / AnyLogic-Pypeline

A custom AnyLogic library for running Python inside an AnyLogic model (Java)
https://github.com/the-anylogic-company/AnyLogic-Pypeline/wiki
MIT License
107 stars 27 forks source link

Agent population to json issue #16

Closed ChristianMalan closed 3 years ago

ChristianMalan commented 3 years ago

Hi,

I'm back with some more questions! Thank you for the response on my previous question. I saw that it was my own mistakes rather than a bug. I really appreciate the feedback.

At the moment, I'm struggling with the details of translating an agent into a usable format in Python. Specifically, I am having issues with the parsing of certain variables. One of my agent's variable, for instance, is a Boolean variable. Python cannot interpret the false and true values contained in the Boolean variable.

I can successfully translate the agent into a json string using the toJson function. A snippet of the string is given by (the details are unimportant, I would just like to show what the variable data looks like):

{"y_home" : 55.70862345859101, "_index" : 9, "value_of_time" : 4.7033915765776175, "trip_accepted" : false, "customer_to_serve" : null, "x_home" : 40.08944843268986, "incumbent_best" : null, "$SWITCH_TABLE$vrp_first_attempt$Occasional_Driver$statechart_state" : [ 1, 2, 3, 4 ], "_group" : 5}

Thereafter, the json string is used as the input to a function written in a Python script.

However, the following issue comes up:

Exception('The following line failed to parse (as strictly Python or JSON-parsed code):

The end of the message reads as follows:

Python reason: name \'false\' is not defined\n2. JSON reason: Expecting value: line 1 column 1 (char 0)')

I want to ask if there is a way to avoid sending ALL the agent's information when performing the toJson functionality. I know in a previous version all non-essential aspects of an agent was ignored (e.g. _text or _groups). If it were possible to ignore or leave out certain variable, it would be amazing.

Alternatively, do you have another way of dealing with the boolean variable, since false or true cannot be interpreted in Python. A similar issue persists with variable with a value of null.

I hope my description explains the issue sufficiently.

Hope to hear from you soon.

Kind regards, Christian Malan

t-wolfeadam commented 3 years ago

Hello again! Please excuse my delay.

The specific JSON error is because the Python script needs to process the json to be usable. If you pass your snippet into a call to Python's json.loads function, it will output the expected dictionary.

Filtering specific groupings or variables is possible to be implemented and is a good idea, thanks! I'll look to see if there's an elegant way of doing that.

ChristianMalan commented 3 years ago

Hi!

Thank you for the response.

I got it working! Thank you so much for your support, it is greatly appreciated.

Regards, Christian