Advertisement
MrSpaceCow

Minimal Reproducible Example

Jun 7th, 2020
2,347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <boost/archive/binary_oarchive.hpp>
  3. #include <boost/archive/binary_iarchive.hpp>
  4. #include <boost/serialization/serialization.hpp>
  5. #include <boost/serialization/unordered_map.hpp>
  6. #include <boost/serialization/map.hpp>
  7. #include <boost/serialization/vector.hpp>
  8. #include <boost/functional.hpp>
  9. #include <boost/container_hash/hash.hpp>
  10.  
  11. using boost::hash_value;
  12.  
  13. struct Card {
  14.     int rank, suit;
  15.  
  16.     Card(int rank = 0, int suit = 0) : rank(rank), suit(suit) {}
  17.  
  18.     template<class Archive>
  19.     void serialize(Archive& ar, unsigned /*version*/) {
  20.         ar & rank;
  21.         ar & suit;
  22.     }
  23.  
  24.     friend size_t hash_value(Card const& c) {
  25.         auto v = hash_value(c.rank);
  26.         boost::hash_combine(v, hash_value(c.suit));
  27.         return v;
  28.     }
  29.  
  30.     bool operator==(Card const& rhs) const {
  31.         return std::tie(rank, suit) == std::tie(rhs.rank, rhs.suit);
  32.     }
  33. };
  34.  
  35. struct DrawAction {
  36.     bool fromDeck;
  37.     Card card;
  38.  
  39.     explicit DrawAction(bool fromDeck = false, Card card = {})
  40.             : fromDeck(fromDeck), card(card) {}
  41.  
  42.     template<class Archive>
  43.     void serialize(Archive& ar, unsigned /*version*/) {
  44.         ar & fromDeck;
  45.         ar & card;
  46.     }
  47.  
  48.     friend size_t hash_value(DrawAction const& da) {
  49.         auto v = hash_value(da.fromDeck);
  50.         boost::hash_combine(v, hash_value(da.card));
  51.         return v;
  52.     }
  53.  
  54.     bool operator==(DrawAction const& rhs) const {
  55.         return std::tie(fromDeck, card) == std::tie(rhs.fromDeck, rhs.card);
  56.     }
  57. };
  58.  
  59. using Cards = std::vector<Card>;
  60. using Hand  = Cards;
  61. using Draws = std::vector<DrawAction>;
  62.  
  63. class InfosetHistory {
  64. public:
  65.     Card initialDiscardPile;
  66.     Hand initialHand;
  67.     Draws playerDrawActions;
  68.     Cards playerDiscardActions;
  69.     Draws opponentDrawActions;
  70.     Cards opponentDiscardActions;
  71.  
  72.     InfosetHistory(
  73.             Card initialDiscardPile = {},
  74.             Hand hand = {},
  75.             Draws playerDrawActions = {},
  76.             Cards playerDiscardActions = {},
  77.             Draws opponentDrawActions = {},
  78.             Cards opponentDiscardActions = {}
  79.     ) : initialDiscardPile(initialDiscardPile),
  80.         initialHand(std::move(hand)),
  81.         playerDrawActions(std::move(playerDrawActions)),
  82.         playerDiscardActions(std::move(playerDiscardActions)),
  83.         opponentDrawActions(std::move(opponentDrawActions)),
  84.         opponentDiscardActions(std::move(opponentDiscardActions)) {}
  85.  
  86.     template<class Archive>
  87.     void serialize(Archive& ar, const unsigned int /*version*/) {
  88.         ar & initialDiscardPile & initialHand
  89.         & playerDrawActions & playerDiscardActions
  90.         & opponentDrawActions & opponentDiscardActions;
  91.     }
  92.  
  93.     friend size_t hash_value(InfosetHistory const& ish) {
  94.         auto v = hash_value(ish.initialDiscardPile);
  95.  
  96.         auto combine = [&v](auto& range) { boost::hash_range(v, begin(range), end(range)); };
  97.         combine(ish.initialHand);
  98.         combine(ish.playerDrawActions);
  99.         combine(ish.playerDiscardActions);
  100.         combine(ish.opponentDrawActions);
  101.         combine(ish.opponentDiscardActions);
  102.         return v;
  103.     }
  104.  
  105.     bool operator==(InfosetHistory const& rhs) const {
  106.         return std::tie(initialDiscardPile, initialHand, playerDrawActions,
  107.                         playerDiscardActions, opponentDrawActions,
  108.                         opponentDiscardActions)
  109.                == std::tie(rhs.initialDiscardPile, rhs.initialHand,
  110.                            rhs.playerDrawActions, rhs.playerDiscardActions,
  111.                            rhs.opponentDrawActions, rhs.opponentDiscardActions);
  112.     }
  113. };
  114.  
  115. class Node {
  116. public:
  117.     Cards allowedActions;
  118.     unsigned int NUM_ACTIONS{};
  119.     std::vector<double> regretSum;
  120.     std::vector<double> strategySum;
  121.     unsigned char phase{};
  122.  
  123.     explicit Node(std::vector<Card> allowedActions = {},
  124.                   unsigned int NUM_ACTIONS = 0,
  125.                   std::vector<double> regretSum = {},
  126.                   std::vector<double> strategySum = {},
  127.                   unsigned char phase = 0
  128.     ) : allowedActions(std::move(allowedActions)),
  129.         NUM_ACTIONS(NUM_ACTIONS),
  130.         regretSum(std::move(regretSum)),
  131.         strategySum(std::move(strategySum)),
  132.         phase(phase) {}
  133.  
  134.     template<class Archive>
  135.     void serialize(Archive& ar, unsigned /*version*/) {
  136.         ar & allowedActions
  137.         & NUM_ACTIONS
  138.         & regretSum & strategySum & phase;
  139.     }
  140. };
  141.  
  142. #include <map>
  143. #include <fstream>
  144.  
  145. template<typename K, typename V>
  146. using htable = std::unordered_map<K, V, boost::hash<K> >;
  147.  
  148. using NodeMap = htable<std::vector<Card>, htable<InfosetHistory, Node>>;
  149.  
  150. int main() {
  151.     {
  152.         // LINES 147 - 267 ARE FOR READING IN THE DATA FROM THE FILE
  153.         std::ifstream file("StrategyWritten0.txt");
  154.         NodeMap nodeMap1;
  155.         int mapSize;
  156.         file >> mapSize;
  157.         for (int i = 0; i < mapSize; i++) {
  158.             std::vector<Card> hand;
  159.             for (int t = 0; t < 10; t++) {
  160.                 int rank;
  161.                 file >> rank;
  162.                 int suit;
  163.                 file >> suit;
  164.                 hand.emplace_back(rank, suit);
  165.             }
  166.             htable<InfosetHistory, Node> subMap;
  167.             int subMapSize;
  168.             file >> subMapSize;
  169.             for (int j = 0; j < subMapSize; j++) {
  170.                 Card initialDiscardPile;
  171.                 std::vector<Card> initialHand;
  172.                 std::vector<DrawAction> playerDrawActions;
  173.                 std::vector<Card> playerDiscardActions;
  174.                 std::vector<DrawAction> opponentDrawActions;
  175.                 std::vector<Card> opponentDiscardActions;
  176.                 for (int t = 0; t < 10; t++) {
  177.                     int rank;
  178.                     file >> rank;
  179.                     int suit;
  180.                     file >> suit;
  181.                     initialHand.emplace_back(rank, suit);
  182.                 }
  183.                 int r;
  184.                 file >> r;
  185.                 int s;
  186.                 file >> s;
  187.                 initialDiscardPile = Card(r, s);
  188.                 int numPlayerDrawActions;
  189.                 file >> numPlayerDrawActions;
  190.                 for (int t = 0; t < numPlayerDrawActions; t++) {
  191.                     bool fromDeck;
  192.                     file >> fromDeck;
  193.                     int rank;
  194.                     file >> rank;
  195.                     int suit;
  196.                     file >> suit;
  197.                     playerDrawActions.emplace_back(fromDeck, Card(rank, suit));
  198.                 }
  199.                 int numPlayerDiscardActions;
  200.                 file >> numPlayerDiscardActions;
  201.                 for (int t = 0; t < numPlayerDiscardActions; t++) {
  202.                     int rank;
  203.                     file >> rank;
  204.                     int suit;
  205.                     file >> suit;
  206.                     playerDiscardActions.emplace_back(rank, suit);
  207.                 }
  208.                 int numOpponentDrawActions;
  209.                 file >> numOpponentDrawActions;
  210.                 for (int t = 0; t < numOpponentDrawActions; t++) {
  211.                     bool fromDeck;
  212.                     file >> fromDeck;
  213.                     int rank;
  214.                     file >> rank;
  215.                     int suit;
  216.                     file >> suit;
  217.                     opponentDrawActions.emplace_back(fromDeck, Card(rank, suit));
  218.                 }
  219.                 int numOpponentDiscardActions;
  220.                 file >> numOpponentDiscardActions;
  221.                 for (int t = 0; t < numOpponentDiscardActions; t++) {
  222.                     int rank;
  223.                     file >> rank;
  224.                     int suit;
  225.                     file >> suit;
  226.                     opponentDiscardActions.emplace_back(rank, suit);
  227.                 }
  228.                 InfosetHistory history(initialDiscardPile, initialHand, playerDrawActions, playerDiscardActions,
  229.                                        opponentDrawActions, opponentDiscardActions);
  230.  
  231.                 std::vector<Card> allowedActions;
  232.                 unsigned int NUM_ACTIONS;
  233.                 std::vector<double> regretSum;
  234.                 std::vector<double> strategySum;
  235.                 unsigned char phase;
  236.  
  237.                 int numAllowedActions;
  238.                 file >> numAllowedActions;
  239.                 for (int t = 0; t < numAllowedActions; t++) {
  240.                     int rank;
  241.                     file >> rank;
  242.                     int suit;
  243.                     file >> suit;
  244.                     allowedActions.emplace_back(rank, suit);
  245.                 }
  246.                 file >> NUM_ACTIONS;
  247.                 for (int t = 0; t < NUM_ACTIONS; t++) {
  248.                     double regret;
  249.                     file >> regret;
  250.                     regretSum.push_back(regret);
  251.                 }
  252.                 for (int t = 0; t < NUM_ACTIONS; t++) {
  253.                     double sum;
  254.                     file >> sum;
  255.                     strategySum.push_back(sum);
  256.                 }
  257.                 file >> phase;
  258.  
  259.                 Node node(allowedActions, NUM_ACTIONS, regretSum, strategySum, phase);
  260.  
  261.                 subMap.insert(std::make_pair(history, node));
  262.             }
  263.  
  264.             nodeMap1.insert(std::make_pair(hand, subMap));
  265.         }
  266.  
  267.         std::cout << "Successfully created Node Map" << std::endl;
  268.  
  269.         // SERIALIZE NODE MAP
  270.         std::ofstream f("TempStrategy.bin");
  271.         boost::archive::binary_oarchive archive(f);
  272.         archive << nodeMap1;
  273.  
  274.         std::cout << "Serialized Node Map" << std::endl;
  275.     }
  276.  
  277.     {
  278.         // DESERIALIZE NODE MAP
  279.         std::ifstream ifs("TempStrategy.bin");
  280.         NodeMap nodeMap2;
  281.         if (ifs.good()) {
  282.             boost::archive::binary_iarchive ia(ifs);
  283.             ia >> nodeMap2;
  284.         }
  285.  
  286.         std::cout << "Deserialized Node Map" << std::endl;
  287.     }
  288.  
  289.     return 0;
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement