# Function Maximization in Java

I have a bivariate function like

$f(x,y) = \frac{1}{x^3 \sqrt{\pi}}. e^{\frac{2-x}{x^2}} . y^3 . e^{3.y \over 3-y}$

and I want to find its global maximum over a range of $x \in [0, 200] \text{, and } y \in [300,50000]$

What kind of algorithms I can use to find the global maximum. I want to have keywords for searching and finding materials.

Are there any java library which I can use to solve these kind of problems?

• Welcome! Note that algorithms are ontopic here, but Java libraries are not. Jul 18, 2012 at 1:36

The most basic algorithm is probably gradient gescent which can also be used for maximization. It uses the gradient of the function to optimize and (starting from an initial point) "goes" into the most promising direction. For maximization this is the direction of the gradient. Look at the examples on Wikipedia. That should give you an idea how it works. Note that in is pure variant, it always gets stuck in local optima. I don't know if there is a Java library for this. I programmed it myself some time ago, but this code is very specific to the task.

Another thing to look at is the Nelder-Mead method. It is also susceptible to local minima and requires a smooth function. It is implemented in Flanagan's Java Scientific Library. Basically you have to extend MaximizationFunction and implement the function

public double function(double[] param)


which contains your function above. This method evaluates your function given the parameters param (in your case two values: x and y) and returns the function value.

Then you can use the whole program like this:

//Create instance of Maximisation
Maximization max = new Maximization();

// Create instace of class holding function to be maximised
YourFunction funct = new YourFunction();

// initial estimates (your initial x and y)
double[] start = {100.0, 3000.0};

// initial step sizes (take a good guess)
double[] step = new double[start.length];
Arrays.fill(step, 100);

// convergence tolerance
double ftol = 0.0001;

// maximal number of iterations
int maxIter = 5000;

// Nelder and Mead maximisation procedure

Since your variables have restrictions, you should add some constraints via the addConstraint method of Maximization.
• I see. I edited the answer to have more flow (imho). Still, it's not a particularly good answer: what are (dis)advantages of the methods? Why should I implement/use the more complicated Nelder-Mead? Are there ways to extend either method to multimodel functions? Note that the function $f$ the OP proposes seems to be neither unimodal nor continuous. Jul 18, 2012 at 9:42