Example usage for org.apache.commons.math3.optim InitialGuess InitialGuess

List of usage examples for org.apache.commons.math3.optim InitialGuess InitialGuess

Introduction

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

Prototype

public InitialGuess(double[] startPoint) 

Source Link

Usage

From source file:net.sf.tweety.math.opt.solver.ApacheCommonsCMAESOptimizer.java

@Override
public Map<Variable, Term> solve(ConstraintSatisfactionProblem problem) throws GeneralMathException {
    // only optimization problems
    if (!(problem instanceof OptimizationProblem))
        throw new IllegalArgumentException("Only optimization problems allowed for this solver.");
    OptimizationProblem p = (OptimizationProblem) problem;
    // only box constraints allowed (so far)
    if (!p.isEmpty())
        throw new IllegalArgumentException(
                "Only optimization problems with box constraints on variables allowed for this solver (no other constraints.");
    final List<Variable> vars = new ArrayList<Variable>(p.getTargetFunction().getVariables());
    double[] lb = new double[vars.size()];
    double[] ub = new double[vars.size()];
    double[] s = new double[vars.size()];
    double[] sigma = new double[vars.size()];
    for (int i = 0; i < vars.size(); i++) {
        lb[i] = vars.get(i).getLowerBound();
        ub[i] = vars.get(i).getUpperBound();
        s[i] = (lb[i] + ub[i]) / 2;//from  www  .  ja  v a 2s . c om
        sigma[i] = ub[i] - lb[i];
    }
    final Term targetFunction = p.getTargetFunction();
    MultivariateFunction target = new MultivariateFunction() {
        @Override
        public double value(double[] arg0) {
            return targetFunction.replaceAllTerms(arg0, vars).doubleValue();
        }
    };
    // construct solver
    CMAESOptimizer optimizer = new CMAESOptimizer(this.maxIterations, this.stopFitness, this.isActiveCMA,
            this.diagonalOnly, this.checkFeasableCount, new JDKRandomGenerator(), false,
            new SimplePointChecker<PointValuePair>(this.precision, this.precision));
    PointValuePair val = optimizer.optimize(new CMAESOptimizer.Sigma(sigma), new ObjectiveFunction(target),
            new InitialGuess(s),
            p.getType() == OptimizationProblem.MAXIMIZE ? GoalType.MAXIMIZE : GoalType.MINIMIZE,
            new MaxEval(this.maxIterations), new SimpleBounds(lb, ub),
            new CMAESOptimizer.PopulationSize(this.populationSize));
    Map<Variable, Term> result = new HashMap<Variable, Term>();
    for (int i = 0; i < vars.size(); i++)
        result.put(vars.get(i), new FloatConstant(val.getPoint()[i]));
    return result;
}

From source file:de.thkwalter.et.ortskurve.Ausgleichsproblem.java

/**
 * Diese Methode lst das Ausgleichsproblem.
 * /* w  w w.  j  a  v a 2s.c  o m*/
 * @param startpunkt Der Startpunkt der Ausgleichsrechnung
 * @param ausgleichsproblemtyp Der Typ des Ausgleichsproblems
 * 
 * @return Die berechnete Ortskurve
 */
