jenes.tutorials.problem3.TSPScrambleMutator.java

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
023 02110-1301  USA
024  *
025  * COPYRIGHT DISCLAIMER
026  * CISELab, hereby disclaims all copyright interest in the
027  * library `JENES' (A Java Library for Genetic Algorithms)
028  * written by Luigi Troiano et al.
029  *
030  * Luigi Troiano, 1 January 2009
031  * CISELab Coordinator
032  */
033 package jenes.tutorials.problem3;
034 
035 import jenes.Random;
036 import jenes.chromosome.Chromosome;
037 import jenes.chromosome.IntegerChromosome;
038 import jenes.population.Individual;
039 import jenes.stage.operator.Mutator;
040 
041 /**Algorithm description:
042  * Two random indexes, i1 and i2, are choosed; the order of the elements within the
043  * range [i1,i2] changes randomly.
044  *
045  * e.g.
046  * i1=0; i2=3
047  *         position:   0 1 2 3 4 5
048  *        start_chrom: 5 2 1 4 6 3
049  *        end_chrom:   2 5 4 1 6 3
050  */
051 public class TSPScrambleMutator<T extends Chromosome> extends Mutator<T> {
052     
053     public TSPScrambleMutator(double pMut) {
054         super(pMut);
055     }
056     
057     @Override
058     protected void mutate(Individual<T> t) {
059         int size = t.getChromosomeLength();
060         int index1,index2;
061         do{
062             index1 = Random.getInstance().nextInt(0,size);
063             index2 = Random.getInstance().nextInt(0,size);
064         }while(index2==index1);
065         
066         int min,max;
067         if(index1<index2){
068             min=index1;
069             max=index2;
070         }else{
071             min=index2;
072             max=index1;
073         }
074         
075         randomize(t,min, max);
076     }
077     
078     /**
079      * Randomizes the elements chromosome within the range [min,max]
080      <p>
081      @param c the individual to mutate
082      @param min the lower bound
083      @param max the upper bound
084      */
085     public void randomize(Individual<T> c,int min, int max) {
086         IntegerChromosome chrom = (IntegerChromosome) c.getChromosome();
087         //we create a temporany array
088         int len = max-min+1;
089         int[] base = new int[len];
090         //we fill it with the elements within [min,max]
091         for(int i=0;i<len;i++)
092             base[i]= chrom.getValue(min+i);
093         
094         //a randomizes process starts
095         int i=0;
096         //the loop ends when the temporany array is empty
097         while(len!=0){
098             //we choose a random position pos in the array and copy the element at pos in the chromosome
099             int pos = Random.getInstance().nextInt(0,len);
100             chrom.setValue(min+i,base[pos]);
101             //we removes the choosed element from the temporany array
102             for(int j=pos;j<(len-1);j++){
103                 base[j]=base[j+1];
104             }
105             //we updates len and i
106             len--;i++;
107         }
108     }
109 }
Comments