01 /*
02 * CISELab, Computational and Intelligent Systems Engineering Laboratory
03 * Department of Engineering
04 * University of Sannio, 82100 Benevento (ITALY)
05 * web-site: www.ciselab.org
06 *
07 * JENES, A Java Library for Genetic Algorithms
08 * Copyright (C) 2009, Luigi Troiano
09 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 02110-1301 USA
24 *
25 * COPYRIGHT DISCLAIMER
26 * CISELab, hereby disclaims all copyright interest in the
27 * library `JENES' (A Java Library for Genetic Algorithms)
28 * written by Luigi Troiano et al.
29 *
30 * Luigi Troiano, 1 January 2009
31 * CISELab Coordinator
32 */
33 package jenes.tutorials.problem4;
34
35 import jenes.GeneticAlgorithm;
36 import jenes.algorithms.SimpleGA;
37 import jenes.chromosome.DoubleChromosome;
38 import jenes.population.Individual;
39 import jenes.population.Population;
40 import jenes.population.Population.Statistics;
41 import jenes.tutorials.utils.Utils;
42
43 public class EntropyProblem {
44
45 private static int POPULATION_SIZE = 100;
46 private static int CHROMOSOME_LENGTH = 5;
47 private static int GENERATION_LIMIT = 100;
48
49 public static void main(String[] args) {
50 Utils.printHeader();
51 System.out.println();
52
53 System.out.println("TUTORIAL 4:");
54 System.out.println("Find the probability distribution that maximizes the Shannon's entropy.");
55 System.out.println();
56
57 Individual<DoubleChromosome> sample = new Individual<DoubleChromosome>(new DoubleChromosome(CHROMOSOME_LENGTH,0,1));
58 Population<DoubleChromosome> pop = new Population<DoubleChromosome>(sample, POPULATION_SIZE);
59
60 SimpleGA<DoubleChromosome> ga = new SimpleGA<DoubleChromosome>(pop, GENERATION_LIMIT) {
61 @Override
62 protected void evaluateIndividual(Individual<DoubleChromosome> individual) {
63 DoubleChromosome chrom = individual.getChromosome();
64
65 int length = chrom.length();
66
67 double sum = 0.0;
68 for(int i=0; i<length; ++i) {
69 sum += chrom.getValue(i);
70 }
71
72 double entropy = 0.0;
73 for(int i=0; i<length; ++i) {
74 double pxi = chrom.getValue(i)/sum;
75 chrom.setValue(i, pxi);
76
77 entropy -= (pxi * Math.log(pxi) / Math.log(2));
78 }
79
80 individual.setScore(entropy);
81 }
82
83 };
84 ga.setBiggerIsBetter(false);
85 ga.evolve();
86
87 Statistics stats = ga.getCurrentPopulation().getStatistics();
88 GeneticAlgorithm.Statistics algostats = ga.getStatistics();
89
90 System.out.println("Solution: ");
91 System.out.println(stats.getLegalHighestIndividual().getChromosome() );
92 System.out.println(stats.getLegalHighestIndividual());
93 System.out.format("found in %d ms.\n", algostats.getExecutionTime() );
94 System.out.println();
95
96 Utils.printStatistics(stats);
97 }
98 }
|
|
|