summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2017-02-09 20:03:33 +0100
committerNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2017-02-09 20:03:33 +0100
commit7af295b2ec22f06b24079bf895ac97079f64b6d7 (patch)
tree84a554fc2c169956e3ee975152332c39f6c3615a /src/knowledge/knowledge_learn_markov_sequence.c
parent9ca43c73ba29d6b42cd771f1567074418c883c3e (diff)
It's starting to "properly" reply...
The ACSL coverage is far behind though.
Diffstat (limited to 'src/knowledge/knowledge_learn_markov_sequence.c')
-rw-r--r--src/knowledge/knowledge_learn_markov_sequence.c87
1 files changed, 73 insertions, 14 deletions
diff --git a/src/knowledge/knowledge_learn_markov_sequence.c b/src/knowledge/knowledge_learn_markov_sequence.c
index 35cc123..54357c0 100644
--- a/src/knowledge/knowledge_learn_markov_sequence.c
+++ b/src/knowledge/knowledge_learn_markov_sequence.c
@@ -4,7 +4,7 @@
#include "../sequence/sequence.h"
-#include "../pipe/pipe.h"
+#include "../error/error.h"
#include "knowledge.h"
@@ -40,12 +40,12 @@ static void set_nth_sequence
static int reallocate_sequences_list
(
struct ZoO_knowledge k [const restrict static 1],
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
)
{
ZoO_index ** new_sequences;
- if ((SIZE_MAX / sizeof(ZoO_index *)) > (size_t) k->sequences_length)
+ if ((SIZE_MAX / sizeof(ZoO_index *)) < (size_t) k->sequences_length)
{
ZoO_S_ERROR
(
@@ -83,12 +83,12 @@ static int reallocate_sequences_list
static int reallocate_sequences_sorted_list
(
struct ZoO_knowledge k [const restrict static 1],
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
)
{
ZoO_index * new_sequences_sorted;
- if ((SIZE_MAX / sizeof(ZoO_index)) > (size_t) k->sequences_length)
+ if ((SIZE_MAX / sizeof(ZoO_index)) < (size_t) k->sequences_length)
{
ZoO_S_ERROR
(
@@ -104,7 +104,7 @@ static int reallocate_sequences_sorted_list
(ZoO_index *) realloc
(
(void *) k->sequences_sorted,
- ((size_t) k->sequences_length) * sizeof(ZoO_index)
+ (((size_t) k->sequences_length) * sizeof(ZoO_index))
);
if (new_sequences_sorted == (ZoO_index *) NULL)
@@ -144,7 +144,7 @@ static ZoO_index * copy_sequence
(
const ZoO_index base [const restrict static 1],
const ZoO_index destination_length,
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
)
{
ZoO_index i, diff;
@@ -189,7 +189,7 @@ static int add_sequence
const ZoO_index markov_order, /* Pre (> markov_order 1) */
const ZoO_index sequence_id,
const ZoO_index sorted_sequence_id,
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
)
{
ZoO_index * stored_sequence;
@@ -213,6 +213,30 @@ static int add_sequence
return -1;
}
+ if (ZoO_DEBUG_KNOWLEDGE_LEARN_SEQUENCE)
+ {
+ ZoO_index i;
+
+ ZoO_S_DEBUG
+ (
+ io,
+ ZoO_DEBUG_KNOWLEDGE_LEARN_SEQUENCE,
+ "Learning new sequence."
+ );
+
+ for (i = 0; i < (markov_order - 1); ++i)
+ {
+ ZoO_DEBUG
+ (
+ io,
+ ZoO_DEBUG_KNOWLEDGE_LEARN_SEQUENCE,
+ "markov_sequence[%u]: %u",
+ i,
+ stored_sequence[i]
+ );
+ }
+ }
+
k->sequences_length += 1;
if (reallocate_sequences_list(k, io) < 0)
@@ -271,16 +295,15 @@ int ZoO_knowledge_find_sequence
current_min = 0;
current_max -= 1;
- for (;;)
+ while (current_min <= current_max)
{
i = (current_min + ((current_max - current_min) / 2));
cmp =
ZoO_sequence_cmp
(
- k->sequences[k->sequences_sorted[i]],
- markov_sequence_length,
sequence,
+ k->sequences[k->sequences_sorted[i]],
markov_sequence_length
);
@@ -297,9 +320,9 @@ int ZoO_knowledge_find_sequence
}
else if (cmp < 0)
{
- if ((current_min > current_max) || (i == 0))
+ if ((current_min >= current_max) || (i == 0))
{
- *sequence_id = i;
+ *sequence_id = current_min;
return -1;
}
@@ -313,6 +336,10 @@ int ZoO_knowledge_find_sequence
return 0;
}
}
+
+ *sequence_id = current_min;
+
+ return -1;
}
/******************************************************************************/
@@ -325,11 +352,35 @@ int ZoO_knowledge_learn_markov_sequence
const ZoO_index sequence [const restrict static 1],
const ZoO_index markov_order, /* Pre (> markov_order 1) */
ZoO_index sequence_id [const restrict static 1],
- const struct ZoO_pipe io [const restrict static 1]
+ FILE io [const restrict static 1]
)
{
ZoO_index sorted_id;
+ if (ZoO_DEBUG_KNOWLEDGE_LEARN_SEQUENCE)
+ {
+ ZoO_index i;
+
+ ZoO_S_DEBUG
+ (
+ io,
+ ZoO_DEBUG_KNOWLEDGE_LEARN_SEQUENCE,
+ "Studying markov sequence..."
+ );
+
+ for (i = 0; i < (markov_order - 1); ++i)
+ {
+ ZoO_DEBUG
+ (
+ io,
+ ZoO_DEBUG_KNOWLEDGE_LEARN_SEQUENCE,
+ "markov_sequence[%u]: %u",
+ i,
+ sequence[i]
+ );
+ }
+ }
+
if
(
ZoO_knowledge_find_sequence
@@ -341,6 +392,14 @@ int ZoO_knowledge_learn_markov_sequence
) == 0
)
{
+ ZoO_DEBUG
+ (
+ io,
+ ZoO_DEBUG_KNOWLEDGE_LEARN_SEQUENCE,
+ "Markov sequence is known. ID: %u",
+ *sequence_id
+ );
+
return 0;
}