01 /*
02 * JENES
03 * A time asnd memory efficient Java library for genetic algorithms and more
04 * Copyright (C) 2011 Intelligentia srl
05 *
06 * This program is free software: you can redistribute it and/or modify
07 * it under the terms of the GNU General Public License as published by
08 * the Free Software Foundation, either version 3 of the License, or
09 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 package jenes.tutorials.old.problem4;
20
21 import jenes.GeneticAlgorithm;
22 import jenes.algorithms.SimpleGA;
23 import jenes.chromosome.DoubleChromosome;
24 import jenes.population.Individual;
25 import jenes.population.Population;
26 import jenes.population.Population.Statistics;
27 import jenes.tutorials.utils.Utils;
28
29 /**
30 * Tutorial showing how to set-up a minimization problem.
31 * The problem is to find a vector whose entroy, after normalization, is minimal.
32 *
33 * @author Luigi Troiano
34 * @author Pierpaolo Lombardi
35 *
36 * @version 1.0
37 * @since 1.0
38 */
39 public class EntropyProblem {
40
41 private static int POPULATION_SIZE = 100;
42 private static int CHROMOSOME_LENGTH = 5;
43 private static int GENERATION_LIMIT = 100;
44
45 public static void main(String[] args) {
46 Utils.printHeader();
47 System.out.println();
48
49 System.out.println("TUTORIAL 4:");
50 System.out.println("Find the probability distribution that maximizes the Shannon's entropy.");
51 System.out.println();
52
53 Individual<DoubleChromosome> sample = new Individual<DoubleChromosome>(new DoubleChromosome(CHROMOSOME_LENGTH,0,1));
54 Population<DoubleChromosome> pop = new Population<DoubleChromosome>(sample, POPULATION_SIZE);
55
56 SimpleGA<DoubleChromosome> ga = new SimpleGA<DoubleChromosome>(pop, GENERATION_LIMIT) {
57 @Override
58 public void evaluateIndividual(Individual<DoubleChromosome> individual) {
59 DoubleChromosome chrom = individual.getChromosome();
60
61 int length = chrom.length();
62
63 double sum = 0.0;
64 for(int i=0; i<length; ++i) {
65 sum += chrom.getValue(i);
66 }
67
68 double entropy = 0.0;
69 for(int i=0; i<length; ++i) {
70 double pxi = chrom.getValue(i)/sum;
71 chrom.setValue(i, pxi);
72
73 entropy -= (pxi * Math.log(pxi) / Math.log(2));
74 }
75
76 individual.setScore(entropy);
77 }
78
79 };
80 ga.setBiggerIsBetter(true);
81 ga.evolve();
82
83 Statistics stats = ga.getCurrentPopulation().getStatistics();
84 GeneticAlgorithm.Statistics algostats = ga.getStatistics();
85
86 System.out.println("Solution: ");
87 System.out.println(stats.getLegalHighestIndividual().getChromosome() );
88 System.out.println(stats.getLegalHighestIndividual());
89 System.out.format("found in %d ms.\n", algostats.getExecutionTime() );
90 System.out.println();
91
92 Utils.printStatistics(stats);
93 }
94 }
|
|
|