feat(assessment): grade choice answers with distractors from error models

This commit is contained in:
shira committed 2026-10-11 03:12:24 +03:00
1 parent efa41d5967
commit f1147b1885
22 files changed
+1346 -47

No files matched your search

@@ -1,6 +1,7 @@
package domain
import (
"slices"
"strconv"
"strings"
)
@@ -44,6 +45,17 @@ var assessmentAlgorithms = map[string]assessmentAlgorithm{
requiredNames: []string{"sequence", "capacity"},
generate: generateLRUHits,
},
"lru_hits_choice.v1": {
answerFormat: AnswerFormatChoice,
parameters: []assessmentParameterSpec{
{name: "accesses", lowest: 8, highest: 500},
{name: "keys", lowest: 3, highest: 100},
{name: "capacity", lowest: 2, highest: 50},
},
dataNames: []string{"sequence"},
requiredNames: []string{"sequence", "capacity"},
generateChoice: generateLRUHitsChoice,
},
"dependency_order.v1": {
answerFormat: AnswerFormatIntegerSequence,
parameters: []assessmentParameterSpec{
@@ -141,12 +153,107 @@ func dedupProcessed(keys, times []int, ttl int) int {
// Data names: sequence.
func generateLRUHits(random *seededRandom, parameters map[string]int32) (map[string]string, string) {
accesses := drawLRUAccesses(random, parameters)
hits := lruHits(accesses, int(parameters["capacity"]))
return map[string]string{"sequence": joinInts(accesses)}, strconv.Itoa(hits)
}
// drawLRUAccesses draws the access sequence of the cache algorithms. The
// draws are part of what the golden tests of lru_hits.v1 pin.
func drawLRUAccesses(random *seededRandom, parameters map[string]int32) []int {
accesses := make([]int, parameters["accesses"])
for index := range accesses {
accesses[index] = 1 + random.below(int(parameters["keys"]))
}
hits := lruHits(accesses, int(parameters["capacity"]))
return map[string]string{"sequence": joinInts(accesses)}, strconv.Itoa(hits)
return accesses
}
// nearbyCountSteps is how far from the correct count the last-resort
// distractors of a count answer reach.
const nearbyCountSteps = 8
// Data names: sequence. The answer is the number of hits like lru_hits.v1,
// offered among options. The wrong options come from mistakes that a solver
// typically makes: an off-by-one count in either direction, counting misses
// instead of hits, a cache where a hit does not renew the key (FIFO
// eviction), a cache that evicts the most recently used key, and a capacity
// that is off by one in either direction.
func generateLRUHitsChoice(random *seededRandom, parameters map[string]int32) choiceTask {
accesses := drawLRUAccesses(random, parameters)
capacity := int(parameters["capacity"])
total := len(accesses)
hits := cacheHits(accesses, capacity, cachePolicy{refreshOnHit: true})
mistakes := []int{
hits + 1,
hits - 1,
total - hits,
cacheHits(accesses, capacity, cachePolicy{}),
cacheHits(accesses, capacity, cachePolicy{refreshOnHit: true, evictNewest: true}),
cacheHits(accesses, capacity-1, cachePolicy{refreshOnHit: true}),
cacheHits(accesses, capacity+1, cachePolicy{refreshOnHit: true}),
}
var nearby []int
for step := 2; step <= nearbyCountSteps; step++ {
nearby = append(nearby, hits+step, hits-step)
}
return choiceTask{
data: map[string]string{"sequence": joinInts(accesses)},
correct: strconv.Itoa(hits),
mistakes: possibleCounts(mistakes, total),
nearby: possibleCounts(nearby, total),
}
}
// possibleCounts writes the values that a count of up to total can take, in
// their order.
func possibleCounts(values []int, total int) []string {
texts := make([]string, 0, len(values))
for _, value := range values {
if value >= 0 && value <= total {
texts = append(texts, strconv.Itoa(value))
}
}
return texts
}
// cachePolicy selects a replacement rule of cacheHits. The zero value is a
// first-in-first-out cache that evicts its oldest key.
type cachePolicy struct {
// refreshOnHit makes a hit renew the key, which is the rule of a
// least-recently-used cache.
refreshOnHit bool
// evictNewest evicts the newest key of a full cache instead of the oldest.
evictNewest bool
}
// cacheHits counts the accesses that find their key in a cache of the given
// capacity under the replacement rule. With refreshOnHit and no evictNewest
// it counts like lruHits, which the tests keep equal; the other rules model
// wrong solutions and exist only to make distractors.
func cacheHits(accesses []int, capacity int, policy cachePolicy) int {
// held keeps the cached keys from the oldest entry to the newest.
held := make([]int, 0, capacity)
hits := 0
for _, key := range accesses {
position := slices.Index(held, key)
if position >= 0 {
hits++
if policy.refreshOnHit {
held = append(held[:position], held[position+1:]...)
held = append(held, key)
}
continue
}
if len(held) == capacity {
if policy.evictNewest {
held = held[:len(held)-1]
} else {
held = held[1:]
}
}
held = append(held, key)
}
return hits
}
// lruHits counts the accesses that find their key in a least-recently-used