-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1692 lines (1595 loc) · 71.5 KB
/
index.html
File metadata and controls
1692 lines (1595 loc) · 71.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2017-03-06 Mon 16:03 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>General Problem Solver in Common Lisp</title>
<meta name="generator" content="Org mode" />
<meta name="author" content="benrudgers" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
/* Languages per Org manual */
pre.src-asymptote:before { content: 'Asymptote'; }
pre.src-awk:before { content: 'Awk'; }
pre.src-C:before { content: 'C'; }
/* pre.src-C++ doesn't work in CSS */
pre.src-clojure:before { content: 'Clojure'; }
pre.src-css:before { content: 'CSS'; }
pre.src-D:before { content: 'D'; }
pre.src-ditaa:before { content: 'ditaa'; }
pre.src-dot:before { content: 'Graphviz'; }
pre.src-calc:before { content: 'Emacs Calc'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-fortran:before { content: 'Fortran'; }
pre.src-gnuplot:before { content: 'gnuplot'; }
pre.src-haskell:before { content: 'Haskell'; }
pre.src-java:before { content: 'Java'; }
pre.src-js:before { content: 'Javascript'; }
pre.src-latex:before { content: 'LaTeX'; }
pre.src-ledger:before { content: 'Ledger'; }
pre.src-lisp:before { content: 'Lisp'; }
pre.src-lilypond:before { content: 'Lilypond'; }
pre.src-lua:before { content: 'Lua'; }
pre.src-matlab:before { content: 'MATLAB'; }
pre.src-mscgen:before { content: 'Mscgen'; }
pre.src-ocaml:before { content: 'Objective Caml'; }
pre.src-octave:before { content: 'Octave'; }
pre.src-org:before { content: 'Org mode'; }
pre.src-oz:before { content: 'OZ'; }
pre.src-plantuml:before { content: 'Plantuml'; }
pre.src-processing:before { content: 'Processing.js'; }
pre.src-python:before { content: 'Python'; }
pre.src-R:before { content: 'R'; }
pre.src-ruby:before { content: 'Ruby'; }
pre.src-sass:before { content: 'Sass'; }
pre.src-scheme:before { content: 'Scheme'; }
pre.src-screen:before { content: 'Gnu Screen'; }
pre.src-sed:before { content: 'Sed'; }
pre.src-sh:before { content: 'shell'; }
pre.src-sql:before { content: 'SQL'; }
pre.src-sqlite:before { content: 'SQLite'; }
/* additional languages in org.el's org-babel-load-languages alist */
pre.src-forth:before { content: 'Forth'; }
pre.src-io:before { content: 'IO'; }
pre.src-J:before { content: 'J'; }
pre.src-makefile:before { content: 'Makefile'; }
pre.src-maxima:before { content: 'Maxima'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-picolisp:before { content: 'Pico Lisp'; }
pre.src-scala:before { content: 'Scala'; }
pre.src-shell:before { content: 'Shell Script'; }
pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
/* additional language identifiers per "defun org-babel-execute"
in ob-*.el */
pre.src-cpp:before { content: 'C++'; }
pre.src-abc:before { content: 'ABC'; }
pre.src-coq:before { content: 'Coq'; }
pre.src-groovy:before { content: 'Groovy'; }
/* additional language identifiers from org-babel-shell-names in
ob-shell.el: ob-shell is the only babel language using a lambda to put
the execution function name together. */
pre.src-bash:before { content: 'bash'; }
pre.src-csh:before { content: 'csh'; }
pre.src-ash:before { content: 'ash'; }
pre.src-dash:before { content: 'dash'; }
pre.src-ksh:before { content: 'ksh'; }
pre.src-mksh:before { content: 'mksh'; }
pre.src-posh:before { content: 'posh'; }
/* Additional Emacs modes also supported by the LaTeX listings package */
pre.src-ada:before { content: 'Ada'; }
pre.src-asm:before { content: 'Assembler'; }
pre.src-caml:before { content: 'Caml'; }
pre.src-delphi:before { content: 'Delphi'; }
pre.src-html:before { content: 'HTML'; }
pre.src-idl:before { content: 'IDL'; }
pre.src-mercury:before { content: 'Mercury'; }
pre.src-metapost:before { content: 'MetaPost'; }
pre.src-modula-2:before { content: 'Modula-2'; }
pre.src-pascal:before { content: 'Pascal'; }
pre.src-ps:before { content: 'PostScript'; }
pre.src-prolog:before { content: 'Prolog'; }
pre.src-simula:before { content: 'Simula'; }
pre.src-tcl:before { content: 'tcl'; }
pre.src-tex:before { content: 'TeX'; }
pre.src-plain-tex:before { content: 'Plain TeX'; }
pre.src-verilog:before { content: 'Verilog'; }
pre.src-vhdl:before { content: 'VHDL'; }
pre.src-xml:before { content: 'XML'; }
pre.src-nxml:before { content: 'XML'; }
/* add a generic configuration mode; LaTeX export needs an additional
(add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
pre.src-conf:before { content: 'Configuration File'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
.org-svg { width: 90%; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<h1 class="title">General Problem Solver in Common Lisp</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org3ef168f">PREFACE</a></li>
<li><a href="#orgb9151fc">INTRODUCTION</a>
<ul>
<li><a href="#orgc97092a">GPS Main Components</a></li>
<li><a href="#org995ce95">Goal Types</a></li>
<li><a href="#org4d98320">Heuristics</a></li>
<li><a href="#orgde86803">Executive Structure of GPS</a></li>
</ul>
</li>
<li><a href="#orgd8fc96f">PROCESS STAGES</a>
<ul>
<li><a href="#org58e0164">Description</a></li>
<li><a href="#orgf818147">Specification</a></li>
</ul>
</li>
<li><a href="#org9da3ce8">VERSION ONE</a>
<ul>
<li><a href="#org8c25e14">Implementation</a>
<ul>
<li><a href="#org41f9b72">Glossary</a></li>
<li><a href="#orga4aefba">GPS</a></li>
<li><a href="#orgabefde4">Special Variables</a></li>
<li><a href="#orgb02ce71">Op Data Structure</a></li>
<li><a href="#org85ab2de">Achieve</a></li>
<li><a href="#org8437cd6">Appropriate-p</a></li>
<li><a href="#org5aca2f5">Apply-op</a></li>
<li><a href="#orgf34e7f9">find-all</a></li>
</ul>
</li>
<li><a href="#org665c9e5">Problems</a>
<ul>
<li><a href="#org5bf779e">Running Around the Block Problem</a></li>
<li><a href="#orgd504fc1">Clobbered Sibling Goal Problem</a></li>
<li><a href="#orgff17787">Leaping Before Looking Problem</a></li>
<li><a href="#org9cc7161">Recursive Subgoals Problem</a></li>
<li><a href="#org4aaf938">Lack of Intermediate Information</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#orge70e2d8">VERSION TWO</a>
<ul>
<li><a href="#orga216add">GPS Debugger</a></li>
<li><a href="#org28827e7">Glossary</a></li>
<li><a href="#orgb18d239">GPS</a>
<ul>
<li><a href="#org51a7ce9">Simple GPS</a></li>
<li><a href="#org56544ea">GPS Updating Dynamic State</a></li>
</ul>
</li>
<li><a href="#org7999cf7">Special Variables</a></li>
<li><a href="#org7ffc1ef">Data Structure</a></li>
<li><a href="#orgec275a3">Major Functions</a>
<ul>
<li><a href="#org37ab8fb">Achieve-all</a></li>
<li><a href="#org55aaf26">Achieve</a></li>
<li><a href="#orga436992">Appropriate-p</a></li>
<li><a href="#orgd0fab7b">Apply-op</a></li>
</ul>
</li>
<li><a href="#org90ad8ed">Auxiliary Functions</a>
<ul>
<li><a href="#orgcdc0db2">executing-p</a></li>
<li><a href="#orge46896e">starts-with</a></li>
<li><a href="#org699ad04">convert-op</a></li>
<li><a href="#orgce91cc2">op</a></li>
<li><a href="#orgc21003c">use</a></li>
<li><a href="#org84a8ae5">member-equal</a></li>
</ul>
</li>
<li><a href="#org087bc37">Previously Defined Functions</a></li>
</ul>
</li>
<li><a href="#orgc037246">VERSION THREE</a>
<ul>
<li><a href="#org6eeddab">GPS</a></li>
<li><a href="#org7571a64">Action-p</a></li>
<li><a href="#orgdb9aa40">Previously defined functions</a></li>
</ul>
</li>
<li><a href="#org3ce1df4">VERSION FOUR</a>
<ul>
<li><a href="#org4c7812d">achieve-all</a></li>
<li><a href="#org7282f3e">achieve-each</a></li>
<li><a href="#org8d6f92a">orderings</a></li>
<li><a href="#org3cf78aa">achieve</a></li>
<li><a href="#orgde15e91">appropriate-ops</a></li>
</ul>
</li>
<li><a href="#org8040545">KNOWLEDGE BASES</a>
<ul>
<li><a href="#orga073ed3">Parameterize knowledge base</a></li>
<li><a href="#orgc309e21">School Ops</a></li>
<li><a href="#org59d4052">Banana Ops</a></li>
<li><a href="#org9b90e9a">Maze Ops</a>
<ul>
<li><a href="#org85a45c4">make-maze-ops</a></li>
<li><a href="#orge96de3e">make-maze-op</a></li>
<li><a href="#org947c413">Using Maze Ops in Another Program</a></li>
</ul>
</li>
<li><a href="#org7c99177">Block Ops</a>
<ul>
<li><a href="#org26d3158">make-block-ops</a></li>
<li><a href="#org2c91678">move-op</a></li>
<li><a href="#org1a490bc">move-ons</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#orgcb5b4e5">TESTING CODE</a>
<ul>
<li><a href="#org050e32e">GPSv1 School Ops</a></li>
<li><a href="#org5e88cef">GPSv2 Monkey Ops</a></li>
<li><a href="#org51d9492">GPSv3 Maze Ops</a></li>
<li><a href="#orgbd9742c">GPSv4 Block Ops</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div id="outline-container-org3ef168f" class="outline-2">
<h2 id="org3ef168f">PREFACE</h2>
<div class="outline-text-2" id="text-org3ef168f">
<p>
<b><b>The computer code here is not original to me.</b></b> and this document reflects several of my ongoing projects:
</p>
<ol class="org-ol">
<li>Learning 'classic' AI by working through Peter Norvig's <a href="http://norvig.com/paip.html"><b>Paradigms of Artificial Intelligence: Case Studies in Common Lisp</b></a>. Norvig's book is the source for the Lisp code in this project.</li>
<li>Learning more Emacs in general and org-mode in particular.</li>
<li>Developing a work process around Literate Programming.</li>
</ol>
</div>
</div>
<div id="outline-container-orgb9151fc" class="outline-2">
<h2 id="orgb9151fc">INTRODUCTION</h2>
<div class="outline-text-2" id="text-orgb9151fc">
<p>
The General Problem Solver was developed in 1957 by <a href="https://en.wikipedia.org/wiki/Allen_Newell">Alan Newell</a>, <a href="https://en.wikipedia.org/wiki/Cliff_Shaw">Cliff Shaw</a> and <a href="https://en.wikipedia.org/wiki/Herbert_A._Simon">Herbert Simon</a>. The paper associated with their initial work is <a href="http://bitsavers.informatik.uni-stuttgart.de/pdf/rand/ipl/P-1584_Report_On_A_General_Problem-Solving_Program_Feb59.pdf"><b>A Report on a General Problem Solving Program</b></a>. However, the code in this version is based on Chapter 4 of <a href="http://norvig.com/paip.html"><b>Paradigms of Artificial Intelligence: Case Studies in Common Lisp</b></a>.
</p>
<p>
Outside of historical value, the General Problem Solver is deemed a failure due to the likelihood of combinatorial explosion. But as I work through the code base, I can't help but appreciate the soundness of its program structure and logic…it was developed years before the discovery of NP-hard and it seems like an approach that at least allows for solving simple problems in a straight forward way. This is due, I think to the quality of its abstractions over state and mutating it.
</p>
</div>
<div id="outline-container-orgc97092a" class="outline-3">
<h3 id="orgc97092a">GPS Main Components</h3>
<div class="outline-text-3" id="text-orgc97092a">
<ol class="org-ol">
<li>A vocabulary for talking about the task environment: operators, objects, difference, feature, etc.</li>
<li>A vocabulary for dealing with the organization of problem-solving processes: goal, method, evaluation.</li>
<li>Programs defining the terms of the problem solving vocabulary in terms of the task environment vocabulary.</li>
<li>A set of correlative definitions applying the task-environment vocabulary to a particular domain environment.</li>
</ol>
</div>
</div>
<div id="outline-container-org995ce95" class="outline-3">
<h3 id="org995ce95">Goal Types</h3>
<div class="outline-text-3" id="text-org995ce95">
<ol class="org-ol">
<li>Transform <code>object a</code> into <code>object b</code> by applying operators from the task environment. An example is proof from axioms.</li>
<li>Apply <code>operator q</code> to <code>object a</code> or an object obtained from a by admissible transformations. An example is integrating an expression.</li>
<li>Reduce the difference (distance) between <code>object a</code> and <code>object b</code> by transforming <code>object a</code>.</li>
</ol>
</div>
</div>
<div id="outline-container-org4d98320" class="outline-3">
<h3 id="org4d98320">Heuristics</h3>
<div class="outline-text-3" id="text-org4d98320">
<p>
The main heuristic is the principle of sub-goal reduction: reduce hard goals to sets of easier goals. This assumes that the difference between the current state and the goal can be determined; operators have partial effects on the environment and some portions always remain unchanged by each operator; some differences are harder to effect than others and so it is good practice to eliminate hard differences at the cost of introducing easier differences so long as progress is made.
</p>
</div>
</div>
<div id="outline-container-orgde86803" class="outline-3">
<h3 id="orgde86803">Executive Structure of GPS</h3>
<div class="outline-text-3" id="text-orgde86803">
<p>
Make the diagram using Graphviz's Dot language.
</p>
<div class="org-src-container">
<pre class="src src-dot">digraph G {
size="5,5";
ratio=1.1;
a[label="Command to\nachieve goal", shape=plaintext];
b[label="Evaluate Goal", shape=box];
bb[label="Do not try\nto achieve", shape=plaintext];
c[label="Select method\nfor this type of goal", shape=box];
d[label="Execute method", shape=box];
e[label="Goal achieved", shape=plaintext];
splines=ortho;
{
a -> b -> c -> d -> e;
}
rank=same {b, bb}
b:e -> bb:w [label="Reject"];
d:w -> b:w [xlabel="Goal\nnot\nachieved", constraint=false];
}
</pre>
</div>
<div class="figure">
<p><img src="gps-exec.png" alt="gps-exec.png" />
</p>
</div>
<p>
<i>Source code for this diagram is written in</i> <code>graphviz</code> <i>and contained in the literate programming file.</i>
</p>
</div>
</div>
</div>
<div id="outline-container-orgd8fc96f" class="outline-2">
<h2 id="orgd8fc96f">PROCESS STAGES</h2>
<div class="outline-text-2" id="text-orgd8fc96f">
</div><div id="outline-container-org58e0164" class="outline-3">
<h3 id="org58e0164">Description</h3>
<div class="outline-text-3" id="text-org58e0164">
<p>
The description of problem solving uses the theory of means-ends analysis. The problem is stated in terms of what we want to happen and look at the difference between what we want and what we have. Some actions have preconditions.
</p>
</div>
</div>
<div id="outline-container-orgf818147" class="outline-3">
<h3 id="orgf818147">Specification</h3>
<div class="outline-text-3" id="text-orgf818147">
<p>
The specification has seven parts.
</p>
<ul class="org-ul">
<li>Represent the current state of the world (environment) as a set of conditions. Represent the desired state of the world as a set of conditions.</li>
<li>A list of allowable operators.</li>
<li>Each operator is: a list of preconditions, an action, a list of effects.</li>
<li>A complete problem is a starting state, a goal state, a set of known operators.</li>
<li>A goal condition can be achieved two ways: it is in the current state or by applying an operator to the current environment.</li>
<li>An operator is appropriate if it adds the goal to the environment.</li>
<li>An operator can be applied if all the preconditions for its application can be met.</li>
</ul>
</div>
</div>
</div>
<div id="outline-container-org9da3ce8" class="outline-2">
<h2 id="org9da3ce8">VERSION ONE</h2>
<div class="outline-text-2" id="text-org9da3ce8">
<p>
This is the first iteration of GPS from Norvig's book. It is an exercise to get something to run, but it has problems that are discussed at the end of this section. There is a second version of GPS provided in the next section.
</p>
</div>
<div id="outline-container-org8c25e14" class="outline-3">
<h3 id="org8c25e14">Implementation</h3>
<div class="outline-text-3" id="text-org8c25e14">
</div><div id="outline-container-org41f9b72" class="outline-4">
<h4 id="org41f9b72">Glossary</h4>
<div class="outline-text-4" id="text-org41f9b72">
<!-- This HTML table template is generated by emacs 25.1.1 -->
<table border="1">
<tr>
<td align="left" valign="top">
*Top Level Function* <br />
GPS Solve a goal from a state <br />
using a list of operators.
</td>
</tr>
</table>
</div>
</div>
<div id="outline-container-orga4aefba" class="outline-4">
<h4 id="orga4aefba">GPS</h4>
<div class="outline-text-4" id="text-orga4aefba">
<p>
For literate programming purposes the source block name had to use 'one' instead of '1' to work with <code>:noweb</code>.
</p>
<div class="org-src-container">
<pre class="src src-lisp" id="org186f302">;;; THIS FILE AUTOGENERATATED
;;; Helper Function
<<find-all-v-one>>
;;; Special Variables
<<special-variables-v-one>>
;;; Operations
<<op-v-one>>
;;; Main
(defun gps (*state* goals *ops*)
"General Problem Solver: achieve all goals using *ops*."
(if (every #'achieve goals) 'solved))
;;; Functions
<<achieve-v-one>>
<<appropriate-p-v-one>>
<<apply-op-v-one>>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgabefde4" class="outline-4">
<h4 id="orgabefde4">Special Variables</h4>
<div class="outline-text-4" id="text-orgabefde4">
<div class="org-src-container">
<pre class="src src-lisp" id="orgcf92bd9">(defvar *state* nil "The current state: a list of all conditions.")
(defvar *ops* nil "A list of available operations.")
</pre>
</div>
</div>
</div>
<div id="outline-container-orgb02ce71" class="outline-4">
<h4 id="orgb02ce71">Op Data Structure</h4>
<div class="outline-text-4" id="text-orgb02ce71">
<p>
Note that <code>nil</code> is the default value for each field of the <code>op</code> structure.
</p>
<div class="org-src-container">
<pre class="src src-lisp" id="org26a77dc">(defstruct op
"An operation."
(action nil)
(preconds nil)
(add-list nil)
(del-list nil))
</pre>
</div>
</div>
</div>
<div id="outline-container-org85ab2de" class="outline-4">
<h4 id="org85ab2de">Achieve</h4>
<div class="outline-text-4" id="text-org85ab2de">
<div class="org-src-container">
<pre class="src src-lisp" id="org768f358">(defun achieve (goal)
"A goal is achieved if it already holds. Or if there
is an appropriate op for it that is applicable."
(or (member goal *state*)
(some #'apply-op
(find-all goal *ops* :test #'appropriate-p))))
</pre>
</div>
</div>
</div>
<div id="outline-container-org8437cd6" class="outline-4">
<h4 id="org8437cd6">Appropriate-p</h4>
<div class="outline-text-4" id="text-org8437cd6">
<div class="org-src-container">
<pre class="src src-lisp" id="org2790f27">(defun appropriate-p (goal op)
"An op is appropriate to a goal if the goal is on the op's add-list."
(member goal (op-add-list op)))
</pre>
</div>
</div>
</div>
<div id="outline-container-org5aca2f5" class="outline-4">
<h4 id="org5aca2f5">Apply-op</h4>
<div class="outline-text-4" id="text-org5aca2f5">
<div class="org-src-container">
<pre class="src src-lisp" id="orgf3de82f">(defun apply-op (op)
"Print a message and update *state* when op is applicable."
(when (every #'achieve (op-preconds op))
(print (list 'executing (op-action op)))
(setf *state* (set-difference *state* (op-del-list op)))
(setf *state* (union *state* (op-add-list op)))))
</pre>
</div>
</div>
</div>
<div id="outline-container-orgf34e7f9" class="outline-4">
<h4 id="orgf34e7f9">find-all</h4>
<div class="outline-text-4" id="text-orgf34e7f9">
<p>
<code>FIND-ALL</code> is introduced early in the book and provides an example of a user function that has many of the features of a Common Lisp built-in function.
</p>
<div class="org-src-container">
<pre class="src src-lisp" id="org27987b8">(defun find-all (item sequence &rest keyword-args
&key (test #'eql)
test-not
&allow-other-keys)
"Find all those elements of sequence that match item.
according to the keywords. Does not alter sequence"
(if test-not
(apply #'remove item sequence
:test-not (complement test-not) keyword-args)
(apply #'remove item sequence
:test (complement test) keyword-args)))
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-org665c9e5" class="outline-3">
<h3 id="org665c9e5">Problems</h3>
<div class="outline-text-3" id="text-org665c9e5">
</div><div id="outline-container-org5bf779e" class="outline-4">
<h4 id="org5bf779e">Running Around the Block Problem</h4>
<div class="outline-text-4" id="text-org5bf779e">
<p>
Version One does not handle the run for the side effects case where the start state and the end state are the same…e.g. if we run around the block for exercise.
</p>
</div>
</div>
<div id="outline-container-orgd504fc1" class="outline-4">
<h4 id="orgd504fc1">Clobbered Sibling Goal Problem</h4>
<div class="outline-text-4" id="text-orgd504fc1">
<p>
The code only checks if each goal requirement is achieved at some point, therefore
</p>
<div class="org-src-container">
<pre class="src src-lisp">(gps '(son-at-home car-needs-battery have-money have-phone-book)
'(have-money son-at-school)
*school-ops*)
=> SOLVED
</pre>
</div>
<p>
Despite repairing the car using all the money.
</p>
</div>
</div>
<div id="outline-container-orgff17787" class="outline-4">
<h4 id="orgff17787">Leaping Before Looking Problem</h4>
<div class="outline-text-4" id="text-orgff17787">
<p>
GPS will choose a course of action and continue it regardless of whether it will solve the problem. Even worse, it will not back up and try a different approach after hitting a dead end.
</p>
</div>
</div>
<div id="outline-container-org9cc7161" class="outline-4">
<h4 id="org9cc7161">Recursive Subgoals Problem</h4>
<div class="outline-text-4" id="text-org9cc7161">
<p>
It is possible to create operations which will send GPS into an infinite loop.
</p>
</div>
</div>
<div id="outline-container-org4aaf938" class="outline-4">
<h4 id="org4aaf938">Lack of Intermediate Information</h4>
<div class="outline-text-4" id="text-org4aaf938">
<p>
GPS will report failure but only as <code>NIL</code>. It will not report what it tried.
</p>
</div>
</div>
</div>
</div>
<div id="outline-container-orge70e2d8" class="outline-2">
<h2 id="orge70e2d8">VERSION TWO</h2>
<div class="outline-text-2" id="text-orge70e2d8">
<p>
At one level, adding GPSv2 into this same source file is kind of a mess. But it makes for a richer exercise from an Emacs/Org-Babel/Literate Programming perspective. It certainly pushes me to understand more about the topic and makes for a more robust documentation of the project's development.
</p>
<div class="org-src-container">
<pre class="src src-lisp" id="orge195196">;; This file was auto generated from
;; general-problem-solver.org
<<load-gps-debugger-v-two>>
<<gps-v-two-simple>>
<<special-variables-v-two>>
<<op-v-two>>
<<major-functions-v-two>>
<<auxiliary-functions-v-two>>
<<previously-defined-functions-v-two>>
</pre>
</div>
</div>
<div id="outline-container-orga216add" class="outline-3">
<h3 id="orga216add">GPS Debugger</h3>
<div class="outline-text-3" id="text-orga216add">
<p>
The first of the problems with <code>Version One</code> to be addressed is the Lack of Intermediate Information. Using a debugger seems like a reasonable approach: <code>using-a-debugger</code> requires <code>having-a-debugger</code> and <code>having-a-debugger</code> requires <code>writing-a-debugger</code>. It's what I love about this book, it's GPS all the way down.
</p>
<p>
The debugger is named <code>gps-debug</code> to avoid conflict with SBCL. To use <code>(gps-debug :gps :some-other-symbol :etc)</code>. To turn debugging off <code>(gps-debug)</code>.
</p>
<div class="org-src-container">
<pre class="src src-lisp" id="org0a533f4">(defvar *dbg-ids* nil
"Identifiers used by dbg")
(defun dbg (id format-string &rest args)
"Print debugging information if (DEBUG-ID) has been specified."
(when (member id *dbg-ids*)
(fresh-line *debug-io*)
(apply #'format *debug-io* format-string args)))
;;; Using gps-debug because 'debug' reserved in SBCL
(defun gps-debug (&rest ids)
"Start dbg output on the given ids."
(setf *dbg-ids* (union ids *dbg-ids*)))
(defun undebug (&rest ids)
"Stop dbg on the ids. With no ids, stop debugging altogether."
(setf *dbg-ids* (if (null ids) nil
(set-difference *dbg-ids* ids))))
(defun dbg-indent (id indent format-string &rest args)
"Print indented debugging info if (DEBUG ID) has been specified."
(when (member id *dbg-ids*)
(fresh-line *debug-io*)
(dotimes (i indent) (princ " " *debug-io*))
(apply #'format *debug-io* format-string args)))
</pre>
</div>
<p>
For the current project, I am just going to <code>load</code> the debugger into GPSv2 rather than setting up ASDF…because my head is not quite wrapped around ASDF yet in terms of how it would work with this project.
</p>
<div class="org-src-container">
<pre class="src src-lisp" id="org4837a1d">(load "gps-debugger.lisp")
</pre>
</div>
</div>
</div>
<div id="outline-container-org28827e7" class="outline-3">
<h3 id="org28827e7">Glossary</h3>
<div class="outline-text-3" id="text-org28827e7">
<!-- This HTML table template is generated by emacs 25.1.1 -->
<table border="1">
<tr>
<td align="left" valign="top">
*Top Level Function* <br />
GPS Solve a goal from a state using a list of operators.
</td>
</tr>
<tr>
<td align="left" valign="top">
*Special Variables* <br />
*ops* A List of available operators.
</td>
</tr>
<tr>
<td align="left" valign="top">
*Data Types* <br />
op An operation with preconds, add-list, del-list.
</td>
</tr>
<tr>
<td align="left" valign="top">
*Major Functions* <br />
achieve-all Achieve a list of goals. <br />
achieve Achieve a single goal. <br />
appropriate-p Decide if an operator is appropriate to a goal. <br />
apply-op Apply operator to current state.
</td>
</tr>
<tr>
<td align="left" valign="top">
*Auxiliary Functions* <br />
executing-p Is condition an executing form? <br />
starts-with Is the argument a list that starts with a given atom? <br />
convert-op Convert an operator to use the executing convention. <br />
op Create an operator. <br />
use Use a list of operators. <br />
member-equal Test if an element is equal to a member of a list.
</td>
</tr>
<tr>
<td align="left" valign="top">
*Selected Common Lisp Functions* <br />
member Test if an element is a member of a list. <br />
set-difference All elements in one set but not in the other. <br />
subsetp Is one set wholly contained in another? <br />
union All elements in either of two sets. <br />
every Test if every element of a list passes test. <br />
some Test if any element of a list passes test. <br />
remove-if Remove all items satisfying a test.
</td>
</tr>
</table>
</div>
</div>
<div id="outline-container-orgb18d239" class="outline-3">
<h3 id="orgb18d239">GPS</h3>
<div class="outline-text-3" id="text-orgb18d239">
<p>
Norvig provides two alternative implementations of <code>gps</code> in GPSv2. The first is largely similar to GPSv1 with the addition of the goal stack and a <code>start</code> state and treating the parameters as an optional argument which otherwise defaults to the dynamic scope of <code>*ops*</code>. I think adding a <code>start</code> state makes GPS more straight forward in terms of automata theory.
</p>
</div>
<div id="outline-container-org51a7ce9" class="outline-4">
<h4 id="org51a7ce9">Simple GPS</h4>
<div class="outline-text-4" id="text-org51a7ce9">
<div class="org-src-container">
<pre class="src src-lisp" id="orgf339e76">(defun gps (state goals &optional (*ops* *ops*))
"General Problem Solver: from state achieves goals using *ops*."
(remove-if #'atom (achieve-all (cons '(start) state) goals nil)))
</pre>
</div>
</div>
</div>
<div id="outline-container-org56544ea" class="outline-4">
<h4 id="org56544ea">GPS Updating Dynamic State</h4>
<div class="outline-text-4" id="text-org56544ea">
<p>
The second implementation of GPSv2, in addition to accepting the operations as an optional argument also updates the global parameter <code>*ops*</code> to match the argument if it is supplied. Norvig says this is more complicated but useful without explaining why. I suspect it is more useful in the case of more advanced agents (or multiple agents) where the availble operations change during program execution.
</p>
<p>
As I was entering the code, I kind of choked for a second on the documentation string; thinking shouldn't it be 'using ops'. But then I realized that since <code>*ops*</code> quickly becomes <code>ops</code>, it made sense. It's the sort of subtle understanding that only comes from really working with the code in a textbook. No matter how much I read, without typing, I'd never have had that thought. Then after typing the code, I saw that this version cleans up after itself by restoring the old value of <code>*ops*</code> before returning the result. It's a sort of manual shadowing of variables. Probably problematic in a concurrent environment, but one <i>general problem</i> at a time.
</p>
<div class="org-src-container">
<pre class="src src-lisp" id="org965c34f">(defun gps (state goals &optional (ops *ops*))
"General Problem Solver: from state achieve goals using *ops*."
(let ((lod-ops *ops*))
(setf *ops* ops)
(let ((result (remove-if #'atom
(achieve-all (cons '(start) state)
goals
nil))))
(setf *ops* old-ops)
result)))
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-org7999cf7" class="outline-3">
<h3 id="org7999cf7">Special Variables</h3>
<div class="outline-text-3" id="text-org7999cf7">
<p>
GPS Version Two handles state differently than version one and so only requires the <code>*ops*</code> parameter. The <code>*ops*</code> parameter allows the creation of different applications based on the use of different operations. This ability is what puts the 'general' in GPS.
</p>
<div class="org-src-container">
<pre class="src src-lisp" id="org726f485">(defvar *ops* nil "A list of available operators.")
</pre>
</div>
</div>
</div>
<div id="outline-container-org7ffc1ef" class="outline-3">
<h3 id="org7ffc1ef">Data Structure</h3>
<div class="outline-text-3" id="text-org7ffc1ef">
<p>
The <code>op</code> data structure is not changed from version one so we can reuse it. But I'll still make a source code block to make changes easier in the future.
</p>
<div class="org-src-container">
<pre class="src src-lisp" id="orga7f2fd4"><<op-v-one>>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgec275a3" class="outline-3">
<h3 id="orgec275a3">Major Functions</h3>
<div class="outline-text-3" id="text-orgec275a3">
<div class="org-src-container">
<pre class="src src-lisp" id="org1fd9029">;;; Major Functions
<<achieve-all-v-two>>
<<achieve-v-two>>
<<appropriate-p-v-two>>
<<apply-op-v-two>>
</pre>
</div>
<p>
The only additional function for GPS Version 2 is the addition of <code>achieve-all</code>. However, the other functions: <code>achieve</code>, <code>appropriate-p</code>, and <code>apply-op</code> are modified.
</p>
</div>
<div id="outline-container-org37ab8fb" class="outline-4">
<h4 id="org37ab8fb">Achieve-all</h4>
<div class="outline-text-4" id="text-org37ab8fb">
<p>
Achieve all is Norvig's solution to the clobbered sibling goal. It makes ensures that after achieving the goals, all the goals are still in the current state of the world.
</p>
<div class="org-src-container">
<pre class="src src-lisp" id="org0580202">(defun achieve-all (state goals goal-stack)
"Try to achieve each goal and make sure each still holds at the end."
(let ((current-state state))
(if (and (every #'(lambda (g)
(setf current-state
(achieve current-state g goal-stack)))
goals)
(subsetp goals current-state :test #'equal))
current-state)))
</pre>
</div>
</div>
</div>
<div id="outline-container-org55aaf26" class="outline-4">
<h4 id="org55aaf26">Achieve</h4>
<div class="outline-text-4" id="text-org55aaf26">
<p>
The new version of <code>achieve</code> is designed to handle the recursive subgoal problem. It uses a 'goal stack' to check if the current goal has become a sub-goal of itself. It also adds debugging.
</p>
<div class="org-src-container">
<pre class="src src-lisp" id="org1d36819">(defun achieve (state goal goal-stack)
"A goal is achieved if:\n It already holds\n Or\n There is an applicable appropriate op."
(dbg-indent :gps (length goal-stack) "Goal: ~a" goal)
(cond ((member-equal goal state) state)
((member-equal goal goal-stack) nil)
(t (some #'(lambda (op)
(apply-op state goal op goal-stack))
(find-all goal *ops* :test #'appropriate-p)))))
</pre>
</div>
</div>
</div>
<div id="outline-container-orga436992" class="outline-4">
<h4 id="orga436992">Appropriate-p</h4>
<div class="outline-text-4" id="text-orga436992">
<p>
The change for GPSv2 is to use <a href="#orgb57963d"><code>member-equal</code></a> when looking for a goal in an <code>op-add-list</code>. The problem arises because in GPLv2 goals can be lists rather than symbols: for example <code>'(executing run-around-block)</code>.
</p>
<div class="org-src-container">
<pre class="src src-lisp" id="orgd2753b1">(defun appropriate-p (goal op)
"An op is approriate to a goal if the goal is in the op's add list."
(member-equal goal (op-add-list op)))
</pre>
</div>
</div>
</div>
<div id="outline-container-orgd0fab7b" class="outline-4">
<h4 id="orgd0fab7b">Apply-op</h4>
<div class="outline-text-4" id="text-orgd0fab7b">
<p>
GPLv2 uses a more sophisticated application mechanism and most of it is reflected in changes to <code>apply-op</code>. One the one hand, the mechanism is more 'functional': <code>apply-op</code> now takes a state and returns a state rather than mutating a global state. On the other hand, the logic runs deeper and does more.
</p>
<p>
It also adds debugging and replaces the set oriented functions <code>union</code> and <code>set-diff</code> with order preserving functions <code>append</code> and <code>remove-if</code>. Once the 'executing' syntax was added, time became a bigger part of the model.
</p>
<div class="org-src-container">
<pre class="src src-lisp" id="org351d634">(defun apply-op (state goal op goal-stack)
"Return a new state that is a transformation of the input state when op is applicable."
(dbg-indent :gps (length goal-stack) "Consider: ~a" (op-action op))
(let ((state2 (achieve-all state
(op-preconds op)
(cons goal goal-stack))))
(unless (null state2)
;; return an updated state
(dbg-indent :gps (length goal-stack) "Action: ~a" (op-action op))
(append (remove-if #'(lambda (x)
(member-equal x (op-del-list op)))
state2)
(op-add-list op)))))
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-org90ad8ed" class="outline-3">
<h3 id="org90ad8ed">Auxiliary Functions</h3>
<div class="outline-text-3" id="text-org90ad8ed">
<p>
These functions implement a new form for ops so that the action list contains an "executing" action to address the running around the block problem. There is also a conversion function to back port the changed structure to <b>school-ops</b>.
</p>
<div class="org-src-container">
<pre class="src src-lisp" id="org695c684"><<executing-p-v-two>>
<<starts-with-v-two>>
<<convert-op-v-two>>