Soar Kernel  9.3.2 08-06-12
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Data Structures | Macros | Typedefs | Enumerations | Functions
wma.h File Reference
#include <string>
#include <queue>
#include "soar_module.h"

Go to the source code of this file.

Data Structures

class  wma_activation_param
class  wma_activation_predicate< T >
struct  wma_cycle_reference_struct
struct  wma_decay_element_struct
class  wma_decay_param
struct  wma_history_struct
class  wma_param_container
class  wma_stat_container
class  wma_timer
class  wma_timer_container
class  wma_timer_level_predicate

Macros

#define WMA_ACTIVATION_LOW   -1000000000
#define WMA_ACTIVATION_NONE   1.0
#define WMA_DECAY_HISTORY   10
#define WMA_FORGOTTEN_CYCLE   0
#define WMA_REFERENCES_PER_DECISION   50
#define WMA_TIME_SUM_NONE   2.71828182845905

Typedefs

typedef struct
wma_cycle_reference_struct 
wma_cycle_reference
typedef uint64_t wma_d_cycle
typedef std::set< wma_d_cycle,
std::less< wma_d_cycle >
, soar_module::soar_memory_pool_allocator
< wma_d_cycle > > 
wma_decay_cycle_set
typedef struct
wma_decay_element_struct 
wma_decay_element
typedef std::set
< wma_decay_element
*, std::less
< wma_decay_element * >
, soar_module::soar_memory_pool_allocator
< wma_decay_element * > > 
wma_decay_set
typedef std::map< wma_d_cycle,
wma_decay_set *, std::less
< wma_d_cycle >
, soar_module::soar_memory_pool_allocator
< std::pair< wma_d_cycle,
wma_decay_set * > > > 
wma_forget_p_queue
typedef struct wma_history_struct wma_history
typedef std::set< wme
*, std::less< wme * >
, soar_module::soar_memory_pool_allocator
< wme * > > 
wma_pooled_wme_set
typedef uint64_t wma_reference
typedef std::map< Symbol
*, uint64_t, std::less< Symbol * >
, soar_module::soar_memory_pool_allocator
< std::pair< Symbol
*, uint64_t > > > 
wma_sym_reference_map
typedef struct wme_struct wme

Enumerations

enum  wma_go_action

Functions

void wma_activate_wme (agent *my_agent, wme *w, wma_reference num_references=1, wma_pooled_wme_set *o_set=NULL, bool o_only=false)
void wma_activate_wmes_in_pref (agent *my_agent, preference *pref)
void wma_activate_wmes_tested_in_prods (agent *my_agent)
bool wma_enabled (agent *my_agent)
double wma_get_wme_activation (agent *my_agent, wme *w, bool log_result)
void wma_get_wme_history (agent *my_agent, wme *w, std::string &buffer)
void wma_go (agent *my_agent, wma_go_action go_action)
void wma_remove_decay_element (agent *my_agent, wme *w)
void wma_remove_pref_o_set (agent *my_agent, preference *pref)

Macro Definition Documentation

#define WMA_ACTIVATION_LOW   -1000000000

If no history, this is a low number to report as activation

Definition at line 48 of file wma.h.

Referenced by wma_calculate_decay_activation().

#define WMA_ACTIVATION_NONE   1.0

If an external caller asks for the activation level/value of a WME that is not activated, then this is the value that is returned.

Definition at line 42 of file wma.h.

Referenced by wma_get_wme_activation().

#define WMA_DECAY_HISTORY   10

This is the size of the reference history.

Definition at line 29 of file wma.h.

Referenced by wma_activate_wme(), wma_history_next(), wma_history_prev(), and wma_update_decay_histories().

#define WMA_FORGOTTEN_CYCLE   0

If below decay thresh, but not forgotten, forget_cycle =

Definition at line 53 of file wma.h.

Referenced by wma_forgetting_update_p_queue().

#define WMA_REFERENCES_PER_DECISION   50

How many references are expected per decision (this affects creation of the power/approx cache)

Definition at line 35 of file wma.h.

Referenced by wma_forgetting_estimate_cycle(), and wma_init().

#define WMA_TIME_SUM_NONE   2.71828182845905

Definition at line 43 of file wma.h.

Referenced by wma_get_wme_activation().

Typedef Documentation

typedef uint64_t wma_d_cycle

Definition at line 155 of file wma.h.

Definition at line 207 of file wma.h.

Definition at line 205 of file wma.h.

Definition at line 206 of file wma.h.

typedef std::set< wme*, std::less< wme* >, soar_module::soar_memory_pool_allocator< wme* > > wma_pooled_wme_set

Definition at line 209 of file wma.h.

typedef uint64_t wma_reference

Definition at line 154 of file wma.h.

typedef std::map< Symbol*, uint64_t, std::less< Symbol* >, soar_module::soar_memory_pool_allocator< std::pair< Symbol*, uint64_t > > > wma_sym_reference_map

Definition at line 210 of file wma.h.

typedef struct wme_struct wme

Definition at line 20 of file wma.h.

Enumeration Type Documentation

Enumerator:
wma_histories 
wma_forgetting 

Definition at line 220 of file wma.h.

Function Documentation

void wma_activate_wme ( agent my_agent,
wme w,
wma_reference  num_references = 1,
wma_pooled_wme_set o_set = NULL,
bool  o_only = false 
)

Definition at line 470 of file wma.cpp.

References wma_history_struct::access_history, wme_struct::attr, wma_cycle_reference_struct::d_cycle, agent_struct::d_cycle_count, symbol_union::fc, wma_history_struct::first_reference, FLOAT_CONSTANT_SYMBOL_TYPE, wma_decay_element_struct::forget_cycle, wma_history_struct::history_ct, wma_history_struct::history_references, symbol_union::ic, wme_struct::id, symbol_union::id, preference_struct::inst, INT_CONSTANT_SYMBOL_TYPE, wma_decay_element_struct::just_created, wma_decay_element_struct::just_removed, sym_constant_struct::name, identifier_struct::name_letter, identifier_struct::name_number, condition_struct::next, wma_history_struct::next_p, wma_cycle_reference_struct::num_references, wma_decay_element_struct::num_references, POSITIVE_CONDITION, wme_struct::preference, print(), preference_struct::reference_count, symbol_union::sc, SYM_CONSTANT_SYMBOL_TYPE, agent_struct::sysparams, wma_decay_element_struct::this_wme, wme_struct::timetag, instantiation_struct::top_of_instantiated_conditions, wma_history_struct::total_references, wma_decay_element_struct::touches, TRACE_WMA_SYSPARAM, wme_struct::value, int_constant_struct::value, float_constant_struct::value, wma_activate_wme(), wma_calculate_initial_boost(), wme_struct::wma_decay_el, agent_struct::wma_decay_element_pool, WMA_DECAY_HISTORY, preference_struct::wma_o_set, wma_should_have_decay_element(), agent_struct::wma_touched_elements, agent_struct::wma_wme_oset_pool, wme_add_ref(), and xml_generate_warning().

Referenced by add_input_wme(), decide_non_context_slot(), wma_activate_wme(), wma_activate_wmes_in_pref(), and wma_activate_wmes_tested_in_prods().

