~/ learn/ comp-456/ cards/ Case-based reasoning in depth
1 of 3

Retrieve the most similar case from a case base by weighted feature match (salient features weigh more), even when no case matches every feature.

Retrieve the most similar case from a case base by weighted feature match (salient features weigh more), even when no case matches every feature.

Answer

case_base = [ {"id": "case#1", "features": {"engine": "no_start", "lights": "on", "fuel": "full"}, "solution": "check_starter"}, {"id": "case#2", "features": {"engine": "stalls", "lights": "dim", "fuel": "low"}, "solution": "refuel"}, {"id": "case#3", "features": {"engine": "no_start", "lights": "dim", "fuel": "full"}, "solution": "charge_battery"}, ] weights = {"engine": 0.5, "lights": 0.3, "fuel": 0.2} query = {"engine": "no_start", "lights": "dim", "fuel": "low"} def similarity(case, q): return sum(weights[k] for k, v in q.items() if case["features"].get(k) == v) scored = sorted(case_base, key=lambda c: similarity(c, query), reverse=True) best = scored[0] print(f"best match: {best['id']} (similarity {similarity(best, query):.1f}) -> solution: {best['solution']}")

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/1c2f44cb-da6c-4363-ab31-2566d120352c/flashcard utf-8 LF