aboutsummaryrefslogtreecommitdiff
path: root/src/inference-engine.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/inference-engine.c')
-rw-r--r--src/inference-engine.c244
1 files changed, 244 insertions, 0 deletions
diff --git a/src/inference-engine.c b/src/inference-engine.c
new file mode 100644
index 0000000..db4f33a
--- /dev/null
+++ b/src/inference-engine.c
@@ -0,0 +1,244 @@
1#include "inference-engine.h"
2#include <string.h>
3#include "utils.h"
4#include "dynamic-array.h"
5
6typedef struct decision_tree_node {
7 union {
8 enum question question;
9 enum decision decision;
10 };
11 uint32_t branch_yes;
12 uint32_t branch_no;
13} *DECISION_TREE_NODE;
14
15struct decision_tree {
16 struct decision_tree_node *items;
17 uint32_t count;
18 uint32_t capacity;
19};
20
21#define MAX_DECISION_TREE_NODE_COUNT ((1 << QUESTION_COUNT) - 1)
22
23static struct decision_tree decision_tree = {0};
24
25static int answers_known [QUESTION_COUNT] = {0};
26static int question_queue [QUESTION_COUNT] = {0};
27
28static inline bool
29is_answer_known (enum decision decision, enum question question) {
30 switch (get_decision_predicate (decision, question)) {
31 case ANSWER_YES:
32 case ANSWER_NO:
33 return true;
34 default:
35 return false;
36 }
37}
38
39static void
40build_decision_tree_impl (enum decision *begin,
41 enum decision *end, int order,
42 DECISION_TREE_NODE node)
43{
44 if (begin == end)
45 return;
46
47 if (begin + 1 == end) {
48 // printf ("INSERT DECISION %X\n", *begin);
49 node->decision = *begin;
50 return;
51 }
52
53 enum question question;
54 for (; order < QUESTION_COUNT; ++order) {
55 question = question_queue[order];
56
57 enum decision *decision;
58 for (decision = begin; decision < end; ++decision) {
59 if (!is_answer_known (*decision, question))
60 break;
61 } if (decision == end) break;
62 } ASSERT (order != QUESTION_COUNT && "NOT ENOUGH KNOWLEDGE");
63
64 // printf ("INSERT QUESTION %X\n", question);
65 node->question = question;
66
67 enum decision *pivot = begin;
68 for (enum decision *decision = begin; decision < end; ++decision) {
69 if (get_decision_predicate (*decision, question) == ANSWER_NO) {
70 pivot = decision;
71 break;
72 }
73 }
74
75 for (enum decision *head = pivot + 1; head < end; ++head) {
76 if (get_decision_predicate (*head, question) == ANSWER_YES) {
77 enum decision tmp = *pivot;
78 *pivot++ = *head;
79 *head = tmp;
80 }
81 }
82
83 // for (enum decision *decision = begin; decision < end; ++decision) {
84 // printf ("%s%X\n", decision == pivot ? "->" : " ", *decision + 1);
85 // }
86
87 DYNAMIC_ARRAY_APPEND (decision_tree, (struct decision_tree_node) {0});
88 // printf ("BRANCH YES TO %X\n", decision_tree.count - 1);
89 node->branch_yes = decision_tree.count - 1;
90 build_decision_tree_impl (begin, pivot, order + 1,
91 &decision_tree.items[decision_tree.count - 1]);
92 DYNAMIC_ARRAY_APPEND (decision_tree, (struct decision_tree_node) {0});
93 // printf ("BRANCH NO TO %X\n", decision_tree.count - 1);
94 node->branch_no = decision_tree.count - 1;
95 build_decision_tree_impl (pivot, end, order + 1,
96 &decision_tree.items[decision_tree.count - 1]);
97}
98
99void
100build_decision_tree (void)
101{
102 // compute answers_known[question] and question_order[order]
103 for (int question = 0; question < QUESTION_COUNT; ++question) {
104 for (int decision = 0; decision < DECISION_COUNT; ++decision)
105 answers_known[question] += is_answer_known (decision, question);
106
107 int order = 0;
108 while (order < question &&
109 answers_known[question] <=
110 answers_known[question_queue[order]]) {
111 order += 1;
112 }
113
114 memmove (&question_queue[order + 1],
115 &question_queue[order + 0],
116 (question - order)*sizeof (*question_queue));
117 question_queue[order] = question;
118 }
119
120 // initialize context and run recursive function on it
121 enum decision decisions[DECISION_COUNT];
122 for (int decision = 0; decision < DECISION_COUNT; ++decision)
123 decisions[decision] = DECISION_COUNT - 1 - decision;
124 DYNAMIC_ARRAY_ENSURE_CAPACITY (decision_tree, MAX_DECISION_TREE_NODE_COUNT);
125 DYNAMIC_ARRAY_APPEND (decision_tree, (struct decision_tree_node) {0});
126 build_decision_tree_impl (decisions, decisions + DECISION_COUNT, 0,
127 &decision_tree.items[0]);
128 DYNAMIC_ARRAY_SHRINK (decision_tree);
129}
130
131static void
132write_decision_tree_in_dot_format_impl (FILE *file, DECISION_TREE_NODE node)
133{
134 uint32_t node_id = node - decision_tree.items;
135
136 if (node->branch_yes && node->branch_no) {
137 fprintf (file, "\tn%u [label=\"%u: %s\"]\n",
138 node_id, node_id + 1, question_labels[node->question]);
139
140 write_decision_tree_in_dot_format_impl (
141 file, &decision_tree.items[node->branch_yes]);
142 uint32_t branch_yes_id = &decision_tree.items[node->branch_yes] - decision_tree.items;
143 fprintf (file, "\tn%u -> n%u [label=\"%s\"]\n",
144 node_id, branch_yes_id, question_branch_labels[node->question].answer_yes);
145
146 write_decision_tree_in_dot_format_impl (file, &decision_tree.items[node->branch_no]);
147 uint32_t branch_no_id = &decision_tree.items[node->branch_no] - decision_tree.items;
148 fprintf (file, "\tn%u -> n%u [label=\"%s\"]\n", node_id, branch_no_id, question_branch_labels[node->question].answer_no);
149 } else {
150 fprintf (file, "\tn%u [label=\"%u: %s\"]\n", node_id, node_id + 1, decision_labels[node->decision]);
151 }
152}
153
154struct decision_tree_node_stack {
155 struct decision_tree_node **items;
156 uint32_t count;
157 uint32_t capacity;
158};
159
160void
161write_decision_tree_in_dot_format (void)
162{
163 FILE *file = fopen ("decision-tree.dot", "w");
164 if (file == NULL) {
165 fprintf (stderr, "Error opening file!\n");
166 return;
167 }
168
169 fprintf (file, "digraph {\n");
170 fprintf (file, "\trankdir = \"LR\";\n");
171 // write_decision_tree_in_dot_format_impl (file, &decision_tree.items[0]);
172
173 uint32_t node_visual_id = 0;
174 struct decision_tree_node_stack stack1 = {0};
175 struct decision_tree_node_stack stack2 = {0};
176 struct decision_tree_node_stack *front_stack = &stack1;
177 struct decision_tree_node_stack *back_stack = &stack2;
178 DYNAMIC_ARRAY_APPEND (*front_stack, &decision_tree.items[0]);
179 while (front_stack->count) {
180 DYNAMIC_ARRAY_RESET (*back_stack);
181 DYNAMIC_ARRAY_FOREACH (n, *front_stack) {
182 DECISION_TREE_NODE node = *n;
183 uint32_t node_id = node - decision_tree.items;
184
185 if (node->branch_yes && node->branch_no) {
186 fprintf (file, "\tn%u [label=\"%u: %s\"]\n", node_id, ++node_visual_id, question_labels[node->question]);
187
188 DYNAMIC_ARRAY_APPEND (*back_stack, &decision_tree.items[node->branch_yes]);
189 uint32_t branch_yes_id = &decision_tree.items[node->branch_yes] - decision_tree.items;
190 fprintf (file, "\tn%u -> n%u [label=\"%s\"]\n", node_id, branch_yes_id, question_branch_labels[node->question].answer_yes);
191
192 DYNAMIC_ARRAY_APPEND (*back_stack, &decision_tree.items[node->branch_no]);
193 uint32_t branch_no_id = &decision_tree.items[node->branch_no] - decision_tree.items;
194 fprintf (file, "\tn%u -> n%u [label=\"%s\"]\n", node_id, branch_no_id, question_branch_labels[node->question].answer_no);
195 } else {
196 fprintf (file, "\tn%u [label=\"%u: %s\"]\n", node_id, ++node_visual_id, decision_labels[node->decision]);
197 }
198 }
199
200 struct decision_tree_node_stack *tmp = front_stack;
201 front_stack = back_stack;
202 back_stack = tmp;
203 }
204 fprintf (file, "}\n");
205
206 fclose (file);
207
208 puts ("decision-tree.dot:0:0: Decision tree is ready for viewing.");
209}
210
211void
212question_user (void)
213{
214 char *line = NULL;
215 size_t line_size = 0;
216 struct decision_tree_node *node = &decision_tree.items[0];
217 while (node->branch_yes && node->branch_no) {
218 enum question question = node->question;
219 printf ("%s\n", question_labels[question]);
220 printf (" [1] %s\n", question_branch_labels[question].answer_yes);
221 printf (" [2] %s\n", question_branch_labels[question].answer_no);
222
223 enum answer answer = 0;
224 for (;;) {
225 printf ("> ");
226 getline (&line, &line_size, stdin);
227 sscanf (line, "%d", &answer);
228 if (answer < 1 || answer > 2)
229 continue;
230
231 switch (answer) {
232 case ANSWER_YES:
233 node = &decision_tree.items[node->branch_yes];
234 break;
235 case ANSWER_NO:
236 node = &decision_tree.items[node->branch_no];
237 break;
238 default: break;
239 } break;
240 }
241 }
242 free (line);
243 printf ("Рекомендация: %s\n", decision_labels[node->decision]);
244}