1 | /* |
---|
2 | Copyright 2010 by Sean Luke and George Mason University |
---|
3 | Licensed under the Academic Free License version 3.0 |
---|
4 | See the file "LICENSE" for more information |
---|
5 | */ |
---|
6 | |
---|
7 | package ec.multiobjective.nsga2; |
---|
8 | |
---|
9 | import java.io.*; |
---|
10 | import ec.util.Code; |
---|
11 | import ec.multiobjective.MultiObjectiveFitness; |
---|
12 | import ec.EvolutionState; |
---|
13 | import ec.Fitness; |
---|
14 | |
---|
15 | /* |
---|
16 | * NSGA2MultiObjectiveFitness.java |
---|
17 | * |
---|
18 | * Created: Thu Feb 04 2010 |
---|
19 | * By: Faisal Abidi and Sean Luke |
---|
20 | */ |
---|
21 | |
---|
22 | /** |
---|
23 | * NSGA2MultiObjectiveFitness is a subclass of MultiObjeciveFitness which |
---|
24 | * adds auxiliary fitness measures (sparsity, rank) largely used by MultiObjectiveStatistics. |
---|
25 | * It also redefines the comparison measures to compare based on rank, and break ties |
---|
26 | * based on sparsity. |
---|
27 | * |
---|
28 | */ |
---|
29 | |
---|
30 | public class NSGA2MultiObjectiveFitness extends MultiObjectiveFitness |
---|
31 | { |
---|
32 | public static final String NSGA2_RANK_PREAMBLE = "Rank: "; |
---|
33 | public static final String NSGA2_SPARSITY_PREAMBLE = "Sparsity: "; |
---|
34 | |
---|
35 | public String[] getAuxilliaryFitnessNames() { return new String[] { "Rank", "Sparsity" }; } |
---|
36 | public double[] getAuxilliaryFitnessValues() { return new double[] { rank, sparsity }; } |
---|
37 | |
---|
38 | /** Pareto front rank measure (lower ranks are better) */ |
---|
39 | public int rank; |
---|
40 | |
---|
41 | /** Sparsity along front rank measure (higher sparsity is better) */ |
---|
42 | public double sparsity; |
---|
43 | |
---|
44 | public String fitnessToString() |
---|
45 | { |
---|
46 | return super.fitnessToString() + "\n" + NSGA2_RANK_PREAMBLE + Code.encode(rank) + "\n" + NSGA2_SPARSITY_PREAMBLE + Code.encode(sparsity); |
---|
47 | } |
---|
48 | |
---|
49 | public String fitnessToStringForHumans() |
---|
50 | { |
---|
51 | return super.fitnessToStringForHumans() + "\n" + "R=" + rank + " S=" + sparsity; |
---|
52 | } |
---|
53 | |
---|
54 | public void readFitness(final EvolutionState state, final LineNumberReader reader) throws IOException |
---|
55 | { |
---|
56 | super.readFitness(state, reader); |
---|
57 | rank = Code.readIntegerWithPreamble(NSGA2_RANK_PREAMBLE, state, reader); |
---|
58 | sparsity = Code.readDoubleWithPreamble(NSGA2_SPARSITY_PREAMBLE, state, reader); |
---|
59 | } |
---|
60 | |
---|
61 | public void writeFitness(final EvolutionState state, final DataOutput dataOutput) throws IOException |
---|
62 | { |
---|
63 | super.writeFitness(state, dataOutput); |
---|
64 | dataOutput.writeInt(rank); |
---|
65 | dataOutput.writeDouble(sparsity); |
---|
66 | } |
---|
67 | |
---|
68 | public void readFitness(final EvolutionState state, final DataInput dataInput) throws IOException |
---|
69 | { |
---|
70 | super.readFitness(state, dataInput); |
---|
71 | rank = dataInput.readInt(); |
---|
72 | sparsity = dataInput.readDouble(); |
---|
73 | } |
---|
74 | |
---|
75 | public boolean equivalentTo(Fitness _fitness) |
---|
76 | { |
---|
77 | NSGA2MultiObjectiveFitness other = (NSGA2MultiObjectiveFitness) _fitness; |
---|
78 | return (rank == ((NSGA2MultiObjectiveFitness) _fitness).rank) && |
---|
79 | (sparsity == other.sparsity); |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * We specify the tournament selection criteria, Rank (lower |
---|
84 | * values are better) and Sparsity (higher values are better) |
---|
85 | */ |
---|
86 | public boolean betterThan(Fitness _fitness) |
---|
87 | { |
---|
88 | NSGA2MultiObjectiveFitness other = (NSGA2MultiObjectiveFitness) _fitness; |
---|
89 | // Rank should always be minimized. |
---|
90 | if (rank < ((NSGA2MultiObjectiveFitness) _fitness).rank) |
---|
91 | return true; |
---|
92 | else if (rank > ((NSGA2MultiObjectiveFitness) _fitness).rank) |
---|
93 | return false; |
---|
94 | |
---|
95 | // otherwise try sparsity |
---|
96 | return (sparsity > other.sparsity); |
---|
97 | } |
---|
98 | } |
---|