From f7d2e7c8a70e4d96c5cbc4e8774599f351e59216 Mon Sep 17 00:00:00 2001 From: Aiden Woodruff Date: Sun, 16 Nov 2025 23:38:16 -0500 Subject: add strategy_record - src/NameGame.h: add strategy record as vector of tuples. - src/NameGame.cc (runRound): remove each round printout but update the strategy record. - fix speaker memory update. - (clearRecord): add function. - (writeRecord): add function to output CSV file with record info. - (NameGame::run): print average strategy. - src/main.cc: clear memory and run 1000 rounds. - TASKS.md: update tasks Signed-off-by: Aiden Woodruff --- src/NameGame.cc | 42 +++++++++++++++++++++++++++++++++++------- src/NameGame.h | 5 +++++ src/main.cc | 3 ++- 3 files changed, 42 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/NameGame.cc b/src/NameGame.cc index 8ea3dcf..022b951 100644 --- a/src/NameGame.cc +++ b/src/NameGame.cc @@ -1,3 +1,4 @@ +#include #include #include #include @@ -75,9 +76,16 @@ void NameGame::run(int rounds) { for (int i = 0; i < rounds; ++i) runRound(); // Track all plays and plays by non-CM. // Report average plays. + float avg = 0; + for (const auto r : strategy_record) { + avg += std::get<2>(r) + std::get<3>(r); + } + avg /= strategy_record.size(); + std::cout << avg << std::endl; } void NameGame::runRound() { + bool verbose_flag = false; // Select random speaker and hearer. auto speaker = graph.GetNI(dist(rng)); auto hearer = graph.GetNI( @@ -86,21 +94,26 @@ void NameGame::runRound() { // Speaker chooses best strategy. int s_strategy = best_move(speaker.GetId()); - std::cout << "speaker (" << speaker.GetId() << " " - << std::boolalpha << speaker().Val1 - << ") chose: " << s_strategy << std::endl; + if (verbose_flag) { + std::cout << "speaker (" << speaker.GetId() << " " + << std::boolalpha << speaker().Val1 + << ") chose: " << s_strategy << std::endl; + } // Listener updates memory. update_memory(hearer.GetId(), s_strategy); // Listener chooses best strategy. int h_strategy = best_move(hearer.GetId()); - std::cout << "hearer (" << hearer.GetId() << " " - << std::boolalpha << hearer().Val1 - << ") chose: " << h_strategy << std::endl; + if (verbose_flag) { + std::cout << "hearer (" << hearer.GetId() << " " + << std::boolalpha << hearer().Val1 + << ") chose: " << h_strategy << std::endl; + } // Speaker updates memory. - update_memory(speaker.GetId(), s_strategy); + update_memory(speaker.GetId(), h_strategy); + strategy_record.emplace_back(speaker.GetId(), hearer.GetId(), s_strategy, h_strategy); } int NameGame::best_move(int nId) const { @@ -127,4 +140,19 @@ void NameGame::update_memory(int nId, int strategy) { } } +void NameGame::clearRecord() { + strategy_record.clear(); +} + +void NameGame::writeRecord(const char* fname) { + std::ofstream f(fname); + f << "speaker,hearer,speaker_strategy,hearer_strategy\n"; + int speaker, hearer, s_strategy, h_strategy; + for (const auto& r : strategy_record) { + std::tie(speaker, hearer, s_strategy, h_strategy) = r; + f << speaker << ',' << hearer << ',' << s_strategy << ',' + << h_strategy << '\n'; + } +} + } // namespace tp diff --git a/src/NameGame.h b/src/NameGame.h index 8a817ce..73f5630 100644 --- a/src/NameGame.h +++ b/src/NameGame.h @@ -2,6 +2,8 @@ #define TIPPING_POINTS_NAMEGAME_H #include +#include +#include #include @@ -15,7 +17,9 @@ public: void initGraph(); void initMemory(); + void clearRecord(); void run(int rounds); + void writeRecord(const char* fname); protected: int best_move(int nId) const; @@ -27,6 +31,7 @@ private: std::mt19937 rng; std::uniform_int_distribution<> dist; TNodeNet> graph; + std::vector > strategy_record; }; // class NameGame } // namespace tp diff --git a/src/main.cc b/src/main.cc index e41f4ff..9f47446 100644 --- a/src/main.cc +++ b/src/main.cc @@ -7,10 +7,11 @@ int main(int argc, char* argv[]) { tp::NameGame namegame(group_size, 0); for (int cmperc = 18; cmperc < 28; cmperc += 2) { int cmsize = group_size * cmperc / 100; + namegame.clearRecord(); namegame.setCMsize(cmsize); namegame.initMemory(); std::cout << "CM " << cmsize << " / " << group_size << std::endl; - namegame.run(10); + namegame.run(1000); } return 0; } -- cgit