OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
FuzzyTruthValue.cc
Go to the documentation of this file.
1 /*
2  * opencog/atomspace/FuzzyTruthValue.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 
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License v3 as
12  * published by the Free Software Foundation and including the exceptions
13  * at http://opencog.org/wiki/Licenses
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program; if not, write to:
22  * Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  *
25  */
26 
27 #include <math.h>
28 #include <typeinfo>
29 
30 #include <opencog/util/platform.h>
31 #include <opencog/util/exceptions.h>
32 
33 #include "FuzzyTruthValue.h"
34 
35 //#define DPRINTF printf
36 #define DPRINTF(...)
37 
38 using namespace opencog;
39 
40 #define KKK 800.0f
41 
43 {
44  mean = m;
45  count = c;
46 }
47 
49 {
50  mean = source.getMean();
51  count = source.getCount();
52 }
54 {
55  mean = source.mean;
56  count = source.count;
57 }
58 
60 {
61  return mean;
62 }
63 
65 {
66  return count;
67 }
68 
70 {
71  return countToConfidence(count);
72 }
73 
74 // This is the merge formula appropriate for PLN.
76 {
77  if (other->getType() != SIMPLE_TRUTH_VALUE) {
78  throw RuntimeException(TRACE_INFO,
79  "Don't know how to merge %s into a FuzzyTruthValue",
80  typeid(*other).name());
81  }
82 
83  if (other->getConfidence() > getConfidence()) {
84  return other->clone();
85  }
86  return clone();
87 }
88 
89 std::string FuzzyTruthValue::toString() const
90 {
91  char buf[1024];
92  sprintf(buf, "(stv %f %f)",
93  static_cast<float>(getMean()),
94  static_cast<float>(getConfidence()));
95  return buf;
96 }
97 
99 {
100  const FuzzyTruthValue *stv = dynamic_cast<const FuzzyTruthValue *>(&rhs);
101  if (NULL == stv) return false;
102 
103 #define FLOAT_ACCEPTABLE_MEAN_ERROR 0.000001
104  if (FLOAT_ACCEPTABLE_MEAN_ERROR < fabs(mean - stv->mean)) return false;
105 
106 // Converting from confidence to count and back again using single-precision
107 // float is a real accuracy killer. In particular, 2/802 = 0.002494 but
108 // converting back gives 800*0.002494/(1.0-0.002494) = 2.000188 and so
109 // comparison tests can only be accurate to about 0.000188 or
110 // thereabouts.
111 #define FLOAT_ACCEPTABLE_COUNT_ERROR 0.0002
112 
113  if (FLOAT_ACCEPTABLE_COUNT_ERROR < fabs(1.0 - (stv->count/count))) return false;
114  return true;
115 }
116 
118 {
119  return FUZZY_TRUTH_VALUE;
120 }
121 
123 {
124  // There are not quite 16 digits in double precision
125  // not quite 7 in single-precision float
126  cf = std::min(cf, 0.9999998f);
127  return static_cast<count_t>(KKK * cf / (1.0f - cf));
128 }
129 
131 {
132  return static_cast<confidence_t>(cn / (cn + KKK));
133 }
confidence_t getConfidence() const
virtual count_t getCount() const =0
virtual bool operator==(const TruthValue &rhs) const
count_t getCount() const
TruthValueType
Definition: TruthValue.h:63
std::shared_ptr< TruthValue > TruthValuePtr
Definition: TruthValue.h:85
static confidence_t countToConfidence(count_t)
#define FLOAT_ACCEPTABLE_MEAN_ERROR
static count_t confidenceToCount(confidence_t)
TruthValueType getType() const
float strength_t
strength_t mean
Mean of the strength of the TV over all observations.
float confidence_t
double count_t
TruthValuePtr clone() const
FuzzyTruthValue(strength_t mean, count_t count)
a TruthValue that stores a mean and the number of observations (strength and confidence) ...
TruthValuePtr merge(TruthValuePtr, TVMergeStyle ms=DEFAULT) const
#define FLOAT_ACCEPTABLE_COUNT_ERROR
std::string toString() const
strength_t getMean() const
count_t count
Total number of observations used to estimate the mean.
#define KKK
virtual strength_t getMean() const =0
TVMergeStyle
Definition: TruthValue.h:76