<commit_before>/** distributed_moses.h --- 
 *
 * Copyright (C) 2010 Novamente LLC
 *
 * Author: <NAME>
 *
 * 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.
 */


#ifndef _OPENCOG_DISTRIBUTED_MOSES_H
#define _OPENCOG_DISTRIBUTED_MOSES_H

#include <stdio.h>
#include <stropts.h>

#include <ext/stdio_filebuf.h>

#include <boost/program_options.hpp>

#include <opencog/util/iostreamContainer.h>

#include "../main/moses_options_names.h"
#include "metapopulation.h"
#include "moses.h"

namespace moses
{

using namespace boost::program_options;

/**
 * generate a command line that launches moses-exec with exemplar base
 * 'tr' keeping all initial options, but running over one generation
 * and returning the adequate information to merge the result with the
 * metapopulation
 */
string build_command_line(const variables_map& vm, 
                          const combo_tree& tr, unsigned int max_evals) {
    string res("./moses-exec");
    // replicate initial command's options, except:
    // exemplar, output options, jobs, max_evals, max_gens and log_file_dep_opt
    for(variables_map::const_iterator it = vm.begin(); it != vm.end(); it++) {
        if(it->first != exemplars_str_opt.first
           && it->first != exemplars_str_opt.first
           && it->first != output_bscore_opt.first
           && it->first != output_complexity_opt.first
           && it->first != output_eval_number_opt.first
           && it->first != jobs_opt.first
           && it->first != max_evals_opt.first
           && it->first != max_gens_opt.first
           && !it->second.defaulted()) {
            res += string(" --") + it->first + " " + to_string(it->second);
        }
    }
    // add exemplar option
    stringstream ss;
    ss << tr;
    res += string(" -") + exemplars_str_opt.second + " \"" + ss.str() + "\"";
    // add output options
    res += string(" -") + output_bscore_opt.second + " 1";
    res += string(" -") + output_complexity_opt.second + " 1";
    res += string(" -") + output_eval_number_opt.second + " 1";    
    // add number of evals option
    res += string(" -") + max_evals_opt.second + " " 
        + boost::lexical_cast<string>(max_evals);
    // add one generation option
    res += string(" -") + max_gens_opt.second + " 1";
    // add log option determined name option
    res += string(" -") + log_file_dep_opt_opt.second;
    
    OC_ASSERT(res.size() < 255,
              "It is unlikely the OS support such a long name %s, the only thing"
              " to do is upgrade the code so that it can express this command"
              " in less than 255 chars", res.c_str());

    std::cout << res << std::endl;

    return res;
}

/**
 * read the istream, add the candidates, fill max_evals
 */
void parse_result(istream& in, metapop_candidates& candidates, int& evals) {
    while(!in.eof()) {
        string s;
        in >> s;
        if(s.empty())
            continue;
        else if(s == string(number_of_evals_str).append(":")) {
            in >> evals;
        } else {
            // read score
            score_t score = boost::lexical_cast<score_t>(s);
            // read complexity
            complexity_t complexity;
            in >> complexity;
            // read candidate
            combo_tree tr;<commit_msg>MOSES: remove assert that prevents to write command above 255 chars<commit_after>/** distributed_moses.h --- 
 *
 * Copyright (C) 2010 Novamente LLC
 *
 * Author: <NAME>
 *
 * 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.
 */


#ifndef _OPENCOG_DISTRIBUTED_MOSES_H
#define _OPENCOG_DISTRIBUTED_MOSES_H

#include <stdio.h>
#include <stropts.h>

#include <ext/stdio_filebuf.h>

#include <boost/program_options.hpp>

#include <opencog/util/iostreamContainer.h>

#include "../main/moses_options_names.h"
#include "metapopulation.h"
#include "moses.h"

namespace moses
{

using namespace boost::program_options;

/**
 * generate a command line that launches moses-exec with exemplar base
 * 'tr' keeping all initial options, but running over one generation
 * and returning the adequate information to merge the result with the
 * metapopulation
 */
string build_command_line(const variables_map& vm, 
                          const combo_tree& tr, unsigned int max_evals) {
    string res("./moses-exec");
    // replicate initial command's options, except:
    // exemplar, output options, jobs, max_evals, max_gens and log_file_dep_opt
    for(variables_map::const_iterator it = vm.begin(); it != vm.end(); it++) {
        if(it->first != exemplars_str_opt.first
           && it->first != exemplars_str_opt.first
           && it->first != output_bscore_opt.first
           && it->first != output_complexity_opt.first
           && it->first != output_eval_number_opt.first
           && it->first != jobs_opt.first
           && it->first != max_evals_opt.first
           && it->first != max_gens_opt.first
           && !it->second.defaulted()) {
            res += string(" --") + it->first + " " + to_string(it->second);
        }
    }
    // add exemplar option
    stringstream ss;
    ss << tr;
    res += string(" -") + exemplars_str_opt.second + " \"" + ss.str() + "\"";
    // add output options
    res += string(" -") + output_bscore_opt.second + " 1";
    res += string(" -") + output_complexity_opt.second + " 1";
    res += string(" -") + output_eval_number_opt.second + " 1";    
    // add number of evals option
    res += string(" -") + max_evals_opt.second + " " 
        + boost::lexical_cast<string>(max_evals);
    // add one generation option
    res += string(" -") + max_gens_opt.second + " 1";
    // add log option determined name option
    res += string(" -") + log_file_dep_opt_opt.second;

    // it seems ok so far, there is probably a limit but it is above 255
    // OC_ASSERT(res.size() < 255,
    //           "It is unlikely the OS support such a long name %s, the only thing"
    //           " to do is upgrade the code so that it can express this command"
    //           " in less than 255 chars", res.c_str());

    std::cout << res << std::endl;

    return res;
}

/**
 * read the istream, add the candidates, fill max_evals
 */
void parse_result(istream& in, metapop_candidates& candidates, int& evals) {
    while(!in.eof()) {
        string s;
        in >> s;
        if(s.empty())
            continue;
        else if(s == string(number_of_evals_str).append(":")) {
            in >> evals;
        } else {
            // read score
            score_t score = boost::lexical_cast<score_t>(s);
            // read complexity
            complexity_t complexity;
            in >> complexity;
            // read candidate
            combo_tree tr;
{'fraction_non_alphanumeric': 0.11854065496202217, 'fraction_numerical': 0.0072220146930643755, 'mean_word_length': 2.64593735814798, 'pattern_counts': {'":': 2, '<': 33, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2395241', 'n_tokens_mistral': 2462, 'n_tokens_neox': 2366, 'n_words': 746}