top of page

Optimization

Optimization allows you to solve many problems from different areas of knowledge, optimizing incoming data. Familiarity with optimization in Python will allow you to learn libraries such as PyDOE2, numpy and learn how to solve polynomials in various ways. In this example, I will use the OLS Regression method or the least-squares method.

Suppose you have some kind of dependent variable that can be measured under various conditions. These conditions can also be changed to some extent. For the calculation, you need to take the min and max values ​​to see the change in the dependent variable.

 

The DataFrame is shown below, where "T" and "P" are independent variables, and "y" is a dependent variable. The main question is to find the optimal solution in which "y" takes the maximum value:

1.PNG

For a more accurate solution, I will supplement the table with a 5th value that will be determined by the average values of the independent variables:

2.PNG

To determine which next step of approximation, I must solve a polynomial of the 1st degree. I will do this using the "statsmodels" function:

3.PNG

Despite a small error, I got a fairly good model with a convergence of 99.6%. And he determined the unknown coefficients 389.8, 134, 55 and -3.5. The parameter "t" here demonstrates the significance of a variable. 

Let's plot:

4.PNG

Isolinia 400 is very close to my point 407, which suggests that the model is good.

In small steps, I have to move towards the maximum of "y"

5.PNG

We define in our model the very midpoint. It will be equal to 389.8 which is not much less than the value of 407.

7.PNG
8.PNG

We again found a good approximation of 98.5%. The midpoint value is also close to expected (y = 645.4).

9.PNG
10.PNG

Find the coordinates of this point:

11.PNG

Let's rebuild our plot again:

Note that the contours are already trying to change direction, so you can already try to find the maximum of our y.

12.PNG
13.PNG

To calculate the equation, compose a Dataframe with additional ray points.

14.PNG

We solve a polynomial of the second degree:

15.PNG

Let's build the resulting plot:

We display all the previous points on the plot and display it in colour.

16.PNG

At the maximum values of the variables, our point takes a maximum at 1963.4.

19.PNG

Build a 3D graph of our function:

20.PNG
21.PNG

Also, the maximum can be found by finding the derivative of the function with respect to x and y and solving equations with 2 unknowns. For example, let's create a new DataFrame and plot the graph:

22.PNG
23.PNG
24.PNG
25.PNG

We rewrite the resulting equation and find the coordinates of this function:

26.PNG

This technique is applied in absolutely any areas where it is necessary to find the most optimal result with certain input data.

bottom of page