Skip to content

Commit

Permalink
current compiling version with npe
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Jöst committed Oct 13, 2019
1 parent 6c50e62 commit 913eb85
Show file tree
Hide file tree
Showing 18 changed files with 166 additions and 81 deletions.
2 changes: 2 additions & 0 deletions open_spiel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ set (OPEN_SPIEL_OBJECTS
$<TARGET_OBJECTS:game_transforms>
$<TARGET_OBJECTS:bridge_double_dummy_solver>
# $<TARGET_OBJECTS:hanabi_learning_environment>
$<TARGET_OBJECTS:universal_poker_clib>
$<TARGET_OBJECTS:universal_poker_lib>
$<TARGET_OBJECTS:algorithms>
)

Expand Down
91 changes: 70 additions & 21 deletions open_spiel/games/universal_poker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

#include <algorithm>
#include <array>
#include <numeric>
#include <utility>

#include "open_spiel/abseil-cpp/absl/strings/str_format.h"
#include "open_spiel/abseil-cpp/absl/strings/str_join.h"
#include "open_spiel/game_parameters.h"
#include "open_spiel/spiel_utils.h"

#include "open_spiel/games/universal_poker/PokerGame/PokerGame.h"

namespace open_spiel::universal_poker {
const GameType kGameType{
/*short_name=*/"universal_poker",
Expand All @@ -26,16 +24,19 @@ namespace open_spiel::universal_poker {
/*provides_observation=*/true,
/*provides_observation_as_normalized_vector=*/true,
/*parameter_specification=*/
{{"players", GameParameter(kDefaultPlayers)}}};
{{"gameDesc", GameParameter(gameDesc)}}};

std::shared_ptr<const Game> Factory(const GameParameters& params) {
std::shared_ptr<const Game> Factory(const GameParameters &params) {
return std::shared_ptr<const Game>(new UniversalPokerGame(params));
}

REGISTER_SPIEL_GAME(kGameType, Factory);

// namespace universal_poker
UniversalPokerState::UniversalPokerState(std::shared_ptr<const Game> game) : State(game) {
UniversalPokerState::UniversalPokerState(std::shared_ptr<const Game> game)
: State(game),
internalState_( ) {


}

Expand Down Expand Up @@ -83,42 +84,90 @@ namespace open_spiel::universal_poker {
return State::ChanceOutcomes();
}

std::vector<Action> UniversalPokerState::LegalActions() const {
return std::vector<Action>();
}


/**
* Universal Poker Game Constructor
* @param params
*/
UniversalPokerGame::UniversalPokerGame(const GameParameters &params)
:Game(kGameType, params)
{
: Game(kGameType, params),
gameDesc_(ParameterValue<std::string>("gameDesc")),
pokerGame_(PokerGame::createFromGamedef(gameDesc_)) {
maxGameLength_ = pokerGame_.getGameLength();
maxBoardCardCombinations_ = numBoardCardCombinations_(0);

for (int r = 1; r < numRounds_(); r++) {
int combos = numBoardCardCombinations_(r);
if (combos > maxBoardCardCombinations_) {
maxBoardCardCombinations_ = combos;
}
}
}

std::unique_ptr<State> UniversalPokerGame::NewInitialState() const {
return std::unique_ptr<State>();
return std::unique_ptr<State>(new UniversalPokerState(shared_from_this()));
}

std::vector<int> UniversalPokerGame::InformationStateNormalizedVectorShape() const {
// One-hot encoding for player number (who is to play).
// 2 slots of cards (total_cards_ bits each): private card, public card
// Followed by maximum game length * 2 bits each (call / raise)
return {(numPlayers_()) + ((numBoardCards_(numRounds_() - 1) + numHoleCards_(0))) + (MaxGameLength() * 2)};
}

std::vector<int> UniversalPokerGame::ObservationNormalizedVectorShape() const {
// One-hot encoding for player number (who is to play).
// 2 slots of cards (total_cards_ bits each): private card, public card
// Followed by the contribution of each player to the pot
return {(numPlayers_()) + (numBoardCards_(numRounds_() - 1) + numHoleCards_(0)) + (numPlayers_())};
}

double UniversalPokerGame::MaxUtility() const {
return 0;
// In poker, the utility is defined as the money a player has at the end of
// the game minus then money the player had before starting the game.
// The most a player can win *per opponent* is the most each player can put
// into the pot, which is the raise amounts on each round times the maximum
// number raises, plus the original chip they put in to play.
return 1.0;
}

double UniversalPokerGame::MinUtility() const {
return 0;
// In poker, the utility is defined as the money a player has at the end of
// the game minus then money the player had before starting the game.
// The most any single player can lose is the maximum number of raises per
// round times the amounts of each of the raises, plus the original chip they
// put in to play.
return -1.0;
}

std::vector<int> UniversalPokerGame::InformationStateNormalizedVectorShape() const {
return Game::InformationStateNormalizedVectorShape();
}

std::vector<int> UniversalPokerGame::ObservationNormalizedVectorShape() const {
return Game::ObservationNormalizedVectorShape();
int UniversalPokerGame::numBoardCardCombinations_(const int r) const {
assert(r < numRounds_());
int deckSize = deckSize_();

for (int i = 0; i < r; i++) {
deckSize -= numBoardCards_(r);
}

return choose_(deckSize, numBoardCards_(r));
}

int UniversalPokerGame::MaxGameLength() const {
return 0;
int UniversalPokerGame::choose_(int n, int k) {
if (k == 0) {
return 1;
}
return (n * choose_(n - 1, k - 1)) / k;
}

int UniversalPokerGame::MaxChanceOutcomes() const {
return Game::MaxChanceOutcomes();
return maxBoardCardCombinations_;
}

int UniversalPokerGame::NumPlayers() const {
return 0;
return numPlayers_();
}
}
47 changes: 30 additions & 17 deletions open_spiel/games/universal_poker.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,14 @@
#include <memory>
#include <string>
#include <vector>
#include <open_spiel/games/universal_poker/PokerGame/PokerGame.h>

#include "open_spiel/spiel.h"

namespace open_spiel {
namespace universal_poker {

// Default parameters.

// TODO(b/127425075): Use std::optional instead of sentinel values once absl is
// added as a dependency.
inline constexpr int kInvalidCard = -10000;
inline constexpr int kDefaultPlayers = 2;
inline constexpr int kNumSuits = 2;
inline constexpr int kFirstRaiseAmount = 2;
inline constexpr int kSecondRaiseAmount = 4;
inline constexpr int kTotalRaisesPerRound = 2;
inline constexpr int kMaxRaises = 2;
inline constexpr int kStartingMoney = 100;
inline constexpr int kNumInfoStates =
936; // Number of info state in the 2P game.
inline const std::string gameDesc("GAMEDEF\nnolimit\nnumPlayers = 2\nnumRounds = 2\nstack = 1200 1200\nblind = 100 100\nfirstPlayer = 1 1\nnumSuits = 2\nnumRanks = 3\nnumHoleCards = 1\nnumBoardCards = 0 1\nEND GAMEDEF");

class UniversalPokerGame;

Expand All @@ -48,6 +36,11 @@ class UniversalPokerState : public State {
std::unique_ptr<State> Clone() const override;
// The probability of taking each possible action in a particular info state.
std::vector<std::pair<Action, double>> ChanceOutcomes() const override;

std::vector<Action> LegalActions() const override;

private:
PokerGameState internalState_;
};

class UniversalPokerGame : public Game {
Expand All @@ -56,18 +49,38 @@ class UniversalPokerGame : public Game {

int NumDistinctActions() const override { return 3; }
std::unique_ptr<State> NewInitialState() const override;
int MaxChanceOutcomes() const override;
int NumPlayers() const override;
double MinUtility() const override;
double MaxUtility() const override;
double UtilitySum() const override { return 0; }

int MaxChanceOutcomes() const override;

double UtilitySum() const override { return 0; }
std::shared_ptr<const Game> Clone() const override {
return std::shared_ptr<const Game>(new UniversalPokerGame(*this));
}
std::vector<int> InformationStateNormalizedVectorShape() const override;
std::vector<int> ObservationNormalizedVectorShape() const override;
int MaxGameLength() const override;
int MaxGameLength() const override {return maxGameLength_ ;};

private:
std::string gameDesc_;
PokerGame pokerGame_;
int maxGameLength_;
int maxBoardCardCombinations_;

protected:
int numPlayers_() const {return pokerGame_.getGame().numPlayers;};
int deckSize_() const {return pokerGame_.getGame().numRanks * pokerGame_.getGame().numSuits; };
int numRounds_() const {return pokerGame_.getGame().numRounds;};
int stackSize_(int p) const {return pokerGame_.getGame().stack[p]; };
int numHoleCards_(int p) const {return pokerGame_.getGame().numHoleCards; };
int numRounds() const {return pokerGame_.getGame().numRounds; };

int numBoardCards_(int r) const {return pokerGame_.getGame().numBoardCards[r]; };
int numBoardCardCombinations_(int r) const;

static int choose_(int n, int k);
};

} // namespace universal_poker
Expand Down
2 changes: 1 addition & 1 deletion open_spiel/games/universal_poker/BettingTree/BettingNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <string>
extern "C"
{
#include <ACPC/game.h>
#include "open_spiel/games/universal_poker/ACPC/game.h"
};

#define ACTION_FOLD 0
Expand Down
6 changes: 3 additions & 3 deletions open_spiel/games/universal_poker/BettingTree/BettingTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
#include <cassert>
extern "C"
{
#include <ACPC/game.h>
#include "open_spiel/games/universal_poker/ACPC/game.h"
};

#include <cmath>
#include <ACPC/GameDefinitions.h>
#include <ACPC/game.h>
#include "open_spiel/games/universal_poker/ACPC/GameDefinitions.h"
#include "open_spiel/games/universal_poker/ACPC/game.h"
#include <algorithm>
#include <iostream>
#include <sstream>
Expand Down
2 changes: 0 additions & 2 deletions open_spiel/games/universal_poker/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ set(SOURCE_FILES
add_library(universal_poker_clib ${CLIB_FILES} )
set_target_properties(universal_poker_clib PROPERTIES POSITION_INDEPENDENT_CODE ON)

include_directories(.)

# The library contains header and source files.
add_library(universal_poker_lib STATIC
${SOURCE_FILES}
Expand Down
4 changes: 2 additions & 2 deletions open_spiel/games/universal_poker/CardTree/CardNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#include <eigen3/Eigen/Dense>
#endif

#include "CardTree/CardSet.h"
#include "CardTree/CardSetIndex.h"
#include "open_spiel/games/universal_poker/CardTree/CardSet.h"
#include "open_spiel/games/universal_poker/CardTree/CardSetIndex.h"


typedef Eigen::Map<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>, 0,
Expand Down
4 changes: 2 additions & 2 deletions open_spiel/games/universal_poker/CardTree/CardSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
std::string suitChars = "cdhs";
std::string rankChars = "23456789TJQKA";
extern "C" {
#include "ACPC/game.h"
#include "ACPC/evalHandTables"
#include "open_spiel/games/universal_poker/ACPC/game.h"
#include "open_spiel/games/universal_poker/ACPC/evalHandTables"
}
CardSet::CardSet()
:cs()
Expand Down
2 changes: 1 addition & 1 deletion open_spiel/games/universal_poker/CardTree/CardSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

extern "C"
{
#include "ACPC/game.h"
#include "open_spiel/games/universal_poker/ACPC/game.h"
};

class CardSet {
Expand Down
2 changes: 1 addition & 1 deletion open_spiel/games/universal_poker/CardTree/CardSetIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "CardSetIndex.h"
extern "C" {
#include "ACPC/game.h"
#include "open_spiel/games/universal_poker/ACPC/game.h"
}

#include <algorithm>
Expand Down
2 changes: 1 addition & 1 deletion open_spiel/games/universal_poker/CardTree/CardTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Created by Dennis Jöst on 03.05.18.
//

#include <ACPC/game.h>
#include "open_spiel/games/universal_poker/ACPC/game.h"
#include <iostream>
#include "CardTree.h"
#define DEBUG 0
Expand Down
2 changes: 1 addition & 1 deletion open_spiel/games/universal_poker/CardTree/CardTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "CardNode.h"

extern "C" {
#include <ACPC/game.h>
#include "open_spiel/games/universal_poker/ACPC/game.h"
};

class CardTree {
Expand Down
4 changes: 2 additions & 2 deletions open_spiel/games/universal_poker/GameTree/GameNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#define DEEPSTACK_CPP_GAMENODE_H


#include <BettingTree/BettingNode.h>
#include <CardTree/CardNode.h>
#include "open_spiel/games/universal_poker/BettingTree/BettingNode.h"
#include "open_spiel/games/universal_poker/CardTree/CardNode.h"

class GameNode {
public:
Expand Down
4 changes: 2 additions & 2 deletions open_spiel/games/universal_poker/GameTree/GameTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
//

extern "C" {
#include <ACPC/game.h>
#include "open_spiel/games/universal_poker/ACPC/game.h"
};

#include <iostream>
#include <fstream>
#include <ACPC/game.h>
#include "open_spiel/games/universal_poker/ACPC/game.h"
#include <random>
#include "GameTree.h"
#include <regex>
Expand Down
4 changes: 2 additions & 2 deletions open_spiel/games/universal_poker/GameTree/GameTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

#define MAX_GAME_NODES (16*1024*1024)

#include <BettingTree/BettingTree.h>
#include <CardTree/CardTree.h>
#include "open_spiel/games/universal_poker/BettingTree/BettingTree.h"
#include "open_spiel/games/universal_poker/CardTree/CardTree.h"
#include <random>
#include <memory>
#include <mutex>
Expand Down
Loading

0 comments on commit 913eb85

Please sign in to comment.