-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.php
More file actions
855 lines (676 loc) · 20.4 KB
/
solver.php
File metadata and controls
855 lines (676 loc) · 20.4 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
<?php
define('STATE_UNDEFINED', 'undefined');
/**
* A rule which allows you to find a fact.
*
* <rule>
* [<description>]
* <if/>
* <then/>
* </rule>
*/
class Rule
{
public $inferred_facts;
public $description;
public $condition;
public $consequences = array();
public $priority = 0;
public $line_number;
public function __construct()
{
$this->inferred_facts = new Set();
}
public function infers($fact)
{
return $this->inferred_facts->contains($fact);
}
public function __toString()
{
return sprintf('[Rule "%s" (line %d)]',
$this->description,
$this->line_number);
}
}
/**
* A question which can answer one of the $inferred_facts.
*
* <question>
* <description/>
* <option/>
* </question>
*/
class Question
{
public $inferred_facts;
public $description;
public $options = array();
public $priority = 0;
public $line_number;
public function __construct()
{
$this->inferred_facts = new Set();
}
public function infers($fact)
{
return $this->inferred_facts->contains($fact);
}
public function __toString()
{
return sprintf('[Question: %s]', $this->description);
}
}
class AskedQuestion extends Question
{
public $skippable;
public function __construct(Question $question, $skippable)
{
$this->description = $question->description;
$this->options = $question->options;
$this->skippable = $skippable;
}
}
/**
* Een mogelijk antwoord op een Question.
*
* <option>
* <description/>
* <then/>
* </option>
*/
class Option
{
public $description;
public $consequences = array();
}
interface Condition
{
public function evaluate(KnowledgeState $state);
public function asArray();
}
/**
* <and>
* Conditions, e.g. <fact/>
* </and>
*/
class WhenAllCondition implements Condition
{
public $conditions;
public function __construct()
{
$this->conditions = new Set();
}
public function addCondition(Condition $condition)
{
$this->conditions->push($condition);
}
public function evaluate(KnowledgeState $state)
{
// assumptie: er moet ten minste één conditie zijn
assert('count($this->conditions) > 0');
$values = array();
foreach ($this->conditions as $condition)
$values[] = $condition->evaluate($state);
// Als er minstens één Nee bij zit, dan iig niet.
$nos = array_filter_type('No', $values);
if (count($nos) > 0)
return No::because($nos);
// Als er een maybe in zit, dan nog steeds onzeker.
$maybes = array_filter_type('Maybe', $values);
if (count($maybes) > 0)
return Maybe::because($maybes);
return Yes::because($values);
}
public function asArray()
{
return array($this, array_map_method('asArray', $this->conditions));
}
}
class FrequencyCondition implements Condition
{
public $conditions;
public $count;
public function __construct($count)
{
$this->count=$count;
$this->conditions = new Set();
}
public function addCondition(Condition $condition)
{
$this->conditions->push($condition);
}
public function evaluate(KnowledgeState $state)
{
// assumptie: er moet ten minste één conditie zijn
assert('count($this->conditions) > 0');
$values = array();
foreach ($this->conditions as $condition)
$values[] = $condition->evaluate($state);
$yes = array_filter_type('Yes', $values);
if (count($yes) >= $this->count)
return Yes::because($yes);
$maybe = array_filter_type('Maybe', $values);
//als er nog genoeg maybes zijn om nog een yes te krijgen.
if ((count($maybe)+count($yes))>=($this->count))
return Maybe::because($maybe);
return No::because($values);
}
public function asArray()
{
return array($this, array_map_method('asArray', $this->conditions));
}
}
/**
* <or>
* Conditions, e.g. <fact/>
* </or>
*/
class WhenAnyCondition implements Condition
{
public $conditions;
public function __construct()
{
$this->conditions = new Set();
}
public function addCondition(Condition $condition)
{
$this->conditions->push($condition);
}
public function evaluate(KnowledgeState $state)
{
// assumptie: er moet ten minste één conditie zijn
assert('count($this->conditions) > 0');
$values = array();
foreach ($this->conditions as $condition)
$values[] = $condition->evaluate($state);
// Is er een ja, dan is dit zeker goed.
$yesses = array_filter_type('Yes', $values);
if ($yesses)
return Yes::because($yesses);
// Is er een misschien, dan zou dit ook goed kunnen zijn
$maybes = array_filter_type('Maybe', $values);
if ($maybes)
return Maybe::because($maybes);
// Geen ja's, geen misschien's, dus alle condities gaven No terug.
return No::because($values);
}
public function asArray()
{
return array($this, array_map_method('asArray', $this->conditions));
}
}
/**
* <not>
* Condition, e.g. <fact/>
* </not>
*/
class NegationCondition implements Condition
{
public $condition;
public function __construct(Condition $condition)
{
$this->condition = $condition;
}
public function evaluate(KnowledgeState $state)
{
return $this->condition->evaluate($state)->negate();
}
public function asArray()
{
return array($this, $this->condition->asArray());
}
}
/**
* <fact name="fact_name">value</fact>
*/
class FactCondition implements Condition
{
public $name;
public $value;
public function __construct($name, $value)
{
$this->name = trim($name);
$this->value = trim($value);
}
public function evaluate(KnowledgeState $state)
{
$state_value = $state->value($this->name);
if ($state_value instanceof Maybe)
return $state_value;
return $state_value == $this->value
? Yes::because([$this->name])
: No::because([$this->name]);
}
public function asArray()
{
return array($this);
}
}
/**
* Voor het gemak kan je ook goals in je knowledge base voor programmeren.
* Als je dan main.php zonder te bewijzen goal aanroept gaat hij al deze
* goals proberen af te leiden.
*
* <goal name="">
* <description/>
* <answer/>
* </goal>
*/
class Goal
{
public $name;
public $description;
public $answers;
public function __construct()
{
$this->answers = new Set();
}
public function answer(KnowledgeState $state)
{
$state_value = $state->value($this->name);
foreach ($this->answers as $answer)
{
$answer_value = $answer->value;
// If this is the default option, return it always.
if ($answer_value === null)
return $answer;
// If the value is a variable, try to resolve it.
if (KnowledgeState::is_variable($answer_value))
$answer_value = $state->resolve($answer_value);
if ($state_value == $answer_value)
return $answer;
}
// We didn't find an appropriate answer :O
return null;
}
}
/**
* <answer value="value">message</answer>
*/
class Answer
{
public $value;
public $description;
}
abstract class TruthState
{
public $factors;
public function __construct(Traversable $factors)
{
$this->factors = $factors;
}
public function __toString()
{
return sprintf("[%s because: %s]",
get_class($this),
implode(', ', array_map('strval', iterator_to_array($this->factors))));
}
abstract public function negate();
static public function because($factors = null)
{
if (is_null($factors))
$factors = new EmptyIterator();
elseif (is_scalar($factors))
$factors = new ArrayIterator([$factors]);
elseif (is_array($factors))
$factors = new ArrayIterator($factors);
$called_class = get_called_class();
return new $called_class($factors);
}
}
class Yes extends TruthState
{
public function negate()
{
return new No($this->factors);
}
}
class No extends TruthState
{
public function negate()
{
return new Yes($this->factors);
}
}
class Maybe extends TruthState
{
public function negate()
{
return new Maybe($this->factors);
}
public function causes()
{
// Hier wordt de volgorde van de vragen effectief bepaald!
// We kijken naar alle factoren die ervoor zorgden dat de vraag niet
// beantwoord kon worden, welke het meest invloedrijk is, en sorteren
// daarop om te zien waar we verder mee moeten.
// (Deze implementatie is zeker voor verbetering vatbaar.)
$causes = $this->divideAmong(1.0, $this->factors)->data();
// grootst verantwoordelijk ontbrekend fact op top.
asort($causes);
$causes = array_reverse($causes);
return array_keys($causes);
}
private function divideAmong($percentage, Traversable $factors)
{
$effects = new Map(0.0);
// als er geen factors zijn, dan heeft het ook geen zin
// de verantwoordelijkheid per uit te rekenen.
if (count($factors) == 0)
return $effects;
// iedere factor op hetzelfde niveau heeft evenveel invloed.
$percentage_per_factor = $percentage / count($factors);
foreach ($factors as $factor)
{
// recursief de hoeveelheid invloed doorverdelen en optellen bij het totaal per factor.
if ($factor instanceof TruthState)
foreach ($this->divideAmong($percentage_per_factor, $factor->factors) as $factor_name => $effect)
$effects[$factor_name] += $effect;
else
$effects[$factor] += $percentage_per_factor;
}
return $effects;
}
}
class KnowledgeDomain
{
public $values;
public function __construct()
{
$this->values = new Map(function() {
return new Set();
});
}
static public function deduceFromState(KnowledgeState $state)
{
$domain = new self();
// Obtain all the FactConditions from the rules that test
// or conclude some value.
foreach ($state->rules as $rule)
{
$fact_conditions = array_filter_type('FactCondition',
array_flatten($rule->condition->asArray()));
foreach ($fact_conditions as $condition)
$domain->values[$condition->name]->push($condition->value);
foreach ($rule->consequences as $fact_name => $value)
$domain->values[$fact_name]->push($value);
}
// Obtain all the possible answers from the questions
foreach ($state->questions as $question)
foreach ($question->options as $option)
foreach ($option->consequences as $fact_name => $value)
$domain->values[$fact_name]->push($value);
return $domain;
}
}
/**
* Een knowledge base op een bepaald moment. Via KnowledgeState::apply kunnen er
* nieuwe feiten aan de state toegevoegd worden (en wordt het stieken een nieuwe
* state).
*/
class KnowledgeState
{
public $title;
public $description;
public $facts;
public $rules;
public $questions;
public $goals;
public $solved;
public $goalStack;
public function __construct()
{
$this->facts = array(
'undefined' => STATE_UNDEFINED
);
$this->rules = new Set();
$this->questions = new Set();
$this->goals = new Set();
$this->solved = new Set();
$this->goalStack = new Stack();
}
/**
* Past $consequences toe op de huidige $state, en geeft dat als nieuwe state terug.
* Alle $consequences krijgen $reason als reden mee.
*
* @return KnowledgeState
*/
public function apply(array $consequences)
{
$this->facts = array_merge($this->facts, $consequences);
}
public function value($fact_name)
{
$fact_name = $this->resolve($fact_name);
if (!isset($this->facts[$fact_name]))
return Maybe::because([$fact_name]);
return $this->resolve($this->facts[$fact_name]);
}
public function resolve($value)
{
$stack = array();
while (self::is_variable($value))
{
if (in_array($value, $stack))
throw new RuntimeException("Infinite recursion when trying to retrieve fact '$value' after I retrieved " . implode(', ', $stack) . ".");
$stack[] = $value;
if (isset($this->facts[self::variable_name($value)]))
$value = $this->facts[self::variable_name($value)];
else
return self::variable_name($value);
}
return $value;
}
public function substitute_variables($text, $formatter = null)
{
$callback = function($match) use ($formatter) {
$value = $this->value($match[1]);
if ($value instanceof Maybe)
return $match[0];
if ($formatter)
$value = call_user_func_array($formatter, [$value]);
return $value;
};
return preg_replace_callback('/\$([a-z][a-z0-9_]*)\b/i', $callback, $text);
}
static public function is_variable($fact_name)
{
return substr($fact_name, 0, 1) == '$';
}
static public function variable_name($fact_name)
{
return substr($fact_name, 1); // strip of the $
}
static public function is_default_fact($fact_name)
{
$empty_state = new self();
return isset($empty_state->facts[$fact_name]);
}
}
/**
* Solver is een forward & backward chaining implementatie die op basis van
* een knowledge base (een berg regels, mogelijke vragen en gaandeweg feiten)
* blijft zoeken, regels toepassen en vragen kiezen totdat alle goals opgelost
* zijn. Gebruik Solver::solveAll(state) tot deze geen vragen meer teruggeeft.
*/
class Solver
{
protected $log;
public function __construct(Logger $log = null)
{
$this->log = $log;
}
/**
* Probeer gegeven een initiële $knowledge state en een lijst van $goals
* zo veel mogelijk $goals op te lossen. Dit doet hij door een stack met
* goals op te lossen. Als een goal niet op te lossen is, kijkt hij naar
* de meest primaire reden waarom (Maybe::$factors) en voegt hij die factor
* op top van de goal stack.
* Als een goal niet op te lossen is omdat er geen vragen/regels meer voor
* zijn geeft hij een Notice en gaat hij verder met de andere goals op de
* stack.
*
* @param KnowledgeState $knowledge begin-state
* @return AskedQuestion | null
*/
public function solveAll(KnowledgeState $state)
{
// herhaal zo lang er goals op de goal stack zitten
while (!$state->goalStack->isEmpty())
{
$this->log('Trying to solve %s', [$state->goalStack->top()]);
// probeer het eerste goal op te lossen
$result = $this->solve($state, $state->goalStack->top());
// Oh, dat resulteerde in een vraag. Stel hem (of geef hem terug om
// de interface hem te laten stellen eigenlijk.)
if ($result instanceof AskedQuestion)
{
$this->log('This resulted in the question %s', [$result]);
return $result;
}
// Goal is niet opgelost, het antwoord is nog niet duidelijk.
elseif ($result instanceof Maybe)
{
// waarom niet? $causes bevat een lijst van facts die niet
// bekend zijn, dus die willen we proberen op te lossen.
$causes = $result->causes();
$this->log('But I cannot, because the facts %s are not known yet', [implode(', ', $causes)]);
// echo '<pre>', print_r($causes, true), '</pre>';
// er zijn facts die nog niet zijn afgeleid
while (count($causes) > 0)
{
// neem het meest invloedrijke fact, leidt dat af
$main_cause = array_shift($causes);
// meest invloedrijke fact staat al op todo-lijst?
// sla het over.
// TODO: misschien beter om juist naar de top te halen?
// en dan dat opnieuw proberen te bewijzen?
if (iterator_contains($state->goalStack, $main_cause))
continue;
// Het kan niet zijn dat het al eens is opgelost. Dan zou hij
// in facts moeten zitten.
assert('!$state->solved->contains($main_cause)');
// zet het te bewijzen fact bovenaan op de todo-lijst.
$state->goalStack->push($main_cause);
$this->log('I added %s to the goal stack. The stack is now %s', [$main_cause, $state->goalStack]);
// .. en spring terug naar volgende goal op goal-stack!
continue 2;
}
// Er zijn geen redenen waarom het goal niet afgeleid kon worden? Ojee!
if (count($causes) == 0)
{
// Haal het onbewezen fact van de todo-lijst
$unsatisfied_goal = $state->goalStack->pop();
$this->log('I mark %s as a STATE_UNDEFINED because I do not know its value ' .
'but there are also no rules or questions which I can use to infer it.', [$unsatisfied_goal], LOG_LEVEL_WARNING);
// en markeer hem dan maar als niet waar (closed-world assumption?)
$state->apply(array($unsatisfied_goal => STATE_UNDEFINED));
// compute the effects of this change by applying the other rules
$this->forwardChain($state);
$state->solved->push($unsatisfied_goal);
}
}
// Yes, het is gelukt om een Yes of No antwoord te vinden voor dit goal.
// Mooi, dan kan dat van de te bewijzen stack af.
else
{
$this->log('Inferred %s to be %s and removed it from the goal stack.', [$state->goalStack->top(), $result]);
// aanname: als het goal kon worden afgeleid, dan is het nu deel van
// de afgeleide kennis.
assert('isset($state->facts[$state->goalStack->top()])');
// op naar het volgende goal.
$state->solved->push($state->goalStack->pop());
$this->log('The goal stack is now %s', [$state->goalStack]);
}
}
}
/**
* Solve probeert één $goal op te lossen door regels toe te passen of
* relevante vragen te stellen. Als het lukt een regel toe te passen of
* een vraag te stellen geeft hij een nieuwe $state terug. Ook geeft hij
* de TruthState voor $goal terug. In het geval van Maybe kan dat gebruikt
* worden om af te leiden welk $goal als volgende moet worden afgeleid om
* verder te komen.
*
* @param KnowledgeState $state huidige knowledge state
* @param string goal naam van het fact dat wordt afgeleid
* @return TruthState | AskedQuestion
*/
public function solve(KnowledgeState $state, $goal)
{
// Forward chain until there is nothing left to derive.
$this->forwardChain($state);
// Test whether the fact is already in the knowledge base and if not, if it is solely
// unknown because we don't know the current goal we try to prove. Because, it could
// have a variable as value which still needs to be resolved, but that might be a
// different goal!
$current_value = $state->value($goal);
if (!($current_value instanceof Maybe && $current_value->factors == new ArrayIterator([$goal])))
return $current_value;
// Is er misschien een regel die we kunnen toepassen
$relevant_rules = new CallbackFilterIterator($state->rules->getIterator(),
function($rule) use ($goal) { return $rule->infers($goal); });
// Assume that all relevant rules result in maybe's. If not, something went
// horribly wrong in $this->forwardChain()!
foreach ($relevant_rules as $rule)
assert('$rule->condition->evaluate($state) instanceof Maybe');
// Is er misschien een directe vraag die we kunnen stellen?
$relevant_questions = new CallbackFilterIterator($state->questions->getIterator(),
function($question) use ($goal) { return $question->infers($goal); });
$this->log("Found %d rules and %s questions", [iterator_count($relevant_rules),
iterator_count($relevant_questions)], LOG_LEVEL_VERBOSE);
// If this problem can be solved by a rule, use it!
if (iterator_count($relevant_rules) > 0)
return Maybe::because(new CallbackMapIterator($relevant_rules, function($rule) use ($state) {
return $rule->condition->evaluate($state);
}));
// If not, but when we do have a question to solve it, use that instead.
if (iterator_count($relevant_questions) > 0)
{
$question = iterator_first($relevant_questions);
// deze vraag is alleen over te slaan als er nog regels open staan om dit feit
// af te leiden of als er alternatieve vragen naast deze (of eerder gestelde,
// vandaar $n++) zijn.
$skippable = iterator_count($relevant_questions) - 1;
// haal de vraag hoe dan ook uit de mogelijk te stellen vragen. Het heeft geen zin
// om hem twee keer te stellen.
$state->questions->remove($question);
return new AskedQuestion($question, $skippable);
}
// We have no idea how to solve this. No longer our problem!
// (The caller should set $goal to undefined or something.)
return Maybe::because();
}
public function forwardChain(KnowledgeState $state)
{
while (!$state->rules->isEmpty())
{
foreach ($state->rules as $rule)
{
$rule_result = $rule->condition->evaluate($state);
$this->log("Rule '%s' results in %s", [$rule->description, $rule_result],
$rule_result instanceof Yes or $rule_result instanceof No ? LOG_LEVEL_INFO : LOG_LEVEL_VERBOSE);
// If a rule could be applied, remove it to prevent it from being
// applied again.
if ($rule_result instanceof Yes or $rule_result instanceof No)
$state->rules->remove($rule);
// If the rule was true, add the consequences, the inferred knowledge
// to the knowledge state and continue applying rules on the new knowledge.
if ($rule_result instanceof Yes)
{
$this->log("Adding %s to the facts dictionary", [dict_to_string($rule->consequences)]);
$state->apply($rule->consequences);
continue 2;
}
}
// None of the rules changed the state: stop trying.
break;
}
}
protected function log($format, array $arguments = [], $level = LOG_LEVEL_INFO)
{
if (!$this->log)
return;
$this->log->write($format, $arguments, $level);
}
}