Example usage for org.apache.commons.math3.optim.nonlinear.scalar ObjectiveFunction ObjectiveFunction

List of usage examples for org.apache.commons.math3.optim.nonlinear.scalar ObjectiveFunction ObjectiveFunction

Introduction

In this page you can find the example usage for org.apache.commons.math3.optim.nonlinear.scalar ObjectiveFunction ObjectiveFunction.

Prototype

public ObjectiveFunction(MultivariateFunction f) 

Source Link

Usage

From source file:edu.ucsf.valelab.saim.calculations.SaimErrorFunctionFitter.java

public double[] fit(Collection<WeightedObservedPoint> observedPoints) {
    SaimErrorFunction ser = new SaimErrorFunction(data_, observedPoints);
    MultivariateOptimizer optimizer = new BOBYQAOptimizer(6, 10, 1.0E-8);
    double[] lb = { 0.0, 0.0, 0.0 };
    double[] ub = { 64000, 64000, 1000 };
    SimpleBounds sb = new SimpleBounds(lb, ub);
    PointValuePair results = optimizer.optimize(new MaxEval(20000), GoalType.MINIMIZE, new InitialGuess(guess_),
            new ObjectiveFunction(ser), sb);
    System.out.println("Value: " + results.getValue());
    return results.getPoint();

}

From source file:eu.crisis_economics.abm.markets.clearing.heterogeneous.BoundedQuadraticEstimationClearingAlgorithm.java

@Override
public double applyToNetwork(final MixedClearingNetwork network) {
    Preconditions.checkNotNull(network);
    final int dimension = network.getNumberOfEdges();

    final ResidualCostFunction aggregateCostFunction = super.getResidualScalarCostFunction(network);
    final RealVector start = new ArrayRealVector(network.getNumberOfEdges());
    start.set(1.0); // Initial rate guess.

    final BOBYQAOptimizer optimizer = new BOBYQAOptimizer(2 * dimension + 1, 1.2, 1.e-8);
    final PointValuePair result = optimizer.optimize(new MaxEval(maximumEvaluations),
            new ObjectiveFunction(aggregateCostFunction), GoalType.MINIMIZE,
            new SimpleBounds(new double[dimension], ArrayUtil.ones(dimension)),
            new InitialGuess(start.toArray()));

    final double residualCost = result.getValue();
    System.out.println("Network cleared: residual cost: " + residualCost + ".");

    return residualCost;
}

From source file:edu.utexas.cs.tactex.tariffoptimization.OptimizerWrapperApachePowell.java

@Override
public TreeMap<Double, TariffSpecification> findOptimum(TariffUtilityEstimate tariffUtilityEstimate,
        int NUM_RATES, int numEval) {

    double[] startingVertex = new double[NUM_RATES]; // start from the fixed-rate tariff's offset
    Arrays.fill(startingVertex, 0.0);

    PowellOptimizer optimizer = new PowellOptimizer(1e-3, 5);

    // needed since one optimization found positive 
    // charges (paying customer to consume...)
    double[][] boundaries = createBoundaries(NUM_RATES);

    final PointValuePair optimum = optimizer.optimize(new MaxEval(numEval),
            //new ObjectiveFunction(new MultivariateFunctionMappingAdapter(tariffUtilityEstimate, boundaries[0], boundaries[1])),
            new ObjectiveFunction(new OptimizerWrapperApacheObjective(tariffUtilityEstimate)),
            GoalType.MAXIMIZE, new InitialGuess(startingVertex));

    TreeMap<Double, TariffSpecification> eval2TOUTariff = new TreeMap<Double, TariffSpecification>();
    eval2TOUTariff.put(optimum.getValue(), tariffUtilityEstimate.getCorrespondingSpec(optimum.getKey()));
    return eval2TOUTariff;
}

From source file:edu.utexas.cs.tactex.tariffoptimization.OptimizerWrapperApacheAmoeba.java

@Override
public TreeMap<Double, TariffSpecification> findOptimum(TariffUtilityEstimate tariffUtilityEstimate,
        int NUM_RATES, int numEval) {
    double[] startingVertex = new double[NUM_RATES]; // start from the fixed-rate tariff's offset
    //Arrays.fill(startingVertex, -0.5 * SIMPLEX_EDGE);
    //Arrays.fill(startingVertex, -1 * SIMPLEX_EDGE);

    // TODO if there are convergence issues, change these guessed thresholds
    //SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); 
    //SimplexOptimizer optimizer = new SimplexOptimizer(1e-4, 1e-4); 
    //SimplexOptimizer optimizer = new SimplexOptimizer(1e-2, 10); 
    SimplexOptimizer optimizer = new SimplexOptimizer(1e-3, 5);
    //SimplexOptimizer optimizer = new SimplexOptimizer(1e-2, 5); 

    final PointValuePair optimum = optimizer.optimize(new MaxEval(numEval),
            new ObjectiveFunction(new OptimizerWrapperApacheObjective(tariffUtilityEstimate)),
            GoalType.MAXIMIZE, new InitialGuess(startingVertex),
            //new NelderMeadSimplex(NUM_RATES, -1 * SIMPLEX_EDGE)); 
            new NelderMeadSimplex(NUM_RATES, SIMPLEX_EDGE));// should be positive since this reflects decrease in (negative) charges

    TreeMap<Double, TariffSpecification> eval2TOUTariff = new TreeMap<Double, TariffSpecification>();
    eval2TOUTariff.put(optimum.getValue(), tariffUtilityEstimate.getCorrespondingSpec(optimum.getKey()));
    return eval2TOUTariff;
}

