Hi Philippe,
fyi: the optimization package has been refactored from base-package
optimization to optim
both contain more or less the same functionality, but the interface is
slightly different.
Looking at your example, you seem to have a univariate case, so you could
use the BrentSolver like this (of course adapting the search interval as
needed):
public static void main(String[] args) {
UnivariateFunction f = new UnivariateFunction() {
public double value(double x) {
return -x*x;
}
};
BrentOptimizer optimizer = new BrentOptimizer(1e-6, 1e-12);
UnivariatePointValuePair solution =
optimizer.optimize(new UnivariateObjectiveFunction(f),
new MaxEval(100),
GoalType.MAXIMIZE,
new SearchInterval(-10, 10));
System.out.println(solution.getValue());
}
Hope this helps, otherwise just ask.
Thomas
On Thu, May 23, 2013 at 3:40 PM, Mister Mak <[email protected]> wrote:
> Hello,
>
> I am new to Commons Math (3.2 on MacOS 10.5.8) and am struggling to
> maximize a simple function. I think I need to use the optimization
> classes, but the user guide (
> http://commons.apache.org/proper/commons-math/userguide/optimization.html)
> seems to refer mainly to functions that are deprecated according to the
> API. Mucking around through the API, I think I have found some other
> functions that are not deprecated, but it's really not clear to me how to
> use them.
>
> What I have done so far is:
>
> import org.apache.commons.math3.analysis.*;
> import org.apache.commons.math3.optim.nonlinear.scalar.noderiv.*;
> import org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction;
> import org.apache.commons.math3.optim.nonlinear.scalar.GoalType;
>
> then somewhere I define a mutlivariate function:
>
> class TestFcMulti implements MultivariateFunction {
> public double value(double[] par) {
> return - par[0] * par[0];
> }
> }
>
> but then the following code does not work:
>
> public Test_MC() {
>
> TestFcMulti tf = new TestFcMulti();
> ObjectiveFunction obFc = new ObjectiveFunction(tf);
> SimplexOptimizer so = new SimplexOptimizer(0.01, 0.01);
>
> so.optimize(obFc, GoalType.MAXIMIZE); // Doesn't work
> so.optimize(); // Doesn't work
> }
>
>
> Is there a simple example somewhere, or can someone explain what to do?
>
> Thanks,
>
> Philippe
>