-
Notifications
You must be signed in to change notification settings - Fork 0
/
DepthFirstPlanner.hx
126 lines (94 loc) · 4.31 KB
/
DepthFirstPlanner.hx
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
import Executive;
// planner which keeps track of a "front" of goals with a table of goals which is ordered by priority
class DepthFirstPlanner {
public var table:GoalTable = new GoalTable();
public var exec:Executive; // executive wired to the planner
public var debugGoalSystem:Bool = true;
public function new() {}
public function step() {
if(table.items.length == 0) {
return; // nothing to do
}
var sampledItem: GoalTableItem = table.items[0];
var sampledGoal:ActiveGoal2 = sampledItem.goal;
table.items = table.items.slice(1);
// derive conclusion goals
if (sampledGoal.condOps.ops.length == 0) {
Dbg.dbg(debugGoalSystem, 'goalsystem: GOAL DERIVATION');
var selGoalEvent:Term = sampledGoal.condOps.cond.events[0]; // select first event of par events
Dbg.dbg(debugGoalSystem, 'goalsystem: sel goal = '+TermUtils.convToStr(selGoalEvent)+"!");
var matchingImplSeqs:Array<ImplSeq> = exec.mem.queryByPredicate(selGoalEvent);
for(iMatchedImplSeq in matchingImplSeqs) {
Dbg.dbg(debugGoalSystem, 'goalsystem: matching impl seq = '+iMatchedImplSeq.convToStr());
}
// we need to derive goals from matching implSeqs by goal deduction
// a =/> b.
// b!
// |- ded
// a!
for(iImplSeq in matchingImplSeqs) {
var tvCompound = new Tv(iImplSeq.calcFreq(), iImplSeq.calcConf());
var tvComponent = sampledGoal.desire;
var tvConcl = Tv.deduction(tvCompound, tvComponent);
var stampConcl = Stamp.merge(sampledGoal.stamp, iImplSeq.stamp);
// TODO< we need to deal with multiple condops! >
var goal:ActiveGoal2 = new ActiveGoal2(iImplSeq.condops[0], tvConcl, stampConcl, sampledGoal.creationTime);
submitGoal(goal, sampledItem.prio * 0.9);
}
}
}
public function submitGoal(goal:ActiveGoal2, priority:Float) {
if (goal.condOps.ops.length == 0) {
submitGoal3(goal, priority);
}
else { // case with ops
if (goal.condOps.cond.events.length > 0) { // must have cond events!
// (a &/ ^x)!
// |- DesireDed (deduction) (structural deduction)
// a!
var condOpsConcl = new CondOps(goal.condOps.cond, []); // split off ops
var desireConcl = Tv.structDeduction(goal.desire);
var goalConcl:ActiveGoal2 = new ActiveGoal2(condOpsConcl, desireConcl, goal.stamp, goal.creationTime);
submitGoal3(goalConcl, priority);
}
}
}
public function submitGoal3(goal:ActiveGoal2, priority:Float) {
table.items.push(new GoalTableItem(goal, priority));
table.items.sort((a, b) -> Std.int(a.prio - b.prio)); // force sorted by priority!
table.items.slice(0, 200); // keep under AIKR
}
// main for testing
public static function main() {
var reasoner = new Nar(null);
reasoner.input("<(<a --> A> &/ <({SELF} * a0) --> ^a> ) =/> <b --> B>>.");
reasoner.input("<(<z --> Z> &/ <({SELF} * a0) --> ^a> ) =/> <a --> A>>.");
var planner:DepthFirstPlanner = new DepthFirstPlanner();
planner.exec = reasoner.executive; // wire up
{
var goalTerm = Term.Cop("-->", Term.Name("b",false), Term.Name("B",false));
var tv:Tv = new Tv(1.0, 0.98);
var stamp = planner.exec.createStamp();
var currentTime = 0; // TODO< real time >
var goalCondOp:CondOps = new CondOps(new Par([goalTerm]), []);
var goal:ActiveGoal2 = new ActiveGoal2(goalCondOp, tv, stamp, currentTime);
planner.submitGoal3(goal, 1.0);
}
planner.step();
planner.step();
planner.step();
}
}
// table with active goals, ordered by priority
class GoalTable {
public var items:Array<GoalTableItem> = [];
public function new() {}
}
class GoalTableItem {
public var prio:Float; // priority
public var goal:ActiveGoal2; // actual goal
public function new(goal, prio) {
this.goal = goal;
this.prio = prio;
}
}