okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221rc1 merge split (1ba231217)fe57a0cb5

main
7.7 KiB192 linesraw
1// The batched drafts' row plan (src/batch_plan.hpp) on the CPU: it stays within the rows and each lane's offer, it finds
2// the most tokens per ms a brute force finds over every split (random offers of 1..6 lanes), and for one lane it picks
3// the chain length Session::generate's chain_len picks alone.
4//
5//   batch_plan_test   (make bin/batch_plan_test; no card, no model)
6#include <cmath>
7#include <cstdio>
8#include <functional>
9#include <random>
10#include <stdexcept>
11#include <vector>
12
13#include "../src/batch_plan.hpp"
14
15namespace {
16constexpr int ROWS = 16;   // MAXT
17
18double rate(const std::vector<fnx::RowOffer>& L, const std::vector<int>& d, double base, const fnx::RowCost& c) {
19    double tok = (double) L.size(), ms = base;
20    int rows = (int) L.size();
21    for (size_t i = 0; i < L.size(); ++i)
22        for (int j = 0; j < d[i]; ++j) {
23            tok += L[i].reach[(size_t) j];
24            ms += c.row(++rows) + (L[i].chain && j >= 1 ? c.chain_ms : 0.0);
25        }
26    return tok / ms;
27}
28
29double best_rate(const std::vector<fnx::RowOffer>& L, double base, const fnx::RowCost& c) {
30    std::vector<int> d(L.size(), 0);
31    double best = 0.0;
32    std::function<void(size_t, int)> go = [&](size_t i, int left) {
33        if (i == L.size()) {
34            best = std::max(best, rate(L, d, base, c));
35            return;
36        }
37        for (int k = 0; k <= std::min(L[i].avail, left); ++k) {
38            d[i] = k;
39            go(i + 1, left - k);
40        }
41        d[i] = 0;
42    };
43    go(0, ROWS - (int) L.size());
44    return best;
45}
46}  // namespace
47
48int main() {
49    std::mt19937_64 rng(12345);
50    std::uniform_real_distribution<double> u(0.0, 1.0);
51    const fnx::RowCost c;
52    int fails = 0, cases = 0;
53    for (int it = 0; it < 4000; ++it) {
54        const int n = 1 + (int) (rng() % 6);
55        std::vector<fnx::RowOffer> L((size_t) n);
56        for (auto& o : L) {
57            o.chain = rng() % 2;
58            o.avail = (int) (rng() % (o.chain ? 7 : 16));
59            const double p = u(rng);   // per-position acceptance, non-increasing reach
60            double r = 1.0;
61            for (int j = 0; j < o.avail; ++j) o.reach.push_back(r *= p * (0.8 + 0.2 * u(rng)));
62        }
63        const double base = c.round_ms + c.rows_ms(n) + c.call_ms * n;
64        const std::vector<int> d = fnx::plan_rows(L, ROWS, base, c);
65        int rows = n;
66        for (int i = 0; i < n; ++i) {
67            rows += d[(size_t) i];
68            if (d[(size_t) i] < 0 || d[(size_t) i] > L[(size_t) i].avail) {
69                std::printf("case %d: lane %d gets %d drafts of %d offered\n", it, i, d[(size_t) i], L[(size_t) i].avail);
70                ++fails;
71            }
72        }
73        if (rows > ROWS) {
74            std::printf("case %d: %d rows\n", it, rows);
75            ++fails;
76        }
77        const double got = rate(L, d, base, c), best = best_rate(L, base, c);
78        if (got < best * (1.0 - 1e-9)) {
79            std::printf("case %d (%d lanes): %.6f tokens/ms, the best split %.6f\n", it, n, got, best);
80            ++fails;
81        }
82        ++cases;
83    }
84    // one lane, a chain: chain_len's choice (k drafts, at least 1) where the first draft pays at all
85    for (int it = 0; it < 2000; ++it) {
86        fnx::RowOffer o;
87        o.chain = true;
88        o.avail = 1 + (int) (rng() % 6);
89        double r = 1.0;
90        const double p = 0.2 + 0.8 * u(rng);
91        for (int j = 0; j < o.avail; ++j) o.reach.push_back(r *= p * (0.8 + 0.2 * u(rng)));
92        int best = 1;
93        double best_rate = -1.0, expect = 1.0;
94        for (int k = 1; k <= o.avail; ++k) {
95            expect += o.reach[(size_t) k - 1];
96            const double rt = expect / (c.round_ms + c.rows_ms(k + 1) + std::max(0, k - 1) * c.chain_ms);
97            if (rt > best_rate) best_rate = rt, best = k;
98        }
99        const int d = fnx::plan_rows({o}, ROWS, c.round_ms, c)[0];
100        if (d != best) {
101            std::printf("one lane: plan %d drafts, chain_len %d (avail %d, reach0 %.3f)\n", d, best, o.avail, o.reach[0]);
102            ++fails;
103        }
104        ++cases;
105    }
106    // choose_rows: within the offers and rows; the best of plan_rows' plan, the uniform plans (each charged the capture
107    // unless held) and the held layouts that fit; an explorer's draft never adds a capture
108    for (int it = 0; it < 4000; ++it) {
109        const int n = 1 + (int) (rng() % 6);
110        std::vector<fnx::RowOffer> L((size_t) n);
111        for (auto& o : L) {
112            o.chain = rng() % 2;
113            o.avail = (int) (rng() % (o.chain ? 7 : 16));
114            o.explore = rng() % 4 == 0;
115            const double p = u(rng);
116            double r = 1.0;
117            for (int j = 0; j < o.avail; ++j) o.reach.push_back(r *= p * (0.8 + 0.2 * u(rng)));
118        }
119        const double base = c.round_ms + c.rows_ms(n) + c.call_ms * n, cap = 10.0 * u(rng);
120        std::vector<std::vector<int>> held;
121        for (int h = (int) (rng() % 4); h > 0; --h) {
122            std::vector<int> counts((size_t) n);
123            for (int& k : counts) k = 1 + (int) (rng() % 5);
124            held.push_back(counts);
125        }
126        auto fits = [&](const std::vector<int>& d) {
127            int rows = n;
128            for (int i = 0; i < n; ++i) {
129                if (d[(size_t) i] < 0 || d[(size_t) i] > L[(size_t) i].avail) return false;
130                rows += d[(size_t) i];
131            }
132            return rows <= ROWS;
133        };
134        auto held_d = [&](const std::vector<int>& d) {
135            for (const auto& h : held) {
136                bool same = true;
137                for (int i = 0; i < n; ++i) same = same && h[(size_t) i] == d[(size_t) i] + 1;
138                if (same) return true;
139            }
140            return false;
141        };
142        auto bare = [&](std::vector<fnx::RowOffer> o) {
143            for (auto& x : o) x.explore = false;
144            return o;
145        };
146        const std::vector<int> d = fnx::choose_rows(L, ROWS, base, c, held, cap);
147        const std::vector<int> d0 = fnx::choose_rows(bare(L), ROWS, base, c, held, cap);
148        if (!fits(d) || !fits(d0)) {
149            std::printf("choose case %d: a plan past the offers or the rows\n", it);
150            ++fails;
151            continue;
152        }
153        // d0's rate (charged as chosen) is the best of every candidate's: plan_rows' plan, the uniform plans, the held
154        const auto Lb = bare(L);
155        const std::vector<int> p = fnx::plan_rows(Lb, ROWS, base, c);
156        double best = fnx::plan_rate(Lb, p, base, c, held_d(p) ? 0.0 : cap);
157        for (int k = 0; k <= 15; ++k) {
158            std::vector<int> u;
159            for (const auto& o : Lb) u.push_back(std::min(k, o.avail));
160            if (fits(u)) best = std::max(best, fnx::plan_rate(Lb, u, base, c, held_d(u) ? 0.0 : cap));
161        }
162        for (const auto& h : held) {
163            std::vector<int> hd;
164            for (int k : h) hd.push_back(k - 1);
165            if (fits(hd)) best = std::max(best, fnx::plan_rate(Lb, hd, base, c, 0.0));
166        }
167        const double got = fnx::plan_rate(Lb, d0, base, c, held_d(d0) ? 0.0 : cap);
168        if (got < best * (1.0 - 1e-9)) {
169            std::printf("choose case %d: %.6f tokens/ms, the best candidate %.6f\n", it, got, best);
170            ++fails;
171        }
172        if (held_d(d0) && !held_d(d)) {
173            std::printf("choose case %d: an explorer's draft made a held layout capture\n", it);
174            ++fails;
175        }
176        cases += 1;
177    }
178    bool threw = false;
179    try {
180        fnx::RowOffer o;
181        o.avail = 2;
182        fnx::plan_rows({o}, ROWS, c.round_ms, c);
183    } catch (const std::runtime_error&) {
184        threw = true;
185    }
186    if (!threw) {
187        std::printf("an offer without its reach was taken\n");
188        ++fails;
189    }
190    std::printf("%d cases, %d failures: %s\n", cases, fails, fails ? "FAIL" : "ok");
191    return fails ? 1 : 0;
192}