2

Hi I am working with both the SLSQP solver on python and diffev2 which is part of the mystic package.

For multiple parameters the format shown below works:

bnds = ((0, 1e3), (0, 1e-4))
optimize.minimize(Error, [1e-8, 1e-7], args=(E, Bt, L, dt, TotT, nz, Co, Exmatrix), method = 'SLSQP', bounds = bnds)

I want to optimize only one parameter and that is when I run into the error: SLSQP Error: the length of bounds is not compatible with that of x0. I use the syntax shown below:

bnds = ((1e-9, 1e-2))
optimize.minimize(Error, [1e-8], args=(U, E, Bt, L, dt, TotT, nz, Co, Exmatrix), method = 'SLSQP', bounds = bnds)

I am not sure what is wrong, as I have only one tuple pair in the bnds variable and one guess, not sure what is wrong.

sascha
  • 32,238
  • 6
  • 68
  • 110
Michael Black
  • 21
  • 1
  • 2
  • Thank you, that worked for SLSQP but for the diffev2 solver from mystic package, it seems to be taking parameter as a sequence and producing an error inside my code saying: can't multiply sequence by non-int of type 'float'. The non integer is another parameter of value 0.5 that is multiplied with the optimized parameter. . I did not think of the `minimize_scalar` as I had just been using SLSQP with multi variables and only recently reduced the variable to one. – Michael Black Mar 28 '18 at 15:51
  • But that's not related to the question above. If the answer solves that, mark it as such and if you got a new problem, ask a new question with details! (and tag it correctly; here you missed scipy for example which is rather relevant). ```can't multiply sequence by non-int of type 'float'.``` usually is something like: you used lists, where numpy.arrays where expected (as ```[0,1,2] * 0.2``` does not work for lists; while it does for numpy). – sascha Mar 28 '18 at 15:59

1 Answers1

4

Straight from python's docs:

The trailing comma is required only to create a single tuple (a.k.a. a singleton); it is optional in all other cases. A single expression without a trailing comma doesn’t create a tuple, but rather yields the value of that expression. (To create an empty tuple, use an empty pair of parentheses: ().)

Working alternatives:

bnds = ((1e-9, 1e-2),)
bnds = [(1e-9, 1e-2)]

Internally, this happens:

bnds = ((1e-9, 1e-2))
np.array(bnds, float).shape
# (2,)

bnds = ((1e-9, 1e-2),)
np.array(bnds, float).shape
# (1, 2)

# and then N is compared to the size of the first dimension (2 vs. 1)

(And make sure you got a reason not to use minimize_scalar)

sascha
  • 32,238
  • 6
  • 68
  • 110
  • Thank you, that worked fine for the SLSQP. I will try the `minimize_scalar` function as well. I have been trying the diffev2 global solver for some of my other work, and when I try this syntax, for some reason, it seems to read my parameter as a sequence. Will investigate further and perhaps post another comment. – Michael Black Mar 28 '18 at 16:00