Как использовать генетический алгоритм в java?
привет,
Я использовал общий код генетического алгоритма в java, но я не понимал, как заставить его работать с моей проблемой . моя проблема заключается в том, " как выбрать, какого пользователя назначить Wi-Fi или LiFi на основе некоторых входов?
Я понимаю, как это работает, но я не знаю, должен ли я считать пользователя хромосомой, а входы-генами или входы-хромосомой?
Я буду счастлив, если кто-нибудь сможет мне в этом помочь:)
Что я уже пробовал:
Chromosome class: package com.za.tutorial.ga; import java.util.Arrays; public class Chromosome { private boolean isFitnessChanged = true; private int fitness = 0; private int[] genes; public Chromosome(int length) { genes = new int[length]; } public Chromosome initalizeChromosome() { for (int x = 0; x < genes.length; x++) { if (Math.random() >= 0.5) genes[x] = 1; else genes[x] = 0; } return this; } public int[] getGenes() { isFitnessChanged = true; return genes; } public int getFitness() { if (isFitnessChanged) { fitness = recalculateFitness(); isFitnessChanged = false; } return fitness; } public int recalculateFitness() { int chromosomeFitness =0; for (int x = 0; x < genes.length; x++) { if (genes [x] == Geneticalgorthim.TARGET_CHROMOSOME[x]) chromosomeFitness++; } return chromosomeFitness; } public String toString() { return Arrays.toString(this.genes); } } Population class: package com.za.tutorial.ga; import java.util.Arrays; public class Population { private Chromosome[] chromosomes; public Population(int length) { chromosomes = new Chromosome[length]; } public Population intializePopulation() { for (int x = 0; x < chromosomes.length; x++) { chromosomes[x] = new Chromosome(Geneticalgorthim.TARGET_CHROMOSOME.length).initalizeChromosome(); } sortChromosomesByFitness(); return this; } public Chromosome[] getChromosomes() { return chromosomes; } public void sortChromosomesByFitness() { Arrays.sort(chromosomes, (chromosome1, chromosome2) -> { int flag = 0; if (chromosome1.getFitness() > chromosome2.getFitness()) flag = -1; else if (chromosome1.getFitness() < chromosome2.getFitness()) flag = 1; return flag; }); } } * Geneticalgorthim class: package com.za.tutorial.ga; public class Geneticalgorthim { public static final int POPULATION_SIZE = 8; public static final int [] TARGET_CHROMOSOME = {1,1,0,1,0,0,1,1,1,0}; public static final double MUTATION_RATE = 0.25; public static final int NUMB_OF_ELITE_CHROMOSOMES = 1; public static final int TOURNAMENT_SELECTION_SIZE = 4; public Population evolve(Population population) { return mutatePopulation(crossoverPopulation(population)); } private Population crossoverPopulation(Population population) { Population crossoverPopulation = new Population(population.getChromosomes().length); for (int x = 0; x < NUMB_OF_ELITE_CHROMOSOMES; x++) { crossoverPopulation.getChromosomes()[x] = population.getChromosomes()[x]; } for (int x = NUMB_OF_ELITE_CHROMOSOMES; x < population.getChromosomes().length; x++) { Chromosome chromosome1 = selectTournamePopulation(population).getChromosomes()[0]; Chromosome chromosome2 = selectTournamePopulation(population).getChromosomes()[0]; crossoverPopulation.getChromosomes()[x] = crossoverChromosome(chromosome1, chromosome2); } return crossoverPopulation; } private Population mutatePopulation(Population population) { Population mutatePopulation = new Population(population.getChromosomes().length); for (int x = 0; x < NUMB_OF_ELITE_CHROMOSOMES; x++) { mutatePopulation.getChromosomes()[x] =population.getChromosomes()[x]; } for (int x = NUMB_OF_ELITE_CHROMOSOMES; x < population.getChromosomes().length; x++) { mutatePopulation.getChromosomes()[x] = mutateChromosome(population.getChromosomes()[x]); } return mutatePopulation; } private Chromosome crossoverChromosome(Chromosome chromosome1, Chromosome chromosome2) { Chromosome crossoverChromosome = new Chromosome(TARGET_CHROMOSOME.length); for (int x = 0; x < chromosome1.getGenes().length; x++) { if (Math.random() < 0.5) crossoverChromosome.getGenes()[x] = chromosome1.getGenes()[x]; else crossoverChromosome.getGenes()[x] = chromosome2.getGenes()[x]; } return crossoverChromosome; } private Chromosome mutateChromosome(Chromosome chromosome) { Chromosome mutateChromosome = new Chromosome(TARGET_CHROMOSOME.length); for (int x = 0; x < chromosome.getGenes().length; x++) { if (Math.random() < MUTATION_RATE) { if (Math.random() < 0.5) mutateChromosome.getGenes()[x] = 1; else mutateChromosome.getGenes()[x] = 0; }else mutateChromosome.getGenes()[x] = chromosome.getGenes()[x]; } return mutateChromosome; } private Population selectTournamePopulation(Population population) { Population tournamePopulation = new Population(TOURNAMENT_SELECTION_SIZE); for (int x = 0; x < TOURNAMENT_SELECTION_SIZE; x++) { tournamePopulation.getChromosomes()[x] = population.getChromosomes()[(int)(Math.random()*population.getChromosomes().length)]; } tournamePopulation.sortChromosomesByFitness(); return tournamePopulation; } } Driver class : package com.za.tutorial.ga; import java.util.Arrays; public class Driver { public static void main(String[] args) { Population population = new Population(Geneticalgorthim.POPULATION_SIZE).intializePopulation(); Geneticalgorthim geneticalgorthim = new Geneticalgorthim(); System.out.println("-------------------------------------------------"); System.out.println("Generation # 0" + " |Fittest chromosome fitness: " + population.getChromosomes()[0].getFitness()); printPopulation(population, "Target Chromosome: "+Arrays.toString(Geneticalgorthim.TARGET_CHROMOSOME)); int generationNumber = 0; while (population.getChromosomes()[0].getFitness() < Geneticalgorthim.TARGET_CHROMOSOME.length) { generationNumber++; System.out.println("\n-------------------------------------------------"); population = geneticalgorthim.evolve(population); population.sortChromosomesByFitness(); System.out.println("Generation # "+generationNumber+" |Fittest chromosome fitness: "+ population.getChromosomes()[0].getFitness()); printPopulation(population, "Target Chromosome: "+Arrays.toString(Geneticalgorthim.TARGET_CHROMOSOME)); } } public static void printPopulation(Population population, String heading) { System.out.println(heading); System.out.println("-----------------------------------------"); for (int x = 0; x < population.getChromosomes().length; x++) { System.out.println("Chromosome #" + x + " : " + Arrays.toString(population.getChromosomes()[x].getGenes())+ " | Fitness"+ population.getChromosomes()[x].getFitness()); } } }