Solve Optimization Problems with Lagrange Multipliers in Python
Key Takeaways
This video demonstrates how to solve optimization problems with constraints using Lagrange multipliers in Python, covering the basics of optimization and constraint handling in Python.
Full Transcript
what is going on guys welcome back in this video today we're going to learn how to solve optimization problems with constraints using lrange multipliers in Python so let us get right into [Music] ited all right so this video today is going to be a little bit more mathematical than usual we're going to learn how to use LR multipliers in Python to solve optimization problems with constraints and I want to show you what this means visually I want to show you what it looks like so you get an intuition of what we're doing here let's say we have some function that we want to find a minimum or maximum for so let's say min max off and we can do something very simple like X squ in this case we only have a minimum uh as you can see here a global minimum we can do the same thing for something like 2 * x^2 - 2 * y 2quared for example I think this doesn't have a global Optimum uh but the goal is not to just find the op Optimum for a given function we want to also have a constraint we want to have a condition for example in this case I could say something like I want to have the minimum and maximum of this function given this condition given this constraint that X to the^ of 2 plus uh y ^ of 2 is equal to 2 for example this would now be a circle and this basically means um that I'm looking for the minimum and maximum at these points here so I'm looking at these uh at this specific line here which is a circle and I'm only interested in points that lie on this line so don't confuse this to be the area it's not the area it's points on this line so they have to be on here and you can see that all the values that we find here are on the line uh maybe to make this a little bit clearer we can use something else let's go with something like X to the^ of 3 um Min - y or - 3 y to the power of 3 that should be a line not a circle uh actually we need to say equals something equals 5 for example um and this will also give us a result uh but you can see now that this is a line so it's not the area we're looking for the lowest and highest point on this line that's the basic idea here so this is what we want to solve and how you do that I don't want to go too much into the mathematics here because I'm not a mathematician I'm a computer scientist I do understand how it works to some degree but I'm not here to explain to you the concept or the theory behind lrange multipliers there are a lot of good videos on YouTube that you can watch for that I want to show you how to solve this in Python easily so you basically solve this in Python the same way you solve it uh by hand but you do it in an automated way so you don't have to to do all the calculations yourself we're going to use a package called simpai for this so we're going to open up the terminal and we're going to install the simpai package and um this is basically a package that allows us to do symbolic mathematics in Python so we're going we're going to say import sypi SSP and then we're going to basically uh State the function that we want to optimize then a second function that is basically our um our constraint and then we're going to take the derivative of the combined function using a Lambda uh which we're going to multiply with the um with the constraint and then we're going to solve the system for equations that comes from setting all the partial derivatives to zero so we're going to start by saying we want to have the c a couple of certain symbols let's go with x y and Lambda I'm going to just use an L here x y and Lambda are going to be SP symbols and we're going to pass your x y and L and now we can use them as variables in mathematics so I can say that my function is for example uh 2 * x² + 2 * or actually minus 2 * y^ 2 and uh this is basically the same thing we had in in wolf from alpha so let's go back to our constraint x^2 + y^2 = 2 now the tricky thing here it's actually not tricky but the thing that you need to do here is if you want to specify the constraint you have to set it equal to zero so you basically have to subtract by two in this case so that the constraint is actually x^2 + y^2 - 2 = 0 because you need to multiply the expression it's not an equation it's actually a function you need to multiply it with Lambda um so we're going to say G our constraint is going to be uh that x^ 2 + y ^ 2 - 2 is equal to 0 but we're not going to specify is equal to zero this is implicit here because now to get the final function we're actually going to take the partial derivatives of uh large F uppercase F we're going to say f plus Lambda time G um this is basically how you do that with your uh with the lrange multiplier so we have basically our function and then we want to multiply it we want to add to it Lambda multiplied with G which is our uh constraint and now all we have to do is we have to compute FX FY and F Lambda so the partial derivatives with respect to x y and Lambda which we can do also with simpai by saying SP diff so take the derivative of f with respect to X and then just copy this to be the same for y and for Lambda for y and for Lambda there you go and all we have to do now is we have to solve the system of equations because this basically now is going to be set to zero if we don't specify any additional thing so we're going to basically have the the derivative with respect to x y and Lambda set to zero and then we just need to solve the system of equations to get the results for x y and Lambda and Lambda is just basically a helper tool we could say because the only thing we're interested in is actually X and Y so the value of Lambda is important for the calculations for the solving of the system but it's not really relevant to us uh once we have the results so we're going to say Solutions is equal to SP solve and we're going to pass here FX FY f l and we're interested in x y and L there you go and then we can print the solutions so I can run this and you can see that our Solutions are the same that we get here from um well from alpha we have all the minimums and all the maximums here in uh in the results so we can also do the same thing with three variables for example we can also have something like XYZ and then we have for example the function xus uh y + z squared now this is hard to visualize because now we have three input values and then we have one output which is four dimensional so uh we're not going to be able to easily visualize that uh but we can go ahead now and say that we want to do something quite similar we want to say plus Z to the^ of 2 minus 2 for example this would now be a um it would now be like like a ball basically and uh but it would only be the the outside of it so it would not be everything in this sphere but it would be um what is it the not circumference the the surface of the sphere is uh what we're looking at here in this four-dimensional space so we're going to do the same thing here the only thing that we need to add is an FZ and we need to say derivative with respect to Z then we need to say f z and we're also interested in Z and when I run this we get a problem because we only have three symbols here so let me Define Z as well run this again and there you go we now have all the optimums all the extreme values for this particular optimization problem now we can also go ahead and add multiple conditions we can go ahead and say for example uh let's go with a different function that I have prepared here three * x - y uh - 3 * z um and now we have two conditions the first one is going to be x + y - Z is equal to zero so just like that and then the second one H is going to be equal to x^2 + 2 * z^ squared is equal to 1 so minus one and to now use both of these constraints all we have to do is we have to Define two lambdas so we can say L1 L2 L1 L2 and then we just say uh L1 * G + L2 * H and all we have to do then is we have to say we want to have F L1 FL L2 L1 L2 F1 FL 2 L1 L2 there you go run this and we get the results easily so this is how you can do that easily in Python now to find out whether these are SLE points or uh maximums or minimums you can always go with the hessan and you can compute uh uh basically uh you can use the hesan to to see if this is a minimum maximum or settle Point uh or you can just plug in some values and and find out by by testing but this is how you can EAS easily use lrange multipliers to solve optimization problems with constraints in Python so that's it for today's video I hope you enjoyed it and hope you learn something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you in the next video and bye
Original Description
Today we learn how to solve optimization problems with constraints using Lagrange multipliers in Python.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
🐍 The Python Bible Book: https://www.neuralnine.com/books/
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
💼 Services 💼
💻 Freelancing & Tutoring: https://www.neuralnine.com/services
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🎙 Discord: https://discord.gg/JU4xr8U3dm
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: AI Pair Programming
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Most useless toy I’ve ever built. Love it.
Medium · AI
Reading Anthropic's "When AI Builds Itself" Changed How I Think About AI and Software Engineering
Dev.to · Hemapriya Kanagala
When AI Writes Most of My Code: What Happens to My Identity as a Software Engineer?
Medium · AI
When AI Writes Most of My Code: What Happens to My Identity as a Software Engineer?
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI