OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
AttentionBank.cc
Go to the documentation of this file.
1 /*
2  * opencog/atomspace/AttentionBank.cc
3  *
4  * Copyright (C) 2013 Linas Vepstas <linasvepstas@gmail.com>
5  * All Rights Reserved
6  *
7  * Written by Joel Pitt
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License v3 as
11  * published by the Free Software Foundation and including the exceptions
12  * at http://opencog.org/wiki/Licenses
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program; if not, write to:
21  * Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24 
25 #include <boost/bind.hpp>
26 
27 #include <opencog/util/Config.h>
28 #include "AttentionBank.h"
29 #include "AtomTable.h"
30 
31 using namespace opencog;
32 
34 {
35  startingFundsSTI = fundsSTI = config().get_int("STARTING_STI_FUNDS");
36  startingFundsLTI = fundsLTI = config().get_int("STARTING_LTI_FUNDS");
38 
40  atab.AVChangedSignal().connect(
41  boost::bind(&AttentionBank::AVChanged, this, _1, _2, _3));
42 }
43 
49 {
50  AVChangedConnection.disconnect();
51 }
52 
54 
55 
57  AttentionValuePtr new_av)
58 {
59  // Add the old attention values to the AtomSpace funds and
60  // subtract the new attention values from the AtomSpace funds
61  updateSTIFunds(old_av->getSTI() - new_av->getSTI());
62  updateLTIFunds(old_av->getLTI() - new_av->getLTI());
63 
64  logger().fine("AVChanged: fundsSTI = %d, old_av: %d, new_av: %d",
65  fundsSTI, old_av->getSTI(), new_av->getSTI());
66 
67  // Check if the atom crossed into or out of the AttentionalFocus
68  // and notify any interested parties
69  if (old_av->getSTI() < attentionalFocusBoundary and
70  new_av->getSTI() >= attentionalFocusBoundary)
71  {
72  AFCHSigl& afch = AddAFSignal();
73  afch(h, old_av, new_av);
74  }
75  else if (new_av->getSTI() < attentionalFocusBoundary and
76  old_av->getSTI() >= attentionalFocusBoundary)
77  {
78  AFCHSigl& afch = RemoveAFSignal();
79  afch(h, old_av, new_av);
80  }
81 }
82 
84 {
85  std::lock_guard<std::mutex> lock(lock_funds);
86  return startingFundsSTI - fundsSTI;
87 }
88 
90 {
91  std::lock_guard<std::mutex> lock(lock_funds);
92  return startingFundsLTI - fundsLTI;
93 }
94 
96 {
97  std::lock_guard<std::mutex> lock(lock_funds);
98  return fundsSTI;
99 }
100 
102 {
103  std::lock_guard<std::mutex> lock(lock_funds);
104  return fundsLTI;
105 }
106 
108 {
109  std::lock_guard<std::mutex> lock(lock_funds);
110  fundsSTI += diff;
111  return fundsSTI;
112 }
113 
115 {
116  std::lock_guard<std::mutex> lock(lock_funds);
117  fundsLTI += diff;
118  return fundsLTI;
119 }
120 
122 {
123  std::lock_guard<std::mutex> lock(lock_maxSTI);
124  maxSTI.update(m);
125 }
126 
128 {
129  std::lock_guard<std::mutex> lock(lock_minSTI);
130  minSTI.update(m);
131 }
132 
134 {
135  std::lock_guard<std::mutex> lock(lock_maxSTI);
136  if (average) {
137  return (AttentionValue::sti_t) maxSTI.recent;
138  } else {
139  return maxSTI.val;
140  }
141 }
142 
144 {
145  std::lock_guard<std::mutex> lock(lock_minSTI);
146  if (average) {
147  return (AttentionValue::sti_t) minSTI.recent;
148  } else {
149  return minSTI.val;
150  }
151 }
152 
154 {
156 }
157 
159 {
160  attentionalFocusBoundary = boundary;
161  return boundary;
162 }
163 
164 float AttentionBank::getNormalisedSTI(AttentionValuePtr av, bool average, bool clip) const
165 {
166  // get normalizer (maxSTI - attention boundary)
167  int normaliser;
168  float val;
169  AttentionValue::sti_t s = av->getSTI();
170  if (s > getAttentionalFocusBoundary()) {
171  normaliser = (int) getMaxSTI(average) - getAttentionalFocusBoundary();
172  if (normaliser == 0) {
173  return 0.0f;
174  }
175  val = (s - getAttentionalFocusBoundary()) / (float) normaliser;
176  } else {
177  normaliser = -((int) getMinSTI(average) + getAttentionalFocusBoundary());
178  if (normaliser == 0) {
179  return 0.0f;
180  }
181  val = (s + getAttentionalFocusBoundary()) / (float) normaliser;
182  }
183  if (clip) {
184  return std::max(-1.0f, std::min(val,1.0f));
185  } else {
186  return val;
187  }
188 }
189 
191 {
192  AttentionValue::sti_t s = av->getSTI();
193  auto normaliser =
195 
196  return (s / normaliser);
197 }
198 
199 float AttentionBank::getNormalisedZeroToOneSTI(AttentionValuePtr av, bool average, bool clip) const
200 {
201  int normaliser;
202  float val;
203  AttentionValue::sti_t s = av->getSTI();
204  normaliser = getMaxSTI(average) - getMinSTI(average);
205  if (normaliser == 0) {
206  return 0.0f;
207  }
208  val = (s - getMinSTI(average)) / (float) normaliser;
209  if (clip) {
210  return std::max(0.0f, std::min(val,1.0f));
211  } else {
212  return val;
213  }
214 }
float getNormalisedSTI(AttentionValuePtr, bool average, bool clip) const
AttentionValue::sti_t setAttentionalFocusBoundary(AttentionValue::sti_t s)
std::shared_ptr< AttentionValue > AttentionValuePtr
long getSTIFunds() const
short lti_t
long-term importance type
AVCHSigl & AVChangedSignal()
Definition: AtomTable.h:347
AttentionValue::sti_t getAttentionalFocusBoundary() const
short sti_t
short-term importance type
AFCHSigl & RemoveAFSignal()
Definition: AttentionBank.h:91
AttentionValue::sti_t getMaxSTI(bool average=true) const
float getNormalisedZeroToOneSTI(AttentionValuePtr, bool average, bool clip) const
long getLTIFunds() const
AttentionValue::sti_t getMinSTI(bool average=true) const
void updateMinSTI(AttentionValue::sti_t m)
AFCHSigl & AddAFSignal()
Definition: AttentionBank.h:90
long updateLTIFunds(AttentionValue::lti_t diff)
AttentionValue::sti_t attentionalFocusBoundary
Definition: AttentionBank.h:58
boost::signals2::signal< void(const Handle &, const AttentionValuePtr &, const AttentionValuePtr &)> AFCHSigl
Definition: AttentionBank.h:43
AttentionBank(AtomTable &)
void updateMaxSTI(AttentionValue::sti_t m)
long getTotalLTI() const
long updateSTIFunds(AttentionValue::sti_t diff)
opencog::recent_val< AttentionValue::sti_t > maxSTI
Definition: AttentionBank.h:64
long getTotalSTI() const
opencog::recent_val< AttentionValue::sti_t > minSTI
Definition: AttentionBank.h:65
boost::signals2::connection AVChangedConnection
Definition: AttentionBank.h:50
void AVChanged(Handle, AttentionValuePtr, AttentionValuePtr)