From source file:eu.crisis_economics.abm.markets.clearing.heterogeneous.NelderMeadClearingAlgorithm.java

@Override
public double applyToNetwork(final MixedClearingNetwork network) {
    Preconditions.checkNotNull(network);
    final SimplexOptimizer optimizer = new SimplexOptimizer(relErrorTarget, absErrorTarget);

    final ResidualCostFunction aggregateCostFunction = super.getResidualScalarCostFunction(network);
    final RealVector start = new ArrayRealVector(network.getNumberOfEdges());
    for (int i = 0; i < network.getNumberOfEdges(); ++i)
        start.setEntry(i, network.getEdges().get(i).getMaximumRateAdmissibleByBothParties());
    start.set(1.);//from www. j  a  v  a 2  s.com

    final PointValuePair result = optimizer.optimize(new MaxEval(maximumEvaluations),
            new ObjectiveFunction(aggregateCostFunction), GoalType.MINIMIZE, new InitialGuess(start.toArray()),
            new NelderMeadSimplex(network.getNumberOfEdges()));

    final double residualCost = result.getValue();
    System.out.println("Network cleared: residual cost: " + residualCost + ".");

    return residualCost;
}

From source file:edu.utexas.cs.tactex.tariffoptimization.OptimizerWrapperApacheBOBYQA.java

@Override
public TreeMap<Double, TariffSpecification> findOptimum(TariffUtilityEstimate tariffUtilityEstimate,
        int NUM_RATES, int numEval) {

    double[] startingVertex = new double[NUM_RATES]; // start from the fixed-rate tariff's offset
    Arrays.fill(startingVertex, 0.0);
    //Arrays.fill(startingVertex, 0.5 * INITIAL_TRUST_REGION_RADIUS);
    //Arrays.fill(startingVertex, 1 * INITIAL_TRUST_REGION_RADIUS);

    final int numIterpolationPoints = 2 * NUM_RATES + 1; // BOBYQA recommends 2n+1 points
    BOBYQAOptimizer optimizer = new BOBYQAOptimizer(numIterpolationPoints, INITIAL_TRUST_REGION_RADIUS,
            STOPPING_TRUST_REGION_RADIUS);

    // needed since one optimization found positive 
    // charges (paying customer to consume...)
    double[][] boundaries = createBoundaries(NUM_RATES);

    final PointValuePair optimum = optimizer.optimize(new MaxEval(numEval),
            new ObjectiveFunction(new OptimizerWrapperApacheObjective(tariffUtilityEstimate)),
            GoalType.MAXIMIZE, new InitialGuess(startingVertex),
            //new SimpleBounds(boundaries[0], boundaries[1]));
            SimpleBounds.unbounded(NUM_RATES));

    TreeMap<Double, TariffSpecification> eval2TOUTariff = new TreeMap<Double, TariffSpecification>();
    eval2TOUTariff.put(optimum.getValue(), tariffUtilityEstimate.getCorrespondingSpec(optimum.getKey()));
    return eval2TOUTariff;
}

From source file:edu.uchc.octane.GaussianFitAstigmatism.java

@Override
public double[] doFit() {
    double[] initParameters;

    if (bPreprocessBg_) {
        initParameters = new double[] { 0, 0, pixelValue(0, 0) - bg_, sigma2_, sigma2_ };
    } else {/*  w w w.j  a  va  2s  . c  om*/
        initParameters = new double[] { 0, 0, pixelValue(0, 0) - bg_, sigma2_, sigma2_, bg_ };
    }

    PowellOptimizer optimizer = new PowellOptimizer(1e-4, 1e-1);

    MultivariateFunction func = new MultivariateFunction() {
        @Override
        public double value(double[] point) {

            double bg = bPreprocessBg_ ? 0 : point[3];

            double v = 0;

            for (int xi = -windowSize_; xi < windowSize_; xi++) {
                for (int yi = -windowSize_; yi < windowSize_; yi++) {
                    double delta = getValueExcludingBackground(xi, yi, point) + bg - pixelValue(xi, yi);
                    v += delta * delta;
                }
            }
            return v;
        }
    };

    pvp_ = optimizer.optimize(new ObjectiveFunction(func), new InitialGuess(initParameters), new MaxEval(10000),
            GoalType.MINIMIZE);

    //      double sigmax = pvp.getPoint()[3];
    //      double sigmay = pvp.getPoint()[4];
    //      if (calibration_ != null) {
    //         double [] pn = new double[4];
    //         pn[0] = p1_;
    //         pn[1] = p2_;
    //         pn[2] = p3_ + 2 * calibration_[2] * (calibration_[0] - sigmax) + 2 * calibration_[5] * (calibration_[3] - sigmay);
    //         pn[3] = calibration_[1] * (calibration_[0] - sigmax) + calibration_[4] * (calibration_[3] - sigmay);
    //         
    //         Complex [] roots = new LaguerreSolver().solveAllComplex(pn, 0);
    //         
    //         int minIndex = -1;
    //         double minV = Double.MAX_VALUE;
    //         for (int i = 0; i < 3; i ++) {
    //            double v;
    //            if (FastMath.abs(roots[i].getImaginary()) < 1e-7) {
    //               double z = roots[i].getReal(); 
    //               double vx = calibration_[0] + calibration_[1] * z + calibration_[2] * z * z - sigmax;
    //               double vy = calibration_[3] + calibration_[4] * z + calibration_[5] * z * z - sigmay;
    //               v = vx * vx + vy * vy;
    //               if (v < minV) {
    //                  minV = v;
    //                  minIndex = i;
    //               }
    //            }
    //         }
    //         
    //         if (minV > errTol_) {
    //            return null;
    //         }
    //
    //         z_ = roots[minIndex].getReal();
    //      }

    if (calibration_ != null) {
        calculateZ();
    }

    return pvp_.getPoint();
}

