172 lines
6.2 KiB
Go
172 lines
6.2 KiB
Go
package matching
|
|
|
|
import (
|
|
"lincodex/backend/internal/domain"
|
|
)
|
|
|
|
// Scores are computed in integers so that the same data and policy always give
|
|
// the same order on every platform. A policy share is BasisPoints (1e-4); a
|
|
// component is stored in micro units (1e-6), the precision at which results
|
|
// are rounded and compared; a skill contribution keeps the exact product of
|
|
// three shares in pico units (1e-12).
|
|
const (
|
|
microPerUnit int64 = 1_000_000
|
|
bpPerUnit int64 = 10_000
|
|
picoPerBasisPoint int64 = 100_000_000
|
|
microPerBasisPoint int64 = 100
|
|
)
|
|
|
|
// requestedSkill is one skill that a need or a stack filter asks for. A skill
|
|
// without a taxonomy code cannot match any evidence: the platform has no
|
|
// reviewed mapping for it, so it stays a gap and never an invented match.
|
|
type requestedSkill struct {
|
|
code string
|
|
required bool
|
|
}
|
|
|
|
// evidenceSource is one permitted piece of evidence of a candidate: either a
|
|
// stated stack tag of the card (trust 0, a claim) or the one assessment fact
|
|
// that supports the confirmed category.
|
|
type evidenceSource struct {
|
|
code string
|
|
trust domain.EvidenceTrust
|
|
attribution domain.EvidenceAttribution
|
|
measured bool
|
|
}
|
|
|
|
// skillOutcome is the contribution of one requested skill and what it rests
|
|
// on. The explanation reads these exact values.
|
|
type skillOutcome struct {
|
|
required bool
|
|
pico int64
|
|
// measured is true when the best evidence is a measurement and false when
|
|
// it is a stated claim or when there is none.
|
|
measured bool
|
|
}
|
|
|
|
// scores are the computed components of one entry, in micro units.
|
|
type scores struct {
|
|
skills []skillOutcome
|
|
coverage int64
|
|
domain int64
|
|
semantics int64
|
|
fit int64
|
|
t int64
|
|
competition int64
|
|
strength int64
|
|
freshness int64
|
|
responsive int64
|
|
rank int64
|
|
}
|
|
|
|
// scoreInput is everything a score depends on.
|
|
type scoreInput struct {
|
|
// confirmed selects the confirmed-profile model. An unconfirmed profile
|
|
// gets only coverage and fit: it has no theta, T, P, or rank score, so
|
|
// those values are never computed for it.
|
|
confirmed bool
|
|
policy domain.MatchingPolicy
|
|
skills []requestedSkill
|
|
evidence []evidenceSource
|
|
}
|
|
|
|
// computeScores applies the FR09 model. Unknown components are neutral: Dom,
|
|
// Sem, Fr, and Rsp use the neutral value, T is neutral because there is no
|
|
// comparable real cohort, and V is zero because no competition evidence
|
|
// exists, which is not a penalty.
|
|
func computeScores(input scoreInput) scores {
|
|
policy := input.policy
|
|
neutral := int64(policy.NeutralUnknown) * microPerBasisPoint
|
|
result := scores{
|
|
domain: neutral, semantics: neutral, t: neutral, freshness: neutral, responsive: neutral,
|
|
}
|
|
result.skills = make([]skillOutcome, 0, len(input.skills))
|
|
var weighted, weights int64
|
|
for _, skill := range input.skills {
|
|
pico, measured := skillContribution(policy, skill, input.evidence)
|
|
weight := int64(policy.DesiredSkillWeight)
|
|
if skill.required {
|
|
weight = int64(policy.RequiredSkillWeight)
|
|
}
|
|
weighted += weight * pico
|
|
weights += weight
|
|
result.skills = append(result.skills, skillOutcome{required: skill.required, pico: pico, measured: measured})
|
|
}
|
|
// An empty request has nothing to cover; it uses the neutral value instead
|
|
// of dividing by zero.
|
|
result.coverage = neutral
|
|
if weights > 0 {
|
|
result.coverage = roundedQuotient(weighted, weights*microPerUnit)
|
|
}
|
|
result.fit = roundedQuotient(
|
|
int64(policy.FitCoverageWeight)*result.coverage+
|
|
int64(policy.FitDomainWeight)*result.domain+
|
|
int64(policy.FitSemanticsWeight)*result.semantics,
|
|
bpPerUnit)
|
|
if !input.confirmed {
|
|
result.t, result.freshness, result.responsive = 0, 0, 0
|
|
return result
|
|
}
|
|
// P = T + boost * (1 - T) * V, with V = 0 without competition evidence.
|
|
result.strength = result.t + roundedQuotient(
|
|
int64(policy.CompetitionBoost)*(microPerUnit-result.t)*result.competition, bpPerUnit*microPerUnit)
|
|
rank := roundedQuotient(
|
|
int64(policy.RankFitWeight)*result.fit+int64(policy.RankStrengthWeight)*result.strength, bpPerUnit)
|
|
rank = multiplyMicro(rank, blendedMultiplier(policy.FreshnessFloor, result.freshness))
|
|
result.rank = multiplyMicro(rank, blendedMultiplier(policy.ResponsivenessFloor, result.responsive))
|
|
return result
|
|
}
|
|
|
|
// skillContribution is the best product of the trust, attribution, and
|
|
// relation coefficients over the evidence, not their sum, so a repeated tag or
|
|
// a second piece of the same kind never raises the contribution. A tie goes to
|
|
// the measurement, so the explanation names the stronger basis.
|
|
func skillContribution(policy domain.MatchingPolicy, skill requestedSkill, evidence []evidenceSource) (int64, bool) {
|
|
if skill.code == "" {
|
|
return 0, false
|
|
}
|
|
var best int64
|
|
var measured bool
|
|
for _, source := range evidence {
|
|
trust, trustOK := policy.TrustCoefficient(source.trust)
|
|
attribution, attributionOK := policy.AttributionCoefficient(source.attribution)
|
|
if !trustOK || !attributionOK {
|
|
continue
|
|
}
|
|
relation := policy.RelationCoefficient(policy.RelateSkill(skill.code, source.code))
|
|
value := int64(trust) * int64(attribution) * int64(relation)
|
|
if value > best || (value == best && value > 0 && source.measured && !measured) {
|
|
best, measured = value, source.measured
|
|
}
|
|
}
|
|
return best, measured
|
|
}
|
|
|
|
// belongsToNearGroup reports whether a requested required skill earns less
|
|
// than the policy threshold, which moves a confirmed profile to "near".
|
|
func (result scores) belongsToNearGroup(policy domain.MatchingPolicy) bool {
|
|
threshold := int64(policy.NearSkillThreshold) * picoPerBasisPoint
|
|
for _, skill := range result.skills {
|
|
if skill.required && skill.pico < threshold {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// blendedMultiplier is floor + (1 - floor) * x, in micro units.
|
|
func blendedMultiplier(floor domain.BasisPoints, x int64) int64 {
|
|
return int64(floor)*microPerBasisPoint + roundedQuotient((bpPerUnit-int64(floor))*x, bpPerUnit)
|
|
}
|
|
|
|
func multiplyMicro(left, right int64) int64 {
|
|
return roundedQuotient(left*right, microPerUnit)
|
|
}
|
|
|
|
// roundedQuotient divides non-negative integers and rounds half up.
|
|
func roundedQuotient(numerator, denominator int64) int64 {
|
|
return (numerator + denominator/2) / denominator
|
|
}
|
|
|
|
func microToFloat(value int64) float64 { return float64(value) / float64(microPerUnit) }
|