Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <boost/archive/binary_oarchive.hpp>
- #include <boost/archive/binary_iarchive.hpp>
- #include <boost/serialization/serialization.hpp>
- #include <boost/serialization/unordered_map.hpp>
- #include <boost/serialization/map.hpp>
- #include <boost/serialization/vector.hpp>
- #include <boost/functional.hpp>
- #include <boost/container_hash/hash.hpp>
- using boost::hash_value;
- struct Card {
- int rank, suit;
- Card(int rank = 0, int suit = 0) : rank(rank), suit(suit) {}
- template<class Archive>
- void serialize(Archive& ar, unsigned /*version*/) {
- ar & rank;
- ar & suit;
- }
- friend size_t hash_value(Card const& c) {
- auto v = hash_value(c.rank);
- boost::hash_combine(v, hash_value(c.suit));
- return v;
- }
- bool operator==(Card const& rhs) const {
- return std::tie(rank, suit) == std::tie(rhs.rank, rhs.suit);
- }
- };
- struct DrawAction {
- bool fromDeck;
- Card card;
- explicit DrawAction(bool fromDeck = false, Card card = {})
- : fromDeck(fromDeck), card(card) {}
- template<class Archive>
- void serialize(Archive& ar, unsigned /*version*/) {
- ar & fromDeck;
- ar & card;
- }
- friend size_t hash_value(DrawAction const& da) {
- auto v = hash_value(da.fromDeck);
- boost::hash_combine(v, hash_value(da.card));
- return v;
- }
- bool operator==(DrawAction const& rhs) const {
- return std::tie(fromDeck, card) == std::tie(rhs.fromDeck, rhs.card);
- }
- };
- using Cards = std::vector<Card>;
- using Hand = Cards;
- using Draws = std::vector<DrawAction>;
- class InfosetHistory {
- public:
- Card initialDiscardPile;
- Hand initialHand;
- Draws playerDrawActions;
- Cards playerDiscardActions;
- Draws opponentDrawActions;
- Cards opponentDiscardActions;
- InfosetHistory(
- Card initialDiscardPile = {},
- Hand hand = {},
- Draws playerDrawActions = {},
- Cards playerDiscardActions = {},
- Draws opponentDrawActions = {},
- Cards opponentDiscardActions = {}
- ) : initialDiscardPile(initialDiscardPile),
- initialHand(std::move(hand)),
- playerDrawActions(std::move(playerDrawActions)),
- playerDiscardActions(std::move(playerDiscardActions)),
- opponentDrawActions(std::move(opponentDrawActions)),
- opponentDiscardActions(std::move(opponentDiscardActions)) {}
- template<class Archive>
- void serialize(Archive& ar, const unsigned int /*version*/) {
- ar & initialDiscardPile & initialHand
- & playerDrawActions & playerDiscardActions
- & opponentDrawActions & opponentDiscardActions;
- }
- friend size_t hash_value(InfosetHistory const& ish) {
- auto v = hash_value(ish.initialDiscardPile);
- auto combine = [&v](auto& range) { boost::hash_range(v, begin(range), end(range)); };
- combine(ish.initialHand);
- combine(ish.playerDrawActions);
- combine(ish.playerDiscardActions);
- combine(ish.opponentDrawActions);
- combine(ish.opponentDiscardActions);
- return v;
- }
- bool operator==(InfosetHistory const& rhs) const {
- return std::tie(initialDiscardPile, initialHand, playerDrawActions,
- playerDiscardActions, opponentDrawActions,
- opponentDiscardActions)
- == std::tie(rhs.initialDiscardPile, rhs.initialHand,
- rhs.playerDrawActions, rhs.playerDiscardActions,
- rhs.opponentDrawActions, rhs.opponentDiscardActions);
- }
- };
- class Node {
- public:
- Cards allowedActions;
- unsigned int NUM_ACTIONS{};
- std::vector<double> regretSum;
- std::vector<double> strategySum;
- unsigned char phase{};
- explicit Node(std::vector<Card> allowedActions = {},
- unsigned int NUM_ACTIONS = 0,
- std::vector<double> regretSum = {},
- std::vector<double> strategySum = {},
- unsigned char phase = 0
- ) : allowedActions(std::move(allowedActions)),
- NUM_ACTIONS(NUM_ACTIONS),
- regretSum(std::move(regretSum)),
- strategySum(std::move(strategySum)),
- phase(phase) {}
- template<class Archive>
- void serialize(Archive& ar, unsigned /*version*/) {
- ar & allowedActions
- & NUM_ACTIONS
- & regretSum & strategySum & phase;
- }
- };
- #include <map>
- #include <fstream>
- template<typename K, typename V>
- using htable = std::unordered_map<K, V, boost::hash<K> >;
- using NodeMap = htable<std::vector<Card>, htable<InfosetHistory, Node>>;
- int main() {
- {
- // LINES 147 - 267 ARE FOR READING IN THE DATA FROM THE FILE
- std::ifstream file("StrategyWritten0.txt");
- NodeMap nodeMap1;
- int mapSize;
- file >> mapSize;
- for (int i = 0; i < mapSize; i++) {
- std::vector<Card> hand;
- for (int t = 0; t < 10; t++) {
- int rank;
- file >> rank;
- int suit;
- file >> suit;
- hand.emplace_back(rank, suit);
- }
- htable<InfosetHistory, Node> subMap;
- int subMapSize;
- file >> subMapSize;
- for (int j = 0; j < subMapSize; j++) {
- Card initialDiscardPile;
- std::vector<Card> initialHand;
- std::vector<DrawAction> playerDrawActions;
- std::vector<Card> playerDiscardActions;
- std::vector<DrawAction> opponentDrawActions;
- std::vector<Card> opponentDiscardActions;
- for (int t = 0; t < 10; t++) {
- int rank;
- file >> rank;
- int suit;
- file >> suit;
- initialHand.emplace_back(rank, suit);
- }
- int r;
- file >> r;
- int s;
- file >> s;
- initialDiscardPile = Card(r, s);
- int numPlayerDrawActions;
- file >> numPlayerDrawActions;
- for (int t = 0; t < numPlayerDrawActions; t++) {
- bool fromDeck;
- file >> fromDeck;
- int rank;
- file >> rank;
- int suit;
- file >> suit;
- playerDrawActions.emplace_back(fromDeck, Card(rank, suit));
- }
- int numPlayerDiscardActions;
- file >> numPlayerDiscardActions;
- for (int t = 0; t < numPlayerDiscardActions; t++) {
- int rank;
- file >> rank;
- int suit;
- file >> suit;
- playerDiscardActions.emplace_back(rank, suit);
- }
- int numOpponentDrawActions;
- file >> numOpponentDrawActions;
- for (int t = 0; t < numOpponentDrawActions; t++) {
- bool fromDeck;
- file >> fromDeck;
- int rank;
- file >> rank;
- int suit;
- file >> suit;
- opponentDrawActions.emplace_back(fromDeck, Card(rank, suit));
- }
- int numOpponentDiscardActions;
- file >> numOpponentDiscardActions;
- for (int t = 0; t < numOpponentDiscardActions; t++) {
- int rank;
- file >> rank;
- int suit;
- file >> suit;
- opponentDiscardActions.emplace_back(rank, suit);
- }
- InfosetHistory history(initialDiscardPile, initialHand, playerDrawActions, playerDiscardActions,
- opponentDrawActions, opponentDiscardActions);
- std::vector<Card> allowedActions;
- unsigned int NUM_ACTIONS;
- std::vector<double> regretSum;
- std::vector<double> strategySum;
- unsigned char phase;
- int numAllowedActions;
- file >> numAllowedActions;
- for (int t = 0; t < numAllowedActions; t++) {
- int rank;
- file >> rank;
- int suit;
- file >> suit;
- allowedActions.emplace_back(rank, suit);
- }
- file >> NUM_ACTIONS;
- for (int t = 0; t < NUM_ACTIONS; t++) {
- double regret;
- file >> regret;
- regretSum.push_back(regret);
- }
- for (int t = 0; t < NUM_ACTIONS; t++) {
- double sum;
- file >> sum;
- strategySum.push_back(sum);
- }
- file >> phase;
- Node node(allowedActions, NUM_ACTIONS, regretSum, strategySum, phase);
- subMap.insert(std::make_pair(history, node));
- }
- nodeMap1.insert(std::make_pair(hand, subMap));
- }
- std::cout << "Successfully created Node Map" << std::endl;
- // SERIALIZE NODE MAP
- std::ofstream f("TempStrategy.bin");
- boost::archive::binary_oarchive archive(f);
- archive << nodeMap1;
- std::cout << "Serialized Node Map" << std::endl;
- }
- {
- // DESERIALIZE NODE MAP
- std::ifstream ifs("TempStrategy.bin");
- NodeMap nodeMap2;
- if (ifs.good()) {
- boost::archive::binary_iarchive ia(ifs);
- ia >> nodeMap2;
- }
- std::cout << "Deserialized Node Map" << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement