JIC-CSB / kilombo

Kilobot simulator in C
Other
43 stars 17 forks source link

Passing in parameters to kilobots from command line. #49

Closed wg15313 closed 5 years ago

wg15313 commented 5 years ago

Is there a way to pass in parameters to the Kilobot simulation from the command line.

Essentially I am wanting to pass in parameters which I plan to set as global variables for that particular simulation. I couldn't see a way to do this in the manual and I have tried passing params in from the command line, however, I can't seem to locate them within argv.

I am trying to optimise a problem by means of an evolutionary algorithm. I was planning on having a python script which would call the simulator as a subprocess, passing in a few parameters. I would then like the subprocess to return a fitness value, declaring how well that swarm performed on that particular simulation. Do you have any suggestions on how I can pass in params, which I get then set as const variables before execution?

mhinsch commented 5 years ago

There is no generic mechanism to pass through arbitrary parameters. However, you can easily change the code in the simulator to add your own parameters to be read from the parameters file. If you take a look at params.h and params.c you'll see that it's pretty straightforward to add your own parameters to the simulator. Basically add them as fields in the simulation_params struct in params.h and then read them in params.c. If you need command line parameters you will have to edit runsim.c. Hope that helps.

wg15313 commented 5 years ago

Thank you very much for your extremely helpful and swift response! Did exactly what you suggested, rebuilt the library and now have access to my custom params json object. Currently accessing those params by calling "simparams -> my_param". I shouldn't have to pass in the parameters directly via the command line, I can hopefully just edit the -p params.json file with my python script to automatically manipulate those parameters, and then I'm hoping to call the simulator as a subprocess to evolve and optimise my swarm controller.

fjansson commented 5 years ago

Just some small comments on this. I like the approach of using params.json for custom parameter passing. Besides, python is good at handling json, so automatically adding your variables to the file should be quite simple. One thing to watch out for is if your script runs many simulations in parallel, then they will each need their individual parameter file. One possibility is to create a new directory for each run.

I like this use of the simulator for an optimization problem. It's also one of the applications we had in mind. If you changed Kilombo itself for the parameter passing to work, please consider making a pull request with the change. And if you can release the whole optimizer at some point, it would of course be very nice!

amirmasoudabdol commented 5 years ago

You can also use jsonnet to systematically generate sets of JSON files for the simulator. Basically, jsonnet allows you to create a template JSON file and use command line to alternate it's parameters to generate a JSON file that you can later on pass to the simulator. Check the documentation for top-level arguments.