use of org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer in project tetrad by cmu-phil.
the class Lofs2 method optimizeRow.
private void optimizeRow(final int rowIndex, final TetradMatrix data, final double range, final List<List<Integer>> rows, final List<List<Double>> parameters) {
System.out.println("A");
final int numParams = rows.get(rowIndex).size();
final double[] dLeftMin = new double[numParams];
final double[] dRightMin = new double[numParams];
double[] values = new double[numParams];
double delta = 0.1;
if (false) {
// isEdgeCorrected()) {
double min = -2;
double max = 2;
int[] dims = new int[values.length];
int numBins = 5;
for (int i = 0; i < values.length; i++) dims[i] = numBins;
CombinationGenerator gen = new CombinationGenerator(dims);
int[] comb;
List<Double> maxParams = new ArrayList<>();
for (int i = 0; i < values.length; i++) maxParams.add(0.0);
double maxV = Double.NEGATIVE_INFINITY;
while ((comb = gen.next()) != null) {
if (Thread.currentThread().isInterrupted()) {
break;
}
List<Double> params = new ArrayList<>();
for (int i = 0; i < values.length; i++) {
params.add(min + (max - min) * (comb[i] / (double) numBins));
}
parameters.set(rowIndex, params);
double v = scoreRow(rowIndex, data, rows, parameters);
if (v > maxV) {
maxV = v;
maxParams = params;
}
}
System.out.println("maxparams = " + maxParams);
parameters.set(rowIndex, maxParams);
for (int i = 0; i < values.length; i++) {
dLeftMin[i] = -range;
dRightMin[i] = range;
values[i] = maxParams.get(i);
}
} else if (false) {
for (int i = 0; i < numParams; i++) {
if (Thread.currentThread().isInterrupted()) {
break;
}
parameters.get(rowIndex).set(i, -range);
double vLeft = scoreRow(rowIndex, data, rows, parameters);
double dLeft = -range;
// Search from the left for the first valley; mark that as dleft.
for (double d = -range + delta; d < range; d += delta) {
if (Thread.currentThread().isInterrupted()) {
break;
}
parameters.get(rowIndex).set(i, d);
double v = scoreRow(rowIndex, data, rows, parameters);
if (Double.isNaN(v))
continue;
if (v > vLeft)
break;
vLeft = v;
dLeft = d;
}
parameters.get(rowIndex).set(i, range);
double vRight = scoreRow(rowIndex, data, rows, parameters);
double dRight = range;
// to avoid high scores at the boundaries.
for (double d = range - delta; d > -range; d -= delta) {
if (Thread.currentThread().isInterrupted()) {
break;
}
parameters.get(rowIndex).set(i, d);
double v = scoreRow(rowIndex, data, rows, parameters);
if (Double.isNaN(v))
continue;
if (v > vRight)
break;
vRight = v;
dRight = d;
}
// If dleft dright ended up reversed, re-reverse them.
if (dLeft > dRight) {
double temp = dRight;
dLeft = dRight;
dRight = temp;
}
System.out.println("dLeft = " + dLeft + " dRight = " + dRight);
dLeftMin[i] = dLeft;
dRightMin[i] = dRight;
values[i] = (dLeft + dRight) / 2.0;
}
} else {
// Default case: search for the maximum score over the entire range.
for (int i = 0; i < numParams; i++) {
if (Thread.currentThread().isInterrupted()) {
break;
}
dLeftMin[i] = -range;
dRightMin[i] = range;
values[i] = 0;
}
}
MultivariateFunction function = new MultivariateFunction() {
public double value(double[] values) {
System.out.println(Arrays.toString(values));
for (int i = 0; i < values.length; i++) {
if (Thread.currentThread().isInterrupted()) {
break;
}
parameters.get(rowIndex).set(i, values[i]);
}
double v = scoreRow(rowIndex, data, rows, parameters);
if (Double.isNaN(v)) {
// was 10000
return Double.POSITIVE_INFINITY;
}
return -v;
}
};
try {
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();
} catch (Exception e) {
e.printStackTrace();
for (int i = 0; i < values.length; i++) {
parameters.get(rowIndex).set(i, Double.NaN);
}
}
}
use of org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer in project tetrad by cmu-phil.
the class GeneralizedSemEstimator method optimize.
private double[] optimize(MultivariateFunction function, double[] values, int optimizer) {
PointValuePair pair;
if (optimizer == 1) {
// 0.01, 0.000001
// 2.0D * FastMath.ulp(1.0D), 1e-8
MultivariateOptimizer search = new PowellOptimizer(1e-7, 1e-7);
pair = search.optimize(new InitialGuess(values), new ObjectiveFunction(function), GoalType.MINIMIZE, new MaxEval(100000));
} else if (optimizer == 2) {
MultivariateOptimizer search = new SimplexOptimizer(1e-7, 1e-7);
pair = search.optimize(new InitialGuess(values), new ObjectiveFunction(function), GoalType.MINIMIZE, new MaxEval(100000), new NelderMeadSimplex(values.length));
} else if (optimizer == 3) {
int dim = values.length;
int additionalInterpolationPoints = 0;
final int numIterpolationPoints = 2 * dim + 1 + additionalInterpolationPoints;
BOBYQAOptimizer search = new BOBYQAOptimizer(numIterpolationPoints);
pair = search.optimize(new MaxEval(100000), new ObjectiveFunction(function), GoalType.MINIMIZE, new InitialGuess(values), SimpleBounds.unbounded(dim));
} else if (optimizer == 4) {
MultivariateOptimizer search = new CMAESOptimizer(3000000, .05, false, 0, 0, new MersenneTwister(), false, new SimplePointChecker<PointValuePair>(0.5, 0.5));
pair = search.optimize(new MaxEval(30000), new ObjectiveFunction(function), GoalType.MINIMIZE, new InitialGuess(values), new CMAESOptimizer.Sigma(new double[values.length]), new CMAESOptimizer.PopulationSize(1000));
} else if (optimizer == 5) {
// 0.01, 0.000001
// 2.0D * FastMath.ulp(1.0D), 1e-8
MultivariateOptimizer search = new PowellOptimizer(.05, .05);
pair = search.optimize(new InitialGuess(values), new ObjectiveFunction(function), GoalType.MINIMIZE, new MaxEval(100000));
} else if (optimizer == 6) {
MultivariateOptimizer search = new PowellOptimizer(1e-7, 1e-7);
pair = search.optimize(new InitialGuess(values), new ObjectiveFunction(function), GoalType.MAXIMIZE, new MaxEval(10000));
} else {
throw new IllegalStateException();
}
return pair.getPoint();
}
Aggregations