How to create a solver in python
Python scipy provides a good number of optimizers/solvers. You can use these optimizers to solve various non-linear and linear equations. However, sometimes things might get tricky and you will not be able to calculate and provide jacobian to these solvers. Well, this at-least happened with us.
So, we developed our own optimizer to suit our problem and got the perfect result. In this "how to" I will provide a simple way to design and develop your own solver to minimize the polynomial equations of type xˆ2 + yˆ2
Idea is pretty simple, we need to vary x and y in small steps and then store the result of equation for each value of x and y. We will need to run our optimizer twice to find the perfect values of x and y. So, if total number of variables are n then we will need to run optimizer n times. Once for each variable.
Below steps will further help you understand this.
First Optimization to find X.
- Vary x starting from 0.1 in the small steps of 0.01
- In an inner loop vary y starting from 0.1 in the small steps of 0.01
- Inside inner loop calculate the value of your objective function xˆ2 + yˆ2.
- At the end of each inner loop, find the minimum value of equation.
- This is your minimum possible objective function value for all possible values of x.
- Now using numpy polyfit, you can calculate the value of x for which you will get the minimum objective. Following code will help you understand this better.
def calcPoly(this, X, diff, deg=2):
min_idx = diff.index(min(diff))-5
if min_idx<0: min_idx = 0
max_idx = min_idx + 10
x, y = X[min_idx:max_idx], diff[min_idx:max_idx]
co = np.polyfit(x, y, deg)
x = abs(co/(2*co))
Second Optimization to find Y.
Similar to first optimization we need to run our two loops again. But, this time, we will not vary X. We know the value of X from our previous optimization.
- In a loop vary y starting from 0.1 in the small steps of 0.01
- Inside loop calculate the value of your objective function xˆ2 + yˆ2.
- At the end of each loop, store the value of your objective function.
- This is your minimum possible objective function value for all possible values of y.
- Now using numpy polyfit, you can calculate the value of y for which you will get the minimum objective. Above code can be used for the same purpose. Instead of X just pass array of all Y values and diff array will hold all objective function results.
You can now run your objective function again to check if calculated x and y values are really giving you an optimized result. If your equations are of type xˆ3 + yˆ3, you can still use the same code. Just pass deg=3 to calcPoly method.
I hope this will help.
Was this article helpful?
Top Related Jobs
Top Freelance jobs are waiting for you, View & apply to earn more.
$2,000, Fixed | India
We are looking for brilliant Developer with a good English , dedicated to the projects and experienced with python,Django,php,Linux . Any additional s...
$250, Fixed | India
Greetings From IQ Online Training. We are looking for one AWS WITH PYSPARK Trainer who can give corporate training through online.Added the training c...
$1,200, Fixed | Nigeria
We have a very tight timeframe we need this to be delivered in however we are available 24 hours to assist. The goal is to take time stamped data e.g....
$20, Fixed | Italy
We need a python GUI code and a python controller code communicating with GUI code. UI code will be displaying some changes based on controller code c...
$100, Fixed | India
We need either instructions for how to configure an automated action in odoo version 10 to automatically cancel quotations that are still in the open...
$28, Hourly | Bangladesh
Desktop utility needed for extracting data from email into fields in PDF form called Checklist.PDF form already exists and cannot be changed.I will pr...
$350, Hourly | Belarus
Im looking for smart developer for long partnership.At first step need to make Chatbot for sending image to remote server and get response (using ex...
$200, Fixed | Turkey
Hello Applicants,I am looking at building an algorithm that compares Description of the item with the respective brand for a comparative studyI will s...
People also searched for