Advertisement
Benlahbib_Abdessamad

Untitled

May 20th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.70 KB | None | 0 0
  1.  
  2. public class Individu {
  3.    
  4.     private int value;
  5.     private String code;
  6.    
  7.    
  8.     public Individu() {
  9.         //this.value = 0;
  10.     }
  11.  
  12.     public Individu(int value) {
  13.         super();
  14.         this.value = value;
  15.         this.setCode(value);
  16.     }
  17.  
  18.     public int getValue() {
  19.         return value;
  20.     }
  21.  
  22.     public void setValue(int value) {
  23.         this.value = value;
  24.     }
  25.  
  26.     public String getCode() {
  27.         return code;
  28.     }
  29.  
  30.     private void setCode(int value) {
  31.         String temp;
  32.         temp = Integer.toBinaryString(value);
  33.         if(temp.length()< 4)
  34.         {
  35.             switch (temp.length())
  36.             {
  37.             case 1 : temp="000" + temp;
  38.             break;
  39.             case 2 : temp="00" + temp;
  40.             break;
  41.             case 3 : temp="0" + temp;
  42.             break;
  43.             }
  44.         }
  45.         this.code=temp;
  46.     }
  47.    
  48.    
  49.  
  50. }
  51.  
  52.  
  53.  
  54. public class Population {
  55.    
  56.     public static final int numberOfIndividu=4;
  57.     private Individu[] pop;
  58.    
  59.     public Population() {
  60.         pop=new Individu[numberOfIndividu];
  61.     }
  62.    
  63.    
  64.  
  65.     public Population(Individu[] pop) {
  66.         super();
  67.         this.pop = pop;
  68.     }
  69.  
  70.     public Individu[] getPop() {
  71.         return pop;
  72.     }
  73.  
  74.     public void setPop(Individu[] pop) {
  75.         this.pop = pop;
  76.     }
  77.    
  78.     public void setIndToPop(Individu ind,int index)
  79.     {
  80.         this.pop[index]=ind;
  81.     }
  82.    
  83.     public Individu get(int index)
  84.     {
  85.         return this.pop[index];
  86.     }
  87.    
  88.  
  89. }
  90.  
  91.  
  92. import java.util.concurrent.ThreadLocalRandom;
  93.  
  94.  
  95. public class Tratement {
  96.    
  97.     public static final double taux = 0.000001;
  98.     private Population p1=new Population();
  99.     private Population p2=new Population();
  100.     private Population p3=new Population();
  101.     private int indOpt=10;
  102.    
  103.     public Tratement() {
  104.         // TODO Auto-generated constructor stub
  105.        
  106.     }
  107.    
  108.     public Tratement(Population p1) {
  109.         super();
  110.         this.p1=p1;
  111.     }
  112.    
  113.     public int getIndOpt() {
  114.         return indOpt;
  115.     }
  116.  
  117.     public Population getP1() {
  118.         return p1;
  119.     }
  120.    
  121.     public void setP1(Population p1) {
  122.         this.p1 = p1;
  123.     }
  124.  
  125.     public Population getP2() {
  126.         return p2;
  127.     }
  128.  
  129.     public void setP2(Population p2) {
  130.         this.p2 = p2;
  131.     }
  132.    
  133.  
  134.     public Population getP3() {
  135.         return p3;
  136.     }
  137.  
  138.     public void setP3(Population p3) {
  139.         this.p3 = p3;
  140.     }
  141.  
  142.     public double fitness(double val)
  143.     {
  144.         return (1/(val+Math.pow(val, 2)));
  145.     }
  146.    
  147.     public double probability(int index)
  148.     {
  149.         double sum=0;
  150.         int i;
  151.         for(i=0;i<p1.numberOfIndividu;i++)
  152.         {
  153.             sum=sum + fitness((p1.getPop()[i]).getValue());
  154.         }
  155.         return (fitness((p1.getPop()[index]).getValue())/sum);
  156.     }
  157.    
  158.     public void getMax()
  159.     {
  160.         System.out.println("Previous max :: " +this.indOpt);
  161.         int indice=0;
  162.         double max=0;
  163.         for(int i=0;i<p1.numberOfIndividu;i++)
  164.         {
  165.             double fit=fitness((p1.getPop()[i]).getValue());
  166.             if( max < fit)
  167.             {
  168.                 max = fit;
  169.                 indice=i;
  170.             }
  171.         }
  172.        
  173.         if(max > fitness(this.indOpt) )
  174.         {
  175.             this.indOpt=p1.get(indice).getValue();
  176.         }
  177.         System.out.println("Actual max :: " +this.indOpt);
  178.     }
  179.    
  180.     public void selection()
  181.     {
  182.         Individu[] ind=new Individu[Population.numberOfIndividu];
  183.         for(int i=0;i<p1.numberOfIndividu;i++)
  184.         {
  185.            
  186.             int rand=ThreadLocalRandom.current().nextInt(0, 4);
  187.             //System.out.println(rand);
  188.             ind[i]=p1.get(rand);
  189.         }
  190.         this.p2.setPop(ind);;
  191.        
  192.     }
  193.    
  194.     public void breeding()
  195.     {
  196.         int index=ThreadLocalRandom.current().nextInt(0, 3);
  197.         Individu[] ind=new Individu[Population.numberOfIndividu];
  198.         String s[]=new String[4];
  199.         for(int i=0;i< Population.numberOfIndividu;i++)
  200.         {
  201.             s[i]=p2.get(i).getCode();
  202.         }
  203.         //System.out.println(index);
  204.         String tmp = s[0];
  205.         s[0] = s[0].substring(0, index+1) + s[3].substring(index+1, s[3].length());
  206.         s[3] = s[3].substring(0, index+1) + tmp.substring(index+1, tmp.length());
  207.         tmp = s[1];
  208.         s[1] = s[1].substring(0, index+1) + s[2].substring(index+1, s[2].length());
  209.         s[2] = s[2].substring(0, index+1) + tmp.substring(index+1, tmp.length());
  210.        
  211.         //System.out.println(s[0] + " " + s[1] + " " + s[2] + " " + s[3]);
  212.        
  213.         for(int i=0;i< Population.numberOfIndividu;i++)
  214.         {
  215.             ind[i]=new Individu(Integer.parseInt(s[i], 2));
  216.         }
  217.         this.p3.setPop(ind);;
  218.     }
  219.    
  220.     public void mutation()
  221.     {
  222.         Individu[] ind=new Individu[Population.numberOfIndividu];
  223.         String s[]=new String[4];
  224.         String tmp;
  225.         double rand;
  226.         for(int i=0;i<Population.numberOfIndividu ; i++)
  227.         {
  228.             tmp="";
  229.             for(int j=0;j<4;j++)
  230.             {
  231.                 rand=Math.random();
  232.                 if(rand < taux)
  233.                 {
  234.                     tmp=tmp + Math.round(rand);
  235.                 }
  236.                 else
  237.                 {
  238.                     tmp=tmp + ((p3.get(i)).getCode()).charAt(j);
  239.                 }
  240.             }
  241.             s[i]=tmp;
  242.         }
  243.        
  244.         for(int i=0;i< Population.numberOfIndividu;i++)
  245.         {
  246.             ind[i]=new Individu(Integer.parseInt(s[i], 2));
  247.         }
  248.         this.p1.setPop(ind);;
  249.        
  250.     }
  251.    
  252.    
  253.    
  254.  
  255. }
  256.  
  257.  
  258.  
  259.  
  260. public class Test {
  261.  
  262.     public static void main(String[] args) {
  263.        
  264.         int counter=0;
  265.         Individu ind[]={new Individu(1),new Individu(2),new Individu(5),new Individu(8)};
  266.         Population pop=new Population(ind);
  267.         Tratement tr=new Tratement(pop);
  268.        
  269.        
  270.         //System.out.println(tr.probability(0));
  271.         //System.out.println(tr.fitness(1));
  272.         //System.out.println(tr.getIndOpt());
  273.        
  274.         while(counter < 100 )
  275.         {
  276.             System.out.println(" Iteartion : " + counter);
  277.            
  278.             tr.getMax();
  279.        
  280.             tr.selection();
  281.            
  282.             System.out.println("After Selection");
  283.            
  284.             for(int i=0;i<4;i++)
  285.             {
  286.                 System.out.println(tr.getP2().getPop()[i].getValue() + " -> " + tr.getP2().getPop()[i].getCode());
  287.             }
  288.            
  289.             tr.breeding();
  290.            
  291.             System.out.println("After Breeding");
  292.            
  293.             for(int i=0;i<4;i++)
  294.             {
  295.                 System.out.println(tr.getP3().getPop()[i].getValue() + " -> " + tr.getP3().getPop()[i].getCode());
  296.             }
  297.            
  298.             tr.mutation();
  299.            
  300.            
  301.             System.out.println("After Mutation");
  302.            
  303.             for(int i=0;i<4;i++)
  304.             {
  305.                 System.out.println(tr.getP1().getPop()[i].getValue() + " -> " + tr.getP1().getPop()[i].getCode());
  306.             }
  307.            
  308.             counter++;
  309.         }
  310.        
  311.         System.out.println(tr.getIndOpt());
  312.     }
  313.  
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement