OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GenericTruthValue.cc
Go to the documentation of this file.
1 /*
2  * GenericTruthValue.cc
3  *
4  * Copyright (C) 2015 OpenCog Foundation
5  *
6  * Author: Leung Man Hin <https://github.com/leungmanhin>
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 <math.h>
25 #include "GenericTruthValue.h"
26 
27 using namespace opencog;
28 
29 #define KKK 800.0f
30 #define CVAL 0.2f
31 
33  strength_t f, strength_t fs,
35 {
36  positiveEvidence = pe;
37  totalEvidence = te;
38  frequency = f;
39  fuzzyStrength = fs;
40  confidence = c;
41  entropy = e;
42 }
43 
45 {
48  frequency = gtv.frequency;
50  confidence = gtv.confidence;
51  entropy = gtv.entropy;
52 }
53 
55 {
56  return positiveEvidence;
57 }
58 
60 {
61  return log(positiveEvidence);
62 }
63 
65 {
66  return totalEvidence;
67 }
68 
70 {
71  return log(totalEvidence);
72 }
73 
75 {
76  return frequency;
77 }
78 
80 {
81  return log(frequency);
82 }
83 
85 {
86  return fuzzyStrength;
87 }
88 
90 {
91  return log(fuzzyStrength);
92 }
93 
95 {
96  return confidence;
97 }
98 
100 {
101  return log(confidence);
102 }
103 
105 {
106  return entropy;
107 }
108 
110 {
111  auto other_te = gtv->getTotalEvidence();
112  auto new_pe = positiveEvidence + gtv->getPositiveEvidence();
113  auto new_te = totalEvidence + other_te
114  - std::min(totalEvidence, other_te) * CVAL;
115  auto new_f = (frequency * totalEvidence + gtv->getFrequency() * other_te)
116  / (totalEvidence + other_te);
117  auto new_fs = std::max(fuzzyStrength, gtv->getFuzzyStrength());
118  auto new_c = new_te / (new_te + KKK);
119 
120  // XXX
121  auto new_e = std::max(entropy, gtv->getEntropy());
122 
123  return std::make_shared<GenericTruthValue>(new_pe, new_te, new_f, new_fs,
124  new_c, new_e);
125 }
126 
127 
129 {
130  if (NULL == &rhs) return false;
131 
132 #define FLOAT_ACCEPTABLE_ERROR 0.000001
133  if (FLOAT_ACCEPTABLE_ERROR < fabs(frequency - rhs.frequency))
134  return false;
136  return false;
137  if (FLOAT_ACCEPTABLE_ERROR < fabs(confidence - rhs.confidence))
138  return false;
139 
140 #define DOUBLE_ACCEPTABLE_ERROR 1.0e-14
142  return false;
143  if (DOUBLE_ACCEPTABLE_ERROR < fabs(1.0 - (rhs.totalEvidence/totalEvidence)))
144  return false;
145  if (DOUBLE_ACCEPTABLE_ERROR < fabs(1.0 - (rhs.entropy/entropy)))
146  return false;
147 
148  return true;
149 }
150 
151 std::string GenericTruthValue::toString() const
152 {
153  char buf[1024];
154  sprintf(buf, "(gtv %f %f %f %f %f %f)",
155  static_cast<double>(getPositiveEvidence()),
156  static_cast<double>(getTotalEvidence()),
157  static_cast<float>(getFrequency()),
158  static_cast<float>(getFuzzyStrength()),
159  static_cast<float>(getConfidence()),
160  static_cast<double>(getEntropy()));
161  return buf;
162 }
#define FLOAT_ACCEPTABLE_ERROR
#define KKK
strength_t getFrequency() const
std::shared_ptr< GenericTruthValue > GenericTruthValuePtr
#define DOUBLE_ACCEPTABLE_ERROR
confidence_t getConfidence() const
float strength_t
count_t getPositiveEvidence() const
float confidence_t
double count_t
count_t getTotalEvidence() const
GenericTruthValuePtr merge(GenericTruthValuePtr) const
virtual bool operator==(const GenericTruthValue &rhs) const
std::string toString() const
strength_t getLogFuzzyStrength() const
strength_t getFuzzyStrength() const
count_t getLogTotalEvidence() const
confidence_t getLogConfidence() const
count_t getLogPositiveEvidence() const
strength_t getLogFrequency() const
#define CVAL
double entropy_t
GenericTruthValue(count_t, count_t, strength_t, strength_t, confidence_t, entropy_t)