Prepared genetic functions and renamed class and struct for a more fitting name
This commit is contained in:
parent
b6da05660f
commit
03fe944c1c
113
src/chess.cpp
113
src/chess.cpp
@ -1,4 +1,5 @@
|
|||||||
#include "chess.hpp"
|
#include "chess.hpp"
|
||||||
|
#include <cstdlib>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <random>
|
#include <random>
|
||||||
|
|
||||||
@ -15,30 +16,32 @@ int GenerateRandomNumber(int generationLimit) {
|
|||||||
return generatedNumber;
|
return generatedNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
GeneticDefaults::GeneticDefaults(void) {
|
GeneticRules::GeneticRules(void) {
|
||||||
size = 4;
|
boardSize = 4;
|
||||||
|
populationSize = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
GeneticDefaults::GeneticDefaults(unsigned int size) {
|
GeneticRules::GeneticRules(unsigned int boardSize, unsigned int populationSize) {
|
||||||
this->size = size;
|
this->boardSize = boardSize;
|
||||||
|
this->populationSize = populationSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
GeneticChess::GeneticChess(void) {
|
Individual::Individual(const unsigned int boardSize) {
|
||||||
generationCount = 0;
|
generationCount = 0;
|
||||||
InitializeGenerator();
|
InitializeGenerator();
|
||||||
// population init part of vector resize
|
// population init part of vector resize
|
||||||
board.resize(genetics.size, 0);
|
board.resize(boardSize, 0);
|
||||||
for (int i = 0; i < board.size(); ++i) {
|
for (int i = 0; i < board.size(); ++i) {
|
||||||
board.at(i) = GenerateRandomNumber(genetics.size);
|
board.at(i) = GenerateRandomNumber(boardSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void GeneticChess::Print(void) {
|
void Individual::Print(void) {
|
||||||
std::cout << generationCount << ':' << std::endl;
|
std::cout << generationCount << ':' << std::endl;
|
||||||
for (int i : board) {
|
for (int i : board) {
|
||||||
std::cout << '[';
|
std::cout << '[';
|
||||||
for (int j = 0; j < genetics.size; ++j) {
|
for (int j = 0; j < board.size(); ++j) {
|
||||||
if (j == i) { std::cout << "Q"; }
|
if (j == i) { std::cout << "Q"; }
|
||||||
else { std::cout << ' '; }
|
else { std::cout << ' '; }
|
||||||
}
|
}
|
||||||
@ -48,7 +51,7 @@ void GeneticChess::Print(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Gets the fitness of the population
|
// Gets the fitness of the population
|
||||||
unsigned int GeneticChess::GetFitness(void) {
|
unsigned int Individual::GetFitness(void) {
|
||||||
unsigned int fitness = 0;
|
unsigned int fitness = 0;
|
||||||
for (unsigned int i : board) {
|
for (unsigned int i : board) {
|
||||||
if (!IsQueenThreatened(i)) { fitness++; }
|
if (!IsQueenThreatened(i)) { fitness++; }
|
||||||
@ -58,29 +61,101 @@ unsigned int GeneticChess::GetFitness(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
void GeneticChess::Selection(void) {
|
void Individual::Selection(void) {
|
||||||
|
/* TODO: Finish selection
|
||||||
|
int fitness[N] = {0};
|
||||||
|
int fitness_sum = 0;
|
||||||
|
int i, j;
|
||||||
|
// get fitness for all members of population
|
||||||
|
for ( i=0; i<N; i++ ){
|
||||||
|
fitness[i] = get_fitness(population[i]);
|
||||||
|
fitness_sum += fitness[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// this is simple fitness proportional selection
|
||||||
|
// (roulette wheel sampling)
|
||||||
|
int roll;
|
||||||
|
int temp_sum = 0;
|
||||||
|
int selection;
|
||||||
|
for ( i=0; i<N; i++ ){
|
||||||
|
temp_sum = 0;
|
||||||
|
roll = rand()%fitness_sum;
|
||||||
|
for ( j=0; j<N; j++ ){
|
||||||
|
temp_sum += fitness[j];
|
||||||
|
if ( roll < temp_sum ){
|
||||||
|
selection = j;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
selected[i] = selection;
|
||||||
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
// Crossover on the population
|
// Crossover on the population
|
||||||
void GeneticChess::Crossover(void) {
|
void Individual::Crossover(void) {
|
||||||
|
/* TODO: Finish Crossover
|
||||||
|
double crossover_roll;
|
||||||
|
int crossover_locus;
|
||||||
|
int i;
|
||||||
|
unsigned char temp1;
|
||||||
|
unsigned char temp2;
|
||||||
|
unsigned char mask;
|
||||||
|
unsigned char temp_population[N];
|
||||||
|
|
||||||
|
for ( i=0; i<N; i+=2){
|
||||||
|
crossover_roll = ((double)rand())/((double)RAND_MAX);
|
||||||
|
temp1 = 0;
|
||||||
|
temp2 = 0;
|
||||||
|
if(crossover_roll <= P_c){ //crossover
|
||||||
|
crossover_locus = rand()%L;
|
||||||
|
mask = get_mask(crossover_locus);
|
||||||
|
temp1 = population[selected[i]] & mask;
|
||||||
|
temp1 ^= population[selected[i+1]] & ~mask;
|
||||||
|
temp2 = population[selected[i+1]] & mask;
|
||||||
|
temp2 ^= population[selected[i]] & ~mask;
|
||||||
|
temp_population[i] = temp1;
|
||||||
|
temp_population[i+1] = temp2;
|
||||||
|
}
|
||||||
|
else{ //clone
|
||||||
|
temp_population[i] = population[selected[i]];
|
||||||
|
temp_population[i+1] = population[selected[i+1]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//copy back to population
|
||||||
|
for ( i=0; i<N; i++ ){
|
||||||
|
population[i] = temp_population[i];
|
||||||
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mutates the population
|
// Mutates the population
|
||||||
void GeneticChess::Mutation(void) {
|
void Individual::Mutation(void) {
|
||||||
|
/* TODO: Finish Mutation
|
||||||
|
double mutation_roll;
|
||||||
|
int i, j;
|
||||||
|
for ( i=0; i<N; i++){
|
||||||
|
for ( j=0; j<L; j++ ){
|
||||||
|
mutation_roll = ((double)rand())/((double)RAND_MAX);
|
||||||
|
if ( mutation_roll <= P_m ){
|
||||||
|
population[i] ^= (1<<j); //toggle bit
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks if the passed in queen is threatened on the board
|
// Checks if the passed in queen is threatened on the board
|
||||||
// Impossible for queens to be threatened by same row
|
// Impossible for queens to be threatened by same row
|
||||||
bool GeneticChess::IsQueenThreatened(const unsigned int queenRow) {
|
bool Individual::IsQueenThreatened(const int queenRow) {
|
||||||
int queenCol = board.at(queenRow);
|
int queenCol = board.at(queenRow);
|
||||||
int diffRow, diffCol;
|
int diffRow, diffCol;
|
||||||
for (int i = 0; i < genetics.size; ++i) {
|
for (int row = 0; row < board.size(); ++row) {
|
||||||
if (i == queenRow) { continue; }
|
if (row == queenRow) { continue; }
|
||||||
diffCol = queenCol - board.at(i);
|
diffCol = abs(queenCol - (int)board.at(row));
|
||||||
diffRow = queenRow - i;
|
diffRow = abs(queenRow - row);
|
||||||
if (diffCol == 0) { return true; } // Column threat
|
if (diffCol == 0) { return true; } // Column threat
|
||||||
if (diffCol == diffRow) { return true; }
|
if (diffCol == diffRow) { return true; }
|
||||||
}
|
}
|
||||||
|
@ -6,28 +6,28 @@
|
|||||||
void InitializeGenerator(void);
|
void InitializeGenerator(void);
|
||||||
int GenerateRandomNumber(int generationLimit);
|
int GenerateRandomNumber(int generationLimit);
|
||||||
|
|
||||||
struct GeneticDefaults {
|
struct GeneticRules {
|
||||||
const float kProbabilityCrossover = 0.7; //crossover probability (typical val.)
|
const float kProbabilityCrossover = 0.7; //crossover probability (typical val.)
|
||||||
const float kProbabilityMutation = 0.001; //mutation probability (typical val.)
|
const float kProbabilityMutation = 0.001; //mutation probability (typical val.)
|
||||||
const unsigned int generationLimit = 10000; //number of generations (something huge)
|
const unsigned int generationLimit = 10000; //number of generations (something huge)
|
||||||
unsigned int size; //population size (change to something even)
|
unsigned int boardSize; //board size
|
||||||
GeneticDefaults(void);
|
unsigned int populationSize; //population size (change to something even)
|
||||||
GeneticDefaults(unsigned int size); //custom generation sizes
|
GeneticRules(void);
|
||||||
|
GeneticRules(unsigned int boardSize, unsigned int populationSize); //custom generation sizes
|
||||||
};
|
};
|
||||||
|
|
||||||
class GeneticChess {
|
class Individual {
|
||||||
public:
|
public:
|
||||||
std::vector<unsigned int> board;
|
std::vector<unsigned int> board;
|
||||||
GeneticDefaults genetics;
|
|
||||||
int generationCount;
|
int generationCount;
|
||||||
GeneticChess(void);
|
Individual(const unsigned int boardSize);
|
||||||
void Print(void);
|
void Print(void);
|
||||||
unsigned int GetFitness(void);
|
unsigned int GetFitness(void);
|
||||||
void Selection(void);
|
void Selection(void);
|
||||||
void Crossover(void);
|
void Crossover(void);
|
||||||
void Mutation(void);
|
void Mutation(void);
|
||||||
private:
|
private:
|
||||||
bool IsQueenThreatened(const unsigned int queenRow);
|
bool IsQueenThreatened(const int queenRow);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CHESS_HPP
|
#endif // CHESS_HPP
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
#include "chess.hpp"
|
#include "chess.hpp"
|
||||||
#include "genetic_algorithm.hpp"
|
#include "genetic_algorithm.hpp"
|
||||||
#include <stdio.h>
|
#include <vector>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
int main(){
|
int main() {
|
||||||
GeneticChess population;
|
GeneticRules genetics;
|
||||||
population.Print();
|
std::vector<Individual> population;
|
||||||
population.GetFitness();
|
Individual person(genetics.boardSize);
|
||||||
|
person.Print();
|
||||||
|
person.GetFitness();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
unsigned char population[N] = {0};
|
unsigned char population[N] = {0};
|
||||||
|
Loading…
Reference in New Issue
Block a user