Click here to Skip to main content
15,846,977 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello,

I have used a general genetic algorithm code in java, but I didn't understand how to make it work with my problem . my problem is " a way to choose which user to assign to WiFi or LiFi based on some inputs?

I understand the way it is working in but I don't know if I should assume the user as chromosome and the inputs as genes or the inputs as chromosome?

I will be happy if anyone can help me in it:)

What I have tried:

Java
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());
    	}
    }
}
Posted
Updated 8-Apr-18 18:13pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900