OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
CountTruthValue.cc
Go to the documentation of this file.
1 /*
2  * opencog/atomspace/CountTruthValue.cc
3  *
4  * Copyright (C) 2002-2007 Novamente LLC
5  * All Rights Reserved
6  *
7  * Written by Welter Silva <welter@vettalabs.com>
8  * Guilherme Lamacie
9  * Linas Vepstas <linasvepstas@gmail.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU Affero General Public License v3 as
13  * published by the Free Software Foundation and including the exceptions
14  * at http://opencog.org/wiki/Licenses
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU Affero General Public License
22  * along with this program; if not, write to:
23  * Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  *
26  */
27 
28 #include "CountTruthValue.h"
29 
30 #include <math.h>
31 
32 #include <opencog/util/platform.h>
33 #include <opencog/util/exceptions.h>
34 
35 using namespace opencog;
36 
38 {
39  mean = m;
40  confidence = n;
41  count = c;
42 }
43 
45 {
46  mean = source.getMean();
47  confidence = source.getConfidence();
48  count = source.getCount();
49 }
51 {
52  mean = source.mean;
53  confidence = source.confidence;
54  count = source.count;
55 }
56 
58 {
59  return mean;
60 }
61 
63 {
64  return count;
65 }
66 
68 {
69  return confidence;
70 }
71 
72 std::string CountTruthValue::toString() const
73 {
74  char buf[1024];
75  sprintf(buf, "(ctv %f %f %f)",
76  static_cast<float>(getMean()),
77  static_cast<float>(getCount()),
78  static_cast<double>(getConfidence()));
79  return buf;
80 }
81 
83 {
84  const CountTruthValue *ctv = dynamic_cast<const CountTruthValue *>(&rhs);
85  if (NULL == ctv) return false;
86 
87 #define FLOAT_ACCEPTABLE_ERROR 0.000001
88  if (FLOAT_ACCEPTABLE_ERROR < fabs(mean - ctv->mean)) return false;
89  if (FLOAT_ACCEPTABLE_ERROR < fabs(confidence - ctv->confidence)) return false;
90 #define DOUBLE_ACCEPTABLE_ERROR 1.0e-14
91  if (DOUBLE_ACCEPTABLE_ERROR < fabs(1.0 - (ctv->count/count))) return false;
92 
93  return true;
94 }
95 
97 {
98  return COUNT_TRUTH_VALUE;
99 }
100 
101 // Note: this is NOT the merge formula used by PLN. This is
102 // because the CountTruthValue usally stores an integer count,
103 // and a log-probability or entropy, instead of a confidence.
105 {
106  CountTruthValuePtr oc =
107  std::dynamic_pointer_cast<CountTruthValue>(other);
108 
109  // If other is a simple truth value, *and* its not the default TV,
110  // then perhaps we should merge it in, as if it were a count truth
111  // value with a count of 1? In which case, we should add a merge
112  // routine to SimpleTruthValue to do likewise... Anyway, for now,
113  // just ignore this possible complication to the semantics.
114  if (NULL == oc) return std::static_pointer_cast<TruthValue>(clone());
115 
116  // If both this and other are counts, then accumulate to get the
117  // total count, and average together the strengths, using the
118  // count as the relative weight.
119  CountTruthValuePtr nc = std::static_pointer_cast<CountTruthValue>(clone());
120  nc->count += oc->count;
121  nc->mean = (this->mean * this->count +
122  oc->mean * oc->count) / nc->count;
123 
124  // XXX This is not the correct way to handle confidence ...
125  // The confidence will typically hold the log probability,
126  // where the probability is the normalized count. Thus
127  // the right thing to do is probably to add the probabilities!?
128  // However, this is not correct when the confidence is actually
129  // holding the mutual information ... which is additive ...
130  // Argh .. what to do?
131  // nc->confidence = oc->confidence;
132 
133  return std::static_pointer_cast<TruthValue>(nc);
134 }
135 
TruthValueType getType() const
virtual count_t getCount() const =0
#define FLOAT_ACCEPTABLE_ERROR
count_t getCount() const
virtual TruthValuePtr merge(TruthValuePtr, TVMergeStyle ms=DEFAULT) const
std::shared_ptr< CountTruthValue > CountTruthValuePtr
TruthValueType
Definition: TruthValue.h:63
std::shared_ptr< TruthValue > TruthValuePtr
Definition: TruthValue.h:85
TruthValuePtr clone() const
#define DOUBLE_ACCEPTABLE_ERROR
CountTruthValue(strength_t, confidence_t, count_t)
float strength_t
float confidence_t
double count_t
virtual confidence_t getConfidence() const =0
std::string toString() const
confidence_t getConfidence() const
virtual bool operator==(const TruthValue &rhs) const
a TruthValue that stores a mean, a confidence and the number of observations
virtual strength_t getMean() const =0
TVMergeStyle
Definition: TruthValue.h:76
strength_t getMean() const