aboutsummaryrefslogtreecommitdiff
path: root/src/inference-engine.c
blob: db4f33aa14e50538512bbde58c2035791e49e105 (plain)
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
#include "inference-engine.h"
#include <string.h>
#include "utils.h"
#include "dynamic-array.h"

typedef struct decision_tree_node {
  union {
    enum question question;
    enum decision decision;
  };
  uint32_t branch_yes;
  uint32_t branch_no;
} *DECISION_TREE_NODE;

struct decision_tree {
  struct decision_tree_node *items;
  uint32_t                   count;
  uint32_t                   capacity;
};

#define MAX_DECISION_TREE_NODE_COUNT ((1 << QUESTION_COUNT) - 1)

static struct decision_tree decision_tree = {0};

static int answers_known  [QUESTION_COUNT] = {0};
static int question_queue [QUESTION_COUNT] = {0};

static inline bool
is_answer_known (enum decision decision, enum question question) {
  switch (get_decision_predicate (decision, question)) {
    case ANSWER_YES:
    case ANSWER_NO:
      return true;
    default:
      return false;
  }
}

static void
build_decision_tree_impl (enum decision *begin,
                          enum decision *end, int order,
                          DECISION_TREE_NODE node)
{
  if (begin == end)
    return;

  if (begin + 1 == end) {
    // printf ("INSERT DECISION %X\n", *begin);
    node->decision = *begin;
    return;
  }

  enum question question;
  for (; order < QUESTION_COUNT; ++order) {
    question = question_queue[order];

    enum decision *decision;
    for (decision = begin; decision < end; ++decision) {
      if (!is_answer_known (*decision, question))
        break;
    } if (decision == end) break;
  } ASSERT (order != QUESTION_COUNT && "NOT ENOUGH KNOWLEDGE");

  // printf ("INSERT QUESTION %X\n", question);
  node->question = question;

  enum decision *pivot = begin;
  for (enum decision *decision = begin; decision < end; ++decision) {
    if (get_decision_predicate (*decision, question) == ANSWER_NO) {
      pivot = decision;
      break;
    }
  }

  for (enum decision *head = pivot + 1; head < end; ++head) {
    if (get_decision_predicate (*head, question) == ANSWER_YES) {
      enum decision tmp = *pivot;
      *pivot++ = *head;
      *head = tmp;
    }
  }

  // for (enum decision *decision = begin; decision < end; ++decision) {
  //   printf ("%s%X\n", decision == pivot ? "->" : "  ", *decision + 1);
  // }

  DYNAMIC_ARRAY_APPEND (decision_tree, (struct decision_tree_node) {0});
  // printf ("BRANCH YES TO %X\n", decision_tree.count - 1);
  node->branch_yes = decision_tree.count - 1;
  build_decision_tree_impl (begin, pivot, order + 1,
                            &decision_tree.items[decision_tree.count - 1]);
  DYNAMIC_ARRAY_APPEND (decision_tree, (struct decision_tree_node) {0});
  // printf ("BRANCH NO TO %X\n", decision_tree.count - 1);
  node->branch_no = decision_tree.count - 1;
  build_decision_tree_impl (pivot, end,   order + 1,
                            &decision_tree.items[decision_tree.count - 1]);
}

void
build_decision_tree (void)
{
  // compute answers_known[question] and question_order[order]
  for (int question = 0; question < QUESTION_COUNT; ++question) {
    for (int decision = 0; decision < DECISION_COUNT; ++decision)
      answers_known[question] += is_answer_known (decision, question);

    int order = 0;
    while (order < question &&
           answers_known[question] <=
           answers_known[question_queue[order]]) {
      order += 1;
    }

    memmove (&question_queue[order + 1],
             &question_queue[order + 0],
             (question - order)*sizeof (*question_queue));
    question_queue[order] = question;
  }

  // initialize context and run recursive function on it
  enum decision decisions[DECISION_COUNT];
  for (int decision = 0; decision < DECISION_COUNT; ++decision)
    decisions[decision] = DECISION_COUNT - 1 - decision;
  DYNAMIC_ARRAY_ENSURE_CAPACITY (decision_tree, MAX_DECISION_TREE_NODE_COUNT);
  DYNAMIC_ARRAY_APPEND (decision_tree, (struct decision_tree_node) {0});
  build_decision_tree_impl (decisions, decisions + DECISION_COUNT, 0,
                            &decision_tree.items[0]);
  DYNAMIC_ARRAY_SHRINK (decision_tree);
}

