use of ntuple.ScoredVec in project SimpleAsteroids by ljialin.
the class PairedDifferenceLearner method learn.
@Override
public int[] learn(List<ScoredVec> scoredVecs, NoisySolutionEvaluator evaluator) {
int n = evaluator.searchSpace().nDims();
fitness = new double[scoredVecs.size()];
GeneArrayModel geneArrayModel = new GeneArrayModel(evaluator.searchSpace());
for (int i = 0; i < scoredVecs.size(); i++) {
for (int j = 0; j < i; j++) {
// System.out.println(i + " <> " + j);
ScoredVec svi = scoredVecs.get(i);
ScoredVec svj = scoredVecs.get(j);
if (diffUpdate) {
geneArrayModel.updateModelDiff(svi, svj);
} else {
if (svi.score > svj.score) {
geneArrayModel.updateModel(svi, svj);
} else {
geneArrayModel.updateModel(svj, svi);
}
}
}
fitness[i] = evaluator.trueFitness(geneArrayModel.argMax());
}
// geneArrayModel.report();
return geneArrayModel.argMax();
}
use of ntuple.ScoredVec in project SimpleAsteroids by ljialin.
the class MeanLearner method learn.
@Override
public int[] learn(List<ScoredVec> scoredVecs, NoisySolutionEvaluator evaluator) {
int n = evaluator.searchSpace().nDims();
fitness = new double[scoredVecs.size()];
// set up the models
GeneMeanModel[] models = new GeneMeanModel[n];
for (int i = 0; i < n; i++) models[i] = new GeneMeanModel(evaluator.searchSpace().nValues(i));
// now train the,
int ix = 0;
for (ScoredVec sv : scoredVecs) {
// update it here
for (int i = 0; i < n; i++) {
models[i].updateMean(sv.p[i], sv.score);
}
int[] p = argmax(models);
// System.out.println(Arrays.toString(p));
fitness[ix++] = evaluator.trueFitness(p);
}
// now return the argmax array
return argmax(models);
}
use of ntuple.ScoredVec in project SimpleAsteroids by ljialin.
the class ProblemInstance method useVecsAroundRandomPoint.
public ProblemInstance useVecsAroundRandomPoint() {
scoredVecs = new ArrayList<>();
int[] px = SearchSpaceUtil.randomPoint(evaluator.searchSpace());
DefaultMutator mutator = new DefaultMutator(evaluator.searchSpace());
// 1.0; // 3 / n;
mutator.pointProb = 0.0;
// mutator
DefaultMutator.flipAtLeastOneValueDefault = true;
DefaultMutator.totalRandomChaosMutation = false;
for (int i = 0; i < k; i++) {
// int[] p = SearchSpaceUtil.randomPoint(searchSpace);
int[] p = mutator.randMut(px);
double score = evaluator.evaluate(p);
scoredVecs.add(new ScoredVec(p, score));
// System.out.println(Arrays.toString(p) + " : " + String.format("%.2f\t %.0f", score, evaluator.trueFitness(p)));
}
return this;
}
use of ntuple.ScoredVec in project SimpleAsteroids by ljialin.
the class ProblemInstance method useRandomVecs.
public ProblemInstance useRandomVecs() {
scoredVecs = new ArrayList<>();
for (int i = 0; i < k; i++) {
// int[] p = SearchSpaceUtil.randomPoint(searchSpace);
int[] p = SearchSpaceUtil.randomPoint(evaluator.searchSpace());
double score = evaluator.evaluate(p);
scoredVecs.add(new ScoredVec(p, score));
// System.out.println(Arrays.toString(p) + " : " + String.format("%.2f\t %.0f", score, evaluator.trueFitness(p)));
}
return this;
}
use of ntuple.ScoredVec in project SimpleAsteroids by ljialin.
the class RankCorrelationEDA method runTrial.
@Override
public int[] runTrial(SolutionEvaluator evaluator, int nEvals) {
this.evaluator = evaluator;
// set up some convenient references
SearchSpace searchSpace = evaluator.searchSpace();
int n = searchSpace.nDims();
history = new ArrayList<>();
geneArrayModel = new GeneArrayMeanModel(searchSpace);
int nSteps = 0;
Long endTime = null;
if (timeLimit != null) {
// endTime = timeLimit + System.currentTimeMillis();
endTime = timeLimit + System.nanoTime() / 1000000;
}
while (evaluator.nEvals() < nEvals && (endTime == null || System.nanoTime() / 1000000 < endTime)) {
int prevEvals = evaluator.nEvals();
// each time around evaluate a single new individual: x
// but occasionally have the possibility of sampling the best guess so far
int[] x = geneArrayModel.generate();
double f = fitness(evaluator, x, nSamples).mean();
ScoredVec scoredVec = new ScoredVec(x, f);
// now treat the history like a circular buffer and update it
// always add the ScoredVector in
geneArrayModel.updateModelMean(scoredVec);
// geneArrayModel.report();
if (history.size() < historyLength) {
history.add(scoredVec);
} else {
// if we're replacing one in the history
// then remove it from our stats
int ix = nSteps % historyLength;
geneArrayModel.removeVec(history.get(ix));
history.set(ix, scoredVec);
}
nSteps++;
int diffEvals = evaluator.nEvals() - prevEvals;
for (int i = 0; i < diffEvals; i++) {
evaluator.logger().logBestYest(geneArrayModel.argMax());
}
// if (verbose) {
// int[] solution = CompactGAUtil.argmax(pVec);
// System.out.format("%.3f\t %s\n", evaluator.evaluate(solution), Arrays.toString(solution));
// // System.out.println(Arrays.toString(pVec));
// for (double p : pVec) {
// System.out.format("%.4f\t", p);
// }
// System.out.println();
// System.out.println();
//
// }
}
// now draw each x and y vec according to pVec
// indeed, what to return
// finally, return the argmax of each dimension
int[] solution = geneArrayModel.argMax();
// logger.
evaluator.logger().keepBest(solution, evaluator.evaluate(solution));
return solution;
}
Aggregations