Skip to content

Commit

Permalink
Iridium 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmobobak committed May 13, 2021
1 parent f68f314 commit c892a21
Show file tree
Hide file tree
Showing 23 changed files with 880 additions and 163 deletions.
14 changes: 5 additions & 9 deletions src/MCSearch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@
#include <chrono>
#include <limits>

#include "Connect4-4x4.hpp"
#include "Connect4.hpp"
#include "Gomoku.hpp"
#include "RawTree.hpp"
#include "TicTacToe.hpp"
#include "TreeNode.hpp"
#include "UCT.hpp"
#include "UTTT.hpp"
#include "TreeNode.hpp"

constexpr auto INF = std::numeric_limits<int>::max();
constexpr auto N_INF = std::numeric_limits<int>::lowest() + 1;
Expand All @@ -33,11 +27,12 @@ class MCTS {

// flags
bool readout = true;
bool debug = true;
bool debug = false;
bool limit_by_rollouts;
bool limit_by_time;

// recorded search data
// std::array<int, State::NUM_UNIQUE_MOVES> amaf_counters;
// the win / loss ratio of the most recently played move
double last_winloss;
int node_count;
Expand Down Expand Up @@ -244,7 +239,7 @@ class MCTS {
return node;
}

int relative_reward(int perspective, int reward) {
int relative_reward(int perspective, int reward) const {
// designed for two-player zero-sum environments.
// my win == your loss
if (perspective == this->side) {
Expand Down Expand Up @@ -280,6 +275,7 @@ class MCTS {
}

return (playout_board.evaluate() + 1) * 5; // 1/0/-1 -> 10/5/0
// return playout_board.evaluate() == side ? 10 : -10; // 1/0/-1 -> 10/0/-10
}

// DEBUG
Expand Down
4 changes: 2 additions & 2 deletions src/NMSearch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class Negamax {
for (auto move : node.legal_moves()) {
node.play(move);
score = -negamax(node, depth - 1, -colour, -b, -a);
node.unplay();
node.unplay(move);

if (score >= b) {
// beta cutoff
Expand All @@ -125,7 +125,7 @@ class Negamax {
for (auto move : node.legal_moves()) {
node.play(move);
int score = -dnegamax(node, -colour, -b, -a);
node.unplay();
node.unplay(move);
// std::cout << "score for move " << (int)move << ": " << score << "\n";

if (score >= b) {
Expand Down
55 changes: 0 additions & 55 deletions src/RAVE.hpp

This file was deleted.

11 changes: 5 additions & 6 deletions src/TreeNode.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#pragma once

#include "Connect4-4x4.hpp"
#include "Connect4.hpp"
#include "Gomoku.hpp"
#include "RawTree.hpp"
#include "TicTacToe.hpp"
#include "UTTT.hpp"
#include "games/Connect4-4x4.hpp"
#include "games/Connect4.hpp"
#include "games/Gomoku.hpp"
#include "games/TicTacToe.hpp"
#include "games/UTTT2.hpp"

namespace TreeNode {
template <class State>
Expand Down
16 changes: 7 additions & 9 deletions src/UCT.hpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
#pragma once

#include <limits>
#include <cmath>
#include <limits>

#include "Connect4-4x4.hpp"
#include "Connect4.hpp"
#include "Gomoku.hpp"
#include "RawTree.hpp"
#include "TicTacToe.hpp"
#include "TreeNode.hpp"
#include "UTTT.hpp"
#include "games/Connect4-4x4.hpp"
#include "games/Connect4.hpp"
#include "games/Gomoku.hpp"
#include "games/TicTacToe.hpp"
#include "games/UTTT2.hpp"

template <class Node, int EXP_FACTOR>
class UCT {
public:
static auto ucb1_value(int parent_visits, int win_count, int visits) -> double {
if (visits == 0) {
return std::numeric_limits<double>::max();
return std::numeric_limits<double>::max() - 1;
}
double exploitation = (double)win_count / (double)visits;
double exploration = sqrt(log((double)parent_visits) / (double)visits) * EXP_FACTOR;
Expand Down
26 changes: 0 additions & 26 deletions src/accelerations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,6 @@
#include <iostream>
#include <string>

template <typename T>
auto pop(std::vector<T> v) -> T {
T result = v.back();
v.pop_back();
return result;
}

template <typename T, std::size_t N>
struct std::hash<std::array<T, N> > {
std::size_t operator()(const std::array<T, N>& arr) const {
/* 32 bit FNV-1a hash */
std::size_t h = 2166136261;
for (const auto& item : arr) {
h ^= std::hash<T>()(item);
h *= 16777619;
}
return h;
}
};

inline void copy_int8_array_81(const uint_fast8_t in[], uint_fast8_t out[]) {
for (size_t i = 0; i < 81; i++) {
out[i] = in[i];
}
}

template <typename T>
auto zipstring(std::vector<T> v1, std::vector<T> v2) -> std::string {
assert(v1.size() == v2.size());
Expand Down
Binary file modified src/bench
Binary file not shown.
File renamed without changes.
10 changes: 7 additions & 3 deletions src/Connect4-4x4.hpp → src/games/Connect4-4x4.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include <numeric>
#include <vector>

#include "accelerations.hpp"

namespace Connect4x4 {

class State {
Expand All @@ -19,6 +17,7 @@ class State {
static constexpr auto NUM_COLS = 4;
static constexpr auto GAME_EXP_FACTOR = 8;
static constexpr auto BB_ALL = 0b1111;
static constexpr auto NUM_UNIQUE_MOVES = 4;
static constexpr std::array<int, NUM_COLS> weights = {2, 1, 1, 2};

private:
Expand Down Expand Up @@ -305,7 +304,12 @@ class State {
std::vector<Move> shiftedLegals;
std::transform(legals.begin(), legals.end(), std::back_inserter(shiftedLegals), [](Move n) { return n + 1; });
std::cout << "Your legal moves are: ";
showvec(shiftedLegals);
std::cout << "{ ";
for (auto i : shiftedLegals) {
std::cout << (int)i;
std::cout << ", ";
}
std::cout << "}";
}

auto get_player_move() const -> Move {
Expand Down
29 changes: 15 additions & 14 deletions src/Connect4.hpp → src/games/Connect4.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include <numeric>
#include <vector>

#include "accelerations.hpp"

namespace Connect4 {
using Bitrow = uint_fast8_t;
using Bitboard = unsigned long long;
Expand Down Expand Up @@ -51,6 +49,11 @@ class State {
return node;
}

// SETTERS
void set_move_count(int n) {
move_count = n;
}

// PREDICATES
auto is_full() const -> bool {
return move_count == MAX_GAME_LENGTH;
Expand All @@ -68,12 +71,6 @@ class State {
[move](Move i) { return i == move; });
}

void set_node(unsigned long long xs, unsigned long long ys) {}

void set_move_count(int n) {
move_count = n;
}

// MOVE GENERATION
auto num_legal_moves() const -> int {
// this is a fast function to determine the number
Expand Down Expand Up @@ -366,7 +363,12 @@ class State {
std::vector<Move> shiftedLegals;
std::transform(legals.begin(), legals.end(), std::back_inserter(shiftedLegals), [](Move n) { return n + 1; });
std::cout << "Your legal moves are: ";
showvec(shiftedLegals);
std::cout << "{ ";
for (auto i : shiftedLegals) {
std::cout << (int)i;
std::cout << ", ";
}
std::cout << "}";
}

auto get_player_move() const -> Move {
Expand All @@ -381,10 +383,9 @@ class State {
}
return move - 1;
}
};

bool operator==(const State a, const State b) {
return a.get_node()[0] == b.get_node()[0]
&& a.get_node()[1] == b.get_node()[1];
}
friend bool operator==(const State& a, const State& b) {
return a.node == b.node;
}
};
} // namespace Connect4
1 change: 1 addition & 0 deletions src/Go.hpp → src/games/Go.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class State {
using Move = int;
using Bitboard = unsigned long long;
std::array<std::bitset<WIDTH * HEIGHT>, 2> node;
static constexpr auto NUM_UNIQUE_MOVES = WIDTH * HEIGHT;
int turn = 1;
std::vector<Move> movestack;

Expand Down
22 changes: 20 additions & 2 deletions src/Gomoku.hpp → src/games/Gomoku.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,25 @@
#include <bitset>
#include <iostream>
#include <vector>

#include "accelerations.hpp"
#include <cassert>

template <typename T>
auto zipstring(std::vector<T> v1, std::vector<T> v2) -> std::string {
assert(v1.size() == v2.size());
std::string builder;
builder.append("{ ");
for (size_t i = 0; i < v1.size(); i++) {
// builder.append((std::to_string)(i));
builder += '(';
builder.append(std::to_string(v1[i]));
builder += ',';
builder.append(std::to_string(v2[i]));
builder += ')';
builder.append(", ");
}
builder.append("}");
return builder;
}

namespace Gomoku {

Expand All @@ -17,6 +34,7 @@ class State {
static constexpr auto WIDTH = 8;
static constexpr auto HEIGHT = 8;
static constexpr auto MAX_GAME_LENGTH = WIDTH * HEIGHT;
static constexpr auto NUM_UNIQUE_MOVES = WIDTH * HEIGHT;
static constexpr std::array<char, 2> players = {'X', 'O'};

private:
Expand Down
Loading

0 comments on commit c892a21

Please sign in to comment.