alexblaessle / constrNMPy

A Python package for constrained Nelder-Mead optimization.
GNU General Public License v3.0
11 stars 2 forks source link

Error using the optimizer #2

Open pranshrana opened 6 years ago

pranshrana commented 6 years ago

Hi, I just installed the constrNMPy library and was trying out the examples. But I get the following error:

C:\Users\User\Desktop\constrNMPy-master\constrNMPy\examples\beales.py Traceback (most recent call last): File "C:\Users\User\Desktop\constrNMPy-master\constrNMPy\examples\beales.py", line 18, in res=cNM.constrNM(cNM.test_funcs.beales,x0,LB,UB,full_output=True) File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\constrnmpy-0.1-py3.6.egg\constrNMPy\constrNMPy.py", line 60, in constrNM if not ((x>LB[i] or LB[i]==None) and (x<UB[i] or UB[i]==None)): TypeError: '<' not supported between instances of 'int' and 'NoneType'

What should I do?

alexblaessle commented 6 years ago

Hi,

First of all, sorry for the late reply. I am travelling right and only have sporadic internet access.

I didnt try running constrnmpy with python3 so far, and thus never encountered the problem. Could you try running the same example using py2.7 and see if this is actually a py version problem. Otherwise, we would need to add an extra test case just testing if UB is None.

Cheers,

Alex

On Thu, Mar 29, 2018, 10:35 AM pranshrana notifications@github.com wrote:

Hi, I just installed the constrNMPy library and was trying out the examples. But I get the following error:

C:\Users\User\Desktop\constrNMPy-master\constrNMPy\examples\beales.py Traceback (most recent call last): File "C:\Users\User\Desktop\constrNMPy-master\constrNMPy\examples\beales.py", line 18, in res=cNM.constrNM(cNM.test_funcs.beales,x0,LB,UB,full_output=True) File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\constrnmpy-0.1-py3.6.egg\constrNMPy\constrNMPy.py", line 60, in constrNM if not ((x>LB[i] or LB[i]==None) and (x<UB[i] or UB[i]==None)): TypeError: '<' not supported between instances of 'int' and 'NoneType'

What should I do?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/alexblaessle/constrNMPy/issues/2, or mute the thread https://github.com/notifications/unsubscribe-auth/AQI2T6Fd2jXqASTO9jTWvLkagrzuxkbsks5tjQ1ggaJpZM4TAoe7 .

klankschap commented 6 years ago

if not ((x>LB[i] or LB[i]==None) and (x<UB[i] or UB[i]==None)):

you cannot compare an int with a None, so if UB[i] is None, the comparison x < UB[i] will fail if x is an int. As the expression is resolved from left to right, you could reorder the expression, placing the None check before the comparison

In [1]: a = 10 In [2]: b = None

In [3]: a < b

TypeError Traceback (most recent call last)

in () ----> 1 a < b TypeError: '<' not supported between instances of 'int' and 'NoneType' In [4]: b is None or a < b Out[4]: True In [5]: a < b or b is None --------------------------------------------------------------------------- TypeError Traceback (most recent call last) in () ----> 1 a < b or b is None TypeError: '<' not supported between instances of 'int' and 'NoneType'
alexblaessle commented 6 years ago

I fixed the issue by introducing a primary check for None and then checking for boundaries. I hope this fixes the problem. Can you please report back if you still encounter the bug or not so I can close the issue?

klankschap commented 6 years ago

A.

On 5 Jun 2018, at 10:05, Alexander Blaessle notifications@github.com wrote:

I fixed the issue by introducing a primary check for None and then checking for boundaries. I hope this fixes the problem. Can you please report back if you still encounter the bug or not so I can close the issue?

Could you explain as what the intent of these lines (162-165)of code is?

Add offset if necessary to avoid singularities

for l in LB: if l == 0: l = l + offset

as it is now, it is a NOP, e.g. it will not have effect on the data.

How about making the code a bit more pythonic? e.g. instead of manipulating indexes of arrays, just go over the array data themselves.

# Check if x0 is within bounds
for x, lb, ub in zip(x0, LB, UB):
    if not ((lb == None or x > lb) and (ub == None or x < ub)):
        errStr = "Initial guess x0 = {} out of bounds {}..{}".format(x, lb, ub)
        raise ValueError(errStr)

I could be wrong, but isn’t the transformX function the same as:

def transformX(x, LB, UB): x = np.asarray(x, dtype=np.float64) xtrans = [] for xx, lb, ub in zip(x, LB, UB): if ub != None: if lb == None:

Upper bound only

            xtrans.append(ub - xx ** 2)
        else:
            # Both bounds
            xtrans.append(max(lb, min(ub, (np.sin(xx) + 1.) / 2. * (ub - lb) + lb)))
    elif ub == None
        if lb != None:
            # Lower bound only
            xtrans.append(lb + xx ** 2)
        else:
            # No bounds
            xtrans.append(xx)
return xtrans

Also, as the data is presented in numpy arrays, you might benefit if you could reformulate the calculations in broadcast / mask operations.

Best regards .F