OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
URECommons.h
Go to the documentation of this file.
1 /*
2  * RECommons.h
3  *
4  * Copyright (C) 2014 Misgana Bayetta
5  *
6  * Author: Misgana Bayetta <misgana.bayetta@gmail.com> Oct 2014
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Affero General Public License v3 as
10  * published by the Free Software Foundation and including the exceptions
11  * at http://opencog.org/wiki/Licenses
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Affero General Public License
19  * along with this program; if not, write to:
20  * Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 #ifndef PLNCOMMONS_H_
24 #define PLNCOMMONS_H_
25 
26 #include <opencog/util/dorepeat.h>
27 #include <opencog/util/random.h>
28 
32 
33 namespace opencog {
34 
35 using namespace std;
36 
40 class URECommons {
42 
43 public:
44  const float FITNESS_PARAM = 0.9;
45  URECommons(AtomSpace& as);
46 
55  Handle create_bindLink(Handle himplicant, bool is_quoted = false)
56  throw (opencog::InvalidParamException);
57 
61  bool exists_in(Handle& hlink, Handle& h);
62 
63  /*
64  * Generate UUID
65  */
66  string get_unique_name(Handle& h);
67 
71  Handle replace_nodes_with_varnode(Handle& himplication_link, Type t =
72  VARIABLE_NODE);
73 
77  Handle change_node_types(Handle& h, map<Handle, Handle>& replacement_map);
78 
83  void get_root_links(Handle h, HandleSeq& parents);
84 
90  template<class Type>
91  Type tournament_select(map<Type, float> tfitnes_map) {
92  // Nothing to select, return the nullptr rule
93  if (tfitnes_map.empty())
94  return Type();
95 
96  // Something to select, randomly pick (without replacement)
97  // about half of the rules and return the best
98  // TODO change the way pick_size is calculated
99  size_t pick_size = std::max(static_cast<size_t>(1),
100  tfitnes_map.size() / 2);
101  multimap<float, Type> winners;
102  dorepeat(pick_size)
103  {
104  auto el = rand_element(tfitnes_map);
105  winners.insert( { el.second, el.first });
106  }
107  return winners.rbegin()->second;
108  }
120  float tv_fitness(Handle h);
121 };
122 
123 } // ~namespace opencog
124 
125 #endif /* PLNCOMMONS_H_ */
std::vector< Handle > HandleSeq
a list of handles
Definition: Handle.h:246
AtomSpace & _as
Definition: URECommons.h:41
unsigned short Type
type of Atoms, represented as short integer (16 bits)
Definition: types.h:40