<commit_before>/** eval-candidate.cc --- 
 *
 * Copyright (C) 2013 OpenCog Foundation
 * Copyright (C) 2014 Aidyia Limited
 *
 * Author: <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.
 */

/**
 * Evaluate the score of a candidate w.r.t. a fitness function. All
 * fitness functions available to moses are available (or at least
 * supposed to be).
 */

#include <boost/math/special_functions/binomial.hpp>
#include <boost/range/algorithm_ext/for_each.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/program_options.hpp>
#include <boost/format.hpp>

#include <opencog/util/iostreamContainer.h>
#include <opencog/util/oc_assert.h>
#include <opencog/util/oc_omp.h>

#include <opencog/comboreduct/combo/iostream_combo.h>
#include <opencog/comboreduct/table/table_io.h>
#include <opencog/learning/moses/moses/types.h>
#include <opencog/learning/moses/moses/complexity.h>

#include "../scoring/discriminating_bscore.h"
#include "../scoring/behave_cscore.h"
#include "../scoring/bscores.h"
#include "../scoring/precision_bscore.h"

#include "eval-candidate.h"

using namespace std;
using namespace opencog;
using namespace moses;
using namespace combo;
using boost::str;
using boost::trim;
using boost::math::binomial_coefficient;

/**
 * Convert a string representing a combo program in a combo_tree.
 *
 * @param combo_prog_str   the string containing the combo program
 * @param labels           a vector of labels
 * @return                 the combo_tree
 */
combo_tree str2combo_tree_label(const std::string& combo_prog_str,
                                const std::vector<std::string>& labels)
{
    // combo pogram with place holders
    std::string combo_prog_ph_str = l2ph(combo_prog_str, labels);
    std::stringstream ss(combo_prog_ph_str);
    combo_tree tr;
    ss >> tr;
    return tr;
}

/**
 * Reverse of str2combo_tree_label
 *
 * @param combo_prog       combo tree of the program
 * @param labels           a vector of labels
 * @return                 string representing the combo tree with labels
 */
std::string combo_tree2str_label(const combo_tree& tr,
                                 const std::vector<std::string>& input_labels)
{
    // Stream combo tree into a string
    stringstream ss;
    ss << tr;

    // Replace the place holders by labels
    return ph2l(ss.str(), input_labels);
}

vector<vector<string>> get_all_combo_tree_str(const eval_candidate_params& ecp)
{
    vector<vector<string>> res;

    // From command line
    if (!ecp.combo_programs.empty())
        res.push_back(ecp.combo_programs);

    // From files
    for (const std::string& combo_prg_file : ecp.combo_program_files) {
        ifstream in(combo_prg_file);
        if (in) {
            res.emplace_back();
            while (in.good()) {
                string line;
                getline(in, line);
                if(line.empty())
                    continue;
                res.back().push_back(line);
            }
        } else {
            logger().error("Error: file %s can not be found.",
                           combo_prg_file.c_str());
            exit(1);
        }
    }

    return res;
}

std::ostream& ostream_scored_trees(std::ostream& out,
                                   const vector<string>& trs_str,
                                   const vector<composite_score>& css,
                                   const eval_candidate_params& ecp,
                                   const vector<string>& ilabels) {
    unsigned size = trs_str.size();
    OC_ASSERT(size == css.size(), "size=%u != css.size()=%u", size, css.size());
    for (unsigned i = 0; i < size; ++i) {
        // Stream out score
        out << css[i].get_score() << " ";

        // Stream out tree
        out << trs_str[i];

        out << std::endl;
    }
    return out;
}

