Virtual coach functionality


#1

Hey,

I was wondering if there is a way to get the run time and simulation time while running an experiment through the virtual coach.

Thanks in advance for your help.

Kind regards,

Carlos


#2

What is your use case? Do you want to run the experiment only for a specific amount of time and then terminate/restart?


#3

Hi,

The end goal is what you said, to run an experiment for a certain time and then terminate it. But it would be nice to see how fast the time goes, in terms of the real time and in the simulation time.

Thanks in advance.

Kind regards,

Carlos


#4

Chipping in here as I recently started using Virtual Coach as well and had the same problem.

I have now found a solution that works very well for me in Jupyter Notebook and I have put the code here if it should be helpful to anyone:
https://pastebin.com/xnd8NeDC

How I solved the problem of getting simulation time was through rospy.Time, and running an experiment for a given amount of time was by running a loop that checks rospy.Time.now() every 3 seconds.
I don’t know if this is a good way to do it but I didn’t find a better alternative.
However, if anyone has a better way I’m all ears :slight_smile:

Here is a quick overview what my script does:

  1. Logs on to NRP local platform
  2. Establishing settings for how long to run experiment and which experiment
  3. Clones experiment
  4. Launches experiment
  5. Starts simulation, checking both wall time and simulation time
  6. Runs simulation for given amount of seconds (sim time) while showing a progress bar
  7. Pauses simulation
  8. Saves out CSV files
  9. Copies all simulation data into a data_package (custom tools that are not included in this paste)
  10. Stops simulation
  11. deletes experiment from local server
  12. Prints out time stats for simulation

#5

I did it differently. There’s a function in the Virtual Coach Simulation module called register_status_callback() where you can register a callback to be called whenever a new simulation status message is received. You can then query the status message for the the simulation time, as well as for other information.

For example, I would save all status messages in an array by defining a callback function within a class like this

 def on_status(self, status):
       self.last_status[0] = status

and define another function that takes a condition and a timeout, and check if the condition has been satisfied withing this timeout

def wait_condition(self, timeout, condition):
    start = time.time()
    while time.time() < start + timeout:
        time.sleep(0.25)
        if condition(self.last_status[0]):
            return
    raise Exception('Condition check failed')

In the main loop I would then use these functions like that

self.sim.register_status_callback(self.on_status) # this one before the loop
for i in range(num_episodes):
    self.sim.start()
    self.wait_condition(1000, lambda x: x['simulationTime'] > 40)
    self.sim.pause()
    self.save_simulation_data(i)
    self.sim.reset('full')
    self.wait_condition(100, lambda x: x['state'] == 'paused' and x['simulationTime'] == 0)

The last function call is to make sure that the reset operation was complete, as the amount of time needed to reset an experiment varies from one experiment to another depending on the number of models for example.

I am currently rewriting the Virtual Coach Tutorial in the Guide Book and will make sure to include a section on how to run the simulation for a specific amount of time.

As for the real time issue, you can just import time in your python module and mark the time the simulation started at sim_start_time = time.time() right after starting the simulation and then calculate the difference of the current time from that marked time in the future.


#6

That’s great! thanks @mahmoudakl ,

I did see the register_callback function, but I couldn’t find any examples of how to implement it, so I went with a more ‘hacked’ version. I didn’t understand how to implement your code snippet in the current Guide, so I am looking forward to your rewritten Guide!

To extend it a little (and sorry for hijacking the thread)

  • In your diagram, what is the method (procedure) to induce parameters into your experiment for each new mutation? I guess its the step you call “Parameterize Brain”?

Would you simply get the Brain model / Transfer Functions, edit them and save them back into the experiment and run again, or is there another (smarter) way?
Would you recommend making a ‘settings’ file that are imported + reloaded in the various TF’s and Brain models and then you edit that settings file for each re-run of the experiment? (forced ‘reload’ because it seems that imported modules in TF’s and Brain models are not automatically being reloaded, when re-running an experiment, even though TF files and Brain models are being reloaded themselves?)

Any pointer of an efficient/easy way to do this would be greatly appreciated

Thanks in advance


Best Practices NRP Development Workflow
#7

Yeah I save the brain code in a string and then modify the string and update the brain with the edit_brain() function. Currently there is no smarter way unfortunately. Same goes for Transfer Functions.

The imported settings could be an option, but I didn’t play around with that. I would be interested in exploring this option once I’m done updating the guide.

Please let me know also if you would like to have more Virtual Coach features.


#8

To be more specific, this line of code in my brain file is how I paramterize the brain each run.

dna = np.array([int(x) for x in '%s'.split(',')]).reshape(10, 29)

Based on the encoding in this array I create connections between neurons.