/** Applies a roulette wheel selection */ public void selection() { RuleSet temp[]; double probability[] = new double[long_poblacion]; double total; double prob; int sel; temp = new RuleSet[long_poblacion]; // sort the poblation in order of fitness Arrays.sort(poblacion, Collections.reverseOrder()); probability[0] = poblacion[0].getFitness(); for (int i = 1; i < long_poblacion; i++) { probability[i] = probability[i - 1] + poblacion[i].getFitness(); } total = probability[long_poblacion - 1]; for (int i = 0; i < long_poblacion; i++) { probability[i] /= total; } for (int i = 0; i < long_poblacion; i++) { prob = Randomize.Rand(); sel = -1; for (int j = 0; j < long_poblacion && sel == -1; j++) { if (probability[j] > prob) sel = j; } temp[i] = new RuleSet(poblacion[sel]); } previousPob = poblacion; poblacion = temp; }
/** * It runs the Single-attribute Evolution Module (SEM) algorithm to obtain a rule set of ONE * attribute */ public void run() { boolean endCondition = false; int gen = 0; int stagnation = 0; evaluate(); while (!endCondition) { tournament_selection(); crossOver(); mutate(); elitism(); evaluate(); Arrays.sort(poblacion, Collections.reverseOrder()); gen++; if (bestCR != poblacion[0].getFitness()) stagnation = 0; else stagnation++; if (gen > generationLimit || stagnation > stagnationLimit || poblacion[0].getFitness() == 1.0) endCondition = true; bestCR = poblacion[0].getFitness(); } }