From source file:edu.uchc.octane.GaussianFit2D.java

@Override
public double[] doFit() {

    double[] initParameters;

    if (floatingSigma_) {
        if (bPreprocessBg_) {
            initParameters = new double[] { 0, 0, pixelValue(0, 0) - bg_, sigma2_ };
        } else {// w w  w. j  a v a2s . com
            initParameters = new double[] { 0, 0, pixelValue(0, 0) - bg_, bg_, sigma2_ };
        }
    } else {
        if (bPreprocessBg_) {
            initParameters = new double[] { 0, 0, pixelValue(0, 0) - bg_ };
        } else {
            initParameters = new double[] { 0, 0, pixelValue(0, 0) - bg_, bg_ };
        }
    }

    PowellOptimizer optimizer = new PowellOptimizer(1e-4, 1e-1);

    MultivariateFunction func = new MultivariateFunction() {
        @Override
        public double value(double[] point) {

            //initParameters = point;
            double bg = bPreprocessBg_ ? 0 : point[3];

            double v = 0;

            for (int xi = -windowSize_; xi < windowSize_; xi++) {
                for (int yi = -windowSize_; yi < windowSize_; yi++) {
                    double delta = getValueExcludingBackground(xi, yi, point) + bg - pixelValue(xi, yi);
                    v += delta * delta;
                }
            }
            return v;
        }
    };

    pvp_ = optimizer.optimize(new ObjectiveFunction(func), new InitialGuess(initParameters), new MaxEval(10000),
            GoalType.MINIMIZE);

    return pvp_.getPoint();
}

From source file:edu.cmu.tetrad.sem.SemOptimizerPowell.java

public void optimize(SemIm semIm) {
    double min = Double.POSITIVE_INFINITY;
    double[] point = null;

    for (int count = 0; count < numRestarts + 1; count++) {
        System.out.println("Trial " + (count + 1));
        SemIm _sem2 = new SemIm(semIm);

        List<Parameter> freeParameters = _sem2.getFreeParameters();

        double[] p = new double[freeParameters.size()];

        for (int i = 0; i < freeParameters.size(); i++) {
            if (freeParameters.get(i).getType() == ParamType.VAR) {
                p[i] = RandomUtil.getInstance().nextUniform(0, 1);
            } else {
                p[i] = RandomUtil.getInstance().nextUniform(-1, 1);
            }//from   w  ww  .  j  a  v a 2  s.c o  m
        }

        _sem2.setFreeParamValues(p);

        MultivariateOptimizer search = new PowellOptimizer(1e-7, 1e-7);
        PointValuePair pair = search.optimize(new InitialGuess(_sem2.getFreeParamValues()),
                new ObjectiveFunction(fittingFunction(semIm)), GoalType.MINIMIZE, new MaxEval(100000));

        double chisq = _sem2.getChiSquare();
        System.out.println("chisq = " + chisq);

        if (chisq < min) {
            min = chisq;
            point = pair.getPoint();
        }
    }

    System.arraycopy(point, 0, semIm.getFreeParamValues(), 0, point.length);
}

From source file:edu.cmu.tetrad.regression.LogisticRegression2.java

public void regress(int[] target, int numValues, double[][] regressors) {
    try {/*w ww.  j a v  a 2  s . co m*/
        int numParams = regressors.length + 1;

        double[] coefficients = new double[(numValues - 1) * numParams];

        // Apparently this needs to be fairly loose.
        int tolerance = 250;
        MultivariateOptimizer search = new PowellOptimizer(tolerance, tolerance);

        PointValuePair pair = search.optimize(new InitialGuess(coefficients),
                new ObjectiveFunction(new FittingFunction(target, regressors)), GoalType.MAXIMIZE,
                new MaxEval(1000000));

        this.likelihood = pair.getValue();
    } catch (TooManyEvaluationsException e) {
        e.printStackTrace();
        this.likelihood = Double.NaN;
    }
}