{
// o-supported, non-architectural WME
{
// if decay structure doesn't exist, create it
if ( !temp_el )
{
allocate_with_pool( my_agent, &( my_agent->wma_decay_element_pool ), &temp_el );
temp_el->this_wme = w;
temp_el->just_removed = false;
temp_el->just_created = true;
temp_el->num_references = wma_calculate_initial_boost( my_agent, w );
temp_el->touches.history_ct = 0;
temp_el->touches.next_p = 0;
for ( int i=0; i<WMA_DECAY_HISTORY; i++ )
{
temp_el->touches.access_history[ i ].d_cycle = 0;
}
temp_el->touches.total_references = 0;
temp_el->touches.first_reference = 0;
// prevents confusion with delayed forgetting
temp_el->forget_cycle = static_cast< wma_d_cycle >( -1 );
w->wma_decay_el = temp_el;
if ( my_agent->sysparams[ TRACE_WMA_SYSPARAM ] )
{
std::string msg( "WMA @" );
std::string temp;
to_string( my_agent->d_cycle_count, temp );
msg.append( temp );
msg.append( ": " );
msg.append( "add " );
to_string( w->timetag, temp );
msg.append( temp );
msg.append( " " );
to_string( w->id->id.name_letter, temp );
msg.append( temp );
to_string( w->id->id.name_number, temp );
msg.append( temp );
msg.append( " " );
switch ( w->attr->common.symbol_type )
{
to_string( w->attr->ic.value, temp );
break;
to_string( w->attr->fc.value, temp );
break;
to_string( w->attr->sc.name, temp );
break;
}
msg.append( temp );
msg.append( " " );
switch ( w->value->common.symbol_type )
{
to_string( w->value->ic.value, temp );
break;
to_string( w->value->fc.value, temp );
break;
to_string( w->value->sc.name, temp );
break;
}
msg.append( temp );
msg.append( "\n" );
print( my_agent, msg.c_str() );
xml_generate_warning( my_agent, msg.c_str() );
}
}
// add to o_set if necessary
if ( o_set )
{
o_set->insert( w );
}
// otherwise update the decay element
else
{
temp_el->num_references += num_references;
my_agent->wma_touched_elements->insert( w );
}
}
// i-supported, non-architectural WME
else if ( !o_only && ( w->preference ) && ( w->preference->reference_count ) )
{
wma_pooled_wme_set::iterator wme_p;
// if doesn't have an o_set, populate
if ( !my_o_set )
{
allocate_with_pool( my_agent, &( my_agent->wma_wme_oset_pool ), &my_o_set );
#ifdef USE_MEM_POOL_ALLOCATORS
my_o_set = new( my_o_set ) wma_pooled_wme_set( std::less< wme* >(), soar_module::soar_memory_pool_allocator< wme* >( my_agent ) );
#else
my_o_set = new( my_o_set ) wma_pooled_wme_set();
#endif
w->preference->wma_o_set = my_o_set;
{
if ( c->type == POSITIVE_CONDITION )
{
wma_activate_wme( my_agent, c->bt.wme_, 0, my_o_set );
}
}
for ( wme_p=my_o_set->begin(); wme_p!=my_o_set->end(); wme_p++ )
{
// add a ref to wmes on this list
wme_add_ref( (*wme_p) );
}
}
// iterate over the o_set
for ( wme_p=my_o_set->begin(); wme_p!=my_o_set->end(); wme_p++ )
{
// if populating o_set, add
if ( o_set )
{
o_set->insert( (*wme_p) );
}
// otherwise, "activate" the wme if it is
// non-architectural (avoids dereferencing
// the wme preference)
else
{
if ( (*wme_p)->wma_decay_el )
{
(*wme_p)->wma_decay_el->num_references += num_references;
my_agent->wma_touched_elements->insert( (*wme_p) );
}
}
}
}
// architectural
else if ( !o_only && !w->preference )
{
// only action is to add it to the o_set
if ( o_set )
{
o_set->insert( w );
}
}
}
void wma_activate_wmes_in_pref ( agent my_agent,
preference pref 
)

Given a preference, this routine increments the reference count of all its WMEs (as necessary).

Definition at line 1062 of file wma.cpp.

References ACCEPTABLE_PREFERENCE_TYPE, wme_struct::next, preference_struct::slot, preference_struct::type, wme_struct::value, preference_struct::value, wma_activate_wme(), and slot_struct::wmes.

Referenced by _epmem_process_buffered_wme_list(), _smem_process_buffered_wme_list(), and assert_new_preferences().

{
wme* w;
{
w = pref->slot->wmes;
while ( w )
{
// id and attr should already match so just compare the value
if ( w->value == pref->value )
{
wma_activate_wme( my_agent, w );
}
w = w->next;
}
}
}
void wma_activate_wmes_tested_in_prods ( agent my_agent)

Increments the reference count of all WMEs that have been referenced this cycle.

Definition at line 1082 of file wma.cpp.