int main(int argc, char** argv)
{
    namespace po = boost::program_options;
    eval_candidate_params ecp;
    unsigned long rand_seed;
    string log_level;
    static const string default_log_file("eval-candidate.log");
    string log_file;

    // Declare the supported options.
    po::options_description desc("Allowed options");
    desc.add_options()
        ("help,h", "Produce help message.\n")

        ("r,random-seed", po::value<unsigned long>(&rand_seed)->default_value(1),
         "Random seed.\n")

        ("j,jobs",
         po::value<unsigned>(&ecp.jobs),
         "Number of jobs allocated for evaluation.\n")

        // IO<commit_msg>Forgot to add a default jobs value (1) for eval-candidate<commit_after>/** eval-candidate.cc --- 
 *
 * Copyright (C) 2013 OpenCog Foundation
 * Copyright (C) 2014 Aidyia Limited
 *
 * Author: <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.
 */

/**
 * Evaluate the score of a candidate w.r.t. a fitness function. All
 * fitness functions available to moses are available (or at least
 * supposed to be).
 */

#include <boost/math/special_functions/binomial.hpp>
#include <boost/range/algorithm_ext/for_each.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/program_options.hpp>
#include <boost/format.hpp>

#include <opencog/util/iostreamContainer.h>
#include <opencog/util/oc_assert.h>
#include <opencog/util/oc_omp.h>

#include <opencog/comboreduct/combo/iostream_combo.h>
#include <opencog/comboreduct/table/table_io.h>
#include <opencog/learning/moses/moses/types.h>
#include <opencog/learning/moses/moses/complexity.h>

#include "../scoring/discriminating_bscore.h"
#include "../scoring/behave_cscore.h"
#include "../scoring/bscores.h"
#include "../scoring/precision_bscore.h"

#include "eval-candidate.h"

using namespace std;
using namespace opencog;
using namespace moses;
using namespace combo;
using boost::str;
using boost::trim;
using boost::math::binomial_coefficient;

/**
 * Convert a string representing a combo program in a combo_tree.
 *
 * @param combo_prog_str   the string containing the combo program
 * @param labels           a vector of labels
 * @return                 the combo_tree
 */
combo_tree str2combo_tree_label(const std::string& combo_prog_str,
                                const std::vector<std::string>& labels)
{
    // combo pogram with place holders
    std::string combo_prog_ph_str = l2ph(combo_prog_str, labels);
    std::stringstream ss(combo_prog_ph_str);
    combo_tree tr;
    ss >> tr;
    return tr;
}

/**
 * Reverse of str2combo_tree_label
 *
 * @param combo_prog       combo tree of the program
 * @param labels           a vector of labels
 * @return                 string representing the combo tree with labels
 */
std::string combo_tree2str_label(const combo_tree& tr,
                                 const std::vector<std::string>& input_labels)
{
    // Stream combo tree into a string
    stringstream ss;
    ss << tr;

    // Replace the place holders by labels
    return ph2l(ss.str(), input_labels);
}

vector<vector<string>> get_all_combo_tree_str(const eval_candidate_params& ecp)
{
    vector<vector<string>> res;

    // From command line
    if (!ecp.combo_programs.empty())
        res.push_back(ecp.combo_programs);

    // From files
    for (const std::string& combo_prg_file : ecp.combo_program_files) {
        ifstream in(combo_prg_file);
        if (in) {
            res.emplace_back();
            while (in.good()) {
                string line;
                getline(in, line);
                if(line.empty())
                    continue;
                res.back().push_back(line);
            }
        } else {
            logger().error("Error: file %s can not be found.",
                           combo_prg_file.c_str());
            exit(1);
        }
    }

    return res;
}

std::ostream& ostream_scored_trees(std::ostream& out,
                                   const vector<string>& trs_str,
                                   const vector<composite_score>& css,
                                   const eval_candidate_params& ecp,
                                   const vector<string>& ilabels) {
    unsigned size = trs_str.size();
    OC_ASSERT(size == css.size(), "size=%u != css.size()=%u", size, css.size());
    for (unsigned i = 0; i < size; ++i) {
        // Stream out score
        out << css[i].get_score() << " ";

        // Stream out tree
        out << trs_str[i];

        out << std::endl;
    }
    return out;
}

int main(int argc, char** argv)
{
    namespace po = boost::program_options;
    eval_candidate_params ecp;
    unsigned long rand_seed;
    string log_level;
    static const string default_log_file("eval-candidate.log");
    string log_file;

    // Declare the supported options.
    po::options_description desc("Allowed options");
    desc.add_options()
        ("help,h", "Produce help message.\n")

        ("r,random-seed", po::value<unsigned long>(&rand_seed)->default_value(1),
         "Random seed.\n")

        ("j,jobs",
         po::value<unsigned>(&ecp.jobs)->default_value(1),
         "Number of jobs allocated for evaluation.\n")

        // IO
{'fraction_non_alphanumeric': 0.11225669685873964, 'fraction_numerical': 0.005588745423010214, 'mean_word_length': 2.6088317107093184, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6806511', 'n_tokens_mistral': 3239, 'n_tokens_neox': 3091, 'n_words': 882}