diff options
| author | Aiden Woodruff <woodra@rpi.edu> | 2025-11-18 00:21:21 -0500 |
|---|---|---|
| committer | Aiden Woodruff <woodra@rpi.edu> | 2025-11-18 00:21:21 -0500 |
| commit | e212f42749e91f937db131f6e6477bfcbe7c4b19 (patch) | |
| tree | 3d66b73363bfd72c1de563d8c427181b9480f315 /src | |
| parent | 732e06cec1fa5a9751fa5a83658be78b4049ab45 (diff) | |
| download | tipping-points-e212f42749e91f937db131f6e6477bfcbe7c4b19.tar.gz tipping-points-e212f42749e91f937db131f6e6477bfcbe7c4b19.tar.bz2 tipping-points-e212f42749e91f937db131f6e6477bfcbe7c4b19.zip | |
add namegame parameters to record file
- src/NameGame.h: replace tuple in strategy_record with private struct.
- src/NameGame.cc (runRound): add round number so it can be written to the
strategy record.
- change record update for struct and add round number.
- use push_back and initializer list.
- (run): does not assume record is empty when calculating average.
- call runRound with round number.
- use struct instead of tuple.
- (writeRecord): write NameGame parameters and round number as well.
- src/main.cc: don't clear record and write entire record to file.
Signed-off-by: Aiden Woodruff <woodra@rpi.edu>
Diffstat (limited to 'src')
| -rw-r--r-- | src/NameGame.cc | 25 | ||||
| -rw-r--r-- | src/NameGame.h | 7 | ||||
| -rw-r--r-- | src/main.cc | 2 |
3 files changed, 20 insertions, 14 deletions
diff --git a/src/NameGame.cc b/src/NameGame.cc index 022b951..5dfcbc7 100644 --- a/src/NameGame.cc +++ b/src/NameGame.cc | |||
| @@ -73,18 +73,19 @@ void NameGame::initMemory() { | |||
| 73 | 73 | ||
| 74 | void NameGame::run(int rounds) { | 74 | void NameGame::run(int rounds) { |
| 75 | if (rounds < 0) throw std::invalid_argument("NameGame::run rounds < 0"); | 75 | if (rounds < 0) throw std::invalid_argument("NameGame::run rounds < 0"); |
| 76 | for (int i = 0; i < rounds; ++i) runRound(); | 76 | size_t srlen = strategy_record.size(); |
| 77 | for (int i = 0; i < rounds; ++i) runRound(i); | ||
| 77 | // Track all plays and plays by non-CM. | 78 | // Track all plays and plays by non-CM. |
| 78 | // Report average plays. | 79 | // Report average plays. |
| 79 | float avg = 0; | 80 | float avg = 0; |
| 80 | for (const auto r : strategy_record) { | 81 | for (size_t i = srlen; i < strategy_record.size(); ++i) { |
| 81 | avg += std::get<2>(r) + std::get<3>(r); | 82 | avg += strategy_record[i].s_strategy + strategy_record[i].h_strategy; |
| 82 | } | 83 | } |
| 83 | avg /= strategy_record.size(); | 84 | avg /= strategy_record.size() - srlen; |
| 84 | std::cout << avg << std::endl; | 85 | std::cout << avg << std::endl; |
| 85 | } | 86 | } |
| 86 | 87 | ||
| 87 | void NameGame::runRound() { | 88 | void NameGame::runRound(int r) { |
| 88 | bool verbose_flag = false; | 89 | bool verbose_flag = false; |
| 89 | // Select random speaker and hearer. | 90 | // Select random speaker and hearer. |
| 90 | auto speaker = graph.GetNI(dist(rng)); | 91 | auto speaker = graph.GetNI(dist(rng)); |
| @@ -113,7 +114,9 @@ void NameGame::runRound() { | |||
| 113 | 114 | ||
| 114 | // Speaker updates memory. | 115 | // Speaker updates memory. |
| 115 | update_memory(speaker.GetId(), h_strategy); | 116 | update_memory(speaker.GetId(), h_strategy); |
| 116 | strategy_record.emplace_back(speaker.GetId(), hearer.GetId(), s_strategy, h_strategy); | 117 | strategy_record.push_back({ |
| 118 | r, speaker.GetId(), hearer.GetId(), s_strategy, h_strategy | ||
| 119 | }); | ||
| 117 | } | 120 | } |
| 118 | 121 | ||
| 119 | int NameGame::best_move(int nId) const { | 122 | int NameGame::best_move(int nId) const { |
| @@ -146,12 +149,12 @@ void NameGame::clearRecord() { | |||
| 146 | 149 | ||
| 147 | void NameGame::writeRecord(const char* fname) { | 150 | void NameGame::writeRecord(const char* fname) { |
| 148 | std::ofstream f(fname); | 151 | std::ofstream f(fname); |
| 149 | f << "speaker,hearer,speaker_strategy,hearer_strategy\n"; | 152 | f << "group_size,cm_size,memlen,round," |
| 150 | int speaker, hearer, s_strategy, h_strategy; | 153 | "speaker,hearer,speaker_strategy,hearer_strategy\n"; |
| 151 | for (const auto& r : strategy_record) { | 154 | for (const auto& r : strategy_record) { |
| 152 | std::tie(speaker, hearer, s_strategy, h_strategy) = r; | 155 | f << gsize << ',' << cmsize << ',' << memlen << ',' << r.round << ',' |
| 153 | f << speaker << ',' << hearer << ',' << s_strategy << ',' | 156 | << r.speaker << ',' << r.hearer << ',' << r.s_strategy << ',' |
| 154 | << h_strategy << '\n'; | 157 | << r.h_strategy << '\n'; |
| 155 | } | 158 | } |
| 156 | } | 159 | } |
| 157 | 160 | ||
diff --git a/src/NameGame.h b/src/NameGame.h index 73f5630..abd9d6d 100644 --- a/src/NameGame.h +++ b/src/NameGame.h | |||
| @@ -26,12 +26,15 @@ protected: | |||
| 26 | void update_memory(int nId, int strategy); | 26 | void update_memory(int nId, int strategy); |
| 27 | 27 | ||
| 28 | private: | 28 | private: |
| 29 | void runRound(); | 29 | void runRound(int r); |
| 30 | int gsize, cmsize, memlen; | 30 | int gsize, cmsize, memlen; |
| 31 | std::mt19937 rng; | 31 | std::mt19937 rng; |
| 32 | std::uniform_int_distribution<> dist; | 32 | std::uniform_int_distribution<> dist; |
| 33 | TNodeNet<TPair<TBool, TIntV>> graph; | 33 | TNodeNet<TPair<TBool, TIntV>> graph; |
| 34 | std::vector<std::tuple<int, int, int, int> > strategy_record; | 34 | struct Record { |
| 35 | int round, speaker, hearer, s_strategy, h_strategy; | ||
| 36 | }; | ||
| 37 | std::vector<Record> strategy_record; | ||
| 35 | }; // class NameGame | 38 | }; // class NameGame |
| 36 | 39 | ||
| 37 | } // namespace tp | 40 | } // namespace tp |
diff --git a/src/main.cc b/src/main.cc index 9f47446..2c56584 100644 --- a/src/main.cc +++ b/src/main.cc | |||
| @@ -7,12 +7,12 @@ int main(int argc, char* argv[]) { | |||
| 7 | tp::NameGame namegame(group_size, 0); | 7 | tp::NameGame namegame(group_size, 0); |
| 8 | for (int cmperc = 18; cmperc < 28; cmperc += 2) { | 8 | for (int cmperc = 18; cmperc < 28; cmperc += 2) { |
| 9 | int cmsize = group_size * cmperc / 100; | 9 | int cmsize = group_size * cmperc / 100; |
| 10 | namegame.clearRecord(); | ||
| 11 | namegame.setCMsize(cmsize); | 10 | namegame.setCMsize(cmsize); |
| 12 | namegame.initMemory(); | 11 | namegame.initMemory(); |
| 13 | std::cout << "CM " << cmsize << " / " << group_size << std::endl; | 12 | std::cout << "CM " << cmsize << " / " << group_size << std::endl; |
| 14 | namegame.run(1000); | 13 | namegame.run(1000); |
| 15 | } | 14 | } |
| 15 | namegame.writeRecord("strategy_record.csv"); | ||
| 16 | return 0; | 16 | return 0; |
| 17 | } | 17 | } |
| 18 | 18 | ||
