sclass OneColorTheoryChecker { replace Cell with URecognizer.Cell. new ProbabilisticScheduler scheduler; sclass Theory { // values are: withProbability(strengthOfEvidence, probabilityOfTheory) new MultiMap> examples; double strengthSum, probabilitySum; double currentProbabilityGuess() { ret doubleRatio(probabilitySum, strengthSum); } S renderProbabilityGuess() { ret "Current probability guess (evidence count " + n2(l(examples)) + "): " + formatDouble3X(currentProbabilityGuess()) + " with strength " + formatDouble3(strengthSum); } bool hasExampleNamed(S name) { ret examples.containsKey(name); } void addEvidence(S desc, double probabilityOfTheory, double strengthOfEvidence) { examples.add(desc, withProbability(strengthOfEvidence, probabilityOfTheory)); probabilitySum += probabilityOfTheory*strengthOfEvidence; strengthSum += strengthOfEvidence; } S toStringWithEvidence() { ret toString() + "\n\n" + pnlToString("EVIDENCE", multiMapToPairs(examples)); } } record OneColorTheory(S text, Cell cell, RGB color) > Theory { toString { ret renderProbabilityGuess() + ": " + dollarVarsMeanFields(text, this); } selfType branch(O... _) { this; } } // create the theory object and initiate the reasoning // you have to step the scheduler afterwards to get results OneColorTheory makeTheory(Cell cell) { var color = cell.averageColor(); var theory = new OneColorTheory("Color of every pixel in $cell is $color.", cell, color); testOneColorTheory(cell, theory); ret theory; } void noteCellColor(Cell cell, OneColorTheory theory) { var desc = "Pixel check at " + cell; if (theory.hasExampleNamed(desc)) return; var strength = doubleRatio(cell.area(), theory.cell.area()); // how much do we have to say about the cell in the theory? var p = colorDistanceToProbability(cell.averageColor(), theory.color); addExampleToTheory(theory, desc, p, strength); } double colorDistanceToProbability(RGB col1, RGB col2) { double sim = colorSimilarity(col1, col2), sqr = sqr(sim); printFunctionCall colorDistanceToProbability(+col1, +col2, +sim, +sqr); ret sqr; } void testOneColorTheory(Cell cell, OneColorTheory theory) { noteCellColor(cell, theory); scheduler.atRelative(dontZoomTooFar(cell), r { for (var split : usefulSplits(cell)) scheduler.atRelative(split.probability(), r { for (var cell : split!) testOneColorTheory(cell, theory.branch(description := "recursion to " + cell)); }); }); } L> usefulSplits(Cell cell) { ret map withProbability1(llNonNulls(cell.split(0, 2), cell.split(1, 2))); } // probability penalty .25 for zooming in 3 steps double dontZoomTooFar(Cell cell) { ret 0.75; } void addExampleToTheory(Theory theory, S desc, double probabilityOfTheory, double strengthOfEvidence) { theory.addEvidence(desc, probabilityOfTheory, /* scheduler.currentProbability()* */ strengthOfEvidence); } }