<commit_before>/*
 * AFImportanceDiffusionAgent.cc
 *
 * Copyright (C) 2016 Opencog Foundation
 *
 * All Rights Reserved
 *
 * Written by <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License v3 as
 * published by the Free Software Foundation and including the exceptions
 * at http://opencog.org/wiki/Licenses
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program; if not, write to:
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <chrono>
#include <thread>

#include <opencog/cogserver/server/CogServer.h>

#include "AFImportanceDiffusionAgent.h"
#include "AttentionParamQuery.h"
#include "Attention.h"
#include "AttentionStat.h"

using namespace opencog;

#ifdef DEBUG
#undef DEBUG
#endif

AFImportanceDiffusionAgent::AFImportanceDiffusionAgent(CogServer& cs) :
    ImportanceDiffusionBase(cs)
{
}

void AFImportanceDiffusionAgent::run()
{
    // Read params
    maxSpreadPercentage = std::stod(_atq.get_param_value(
                                    AttentionParamQuery::dif_spread_percentage));
    hebbianMaxAllocationPercentage =std::stod(_atq.get_param_value(
                                    AttentionParamQuery::heb_max_alloc_percentage));
    spreadHebbianOnly = std::stoi(_atq.get_param_value(
                                  AttentionParamQuery::dif_spread_hebonly));

    spreadImportance();
}

/*
 * Carries out the importance diffusion process, spreading STI along the
 * graph according to the configuration settings
 */
void AFImportanceDiffusionAgent::spreadImportance()
{
    HandleSeq diffusionSourceVector =  ImportanceDiffusionBase::diffusionSourceVector();

    // Calculate the diffusion for each source atom, and store the diffusion
    // event in a stack
    for (Handle atomSource : diffusionSourceVector) diffuseAtom(atomSource);

    // Now, process all of the outstanding diffusion events in the diffusion
    // stack
    processDiffusionStack();
}

/*
 * Returns the total amount of STI that the atom will diffuse
 *
 * Calculated as the maximum spread percentage multiplied by the atom's STI
 */
AttentionValue::sti_t AFImportanceDiffusionAgent::calculateDiffusionAmount(Handle h)
{
    updateMaxSpreadPercentage();

    return (AttentionValue::sti_t) round(get_sti(h) * maxSpreadPercentage);

    // TODO: Using integers for STI values can cause strange consequences.
    // For example, if the amount to diffuse is 0.4, it will become 0, causing
    // no diffusion to occur.
    //   * See: https://github.com/opencog/opencog/issues/676
}
<commit_msg>Update AFImportanceDiffusionAgent.cc<commit_after>/*
 * AFImportanceDiffusionAgent.cc
 *
 * Copyright (C) 2016 Opencog Foundation
 *
 * All Rights Reserved
 *
 * Written by <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License v3 as
 * published by the Free Software Foundation and including the exceptions
 * at http://opencog.org/wiki/Licenses
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program; if not, write to:
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <chrono>
#include <thread>

#include <opencog/cogserver/server/CogServer.h>

#include "AFImportanceDiffusionAgent.h"
#include "AttentionParamQuery.h"
#include "Attention.h"
#include "AttentionStat.h"

using namespace opencog;

#ifdef DEBUG
#undef DEBUG
#endif

AFImportanceDiffusionAgent::AFImportanceDiffusionAgent(CogServer& cs) :
    ImportanceDiffusionBase(cs)
{
}

void AFImportanceDiffusionAgent::run()
{
    // Read params
    maxSpreadPercentage = std::stod(_atq.get_param_value(
                                    AttentionParamQuery::dif_spread_percentage));
    hebbianMaxAllocationPercentage =std::stod(_atq.get_param_value(
                                    AttentionParamQuery::heb_max_alloc_percentage));
    spreadHebbianOnly = std::stoi(_atq.get_param_value(
                                  AttentionParamQuery::dif_spread_hebonly));

    spreadImportance();
}

/*
 * Carries out the importance diffusion process, spreading STI along the
 * graph according to the configuration settings
 */
void AFImportanceDiffusionAgent::spreadImportance()
{
    HandleSeq diffusionSourceVector =  ImportanceDiffusionBase::diffusionSourceVector();

    // Calculate the diffusion for each source atom, and store the diffusion
    // event in a stack
    for (Handle atomSource : diffusionSourceVector) diffuseAtom(atomSource);

    // Now, process all of the outstanding diffusion events in the diffusion
    // stack
    processDiffusionStack();
}

/*
 * Returns the total amount of STI that the atom will diffuse
 *
 * Calculated as the maximum spread percentage multiplied by the atom's STI
 */
AttentionValue::sti_t AFImportanceDiffusionAgent::calculateDiffusionAmount(Handle h)
{
    updateMaxSpreadPercentage();

    return (get_sti(h) * maxSpreadPercentage);
}

{'fraction_non_alphanumeric': 0.07970375595133133, 'fraction_numerical': 0.006700758243695997, 'mean_word_length': 3.8644939965694682, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4547371', 'n_tokens_mistral': 1629, 'n_tokens_neox': 1554, 'n_words': 520}