![](https://secure.gravatar.com/avatar/3988e7b7bafb0e62ba400204a78c93e4.jpg?s=120&d=mm&r=g)
Hi there I am very new to python but have found myself in way over my head. I am trying to perform a constrained optimization using fmin_cobyla and am running into some problems. I was wondering if you may have some examples that I could review. Especially examples that use a list of constraint functions that take inputs from the function that is being optimized. I am a novice who is just trying to learn about python and programming in general. I am working with numpy arrays and when I run my program without the optimization everything seems to run ok. However, when I include the optimization, when I multiply 2 arrays together (element wise not using dot()), I get a (ValueError: shape mismatch: objects cannot be broadcast to a single shape) error message that confuses me as I don't get it when I simply run the program and print out the results. When I exclude the optimization, and print just the array that I pass to the function, it looks like how I define it (N x N array). But when I include the optimization and print what I pass from the function before the code crashes, it no longer looks like how I define it (i.e., 1 x more then N array). Which is why I think I get the shape error. I know that I am calling the optimization wrong so I figured I would give you a snapshot of what I am trying to do, and maybe you can tell what I am doing wrong in calling the optimization. I have attached some of the code with a description of the arrays. Any advice or help you could give would be greatly appreciated. Thanks either way, joe
![](https://secure.gravatar.com/avatar/8fe55f60ba290dfa3f62a3536ede1d7c.jpg?s=120&d=mm&r=g)
joe stano wrote:
I am a novice who is just trying to learn about python and programming in general. I am working with numpy arrays and when I run my program without the optimization everything seems to run ok. However, when I include the optimization, when I multiply 2 arrays together (element wise not using dot()), I get a (ValueError: shape mismatch: objects cannot be broadcast to a single shape) error message that confuses me as I don't get it when I simply run the program and print out the results.
When I exclude the optimization, and print just the array that I pass to the function, it looks like how I define it (N x N array). But when I include the optimization and print what I pass from the function before the code crashes, it no longer looks like how I define it (i.e., 1 x more then N array). Which is why I think I get the shape error.
I haven't run your code but the failure is proabbly to passing the wrong arguments to fmin_cobyla. Usually the scalar minimizers take a 1-D array as input and expect the function to be minimized to return a single scalar value. The constraints functions too have to return a single value > 0 if the constraint is fulfilled, < 0 if not. You get a flattened version of your arrays by calling numpy.ravel(). Later you may want to reshape it too its orginal shape using numpy.reshape(). Modify your script and try again, if it still doesn't work come back here again. Some days ago I posted an example using fmin_cobyla to the list. Have a look at that, too. Regards, Christian
participants (2)
-
Christian Kristukat
-
joe stano