References agent_struct::dummy_top_token, agent_struct::ms_i_assertions, agent_struct::ms_o_assertions, ms_change_struct::next, NIL, token_struct::parent, ms_change_struct::tok, ms_change_struct::w, token_struct::w, and wma_activate_wme().

Referenced by do_preference_phase().

{
ms_change *msc;
token temp_token, *t;
for ( msc=my_agent->ms_o_assertions; msc!=NIL; msc=msc->next )
{
temp_token.parent = msc->tok;
temp_token.w = msc->w;
t = &temp_token;
while ( t != my_agent->dummy_top_token )
{
if (t->w != NIL)
{
wma_activate_wme( my_agent, t->w );
}
t = t->parent;
}
}
for ( msc=my_agent->ms_i_assertions; msc!=NIL; msc=msc->next )
{
temp_token.parent = msc->tok;
temp_token.w = msc->w;
t = &temp_token;
while ( t != my_agent->dummy_top_token )
{
if ( t->w != NIL )
{
wma_activate_wme( my_agent, t->w );
}
t = t->parent;
}
}
}
bool wma_enabled ( agent my_agent)
double wma_get_wme_activation ( agent my_agent,
wme w,
bool  log_result 
)

Retrieve wme activation exact/approximate

Definition at line 1212 of file wma.cpp.

References WMA_ACTIVATION_NONE, wma_calculate_decay_activation(), agent_struct::wma_d_cycle_count, wme_struct::wma_decay_el, and WMA_TIME_SUM_NONE.

Referenced by epmem_build_dnf(), print_wme(), and wma_calculate_initial_boost().

{
double return_val = static_cast<double>( ( log_result )?( WMA_ACTIVATION_NONE ):( WMA_TIME_SUM_NONE ) );
if ( w->wma_decay_el )
{
return_val = wma_calculate_decay_activation( my_agent, w->wma_decay_el, my_agent->wma_d_cycle_count, log_result );
}
return return_val;
}
void wma_get_wme_history ( agent my_agent,
wme w,
std::string &  buffer 
)

Debugging: get list of wme references

Definition at line 1245 of file wma.cpp.

References _wma_ref_to_str(), wma_history_struct::access_history, wma_param_container::approx, wma_param_container::bsearch, wma_history_struct::first_reference, wma_decay_element_struct::forget_cycle, wma_param_container::forgetting, soar_module::constant_param< T >::get_value(), wma_history_struct::history_ct, wma_history_struct::history_references, wma_history_struct::next_p, wma_history_struct::total_references, wma_decay_element_struct::touches, agent_struct::wma_d_cycle_count, wme_struct::wma_decay_el, wma_history_prev(), and agent_struct::wma_params.

{
if ( w->wma_decay_el )
{
wma_history* history = &( w->wma_decay_el->touches );
unsigned int p = history->next_p;
unsigned int counter = history->history_ct;
wma_d_cycle current_cycle = my_agent->wma_d_cycle_count;
//
buffer.append( "history (" );
{
std::string temp;
to_string( history->history_references, temp );
buffer.append( temp );
buffer.append( "/" );
to_string( history->total_references, temp );
buffer.append( temp );
buffer.append( ", first @ d" );
to_string( history->first_reference, temp );
buffer.append( temp );
}
buffer.append( "):" );
//
while ( counter )
{
p = wma_history_prev( p );
counter--;
buffer.append( "\n " );
_wma_ref_to_str( history->access_history[ p ], current_cycle, buffer );
}
//
if ( ( forget == wma_param_container::bsearch ) || ( forget == wma_param_container::approx ) )
{
buffer.append( "\n\n" );
buffer.append( "considering WME for decay @ d" );
std::string temp;
to_string( w->wma_decay_el->forget_cycle, temp );
buffer.append( temp );
}
}
else
{
buffer.assign( "WME has no decay history" );
}
}
void wma_go ( agent my_agent,
wma_go_action  go_action 
)

This routine performs WME activation and forgetting at the end of each cycle.

Definition at line 1308 of file wma.cpp.

