Skip to content

Commit

Permalink
initial genetic alg added
Browse files Browse the repository at this point in the history
  • Loading branch information
dmj12004 committed May 1, 2017
1 parent a5bddbc commit 61bdcb0
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 14 deletions.
6 changes: 5 additions & 1 deletion include/BitBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class BitBoard
void addNewMove(uint32_t start, uint32_t end, vmup &moves) const;
std::string pieceToString(int piece) const;

const int count(uint32_t i) const;

public:
BitBoard();
BitBoard(uint32_t black, uint32_t white, uint32_t kings, bool isBlacksTurn, bool wasKinged);
Expand All @@ -83,7 +85,9 @@ class BitBoard
uint32_t getWhitePieces() { return m_whitePieces; };
uint32_t getKings () { return m_kings; };
bool getIsBlacksTurn() { return m_isBlacksTurn; };
bool getWasKinged() { return m_wasKinged; }
bool getWasKinged() { return m_wasKinged; };

int getTotalPieceCount();

std::string player() const;
vmup actions() const;
Expand Down
17 changes: 17 additions & 0 deletions include/Game.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef GAME_H
#define GAME_H

#include "Specimen.hpp"

class Game {
Specimen black;
Specimen white;

public:
Game(const Specimen& b, const Specimen& w):
black(b),
white(w) {};
void play(int *winner);
};

#endif
28 changes: 28 additions & 0 deletions include/GeneticSimulation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef GENETIC_SIMULATION_H
#define GENETIC_SIMULATION_H

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

class GeneticSimulation {

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

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

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

};


#endif
8 changes: 6 additions & 2 deletions include/Heuristic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@

class Heuristic
{

public:
const static int numFeatures = 11;
private:
// Feature coefficients
int coeff[11] = {1,1,1,1,1,1,1,1,1,1,1};
int coeff[numFeatures] = {1,1,1,1,1,1,1,1,1,1,1};

// Feature Functions
int pawnCount() const;
Expand All @@ -27,7 +31,7 @@ class Heuristic
BitBoard *board;

public:
// Heuristic(int *co[10]) { coeff = *co;};
void set(int index, int value) { coeff[index] = value; };
int evaluate(bup &bitboard);

// Needed public for variable depth search
Expand Down
4 changes: 2 additions & 2 deletions include/MinimaxSearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class MinimaxSearch
double alphaBeta(std::unique_ptr<BitBoard> &board, double alpha, double beta, int depth);
bool shouldStop(std::unique_ptr<BitBoard> &board, int depth);


public:
public:
mup minimaxDecision(std::unique_ptr<BitBoard> &board);
MinimaxSearch(Heuristic& h): heuristic(h){};
MinimaxSearch() {};
void setHeuristics(Heuristic h) { heuristic = h; };
};

#endif
19 changes: 19 additions & 0 deletions include/Specimen.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef SPECIMEN_H
#define SPECIMEN_H

#include "Heuristic.hpp"

class Specimen{
Heuristic startgame;
Heuristic endgame;
public:
Specimen(Heuristic start, Heuristic end):
startgame(start),
endgame(end) {};

Heuristic getStartHeuristics() { return startgame; };
Heuristic getEndHeuristics() { return endgame; };
};


#endif
8 changes: 8 additions & 0 deletions src/BitBoard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@ bup BitBoard::result(mup &move) const
return result;
}

const int BitBoard::count(uint32_t i) const{
return std::bitset<32>(i).count();
}

int BitBoard::getTotalPieceCount(){
return count(m_blackPieces) + count(m_whitePieces);
}

void BitBoard::printState() const
{
std::cout<<"-"<<pieceToString(11)<<"-"<<pieceToString(5)<<"-"<<pieceToString(31)<<"-"<<pieceToString(25)<<std::endl;
Expand Down
67 changes: 67 additions & 0 deletions src/Game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

#include "MinimaxSearch.h"
#include "Game.hpp"

void Game::play(int *winner){
bup board(new BitBoard());

MinimaxSearch blackSearch;
blackSearch.setHeuristics(black.getStartHeuristics());
MinimaxSearch whiteSearch;
whiteSearch.setHeuristics(white.getEndHeuristics());

bool isEndGame = false;

std::unique_ptr<Move> bestMove = blackSearch.minimaxDecision(board);


const int noCaptureLimit = 50;
int noCaptureCount = 0;
int piecesOnBoard = 24;

while(bestMove != nullptr && !bestMove->isEmpty()){
// update board
board = board->result(bestMove);


if(piecesOnBoard > board->getTotalPieceCount())
noCaptureCount = 0;
else
noCaptureCount++;

// draw after some limit of no capture
if(noCaptureCount >= noCaptureLimit)
{
*winner = 3;
break;
}

piecesOnBoard = board->getTotalPieceCount();

// check for endgame
if(!isEndGame)
if(piecesOnBoard > 12){
isEndGame = true;
blackSearch.setHeuristics(black.getEndHeuristics());
whiteSearch.setHeuristics(white.getEndHeuristics());
}

//get next move
if(board->player().compare("black"))
bestMove = blackSearch.minimaxDecision(board);
else
bestMove = whiteSearch.minimaxDecision(board);
}

std::cout<< "\n";
board->printState();

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

}
34 changes: 25 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,12 @@
#include "MinimaxSearch.h"
#include "Heuristic.hpp"
#include "Client.hpp"
#include "GeneticSimulation.hpp"

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

if(argc < 3)
{
std::cout << "Please specify args: userID oppID\n";
std::cout << "E.g. ./build/runner 13 0\n";
return -1;
}
void serverGame(int userID, int oppID){

// Set up telnet connection
Client c(atoi(argv[1]), atoi(argv[2]));
Client c(userID, oppID);

std::unique_ptr<BitBoard> board (new BitBoard);
Heuristic heur_test;
Expand Down Expand Up @@ -96,5 +90,27 @@ int main(int argc, const char * argv[]) {
std::cout << "=======================================" << std::endl;
}

}

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

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

if(argc == 1)
{
std::cout << "Please specify args: userID oppID\n";
std::cout << "E.g. ./build/runner 13 0\n";
std::cout << "\n\nGenetic Alg simulation:\n";
std::cout << "./build/runner specimen rounds 1 \n";
return -1;
}
else if(argc == 3)
serverGame(atoi(argv[1]), atoi(argv[2]));
else
geneticSimulation(atoi(argv[1]));

return 0;
}

0 comments on commit 61bdcb0

Please sign in to comment.