| summaryrefslogtreecommitdiff |
diff options
| author | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2017-02-03 22:20:35 +0100 |
|---|---|---|
| committer | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2017-02-03 22:20:35 +0100 |
| commit | ee26b8ff850add4f83b912635a71dbde06f268d1 (patch) | |
| tree | ada230a0d34aaf2a0e9fbecadde0bdf0dcdf1da4 /src/knowledge/knowledge_get_random_sequence.c | |
| parent | 1dafef5fdf9d98b38cbe717b8a220d721f0ebea8 (diff) | |
Continuing Implementation...
Diffstat (limited to 'src/knowledge/knowledge_get_random_sequence.c')
| -rw-r--r-- | src/knowledge/knowledge_get_random_sequence.c | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/src/knowledge/knowledge_get_random_sequence.c b/src/knowledge/knowledge_get_random_sequence.c new file mode 100644 index 0000000..9055d31 --- /dev/null +++ b/src/knowledge/knowledge_get_random_sequence.c @@ -0,0 +1,83 @@ +#include <stdlib.h> + +#include "../core/char.h" +#include "../core/index.h" +#include "../sequence/sequence.h" + +#include "../pipe/pipe.h" + +#include "knowledge.h" + +static int weighted_random_pick +( + const struct ZoO_knowledge_sequence_collection sc [const restrict static 1], + const ZoO_index sum, + ZoO_index result [const restrict static 1] +) +{ + ZoO_index accumulator, random_number; + + accumulator = 0; + + if (sum == 0) + { + return -1; + } + + random_number = ZoO_index_random_up_to(sum); + /*@ ensures (0 <= random_number <= weights_sum); @*/ + + *result = 0; + + for (;;) + { + accumulator += sc->sequences_ref[*result].occurrences; + + if (accumulator < random_number) + { + *result += 1; + } + else + { + *result = sc->sequences_ref[*result].id; + + return 0; + } + } +} + +int ZoO_knowledge_copy_random_swt_sequence +( + const struct ZoO_knowledge k [const static 1], + ZoO_index sequence [const restrict static 1], + const ZoO_index word_id, + const ZoO_index markov_order, + const struct ZoO_pipe io [const restrict static 1] +) +{ + ZoO_index sequence_id; + + if + ( + weighted_random_pick + ( + &(k->words[word_id].swt), + k->words[word_id].occurrences, + &sequence_id + ) < 0 + ) + { + /* TODO: Err message. */ + + return -1; + } + + memcpy + ( + (void *) sequence, + (const void *) k->sequences[sequence_id], + (((size_t) (markov_order - 1)) * sizeof(ZoO_index)) + ); + + return 0; +} |


