File size: 14,126 Bytes
01f3434
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <vector>

namespace {

struct Config {
    int faces;
    int dice_per_player;
    int num_dice;
    int num_bids;
    int strategy_cols;
};

struct GameData {
    Config cfg;
    std::vector<std::vector<int>> dice_types;
    std::vector<double> dice_probs;
    std::vector<std::vector<int>> face_counts;
    std::vector<int8_t> msb;
    std::vector<uint8_t> parity;
    std::vector<std::vector<uint32_t>> masks_by_parity;
    std::vector<std::vector<int32_t>> mask_to_index;
    std::vector<double> payoff_call_p0;
    int D = 0;
    uint32_t total_masks = 0;
};

static double factorial_small(int n) {
    double out = 1.0;
    for (int i = 2; i <= n; ++i) out *= static_cast<double>(i);
    return out;
}

static void enumerate_dice(GameData& g, int pos, int min_face, std::vector<int>& cur) {
    if (pos == g.cfg.dice_per_player) {
        g.dice_types.push_back(cur);
        return;
    }
    for (int face = min_face; face <= g.cfg.faces; ++face) {
        cur[pos] = face;
        enumerate_dice(g, pos + 1, face, cur);
    }
}

static GameData make_game_data(int faces) {
    GameData g;
    g.cfg.faces = faces;
    g.cfg.dice_per_player = 2;
    g.cfg.num_dice = 4;
    g.cfg.num_bids = g.cfg.num_dice * faces;
    g.cfg.strategy_cols = std::max(faces * faces, g.cfg.num_bids + 1);
    std::vector<int> cur(g.cfg.dice_per_player, 1);
    enumerate_dice(g, 0, 1, cur);
    g.D = static_cast<int>(g.dice_types.size());
    g.face_counts.assign(g.D, std::vector<int>(faces + 1, 0));
    g.dice_probs.assign(g.D, 0.0);
    const double denom = static_cast<double>(faces * faces);
    for (int d = 0; d < g.D; ++d) {
        double multiplicity = factorial_small(g.cfg.dice_per_player);
        for (int face : g.dice_types[d]) g.face_counts[d][face] += 1;
        for (int face = 1; face <= faces; ++face) {
            multiplicity /= factorial_small(g.face_counts[d][face]);
        }
        g.dice_probs[d] = multiplicity / denom;
    }

    g.total_masks = uint32_t(1) << g.cfg.num_bids;
    g.msb.assign(g.total_masks, -1);
    g.parity.assign(g.total_masks, 0);
    for (uint32_t mask = 1; mask < g.total_masks; ++mask) {
        int bit = 31 - __builtin_clz(mask);
        g.msb[mask] = static_cast<int8_t>(bit);
        g.parity[mask] = static_cast<uint8_t>(__builtin_popcount(mask) & 1);
    }
    g.masks_by_parity.assign(2, {});
    g.mask_to_index.assign(2, std::vector<int32_t>(g.total_masks, -1));
    for (uint32_t mask = 0; mask < g.total_masks; ++mask) {
        int p = g.parity[mask];
        int32_t idx = static_cast<int32_t>(g.masks_by_parity[p].size());
        g.masks_by_parity[p].push_back(mask);
        g.mask_to_index[p][mask] = idx;
    }

    g.payoff_call_p0.assign(static_cast<size_t>(g.cfg.num_bids) * g.D * g.D, 0.0);
    const int wild_face = faces;
    for (int bid = 0; bid < g.cfg.num_bids; ++bid) {
        const int quantity = bid / faces + 1;
        const int face = bid % faces + 1;
        const bool bid_is_wild = face == wild_face;
        for (int d0 = 0; d0 < g.D; ++d0) {
            for (int d1 = 0; d1 < g.D; ++d1) {
                int matches = g.face_counts[d0][face] + g.face_counts[d1][face];
                if (!bid_is_wild) matches += g.face_counts[d0][wild_face] + g.face_counts[d1][wild_face];
                const bool bid_true = matches >= quantity;
                const double payoff = bid_true ? -1.0 : 1.0;
                g.payoff_call_p0[(static_cast<size_t>(bid) * g.D + d0) * g.D + d1] = payoff;
            }
        }
    }
    return g;
}

static inline double normalized_prob(
    const double* strat,
    int64_t row,
    int cols,
    int legal_actions,
    int action_index
) {
    if (strat == nullptr) return 1.0 / static_cast<double>(legal_actions);
    const double* base = strat + row * static_cast<int64_t>(cols);
    double sum = 0.0;
    for (int i = 0; i < legal_actions; ++i) {
        if (base[i] > 0.0) sum += base[i];
    }
    if (sum <= 0.0) return 1.0 / static_cast<double>(legal_actions);
    const double v = base[action_index];
    return v > 0.0 ? v / sum : 0.0;
}

static inline int64_t infoset_row(const GameData& g, int player, int dice, uint32_t mask) {
    const int32_t mi = g.mask_to_index[player][mask];
    return static_cast<int64_t>(dice) * static_cast<int64_t>(g.masks_by_parity[player].size()) + mi;
}

static double expected_value(const GameData& g, const double* strat0, const double* strat1, int cols) {
    const int D = g.D;
    const size_t width = static_cast<size_t>(D);
    const size_t total_width = static_cast<size_t>(g.total_masks) * width;
    std::vector<double> reach0(total_width, 0.0), reach1(total_width, 0.0);
    for (int d = 0; d < D; ++d) {
        reach0[d] = 1.0;
        reach1[d] = 1.0;
    }
    auto off = [D](uint32_t mask, int d) -> size_t { return static_cast<size_t>(mask) * D + d; };

    for (uint32_t mask = 1; mask < g.total_masks; ++mask) {
        const int parity = g.parity[mask];
        const int last = g.msb[mask];
        const uint32_t prev = mask ^ (uint32_t(1) << last);
        const int prev_last = g.msb[prev];
        const int local_idx = last - (prev_last + 1);
        const int legal_bids = g.cfg.num_bids - (prev_last + 1);
        const bool call_allowed = prev != 0;
        const int legal_actions = legal_bids + (call_allowed ? 1 : 0);
        if (parity == 1) {
            for (int d = 0; d < D; ++d) {
                const double p = normalized_prob(strat0, infoset_row(g, 0, d, prev), cols, legal_actions, local_idx);
                reach0[off(mask, d)] = reach0[off(prev, d)] * p;
                reach1[off(mask, d)] = reach1[off(prev, d)];
            }
        } else {
            for (int d = 0; d < D; ++d) {
                reach0[off(mask, d)] = reach0[off(prev, d)];
                const double p = normalized_prob(strat1, infoset_row(g, 1, d, prev), cols, legal_actions, local_idx);
                reach1[off(mask, d)] = reach1[off(prev, d)] * p;
            }
        }
    }

    std::vector<double> weight_br(D, 0.0), weight_opp(D, 0.0);
    double ev0 = 0.0;
    for (uint32_t mask = 1; mask < g.total_masks; ++mask) {
        const int actor = g.parity[mask];
        const int last_bid = g.msb[mask];
        const int legal_bids = g.cfg.num_bids - (last_bid + 1);
        const int call_idx = legal_bids;
        const int legal_actions = legal_bids + 1;
        const size_t payoff_base = static_cast<size_t>(last_bid) * D * D;
        if (actor == 0) {
            for (int d0 = 0; d0 < D; ++d0) {
                const double cp = normalized_prob(strat0, infoset_row(g, 0, d0, mask), cols, legal_actions, call_idx);
                weight_br[d0] = g.dice_probs[d0] * reach0[off(mask, d0)] * cp;
            }
            for (int d1 = 0; d1 < D; ++d1) weight_opp[d1] = g.dice_probs[d1] * reach1[off(mask, d1)];
            for (int d0 = 0; d0 < D; ++d0) {
                if (weight_br[d0] == 0.0) continue;
                const size_t row_off = payoff_base + static_cast<size_t>(d0) * D;
                for (int d1 = 0; d1 < D; ++d1) {
                    ev0 += weight_br[d0] * weight_opp[d1] * g.payoff_call_p0[row_off + d1];
                }
            }
        } else {
            for (int d0 = 0; d0 < D; ++d0) weight_opp[d0] = g.dice_probs[d0] * reach0[off(mask, d0)];
            for (int d1 = 0; d1 < D; ++d1) {
                const double cp = normalized_prob(strat1, infoset_row(g, 1, d1, mask), cols, legal_actions, call_idx);
                weight_br[d1] = g.dice_probs[d1] * reach1[off(mask, d1)] * cp;
            }
            for (int d0 = 0; d0 < D; ++d0) {
                if (weight_opp[d0] == 0.0) continue;
                const size_t row_off = payoff_base + static_cast<size_t>(d0) * D;
                for (int d1 = 0; d1 < D; ++d1) {
                    ev0 -= weight_opp[d0] * weight_br[d1] * g.payoff_call_p0[row_off + d1];
                }
            }
        }
    }
    return ev0;
}

static double best_response_value(
    const GameData& g,
    int br_player,
    const double* strat0,
    const double* strat1,
    int cols,
    std::vector<double>& reach_weight,
    std::vector<double>& f
) {
    const int D = g.D;
    const int opp = 1 - br_player;
    const double* opp_strat = opp == 0 ? strat0 : strat1;
    std::fill(reach_weight.begin(), reach_weight.end(), 0.0);
    std::fill(f.begin(), f.end(), 0.0);
    for (int d = 0; d < D; ++d) reach_weight[d] = g.dice_probs[d];
    auto off = [D](uint32_t mask, int d) -> size_t { return static_cast<size_t>(mask) * D + d; };

    for (uint32_t mask = 1; mask < g.total_masks; ++mask) {
        const int parity = g.parity[mask];
        const int last = g.msb[mask];
        const uint32_t prev = mask ^ (uint32_t(1) << last);
        const int last_actor = parity ^ 1;
        if (last_actor != opp) {
            for (int d = 0; d < D; ++d) reach_weight[off(mask, d)] = reach_weight[off(prev, d)];
            continue;
        }
        const int prev_last = g.msb[prev];
        const int local_idx = last - (prev_last + 1);
        const int legal_bids = g.cfg.num_bids - (prev_last + 1);
        const bool call_allowed = prev != 0;
        const int legal_actions = legal_bids + (call_allowed ? 1 : 0);
        for (int d = 0; d < D; ++d) {
            const double p = normalized_prob(opp_strat, infoset_row(g, opp, d, prev), cols, legal_actions, local_idx);
            reach_weight[off(mask, d)] = reach_weight[off(prev, d)] * p;
        }
    }

    std::vector<double> accum(D, 0.0), call_weight(D, 0.0);
    auto payoff_to_br = [&](int caller, int bid, int br_d, int opp_d) -> double {
        const int d0 = br_player == 0 ? br_d : opp_d;
        const int d1 = br_player == 0 ? opp_d : br_d;
        const double caller_payoff = g.payoff_call_p0[(static_cast<size_t>(bid) * D + d0) * D + d1];
        return caller == br_player ? caller_payoff : -caller_payoff;
    };

    for (uint32_t mask = g.total_masks - 1;; --mask) {
        const int actor = g.parity[mask];
        const int last_bid = g.msb[mask];
        const int start_rank = last_bid + 1;
        const bool call_allowed = mask != 0;
        const int legal_bids = g.cfg.num_bids - (last_bid + 1);
        const int call_idx = legal_bids;
        const int legal_actions = call_allowed ? legal_bids + 1 : legal_bids;
        const size_t base = static_cast<size_t>(mask) * D;

        if (actor == opp) {
            std::fill(accum.begin(), accum.end(), 0.0);
            for (int r = start_rank; r < g.cfg.num_bids; ++r) {
                const uint32_t child = mask | (uint32_t(1) << r);
                const size_t child_base = static_cast<size_t>(child) * D;
                for (int d = 0; d < D; ++d) accum[d] += f[child_base + d];
            }
            if (call_allowed) {
                const size_t w_base = static_cast<size_t>(mask) * D;
                for (int d = 0; d < D; ++d) {
                    const double p_call = normalized_prob(opp_strat, infoset_row(g, opp, d, mask), cols, legal_actions, call_idx);
                    call_weight[d] = reach_weight[w_base + d] * p_call;
                }
                for (int br_d = 0; br_d < D; ++br_d) {
                    double term = 0.0;
                    for (int opp_d = 0; opp_d < D; ++opp_d) {
                        term += call_weight[opp_d] * payoff_to_br(opp, last_bid, br_d, opp_d);
                    }
                    accum[br_d] += term;
                }
            }
            for (int d = 0; d < D; ++d) f[base + d] = accum[d];
        } else {
            std::fill(accum.begin(), accum.end(), -INFINITY);
            for (int r = start_rank; r < g.cfg.num_bids; ++r) {
                const uint32_t child = mask | (uint32_t(1) << r);
                const size_t child_base = static_cast<size_t>(child) * D;
                for (int br_d = 0; br_d < D; ++br_d) accum[br_d] = std::max(accum[br_d], f[child_base + br_d]);
            }
            for (int br_d = 0; br_d < D; ++br_d) {
                double best = accum[br_d];
                if (call_allowed) {
                    const size_t w_base = static_cast<size_t>(mask) * D;
                    double call_value = 0.0;
                    for (int opp_d = 0; opp_d < D; ++opp_d) {
                        call_value += reach_weight[w_base + opp_d] * payoff_to_br(br_player, last_bid, br_d, opp_d);
                    }
                    best = std::max(best, call_value);
                }
                f[base + br_d] = best;
            }
        }
        if (mask == 0) break;
    }

    double value = 0.0;
    for (int br_d = 0; br_d < D; ++br_d) value += g.dice_probs[br_d] * f[br_d];
    return value;
}

}  // namespace

