Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/multiobjective/nsga2/NSGA2MultiObjectiveFitness.java @ 8614

Last change on this file since 8614 was 6152, checked in by bfarka, 14 years ago

added ecj and custom statistics to communicate with the okb services #1441

File size: 3.3 KB
Line 
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
7package ec.multiobjective.nsga2;
8
9import java.io.*;
10import ec.util.Code;
11import ec.multiobjective.MultiObjectiveFitness;
12import ec.EvolutionState;
13import 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
30public 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    }
Note: See TracBrowser for help on using the repository browser.