public Ortskurve ausgleichsproblemLoesen(double[] startpunkt, Ausgleichsproblemtyp ausgleichsproblemtyp) {
    // Die Referenzen auf die Modellgleichungen und die Jakobimatrix werden deklariert.
    MultivariateVectorFunction modellgleichungen = null;
    MultivariateMatrixFunction jakobiMatrix = null;

    // Falls das zweidimensionale Ausgleichsproblem gelst werden soll, ...
    if (ausgleichsproblemtyp == Ausgleichsproblemtyp.ORTSKURVE_2d) {
        // Die Modellgleichungen (die Kreisgleichungen) werden erzeugt.
        modellgleichungen = new Modellgleichungen2d(this.messpunkte);

        // Die Jakobi-Matrix der Modellgleichungen (der Kreisgleichungen) wird erzeugt.
        jakobiMatrix = new Jakobimatrix2d(this.messpunkte);
    }

    // Falls das dreidimensionale Ausgleichsproblem gelst werden soll, ...
    else if (ausgleichsproblemtyp == Ausgleichsproblemtyp.ORTSKURVE_3d) {
        // Die Modellgleichungen (die Kreisgleichungen) werden erzeugt.
        modellgleichungen = new Modellgleichungen(this.messpunkte);

        // Die Jakobi-Matrix der Modellgleichungen (der Kreisgleichungen) wird erzeugt.
        jakobiMatrix = new Jakobimatrix(this.messpunkte);
    }

    // Das Ausgleichsproblem wird gelst, wobei hchstens 200 Iterationsschritte durchgefhrt werden.
    double[] ortskurvenparameter = null;
    try {
        PointVectorValuePair endParameter = this.gaussNewtonOptimizer.optimize(new Weight(gewichte),
                new Target(zielwerte), new InitialGuess(startpunkt), new MaxEval(200),
                new ModelFunction(modellgleichungen), new ModelFunctionJacobian(jakobiMatrix));

        ortskurvenparameter = endParameter.getPoint();
    }

    // Falls das Verfahren nicht konvergiert hat, wird die entsprechende Ausnahme gefangen.
    catch (TooManyEvaluationsException e) {
        // Die Fehlermeldung fr den Entwickler wird erzeugt und protokolliert.
        String fehlermeldung = "Der Gauss-Newton-Algorithmus konvergiert nicht!";
        Ausgleichsproblem.logger.severe(fehlermeldung);

        // Die Ausnahme wird erzeugt und mit der Fehlermeldung fr den Benutzer initialisiert.
        String jsfMeldung = "Der Gauss-Newton-Algorithmus zur Berechnung der Ortskurve konvergiert nicht! "
                + "berprfen Sie bitte, ob die eingegebenen Punkte annhernd auf einem Kreis liegen.";
        ApplicationRuntimeException applicationRuntimeException = new ApplicationRuntimeException(jsfMeldung);

        throw applicationRuntimeException;
    }

    // Die Referenz auf die Ortskurve wird deklariert.
    Ortskurve ortskurve = null;

    // Falls das zweidimensionale Ausgleichsproblem gelst werden soll, ...
    if (ausgleichsproblemtyp == Ausgleichsproblemtyp.ORTSKURVE_2d) {
        ortskurve = new Ortskurve(new Vector2D(ortskurvenparameter[0], 0.0), ortskurvenparameter[1]);
    }

    // Falls das dreidimensionale Ausgleichsproblem gelst werden soll, ...
    else if (ausgleichsproblemtyp == Ausgleichsproblemtyp.ORTSKURVE_3d) {
        ortskurve = new Ortskurve(new Vector2D(ortskurvenparameter[0], ortskurvenparameter[1]),
                ortskurvenparameter[2]);
    }

    // Die berechnete Ortskurve wird zurckgegeben.
    return ortskurve;
}

From source file:edu.mit.isos.app.water.WaterControllerState.java

