6

I was wondering if anyone had any suggestions for minimizing a function, f(x,y), where x and y are integers. I have researched lots of minimization and optimization techniques, like BFGS and others out of GSL, and things out of Numerical Recipes. So far, I have tried implenting a couple of different schemes. The first works by picking the direction of largest descent f(x+1,y),f(x-1,y),f(x,y+1),f(x,y-1), and follow that direction with line minimization. I have also tried using a downhill simplex (Nelder-Mead) method. Both methods get stuck far away from a minimum. They both appear to work on simpler functions, like finding the minimum of a paraboloid, but I think that both, and especially the former, are designed for functions where x and y are real-valued (doubles). One more problem is that I need to call f(x,y) as few times as possible. It talks to external hardware, and takes a couple of seconds for each call. Any ideas for this would be greatly appreciated.

Here's an example of the error function. Sorry I didn't post this before. This function takes a couple of seconds to evaluate. Also, the information we query from the device does not add to the error if it is below our desired value, only if it is above

double Error(x,y)
{
  SetDeviceParams(x,y);
  double a = QueryParamA();
  double b = QueryParamB();
  double c = QueryParamC();
  double _fReturnable = 0;
  if(a>=A_desired)
  {
    _fReturnable+=(A_desired-a)*(A_desired-a);
  }
  if(b>=B_desired)
  {
    _fReturnable+=(B_desired-b)*(B_desired-b);
  }
  if(c>=C_desired)
  {
    _fReturnable+=(C_desired-c)*(C_desired-c);
  }
  return Math.sqrt(_fReturnable)
}
Tim
  • 361
  • 1
  • 5
  • 14
  • 1
    Any ideas regarding your function's class and behaviour will also be appreciated. – EFraim Jul 24 '09 at 14:40
  • 1
    Interesting question. Funny how maths first become difficult when you started learning about fractions and real numbers, and the difficult again once you remove these and go back to the natural numbers. =) – Jan Aagaard Jul 24 '09 at 14:40
  • 1
    Do you know the equation for f(x, y)? – Noldorin Jul 24 '09 at 14:46
  • This is not really a programming question. But it's more interesting than "Why doesn't my camera work with my laptop?" so I'm sayin' nothin'! – Daniel Earwicker Jul 24 '09 at 14:49
  • 2
    Sure it's a programming question. Unless you consider algorithms not to be programs. Me, I think that anything which is not an algorithm is not a programming question. Eg, how do I do X in language Y -- a language question, not a programming one. – Daniel C. Sobral Jul 24 '09 at 15:30
  • It's an algorithm question, but it's about a very heavily studied subject in pure mathematics. If I had a question like this, I'd go to a mathematics newsgroup or something like that. – Daniel Earwicker Jul 24 '09 at 17:05
  • Math newsgroups would probably not be very useful if you are interested in practical solutions as opposed to theoretical solutions for this type of problem. – SplittingField Aug 13 '09 at 03:23

8 Answers8

4

There are many, many solutions here. In fact, there are entire books and academic disciplines based on the subject. I am reading an excellent one right now: How to Solve It: Modern Heuristics.

There is no one solution that is correct - different solutions have different advantages based on specific knowledge of your function. It has even been proven that there is no one heuristic that performs the best at all optimization tasks.

If you know that your function is quadratic, you can use Newton-Gauss to find the minimum in one step. A genetic algorithm can be a great general-purpose tool, or you can try simulated annealing, which is less complicated.

Brian Ramsay
  • 7,160
  • 8
  • 37
  • 52
3

Have you looked at genetic algorithms? They are very, very good at finding minimums and maximums, while avoiding local minimum/maximums.

Daniel C. Sobral
  • 284,820
  • 82
  • 479
  • 670
2

If it's an arbitrary function, there's no neat way of doing this.

Suppose we have a function defined as:

f(x, y) = 0 for x==100, y==100
          100 otherwise

How could any algorithm realistically find (100, 100) as the minimum? It could be any possible combination of values.

Do you know anything about the function you're testing?

Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
  • f(x,y) is a calibration error function. There are two parameters that I am interested in. Both are affected by changes in x and y. The function is fairly continuous, but its derivative may not be because as soon as either of the parameters is in spec, I just set it to 0 – Tim Jul 24 '09 at 14:44
  • 2
    @Jon Skeet: That is of course assuming that the function could be *anything*, which indeed forces you to try every combinations of (x, y). If you know the function is pseudo-continuous, things are simplified hugely. – Noldorin Jul 24 '09 at 14:53
  • @Noldorin: That's why I asked what the OP knew about the function. At the time I posted, the function I gave would have satisfied everything we knew. – Jon Skeet Jul 24 '09 at 15:13
  • @Jon Skeet: Indeed, the function you posted was an example of a fairly arbitrary function. However, it's very unlikely that the function is *not* completely arbitrary (as we know it now can't be, being an error function), meaning a better approach is bound to be possible. – Noldorin Jul 24 '09 at 15:30
2

How do you define f(x,y) ? Minimisation is a hard problem, depending on the complexity of your function.

Genetic Algorithms could be a good candidate.

Resources:

Genetic Algorithms in Search, Optimization, and Machine Learning

Implementing a Genetic Algorithms in C#

Simple C# GA

Indy9000
  • 8,188
  • 2
  • 26
  • 34
  • Do you have any suggestions on good resources to get started with genetic algorithms? – Tim Jul 24 '09 at 14:45
  • 1
    There are loads of books on this subject. What got me started was the Goldberg book. http://www.amazon.com/Genetic-Algorithms-Optimization-Machine-Learning/dp/0201157675 – Indy9000 Jul 24 '09 at 14:47
1

What you are generally looking for is called an optimisation technique in mathematics. In general, they apply to real-valued functions, but many can be adapted for integral-valued functions.

In particular, I would recommend looking into non-linear programming and gradient descent. Both would seem quite suitable for your application.

If you could perhaps provide any more details, I might be able to suggest somethign a little more specific.

Noldorin
  • 134,265
  • 53
  • 250
  • 293
  • As I said before its going to be used in a calibration program, so there will be lots of devices that have similar but not identical shapes for their error function. I do not know exactly what the shape looks like, because there is a lot of data that I need to get. both x and y are between 0 and 65535, and it takes a couple of seconds to collect one piece of data. – Tim Jul 24 '09 at 15:01
  • @Tim: Fair enough. Could you perhaps give us the form of this error function however? I'm not terribly optimistic about success here, if a request takes a matter of seconds! – Noldorin Jul 24 '09 at 15:15
  • This is basically what happens. I apologize if its a little ambiguous. double Error(x,y) { SetDeviceParams(x,y); double a = QueryParamA(); double b = QueryParamB(); double c = QueryParamC(); double _fReturnable = 0 if(a>=A_desired) { _fReturnable+=(A_desired-a)*(A_desired-a); } if(b>=B_desired) { _fReturnable+=(B_desired-b)*(B_desired-b); } if(c>=C_desired) { _fReturnable+=(C_desired-c)*(C_desired-c); } return Math.sqrt(_fReturnable) } – Tim Jul 24 '09 at 15:22
  • +1 to avoid being trapped in a local minimum, try running gradient descent a few times from random starting places – Gabe Moothart Jul 24 '09 at 15:43
  • @Gabe: Yeah, exactly. Unless you have a very good idea of where the minimum is near, you need to run the algorithm from a range of starting points. – Noldorin Jul 24 '09 at 15:47
1

Jon Skeet's answer is correct. You really do need information about f and it's derivatives even if f is everywhere continuous.

The easiest way to appreciate the difficulties of what you ask(minimization of f at integer values only) is just to think about an f: R->R (f is a real valued function of the reals) of one variable that makes large excursions between individual integers. You can easily construct such a function so that there is NO correllation between the local minimums on the real line and the minimums at the integers as well as having no relationship to the first derivative.