static void
write_decision_tree_in_dot_format_impl (FILE *file, DECISION_TREE_NODE node)
{
  uint32_t node_id = node - decision_tree.items;

  if (node->branch_yes && node->branch_no) {
    fprintf (file, "\tn%u [label=\"%u: %s\"]\n",
             node_id, node_id + 1, question_labels[node->question]);

    write_decision_tree_in_dot_format_impl (
      file, &decision_tree.items[node->branch_yes]);
    uint32_t branch_yes_id = &decision_tree.items[node->branch_yes] - decision_tree.items;
    fprintf (file, "\tn%u -> n%u [label=\"%s\"]\n",
             node_id, branch_yes_id, question_branch_labels[node->question].answer_yes);

    write_decision_tree_in_dot_format_impl (file, &decision_tree.items[node->branch_no]);
    uint32_t branch_no_id = &decision_tree.items[node->branch_no] - decision_tree.items;
    fprintf (file, "\tn%u -> n%u [label=\"%s\"]\n", node_id, branch_no_id, question_branch_labels[node->question].answer_no);
  } else {
    fprintf (file, "\tn%u [label=\"%u: %s\"]\n", node_id, node_id + 1, decision_labels[node->decision]);
  }
}

struct decision_tree_node_stack {
  struct decision_tree_node **items;
  uint32_t                    count;
  uint32_t                    capacity;
};

void
write_decision_tree_in_dot_format (void)
{
  FILE *file = fopen ("decision-tree.dot", "w");
  if (file == NULL) {
    fprintf (stderr, "Error opening file!\n");
    return;
  }

  fprintf (file, "digraph {\n");
  fprintf (file, "\trankdir = \"LR\";\n");
  // write_decision_tree_in_dot_format_impl (file, &decision_tree.items[0]);

  uint32_t node_visual_id = 0;
  struct decision_tree_node_stack stack1 = {0};
  struct decision_tree_node_stack stack2 = {0};
  struct decision_tree_node_stack *front_stack = &stack1;
  struct decision_tree_node_stack  *back_stack = &stack2;
  DYNAMIC_ARRAY_APPEND (*front_stack, &decision_tree.items[0]);
  while (front_stack->count) {
    DYNAMIC_ARRAY_RESET (*back_stack);
    DYNAMIC_ARRAY_FOREACH (n, *front_stack) {
      DECISION_TREE_NODE node = *n;
      uint32_t node_id = node - decision_tree.items;

      if (node->branch_yes && node->branch_no) {
        fprintf (file, "\tn%u [label=\"%u: %s\"]\n", node_id, ++node_visual_id, question_labels[node->question]);

        DYNAMIC_ARRAY_APPEND (*back_stack, &decision_tree.items[node->branch_yes]);
        uint32_t branch_yes_id = &decision_tree.items[node->branch_yes] - decision_tree.items;
        fprintf (file, "\tn%u -> n%u [label=\"%s\"]\n", node_id, branch_yes_id, question_branch_labels[node->question].answer_yes);

        DYNAMIC_ARRAY_APPEND (*back_stack, &decision_tree.items[node->branch_no]);
        uint32_t branch_no_id = &decision_tree.items[node->branch_no] - decision_tree.items;
        fprintf (file, "\tn%u -> n%u [label=\"%s\"]\n", node_id, branch_no_id, question_branch_labels[node->question].answer_no);
      } else {
        fprintf (file, "\tn%u [label=\"%u: %s\"]\n", node_id, ++node_visual_id, decision_labels[node->decision]);
      }
    }

    struct decision_tree_node_stack *tmp = front_stack;
    front_stack = back_stack;
    back_stack = tmp;
  }
  fprintf (file, "}\n");

  fclose (file);

  puts ("decision-tree.dot:0:0: Decision tree is ready for viewing.");
}

void
question_user (void)
{
  char   *line = NULL;
  size_t  line_size = 0;
  struct decision_tree_node *node = &decision_tree.items[0];
  while (node->branch_yes && node->branch_no) {
    enum question question = node->question;
    printf ("%s\n", question_labels[question]);
    printf (" [1] %s\n", question_branch_labels[question].answer_yes);
    printf (" [2] %s\n", question_branch_labels[question].answer_no);

    enum answer answer = 0;
    for (;;) {
      printf ("> ");
      getline (&line, &line_size, stdin);
      sscanf (line, "%d", &answer);
      if (answer < 1 || answer > 2)
        continue;

      switch (answer) {
        case ANSWER_YES:
          node = &decision_tree.items[node->branch_yes];
          break;
        case ANSWER_NO:
          node = &decision_tree.items[node->branch_no];
          break;
        default: break;
      } break;
    }
  }
  free (line);
  printf ("Рекомендация: %s\n", decision_labels[node->decision]);
}