@Override
public void iterateTick(ElementImpl element, long duration) {
    Set<WaterPlant> plants = new HashSet<WaterPlant>();
    Set<WaterPipeline> pipelines = new HashSet<WaterPipeline>();
    Set<WaterElementImpl> systems = new HashSet<WaterElementImpl>();
    for (ElementImpl e : elements) {
        if (e instanceof WaterPlant && ((WaterPlant) e).isOperating()) {
            plants.add((WaterPlant) e);//from  www  . j ava  2s.c  om
        }
        if (e instanceof WaterPipeline) {
            pipelines.add((WaterPipeline) e);
        }
        if (e instanceof WaterElementImpl) {
            systems.add((WaterElementImpl) e);
        }
    }

    List<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
    double[] costCoefficients = new double[elements.size()];
    double[] initialValues = new double[elements.size()];
    for (WaterElementImpl e : systems) {
        // cost of lifting aquifer is 1
        costCoefficients[elements.indexOf(e)] = 1;
        initialValues[elements.indexOf(e)] = ((WaterElementState) e.getState()).getProduced(e, duration)
                .getQuantity(ResourceType.WATER);
    }
    for (WaterPlant e : plants) {
        initialValues[elements.indexOf(e)] = e.getOperatingState().getProduced(e, duration)
                .getQuantity(ResourceType.WATER);
        double[] productionCoefficients = new double[elements.size()];
        productionCoefficients[elements.indexOf(e)] = 1;
        constraints.add(new LinearConstraint(productionCoefficients, Relationship.LEQ,
                e.getOperatingState().productionCapacity.multiply(duration).getQuantity(ResourceType.WATER)));
    }
    for (WaterPipeline e : pipelines) {
        initialValues[elements.indexOf(e)] = e.getOperatingState().getOutput(e, duration)
                .getQuantity(ResourceType.WATER);
        double[] outputCoefficients = new double[elements.size()];
        outputCoefficients[elements.indexOf(e)] = 1;
        constraints.add(new LinearConstraint(outputCoefficients, Relationship.LEQ,
                e.getOperatingState().outputCapacity.multiply(duration).getQuantity(ResourceType.WATER)));
    }

    for (WaterElementImpl e : systems) {
        double[] flowCoefficients = new double[elements.size()];
        flowCoefficients[elements.indexOf(e)] = 1; // system production
        for (WaterPlant plant : plants) {
            if (plant.getLocation().equals(e.getLocation())) {
                flowCoefficients[elements.indexOf(plant)] = 1; // plant production
            }
        }
        for (WaterPipeline pipeline : pipelines) {
            if (pipeline.getLocation().getOrigin().equals(e.getLocation().getOrigin())) {
                flowCoefficients[elements.indexOf(pipeline)] = -1 / pipeline.getOperatingState().eta; // pipeline input
            } else if (pipeline.getLocation().getDestination().equals(e.getLocation().getOrigin())) {
                flowCoefficients[elements.indexOf(pipeline)] = 1; // pipeline output
            }
        }
        constraints.add(new LinearConstraint(flowCoefficients, Relationship.EQ,
                ((WaterElementState) e.getState()).getSent(e, duration).getQuantity(ResourceType.WATER)));
    }

    try {
        // Run optimization and get results.
        PointValuePair output = new SimplexSolver().optimize(GoalType.MINIMIZE, new MaxIter(1000),
                new NonNegativeConstraint(true), new LinearConstraintSet(constraints),
                new LinearObjectiveFunction(costCoefficients, 0d), new InitialGuess(initialValues));
        for (WaterElementImpl e : systems) {
            e.getOperatingState().setProduced(e,
                    ResourceFactory.create(ResourceType.WATER, output.getPoint()[elements.indexOf(e)]),
                    duration);
        }
        for (WaterPlant e : plants) {
            e.getOperatingState().setProduced(e,
                    ResourceFactory.create(ResourceType.WATER, output.getPoint()[elements.indexOf(e)]),
                    duration);
        }
        for (WaterPipeline e : pipelines) {
            e.getOperatingState().setOutput(e,
                    ResourceFactory.create(ResourceType.WATER, output.getPoint()[elements.indexOf(e)]),
                    duration);
        }
    } catch (TooManyIterationsException ignore) {
        // Don't overwrite existing values.
        ignore.printStackTrace();
    } catch (NoFeasibleSolutionException ignore) {
        // Don't overwrite existing values.
        ignore.printStackTrace();
    }

    for (WaterElementImpl system : systems) {
        Resource received = system.getOperatingState().getConsumed(system, duration)
                .get(ResourceType.ELECTRICITY);
        for (WaterPlant plant : plants) {
            if (plant.getLocation().equals(system.getLocation())) {
                received = received.add(
                        plant.getOperatingState().getConsumed(plant, duration).get(ResourceType.ELECTRICITY));
            }
        }
        for (WaterPipeline pipeline : pipelines) {
            if (pipeline.getLocation().getOrigin().equals(system.getLocation().getOrigin())) {
                received = received.add(pipeline.getOperatingState().getInput(pipeline, duration)
                        .get(ResourceType.ELECTRICITY));
            }
        }
        system.getOperatingState().setReceived(system, received, duration);
    }
}

