Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pre-computing large table of values

Tags:

java

algorithm

I have a mathematical formula in my program that takes in two values, both between 0 and 1, and does a lot of work to find an answer.

I also want to be able to do the inverse, i.e. I want to know what input values will produce a certain output. I cannot do this analytically, as the same answer can be produced from numerous inputs and the formulas are too complex anyway.

My problem is that I am currently doing something like this, which takes fairly long to compute

  for(double i = 0; i <= 1 ; i += 0.0001)
      for(double j = 0; j <= 1; j+= 0.0001)
           answer = formula(i,j); //do the math
           if( Math.abs(answer - answerWanted) < 0.001)
                //close match found

Seeing as the formulas are static, I could surely pre compute these values. I presume it would then be much quicker to look up a value than to perform many calculations.

I have never done anything like this before. Does anyone know what data structures to use/ how to index/ how to store the results? At the moment my only thoughts are that I could somehow sort the answers to reduce the search space or else just initializing a huge array at runtime. If it matters, the answer can only range between 0 and 2000.

like image 968
Roger Avatar asked Apr 05 '11 00:04

Roger


1 Answers

An alternative is to use a more intelligent search algorithm. The best choice will depend on your function, but a good start will probably be the Nelder-Mead (Downhill Simplex) algorithm:

http://en.wikipedia.org/wiki/Nelder–Mead_method

This will greatly reduce the number of calculations. Local minima can be a problem for some search algorithms but Nelder-Mead can get out of many/most of these.

If you find you are searching the same values repeatedly, you can then also add a simple caching mechanism.

like image 171
winwaed Avatar answered Oct 23 '22 22:10

winwaed