@@ -64,8 +64,14 @@ static bool consolidate_rent_and_wages(HandleTrie::TrieNode* node, void* data) {
6464
6565 TokenSpreader::ImportanceChanges* changes =
6666 (TokenSpreader::ImportanceChanges*) ((DATA *) data)->importance_changes ->lookup (node->suffix );
67- value->importance -= changes->rent ;
68- value->importance += changes->wages ;
67+ ImportanceType rent = 0 ;
68+ ImportanceType wages = 0 ;
69+ if (changes != NULL ) {
70+ rent = changes->rent ;
71+ wages = changes->wages ;
72+ value->importance -= rent;
73+ value->importance += wages;
74+ }
6975
7076 // Compute amount to be spread
7177 ImportanceType largest_arity = ((DATA *) data)->largest_arity ;
@@ -80,8 +86,8 @@ static bool consolidate_rent_and_wages(HandleTrie::TrieNode* node, void* data) {
8086 LOG_LOCAL_DEBUG (\
8187 " Update " + node->suffix + " :" + \
8288 " " + std::to_string (original_importance) + \
83- " - " + std::to_string (changes-> rent ) + " (rent)" + \
84- " + " + std::to_string (changes-> wages ) + " (wages)" + \
89+ " - " + std::to_string (rent) + " (rent)" + \
90+ " + " + std::to_string (wages) + " (wages)" + \
8591 " - " + std::to_string (to_spread) + " (spread) " + \
8692 " = " + std::to_string (value->importance ));
8793 // clang-format on
@@ -134,17 +140,17 @@ static bool consolidate_stimulus(HandleTrie::TrieNode* node, void* data) {
134140// Public methods
135141
136142void TokenSpreader::distribute_wages (const dasproto::HandleCount* handle_count,
137- ImportanceType& total_to_spread ,
143+ ImportanceType& total_to_distribute ,
138144 DATA * data) {
139145 auto iterator = handle_count->map ().find (" SUM" );
140146 if (iterator == handle_count->map ().end ()) {
141147 RAISE_ERROR (" Missing 'SUM' key in HandleCount request" );
142148 }
143- unsigned int total_wages = iterator->second ;
144- LOG_DEBUG (" Total wages : " + std::to_string (total_wages ));
149+ unsigned int sum = iterator->second ;
150+ LOG_DEBUG (" Sum : " + std::to_string (sum ));
145151 for (auto pair : handle_count->map ()) {
146152 if (pair.first != " SUM" ) {
147- double normalized_amount = (((double ) pair.second ) * total_to_spread ) / total_wages ;
153+ double normalized_amount = (((double ) pair.second ) * total_to_distribute ) / sum ;
148154 LOG_LOCAL_DEBUG (" Normalized wage to handle " + pair.first + " : " +
149155 std::to_string (normalized_amount));
150156 data->importance_changes ->insert (
@@ -153,7 +159,7 @@ void TokenSpreader::distribute_wages(const dasproto::HandleCount* handle_count,
153159 }
154160}
155161
156- void TokenSpreader::spread_stimuli (const dasproto::HandleCount* request) {
162+ void TokenSpreader::spread_stimuli (const dasproto::HandleCount* request, bool spread_only_flag ) {
157163 HebbianNetwork* network = (HebbianNetwork*) request->hebbian_network ();
158164 if (network == NULL ) {
159165 return ;
@@ -173,22 +179,26 @@ void TokenSpreader::spread_stimuli(const dasproto::HandleCount* request) {
173179 // clang-format on
174180 data.total_rent = 0.0 ;
175181
176- // Collect rent
177- network->visit_nodes (true , &collect_rent, (void *) &data);
178- LOG_DEBUG (" Collected rent: " + std::to_string (data.total_rent ));
179-
180- // Distribute wages
181- ImportanceType total_to_spread = network->alienate_tokens ();
182- LOG_DEBUG (" Alienated tokens: " + std::to_string (total_to_spread));
183- total_to_spread += data.total_rent ;
184- LOG_DEBUG (" Total do spread: " + std::to_string (total_to_spread));
185- distribute_wages (request, total_to_spread, &data);
182+ if (!spread_only_flag) {
183+ // Collect rent
184+ network->visit_nodes (true , &collect_rent, (void *) &data);
185+ LOG_DEBUG (" Collected rent: " + std::to_string (data.total_rent ));
186+ // Distribute wages
187+ ImportanceType total_to_distribute = network->alienate_tokens ();
188+ LOG_DEBUG (" Alienated tokens: " + std::to_string (total_to_distribute));
189+ total_to_distribute += data.total_rent ;
190+ LOG_DEBUG (" Total do spread: " + std::to_string (total_to_distribute));
191+ distribute_wages (request, total_to_distribute, &data);
192+ }
186193
187194 // Consolidate changes
188195#if LOG_LEVEL >= LOCAL_DEBUG_LEVEL
189196 LOG_LOCAL_DEBUG (" Importances before consolidate_rent_and_wages()" );
190197 network->visit_nodes (true , &print_importance, (void *) &data);
191198#endif
199+ // Required regardless spread_only_flag because the amount each node will
200+ // spread is computed here, just after consolidating rent and wage. We could
201+ // separate it but it would require an unecessary extra tree traversal.
192202 network->visit_nodes (true , &consolidate_rent_and_wages, (void *) &data);
193203#if LOG_LEVEL >= LOCAL_DEBUG_LEVEL
194204 LOG_LOCAL_DEBUG (" Importances after consolidate_rent_and_wages()" );
0 commit comments