You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Mister Mak <mm...@gmail.com> on 2013/05/23 15:40:44 UTC
[math] Struggling with opimization
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
Re: [math] Struggling with opimization
Posted by Thomas Neidhart <th...@gmail.com>.
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 <mm...@gmail.com> 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
>
Re: [math] Struggling with opimization
Posted by Gilles <gi...@harfang.homelinux.org>.
On Thu, 23 May 2013 09:40:44 -0400, Mister Mak 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 [...]
The user guide needs updating.
>
> 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?
What is the error?
Regards,
Gilles
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: [Math] Re: Pass ConvergenceChecker to BOBYQAOptimizer
Posted by Gilles <gi...@harfang.homelinux.org>.
On Wed, 28 Aug 2013 04:53:26 -0700 (PDT), Thomas wrote:
> Hi!
>
> I have tried to use the CMAESOptimizer but unfortunately the program
> (given
> below) is aborted with the following exception:
>
> Exception in thread "main" java.lang.NullPointerException
> at
>
> org.apache.commons.math3.optim.nonlinear.scalar.noderiv.CMAESOptimizer.checkParameters(CMAESOptimizer.java:554)
> at
>
> org.apache.commons.math3.optim.nonlinear.scalar.noderiv.CMAESOptimizer.parseOptimizationData(CMAESOptimizer.java:542)
> at
>
> org.apache.commons.math3.optim.BaseOptimizer.optimize(BaseOptimizer.java:137)
> at
>
> org.apache.commons.math3.optim.BaseMultivariateOptimizer.optimize(BaseMultivariateOptimizer.java:66)
> at
>
> org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer.optimize(MultivariateOptimizer.java:64)
> at
>
> org.apache.commons.math3.optim.nonlinear.scalar.noderiv.CMAESOptimizer.optimize(CMAESOptimizer.java:363)
> at CMA_ES.main(CMA_ES.java:122)
>
>
>
> The optimizer I used as follows:
>
>
> public static void main(String[] args){
>
> MultivariateFunction f = new MultivariateFunction() {
> @Override
> public double value(double[] arg0) {
> double res = 0;
> for(int i = 0; i < arg0.length; i++){
> res = res + Math.pow(arg0[i], 2);
> }
> return res;
> }
> };
>
> int dim = 50;
>
> JDKRandomGenerator rand = new JDKRandomGenerator();
> rand.setSeed(1503);
>
> double[] sigma = new double[dim];
> for(int i = 0; i < dim; i++){
> sigma[i] = 5;
> }
>
>
> SimpleValueChecker checker = new SimpleValueChecker(1E-3, 1E-6);
> CMAESOptimizer opt = new CMAESOptimizer(Integer.MAX_VALUE,
> Double.NEGATIVE_INFINITY, true, 1, 1, rand, false, checker);
> PointValuePair p = opt.optimize(new ObjectiveFunction(f),
> GoalType.MINIMIZE, new CMAESOptimizer.Sigma(sigma), new
> CMAESOptimizer.PopulationSize(25));
>
>
> According to the API I have to specify the objective function and the
> goal
> of optimization. Moreover, if using the CMAES I have also to specify
> Sigma
> and PopulationSize. Obviously, I specified these parameters in my
> code,
> hence I wonder a bit why the exception is thrown. Does anyone has
> suggestions? Thanks a lot.
The error is a missing "starting point".
See apidocs of the parent class(es).
HTH,
Gilles
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: [Math] Re: Pass ConvergenceChecker to BOBYQAOptimizer
Posted by Thomas <th...@mathematik.uni-marburg.de>.
Hi!
I have tried to use the CMAESOptimizer but unfortunately the program (given
below) is aborted with the following exception:
Exception in thread "main" java.lang.NullPointerException
at
org.apache.commons.math3.optim.nonlinear.scalar.noderiv.CMAESOptimizer.checkParameters(CMAESOptimizer.java:554)
at
org.apache.commons.math3.optim.nonlinear.scalar.noderiv.CMAESOptimizer.parseOptimizationData(CMAESOptimizer.java:542)
at
org.apache.commons.math3.optim.BaseOptimizer.optimize(BaseOptimizer.java:137)
at
org.apache.commons.math3.optim.BaseMultivariateOptimizer.optimize(BaseMultivariateOptimizer.java:66)
at
org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer.optimize(MultivariateOptimizer.java:64)
at
org.apache.commons.math3.optim.nonlinear.scalar.noderiv.CMAESOptimizer.optimize(CMAESOptimizer.java:363)
at CMA_ES.main(CMA_ES.java:122)
The optimizer I used as follows:
public static void main(String[] args){
MultivariateFunction f = new MultivariateFunction() {
@Override
public double value(double[] arg0) {
double res = 0;
for(int i = 0; i < arg0.length; i++){
res = res + Math.pow(arg0[i], 2);
}
return res;
}
};
int dim = 50;
JDKRandomGenerator rand = new JDKRandomGenerator();
rand.setSeed(1503);
double[] sigma = new double[dim];
for(int i = 0; i < dim; i++){
sigma[i] = 5;
}
SimpleValueChecker checker = new SimpleValueChecker(1E-3, 1E-6);
CMAESOptimizer opt = new CMAESOptimizer(Integer.MAX_VALUE,
Double.NEGATIVE_INFINITY, true, 1, 1, rand, false, checker);
PointValuePair p = opt.optimize(new ObjectiveFunction(f),
GoalType.MINIMIZE, new CMAESOptimizer.Sigma(sigma), new
CMAESOptimizer.PopulationSize(25));
According to the API I have to specify the objective function and the goal
of optimization. Moreover, if using the CMAES I have also to specify Sigma
and PopulationSize. Obviously, I specified these parameters in my code,
hence I wonder a bit why the exception is thrown. Does anyone has
suggestions? Thanks a lot.
--
View this message in context: http://apache-commons.680414.n4.nabble.com/math-Struggling-with-opimization-tp4650246p4653405.html
Sent from the Commons - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org