Open Marfein opened 1 year ago
import java.util.Comparator; import java.util.ArrayList; import java.util.Comparator;
public class GenMultPercetron { java.util.List proba; java.util.List chromosomes; static int POPULATION_SIZE = 1000; GenMultPercetron(){ init(); for(int i=0; i<1000; i++){ mark(); selection(); //selectionRulet(); recombination();
}
mark();
selection();
//selectionRulet();
ChromoPerceptron best = chromosomes.get(0);
//System.out.println(best.weights[0]+" "+best.weights[1]+" "+best.fitness);
best.test();
}
void init(){
chromosomes = new java.util.ArrayList<>();
proba = new java.util.ArrayList<>();
java.util.Random r = new java.util.Random();
for(int i=0; i<100; i++){
ChromoPerceptron chromosome = new ChromoPerceptron();
chromosome.weights[0] = r.nextDouble();
chromosome.weights[1] = r.nextDouble();
chromosomes.add(chromosome);
}
}
void mark(){
for(ChromoPerceptron chromosome:chromosomes){
chromosome.countFitness();
}
}
void selection(){
chromosomes.sort(new Comparator(){
@Override
public int compare(Object obj1, Object obj2){
int res = 0;
double fitness1 = ((ChromoPerceptron)obj1).fitness;
double fitness2 = ((ChromoPerceptron)obj2).fitness;
if (fitness2<fitness1) res=1; else
if(fitness2>fitness1) res = -1; else res=0;
return res;
}
});
chromosomes = chromosomes.subList(0, chromosomes.size()/2);
for(ChromoPerceptron chromosome:chromosomes){
System.out.println(chromosome.weights[0]+" "+chromosome.weights[1]+" "+chromosome.fitness);;
}
System.out.println("__________________________________________________________");
}
void selection1() { chromosomes.sort(new Comparator() { @Override public int compare(Object obj1, Object obj2) { int res = 0; double prob1 = ((Chromosome) obj1).selectionProb; double prob2 = ((Chromosome) obj2).selectionProb; if (prob2 < prob1) res = 1; else if (prob2 > prob1) res = -1; else res = 0; return res; } }); }
void recombination(){
java.util.Random r = new java.util.Random();
java.util.ArrayList<ChromoPerceptron> chromosomes2 = new java.util.ArrayList<>();
for(int i=0; i<50; i++){
int index = r.nextInt(chromosomes.size());
ChromoPerceptron c1 = chromosomes.get(index);
index = r.nextInt(chromosomes.size());
ChromoPerceptron c2 = chromosomes.get(index);
ChromoPerceptron c1new = new ChromoPerceptron();
c1new.weights[0] = c1.weights[0];
c1new.weights[1] = c2.weights[1];
ChromoPerceptron c2new = new ChromoPerceptron();
c2new.weights[0] = c2.weights[0];
c2new.weights[1] = c1.weights[1];
chromosomes2.add(c1new);
chromosomes2.add(c2new);
}
chromosomes = chromosomes2;
}
}
public class ChromoPerceptron {
double[] enters; double outer; double[] weights; double[][] patterns = { {0, 0}, {1, 0}, {0, 1}, {1, 1}
}; double fitness; double answers[] = {0, 1, 1, 1};
ChromoPerceptron(){ enters = new double[2]; weights = new double[enters.length]; // for (int i = 0; i < weights.length; i++) { // weights[i] = 0.2 * Math.random() + 0.1; // } }
void countFitness() { fitness = 0; for (int p = 0; p < patterns.length; p++) { enters = java.util.Arrays.copyOf(patterns[p], patterns[p].length); countOuter(); double error = answers[p] - outer; fitness += Math.abs(error); // for (int i = 0; i < enters.length; i++) { // weights[i] += 0.1 error enters[i]; // } } }
public void countOuter() { outer = 0; for (int i = 0; i < enters.length; i++) { outer += enters[i] * weights[i]; } if (outer > 0.5) outer = 1; else outer = 0; }
public void test() { for (int p = 0; p < patterns.length; p++) { enters = java.util.Arrays.copyOf(patterns[p], patterns[p].length); countOuter(); System.out.println(outer);
} }