Download src/engine/optimizer.js from ismizo/bet: direct link, hf CLI and curl.
- Browser
- Download file 8.4 kB
-
https://huggingface.co/spaces/ismizo/bet/resolve/main/src/engine/optimizer.js
- Command line
-
hf download hf://spaces/ismizo/bet/src/engine/optimizer.js
-
curl -L -o optimizer.js https://huggingface.co/spaces/ismizo/bet/resolve/main/src/engine/optimizer.js
8.4 kB
| 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, | |
| }; | |
| } | |