SciPy Beginner's Guide for Optimization

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is a demonstration on how to use optimization in Python we're going to have a couple elements to our problem here first of all we have an objective and we have might have other types of constraints that were subject to such as this inequality constraint so we know that the product of those four variables x1 x2 x3 x4 has to be greater than or equal to 25 the next one is going to be an equality constraint and here the product from I equals 1 to 4 of X I squared has to equal 40 we just write that out like this and then the final thing that we have with this optimization problem is that all of the variables have to be less than or equal to 5 and greater than or equal to 1 so we have some simple bounds on the variables as well and then we have some initial conditions these are going to be the initial guessed values for optimization problem where x1 is going to be 1 X 2 is 5 and so on okay so we want to program this in to Python and be able to let's have this thing over here ok our optimization problem and then we'll go ahead and program this in using the SyFy optimize minimize package and the first thing that we'll need is you know we'll just go ahead and import numpy and also from Sify optimize we're going to import the minimize package and [Music] ok so next we're going to define some of our functions the first one is going to be our objective and we'll make that function of X and X will be a vector in on the right side there I have X 1 through 4 but in Python we start with X 0 so I'm just going to shift that down by 1 you know if you need to make a little bit more understand for yourself you could do this actually let's do that it'll be a little bit clearer here okay x3 and x4 okay so now I can just do x1 times x4 times okay I'll just add these up and then plus x3 okay so we're going to return that from our objective and then the next thing we need to do is define our constraints and the first one that we're going to do is our inequality constraint but the way that we define this is we've got to move this over to the left side and so we need to have a function of X is greater than or equal to zero so we're going to return this value and it will tell it that it's an inequality constraint so I'm going to move that over to the other side I'll define my first constraint I'll just call that constraint one okay in this case it's going to be the let's see the product of all the values okay so I'll just do x0 I'll return x0 times x1 times x2 times x3 okay minus 25 okay there's our first constraint and then our second constraint is going to be similar here but it's just going to be the summation of all the squares okay of the variables so I'm just going to do some of this squares equals so that's equal to 40 and then we'll subtract so in this case we need to for this next one let go just write that right here what we need for this one then is put everything on one side so that equals zero so what I'll do is I'll just move all of these over to that side and have it equal to zero so I'll do the sum okay and then for I in range for and you know do some this of squares equals okay so I'm of the squares minus X I squared okay and they'll return the sum squares okay so that's our our constraint 2 so we've got our problem defined and now what we need to do is just go ahead and enter down starting a new cell we need to give it some initial guesses okay so first of all I'll just say x0 equals numb by zeroes for okay we'll create a vector of four values the other way we can do that let me do this a little bit easier with a list here I'll just say that's one five five one okay so there's our initial guess right there and let's go ahead and just print our objective as a function of X zero okay so our initial gas will going to print this and our optimizer is going to try to give us a minimum objective okay so here we with those initial guesses we have an objective of 16 we're going to see if we can do better than that with satisfying the different constraints so we then need to set up some balance for our variables now the third thing is that you know x1 through x4 we have x1 is going to be less than or equal to 5 and greater than or equal to 1 and we'll copy that constraint for all of these so my bound is going to be between 1 and five okay and then we have bounds and I'll just put these four they all have the same balance the next thing we need to do is give it our constraints we need to give us some information you know we know that one is an inequality constraint the other one's inequality but we need to tell it the first type is going to be in equality I an EQ and then our function name is going to be constraint one okay now we'll do our constraint - okay this one's going to be an equality constraint and that's our second one and then I'm going to put these into a list our constraints are going to be constraint one and constraint 2 okay so we've got our bounds and our constraints defined as well I now comes the our solution is I'll put just Sol is going to be equal to minimize we're going to use the SyFy optimize minimize function and well first of all tell it that we have you know the name of our function which was objective okay there it is right there and then we need to give it our initial guesses and in this case I'm going to use method of STP method there and just because this is kind of long I'll go ahead and turn down onto the next line and then I'll say my bounds are equal to B B NDS and my constraints are equal to CO and s okay so here it is now if I enter hopefully this will solve okay and I have some invalid syntax here well you know what I needed to do an equal sign right there and let's try this one again okay so it's solve and so if I print solutions give me a bunch of stuff here about you know about the function the Jacobean message it terminated successfully number function evaluation number of iterations number of Jacobean evaluations of status success and then comes my solution most times we just want our solution values you know we had our objective function so we could print solution dot F UN okay and there's our objective function or we can print something like our solution values okay so there they are right there let's say I just wanted the first the first one I get the first one or the second lon okay okay so there you go you got the all of the solution values to that so so we've optimized this problem right here we had our objective function we had an inequality constraint a general non linear inequality constraint right here you had an equality constraint we had some bounds on our variables we gave them initial guesses we're able to minimize this objective function right here subject to all of those constraints so if you want to download this code I'm going to go to post it here on the AP modern excite just come to CG 263 and it's down here under the python side and if you come down to optimization you'll see this this is the optimization problem that we're solving here and a little bit more information method one is with do you monitor Python package and other video there and then I'll post this video and this source code there just come down here and click the get code in the bottom right ok so that's it for this tutorial on how to use an optimization solver within Python
Info
Channel: APMonitor.com
Views: 234,643
Rating: undefined out of 5
Keywords: Python, SciPy, Minimization, Optimization, Sequential Quadratic Programming, SQP, NLP, Nonlinear programming
Id: cXHvC_FGx24
Channel Id: undefined
Length: 11min 2sec (662 seconds)
Published: Fri Oct 14 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.