01 /*
02 * JENES
03 * A time and 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.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.population.Population.Statistics.Group;
28 import jenes.tutorials.utils.Utils;
29
30 /**
31 * Tutorial showing how to set-up a minimization problem.
32 * The problem is to find a vector whose entroy, after normalization, is minimal.
33 *
34 * @author Luigi Troiano
35 *
36 * @version 2.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 private static GeneticAlgorithm<DoubleChromosome> ga;
46
47 public static void main(String[] args) {
48 Utils.printHeader();
49 System.out.println();
50
51 System.out.println("TUTORIAL 4:");
52 System.out.println("Find the probability distribution that maximizes (or minimize) the Shannon's entropy.");
53 System.out.println();
54
55 Individual<DoubleChromosome> sample = new Individual<DoubleChromosome>(new DoubleChromosome(CHROMOSOME_LENGTH,0,1));
56 Population<DoubleChromosome> pop = new Population<DoubleChromosome>(sample, POPULATION_SIZE);
57
58 ga = new SimpleGA<DoubleChromosome>(null, pop, GENERATION_LIMIT);
59
60 System.out.println("Solving Max!:");
61 solve( EntropyFitness.MAX );
62
63 System.out.println("Solving Min!:");
64 solve( EntropyFitness.MIN );
65 }
66
67 private static void solve(EntropyFitness fitness) {
68 ga.setFitness(fitness);
69 ga.evolve();
70
71 Statistics stats = ga.getCurrentPopulation().getStatistics();
72 GeneticAlgorithm.Statistics algostats = ga.getStatistics();
73
74 Group legals = stats.getGroup(Population.LEGALS);
75
76 System.out.println(legals.get(0));
77 System.out.format("found in %d ms.\n", algostats.getExecutionTime() );
78 System.out.println();
79
80 Utils.printStatistics(stats);
81 }
82 }
|
|
|