andim / noisyopt

Python library for optimizing noisy functions.
http://noisyopt.readthedocs.io/
MIT License
89 stars 15 forks source link

Use minimizeCompass for one variable #13

Closed montus15 closed 3 years ago

montus15 commented 3 years ago

Hello,

In the example, the objective function has two variables.

def obj(x): return (x*2).sum() + 0.1np.random.randn()

I try to apply to it on a univariate function

def obj1(x): return x*2 + 0.1np.random.randn()

I set bounds = [lb, ub] x0 = a

I got the following error: "res = minimizeCompass(obj1, bounds=bounds, x0=x0, deltatol=0.1, paired=False) Traceback (most recent call last):

File "", line 1, in res = minimizeCompass(obj1, bounds=bounds, x0=x0, deltatol=0.1, paired=False)

File "C:\Users\Julia\Anaconda3\lib\site-packages\noisyopt\main.py", line 135, in minimizeCompass np.clip(x0, bounds[:, 0], bounds[:, 1], out=x0)

IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed"

Is there a quick way to fix this issue?

Thank you very much.

andim commented 3 years ago

The quickest fix is to define both the bounds and the value as a two-dimensional array with a second dimension of length 1.

montus15 commented 3 years ago

Thank you very much for your help. In the following, I have tried to change the bounds to a 2-D array. For the value part, I am not sure how to change it.

Could you shed light on that? Thanks again for the help.

def obj1(x): return x*2 + 0.1np.random.randn()

bounds = np.array([[-2.0, 2.0]]) x0 = 1.5 res = minimizeCompass(obj1, bounds=bounds, x0=x0, deltatol=0.1, paired=False)

andim commented 3 years ago

You could do something like np.array([x0]).

As a side note: you might also want to try out the noisy bisection method, which is more taylored towards 1d problems (https://noisyopt.readthedocs.io/en/latest/api.html#bisect)

montus15 commented 3 years ago

Thank you very much for your help. It works.