<commit_before>/*
 * opencog/learning/moses/representation/build_knobs.cc
 *
 * Copyright (C) 2002-2008 Novamente LLC
 * All Rights Reserved
 *
 * Written by <NAME>, <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.
 */
#include <future>

#include "build_knobs.h"
#include <opencog/comboreduct/reduct/meta_rules.h>
#include <opencog/comboreduct/reduct/general_rules.h>
#include <opencog/util/iostreamContainer.h>
#include <opencog/util/lazy_random_selector.h>
#include <opencog/util/exceptions.h>
#include <opencog/util/dorepeat.h>
#include <opencog/util/oc_omp.h>

#include <opencog/learning/moses/moses/scoring.h>
#include <opencog/learning/moses/moses/ann_scoring.h>

// uncomment this line for debug information to be given during execution
// #define DEBUG_INFO

namespace opencog { namespace moses {

using namespace std;

typedef combo_tree::sibling_iterator sib_it;
typedef combo_tree::pre_order_iterator pre_it;

build_knobs::build_knobs(RandGen& _rng,
                         combo_tree& exemplar,
                         const combo::type_tree& tt,
                         representation& rep,
                         const operator_set& ignore_ops,
                         const combo_tree_ns_set* perceptions,
                         const combo_tree_ns_set* actions,
                         contin_t step_size,
                         contin_t expansion,
                         field_set::width_t depth)
    : rng(_rng), _exemplar(exemplar), _type(tt), _rep(rep),
      _arity(tt.begin().number_of_children() - 1),
      _step_size(step_size), _expansion(expansion), _depth(depth),
      _perm_ratio(0),
      _ignore_ops(ignore_ops), _perceptions(perceptions), _actions(actions)
{
    type_tree output_type = type_tree_output_type_tree(_type);

    type_tree action_result_type_tree(id::action_result_type);
    type_tree boolean_type_tree(id::boolean_type);
    type_tree contin_type_tree(id::contin_type);
    type_tree ann_type_tree(id::ann_type);

    // Make sure that the output type is one that we can work with.
    stringstream ss;
    ss << output_type;
    stringstream art_ss; //action_result_type
    art_ss << combo::id::action_result_type;
    OC_ASSERT((((perceptions != NULL || actions != NULL) &&
                output_type == action_result_type_tree) ||
               output_type == boolean_type_tree ||
               output_type == contin_type_tree ||
               output_type == ann_type_tree),
              "Types differ. Expected '%s', got '%s'",
              art_ss.str().c_str(), ss.str().c_str());

    // Determine if the tree is of 'predicate type'. A predicate type
    // tree is a lambda which outputs boolean, but is a mixture of
    // other kinds of types as argumentss. So, for example, 
    // and (greater_than_zero (#1) greater_than_zero(#2))
    // is a logical-and of two predicates on contin_t expressions.
    bool predicate_type = false;
    if (output_type == boolean_type_tree) {
       type_tree_pre_it it = tt.begin();
       while (!predicate_type) {
         if (*it != id::boolean_type)
              predicate_type = true;
         it ++;
       }
    }

// stubbed out, for now.
predicate_type = false;
    if (predicate_type) {
// under construction. We will need to build knobs in a diferent kind of way for this.
cout << "duuude type tree is " << _type << endl;
    }
    else if (output_type == boolean_type_tree) {
        logical_canonize(_exemplar.begin());
        build_logical(_exemplar.begin());
        logical_cleanup();
    }
    else if (output_type == action_result_type_tree) {
        // Petbrain
        action_canonize(_exemplar.begin());
        build_action(_exemplar.begin());
        action_cleanup();
    }
    else if (output_type == ann_type_tree) {
        // ANN
        ann_canonize(_exemplar.begin());
        build_contin(_exemplar.begin());
    }
    else {
        OC_ASSERT(output_type == contin_type_tree,
                  "Types differ. Expected 'combo::id::contin_type', got '%s'",
                  ss.str().c_str());
<commit_msg>Fix bad loop when looking for predicates.<commit_after>/*
 * opencog/learning/moses/representation/build_knobs.cc
 *
 * Copyright (C) 2002-2008 Novamente LLC
 * All Rights Reserved
 *
 * Written by <NAME>, <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.
 */
#include <future>

#include "build_knobs.h"
#include <opencog/comboreduct/reduct/meta_rules.h>
#include <opencog/comboreduct/reduct/general_rules.h>
#include <opencog/util/iostreamContainer.h>
#include <opencog/util/lazy_random_selector.h>
#include <opencog/util/exceptions.h>
#include <opencog/util/dorepeat.h>
#include <opencog/util/oc_omp.h>

#include <opencog/learning/moses/moses/scoring.h>
#include <opencog/learning/moses/moses/ann_scoring.h>

// uncomment this line for debug information to be given during execution
// #define DEBUG_INFO

namespace opencog { namespace moses {

using namespace std;

typedef combo_tree::sibling_iterator sib_it;
typedef combo_tree::pre_order_iterator pre_it;

build_knobs::build_knobs(RandGen& _rng,
                         combo_tree& exemplar,
                         const combo::type_tree& tt,
                         representation& rep,
                         const operator_set& ignore_ops,
                         const combo_tree_ns_set* perceptions,
                         const combo_tree_ns_set* actions,
                         contin_t step_size,
                         contin_t expansion,
                         field_set::width_t depth)
    : rng(_rng), _exemplar(exemplar), _type(tt), _rep(rep),
      _arity(tt.begin().number_of_children() - 1),
      _step_size(step_size), _expansion(expansion), _depth(depth),
      _perm_ratio(0),
      _ignore_ops(ignore_ops), _perceptions(perceptions), _actions(actions)
{
    type_tree output_type = type_tree_output_type_tree(_type);

    type_tree action_result_type_tree(id::action_result_type);
    type_tree boolean_type_tree(id::boolean_type);
    type_tree contin_type_tree(id::contin_type);
    type_tree ann_type_tree(id::ann_type);

    // Make sure that the output type is one that we can work with.
    stringstream ss;
    ss << output_type;
    stringstream art_ss; //action_result_type
    art_ss << combo::id::action_result_type;
    OC_ASSERT((((perceptions != NULL || actions != NULL) &&
                output_type == action_result_type_tree) ||
               output_type == boolean_type_tree ||
               output_type == contin_type_tree ||
               output_type == ann_type_tree),
              "Types differ. Expected '%s', got '%s'",
              art_ss.str().c_str(), ss.str().c_str());

    // Determine if the tree is of 'predicate type'. A predicate type
    // tree is a lambda which outputs boolean, but is a mixture of
    // other kinds of types as argumentss. So, for example, 
    // and (greater_than_zero (#1) greater_than_zero(#2))
    // is a logical-and of two predicates on contin_t expressions.
    bool predicate_type = false;
    if (output_type == boolean_type_tree) {
       type_tree_pre_it it = tt.begin();

       // The first element will be lambda, almost always. skip it.
       if (it != tt.end()) it++;
       while (it != tt.end()) {
         if (*it != id::boolean_type) {
              predicate_type = true;
              break;
         }
         it ++;
       }
    }

    if (predicate_type) {
// under construction. We will need to build knobs in a diferent kind of way for this.
cout << "duuude type tree is " << _type << endl;
    }
    else if (output_type == boolean_type_tree) {
        logical_canonize(_exemplar.begin());
        build_logical(_exemplar.begin());
        logical_cleanup();
    }
    else if (output_type == action_result_type_tree) {
        // Petbrain
        action_canonize(_exemplar.begin());
        build_action(_exemplar.begin());
        action_cleanup();
    }
    else if (output_type == ann_type_tree) {
        // ANN
        ann_canonize(_exemplar.begin());
        build_contin(_exemplar.begin());
    }
    else {
        OC_ASSERT(output_type == contin_type_tree,
                  "Types differ. Expected 'combo::id::contin_type', got '%s'",
                  ss.str().c_str());

{'fraction_non_alphanumeric': 0.10204294350635815, 'fraction_numerical': 0.0050031269543464665, 'mean_word_length': 2.783517350157729, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 27, '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': '3133866', 'n_tokens_mistral': 2965, 'n_tokens_neox': 2854, 'n_words': 786}