Solution of the Knapsack problem with Genetic Algorithm and State, Strategy, Abstract Factory design patterns
https://atagunay.github.io/knapsack/doc/index.html
- Configure Genetic algorithm variables in main file
public class Main {
public static void main(String[] args) {
// Set constant variables
GeneticAlgorithmSettings.REPRODUCTION_RATE = 0.25;
GeneticAlgorithmSettings.MUTATION_RATE = 0.10;
GeneticAlgorithmSettings.CROSSOVER_RATE = 0.50;
}
}
- Configure Knapsack problem variables in main file
public class Main {
public static void main(String[] args) {
// Set constant variables
GeneticAlgorithmSettings.REPRODUCTION_RATE = 0.25;
GeneticAlgorithmSettings.MUTATION_RATE = 0.10;
GeneticAlgorithmSettings.CROSSOVER_RATE = 0.50;
KnapsackSettings.WEIGHT1 = 7;
KnapsackSettings.WEIGHT2 = 2;
KnapsackSettings.WEIGHT3 = 1;
KnapsackSettings.WEIGHT4 = 9;
KnapsackSettings.VALUE1 = 5;
KnapsackSettings.VALUE2 = 4;
KnapsackSettings.VALUE3 = 7;
KnapsackSettings.VALUE4 = 2;
}
}
- Create a Genetic Algorithm Instance and run it
public class Main {
public static void main(String[] args) {
// Set constant variables
GeneticAlgorithmSettings.REPRODUCTION_RATE = 0.25;
GeneticAlgorithmSettings.MUTATION_RATE = 0.10;
GeneticAlgorithmSettings.CROSSOVER_RATE = 0.50;
KnapsackSettings.WEIGHT1 = 7;
KnapsackSettings.WEIGHT2 = 2;
KnapsackSettings.WEIGHT3 = 1;
KnapsackSettings.WEIGHT4 = 9;
KnapsackSettings.VALUE1 = 5;
KnapsackSettings.VALUE2 = 4;
KnapsackSettings.VALUE3 = 7;
KnapsackSettings.VALUE4 = 2;
// Select problem to solve with GA
GeneticAlgorithm ga = new GeneticAlgorithm("knapsack");
// Run GA
// Idle to complete state
ga.nextStep();
// Complete to idle state
ga.nextStep();
}
}
- Expected Output
- Program output indicates index of the elements in the knapsack
- Example:
- [0,1,1,1] = Take second, thirth and fourth items. Leave first item.
VOILA!!! You have completed your Genetic Algorithm Process
You Genetic Algorithm Settings:
Crossover Rate: 0.5
Mutation Rate: 0.1
Reproduction Rate: 0.25
Program Output: [0, 1, 1, 1]
Idle -> Complete
Complete -> Idle
- A Genetic Algorithm process may have some states like idle and complete to inform the client developer
Document Links:
classDiagram
GeneticAlgorithm *-- State
State <|.. Idle
State <|.. Complete
- GeneticAlgorithmManager class can be used by the different classes or threads to manage the Genetic Algorithm process
- Creating new instances of GeneticAlgorithmManager for every new Genetic Algortihm will be redundant. For this reason, Singleton Pattern is used here
Document Links:
classDiagram
Idle *-- GeneticAlgorithmManager
- Next generation process has some behaviours/algorithms like mutation, crossover, selection
- These algorithms can be changed on runtime
- We may want to use different algorithms inside the next generation process. For this reason Strategy Pattern is used here
Document Links:
classDiagram
NextGeneration *-- SelectionBehaviour: has-a
NextGeneration *-- CrossoverBehaviour: has-a
NextGeneration *-- MutationBehaviour: has-a
MutationBehaviour <|.. RandomMutation: implement
CrossoverBehaviour <|.. HalfElementCrossover: implement
SelectionBehaviour <|.. TournamentSelection: implement
- Genetic Algorithm factory may create different families like knapsack family or travelling salesman family
- Each family should have some products like selection, crossover and mutation. For this reason, Abstract Factory Pattern is used here
Document Links:
classDiagram
GeneticAlgorithmFactory <|.. KnapsackFactory: implement
KSInitialPopulation <.. KnapsackFactory: create
KSFitnessCalculation <.. KnapsackFactory: create
KSResultDetection <.. KnapsackFactory: create
KSNextGeneration <.. KnapsackFactory: create
InitialPopulation <|-- KSInitialPopulation: extend
FitnessCalculation <|-- KSFitnessCalculation: extend
NextGeneration <|-- KSNextGeneration: extend
ResultDetection <|-- KSResultDetection: extend