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

fromJson for an int[][] #15

Closed ChristianMalan closed 3 years ago

ChristianMalan commented 3 years ago

Hi,

Thank you for the quick feedback last time, it really helped a lot.

I am once again stuck.

I'm looking to convert the output from my python script run_vehicle_routing_sweep into an array of type int[][] in AnyLogic. In this case, the output from the python script is:

[ [-1, 0, 6, 4, 1, -1], [-1, 2, 3, 5, -1] ]

I try to convert it into something useable in AnyLogic with the following code:

String sweep_result = pyCommunicator.runResults(String.class, String.format("run_vehicle_routing_sweep(%s,%s,%s)", customerJSON, vehicle_capacityJSON, depotJSON));

traceln("Sweep results: " + sweep_result);

int[][]route_order = pyCommunicator.fromJson(sweep_result, int[][].class);

traceln("Local route order: " + route_order[0]); 

This results in the output:

Sweep results: [[-1, 0, 6, 4, 1, -1], [-1, 2, 3, 5, -1]] Local route order: [D@63d3832e

Do you have any suggestions?
I also tried to use a dictionary approach, but that gives additional errors (relating to java expecting double quotes, rather than the single quotes from the python dictionary).

I don't know if my approach is off, or if it is a bug. Either way, I would really appreciate some help.

t-wolfeadam commented 3 years ago

Glad to hear it was helpful! Regarding your current question - fortunately, there's no problem at all :)

What you're seeing is that Java arrays don't override toString function. When this happens - to any Java class - what gets printed out follows the format: class_identifier + @ + hash_code_hex (open this demo model for an example).

In the case of primitive arrays, the class identifier is a bracket for each depth and a single letter representing the data type.

If you want to print out your array to the console, you can use Arrays.toString(route_order[0]) or Arrays.deepToString(route_order) - the Arrays class is built-in, so you don't need to import anything.

Otherwise, your array is still intact! So if you did traceln(10 * route_order[0][0]); it would correctly output -10 (10 * -1)

t-wolfeadam commented 3 years ago

@ChristianMalan , I'm closing this for now, but if you have any more questions about this, feel free to ask