mrsonandrade / pyswarming

A research toolkit for Swarm Robotics.
https://pyswarming.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
21 stars 3 forks source link

Auto-Differentiation Module #7

Closed JHartzer closed 1 year ago

JHartzer commented 1 year ago

The module auto_differentiation.py seems to add significant complexity to the package without much benefit since it is only used for the gradient calculation. At the moment, it doesn't seem clear that to use the geofencing function with more complex functions, those functions must be imported from auto_differentiation e.g. from pyswarming.auto_differentiation import sin, cos, erf instead of using the existing math or numpy functions. A user (such as myself) might attempt the following and be confused by the result

>>> import numpy as np
>>> f = lambda x,y,z: np.sqrt(x+y+z)
>>> geofencing( (1,2,3), f)
array([nan, nan, nan])

Would it make more sense to utilize numeric gradient calculation to improve the clarity of what is being done and allow users to create even more complex functions using other libraries? I think that would produce results with all the accuracy needed for the behaviors without needlessly increasing complexity and limiting users in the construction of their geofencing functions.

mrsonandrade commented 1 year ago

@JHartzer thank you for the observation! I looked here and I have found the numdifftools package. I will make some tests and if it works as expected I will migrate to it 😃 As you said, I think that this will improve the clarity of what is being done.

mrsonandrade commented 1 year ago

Hi @JHartzer! Thank you for the suggestion 😃 I have integrated numdifftools for the gradient calculation. Now you can use it with something like yours example:

>>> import numpy as np
>>> import pyswarming.behaviors as pb
>>> f = lambda x: np.sqrt(x[0]+x[1]+x[2])
>>> pb.geofencing(np.array([1,2,3]), f)
array([-0.53146485, -0.53146485, -0.53146485])