diff options
| author | Aiden Woodruff <woodra@rpi.edu> | 2025-11-18 16:53:01 -0500 |
|---|---|---|
| committer | Aiden Woodruff <woodra@rpi.edu> | 2025-11-18 16:53:01 -0500 |
| commit | 9008a72760c8f04ef559c72ee2d7ef9d9b61a608 (patch) | |
| tree | 021f93d7b819d6673054ef53b0f361cea77deac6 /src | |
| parent | 220aa3dc4c8d5fa82740943125da415dac5788a3 (diff) | |
| download | tipping-points-9008a72760c8f04ef559c72ee2d7ef9d9b61a608.tar.gz tipping-points-9008a72760c8f04ef559c72ee2d7ef9d9b61a608.tar.bz2 tipping-points-9008a72760c8f04ef559c72ee2d7ef9d9b61a608.zip | |
allow appending to CSV file and use biunit answer
- src/NameGame.h: add append option to writeRecord to allow updating one
big CSV file.
- add speaker and hearer type (i.e. committed) to CSV file.
- src/NameGame.cc (initMemory): change answers to biunit i.e. -1 and 1
for easier analysis.
- (runRound): write speaker/hearer type.
- (writeRecord): add append mode to allow updating a single CSV file.
- had to change to fstream with in/out because using an ofstream always
overwrote the file.
- only write the header line when at the beginning of the file.
- add speaker/hearer type.
- src/main.cc: clear record again in each loop and go into append mode
for every iteration after the first one.
Signed-off-by: Aiden Woodruff <woodra@rpi.edu>
Diffstat (limited to 'src')
| -rw-r--r-- | src/NameGame.cc | 24 | ||||
| -rw-r--r-- | src/NameGame.h | 4 | ||||
| -rw-r--r-- | src/main.cc | 6 |
3 files changed, 21 insertions, 13 deletions
diff --git a/src/NameGame.cc b/src/NameGame.cc index 5dfcbc7..2dd64c0 100644 --- a/src/NameGame.cc +++ b/src/NameGame.cc | |||
| @@ -59,7 +59,7 @@ void NameGame::initMemory() { | |||
| 59 | auto& rDat = graph.GetNDat(rId); | 59 | auto& rDat = graph.GetNDat(rId); |
| 60 | rDat.Val1 = true; | 60 | rDat.Val1 = true; |
| 61 | rDat.Val2.Clr(); | 61 | rDat.Val2.Clr(); |
| 62 | rDat.Val2.Add(2); | 62 | rDat.Val2.Add(1); |
| 63 | ids.erase(ids.begin() + r); | 63 | ids.erase(ids.begin() + r); |
| 64 | } | 64 | } |
| 65 | // Establish norm (fully memory of strategy 1) for remaining nodes. | 65 | // Establish norm (fully memory of strategy 1) for remaining nodes. |
| @@ -67,7 +67,7 @@ void NameGame::initMemory() { | |||
| 67 | auto& d = graph.GetNDat(id); | 67 | auto& d = graph.GetNDat(id); |
| 68 | d.Val1 = false; | 68 | d.Val1 = false; |
| 69 | d.Val2.Clr(); | 69 | d.Val2.Clr(); |
| 70 | while (d.Val2.Len() < memlen) d.Val2.Add(1); | 70 | while (d.Val2.Len() < memlen) d.Val2.Add(-1); |
| 71 | } | 71 | } |
| 72 | } | 72 | } |
| 73 | 73 | ||
| @@ -115,7 +115,8 @@ void NameGame::runRound(int r) { | |||
| 115 | // Speaker updates memory. | 115 | // Speaker updates memory. |
| 116 | update_memory(speaker.GetId(), h_strategy); | 116 | update_memory(speaker.GetId(), h_strategy); |
| 117 | strategy_record.push_back({ | 117 | strategy_record.push_back({ |
| 118 | r, speaker.GetId(), hearer.GetId(), s_strategy, h_strategy | 118 | r, speaker.GetId(), speaker().Val1, |
| 119 | hearer.GetId(), hearer().Val1, s_strategy, h_strategy | ||
| 119 | }); | 120 | }); |
| 120 | } | 121 | } |
| 121 | 122 | ||
| @@ -147,14 +148,19 @@ void NameGame::clearRecord() { | |||
| 147 | strategy_record.clear(); | 148 | strategy_record.clear(); |
| 148 | } | 149 | } |
| 149 | 150 | ||
| 150 | void NameGame::writeRecord(const char* fname) { | 151 | void NameGame::writeRecord(const char* fname, bool append) { |
| 151 | std::ofstream f(fname); | 152 | auto mode = std::ios::in | std::ios::out; |
| 152 | f << "group_size,cm_size,memlen,round," | 153 | if (append) mode |= std::ios::ate; |
| 153 | "speaker,hearer,speaker_strategy,hearer_strategy\n"; | 154 | else mode |= std::ios::trunc; |
| 155 | std::fstream f(fname, mode); | ||
| 156 | if (!append || f.tellp() == 0) { | ||
| 157 | f << "group_size,cm_size,memlen,round,speaker,speaker_type," | ||
| 158 | "hearer,hearer_type,speaker_strategy,hearer_strategy\n"; | ||
| 159 | } | ||
| 154 | for (const auto& r : strategy_record) { | 160 | for (const auto& r : strategy_record) { |
| 155 | f << gsize << ',' << cmsize << ',' << memlen << ',' << r.round << ',' | 161 | f << gsize << ',' << cmsize << ',' << memlen << ',' << r.round << ',' |
| 156 | << r.speaker << ',' << r.hearer << ',' << r.s_strategy << ',' | 162 | << r.speaker << ',' << r.s_cm << ',' << r.hearer << ',' << r.h_cm << ',' |
| 157 | << r.h_strategy << '\n'; | 163 | << r.s_strategy << ',' << r.h_strategy << '\n'; |
| 158 | } | 164 | } |
| 159 | } | 165 | } |
| 160 | 166 | ||
diff --git a/src/NameGame.h b/src/NameGame.h index abd9d6d..eae45f3 100644 --- a/src/NameGame.h +++ b/src/NameGame.h | |||
| @@ -19,7 +19,7 @@ public: | |||
| 19 | void initMemory(); | 19 | void initMemory(); |
| 20 | void clearRecord(); | 20 | void clearRecord(); |
| 21 | void run(int rounds); | 21 | void run(int rounds); |
| 22 | void writeRecord(const char* fname); | 22 | void writeRecord(const char* fname, bool append = false); |
| 23 | 23 | ||
| 24 | protected: | 24 | protected: |
| 25 | int best_move(int nId) const; | 25 | int best_move(int nId) const; |
| @@ -32,7 +32,7 @@ private: | |||
| 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 | struct Record { | 34 | struct Record { |
| 35 | int round, speaker, hearer, s_strategy, h_strategy; | 35 | int round, speaker, s_cm, hearer, h_cm, s_strategy, h_strategy; |
| 36 | }; | 36 | }; |
| 37 | std::vector<Record> strategy_record; | 37 | std::vector<Record> strategy_record; |
| 38 | }; // class NameGame | 38 | }; // class NameGame |
diff --git a/src/main.cc b/src/main.cc index 2c56584..7a5f2bc 100644 --- a/src/main.cc +++ b/src/main.cc | |||
| @@ -5,14 +5,16 @@ | |||
| 5 | int main(int argc, char* argv[]) { | 5 | int main(int argc, char* argv[]) { |
| 6 | int group_size = 100; | 6 | int group_size = 100; |
| 7 | tp::NameGame namegame(group_size, 0); | 7 | tp::NameGame namegame(group_size, 0); |
| 8 | for (int cmperc = 18; cmperc < 28; cmperc += 2) { | 8 | int cmperc_start = 18; |
| 9 | for (int cmperc = cmperc_start; cmperc < 28; cmperc += 2) { | ||
| 9 | int cmsize = group_size * cmperc / 100; | 10 | int cmsize = group_size * cmperc / 100; |
| 11 | namegame.clearRecord(); | ||
| 10 | namegame.setCMsize(cmsize); | 12 | namegame.setCMsize(cmsize); |
| 11 | namegame.initMemory(); | 13 | namegame.initMemory(); |
| 12 | std::cout << "CM " << cmsize << " / " << group_size << std::endl; | 14 | std::cout << "CM " << cmsize << " / " << group_size << std::endl; |
| 13 | namegame.run(1000); | 15 | namegame.run(1000); |
| 16 | namegame.writeRecord("strategy_record.csv", cmperc != cmperc_start); | ||
| 14 | } | 17 | } |
| 15 | namegame.writeRecord("strategy_record.csv"); | ||
| 16 | return 0; | 18 | return 0; |
| 17 | } | 19 | } |
| 18 | 20 | ||
