okami.mom/strata

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

Niko1221Merge f-027-gh-eng: GitHub engine items for 0.2.76ca60e9a0

main
5.4 KiB101 linesraw
1// Enumerate proposal and acceptance branches exactly; no Monte Carlo or GPU.
2#include "strata/core/spec_prob.hpp"
3#include "strata/core/gated_proposal.hpp"
4#include "strata/spec/draft_policy.hpp"
5#include <algorithm>
6#include <array>
7#include <cmath>
8#include <cstdio>
9#include <cstring>
10using namespace strata::core;
11using Dist = std::array<double, 2>;
12int failures = 0;
13void check(bool ok, const char* message) {
14    if (!ok) { ++failures; std::fprintf(stderr, "FAIL: %s\n", message); }
15}
16Dist judged(const Dist& p, const std::array<float,2>& q, int d) {
17    const int32_t ids[2] = {0,1};
18    const SpecP P{ids,p.data(),2}; const SpecQ Q{ids,q.data(),2};
19    const double a=std::min(1.0,p[d]/q[d]); Dist result{};
20    if (a>0) result[spec_verify_row(P,d,Q,(float)(a/2),0.5f).token]+=a;
21    if (a<1) result[spec_verify_row(P,d,Q,(float)((1+a)/2),0.5f).token]+=1-a;
22    return result;
23}
24Dist legacy_raw_q_plain_p(const Dist& p, const std::array<float,2>& q, bool own_draw, float threshold) {
25    Dist result{};
26    for (int d=0;d<2;++d) {
27        const float gate=own_draw?q[d]:std::max(q[0],q[1]);
28        const auto row=gate>=threshold?judged(p,q,d):p;
29        for (int i=0;i<2;++i) result[i]+=q[d]*row[i];
30    }
31    return result;
32}
33bool equal(const Dist& a,const Dist& b) {
34    return std::abs(a[0]-b[0])<1e-12 && std::abs(a[1]-b[1])<1e-12;
35}
36// Enumerate the repaired first-row mixture using the ACTUAL packet builder.
37// No candidate-conditioned renormalization: every draw receives the same root mu.
38Dist proposal_contract(const Dist& p,const std::array<float,2>& q,float threshold,
39                       strata::core::gated::RootChoice root={}) {
40    std::array<int32_t,strata::core::gated::kStride> raw;
41    raw.fill(-1);raw[0]=0;raw[1]=1;
42    std::memcpy(raw.data()+strata::core::gated::kEntries,q.data(),sizeof(q));
43    Dist result{};
44    for (int d=0;d<2;++d) {
45        const int32_t draft=d;const float score=q[d];strata::core::gated::Packet packet;
46        const bool ok=strata::core::gated::make_packet(packet,raw.data(),&draft,&score,1,1,2,2,threshold,false,root);
47        check(ok,"valid gated packet");if(!ok)continue;
48        Dist mu{strata::core::gated::mass_at(packet.mu.data(),0),strata::core::gated::mass_at(packet.mu.data(),1)};
49        double acceptance=0;
50        if (!packet.null_tail) {
51            check(mu[d]>0,"offer has positive mu");
52            acceptance=std::min(1.0,p[d]/mu[d]);
53            result[d]+=q[d]*acceptance;
54        }
55        if (acceptance<1) {
56            Dist residual{std::max(0.0,p[0]-mu[0]),std::max(0.0,p[1]-mu[1])};
57            const double sum=residual[0]+residual[1];check(sum>0,"reachable failure has residual");
58            if(sum>0)for(int y=0;y<2;++y)result[y]+=q[d]*(1-acceptance)*residual[y]/sum;
59        }
60    }
61    return result;
62}
63int main(int argc,char** argv) {
64    const bool expect_pick=!(argc>1 && std::strcmp(argv[1],"top")==0);
65    check(spec_gate_pick()==expect_pick,"pick is default; only explicit lowercase top selects Top compatibility");
66    const Dist p{0.5,0.5};const std::array<float,2> q{0.875f,0.125f};
67    check(equal(legacy_raw_q_plain_p(p,q,true,0.5f),Dist{0.5625,0.4375}),
68          "LEGACY negative control: own-draw gate with full-q verifier/plain-p failure is biased");
69    check(equal(proposal_contract(p,q,0.5f),p),"new pick mu/NULL mixture preserves p");
70    check(equal(spec_gate_pick()?proposal_contract(p,q,0.5f):legacy_raw_q_plain_p(p,q,false,0.5f),p),
71          "configured repaired pick or Top compatibility preserves p");
72    int top_cases=0,proposal_cases=0;
73    for(int a=1;a<16;++a)for(int b=1;b<16;++b) {
74        const Dist P{a/16.0,1-a/16.0};const std::array<float,2> Q{b/16.0f,1-b/16.0f};
75        for(float threshold:{0.0f,0.5f,0.9f,1.1f}) {
76            check(equal(legacy_raw_q_plain_p(P,Q,false,threshold),P),"Top compatibility distribution law");++top_cases;
77            check(equal(proposal_contract(P,Q,threshold),P),"H-only proposal mixture");++proposal_cases;
78            for(int rescue:{0,1}) {
79                check(equal(proposal_contract(P,Q,threshold,{true,rescue,2}),P),"fixed root UNION on every source branch");++proposal_cases;
80            }
81        }
82    }
83    // A planner must not learn from draws it may select, including discarded position-keyed lookahead.
84    strata::spec::DraftPolicy live(6);
85    for(int i=0;i<5;++i) { live.observe(false,4,3,24,1.0);live.observe(false,6,5,24,1.5); }
86    const auto request_plan=live;
87    auto short_chain=live,long_chain=live;
88    short_chain.observe(false,4,3,24,0.2);long_chain.observe(false,4,3,24,8.0);
89    const bool short_lookup=short_chain.choose(4,5,24).lookup,long_lookup=long_chain.choose(4,5,24).lookup;
90    check(!short_lookup && long_lookup,"draw-dependent chain time can change the actual policy choice");
91    Dist conditional{},planned{};
92    for(int d=0;d<2;++d) {
93        const auto selected=(d==0?long_lookup:short_lookup)?p:judged(p,q,d);
94        const auto fixed=request_plan.choose(4,5,24).lookup?p:judged(p,q,d);
95        for(int i=0;i<2;++i) { conditional[i]+=q[d]*selected[i];planned[i]+=q[d]*fixed[i]; }
96    }
97    check(equal(conditional,Dist{0.4375,0.5625}),"LEGACY negative: D-conditioned source without common first measure changes proposal law");
98    check(equal(planned,p),"request plan preserves p despite subsequent timing observations");
99    std::printf("spec_admission_test: %d Top compatibility cases, %d actual mu-packet cases, legacy negatives; %d failures\n",top_cases,proposal_cases,failures);
100    return failures?1:0;
101}