Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

141
LINES

< > BotCompany Repo | #1028122 // PatternMaker1 [OK]

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (16498L/114K).

1  
// for notes see #1028089
2  
3  
sclass PatternMaker1 {
4  
  S comment, examplesText, counterexamplesText;
5  
  transient Concepts cc;
6  
  transient Set<S> examples, counterexamples;
7  
  transient new Lowest<Theory> simplestFullSolutionCollector;
8  
  TreeMap<Int, Scored<S>> bestPatternsByClass;
9  
  S simplestFullSolution; // simplest pattern solving all examples
10  
  Runnable strategy;
11  
  
12  
  // collectors
13  
  transient new TreeMap<Int, Theory> bestByComplexity;
14  
  transient Map<Int, TreeSetWithDuplicates<Theory>> allByComplexity
15  
    = autoTreeMap(() -> treeSetWithDuplicatesOverCalculatedField theoryScore());
16  
17  
  sconcept Example {
18  
    S text;
19  
    bool pos;
20  
    
21  
    toString { ret text + "/" + pos; }
22  
  }
23  
24  
  sconcept Theory {
25  
    S pattern;
26  
  
27  
    transient simplyCached Cl<Example> solvedExamples() {
28  
      ret filter(list(_concepts, Example.class), e -> e.pos == mmo2_match(pattern, e.text));
29  
    }
30  
    
31  
    transient simplyCached Cl<Example> unsolvedExamples() {
32  
      ret setMinusSet(list(_concepts, Example.class), solvedExamples());
33  
    }
34  
    
35  
    bool isFullSolution() { ret empty(unsolvedExamples()); }
36  
    
37  
    int score() { ret l(solvedExamples()); }
38  
    
39  
    transient simplyCached int complexityClass() { ret numberOfWords2(pattern); }
40  
  } // end of Theory
41  
  
42  
  *() {}
43  
  *(S *examplesText, S *counterexamplesText) {}
44  
  *(LS examples, LS counterexamples) {
45  
    examplesText = lines(examples);
46  
    counterexamplesText = lines(counterexamples);
47  
  }
48  
  
49  
  void prepare {
50  
    cc = new Concepts;
51  
    clearCollectors();
52  
    indexConceptFieldCI(cc, Example.class, 'text);
53  
    indexConceptFieldCI(cc, Theory.class, 'pattern);
54  
    setField(simplestFullSolution := null);
55  
    simplestFullSolutionCollector.clear();
56  
    
57  
    examples = asLinkedHashSet(tlftj(examplesText));
58  
    counterexamples = asLinkedHashSet(tlftj(counterexamplesText));
59  
    Set<S> intersection = setIntersection(examples, counterexamples);
60  
    if (nempty(intersection))
61  
      ret with infoBox("Error: Examples appear in both lists, e.g. " + first(intersection));
62  
63  
    for (S s : examples) uniqCI(cc, Example.class, text := s, pos := true);
64  
    for (S s : counterexamples) uniqCI(cc, Example.class, text := s, pos := false);
65  
    int nExamples = countConcepts(cc, Example.class);
66  
    print("Have " + nExamples(nExamples));
67  
  }
68  
69  
  void think {
70  
    prepare();
71  
72  
    strategy = makeStrategy();
73  
    pcallF(strategy);
74  
75  
    afterThink();
76  
  }
77  
  
78  
  void afterThink {
79  
    print("Have " + nTheories(countConcepts(cc, Theory.class)));
80  
    
81  
    setField(bestPatternsByClass := mapValues(t -> scoredNonPercent(t.score(), t.pattern), bestByComplexity));
82  
83  
    doneThinking();
84  
  }
85  
  
86  
  swappable Runnable makeStrategy() { ret new Strategy1; }
87  
  
88  
  runnable class Strategy1 {
89  
    // positive examples to pattern
90  
    for (S s : examples)
91  
      addPatterns(ai_inputExampleToPossibleMMOPatterns1(s));
92  
    
93  
    // combine some pattern pairs from complexity classes 1-2
94  
    twice {
95  
      for (Theory a : allByComplexity.get(1))
96  
        for (int n = 1; n <= 2; n++)
97  
          for (Theory b : allByComplexity.get(n))
98  
            addPattern(mmo2_combineWithOr(a.pattern, b.pattern));
99  
    }
100  
  }
101  
102  
  Map theoryToMap(Theory t) {  
103  
    ret t == null ? null :
104  
      litorderedmap(
105  
        "Pattern" := t.pattern,
106  
        "Complexity class" := t.complexityClass(),
107  
        "Solved examples" := l(t.solvedExamples()) + " of " + countConcepts(cc, Example.class),
108  
        "Unsolved" := joinWithComma(quoteAll(collect text(t.unsolvedExamples()))));
109  
  }
110  
  
111  
  void addPatterns(Iterable<S> l) { fOr (S s : l) addPattern(s); }
112  
  void addPattern(S pattern) {
113  
    addToCollectors(uniqCI_returnIfNew(cc, Theory.class, +pattern));
114  
  }
115  
  void tryPattern(S pattern) { addPattern(pattern); }
116  
  
117  
  int theoryScore(Theory t) { ret t == null ? 0 : t.score(); }
118  
  
119  
  void clearCollectors {
120  
    bestByComplexity.clear();
121  
    allByComplexity.clear();
122  
  }
123  
  
124  
  void addToCollectors(Theory t) {
125  
    if (t == null) ret;
126  
    putIfHigherByCalculatedField theoryScore(bestByComplexity, t.complexityClass(), t);
127  
    allByComplexity.get(t.complexityClass()).add(t);
128  
    if (t.isFullSolution() && simplestFullSolutionCollector.put(t, t.complexityClass()))
129  
      setField(simplestFullSolution := t.pattern);
130  
  }
131  
  
132  
  void printBestByClass() {
133  
    for (int complexity, Theory t : bestByComplexity) {
134  
      print("Best theory for complexity " + complexity + ": " + shorten(100, str(theoryToMap(t))));
135  
    }
136  
  }
137  
  
138  
  !include #1028121 // setField + change
139  
  
140  
  event doneThinking;
141  
}

Author comment

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: 246 / 608
Version history: 16 change(s)
Referenced in: [show references]