Soar Kernel  9.3.2 08-06-12
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
episodic_memory.h
Go to the documentation of this file.
1 /*************************************************************************
2  * PLEASE SEE THE FILE "COPYING" (INCLUDED WITH THIS SOFTWARE PACKAGE)
3  * FOR LICENSE AND COPYRIGHT INFORMATION.
4  *************************************************************************/
5 
6 /*************************************************************************
7  *
8  * file: episodic_memory.h
9  *
10  * =======================================================================
11  */
12 
13 #ifndef EPISODIC_MEMORY_H
14 #define EPISODIC_MEMORY_H
15 
16 #include <portability.h>
17 
18 #include <map>
19 #include <list>
20 #include <stack>
21 #include <set>
22 #include <queue>
23 
24 #include "soar_module.h"
25 #include "soar_db.h"
26 
28 // EpMem Experimentation
29 //
30 // If defined, we hijack the main EpMem function
31 // for tight-loop experimentation/timing.
32 //
34 
35 //#define EPMEM_EXPERIMENT
36 
37 
39 // EpMem Constants
41 
43 {
47 };
48 
49 // algorithm constants
50 #define EPMEM_MEMID_NONE 0
51 #define EPMEM_NODEID_ROOT 0
52 #define EPMEM_NODEID_BAD -1
53 #define EPMEM_HASH_ACCEPTABLE 1
54 
55 #define EPMEM_NODE_POS 0
56 #define EPMEM_NODE_NEG 1
57 #define EPMEM_RANGE_START 0
58 #define EPMEM_RANGE_END 1
59 #define EPMEM_RANGE_EP 0
60 #define EPMEM_RANGE_NOW 1
61 #define EPMEM_RANGE_POINT 2
62 
63 #define EPMEM_RIT_ROOT 0
64 #define EPMEM_RIT_OFFSET_INIT -1
65 #define EPMEM_LN_2 0.693147180559945
66 
67 #define EPMEM_DNF 2
68 
69 #define EPMEM_RIT_STATE_NODE 0
70 #define EPMEM_RIT_STATE_EDGE 1
71 
72 
74 // EpMem Typedefs
76 
77 // represents a unique node identifier in the episodic store
78 typedef int64_t epmem_node_id;
79 
80 // represents a unique temporal hash in the episodic store
81 typedef uint64_t epmem_hash_id;
82 
83 // represents a unique episode identifier in the episodic store
84 typedef uint64_t epmem_time_id;
85 
86 
88 // EpMem Parameters
90 
91 class epmem_path_param;
92 
94 {
95  public:
96 
97  // storage
98  enum db_choices { memory, file };
99 
100  // encoding
104 
105  // performance
108 
109  // experimental
112 
115 
117 
118  // encoding
123 
124  // storage
128 
129  // retrieval
132 
133  // performance
138 
139  // experimental
142 
143  epmem_param_container( agent *new_agent );
144 };
145 
147 {
148  protected:
150 
151  public:
152  epmem_path_param( const char *new_name, const char *new_value, soar_module::predicate<const char *> *new_val_pred, soar_module::predicate<const char *> *new_prot_pred, agent *new_agent );
153  virtual void set_value( const char *new_value );
154 };
155 
156 template <typename T>
158 {
159  public:
160  epmem_db_predicate( agent *new_agent );
161  bool operator() ( T val );
162 };
163 
164 
166 // EpMem Statistics
168 
171 
174 class epmem_mem_high_stat;
175 
177 {
178  public:
187 
193 
195 
200 
205 
207 };
208 
209 //
210 
212 {
213  protected:
215 
216  public:
217  epmem_db_lib_version_stat( agent* new_agent, const char* new_name, const char* new_value, soar_module::predicate< const char* >* new_prot_pred );
218  const char* get_value();
219 };
220 
221 //
222 
224 {
225  protected:
227 
228  public:
229  epmem_mem_usage_stat( agent *new_agent, const char *new_name, int64_t new_value, soar_module::predicate<int64_t> *new_prot_pred );
230  int64_t get_value();
231 };
232 
233 //
234 
236 {
237  protected:
239 
240  public:
241  epmem_mem_high_stat( agent *new_agent, const char *new_name, int64_t new_value, soar_module::predicate<int64_t> *new_prot_pred );
242  int64_t get_value();
243 };
244 
245 
247 // EpMem Timers
249 
251 {
252  public:
264 
269 
277 
285 
287 };
288 
289 class epmem_timer_level_predicate: public soar_module::agent_predicate<soar_module::timer::timer_level>
290 {
291  public:
292  epmem_timer_level_predicate( agent *new_agent );
294 };
295 
297 {
298  public:
299  epmem_timer( const char *new_name, agent *new_agent, timer_level new_level );
300 };
301 
302 
304 // EpMem Statements
306 
308 {
309  public:
313 
316 
321 
324 
326 };
327 
329 {
330  public:
332 
333  //
334 
339 
342 
343  //
344 
349 
353 
354  //
355 
359 
362 
363  //
364 
368 
369  //
370 
372 
373  //
374 
379 
380  //
381 
383 };
384 
385 
387 // Common Types
389 
390 // represents a vector of times
391 typedef std::vector<epmem_time_id> epmem_time_list;
392 
393 // represents a list of wmes
394 typedef std::list<wme *> epmem_wme_list;
395 
396 // keeping state for multiple RIT's
398 {
402 
404 {
409 
413 
415 // Soar Integration Types
417 
418 // list used primarily like a stack
419 #ifdef USE_MEM_POOL_ALLOCATORS
420 typedef std::list< preference*, soar_module::soar_memory_pool_allocator< preference* > > epmem_wme_stack;
421 #else
422 typedef std::list< preference* > epmem_wme_stack;
423 #endif
424 
425 // data associated with each state
426 typedef struct epmem_data_struct
427 {
428  uint64_t last_ol_time; // last update to output-link
429  uint64_t last_ol_count; // last count of output-link
430 
431  uint64_t last_cmd_time; // last update to epmem.command
432  uint64_t last_cmd_count; // last update to epmem.command
433 
434  epmem_time_id last_memory; // last retrieved memory
435 
436  epmem_wme_stack* epmem_wmes; // preferences generated in last epmem
437 } epmem_data;
438 
439 // lookup tables to facilitate shared identifiers
440 typedef std::map<epmem_node_id, Symbol *> epmem_id_mapping;
441 
442 // types/structures to facilitate re-use of identifiers
443 typedef std::pair<epmem_node_id, epmem_node_id> epmem_id_pair;
444 typedef std::list<epmem_id_pair> epmem_id_pool;
445 typedef std::map<epmem_node_id, epmem_id_pool *> epmem_hashed_id_pool;
446 typedef std::map<epmem_node_id, epmem_hashed_id_pool *> epmem_parent_id_pool;
447 typedef std::map<epmem_node_id, epmem_id_pool *> epmem_return_id_pool;
448 
449 #ifdef USE_MEM_POOL_ALLOCATORS
450 typedef std::set< wme*, std::less< wme* >, soar_module::soar_memory_pool_allocator< wme* > > epmem_wme_set;
451 typedef std::list< Symbol*, soar_module::soar_memory_pool_allocator< Symbol* > > epmem_symbol_stack;
452 
453 // types/structures to facilitate incremental storage
454 typedef std::map< epmem_node_id, bool, std::less< epmem_node_id >, soar_module::soar_memory_pool_allocator< std::pair< epmem_node_id, bool > > > epmem_id_removal_map;
455 typedef std::set< Symbol*, std::less< Symbol* >, soar_module::soar_memory_pool_allocator< Symbol* > > epmem_symbol_set;
456 
457 #else
458 typedef std::set< wme* > epmem_wme_set;
459 typedef std::list< Symbol* > epmem_symbol_stack;
460 
461 // types/structures to facilitate incremental storage
462 typedef std::map<epmem_node_id, bool> epmem_id_removal_map;
463 typedef std::set< Symbol* > epmem_symbol_set;
464 #endif
465 typedef std::map<epmem_node_id, epmem_wme_set*> epmem_id_ref_counter;
466 
468 {
473 
474 // represents a graph edge (i.e. identifier)
475 // follows cs theory notation of finite automata: q1 = d( q0, w )
476 typedef struct epmem_edge_struct
477 {
478 
480  Symbol *w; // attr
481  epmem_node_id q1; // value
482 
485  uint64_t val_num;
486 
487 } epmem_edge;
488 
490 // Parameter Functions (see cpp for comments)
492 
493 // shortcut for determining if EpMem is enabled
494 extern bool epmem_enabled( agent *my_agent );
495 
497 // Soar Functions (see cpp for comments)
499 
500 // init, end
501 extern void epmem_reset( agent *my_agent, Symbol *state = NULL );
502 extern void epmem_close( agent *my_agent );
503 
504 // perform epmem actions
505 extern void epmem_go( agent *my_agent, bool allow_store = true );
506 extern bool epmem_backup_db( agent* my_agent, const char* file_name, std::string *err );
507 extern void epmem_schedule_promotion( agent* my_agent, Symbol* id );
508 
509 // visualization
510 extern void epmem_visualize_episode( agent* my_agent, epmem_time_id memory_id, std::string* buf );
511 extern void epmem_print_episode( agent* my_agent, epmem_time_id memory_id, std::string* buf );
512 
514 // Episodic Memory Search
516 
517 // defined below
523 
524 // pairs
525 typedef struct std::pair<Symbol*, epmem_literal*> epmem_symbol_literal_pair;
526 typedef struct std::pair<Symbol*, epmem_node_id> epmem_symbol_node_pair;
528 typedef struct std::pair<epmem_node_id, epmem_node_id> epmem_node_pair;
529 
530 // collection classes
531 typedef std::deque<epmem_literal*> epmem_literal_deque;
532 typedef std::deque<epmem_node_id> epmem_node_deque;
533 typedef std::map<Symbol*, int> epmem_symbol_int_map;
534 typedef std::map<epmem_literal*, epmem_node_pair> epmem_literal_node_pair_map;
535 typedef std::map<epmem_literal_node_pair, int> epmem_literal_node_pair_int_map;
536 typedef std::map<epmem_node_id, Symbol*> epmem_node_symbol_map;
537 typedef std::map<epmem_node_id, int> epmem_node_int_map;
538 typedef std::map<epmem_symbol_literal_pair, int> epmem_symbol_literal_pair_int_map;
539 typedef std::map<epmem_symbol_node_pair, int> epmem_symbol_node_pair_int_map;
540 typedef std::map<epmem_triple, epmem_pedge*> epmem_triple_pedge_map;
541 typedef std::map<wme*, epmem_literal*> epmem_wme_literal_map;
542 typedef std::set<epmem_literal*> epmem_literal_set;
543 typedef std::set<epmem_pedge*> epmem_pedge_set;
544 
545 #ifdef USE_MEM_POOL_ALLOCATORS
546 typedef std::map<epmem_triple, epmem_uedge*, std::less<epmem_triple>, soar_module::soar_memory_pool_allocator<std::pair<const epmem_triple, epmem_uedge*> > > epmem_triple_uedge_map;
547 typedef std::set<epmem_interval*, std::less<epmem_interval*>, soar_module::soar_memory_pool_allocator<epmem_interval*> > epmem_interval_set;
548 typedef std::set<epmem_node_id, std::less<epmem_node_id>, soar_module::soar_memory_pool_allocator<epmem_node_id> > epmem_node_set;
549 typedef std::set<epmem_node_pair, std::less<epmem_node_pair>, soar_module::soar_memory_pool_allocator<epmem_node_pair> > epmem_node_pair_set;
550 #else
551 typedef std::map<epmem_triple, epmem_uedge*> epmem_triple_uedge_map;
552 typedef std::set<epmem_interval*> epmem_interval_set;
553 typedef std::set<epmem_node_id> epmem_node_set;
554 typedef std::set<epmem_node_pair> epmem_node_pair_set;
555 #endif
556 
557 // structs
562  bool operator<(const epmem_triple& other) const {
563  if (q0 != other.q0) {
564  return (q0 < other.q0);
565  } else if (w != other.w) {
566  return (w < other.w);
567  } else {
568  return (q1 < other.q1);
569  }
570  }
571 };
572 
576  int is_neg_q;
578  bool is_leaf;
582  double weight;
587 };
588 
596 };
597 
605  bool activated;
606 };
607 
613 };
614 
615 // priority queues and comparison functions
617  bool operator()(const epmem_pedge *a, const epmem_pedge *b) const {
618  if (a->time != b->time) {
619  return (a->time < b->time);
620  } else {
621  return (a < b);
622  }
623  }
624 };
625 typedef std::priority_queue<epmem_pedge*, std::vector<epmem_pedge*>, epmem_pedge_comparator> epmem_pedge_pq;
627  bool operator()(const epmem_interval *a, const epmem_interval *b) const {
628  if (a->time != b->time) {
629  return (a->time < b->time);
630  } else if (a->is_end_point == b->is_end_point) {
631  return (a < b);
632  } else {
633  // arbitrarily put starts before ends
634  return (a->is_end_point == EPMEM_RANGE_START);
635  }
636  }
637 };
638 typedef std::priority_queue<epmem_interval*, std::vector<epmem_interval*>, epmem_interval_comparator> epmem_interval_pq;
639 
640 #endif