From source file:com.itemanalysis.psychometrics.irt.equating.HaebaraMethodTest.java

/**
 * Item parameters and true results from Kolen's STUIRT program.
 *///from w  ww  . j  a  va 2  s. c  o  m
@Test
public void haebaraTest1() {
    System.out.println("Haebara Test 1: Actual Distribution");

    LinkedHashMap<String, ItemResponseModel> irmX = new LinkedHashMap<String, ItemResponseModel>();
    LinkedHashMap<String, ItemResponseModel> irmY = new LinkedHashMap<String, ItemResponseModel>();

    irmX.put("i1", new Irm3PL(0.4551, -0.7101, 0.2087, 1.7));
    irmX.put("i2", new Irm3PL(0.5839, -0.8567, 0.2038, 1.7));
    irmX.put("i3", new Irm3PL(0.7544, 0.0212, 0.1600, 1.7));
    irmX.put("i4", new Irm3PL(0.6633, 0.0506, 0.1240, 1.7));
    irmX.put("i5", new Irm3PL(1.0690, 0.9610, 0.2986, 1.7));
    irmX.put("i6", new Irm3PL(0.9672, 0.1950, 0.0535, 1.7));
    irmX.put("i7", new Irm3PL(0.3479, 2.2768, 0.1489, 1.7));
    irmX.put("i8", new Irm3PL(1.4579, 1.0241, 0.2453, 1.7));
    irmX.put("i9", new Irm3PL(1.8811, 1.4062, 0.1992, 1.7));
    irmX.put("i10", new Irm3PL(0.7020, 2.2401, 0.0853, 1.7));
    irmX.put("i11", new Irm3PL(1.4080, 1.5556, 0.0789, 1.7));
    irmX.put("i12", new Irm3PL(1.2993, 2.1589, 0.1075, 1.7));

    irmY.put("i1", new Irm3PL(0.4416, -1.3349, 0.1559, 1.7));
    irmY.put("i2", new Irm3PL(0.5730, -1.3210, 0.1913, 1.7));
    irmY.put("i3", new Irm3PL(0.5987, -0.7098, 0.1177, 1.7));
    irmY.put("i4", new Irm3PL(0.6041, -0.3539, 0.0818, 1.7));
    irmY.put("i5", new Irm3PL(0.9902, 0.5320, 0.3024, 1.7));
    irmY.put("i6", new Irm3PL(0.8081, -0.1156, 0.0648, 1.7));
    irmY.put("i7", new Irm3PL(0.4140, 2.5538, 0.2410, 1.7));
    irmY.put("i8", new Irm3PL(1.3554, 0.5811, 0.2243, 1.7));
    irmY.put("i9", new Irm3PL(1.0417, 0.9392, 0.1651, 1.7));
    irmY.put("i10", new Irm3PL(0.6336, 1.8960, 0.0794, 1.7));
    irmY.put("i11", new Irm3PL(1.1347, 1.0790, 0.0630, 1.7));
    irmY.put("i12", new Irm3PL(0.9255, 2.1337, 0.1259, 1.7));

    double[] points = { -4.0000, -3.1110, -2.2220, -1.3330, -0.4444, 0.4444, 1.3330, 2.2220, 3.1110, 4.0000 };
    double[] xDensity = { 0.0001008, 0.002760, 0.03021, 0.1420, 0.3149, 0.3158, 0.1542, 0.03596, 0.003925,
            0.0001862 };
    double[] yDensity = { 0.0001173, 0.003242, 0.03449, 0.1471, 0.3148, 0.3110, 0.1526, 0.03406, 0.002510,
            0.0001116 };
    UserSuppliedDistributionApproximation distX = new UserSuppliedDistributionApproximation(points, xDensity);
    UserSuppliedDistributionApproximation distY = new UserSuppliedDistributionApproximation(points, yDensity);

    HaebaraMethod hb = new HaebaraMethod(irmX, irmY, distX, distY, EquatingCriterionType.Q1Q2);
    hb.setPrecision(6);
    double[] startValues = { 0, 1 };

    int numIterpolationPoints = 2 * 2;//two dimensions A and B
    BOBYQAOptimizer underlying = new BOBYQAOptimizer(numIterpolationPoints);
    RandomGenerator g = new JDKRandomGenerator();
    RandomVectorGenerator generator = new UncorrelatedRandomVectorGenerator(2, new GaussianRandomGenerator(g));
    MultiStartMultivariateOptimizer optimizer = new MultiStartMultivariateOptimizer(underlying, 10, generator);
    org.apache.commons.math3.optim.PointValuePair optimum = optimizer.optimize(new MaxEval(1000),
            new ObjectiveFunction(hb), GoalType.MINIMIZE, SimpleBounds.unbounded(2),
            new InitialGuess(startValues));

    double[] hbCoefficients = optimum.getPoint();
    hb.setIntercept(hbCoefficients[0]);
    hb.setScale(hbCoefficients[1]);

    System.out.println("  Iterations: " + optimizer.getEvaluations());
    System.out.println("  fmin: " + optimum.getValue());
    System.out.println("  B = " + hbCoefficients[0] + "  A = " + hbCoefficients[1]);

    assertEquals("  Intercept test", -0.471281, hb.getIntercept(), 1e-4);
    assertEquals("  Scale test", 1.067800, hb.getScale(), 1e-4);

    System.out.println();

}

From source file:com.insightml.math.optimization.AbstractOptimizable.java

private PointValuePair optimize(final MultivariateOptimizer optimizer, final double[] initialValues,
        final OptimizationData... data) {
    final OptimizationData[] d = new OptimizationData[5 + data.length];
    d[0] = new MaxIter(convergence.maxIt + 1);
    d[1] = new MaxEval(convergence.maxIt * 2);
    d[2] = new ObjectiveFunction(this);
    d[3] = GoalType.MAXIMIZE;//from   w w w. j a va  2s  .c o  m
    d[4] = new InitialGuess(fixBounds(initialValues));
    for (int i = 0; i < data.length; ++i) {
        d[5 + i] = data[i];
    }
    return optimizer.optimize(d);
}

From source file:com.itemanalysis.psychometrics.irt.equating.StockingLordMethodTest.java

/**
 * Item parameters, quadrature points and weights, and "true" linking coefficients were obtained
 * from example1 in Kolen's STUIRT program. However, the Stocking-Lord procedure was changed in
 * the STUIRT example to produce symmetric optimization of the criterion function (i.e. SY BI BI
 * instead of SY BI NO)./*w  w  w.  j  ava  2 s .c o m*/
 *
 * This test runs the Stocking-Lord procedure twice: One with the UNCMIN optimizer and once with
 * the BOBYQA optimizer. Results from each are compared to teh STUIRT results and to each other.
 *
 */
