From 04f8b646c1c8a2435931bea9d1acffdde138f81e Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Sat, 7 Nov 2026 08:06:01 +0300 Subject: publish --- src/inference-engine.c | 244 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 244 insertions(+) create mode 100644 src/inference-engine.c (limited to 'src/inference-engine.c') 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 @@ +#include "inference-engine.h" +#include +#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]); +} -- cgit v1.3.1