jenes.tutorial.problem2.PatternGA.java

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.problem2;
34 
35 import jenes.GeneticAlgorithm;
36 import jenes.chromosome.IntegerChromosome;
37 import jenes.population.Individual;
38 import jenes.population.Population;
39 
40 public class PatternGA extends GeneticAlgorithm<IntegerChromosome>{
41     
42     private int[] target;
43     private int precision;
44     
45     public PatternGA(Population<IntegerChromosome> pop, int numGen) {
46         super(pop,numGen);
47         this.setBiggerIsBetter(false);
48     }
49     
50     public void setTarget(int[] target) {
51         this.target = target;
52     }
53     
54     public void setPrecision(int precision) {
55         this.precision = precision;
56     }
57     
58     @Override
59     protected void evaluateIndividual(Individual<IntegerChromosome> individual) {
60         IntegerChromosome chrom = individual.getChromosome();
61         int diff = 0;
62         int length=chrom.length();
63         for(int i=0;i<length;i++){
64             diff += Math.abs(chrom.getValue(i)-this.target[i]);
65         }
66         individual.setScore(diff);
67     }
68     
69     @Override
70     protected boolean end(){
71         jenes.population.Population.Statistics stat = this.getCurrentPopulation().getStatistics();
72         return stat.getLegalLowestScore() <= this.precision;
73     }
74 }
Comments