Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. public abstract class Solution {
  2.     abstract double fitness();
  3.     boolean isCorrect() {
  4.         return true;
  5.     }
  6. }
  7.  
  8. public abstract class Generation extends ArrayList<Solution>{
  9.     abstract public Solution getBest();
  10. }
  11.  
  12. public class Context {
  13.    
  14. }
  15.  
  16. public interface Initializator {
  17.     Generation getInitialGeneration();
  18. }
  19.  
  20. public interface Mutator {
  21.     //По данному решению получает новое допустимое
  22.     Solution mutate(Solution s);
  23. }
  24. public interface NextGenerator {
  25.     //Получает новое поколение по текущему. Зависим от алгоритма
  26.     Generation next(Generation current);
  27. }
  28. public interface Selector {
  29.     Generation keepTheBestSolutions(Generation generation);
  30. }
  31. public interface StoppingCriteria {
  32.     boolean isStop(Generation generation);
  33. }
  34. public class Algorithm {
  35.     Initializator i;
  36.     NextGenerator ng;
  37.     StoppingCriteria stop;
  38.     Selector eval;
  39.     public Algorithm(Initializator i, NextGenerator ng, StoppingCriteria stop, Selector eval) {
  40.         this.i = i;
  41.         this.ng = ng;
  42.         this.stop = stop;
  43.         this.eval = eval;
  44.     }
  45.    
  46.     Solution run() {
  47.         Generation gen = i.getInitialGeneration();
  48.         while(!stop.isStop(gen)) {
  49.             Generation ngen = ng.next(gen);
  50.             ngen = eval.keepTheBestSolutions(ngen);        
  51.         }
  52.        
  53.         return gen.getBest();
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement