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

245
LINES

< > BotCompany Repo | #1007765 // Demo 2: Fibonacci numbers [best version, WORKS]

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

Download Jar. Uses 4489K of libraries. Click here for Pure Java version (13337L/96K).

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

Author comment

Began life as a copy of #1007739

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: #1007765
Snippet name: Demo 2: Fibonacci numbers [best version, WORKS]
Eternal ID of this version: #1007765/7
Text MD5: 2dd319ce5de4d0c82d3c923919ddd85a
Transpilation MD5: 1c3f9845eae11ba0363f95c62d66e338
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-10 11:09:28
Source code size: 6859 bytes / 245 lines
Pitched / IR pitched: No / No
Views / Downloads: 524 / 1408
Version history: 6 change(s)
Referenced in: [show references]