Libraryless. Click here for Pure Java version (16498L/114K).
// for notes see #1028089 sclass PatternMaker1 { S comment, examplesText, counterexamplesText; transient Concepts cc; transient Set<S> examples, counterexamples; transient new Lowest<Theory> simplestFullSolutionCollector; TreeMap<Int, Scored<S>> bestPatternsByClass; S simplestFullSolution; // simplest pattern solving all examples Runnable strategy; // collectors transient new TreeMap<Int, Theory> bestByComplexity; transient Map<Int, TreeSetWithDuplicates<Theory>> allByComplexity = autoTreeMap(() -> treeSetWithDuplicatesOverCalculatedField theoryScore()); sconcept Example { S text; bool pos; toString { ret text + "/" + pos; } } sconcept Theory { S pattern; transient simplyCached Cl<Example> solvedExamples() { ret filter(list(_concepts, Example.class), e -> e.pos == mmo2_match(pattern, e.text)); } transient simplyCached Cl<Example> unsolvedExamples() { ret setMinusSet(list(_concepts, Example.class), solvedExamples()); } bool isFullSolution() { ret empty(unsolvedExamples()); } int score() { ret l(solvedExamples()); } transient simplyCached int complexityClass() { ret numberOfWords2(pattern); } } // end of Theory *() {} *(S *examplesText, S *counterexamplesText) {} *(LS examples, LS counterexamples) { examplesText = lines(examples); counterexamplesText = lines(counterexamples); } void prepare { cc = new Concepts; clearCollectors(); indexConceptFieldCI(cc, Example.class, 'text); indexConceptFieldCI(cc, Theory.class, 'pattern); setField(simplestFullSolution := null); simplestFullSolutionCollector.clear(); examples = asLinkedHashSet(tlftj(examplesText)); counterexamples = asLinkedHashSet(tlftj(counterexamplesText)); Set<S> intersection = setIntersection(examples, counterexamples); if (nempty(intersection)) ret with infoBox("Error: Examples appear in both lists, e.g. " + first(intersection)); for (S s : examples) uniqCI(cc, Example.class, text := s, pos := true); for (S s : counterexamples) uniqCI(cc, Example.class, text := s, pos := false); int nExamples = countConcepts(cc, Example.class); print("Have " + nExamples(nExamples)); } void think { prepare(); strategy = makeStrategy(); pcallF(strategy); afterThink(); } void afterThink { print("Have " + nTheories(countConcepts(cc, Theory.class))); setField(bestPatternsByClass := mapValues(t -> scoredNonPercent(t.score(), t.pattern), bestByComplexity)); doneThinking(); } swappable Runnable makeStrategy() { ret new Strategy1; } runnable class Strategy1 { // positive examples to pattern for (S s : examples) addPatterns(ai_inputExampleToPossibleMMOPatterns1(s)); // combine some pattern pairs from complexity classes 1-2 twice { for (Theory a : allByComplexity.get(1)) for (int n = 1; n <= 2; n++) for (Theory b : allByComplexity.get(n)) addPattern(mmo2_combineWithOr(a.pattern, b.pattern)); } } Map theoryToMap(Theory t) { ret t == null ? null : litorderedmap( "Pattern" := t.pattern, "Complexity class" := t.complexityClass(), "Solved examples" := l(t.solvedExamples()) + " of " + countConcepts(cc, Example.class), "Unsolved" := joinWithComma(quoteAll(collect text(t.unsolvedExamples())))); } void addPatterns(Iterable<S> l) { fOr (S s : l) addPattern(s); } void addPattern(S pattern) { addToCollectors(uniqCI_returnIfNew(cc, Theory.class, +pattern)); } void tryPattern(S pattern) { addPattern(pattern); } int theoryScore(Theory t) { ret t == null ? 0 : t.score(); } void clearCollectors { bestByComplexity.clear(); allByComplexity.clear(); } void addToCollectors(Theory t) { if (t == null) ret; putIfHigherByCalculatedField theoryScore(bestByComplexity, t.complexityClass(), t); allByComplexity.get(t.complexityClass()).add(t); if (t.isFullSolution() && simplestFullSolutionCollector.put(t, t.complexityClass())) setField(simplestFullSolution := t.pattern); } void printBestByClass() { for (int complexity, Theory t : bestByComplexity) { print("Best theory for complexity " + complexity + ": " + shorten(100, str(theoryToMap(t)))); } } !include #1028121 // setField + change event doneThinking; }
Began life as a copy of #1028089
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1028122 |
Snippet name: | PatternMaker1 [OK] |
Eternal ID of this version: | #1028122/17 |
Text MD5: | 2112bb4eecb6ec544c139ff155ab2625 |
Transpilation MD5: | bc575a10184e2688b8153266bcb819a3 |
Author: | stefan |
Category: | javax / a.i. |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-11-05 12:40:24 |
Source code size: | 4603 bytes / 141 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 245 / 607 |
Version history: | 16 change(s) |
Referenced in: | #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674) |