Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 6.2 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;
8
9import java.util.ArrayList;
10import java.util.Arrays;
11import ec.EvolutionState;
12import ec.Individual;
13import ec.Subpopulation;
14import ec.multiobjective.MultiObjectiveFitness;
15import ec.simple.SimpleStatistics;
16import ec.util.*;
17import java.io.*;
18
19/*
20 * MultiObjectiveStatistics.java
21 *
22 * Created: Thu Feb 04 2010
23 * By: Faisal Abidi and Sean Luke
24 *
25 */
26
27/*
28 * MultiObjectiveStatistics are a SimpleStatistics subclass which overrides the finalStatistics
29 * method to output the current Pareto Front in various ways:
30 *
31 * <ul>
32 * <li><p>Every individual in the Pareto Front is written to the end of the statistics log.
33 * <li><p>A summary of the objective values of the Pareto Front is written to stdout.
34 * <li><p>The objective values of the Pareto Front are written in tabular form to a special
35 * Pareto Front file specified with the parameters below.  This file can be easily read by
36 * gnuplot or Excel etc. to display the Front (if it's 2D or perhaps 3D).
37 *
38 * <p>
39 * <b>Parameters</b><br>
40 * <table>
41 * <tr>
42 * <td valign=top><i>base</i>.<tt>front</tt><br>
43 * <font size=-1>String (a filename)</font></td>
44 * <td valign=top>(The Pareto Front file, if any)</td>
45 * </tr>
46 * </table>
47 */
48
49public class MultiObjectiveStatistics extends SimpleStatistics
50    {   
51    /** front file parameter */
52    public static final String P_PARETO_FRONT_FILE = "front";
53       
54    /** The pareto front log */
55       
56    public static final int NO_FRONT_LOG = -1;
57       
58    public int frontLog;
59
60    public void setup(final EvolutionState state, final Parameter base)
61        {
62        super.setup(state,base);
63       
64        File frontFile = state.parameters.getFile(base.push(P_PARETO_FRONT_FILE),null);
65
66        if (frontFile!=null)
67            try
68                {
69                frontLog = state.output.addLog(frontFile, !compress, compress);
70                }
71            catch (IOException i)
72                {
73                state.output.fatal("An IOException occurred while trying to create the log " + frontFile + ":\n" + i);
74                }
75        else state.output.warning("No Pareto Front statistics file specified.", base.push(P_PARETO_FRONT_FILE));
76        }
77
78
79
80    /** Logs the best individual of the run. */
81    public void finalStatistics(final EvolutionState state, final int result)
82        {
83        // super.finalStatistics(state,result);
84        // I don't want just a single best fitness
85
86        state.output.println("\n\n\n PARETO FRONTS", statisticslog);
87        for (int s = 0; s < state.population.subpops.length; s++)
88            {
89            MultiObjectiveFitness typicalFitness = (MultiObjectiveFitness)(state.population.subpops[s].individuals[0].fitness);
90            state.output.println("\n\nPareto Front of Subpopulation " + s, statisticslog);
91
92            // build front
93            ArrayList front = typicalFitness.partitionIntoParetoFront(state.population.subpops[s].individuals, null, null);
94
95            // sort by objective[0]
96            Object[] sortedFront = front.toArray();
97            QuickSort.qsort(sortedFront, new SortComparator()
98                {
99                public boolean lt(Object a, Object b)
100                    {
101                    return (((MultiObjectiveFitness) (((Individual) a).fitness)).getObjective(0) <
102                        (((MultiObjectiveFitness) ((Individual) b).fitness)).getObjective(0));
103                    }
104               
105                public boolean gt(Object a, Object b)
106                    {
107                    return (((MultiObjectiveFitness) (((Individual) a).fitness)).getObjective(0) >
108                        ((MultiObjectiveFitness) (((Individual) b).fitness)).getObjective(0));
109                    }
110                });
111                       
112            // print out header
113            state.output.message("Pareto Front Summary: " + sortedFront.length + " Individuals");
114            String message = "Ind";
115            int numObjectives = typicalFitness.getObjectives().length;
116            for(int i = 0; i < numObjectives; i++)
117                message += ("\t" + "Objective " + i);
118            String[] names = typicalFitness.getAuxilliaryFitnessNames();
119            for(int i = 0; i < names.length; i++)
120                message += ("\t" + names[i]);
121            state.output.message(message);
122                       
123            // write front to screen
124            for (int i = 0; i < sortedFront.length; i++)
125                {
126                Individual individual = (Individual) (sortedFront[i]);
127
128                float[] objectives = ((MultiObjectiveFitness) individual.fitness).getObjectives();
129                String line = "" + i;
130                for (int f = 0; f < objectives.length; f++)
131                    line += ("\t" + objectives[f]);
132
133                double[] vals = ((MultiObjectiveFitness) individual.fitness).getAuxilliaryFitnessValues();
134                for(int f = 0; f < vals.length; f++)
135                    line += ("\t" + vals[f]);
136                state.output.message(line);
137                }
138                       
139            // print out front to statistics log
140            for (int i = 0; i < sortedFront.length; i++)
141                ((Individual)(sortedFront[i])).printIndividualForHumans(state, statisticslog);
142               
143            // write short version of front out to disk
144            if (frontLog >= 0)
145                {
146                if (state.population.subpops.length > 1)
147                    state.output.println("Subpopulation " + s, frontLog);
148                for (int i = 0; i < sortedFront.length; i++)
149                    {
150                    Individual ind = (Individual)(sortedFront[i]);
151                    MultiObjectiveFitness mof = (MultiObjectiveFitness) (ind.fitness);
152                    float[] objectives = mof.getObjectives();
153
154                    String line = "";
155                    for (int f = 0; f < objectives.length; f++)
156                        line += (objectives[f] + " ");
157                    state.output.println(line, frontLog);
158                    }
159                }
160            }
161        }
162    }
Note: See TracBrowser for help on using the repository browser.