Use ray in the scipy optimze

I wonder how to index the numpy array after ray.put because ray_id does not allow indexing.

This is the MWE

import ray
ray.init()

def ray_func(x):
    x_id = ray.put(x)
    result = (x_id[0] - 1)**2 + (x_id[1] - 2.5)**2
    return ray.get(result)

from scipy.optimize import minimize
import numpy as np
res = minimize(ray_func, np.array([2, 0]), method='SLSQP', options={'disp': True})
res

I have the error of

TypeError                                 Traceback (most recent call last)
<ipython-input-3-9d94b5a82fa4> in <module>()
      1 from scipy.optimize import minimize
      2 import numpy as np
----> 3 res = minimize(ray_func, np.array([2, 0]), method='SLSQP', options={'disp': True})
      4 res

3 frames
<ipython-input-2-4d060a93cf8c> in ray_func(x)
      4 def ray_func(x):
      5     x_id = ray.put(x)
----> 6     result = (x_id[0] - 1)**2 + (x_id[1] - 2.5)**2
      7     return ray.get(result)

TypeError: 'ray._raylet.ObjectRef' object is not subscriptable

Once you put the object into ray.put it’ll become an object, and you need get to get the actual value of this. May I know what do you want to do here? Probably change it to this?

@ray.remote
def helper(x_id):
    return (x_id[0] - 1)**2 + (x_id[1] - 2.5)**2

def ray_func(x):
    return ray.get([helper.remote(x)])[0]