@Test
public void stockingLordTest0() {
    System.out.println("StockingLordMethod Test 0: Actual Distribution");
    LinkedHashMap<String, ItemResponseModel> irmX = new LinkedHashMap<String, ItemResponseModel>();
    LinkedHashMap<String, ItemResponseModel> irmY = new LinkedHashMap<String, ItemResponseModel>();

    irmX.put("i1", new Irm3PL(0.4551, -0.7101, 0.2087, 1.7));
    irmX.put("i2", new Irm3PL(0.5839, -0.8567, 0.2038, 1.7));
    irmX.put("i3", new Irm3PL(0.7544, 0.0212, 0.1600, 1.7));
    irmX.put("i4", new Irm3PL(0.6633, 0.0506, 0.1240, 1.7));
    irmX.put("i5", new Irm3PL(1.0690, 0.9610, 0.2986, 1.7));
    irmX.put("i6", new Irm3PL(0.9672, 0.1950, 0.0535, 1.7));
    irmX.put("i7", new Irm3PL(0.3479, 2.2768, 0.1489, 1.7));
    irmX.put("i8", new Irm3PL(1.4579, 1.0241, 0.2453, 1.7));
    irmX.put("i9", new Irm3PL(1.8811, 1.4062, 0.1992, 1.7));
    irmX.put("i10", new Irm3PL(0.7020, 2.2401, 0.0853, 1.7));
    irmX.put("i11", new Irm3PL(1.4080, 1.5556, 0.0789, 1.7));
    irmX.put("i12", new Irm3PL(1.2993, 2.1589, 0.1075, 1.7));

    irmY.put("i1", new Irm3PL(0.4416, -1.3349, 0.1559, 1.7));
    irmY.put("i2", new Irm3PL(0.5730, -1.3210, 0.1913, 1.7));
    irmY.put("i3", new Irm3PL(0.5987, -0.7098, 0.1177, 1.7));
    irmY.put("i4", new Irm3PL(0.6041, -0.3539, 0.0818, 1.7));
    irmY.put("i5", new Irm3PL(0.9902, 0.5320, 0.3024, 1.7));
    irmY.put("i6", new Irm3PL(0.8081, -0.1156, 0.0648, 1.7));
    irmY.put("i7", new Irm3PL(0.4140, 2.5538, 0.2410, 1.7));
    irmY.put("i8", new Irm3PL(1.3554, 0.5811, 0.2243, 1.7));
    irmY.put("i9", new Irm3PL(1.0417, 0.9392, 0.1651, 1.7));
    irmY.put("i10", new Irm3PL(0.6336, 1.8960, 0.0794, 1.7));
    irmY.put("i11", new Irm3PL(1.1347, 1.0790, 0.0630, 1.7));
    irmY.put("i12", new Irm3PL(0.9255, 2.1337, 0.1259, 1.7));

    double[] points = { -4.0000, -3.1110, -2.2220, -1.3330, -0.4444, 0.4444, 1.3330, 2.2220, 3.1110, 4.0000 };
    double[] xDensity = { 0.0001008, 0.002760, 0.03021, 0.1420, 0.3149, 0.3158, 0.1542, 0.03596, 0.003925,
            0.0001862 };
    double[] yDensity = { 0.0001173, 0.003242, 0.03449, 0.1471, 0.3148, 0.3110, 0.1526, 0.03406, 0.002510,
            0.0001116 };
    UserSuppliedDistributionApproximation distX = new UserSuppliedDistributionApproximation(points, xDensity);
    UserSuppliedDistributionApproximation distY = new UserSuppliedDistributionApproximation(points, yDensity);

    StockingLordMethod sl = new StockingLordMethod(irmX, irmY, distX, distY, EquatingCriterionType.Q1Q2);
    sl.setPrecision(6);
    double[] startValues = { 0, 1 };
    double f = 0;

    //Run test with UNCMIN optimizer
    DefaultUncminOptimizer optimizer = new DefaultUncminOptimizer();
    double[] param1 = null;
    double[] param2 = null;
    try {
        optimizer.minimize(sl, startValues);
        param1 = optimizer.getParameters();
        f = optimizer.getFunctionValue();
        sl.setIntercept(param1[0]);
        sl.setScale(param1[1]);

    } catch (UncminException ex) {
        ex.printStackTrace();
    }

    //Check UNCMIN values against results from STUIRT.
    System.out.println("  UNCMIN Optimization");
    System.out.println("  Iterations: ");
    System.out.println("  fmin: " + f);
    System.out.println("  B = " + sl.getIntercept() + "  A = " + sl.getScale());

    assertEquals("  Intercept test", -0.487619, sl.getIntercept(), 1e-6);
    assertEquals("  Scale test", 1.083417, sl.getScale(), 1e-6);

    //Run test with BOBYQA optimizer
    int numIterpolationPoints = 2 * 2;//two dimensions A and B
    BOBYQAOptimizer underlying = new BOBYQAOptimizer(numIterpolationPoints);
    RandomGenerator g = new JDKRandomGenerator();
    RandomVectorGenerator generator = new UncorrelatedRandomVectorGenerator(2, new GaussianRandomGenerator(g));
    MultiStartMultivariateOptimizer optimizer2 = new MultiStartMultivariateOptimizer(underlying, 10, generator);
    PointValuePair optimum = optimizer2.optimize(new MaxEval(1000), new ObjectiveFunction(sl),
            GoalType.MINIMIZE, SimpleBounds.unbounded(2), new InitialGuess(startValues));

    param2 = optimum.getPoint();
    sl.setIntercept(param2[0]);
    sl.setScale(param2[1]);

    //Check BOBYQA values against results from STUIRT.
    System.out.println();
    System.out.println("  BOBYQA Optimization");
    System.out.println("  Iterations: " + optimizer2.getEvaluations());
    System.out.println("  fmin: " + optimum.getValue());
    System.out.println("  B = " + param2[0] + "  A = " + param2[1]);

    assertEquals("  Intercept test", -0.487619, sl.getIntercept(), 1e-6);
    assertEquals("  Scale test", 1.083417, sl.getScale(), 1e-6);

    //Compare results from each optimizer
    assertEquals("  Intercept test", param1[0], param2[0], 1e-6);
    assertEquals("  Scale test", param1[1], param2[1], 1e-6);

    System.out.println();

}

