Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Individu {
- private int value;
- private String code;
- public Individu() {
- //this.value = 0;
- }
- public Individu(int value) {
- super();
- this.value = value;
- this.setCode(value);
- }
- public int getValue() {
- return value;
- }
- public void setValue(int value) {
- this.value = value;
- }
- public String getCode() {
- return code;
- }
- private void setCode(int value) {
- String temp;
- temp = Integer.toBinaryString(value);
- if(temp.length()< 4)
- {
- switch (temp.length())
- {
- case 1 : temp="000" + temp;
- break;
- case 2 : temp="00" + temp;
- break;
- case 3 : temp="0" + temp;
- break;
- }
- }
- this.code=temp;
- }
- }
- public class Population {
- public static final int numberOfIndividu=4;
- private Individu[] pop;
- public Population() {
- pop=new Individu[numberOfIndividu];
- }
- public Population(Individu[] pop) {
- super();
- this.pop = pop;
- }
- public Individu[] getPop() {
- return pop;
- }
- public void setPop(Individu[] pop) {
- this.pop = pop;
- }
- public void setIndToPop(Individu ind,int index)
- {
- this.pop[index]=ind;
- }
- public Individu get(int index)
- {
- return this.pop[index];
- }
- }
- import java.util.concurrent.ThreadLocalRandom;
- public class Tratement {
- public static final double taux = 0.000001;
- private Population p1=new Population();
- private Population p2=new Population();
- private Population p3=new Population();
- private int indOpt=10;
- public Tratement() {
- // TODO Auto-generated constructor stub
- }
- public Tratement(Population p1) {
- super();
- this.p1=p1;
- }
- public int getIndOpt() {
- return indOpt;
- }
- public Population getP1() {
- return p1;
- }
- public void setP1(Population p1) {
- this.p1 = p1;
- }
- public Population getP2() {
- return p2;
- }
- public void setP2(Population p2) {
- this.p2 = p2;
- }
- public Population getP3() {
- return p3;
- }
- public void setP3(Population p3) {
- this.p3 = p3;
- }
- public double fitness(double val)
- {
- return (1/(val+Math.pow(val, 2)));
- }
- public double probability(int index)
- {
- double sum=0;
- int i;
- for(i=0;i<p1.numberOfIndividu;i++)
- {
- sum=sum + fitness((p1.getPop()[i]).getValue());
- }
- return (fitness((p1.getPop()[index]).getValue())/sum);
- }
- public void getMax()
- {
- System.out.println("Previous max :: " +this.indOpt);
- int indice=0;
- double max=0;
- for(int i=0;i<p1.numberOfIndividu;i++)
- {
- double fit=fitness((p1.getPop()[i]).getValue());
- if( max < fit)
- {
- max = fit;
- indice=i;
- }
- }
- if(max > fitness(this.indOpt) )
- {
- this.indOpt=p1.get(indice).getValue();
- }
- System.out.println("Actual max :: " +this.indOpt);
- }
- public void selection()
- {
- Individu[] ind=new Individu[Population.numberOfIndividu];
- for(int i=0;i<p1.numberOfIndividu;i++)
- {
- int rand=ThreadLocalRandom.current().nextInt(0, 4);
- //System.out.println(rand);
- ind[i]=p1.get(rand);
- }
- this.p2.setPop(ind);;
- }
- public void breeding()
- {
- int index=ThreadLocalRandom.current().nextInt(0, 3);
- Individu[] ind=new Individu[Population.numberOfIndividu];
- String s[]=new String[4];
- for(int i=0;i< Population.numberOfIndividu;i++)
- {
- s[i]=p2.get(i).getCode();
- }
- //System.out.println(index);
- String tmp = s[0];
- s[0] = s[0].substring(0, index+1) + s[3].substring(index+1, s[3].length());
- s[3] = s[3].substring(0, index+1) + tmp.substring(index+1, tmp.length());
- tmp = s[1];
- s[1] = s[1].substring(0, index+1) + s[2].substring(index+1, s[2].length());
- s[2] = s[2].substring(0, index+1) + tmp.substring(index+1, tmp.length());
- //System.out.println(s[0] + " " + s[1] + " " + s[2] + " " + s[3]);
- for(int i=0;i< Population.numberOfIndividu;i++)
- {
- ind[i]=new Individu(Integer.parseInt(s[i], 2));
- }
- this.p3.setPop(ind);;
- }
- public void mutation()
- {
- Individu[] ind=new Individu[Population.numberOfIndividu];
- String s[]=new String[4];
- String tmp;
- double rand;
- for(int i=0;i<Population.numberOfIndividu ; i++)
- {
- tmp="";
- for(int j=0;j<4;j++)
- {
- rand=Math.random();
- if(rand < taux)
- {
- tmp=tmp + Math.round(rand);
- }
- else
- {
- tmp=tmp + ((p3.get(i)).getCode()).charAt(j);
- }
- }
- s[i]=tmp;
- }
- for(int i=0;i< Population.numberOfIndividu;i++)
- {
- ind[i]=new Individu(Integer.parseInt(s[i], 2));
- }
- this.p1.setPop(ind);;
- }
- }
- public class Test {
- public static void main(String[] args) {
- int counter=0;
- Individu ind[]={new Individu(1),new Individu(2),new Individu(5),new Individu(8)};
- Population pop=new Population(ind);
- Tratement tr=new Tratement(pop);
- //System.out.println(tr.probability(0));
- //System.out.println(tr.fitness(1));
- //System.out.println(tr.getIndOpt());
- while(counter < 100 )
- {
- System.out.println(" Iteartion : " + counter);
- tr.getMax();
- tr.selection();
- System.out.println("After Selection");
- for(int i=0;i<4;i++)
- {
- System.out.println(tr.getP2().getPop()[i].getValue() + " -> " + tr.getP2().getPop()[i].getCode());
- }
- tr.breeding();
- System.out.println("After Breeding");
- for(int i=0;i<4;i++)
- {
- System.out.println(tr.getP3().getPop()[i].getValue() + " -> " + tr.getP3().getPop()[i].getCode());
- }
- tr.mutation();
- System.out.println("After Mutation");
- for(int i=0;i<4;i++)
- {
- System.out.println(tr.getP1().getPop()[i].getValue() + " -> " + tr.getP1().getPop()[i].getCode());
- }
- counter++;
- }
- System.out.println(tr.getIndOpt());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement