cromat1

SudokuSegmentMutationOperator

Nov 23rd, 2015
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.16 KB | None | 0 0
  1. package lab2;
  2.  
  3. import java.util.List;
  4. import java.util.Random;
  5.  
  6. import org.jgap.Gene;
  7. import org.jgap.GeneticOperator;
  8. import org.jgap.IChromosome;
  9. import org.jgap.InvalidConfigurationException;
  10. import org.jgap.Population;
  11.  
  12. public class SudokuSegmentMutationOperator implements GeneticOperator{
  13.  
  14.     private static final long serialVersionUID = 1L;
  15.     private int brSegmenta;
  16.    
  17.    
  18.     public SudokuSegmentMutationOperator(int brSegm){
  19.         this.brSegmenta = brSegm;
  20.     }
  21.    
  22.     public SudokuSegmentMutationOperator(){
  23.     }
  24.    
  25.     @SuppressWarnings({ "unchecked", "rawtypes" })
  26.     @Override
  27.     public void operate(Population a_population, List a_candidateChromosomes) {
  28.        
  29.         Random rand = new Random();
  30.         int x = rand.nextInt(Main.BROJ_SEGMENATA);
  31.         int y = rand.nextInt(Main.BROJ_SEGMENATA);
  32.        
  33.            
  34.             IChromosome tempChrom = a_population.getChromosome(brSegmenta);
  35.            
  36.             Gene[] genes = tempChrom.getGenes();
  37.            
  38.             Gene tmpGene = genes[x];
  39.             genes[x] = genes[y];
  40.             genes[y] = tmpGene;
  41.            
  42.             try {
  43.                 tempChrom.setGenes(genes);
  44.             } catch (InvalidConfigurationException e) {
  45.                 e.printStackTrace();
  46.             }
  47.             a_candidateChromosomes.add(tempChrom);
  48.        
  49.  
  50.     }
  51.  
  52.    
  53. }
Advertisement
Add Comment
Please, Sign In to add comment