source: java/ecj/cecj/app/numbers_game/SingleDimensionalNumbersGame.java @ 473

Last change on this file since 473 was 193, checked in by Maciej Komosinski, 11 years ago

Set svn:eol-style native for all textual files

  • Property svn:eol-style set to native
File size: 2.7 KB
Line 
1package cecj.app.numbers_game;
2
3import java.math.BigDecimal;
4import java.math.RoundingMode;
5
6import cecj.interaction.InteractionResult;
7import cecj.interaction.RealValuedResult;
8import cecj.problems.SymmetricTestBasedProblem;
9import cecj.statistics.ObjectiveFitnessCalculator;
10import cecj.utils.Pair;
11
12import ec.EvolutionState;
13import ec.Individual;
14import ec.vector.BitVectorIndividual;
15
16public class SingleDimensionalNumbersGame extends SymmetricTestBasedProblem implements ObjectiveFitnessCalculator {
17
18        @Override
19        public Pair<? extends InteractionResult> test(EvolutionState state, Individual candidate,
20                        Individual test) {
21                if (!(candidate instanceof BitVectorIndividual) || !(test instanceof BitVectorIndividual)) {
22                        state.output.error("Competitor individuals should be represented by bit vectors\n");
23                }
24
25                BitVectorIndividual bitCandidate = (BitVectorIndividual) candidate;
26                BitVectorIndividual bitTest = (BitVectorIndividual) test;
27
28                if (bitCandidate.genomeLength() != bitTest.genomeLength()) {
29                        state.output.error("Competitors' bit vectors should be equal length\n");
30                }
31
32                for (int i = 0; i < bitCandidate.genomeLength(); i++) {
33                        if (bitCandidate.genome[i] != bitTest.genome[i]) {
34                                if (bitCandidate.genome[i]) {
35                                        return new Pair<RealValuedResult>(new RealValuedResult(1),
36                                                new RealValuedResult(0));
37                                } else {
38                                        return new Pair<RealValuedResult>(new RealValuedResult(0),
39                                                new RealValuedResult(1));
40                                }
41                        }
42                }
43
44                return new Pair<RealValuedResult>(new RealValuedResult(0), new RealValuedResult(0));
45        }
46
47        public float calculateObjectiveFitness(EvolutionState state, Individual ind) {
48                if (!(ind instanceof BitVectorIndividual)) {
49                        state.output.error("Competitor individuals should be represented by bit vectors\n");
50                }
51                BitVectorIndividual bitIndividual = (BitVectorIndividual) ind;
52
53                BigDecimal two = new BigDecimal(2);
54                BigDecimal maxValue = two.pow(bitIndividual.genome.length).subtract(BigDecimal.ONE);
55
56                BigDecimal result = BigDecimal.ZERO;
57                for (int i = 0; i < bitIndividual.genomeLength(); i++) {
58                        result = result.multiply(two);
59                        if (bitIndividual.genome[i]) {
60                                result = result.add(BigDecimal.ONE);
61                        }
62                }
63
64                return result.divide(maxValue, 10, RoundingMode.DOWN).floatValue();
65        }
66
67        @Deprecated
68        public float calculateLongObjectiveFitness(EvolutionState state, Individual ind) {
69                if (!(ind instanceof BitVectorIndividual)) {
70                        state.output.error("Competitor individuals should be represented by bit vectors\n");
71                }
72
73                BitVectorIndividual bitIndividual = (BitVectorIndividual) ind;
74                long result = 0;
75                for (int i = 0; i < bitIndividual.genomeLength(); i++) {
76                        result *= 2;
77                        if (bitIndividual.genome[i]) {
78                                result++;
79                        }
80                }
81                return result;
82        }
83}
Note: See TracBrowser for help on using the repository browser.