From source file:hulo.localization.models.obs.GaussianProcessLDPLMean.java

static PointValuePair minimize(MultivariateFunction func, double[] pointInit, double[] diffPointInit) {
    SimplexOptimizer optimizer = new SimplexOptimizer(1e-5, 1e-10);
    NelderMeadSimplex simplex = new NelderMeadSimplex(diffPointInit);
    ObjectiveFunction objFunc = new ObjectiveFunction(func);
    InitialGuess initGuess = new InitialGuess(pointInit);
    PointValuePair pair = optimizer.optimize(maxEval, objFunc, GoalType.MINIMIZE, initGuess, simplex);
    return pair;/*w w  w  . ja v  a2s.c om*/
}

From source file:com.itemanalysis.psychometrics.irt.equating.HaebaraMethodTest.java

@Test
public void haearaTest2() {
    System.out.println("Haebara Test 2: Uniform Distribution");
    int n = aX.length;
    LinkedHashMap<String, ItemResponseModel> irmX = new LinkedHashMap<String, ItemResponseModel>();
    LinkedHashMap<String, ItemResponseModel> irmY = new LinkedHashMap<String, ItemResponseModel>();
    ItemResponseModel irm;/*from   w  ww. j ava2 s .  c  o m*/

    for (int i = 0; i < n; i++) {
        String name = "V" + i;
        irm = new Irm3PL(aX[i], bX[i], cX[i], 1.0);
        irmX.put(name, irm);

        irm = new Irm3PL(aY[i], bY[i], cY[i], 1.0);
        irmY.put(name, irm);
    }

    UniformDistributionApproximation uniform = new UniformDistributionApproximation(-4, 4, 10);

    HaebaraMethod hb = new HaebaraMethod(irmX, irmY, uniform, uniform, EquatingCriterionType.Q1Q2);
    hb.setPrecision(4);
    double[] startValues = { 0, 1 };

    int numIterpolationPoints = 2 * 2;//two dimensions A and B
    BOBYQAOptimizer underlying = new BOBYQAOptimizer(numIterpolationPoints);
    RandomGenerator g = new JDKRandomGenerator();
    RandomVectorGenerator generator = new UncorrelatedRandomVectorGenerator(2, new GaussianRandomGenerator(g));
    MultiStartMultivariateOptimizer optimizer = new MultiStartMultivariateOptimizer(underlying, 10, generator);
    org.apache.commons.math3.optim.PointValuePair optimum = optimizer.optimize(new MaxEval(1000),
            new ObjectiveFunction(hb), org.apache.commons.math3.optim.nonlinear.scalar.GoalType.MINIMIZE,
            SimpleBounds.unbounded(2), new InitialGuess(startValues));

    double[] slCoefficients = optimum.getPoint();
    hb.setIntercept(slCoefficients[0]);
    hb.setScale(slCoefficients[1]);

    System.out.println("  Iterations: " + optimizer.getEvaluations());
    System.out.println("  fmin: " + optimum.getValue());
    System.out.println("  B = " + slCoefficients[0] + "  A = " + slCoefficients[1]);

    assertEquals("  Intercept test", -0.4303, hb.getIntercept(), 1e-4);
    assertEquals("  Scale test", 1.0894, hb.getScale(), 1e-4);

    System.out.println();

}

