summaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/mlr/ga/Individual.java
blob: 5b28b176341ed6f9dbfdd97baacdd613c19ece8e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.mlr.ga;

import com.yahoo.searchlib.rankingexpression.RankingExpression;

import java.util.Collections;
import java.util.List;

/**
 * An individual in an evolving population - a genome with a fitness score.
 * Individuals are comparable by decreasing fitness.
 * <p>
 * As we are training ranking expressions, the genome, here, is the ranking expression.
 *
 * @author bratseth
 */
public class Individual extends Evolvable {

    private final RankingExpression genome;
    private final TrainingSet trainingSet;
    private final double fitness;

    public Individual(RankingExpression genome, TrainingSet trainingSet) {
        this.genome = genome;
        this.trainingSet = trainingSet;
        this.fitness = trainingSet.evaluate(genome);
    }

    public RankingExpression getGenome() { return genome; }

    public double calculateAverageError() {
        return trainingSet.calculateAverageError(genome);
    }

    public double calculateAverageErrorPercentage() {
        return trainingSet.calculateAverageErrorPercentage(genome);
    }

    @Override
    public double getFitness() { return fitness; }

    @Override
    public Individual makeSuccessor(int memberNumber, List<RankingExpression> genepool, TrainingEnvironment environment) {
        return new Individual(environment.recombiner().recombine(genome, genepool), trainingSet);
    }

    @Override
    public RankingExpression getGenepool() {
        return genome;
    }

    @Override
    public String toString() {
        return toSomewhatShortString() + ", expression: " + genome;
    }

    /** Returns a shorter string describing this (not including the expression */
    public String toSomewhatShortString() {
        return "Error % " + calculateAverageErrorPercentage() +
                " average error " + calculateAverageError() +
                " fitness " + getFitness();
    }

    /** Returns a shorter string describing this (not including the expression */
    public String toShortString() {
        return "Error: " + calculateAverageErrorPercentage() + " %";
    }

}