public class GeneticSolver extends java.lang.Object implements SolvingAlgorithm
Modifier and Type | Field and Description |
---|---|
private int[] |
blacks |
private ControlInterface |
ci |
private int |
colorQuant |
private boolean |
doubleColors |
private int |
FEASIBLE_CODES_MAX
Max. amount of feasible codes.
|
private java.util.ArrayList<Row> |
feasibleCodes |
private int[] |
fitness |
private int |
GENERATION_SIZE
Number of generations.
|
private int |
parentPos |
private Row[] |
population |
private int |
POPULATION_SIZE
Size of the population within a generation.
|
private int[] |
whites |
private int |
width |
Constructor and Description |
---|
GeneticSolver(ControlInterface ci)
Initialize the AI with settings from the Mastermind engine.
|
Modifier and Type | Method and Description |
---|---|
private boolean |
addToFeasibleCodes()
A code c is eligible or feasible if it results in the same values for
Xk and Yk for all guesses k that have been played up till that stage,
if c was the secret code.
|
private void |
calcFitness()
Calculates the fitness of every Row in population.
|
private int[] |
compare(Row a,
Row b)
Compares two Rows.
|
private int |
divide(int[] fitness,
Row[] pop,
int low,
int up,
int pivot)
Helper function for partitioning.
|
private void |
doubleToRnd(Row[] newPopulation)
Replaces double elements in newPopulation.
|
private void |
evolvePopulation()
Evolve the population using cross over, mutation, permutation and
inversion.
0.5 probability for both xOver1 and xOver2
after the crossover
0.03 chance for mutation
0.03 chance for permutation
0.02 chance for inversion
Reproduction
|
Row |
generateGuess()
Create new generations until enough egible codes are found.
|
private Row |
generateRndGuess()
Generates a Row with random colors.
|
void |
geneticSolverTest(int repetitions)
Speed test.
|
private int |
getParentPos()
Getter for a good parent position in the population.
|
private void |
initPopulation()
Initializes the Population with random Rows.
|
void |
initResults()
Initialize the arrays "blacks" and "whites" with values from the
GameField.
|
private void |
inversion(Row[] newPopulation,
int popPos)
Inversion.
|
private boolean |
lookForSame(Row[] newPopulation,
int popPos)
Look for Rows that are equal to the Row at popPos in newPopulation.
|
int |
makeGuess()
Do a full guess on the Mastermind engine.
|
private void |
mutation(Row[] newPopulation,
int popPos)
Mutation.
|
private void |
permutation(Row[] newPopulation,
int popPos)
Permutation.
|
private void |
sort(int[] fitness,
Row[] pop,
int low,
int up)
Helper function for recursive sorting.
|
private void |
sortFeasibleByFitness(int[] fitness,
Row[] pop)
This is a Quicksort that sorts the fitness and pop Arrays by the criteria
in the fitness-array.
|
private void |
swap(int[] fitness,
int a,
int b)
Helper function to swap two elements of an int array.
|
private void |
swap(Row[] pop,
int a,
int b)
Helper function to swap two elements of an array of Rows.
|
private void |
xOver1(Row[] newPopulation,
int child1Pos,
int child2Pos)
One-point crossover.
|
private void |
xOver2(Row[] newPopulation,
int child1Pos,
int child2Pos)
Two-point crossover.
|
private final int POPULATION_SIZE
private final int GENERATION_SIZE
private final int FEASIBLE_CODES_MAX
addToFeasibleCodes()
,
Constant Field Valuesprivate ControlInterface ci
private int width
private int colorQuant
private boolean doubleColors
private Row[] population
private int[] fitness
private int[] blacks
private int[] whites
private java.util.ArrayList<Row> feasibleCodes
private int parentPos
public GeneticSolver(ControlInterface ci)
ci
- A control interface the AI will use to
interact with a game.public void initResults()
public int makeGuess()
makeGuess
in interface SolvingAlgorithm
ControlInterface.turn()
public Row generateGuess()
generateGuess
in interface SolvingAlgorithm
initPopulation()
,
calcFitness()
,
sortFeasibleByFitness(int[], common.Row[])
,
evolvePopulation()
,
addToFeasibleCodes()
private void evolvePopulation()
private boolean addToFeasibleCodes()
private void doubleToRnd(Row[] newPopulation)
newPopulation
- The population array that will be manipulated.private boolean lookForSame(Row[] newPopulation, int popPos)
newPopulation
- The population array that will be searched.popPos
- The position of the Row within the population array that
will be compared.private void mutation(Row[] newPopulation, int popPos)
newPopulation
- The population array that will be manipulated.popPos
- The position of the Row within the population array that
will be changed.private void permutation(Row[] newPopulation, int popPos)
newPopulation
- The population array that will be manipulated.popPos
- The position of the Row within the population array that
will be changed.private void inversion(Row[] newPopulation, int popPos)
newPopulation
- The population array that will be manipulated.popPos
- The position of the Row within the population array that
will be changed.private void xOver1(Row[] newPopulation, int child1Pos, int child2Pos)
newPopulation
- The population array that will be manipulated.child1Pos
- The position of a Row within the population array that
will be changed.child2Pos
- The position of a Row within the population array that
will be changed.private void xOver2(Row[] newPopulation, int child1Pos, int child2Pos)
newPopulation
- The population array that will be manipulated.child1Pos
- The position of a Row within the population array that
will be changed.child2Pos
- The position of a Row within the population array that
will be changed.private int getParentPos()
private void calcFitness()
private int[] compare(Row a, Row b)
a
- Row Guesscode.b
- Row Secretcode.private void initPopulation()
private Row generateRndGuess()
private void sortFeasibleByFitness(int[] fitness, Row[] pop)
fitness
- An int array.pop
- An array of Rows.sort(int[], common.Row[], int, int)
,
divide(int[], common.Row[], int, int, int)
,
swap(common.Row[], int, int)
,
swap(int[], int, int)
private void sort(int[] fitness, Row[] pop, int low, int up)
fitness
- An int array.pop
- An array of Rows.low
- The lower limit.up
- The upper limit.private int divide(int[] fitness, Row[] pop, int low, int up, int pivot)
fitness
- An int array.pop
- An array of Rows.low
- The lower limit.up
- The upper limit.pivot
- The position of the Pivot element.private void swap(int[] fitness, int a, int b)
fitness
- An int array.a
- Position of the first element.b
- Position of the second element.private void swap(Row[] pop, int a, int b)
pop
- An array of Rows.a
- Position of the first element.b
- Position of the second element.public void geneticSolverTest(int repetitions)
repetitions
- Repetitions of solving one game.