File size: 8,404 Bytes
4039084 3365da7 4039084 044efc3 4039084 044efc3 4039084 3365da7 4039084 044efc3 4039084 3365da7 4039084 | 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 | import { calcSlipMetrics } from './math.js';
import { PRESETS } from './riskEngine.js';
/**
* Generate all combinations of size k from items where every item has a distinct matchId.
*/
function distinctMatchCombinations(legs, k) {
const results = [];
const chosen = [];
const usedMatches = new Set();
function backtrack(startIdx) {
if (chosen.length === k) {
results.push(chosen.slice());
return;
}
const remainingNeeded = k - chosen.length;
for (let i = startIdx; i <= legs.length - remainingNeeded; i++) {
const leg = legs[i];
if (usedMatches.has(leg.matchId)) continue;
usedMatches.add(leg.matchId);
chosen.push(leg);
backtrack(i + 1);
chosen.pop();
usedMatches.delete(leg.matchId);
}
}
backtrack(0);
return results;
}
/**
* Build an actionable T-60 starting XI checklist from the chosen slip legs.
*/
export function buildLineupChecklist(legs) {
const seen = new Set();
const items = [];
for (const leg of legs) {
if (seen.has(leg.matchId)) continue;
seen.add(leg.matchId);
const m = leg._matchRef;
const checks = [];
if (m?.home?.keyStartersToConfirm?.length) {
checks.push(`Confirm ${m.home.name} starters: ${m.home.keyStartersToConfirm.join(', ')}`);
}
if (m?.away?.keyStartersToConfirm?.length) {
checks.push(`Confirm ${m.away.name} starters: ${m.away.keyStartersToConfirm.join(', ')}`);
}
if (m?.home?.doubts5050?.names?.length) {
checks.push(`Resolve ${m.home.name} 50/50 doubts: ${m.home.doubts5050.names.join(', ')}`);
}
if (m?.away?.doubts5050?.names?.length) {
checks.push(`Resolve ${m.away.name} 50/50 doubts: ${m.away.doubts5050.names.join(', ')}`);
}
if (m?.home?.confirmedOut?.names?.length) {
checks.push(`Verify ${m.home.name} absentees stay out: ${m.home.confirmedOut.names.join(', ')}`);
}
if (m?.away?.confirmedOut?.names?.length) {
checks.push(`Verify ${m.away.name} absentees stay out: ${m.away.confirmedOut.names.join(', ')}`);
}
if (checks.length === 0) {
checks.push('Confirm first-choice goalkeeper, central defense, and lead striker start as expected.');
}
const walkAwayOdds = +(Math.max(1.05, leg.floorOdds - 0.04)).toFixed(2);
items.push({
matchId: leg.matchId,
kickoffEAT: leg.kickoffEAT,
matchLabel: leg.matchLabel,
pickLabel: leg.pickLabel,
consensusOdds: leg.odds,
walkAwayOdds,
checks,
});
}
items.sort((a, b) => String(a.kickoffEAT).localeCompare(String(b.kickoffEAT)));
return items;
}
export function stripInternalRef(leg) {
const { _matchRef, ...pub } = leg;
return pub;
}
/**
* Deterministically find the highest-survival-probability Primary Slip
* and a truly Zero-Overlap Alternative Slip inside the configured odds band.
*/
export function optimizeSlips(evaluated, configInput = {}) {
const config = { ...PRESETS.sniper, ...evaluated.config, ...configInput };
const eligiblePool = evaluated.allLegs.filter((leg) => {
if (leg.status === 'CLEAN') return true;
if (leg.status === 'CAUTION' && config.allowCaution) return true;
return false;
});
// Cap per-match candidates to top 2 lines to avoid combinatorial blowup on 20-100 match slates
const perMatchCount = new Map();
const prunedPool = [];
for (const leg of eligiblePool) {
const c = perMatchCount.get(leg.matchId) || 0;
if (c < 2) {
prunedPool.push(leg);
perMatchCount.set(leg.matchId, c + 1);
}
}
// Cap total pruned legs fed into k>=3 combinations to keep search instantaneous
const searchPool = prunedPool.slice(0, 40);
const candidates = [];
const maxK = Math.min(config.maxLegs || 2, 4, new Set(searchPool.map((l) => l.matchId)).size);
for (let k = 1; k <= maxK; k++) {
const combos = distinctMatchCombinations(searchPool, k);
for (const legs of combos) {
const metrics = calcSlipMetrics(legs);
if (metrics.consensusOdds < config.minOdds || metrics.consensusOdds > config.maxOdds) {
continue;
}
if (metrics.floorOdds < config.minOdds - 0.06) {
continue;
}
const fewerLegBonus = (maxK - k) * (config.singlePreferenceGap || 0.015);
const cleanBonus = metrics.totalSoftFlags === 0 ? 0.008 : 0;
const floorBonus = metrics.floorOdds >= config.minOdds ? 0.004 : 0;
const xgQualityBonus = legs.every((l) => l.hasVerifiedXg) ? 0.006 : 0;
const tier1Bonus = legs.every((l) => l.tier === 1) ? 0.004 : 0;
const score = metrics.adjJointProb + fewerLegBonus + cleanBonus + floorBonus + xgQualityBonus + tier1Bonus;
candidates.push({
legs,
metrics,
score,
});
}
}
candidates.sort((a, b) => {
if (Math.abs(b.score - a.score) > 1e-6) return b.score - a.score;
if (b.metrics.adjJointProb !== a.metrics.adjJointProb) {
return b.metrics.adjJointProb - a.metrics.adjJointProb;
}
return a.metrics.compoundedVigPct - b.metrics.compoundedVigPct;
});
const best = candidates[0] || null;
const minSlipProb = config.minSlipAdjProb || 0.50;
if (!best) {
return {
recommendation: 'NO_BET',
reason: `No eligible combination of ${config.allowCaution ? 'CLEAN/CAUTION' : 'CLEAN'} legs landed inside the ${config.minOdds.toFixed(2)}–${config.maxOdds.toFixed(2)} odds band (max ${config.maxLegs} leg${config.maxLegs > 1 ? 's' : ''}). Passing this slate avoids forcing risky picks.`,
primarySlip: null,
alternativeSlip: null,
alternativeReason: 'No primary slip qualified.',
topCleanSingles: evaluated.cleanLegs.slice(0, 5).map(stripInternalRef),
checklist: [],
candidateCount: 0,
};
}
if (best.metrics.adjJointProb < minSlipProb) {
return {
recommendation: 'NO_BET',
reason: `Best valid slip (${best.metrics.consensusOdds.toFixed(2)} odds) has ${(best.metrics.adjJointProb * 100).toFixed(1)}% risk-adjusted win probability, below this mode's ${(minSlipProb * 100).toFixed(0)}% minimum survival threshold.`,
primarySlip: {
legs: best.legs.map(stripInternalRef),
metrics: best.metrics,
belowThreshold: true,
},
alternativeSlip: null,
alternativeReason: 'Primary slip did not clear survival threshold.',
topCleanSingles: evaluated.cleanLegs.slice(0, 5).map(stripInternalRef),
checklist: buildLineupChecklist(best.legs),
candidateCount: candidates.length,
};
}
const allowAlt = config.includeAlternative !== false;
const primaryMatchIds = new Set(best.legs.map((l) => l.matchId));
const altCandidate = allowAlt
? candidates.find(
(c) =>
c.metrics.adjJointProb >= minSlipProb * 0.94 &&
c.legs.every((l) => !primaryMatchIds.has(l.matchId))
) || null
: null;
const primaryChecklist = buildLineupChecklist(best.legs);
const alternativeChecklist = altCandidate ? buildLineupChecklist(altCandidate.legs) : [];
const combinedLegsForChecklist = altCandidate
? [...best.legs, ...altCandidate.legs]
: [...best.legs];
return {
recommendation: 'BET',
reason: `Found ${candidates.length} compliant combination${candidates.length === 1 ? '' : 's'} inside ${config.minOdds.toFixed(2)}–${config.maxOdds.toFixed(2)}. Primary ${best.legs.length}-leg slip delivers ${(best.metrics.adjJointProb * 100).toFixed(1)}% risk-adjusted survival at ${best.metrics.consensusOdds.toFixed(2)} consensus (${best.metrics.floorOdds.toFixed(2)} floor).`,
primarySlip: {
legs: best.legs.map(stripInternalRef),
metrics: best.metrics,
belowThreshold: false,
},
alternativeSlip: altCandidate
? {
legs: altCandidate.legs.map(stripInternalRef),
metrics: altCandidate.metrics,
zeroOverlap: true,
}
: null,
alternativeReason: !allowAlt
? 'Single best option mode enabled (only 1 best pick selected).'
: altCandidate
? 'Zero-overlap alternative shares 0 matches with the Primary Slip.'
: 'No clean zero-overlap alternative exists on this slate without reusing Primary matches.',
topCleanSingles: evaluated.cleanLegs.slice(0, 5).map(stripInternalRef),
checklist: buildLineupChecklist(combinedLegsForChecklist),
primaryChecklist,
alternativeChecklist,
candidateCount: candidates.length,
};
}
|