1 #include <portability.h>
68 if ( new_value !=
value )
226 double cache_full =
static_cast<double>( exp( ( decay_thresh - log( static_cast<double>(
WMA_REFERENCES_PER_DECISION ) ) ) / decay_rate ) );
231 double cache_bound = (
static_cast<unsigned int>( max_pow_cache * 1024 * 1024 ) / static_cast<unsigned int>(
sizeof(
double ) ) );
233 my_agent->
wma_power_size =
static_cast< unsigned int >( ceil( ( cache_full > cache_bound )?( cache_bound ):( cache_full ) ) );
241 my_agent->
wma_power_array[ i ] = pow( static_cast<double>( i ), decay_rate );
257 my_agent->
wma_approx_array[i] =
static_cast< wma_d_cycle >( ceil( exp( static_cast<double>( decay_thresh - log( static_cast<double>(i) ) ) / static_cast<double>( decay_rate ) ) ) );
288 pq_p->second->~wma_decay_set();
323 if ( cycle_diff < my_agent->wma_power_size )
335 double return_val = 0.0;
337 unsigned int p = history->
next_p;
348 assert( cycle_diff > 0 );
369 ( d_inv * ( ( current_cycle - history->
first_reference ) - cycle_diff ) ) );
382 double history_sum =
wma_sum_history( my_agent, history, current_cycle );
389 if ( history_sum > 0.0 )
391 return log( history_sum );
410 wma_pooled_wme_set::iterator wme_p;
414 uint64_t num_cond_wmes = 0;
415 double combined_time_sum = 0.0;
442 if ( ( (*wme_p)->wma_tc_value != tc ) && ( !(*wme_p)->wma_decay_el || !(*wme_p)->wma_decay_el->just_created ) )
447 (*wme_p)->wma_tc_value = tc;
464 return_val =
static_cast<wma_reference>( floor( combined_time_sum / num_cond_wmes ) );
508 std::string msg(
"WMA @" );
515 msg.append(
"add " );
528 switch ( w->
attr->common.symbol_type )
546 switch ( w->
value->common.symbol_type )
564 print( my_agent, msg.c_str() );
585 wma_pooled_wme_set::iterator wme_p;
591 #ifdef USE_MEM_POOL_ALLOCATORS
607 for ( wme_p=my_o_set->begin(); wme_p!=my_o_set->end(); wme_p++ )
615 for ( wme_p=my_o_set->begin(); wme_p!=my_o_set->end(); wme_p++ )
620 o_set->insert( (*wme_p) );
627 if ( (*wme_p)->wma_decay_el )
629 (*wme_p)->wma_decay_el->num_references += num_references;
682 std::string msg(
"WMA @" );
689 msg.append(
"remove " );
696 print( my_agent, msg.c_str() );
712 for ( wma_pooled_wme_set::iterator p=victim->begin(); p!=victim->end(); p++ )
717 victim->~wma_pooled_wme_set();
738 wma_forget_p_queue::iterator pq_p = my_agent->
wma_forget_pq->find( new_cycle );
743 #ifdef USE_MEM_POOL_ALLOCATORS
748 newbie->insert( decay_el );
750 my_agent->
wma_forget_pq->insert( std::make_pair< wma_d_cycle, wma_decay_set* >( new_cycle, newbie ) );
754 pq_p->second->insert( decay_el );
767 wma_decay_set::iterator d_p = pq_p->second->find( decay_el );
768 if ( d_p != pq_p->second->end() )
770 pq_p->second->erase( d_p );
772 if ( pq_p->second->empty() )
783 if ( decay_el && ( decay_el->
forget_cycle != new_cycle ) )
800 unsigned int p = history->
next_p;
822 return_val += to_add;
834 if ( act >= my_thresh )
836 while ( act >= my_thresh )
848 lower_bound = upper_bound;
852 lower_bound = ( to_add / 2 );
855 while ( lower_bound != upper_bound )
857 mid = ( ( lower_bound + upper_bound ) / 2 );
860 if ( act < my_thresh )
864 if ( upper_bound - lower_bound <= 1 )
873 if ( upper_bound - lower_bound <= 1 )
875 lower_bound = upper_bound;
880 to_add = upper_bound;
883 return_val += to_add;
892 bool return_val =
false;
922 bool return_val =
false;
923 bool do_forget =
false;
929 wma_forget_p_queue::iterator pq_p = my_agent->
wma_forget_pq->begin();
934 if ( pq_p->first == current_cycle )
936 wma_decay_set::iterator d_p=pq_p->second->begin();
937 wma_decay_set::iterator current_p;
939 while ( d_p != pq_p->second->end() )
947 if ( !forget_only_lti || ( (*current_p)->this_wme->id->id.smem_lti !=
NIL ) )
952 if ( forget_only_lti )
954 for ( s=(*current_p)->this_wme->
id->
id.
slots; (s && do_forget); s=s->
next )
956 for ( w=s->
wmes; (w && do_forget); w=w->
next )
968 if ( forget_only_lti )
971 for ( s=(*current_p)->this_wme->
id->
id.
slots; (s && do_forget); s=s->
next )
973 for ( w=s->
wmes; (w && do_forget); w=w->
next )
1000 pq_p->second->clear();
1008 if ( ( pq_p != my_agent->
wma_forget_pq->end() ) && ( pq_p->second->empty() ) )
1010 pq_p->second->~wma_decay_set();
1027 bool return_val =
false;
1031 if ( w->wma_decay_el && ( !forget_only_lti || ( w->id->id.smem_lti !=
NIL ) ) )
1037 if ( ( w->wma_decay_el->touches.total_references>0 ) &&
1038 ( w->wma_decay_el->touches.access_history[
wma_history_prev( w->wma_decay_el->touches.next_p ) ].d_cycle < current_cycle ) &&
1085 token temp_token, *t;
1090 temp_token.
w = msc->
w;
1107 temp_token.
w = msc->
w;
1124 wma_pooled_wme_set::iterator wme_p;
1132 temp_el = (*wme_p)->wma_decay_el;
1145 std::string msg(
"WMA @" );
1152 msg.append(
"activate " );
1163 print( my_agent, msg.c_str() );
1232 str.append(
" @ d" );
1234 to_string( ref.
d_cycle, temp );
1237 str.append(
" (-" );
1239 to_string( cycle_diff, temp );
1250 unsigned int p = history->
next_p;
1256 buffer.append(
"history (" );
1262 buffer.append( temp );
1264 buffer.append(
"/" );
1267 buffer.append( temp );
1269 buffer.append(
", first @ d" );
1272 buffer.append( temp );
1275 buffer.append(
"):" );
1284 buffer.append(
"\n " );
1294 buffer.append(
"\n\n" );
1295 buffer.append(
"considering WME for decay @ d" );
1299 buffer.append( temp );
1304 buffer.assign(
"WME has no decay history" );
1328 bool forgot_something =
false;
1339 if ( forgot_something )
1343 const char *msg =
"\n\nWMA: BEGIN FORGOTTEN WME LIST\n\n";
1345 print( my_agent, const_cast<char *>( msg ) );
1355 if ( wm_removal_diff > 0 )
1362 const char *msg =
"\nWMA: END FORGOTTEN WME LIST\n\n";
1364 print( my_agent, const_cast<char *>( msg ) );