For an arbitrary function I see no way except brute force.

  • Based on the testing that I have done, the gradient is small everywhere, so the function does not change much between integer values, but I can't predict in which direction it will change. Brute force won't work, because it takes so long to get a single piece of data – Tim Jul 24 '09 at 15:31
  • so are you now saying that besides the problem of looking at only integral values you don't event know f and you are only going to sample a subset of the {(x,y)} and try to draw conclusions from a limited subset? –  Jul 24 '09 at 16:00
  • @pgast Unfortunately, that's pretty much what I am saying. I have enough data that I have a good guess of a starting point, but that is about it. The good news is that I don't necessarily care about "the best" solution, as long as the solution I get meets spec – Tim Jul 24 '09 at 16:03
  • If this is a life dependent application then I would start looking for another job, as what you are being asked to do would be negligence of the first order. If not then I would probably brute force it with an intelligent choice of subset. What is the limit, nsamples, on the number of samples you can take? I would try to overlay a grid on 64Kx64 such that the number of grid points is nsamples, sample at the grid points and choose the minimum. –  Jul 24 '09 at 16:31
  • 1
    If you have real knowledge of the function then the initial grid may have less points say nsamples-m, and you and you can use the last m sampleings to brute force around the minimum of the first (nsamples-m) samples If I understand you you are searching for an (x,y) parameter pair in order to set up a device so that the error is small –  Jul 24 '09 at 16:32
1

So let's look at your problem in math-speak. This is all assuming I understand your problem fully. Feel free to correct me if I am mistaken.

we want to minimize the following:

\sqrt((a-a_desired)^2 + (b-b_desired)^2 + (c-c_desired)^2)

or in other notation ||Pos(x - x_desired)||_2

where x = (a,b,c) and Pos(y) = max(y, 0) means we want the "positive part"(this accounts for your if statements). Finally, we wish to restrict ourself to solutions where x is integer valued.

Unlike the above posters, I don't think genetic algorithms are what you want at all.
In fact, I think the solution is much easier (assuming I am understanding your problem).

1) Run any optimization routine on the function above. THis will give you the solution x^* = (a^*, b^*,c^*). As this function is increasing with respect to the variables, the best integer solution you can hope for is (ceil(a^*),ceil(b^*),ceil(c^*)).

Now you say that your function is possibly hard to evaluate. There exist tools for this which are not based on heuristics. The go under the name Derivative-Free Optimization. People use these tools to optimize objective based on simulations (I have even heard of a case where the objective function is based on crop crowing yields!)

Each of these methods have different properties, but in general they attempt to minimize not only the objective, but the number of objective function evaluations.

SplittingField
  • 723
  • 5
  • 11
  • +1 for derivative-free optimization, but the math-y restatement could use an explicit mention of the fact that "x" is a function, and perhaps rename "x" so it doesn't collide with the variables from the posted source. – outis Aug 13 '09 at 03:03
  • x is not a function, just the collection of the variables a,b,c into a single vector. – SplittingField Aug 13 '09 at 03:21
  • 1
    Tim doesn't want to minimize `Err_A(x) = ||Pos(x - A)||₂`, but rather `Err_A(Dev(u))`, where `Dev(u):Z² -> R³`. If the soln. is in `x`, it doesn't need to be in integers. Furthermore, `ceil·x'` might not be a valid soln. in `x`, as there may not be a `u'` such that `ceil·x'=Dev(u')`. For the extensions of `Dev` to some `Dev'(u):R² -> R³` I can imagine (terraces, meshes), solns. in `u` would be at integer values. If an exotic `Dev'(u)` had a minimum at `u'∉Z²`, elements of `{ceil, floor}² · u'` may not be solutions. – outis Aug 13 '09 at 04:24
  • Interesting... I need to think about this some more in a bit, but clearly I am m.isunderstanding something in his function def. Still, I think there are other non-heuristic solutions which I know are used in industry for this type of problem... an exampls is MADS. – SplittingField Aug 13 '09 at 15:44
  • `Dev` represents the device. It's the first 4 lines of his `Error(x,y)` function. – outis Aug 13 '09 at 19:56
0

Sorry the formatting was so bad previously. Here's an example of the error function

double Error(x,y)
{
SetDeviceParams(x,y);
double a = QueryParamA();
double b = QueryParamB();
double c = QueryParamC();
double _fReturnable = 0;
if(a>=A_desired)
{
  _fReturnable+=(A_desired-a)*(A_desired-a);
}
if(b>=B_desired)
{
 _fReturnable+=(B_desired-b)*(B_desired-b);
}
if(c>=C_desired)
{
  _fReturnable+=(C_desired-c)*(C_desired-c);
}
return Math.sqrt(_fReturnable)
}
Tim
  • 361
  • 1
  • 5
  • 14
  • Ah, well, I have done it for you. The formatting ended up different, as I foolishly copied from the displayed text instead of the editting text. – Daniel C. Sobral Jul 24 '09 at 15:37