OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Target.cc
Go to the documentation of this file.
1 /*
2  * Target.cc
3  *
4  * Author: William Ma <https://github.com/williampma>
5  *
6  * Copyright (C) 2015 OpenCog Foundation
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 
24 #include <opencog/util/random.h>
27 
28 #include "Target.h"
29 
30 using namespace opencog;
31 
40 Target::Target(AtomSpace& as, const Handle& h, const Handle& hvardecl) : _as(as)
41 {
44  _selection_count = 0;
45 
46  _vardecl = hvardecl;
47 
48  HandleSeq vars = VariableListCast(_vardecl)->get_variables().varseq;
49 
50  // _varmap is a map that bases on the external space
51  for (auto& hv : vars)
53 }
54 
61 void Target::store_step(const Rule& r, const HandleSeq& premises)
62 {
63  // XXX TODO think of a good structure for storing the inference step
64  // XXX TODO if the rule was actually applied, store the change to the TV?
65  _as.add_link(SET_LINK,
67  _as.add_node(CONCEPT_NODE, r.get_name()),
68  _as.add_link(LIST_LINK, premises));
69 }
70 
77 {
78  for (auto& p : vm)
79  {
80  if (_varmap.count(p.first) == 1)
81  _varmap[p.first].insert(p.second.begin(), p.second.end());
82  }
83 }
84 
91 {
92  for (auto& p : vm)
93  {
94  if (_varmap.count(p.first) == 1)
95  _varmap[p.first].insert(p.second);
96  }
97 }
98 
107 unsigned int Target::rule_count(const Rule& r)
108 {
109  Handle hname = _as.add_node(CONCEPT_NODE, r.get_name());
110  HandleSeq q = get_neighbors(_htarget_internal, false, true,
111  SET_LINK, false);
112 
113  return std::count(q.begin(), q.end(), hname);
114 }
115 
116 
117 //==================================================================
118 
119 
123 TargetSet::TargetSet() : _total_selection(0)
124 {
125 
126 }
127 
132 {
133 
134 }
135 
140 {
142  _targets_map.clear();
143 }
144 
151 {
152  if (_targets_map.count(h) == 1)
153  return;
154 
155  _targets_map.insert(std::pair<Handle, Target>(h, Target(_history_space, h, hvardecl)));
156 }
157 
161 unsigned int TargetSet::size()
162 {
163  return _targets_map.size();
164 }
165 
179 {
180  HandleSeq handles;
181  std::vector<double> weights;
182  for (auto& p : _targets_map)
183  {
184  handles.push_back(p.first);
185 
186  // XXX TODO add more criteria to the weight calculation
187  weights.push_back(_total_selection - p.second.get_selection_count() + 1);
188  }
189 
190  Target& t = _targets_map.at(handles[randGen().randDiscrete(weights)]);
192 
194 
195  return t;
196 }
197 
205 {
206  return _targets_map.at(h);
207 }
Handle add_node(Type t, const std::string &name="", bool async=false)
Definition: AtomSpace.cc:135
unsigned int size()
Definition: Target.cc:161
std::vector< Handle > HandleSeq
a list of handles
Definition: Handle.h:246
HandleSeq get_neighbors(const Handle &h, bool fanin, bool fanout, Type desiredLinkType, bool subClasses)
Definition: AtomUtils.cc:130
Handle _vardecl
Definition: Target.h:120
void store_varmap(VarMultimap &vm)
Definition: Target.cc:76
void emplace(Handle h, Handle hvardecl)
Definition: Target.cc:150
Handle _htarget_external
Definition: Target.h:116
VarMultimap _varmap
Definition: Target.h:121
unsigned int _selection_count
Definition: Target.h:118
unsigned int _total_selection
Definition: Target.h:142
void increment_selection_count()
Definition: Target.h:63
std::map< Handle, UnorderedHandleSet > VarMultimap
static VariableListPtr VariableListCast(const Handle &h)
Definition: VariableList.h:95
unsigned int rule_count(const Rule &r)
Definition: Target.cc:107
std::map< Handle, Handle > VarMap
AtomSpace & _as
Definition: Target.h:123
void store_step(const Rule &r, const HandleSeq &premises)
Definition: Target.cc:61
Target(AtomSpace &as, const Handle &h, const Handle &hvardecl)
Definition: Target.cc:40
std::map< Handle, Target > _targets_map
Definition: Target.h:140
Target & select()
Definition: Target.cc:178
std::unordered_set< Handle, handle_hash > UnorderedHandleSet
a hash that associates the handle to its unique identificator
Definition: Handle.h:250
Handle add_link(Type t, const HandleSeq &outgoing, bool async=false)
Definition: AtomSpace.cc:175
string & get_name()
Definition: Rule.cc:78
Handle _htarget_internal
Definition: Target.h:117
Target & get(Handle &h)
Definition: Target.cc:204
void clear()
Clear the atomspace, remove all atoms.
Definition: AtomSpace.cc:355
Handle add_atom(AtomPtr atom, bool async=false)
Definition: AtomSpace.cc:100
AtomSpace _history_space
Definition: Target.h:141