Search in sources :

Example 1 with GeneArrayMeanModel

use of ntuple.GeneArrayMeanModel 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;
}
Also used : GeneArrayMeanModel(ntuple.GeneArrayMeanModel) ScoredVec(ntuple.ScoredVec)

Aggregations

GeneArrayMeanModel (ntuple.GeneArrayMeanModel)1 ScoredVec (ntuple.ScoredVec)1