001 /*
002 * CISELab, Computational and Intelligent Systems Engineering Laboratory
003 * Department of Engineering
004 * University of Sannio, 82100 Benevento (ITALY)
005 * web-site: www.ciselab.org
006 *
007 * JENES, A Java Library for Genetic Algorithms
008 * Copyright (C) 2009, Luigi Troiano
009 *
010 * This library is free software; you can redistribute it and/or
011 * modify it under the terms of the GNU Lesser General Public
012 * License as published by the Free Software Foundation; either
013 * version 2.1 of the License, or (at your option) any later version.
014 *
015 * This library is distributed in the hope that it will be useful,
016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018 * Lesser General Public License for more details.
019 *
020 * You should have received a copy of the GNU Lesser General Public
021 * License along with this library; if not, write to the Free Software
022 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
023 *
024 * COPYRIGHT DISCLAIMER
025 * CISELab, hereby disclaims all copyright interest in the
026 * library `JENES' (A Java Library for Genetic Algorithms)
027 * written by Luigi Troiano et al.
028 *
029 * Luigi Troiano, 1 January 2009
030 * CISELab Coordinator
031 */
032 package jenes.tutorials.problem7;
033
034 import java.io.File;
035 import java.io.FileNotFoundException;
036 import java.io.IOException;
037 import jenes.GenerationEventListener;
038 import jenes.GeneticAlgorithm;
039 import jenes.chromosome.BooleanChromosome;
040 import jenes.utils.Random;
041 import jenes.population.Individual;
042 import jenes.population.Population;
043 import jenes.population.Population.Statistics;
044 import jenes.tutorials.utils.Utils;
045 import jenes.utils.CSVLogger;
046 import jenes.statistics.StatisticsLogger;
047 import jenes.tutorials.problem6.KnapsackGA;
048 import jenes.utils.XLSLogger;
049
050 /**
051 * A tutorial showing how to log statistics on different media.
052 *
053 * @author Luigi Troiano
054 *
055 * @version 1.3
056 *
057 * @since 1.3
058 */
059 public class KnapsackLoggedProblem {
060
061 private static int POPSIZE=20;
062 private static int GENERATION_LIMIT=100;
063
064 private static final double[] WEIGHTS = {1, 5, 3, 2, 8, 6, 4, 7, 9, 6};
065 private static final double[] UTILITIES = {7, 2, 7, 1, 6, 4, 2, 8, 9, 2};
066
067 private KnapsackGA algorithm;
068 private double[] utilities;
069 private double[] weights;
070
071 private StatisticsLogger csvlogger;
072 private StatisticsLogger xlslogge1;
073 private StatisticsLogger xlslogge2;
074 private XLSLogger xlslogge3;
075 private int exec;
076
077 private final String FOLDER = "files.Tutorial7" + File.separatorChar;
078
079 public KnapsackLoggedProblem(double[] utilities, double[] weights) throws IOException {
080 algorithm = new KnapsackGA(POPSIZE, GENERATION_LIMIT, utilities, weights);
081 this.weights = weights;
082 this.utilities = utilities;
083
084 csvlogger = new StatisticsLogger(
085 new CSVLogger(new String[]{"LegalHighestScore","LegalScoreAvg","LegalScoreDev"},
086 FOLDER+"knapsackproblem.csv" ) );
087
088 xlslogge1 = new StatisticsLogger(
089 new XLSLogger(new String[]{"LegalHighestScore","LegalScoreAvg","LegalScoreDev"},
090 FOLDER+"knapsack1.log.xls" ) );
091
092 xlslogge2 = new StatisticsLogger(
093 new XLSLogger(new String[]{"LegalHighestScore", "LegalScoreAvg" , "IllegalScoreAvg"},
094 FOLDER+"knapsack2.log.xls", FOLDER+"knapsack.tpl.xls" ) );
095
096 xlslogge3 = new XLSLogger(new String[]{"LegalHighestScore", "LegalScoreAvg" , "Run"},
097 FOLDER+"knapsack3.log.xls");
098
099 }
100
101 @SuppressWarnings("unchecked")
102 public void run() {
103 this.algorithm.evolve();
104
105 Statistics stat=algorithm.getCurrentPopulation().getStatistics();
106 Individual solution=stat.getLegalHighestIndividual();
107 System.out.println(solution.getChromosome());
108 System.out.format("W: %f U: %f\n", algorithm.getWeightOf(solution), algorithm.getUtilityOf(solution) );
109 }
110
111 public double getCapacity() {
112 return this.algorithm.getCapacity();
113 }
114
115 public void setCapacity(double c) {
116 this.algorithm.setCapacity(c);
117 }
118
119 public double[] getUtilities() {
120 return utilities;
121 }
122
123 public double[] getWeights() {
124 return weights;
125 }
126
127 public static KnapsackLoggedProblem build(int n) throws FileNotFoundException, IOException {
128
129 Random r = Random.getInstance();
130
131 double[] utilities = new double[n];
132 for( int i = 0; i < n; ++i ) {
133 utilities[i] = r.nextInt(10);
134 }
135
136 double[] weights = new double[n];
137 for( int i = 0; i < n; ++i ) {
138 weights[i] = r.nextInt(10);
139 }
140
141 return new KnapsackLoggedProblem(utilities, weights);
142 }
143
144 public static void main(String[] args) throws FileNotFoundException, IOException {
145
146 Utils.printHeader();
147 System.out.println();
148
149 System.out.println("TUTORIAL 7:");
150 System.out.println("Logging the Knapsack Problem.");
151 System.out.println();
152
153 final KnapsackLoggedProblem prb = KnapsackLoggedProblem.build(20);
154
155 System.out.println("Utilities: " + toString(prb.getUtilities()) );
156 System.out.println(" Weights: " + toString(prb.getWeights()) );
157 System.out.println();
158
159 GenerationEventListener<BooleanChromosome> logger1 = new GenerationEventListener<BooleanChromosome>() {
160
161 public void onGeneration(GeneticAlgorithm ga, long time) {
162 Population.Statistics stats = ga.getCurrentPopulation().getStatistics();
163
164 prb.csvlogger.record(stats);
165 prb.xlslogge1.record(stats);
166 prb.xlslogge2.record(stats);
167
168 }
169
170 };
171
172 prb.algorithm.addGenerationEventListener(logger1);
173
174
175 System.out.println("50 random elements, capacity 50");
176 prb.setCapacity(50);
177 prb.run();
178 System.out.println();
179
180 System.out.println("Saving the logs ...");
181 prb.csvlogger.close();
182 prb.xlslogge1.close();
183 prb.xlslogge2.close();
184 System.out.println("Done.");
185
186 prb.algorithm.removeGenerationEventListener(logger1);
187
188 GenerationEventListener<BooleanChromosome> logger2 = new GenerationEventListener<BooleanChromosome>() {
189 public void onGeneration(GeneticAlgorithm ga, long time) {
190 Population.Statistics stats = ga.getCurrentPopulation().getStatistics();
191
192 prb.xlslogge3.put("LegalHighestScore", stats.getLegalHighestScore());
193 prb.xlslogge3.put("LegalScoreAvg", stats.getLegalScoreAvg());
194 prb.xlslogge3.put("Run", prb.exec);
195
196 prb.xlslogge3.log();
197 }
198
199 };
200
201 prb.algorithm.addGenerationEventListener(logger2);
202
203 System.out.println();
204 System.out.println("Repeating 10 times: 20 random elements, capacity 50");
205 for( prb.exec = 0; prb.exec < 10; ++prb.exec) {
206 System.out.println((prb.exec+1) + " of 10");
207 prb.run();
208 }
209 prb.xlslogge3.close();
210 System.out.println("Done.");
211 }
212
213 private static String toString(double[] values) {
214 String s = "[";
215 for(int i = 0; i < values.length; ++i ){
216 s += values[i]+ (i < values.length-1 ? " " : "]");
217 }
218 return s;
219 }
220
221 }
|