0

I've got a function g which I'm minimizing over an array coeffs subject to a list of constraints cons.

If I use

n = 2 
cons2 = [{'type': 'eq', 'fun': lambda cts: 1 - sabs(apNn(n,n,np.roll(cts, - 0 * 2**(n + 1))))},
            {'type': 'eq', 'fun': lambda cts: 1 - sabs(apNn(n,n,np.roll(cts, - 1 * 2**(n + 1))))}]
ans = scipy.optimize.minimize(g, coeffs, constraints=cons2)

then everything works fine. (sabs and apNn are functions I've defined previously.).

However if I generate the constraints in a loop and use:

cons = []
for v in range(2):
    cons.append({'type': 'eq', 'fun': lambda cts: 1 - sabs(apNn(n,n,np.roll(cts, - v * 2**(n + 1))))})
ans = scipy.optimize.minimize(g, coeffs, constraints=cons)

then the optimization fails with message Singular matrix C in LSQ subproblem. Given that the for loop is generating exactly the same thing I can't see where this difference is coming from.

cel
  • 30,017
  • 18
  • 97
  • 117
JGM272
  • 11
  • 2
  • "Given that the for loop is generating exactly the same thing": it isn't, though-- the value of `v` is only looked up when the function is called. The answer to the linked question explains what's going on and the usual workarounds. – DSM Dec 17 '14 at 13:14
  • Awesome, thank you very much! Would never have got that on my own. – JGM272 Dec 17 '14 at 14:11

0 Answers0