Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/multiobjective/spea2/SPEA2Evaluator.java @ 10677

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

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

File size: 6.0 KB
Line 
1/*
2  Portions copyright 2010 by Sean Luke, Robert Hubley, 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.spea2;
8
9import ec.*;
10import ec.util.*;
11import ec.multiobjective.*;
12import ec.simple.*;
13
14/*
15 * SPEA2Evaluator.java
16 *
17 * Created: Sat Oct 16 11:24:43 EDT 2010
18 * By: Faisal Abidi and Sean Luke
19 * Replaces earlier class by: Robert Hubley, with revisions by Gabriel Balan and Keith Sullivan
20 */
21 
22/**
23 * This subclass of SimpleEvaluator evaluates the population, then computes auxiliary fitness
24 * data of each subpopulation.
25 */
26
27public class SPEA2Evaluator extends SimpleEvaluator
28    {
29    public void evaluatePopulation(final EvolutionState state)
30        {
31        super.evaluatePopulation(state);
32               
33        // build SPEA2 fitness values
34        for(int x = 0;x<state.population.subpops.length;x++)
35            {
36            Individual[] inds = state.population.subpops[x].individuals;
37            computeAuxiliaryData(state, inds);
38            }
39        }
40
41    /** Computes the strength of individuals, then the raw fitness (wimpiness) and kth-closest sparsity
42        measure.  Finally, computes the final fitness of the individuals.  */
43    public void computeAuxiliaryData(EvolutionState state, Individual[] inds)
44        {
45        double[][] distances = calculateDistances(state, inds);
46                       
47        // For each individual calculate the strength
48        for(int y=0;y<inds.length;y++)
49            {
50            // Calculate the node strengths
51            int myStrength = 0;
52            for(int z=0;z<inds.length;z++)
53                if (((SPEA2MultiObjectiveFitness)inds[y].fitness).paretoDominates((MultiObjectiveFitness)inds[z].fitness))
54                    myStrength++;
55            ((SPEA2MultiObjectiveFitness)inds[y].fitness).strength = myStrength;
56            } //For each individual y calculate the strength
57               
58        // calculate k value
59        int kTH = (int) Math.sqrt(inds.length);  // note that the first element is k=1, not k=0
60       
61        // For each individual calculate the Raw fitness and kth-distance
62        for(int y=0;y<inds.length;y++)
63            {
64            double fitness = 0;
65            for(int z=0;z<inds.length;z++)
66                {
67                // Raw fitness
68                if ( ((SPEA2MultiObjectiveFitness)inds[z].fitness).paretoDominates((MultiObjectiveFitness)inds[y].fitness) )
69                    {
70                    fitness += ((SPEA2MultiObjectiveFitness)inds[z].fitness).strength;
71                    }
72                } // For each individual z calculate RAW fitness distances
73            // Set SPEA2 raw fitness value for each individual
74                                   
75            SPEA2MultiObjectiveFitness indYFitness = ((SPEA2MultiObjectiveFitness)inds[y].fitness);
76                       
77            // Density component
78                       
79            // calc k-th nearest neighbor distance.
80            // distances are squared, so we need to take the square root.
81            double kthDistance = Math.sqrt(orderStatistics(distances[y], kTH, state.random[0]));
82                       
83            // Set SPEA2 k-th NN distance value for each individual
84            indYFitness.kthNNDistance = 1.0 / ( 2 + kthDistance);
85                       
86            // Set SPEA2 fitness value for each individual
87            indYFitness.fitness = fitness + indYFitness.kthNNDistance;
88            }
89        }
90   
91       
92    /** Returns a matrix of sum squared distances from each individual to each other individual. */
93    public double[][] calculateDistances(EvolutionState state, Individual[] inds)
94        {
95        double[][] distances = new double[inds.length][inds.length];
96        for(int y=0;y<inds.length;y++)
97            {
98            distances[y][y] = 0;
99            for(int z=y+1;z<inds.length;z++)
100                {
101                distances[z][y] = distances[y][z] =
102                    ((SPEA2MultiObjectiveFitness)inds[y].fitness).
103                    sumSquaredObjectiveDistance( (SPEA2MultiObjectiveFitness)inds[z].fitness );
104                }
105            }
106        return distances;
107        }
108
109
110    /** Returns the kth smallest element in the array.  Note that here k=1 means the smallest element in the array (not k=0).
111        Uses a randomized sorting technique, hence the need for the random number generator. */
112    double orderStatistics(double[] array, int kth, MersenneTwisterFast rng)
113        {
114        return randomizedSelect(array, 0, array.length-1, kth, rng);
115        }
116               
117               
118    /* OrderStatistics [Cormen, p187]:
119     * find the ith smallest element of the array between indices p and r */
120    double randomizedSelect(double[] array, int p, int r, int i, MersenneTwisterFast rng)
121        {
122        if(p==r) return array[p];
123        int q = randomizedPartition(array, p, r, rng);
124        int k = q-p+1;
125        if(i<=k)
126            return randomizedSelect(array, p, q, i,rng);
127        else
128            return randomizedSelect(array, q+1, r, i-k,rng);
129        }
130               
131               
132    /* [Cormen, p162] */
133    int randomizedPartition(double[] array, int p, int r, MersenneTwisterFast rng)
134        {
135        int i = rng.nextInt(r-p+1)+p;
136               
137        //exchange array[p]<->array[i]
138        double tmp = array[i];
139        array[i]=array[p];
140        array[p]=tmp;
141        return partition(array,p,r);
142        }
143               
144               
145    /* [cormen p 154] */
146    int partition(double[] array, int p, int r)
147        {
148        double x = array[p];
149        int i = p-1;
150        int j = r+1;
151        while(true)
152            {
153            do j--; while(array[j]>x);
154            do i++; while(array[i]<x);
155            if ( i < j )
156                {
157                //exchange array[i]<->array[j]
158                double tmp = array[i];
159                array[i]=array[j];
160                array[j]=tmp;
161                }
162            else
163                return j;
164            }
165        }
166    }
Note: See TracBrowser for help on using the repository browser.