Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mbluemer committed May 2, 2017
2 parents 8d88071 + 6fc0724 commit 98dd9eb
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 46 deletions.
19 changes: 13 additions & 6 deletions include/GeneticSimulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,33 @@
#define GENETIC_SIMULATION_H

#include <random>
#include <queue>
#include <deque>
#include <fstream>
#include "Specimen.hpp"

class GeneticSimulation {

int numSpecimen;
std::queue<Specimen> population;
std::deque<Specimen> population;

std::random_device rd;
std::mt19937 gen;
std::uniform_int_distribution<int> dist;

std::ofstream fileOut;

public:
GeneticSimulation(int s):
numSpecimen(s),
dist(-268435456,268435456),
// dist(-268435456,268435456),
dist(-100,100),
rd(),
gen(rd()) {};
void simulate();

gen(rd()) {
fileOut.open("sim_out.txt", std::ios::app);
};
~GeneticSimulation(){ fileOut.close(); };
void simulateRound(int roundNum);
void printWinners();
};


Expand Down
2 changes: 2 additions & 0 deletions include/Heuristic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ class Heuristic
public:
void set(int index, int value) { coeff[index] = value; };
int evaluate(bup &bitboard);
auto getArray(){ return coeff;};
std::string coefficientsToString();

// Needed public for variable depth search
int jumpeablePieces() const;

};

#endif
2 changes: 1 addition & 1 deletion include/MinimaxSearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MinimaxSearch
{
private:
static const int MIN_DEPTH = 5;
static const int MAX_DEPTH = 9;
static const int MAX_DEPTH = 6;
Heuristic heuristic;

double minValue(std::unique_ptr<BitBoard> &board, int currentDepth);
Expand Down
10 changes: 5 additions & 5 deletions src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void Game::play(int *winner){
// draw after some limit of no capture
if(noCaptureCount >= noCaptureLimit)
{
*winner = 3;
*winner = 0;
break;
}

Expand All @@ -47,7 +47,7 @@ void Game::play(int *winner){
}

//get next move
if(board->player().compare("black"))
if(board->player().compare("black") == 0)
bestMove = blackSearch.minimaxDecision(board);
else
bestMove = whiteSearch.minimaxDecision(board);
Expand All @@ -57,11 +57,11 @@ void Game::play(int *winner){
board->printState();

// 1 if black wins, 2 if white, 3 if draw
if(noCaptureCount == noCaptureLimit)
if(noCaptureCount >= noCaptureLimit)
*winner = 0;
else if(board->player().compare("black"))
else if(board->player().compare("black") == 0)
*winner = 1;
else if(board->player().compare("white"))
else if(board->player().compare("white") == 0)
*winner = 2;

}
92 changes: 59 additions & 33 deletions src/GeneticSimulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,48 @@
#include "Game.hpp"
#include "Specimen.hpp"

void GeneticSimulation::simulate(){

for(int i = 0; i < numSpecimen; i++){
Heuristic h1, h2;
for(int j = 0; j < Heuristic::numFeatures; j++){
h1.set(j, dist(gen));
h2.set(j, dist(gen));
}

Specimen s(h1, h2);
population.push(s);

void GeneticSimulation::printWinners(){
for(auto i = population.begin(); i < population.end(); i++){
fileOut << "\nS: {";
Heuristic h = i->getStartHeuristics();
for(int j = 0; j < Heuristic::numFeatures-1; j++)
fileOut << h.getArray()[j] << ", ";
fileOut << h.getArray()[Heuristic::numFeatures-1] << "}\nE: {";
h = i->getEndHeuristics();
for(int j = 0; j < Heuristic::numFeatures-1; j++)
fileOut << h.getArray()[j] << ", ";
fileOut << h.getArray()[Heuristic::numFeatures-1] << "}";
}
}
void GeneticSimulation::simulateRound(int roundNum){

// if first round, randomly populate
// if(population.size() = 0){
for(int i = 0; i < numSpecimen - population.size(); i++){
Heuristic h1, h2;
for(int j = 0; j < Heuristic::numFeatures; j++){
h1.set(j, dist(gen));
h2.set(j, dist(gen));
}

Specimen s(h1, h2);
population.push_back(s);

}


std::cout << numSpecimen;

int cntBlack=0, cntWhite=0, cntTies=0;
int cntTieBlack=0, cntTieWhite=0;

while(population.size() > numSpecimen/5){
int gamesSinceNoTie = 0;
while(population.size() > (numSpecimen/10) && gamesSinceNoTie < population.size()){
Specimen a = population.front();
population.pop();
population.pop_front();
Specimen b = population.front();
population.pop();
population.pop_front();


Game game1(a, b);
Expand All @@ -40,40 +60,47 @@ void GeneticSimulation::simulate(){
game2.play(&winner);

if(winner == 1){
population.push(b);
population.push_back(b);
cntTieBlack++;
}
else if(winner == 2){
population.push(a);
population.push_back(a);
cntTieWhite++;
}
}
else {
if(winner == 1){
population.push(a);
population.push_back(a);
cntBlack++;
}
else if(winner == 2){
population.push(b);
population.push_back(b);
cntWhite++;
}
else cntTies++;

std::cout << population.size() << std::endl;
}

if(winner == 0){
cntTies++;
population.push_back(a);
population.push_back(b);
gamesSinceNoTie++;
}else {
gamesSinceNoTie = 0;
}

}


std::ofstream s;
s.open("sim_out.txt", std::ios::app);
if(fileOut.is_open()){
fileOut<< "\nRound: " << roundNum << std::endl;
fileOut<< "Size: \t\t" << numSpecimen << std::endl;
fileOut<< "Black Wins: \t" << cntBlack << std::endl;
fileOut<< "White Wins: \t" << cntWhite << std::endl;
fileOut<< "White Wins(after Tie): " << cntTieBlack << std::endl;
fileOut<< "Black Wins(after Tie): " << cntTieWhite << std::endl;
fileOut<< "Ties: \t\t" << cntTies<< std::endl;
fileOut<< "End popultion size: \t" << population.size() << std::endl;
}

if(s.is_open()){
s << "Size: \t\t" << numSpecimen << std::endl;
s << "Black Wins: \t" << cntBlack << std::endl;
s << "White Wins: \t" << cntWhite << std::endl;
s << "White Wins(after Tie): " << cntTieBlack << std::endl;
s << "Black Wins(after Tie): " << cntTieWhite << std::endl;
s << "Ties: \t\t" << cntTies << std::endl;
int n = population.size();
for (int i = 0; i < n; ++i) {
Specimen temp = population.front();
Expand All @@ -84,7 +111,6 @@ void GeneticSimulation::simulate(){
s << "END: " << std::endl;
s << temp.getEndHeuristics().coefficientsToString();
}
}
s.close();


}
7 changes: 6 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ void serverGame(int userID, int oppID){

void geneticSimulation(int numSpecimen){
GeneticSimulation gs(numSpecimen);
gs.simulate();

int numRounds = 20;
for(int i = 0; i < numRounds; i++){
gs.simulateRound(i);
}
gs.printWinners();
}

int main(int argc, const char * argv[]) {
Expand Down

0 comments on commit 98dd9eb

Please sign in to comment.