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

243
LINES

< > BotCompany Repo | #1007739 // Demo 2: Fibonacci numbers [hacked for speed, WORKS]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 3874K of libraries. Click here for Pure Java version (11284L/79K).

1  
!7
2  
3  
// include #1007762 // new lispCalculateDeep
4  
5  
sS theFacts = [[
6  
7  
  Fibonacci number n is Fibonacci number n-1 plus Fibonacci number n-2 if n > 2.
8  
  There is no Fibonacci number with a negative index.
9  
  There is no Fibonacci number 0.
10  
  Fibonacci number 1 is 1.
11  
  Fibonacci number 2 is 1.
12  
13  
  // Some technical stuff
14  
  gmtzfidalveipfyx feyhbrhdeovmbvaw amcmxkgwhivbouqs
15  
  pqnfhkzxoqkaoyqk sppxbuuqeetjmzuo
16  
]];
17  
18  
sS theRiddle = [[
19  
  What is Fibonacci number 0?
20  
  What is Fibonacci number 1?
21  
  What is Fibonacci number 2?
22  
  What is Fibonacci number 3?
23  
  What is Fibonacci number 4?
24  
]];
25  
26  
static S concepts = [[
27  
  ["feyhbrhdeovmbvaw", "There is no X."],
28  
  ["mtsrzasaoxrmicch", "X is Y if Z."],
29  
  ["fgvvrzypbkqomktd", "X is Y."],
30  
  ["sppxbuuqeetjmzuo", "X plus Y"],
31  
  ["devwldetfsfylnod", "X with a negative index"],
32  
  ["rgsamtbrhpcqowko", "Fibonacci number X"],
33  
  ["jxcubqjtqykhfvyo", "X - Y", "as in minus"],
34  
  ["wdbphzfoxwlrhdyl", "X > Y"],
35  
  ["urfkhwfasdaqqqse", "What is X?"],
36  
  ["pqnfhkzxoqkaoyqk", "Operation X should be expanded."],
37  
  ["amcmxkgwhivbouqs", "Something that doesn't exist"],
38  
  ["yissyloatogavyuu", "Fibonacci number"],
39  
  ["gmtzfidalveipfyx", "Marker X implies label Y."]
40  
]];
41  
42  
!include #1007689 // Web v2
43  
44  
static int saveInterval = 60000;
45  
static volatile new Web web;
46  
static L<Lisp> facts;
47  
static long lastSave, sizeSaved;
48  
49  
extend Web {
50  
  volatile transient bool cancelled, calculating;
51  
}
52  
53  
p-tt {
54  
  setConsoleTitle(programTitle());
55  
  load("web"); lastSave = sysNow();
56  
  setFrameHeight(consoleFrame(), 400);
57  
  centerConsole();
58  
  //magellan();
59  
  //graphite();
60  
  //mistAqua();
61  
  //moderateTheme();
62  
  sahara();
63  
  swing {
64  
    setConsoleInput("What is Fibonacci number 500?");
65  
    consoleAddAskButton();
66  
    focusConsole();
67  
    final JLabel lblBrainSize = jRightAlignedLabel();
68  
    addToConsole2(withMargin(centerAndEast(lblBrainSize,
69  
      withLeftMargin(jbutton("Reset", "reset")))));
70  
    awtEvery(lblBrainSize, 1000, r {
71  
      lblBrainSize.setText("Brain Size: " + toM(fileSize(programFile("web.structure")), 1) + " MB");
72  
    });
73  
    awtCalcEvery(lblBrainSize, 1000, 1000, r {
74  
      if (web.calculating) ret;
75  
      int webSize = web.count();
76  
      if (shouldSave(webSize, false)) saveWeb(webSize);
77  
    });
78  
  }
79  
80  
  aiEnhancements();
81  
  aiConcepts_noAutoClear();
82  
  useConceptsDump(concepts);
83  
  set englishToConceptLanguage_useBrackets;
84  
  facts = clParse(linesToCL(theFacts));
85  
  print(theFacts);
86  
  useFacts(theFacts);
87  
  //printNumberedLines(facts);
88  
  
89  
  firstRiddle();
90  
  
91  
  makeBot();
92  
  notDone();
93  
}
94  
95  
svoid firstRiddle {
96  
  print();
97  
  for (S rid : toLinesFullTrim(theRiddle)) {
98  
    print("? " + rid);
99  
    print("  " + answer(rid));
100  
  }
101  
  print();
102  
}
103  
104  
static int webSize() { ret web.count(); }
105  
106  
sbool shouldSave(int webSize, bool checkInterval) {
107  
  ret webSize != sizeSaved &&
108  
    (!checkInterval || sysNow() >= lastSave+saveInterval);
109  
}
110  
111  
svoid saveWeb(int webSize) {
112  
  save("web");
113  
  sizeSaved = webSize;
114  
  lastSave = sysNow();
115  
}
116  
117  
static S answer(S rid) {
118  
  web.calculating = true;
119  
  try {
120  
    ret answerImpl(rid);
121  
  } finally {
122  
    web.calculating = false;
123  
  }
124  
}
125  
126  
static S answerImpl(S rid) {
127  
  if (eq(rid, "web")) { print(web); ret "OK"; }
128  
  Web web = main.web;
129  
  Lisp riddle = clParse(englishToConceptLanguage(rid));
130  
  //print("# " + riddle);
131  
  
132  
  if (!riddle.is("urfkhwfasdaqqqse", "mrhggsmuykeceghd")) ret "huh?"; // What is X? // What is the decimal representation of X?
133  
  Lisp mainConcept = riddle.get(0);
134  
135  
  // Make web
136  
  web.node(mainConcept);
137  
  web.relations(facts);
138  
  
139  
  int webSize = web.count();
140  
  int cursor = 0;
141  
  while licensed {
142  
    if (web.cancelled) ret "cancelled";
143  
    //print("Web size: " + webSize);
144  
    if (shouldSave(webSize, true)) {
145  
      printWithoutNL("S");
146  
      saveWeb(webSize);
147  
    } else
148  
      printWithoutNL(".");
149  
    if ((++cursor % 60) == 0) print();
150  
    
151  
    CriticalAction action = beginCriticalAction("Web Update");
152  
    try {
153  
      web_expandMarkers2(web);
154  
      web_expandOperations2(web);
155  
156  
      for (LispStatement ls : lispStatementsByHead("mtsrzasaoxrmicch")) {
157  
        if (web.cancelled) ret "cancelled";
158  
        Lisp rule = ls.term, x = rule.get(0);
159  
        //print("Pot size: " + l(web.getPot(ls.globalID)));
160  
        for (WebNode node : web.clearPot(ls.globalID)) {
161  
          for (Lisp lbl : cloneList(node.labels)) {
162  
            //print("Matching " + x + " and " + lbl);
163  
            Map<S, Lisp> matches = lispMatch(x, lbl, ll("n"));
164  
            if (matches != null) {
165  
              //print("Rule match: " + struct(matches));
166  
              Lisp bla = lispReplaceVars(rule.get(2), matches);
167  
              if (checkACondition(bla)) {
168  
                bla = lispReplaceVars(rule.get(1), matches);
169  
                bla = lispCalculateDeep(bla);
170  
                bla = web_insertNumbers(web, bla);
171  
                //print("bla: " + bla);
172  
                bla = lispCalculateDeep(bla);
173  
                if (lispIsInt(bla)) {
174  
                  node.addLabel(bla); // Found!
175  
                  //pot.remove(node);
176  
                } else {
177  
                  //print("Condition OK! Evaluating: " + bla);
178  
                  web.node(bla);
179  
                  // Place into pot again
180  
                  web.getPot(ls.globalID).add(node);
181  
                }
182  
              }
183  
            }
184  
          }
185  
        }
186  
        //print("Node: " + node + ", condition: " + condition);
187  
      }
188  
    } finally {
189  
      action.done();
190  
    }
191  
    
192  
    int n = web.count();
193  
    if (n == webSize) break;
194  
    webSize = n;
195  
  }
196  
  print();
197  
  
198  
  // print question again
199  
  if (cursor / 60 >= 10)
200  
    print("? " + rid);
201  
    
202  
  // Answer question
203  
  L<Lisp> l = listWithout(web.findNode(mainConcept).labels, mainConcept);
204  
  //print("Labels: " + l);
205  
  S s = chooseALabelToReturn(l);
206  
  s = or2(conceptLanguageToEnglish(unquote(s)), "I don't know");
207  
  ret s;
208  
}
209  
210  
sS chooseALabelToReturn(L<Lisp> ll) {
211  
  L<S> l = map(f clUnparse, ll);
212  
  //print("  Labels: " + l);
213  
  S s = findInteger(l);
214  
  if (s == null) s = firstGlobalID(l);
215  
  //if (s == null) s = random(l);
216  
  if (s == null) ret s = first(l);
217  
  ret s;
218  
}
219  
220  
sbool checkACondition(Lisp l) false on exception {
221  
  ret lispCalculateDeep(l).is("true");
222  
}
223  
224  
svoid cleanMeUp {
225  
  while (criticalActionsInFlight()) sleep(5);
226  
  if (sizeSaved != web.count())
227  
    save("web");
228  
}
229  
230  
svoid reset {
231  
  if (!confirmOKCancel("Really clear the Fibonacci brain?")) ret;
232  
  web.cancelled = true;
233  
  CriticalAction action = beginCriticalAction("Resetting Brain");
234  
  try {
235  
    web = new Web;
236  
    sizeSaved = 0;
237  
    save("web");
238  
  } finally {
239  
    action.done();
240  
  }
241  
  clearConsole();
242  
  firstRiddle();
243  
}

Author comment

Began life as a copy of #1007674

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, wtqryiryparv

No comments. add comment

Snippet ID: #1007739
Snippet name: Demo 2: Fibonacci numbers [hacked for speed, WORKS]
Eternal ID of this version: #1007739/28
Text MD5: 64abf1834799122ed38479014277632f
Transpilation MD5: 2954377c1ddaf0925cd01059d2b42018
Author: stefan
Category: javax / a.i.
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-04-05 11:12:28
Source code size: 6761 bytes / 243 lines
Pitched / IR pitched: No / No
Views / Downloads: 636 / 1340
Version history: 27 change(s)
Referenced in: [show references]