extern "C" int liarsdice_exact_evaluate(
    int faces,
    int cols,
    int64_t rows0,
    int64_t rows1,
    const double* strat0,
    const double* strat1,
    double* out
) {
    if (faces < 2 || faces > 5 || out == nullptr) return 2;
    GameData g = make_game_data(faces);
    if (cols != g.cfg.strategy_cols) return 3;
    const int64_t expected_rows = static_cast<int64_t>(g.D) * static_cast<int64_t>(g.masks_by_parity[0].size());
    if ((strat0 != nullptr && rows0 != expected_rows) || (strat1 != nullptr && rows1 != expected_rows)) return 4;
    const double ev0 = expected_value(g, strat0, strat1, cols);
    const size_t workspace_size = static_cast<size_t>(g.total_masks) * g.D;
    std::vector<double> reach_weight(workspace_size), f(workspace_size);
    const double br0 = best_response_value(g, 0, strat0, strat1, cols, reach_weight, f);
    const double br1 = best_response_value(g, 1, strat0, strat1, cols, reach_weight, f);
    out[0] = ev0;
    out[1] = br0 - ev0;
    out[2] = br1 + ev0;
    return 0;
}

extern "C" int liarsdice_exact_shape(int faces, int64_t* rows, int* cols) {
    if (faces < 2 || faces > 5 || rows == nullptr || cols == nullptr) return 2;
    GameData g = make_game_data(faces);
    *rows = static_cast<int64_t>(g.D) * static_cast<int64_t>(g.masks_by_parity[0].size());
    *cols = g.cfg.strategy_cols;
    return 0;
}