Constrained Multivariable Optimisation of Lambda Functions using SciPy
Multivariable optimisation of lambda functions can be easily computed using python's SciPy library. To accomplish this task, the only import required is the 'minimise' function from the SciPy library.
If you do not have SciPy installed in your current environment, you can always use the following command to directly install it from applications such as jupyter notebooks.
Now, suppose we have an objective function that we want to minimise. For the purposes of the example, I have chosen the following function, however, this can be abstracted to any functional form.
Lets also assume we have two constraints, which are described as follows:
Now in order to represent this constrained optimisation problem on python, we first define the objective function we want to minimise in the form of a lambda function
In our lambda function, x[0] represents the 'x' variable, x[1] represents the 'y' variable and x[2] represents the 'z' variable. Once we have defined our lambda function, we need to code in our constraints.
We can conveniently express our constraints in the form of lambda expressions as well. We later specify whether the constrain is an equality or an inequality. The default way python interprets it is as if the expression is > 0 or = 0. Therefore, you need to be careful of the signs you are using when defining the lambda function for inequality constraints.
After expressing the constraints, we need to define their type as being an 'equality' or an 'inequality', in the form of a dictionary. We then append the two dictionaries into a singe list called 'cons' which is what will be entered into SciPy's minimise function. Another specification we make is the bounds of our variable.
Out here, we have restricted the domain of all three of our variables between -10 and 10. Finally, we need to construct one last variable call 'x0' which stores in the value of an initial guess of the minima. This does not need to be accurate, it is just a starting point for the minimise function to start at.
At this point, we have constructed all the necessary code required to be fed into SciPy's minimise function. We just need to execute the function and store the results.
The results have been stored in the variable solution. Within it, there are several pieces of information which inform us about the minimum point, whether the optimisation terminated successfully, number of iterations, etc.
In order to isolate the solution, we can use the following command
The answer is returned as a numpy array which can easily be stored into another variable and used for further calculations.