From source file:edu.cmu.tetrad.search.Ling.java

private void optimizeNonGaussianity(final int rowIndex, final DoubleMatrix2D dataSetMatrix, final double[][] W,
        List<Mapping> allMappings) {
    final List<Mapping> mappings = mappingsForRow(rowIndex, allMappings);

    MultivariateFunction function = new MultivariateFunction() {
        public double value(double[] values) {
            for (int i = 0; i < values.length; i++) {
                Mapping mapping = mappings.get(i);
                W[mapping.getI()][mapping.getJ()] = values[i];
            }/*  w  ww.jav a  2s  .  c  om*/

            double v = ngFullData(rowIndex, dataSetMatrix, W);

            if (Double.isNaN(v))
                return 10000;

            return -(v);
        }
    };

    {
        double[] values = new double[mappings.size()];

        for (int k = 0; k < mappings.size(); k++) {
            Mapping mapping = mappings.get(k);
            values[k] = W[mapping.getI()][mapping.getJ()];
        }

        MultivariateOptimizer search = new PowellOptimizer(1e-7, 1e-7);

        PointValuePair pair = search.optimize(new InitialGuess(values), new ObjectiveFunction(function),
                GoalType.MINIMIZE, new MaxEval(100000));

        values = pair.getPoint();

        for (int k = 0; k < mappings.size(); k++) {
            Mapping mapping = mappings.get(k);
            W[mapping.getI()][mapping.getJ()] = values[k];
        }
    }

}

From source file:com.itemanalysis.psychometrics.irt.equating.StockingLordMethodTest.java

@Test
public void stockingLordTest1() {
    System.out.println("StockingLordMethod Test 1: Actual Distribution");
    int n = aX.length;
    LinkedHashMap<String, ItemResponseModel> irmX = new LinkedHashMap<String, ItemResponseModel>();
    LinkedHashMap<String, ItemResponseModel> irmY = new LinkedHashMap<String, ItemResponseModel>();
    ItemResponseModel irm;//w  w  w.jav a 2s .  c  o m

    for (int i = 0; i < n; i++) {
        String name = "V" + i;
        irm = new Irm3PL(aX[i], bX[i], cX[i], 1.0);
        irmX.put(name, irm);

        irm = new Irm3PL(aY[i], bY[i], cY[i], 1.0);
        irmY.put(name, irm);
    }

    UserSuppliedDistributionApproximation distX = new UserSuppliedDistributionApproximation(points, xDensity);
    UserSuppliedDistributionApproximation distY = new UserSuppliedDistributionApproximation(points, yDensity);

    StockingLordMethod sl = new StockingLordMethod(irmX, irmY, distX, distY, EquatingCriterionType.Q1Q2);
    sl.setPrecision(4);
    double[] startValues = { 0, 1 };

    int numIterpolationPoints = 2 * 2;//two dimensions A and B
    BOBYQAOptimizer underlying = new BOBYQAOptimizer(numIterpolationPoints);
    RandomGenerator g = new JDKRandomGenerator();
    RandomVectorGenerator generator = new UncorrelatedRandomVectorGenerator(2, new GaussianRandomGenerator(g));
    MultiStartMultivariateOptimizer optimizer = new MultiStartMultivariateOptimizer(underlying, 10, generator);
    PointValuePair optimum = optimizer.optimize(new MaxEval(1000), new ObjectiveFunction(sl), GoalType.MINIMIZE,
            SimpleBounds.unbounded(2), new InitialGuess(startValues));

    double[] slCoefficients = optimum.getPoint();
    sl.setIntercept(slCoefficients[0]);
    sl.setScale(slCoefficients[1]);

    System.out.println("  Iterations: " + optimizer.getEvaluations());
    System.out.println("  fmin: " + optimum.getValue());
    System.out.println("  B = " + slCoefficients[0] + "  A = " + slCoefficients[1]);

    assertEquals("  Intercept test", -0.4788, sl.getIntercept(), 1e-4);
    assertEquals("  Scale test", 1.0911, sl.getScale(), 1e-4);

    System.out.println();

}