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.

  1. Vary x starting from 0.1 in the small steps of 0.01
  2. In an inner loop vary y starting from 0.1 in the small steps of 0.01
  3. Inside inner loop calculate the value of your objective function xˆ2 + yˆ2.
  4. At the end of each inner loop, find the minimum value of equation.
  5. This is your minimum possible objective function value for all possible values of x.
  6. 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[1]/(2*co[0]))    

return x

 

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.

 

  1. In a loop vary y starting from 0.1 in the small steps of 0.01
  2. Inside loop calculate the value of your objective function xˆ2 + yˆ2.
  3. At the end of each loop, store the value of your objective function.
  4. This is your minimum possible objective function value for all possible values of y.
  5. 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.

  • Computer Vision Developer

    $28, Hourly | United States
    Our small company search a developer, who can develop software for recognition road marking lane.
  • Back end engineer needed with Go or Scala experiece

    $18, Hourly | India
    Rewrite of existing code from Python.Our company is a stealth start up in home automation and artificial intelligence.Must have 5 years ples of develo...
  • Break captcha image with Machine Learning

    $50, Fixed | Japan
    The deliverable is a job/script that takes an image (CAPTCHA) as input, and returns the text displayed in the image as a text. The images have a varia...
  • need a website

    $5, Hourly | Romania
    want a good responsive and dynamic website for my personal use
  • Shop bot

    $300, Fixed | United States
    -Proxies can be saved to Proxies tab-Billing info saved in billing tab-captcha harvester (Will discuss)-Key Database (Will discuss)-automation for one...
  • Python Code Review for a Instagram Analytics Project

    $18, Hourly | United Kingdom
    Hi,Need an experienced Python programmer that does peer reviews. The program has the following goals:- Extract information from Instagram- Analyze it-...
  • Azure Python Batch task with input file and parallel compute

    $28, Hourly | Australia
    I have a python script that takes to long to run from a single machine. I want to have the script run across a batch and point to the storage account...
  • Convert Matlab Code into Python and review existing

    $50, Fixed | France
    I would like to write matlab function (findchangepts) into python.Also there is an existing python code to be adjust for plotting timeseries.


People also searched for

Python script for computational science
Python script for computational science
Scripts & Utilities

Now a days, the popularity of scientific computing environments such as IDL, Maple, Mathematica, Matlab and R has increased considerably. Engineer simply feel more productive in su...

Read More
Python Scripting – Learn and Earn
Python Scripting – Learn and Earn
Scripts & Utilities

Welcome to Python programming world! We presume you are trying to find information concerning why and how to get started with Python. Fortunately, an experienced coder in any progr...

Read More
Everyone wants API expert
Everyone wants API expert
Scripts & Utilities

APIs became so valuable it comprises an oversized part of several businesses’ revenue. Major companies like Google, eBay, Salesforce.com, Amazon, and Expedia are simply many of the...

Read More

You are freelancing Ninja?

Try Auto proposal today, The new virtual assistant for your freelancing world.