OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SimpleTruthValue.cc
Go to the documentation of this file.
1 /*
2  * opencog/atomspace/SimpleTruthValue.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 "SimpleTruthValue.h"
34 
35 //#define DPRINTF printf
36 #define DPRINTF(...)
37 
38 using namespace opencog;
39 
40 #define KKK 800.0f
41 #define CVAL 0.2f
42 
44 {
45  mean = m;
46  count = c;
47 }
48 
50 {
51  mean = source.getMean();
52  count = source.getCount();
53 }
55 {
56  mean = source.mean;
57  count = source.count;
58 }
59 
61 {
62  return mean;
63 }
64 
66 {
67  return count;
68 }
69 
71 {
72  return countToConfidence(count);
73 }
74 
75 // This is the merge formula appropriate for PLN.
77 {
78  switch(ms){
79  case DEFAULT:
80  {
81  //Based on section 5.10.2(A heuristic revision rule for STV) of the PLN book
82  if (other->getType() != SIMPLE_TRUTH_VALUE)
83  throw RuntimeException(TRACE_INFO,
84  "Don't know how to merge %s into a SimpleTruthValue using the default style",
85  typeid(*other).name());
86  auto count2 = other->getCount();
87  auto count_new = count+ count2 - std::min(count,count2)*CVAL;
88  auto mean_new = (mean*count + other->getMean()*count2)/(count+count2);
89  return std::make_shared<SimpleTruthValue>(mean_new,count_new);
90  }
91  default:
92  throw RuntimeException(TRACE_INFO,
93  "Unknown or not yet implemented merge strategy");
94  }
95 }
96 
97 std::string SimpleTruthValue::toString() const
98 {
99  char buf[1024];
100  sprintf(buf, "(stv %f %f)",
101  static_cast<float>(getMean()),
102  static_cast<float>(getConfidence()));
103  return buf;
104 }
105 
107 {
108  const SimpleTruthValue *stv = dynamic_cast<const SimpleTruthValue *>(&rhs);
109  if (NULL == stv) return false;
110 
111 #define FLOAT_ACCEPTABLE_MEAN_ERROR 0.000001
112  if (FLOAT_ACCEPTABLE_MEAN_ERROR < fabs(mean - stv->mean)) return false;
113 
114 // Converting from confidence to count and back again using single-precision
115 // float is a real accuracy killer. In particular, 2/802 = 0.002494 but
116 // converting back gives 800*0.002494/(1.0-0.002494) = 2.000188 and so
117 // comparison tests can only be accurate to about 0.000188 or
118 // thereabouts.
119 #define FLOAT_ACCEPTABLE_COUNT_ERROR 0.0002
120 
121  if (FLOAT_ACCEPTABLE_COUNT_ERROR < fabs(1.0 - (stv->count/count))) return false;
122  return true;
123 }
124 
126 {
127  return SIMPLE_TRUTH_VALUE;
128 }
129 
131 {
132  // There are not quite 16 digits in double precision
133  // not quite 7 in single-precision float
134  cf = std::min(cf, 0.9999998f);
135  return static_cast<count_t>(KKK * cf / (1.0f - cf));
136 }
137 
139 {
140  return static_cast<confidence_t>(cn / (cn + KKK));
141 }
virtual count_t getCount() const =0
count_t count
Total number of observations used to estimate the mean.
a TruthValue that stores a mean and the number of observations (strength and confidence) ...
strength_t getMean() const
TruthValuePtr merge(TruthValuePtr, TVMergeStyle ms) const
#define FLOAT_ACCEPTABLE_COUNT_ERROR
TruthValueType
Definition: TruthValue.h:63
std::shared_ptr< TruthValue > TruthValuePtr
Definition: TruthValue.h:85
static confidence_t countToConfidence(count_t)
float strength_t
float confidence_t
double count_t
TruthValueType getType() const
confidence_t getConfidence() const
#define FLOAT_ACCEPTABLE_MEAN_ERROR
virtual bool operator==(const TruthValue &rhs) const
static count_t confidenceToCount(confidence_t)
SimpleTruthValue(strength_t mean, count_t count)
#define KKK
virtual strength_t getMean() const =0
TVMergeStyle
Definition: TruthValue.h:76
std::string toString() const
strength_t mean
Mean of the strength of the TV over all observations.
#define CVAL