diff options
| author | Aiden Woodruff <woodra@rpi.edu> | 2025-11-22 14:00:03 -0500 |
|---|---|---|
| committer | Aiden Woodruff <woodra@rpi.edu> | 2025-11-22 14:00:03 -0500 |
| commit | bdb551d54bc9c2eceff7db96518cfd6e1706bf5b (patch) | |
| tree | b420e0c27893a6c1012bfa72c62d0c3e7e57ea66 /src | |
| parent | 3689e9466a1938dee21af93b8e02da2ed9067093 (diff) | |
| download | tipping-points-bdb551d54bc9c2eceff7db96518cfd6e1706bf5b.tar.gz tipping-points-bdb551d54bc9c2eceff7db96518cfd6e1706bf5b.tar.bz2 tipping-points-bdb551d54bc9c2eceff7db96518cfd6e1706bf5b.zip | |
add macro fenced exponential memory decay
- src/NameGame.cc: add static weight function that memoizes exp for
different weight lengths.
Signed-off-by: Aiden Woodruff <woodra@rpi.edu>
Diffstat (limited to 'src')
| -rw-r--r-- | src/NameGame.cc | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/src/NameGame.cc b/src/NameGame.cc index e427542..1454862 100644 --- a/src/NameGame.cc +++ b/src/NameGame.cc | |||
| @@ -1,3 +1,4 @@ | |||
| 1 | #include <cmath> | ||
| 1 | #include <fstream> | 2 | #include <fstream> |
| 2 | #include <iomanip> | 3 | #include <iomanip> |
| 3 | #include <iostream> | 4 | #include <iostream> |
| @@ -119,12 +120,33 @@ void NameGame::runRound(int r) { | |||
| 119 | strategy_record.push_back({r, speaker.GetId(), speaker().Val1, strategy}); | 120 | strategy_record.push_back({r, speaker.GetId(), speaker().Val1, strategy}); |
| 120 | } | 121 | } |
| 121 | 122 | ||
| 123 | #undef USE_EXP_WEIGHT | ||
| 124 | #ifdef USE_EXP_WEIGHT | ||
| 125 | float weight(int i) { | ||
| 126 | static std::vector<float> weight_table; | ||
| 127 | if (i < 0) throw std::invalid_argument("bad weight input"); | ||
| 128 | while (i < weight_table.size()) { | ||
| 129 | float x = weight_table.size(); | ||
| 130 | weight_table.push_back(std::exp(-x)); | ||
| 131 | } | ||
| 132 | return weight_table[i]; | ||
| 133 | } | ||
| 134 | #endif | ||
| 135 | |||
| 122 | int NameGame::best_move(int nId) const { | 136 | int NameGame::best_move(int nId) const { |
| 123 | const auto& mem = graph.GetNDat(nId).Val2; | 137 | const auto& mem = graph.GetNDat(nId).Val2; |
| 138 | #ifdef USE_EXP_WEIGHT | ||
| 139 | std::map<int, float> votes; | ||
| 140 | for (int i = 0; i < mem.Len(); ++i) { | ||
| 141 | int strategy = mem[i]; | ||
| 142 | if (votes.count(strategy)) votes[strategy] += weight(i); | ||
| 143 | else votes[strategy] = weight(i); | ||
| 144 | #else | ||
| 124 | std::map<int, int> votes; | 145 | std::map<int, int> votes; |
| 125 | for (const auto& v : mem) { | 146 | for (const auto& strategy : mem) { |
| 126 | if (votes.count(v)) votes[v] += 1; | 147 | if (votes.count(strategy)) votes[strategy] += 1; |
| 127 | else votes[v] = 1; | 148 | else votes[strategy] = 1; |
| 149 | #endif | ||
| 128 | } | 150 | } |
| 129 | if (votes.empty()) return 0; | 151 | if (votes.empty()) return 0; |
| 130 | int best = std::max_element(votes.begin(), votes.end(), | 152 | int best = std::max_element(votes.begin(), votes.end(), |