References do_working_memory_phase(), wma_param_container::forgetting, wma_timer_container::forgetting, wma_stat_container::forgotten_wmes, soar_module::constant_param< T >::get_value(), soar_module::primitive_stat< T >::get_value(), wma_timer_container::history, wma_param_container::naive, wma_param_container::off, print(), soar_module::primitive_stat< T >::set_value(), soar_module::timer::start(), soar_module::timer::stop(), agent_struct::sysparams, TRACE_WM_CHANGES_SYSPARAM, wma_forgetting, wma_forgetting_naive_sweep(), wma_forgetting_update_p_queue(), wma_histories, agent_struct::wma_params, agent_struct::wma_stats, agent_struct::wma_timers, wma_update_decay_histories(), agent_struct::wme_removal_count, and xml_generate_message().

Referenced by do_one_top_level_phase().

{
// update history for all touched elements
if ( go_action == wma_histories )
{
my_agent->wma_timers->history->start();
my_agent->wma_timers->history->stop();
}
// check forgetting queue
else if ( go_action == wma_forgetting )
{
if ( forgetting != wma_param_container::off )
{
my_agent->wma_timers->forgetting->start();
bool forgot_something = false;
if ( forgetting == wma_param_container::naive )
{
forgot_something = wma_forgetting_naive_sweep( my_agent );
}
else
{
forgot_something = wma_forgetting_update_p_queue( my_agent );
}
if ( forgot_something )
{
{
const char *msg = "\n\nWMA: BEGIN FORGOTTEN WME LIST\n\n";
print( my_agent, const_cast<char *>( msg ) );
xml_generate_message( my_agent, const_cast<char *>( msg ) );
}
uint64_t wm_removal_diff = my_agent->wme_removal_count;
{
}
wm_removal_diff = ( my_agent->wme_removal_count - wm_removal_diff );
if ( wm_removal_diff > 0 )
{
my_agent->wma_stats->forgotten_wmes->set_value( my_agent->wma_stats->forgotten_wmes->get_value() + static_cast< int64_t >( wm_removal_diff ) );
}
{
const char *msg = "\nWMA: END FORGOTTEN WME LIST\n\n";
print( my_agent, const_cast<char *>( msg ) );
xml_generate_message( my_agent, const_cast<char *>( msg ) );
}
}
my_agent->wma_timers->forgetting->stop();
}
}
}
void wma_remove_decay_element ( agent my_agent,
wme w 
)

Definition at line 667 of file wma.cpp.

References agent_struct::d_cycle_count, wma_decay_element_struct::just_removed, print(), agent_struct::sysparams, wme_struct::timetag, TRACE_WMA_SYSPARAM, wma_deactivate_element(), wme_struct::wma_decay_el, agent_struct::wma_decay_element_pool, and xml_generate_warning().

Referenced by deallocate_wme().

{
if ( temp_el )
{
// Deactivate the wme first
if ( !temp_el->just_removed )
{
wma_deactivate_element( my_agent, w );
}
// log
if ( my_agent->sysparams[ TRACE_WMA_SYSPARAM ] )
{
std::string msg( "WMA @" );
std::string temp;
to_string( my_agent->d_cycle_count, temp );
msg.append( temp );
msg.append( ": " );
msg.append( "remove " );
to_string( w->timetag, temp );
msg.append( temp );
msg.append( "\n" );
print( my_agent, msg.c_str() );
xml_generate_warning( my_agent, msg.c_str() );
}
free_with_pool( &( my_agent->wma_decay_element_pool ), temp_el );
w->wma_decay_el = NULL;
}
}
void wma_remove_pref_o_set ( agent my_agent,
preference pref 
)

Definition at line 705 of file wma.cpp.

References preference_struct::wma_o_set, agent_struct::wma_wme_oset_pool, and wme_remove_ref().

Referenced by deallocate_instantiation(), and deallocate_preference().

{
if ( pref && pref->wma_o_set )
{
wma_pooled_wme_set* victim = pref->wma_o_set;
pref->wma_o_set = NULL;
for ( wma_pooled_wme_set::iterator p=victim->begin(); p!=victim->end(); p++ )
{
wme_remove_ref( my_agent, (*p) );
}
victim->~wma_pooled_wme_set();
free_with_pool( &( my_agent->wma_wme_oset_pool ), victim );
}
}