-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFAnswerSet.h
More file actions
204 lines (170 loc) · 4.52 KB
/
FAnswerSet.h
File metadata and controls
204 lines (170 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/***************************************************************************
* Copyright (C) 2014 by Mushthofa *
* Mushthofa.Mushthofa@Ugent.be *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
/*
* FAnswerSet.h
*
* Created on: Feb 2014
* Author: mush
*/
#ifndef FANSWERSET_H_
#define FANSWERSET_H_
#include "Atom.h"
#include "Program.h"
#include <sstream>
typedef std::map<AtomPtr, Rational> Inter_t;
class FAnswerSet
{
public:
FAnswerSet()
{ }
FAnswerSet(const Inter_t& a )
:as(a)
{}
void addAnswer(AtomPtr a, Rational r)
{
as[a] = r;
}
void delAnswer(AtomPtr a)
{
Inter_t::iterator it = as.find(a);
if(it!=as.end())
as.erase(it);
}
void clear()
{
as.clear();
}
Rational operator[](AtomPtr a) const
{
Inter_t::const_iterator it = as.find(a);
if(it!=as.end())
return it->second;
else
return Rational(0,1);
}
Rational operator[](AtomPtr a)
{
if(as.find(a)!=as.end())
return as[a];
else
return Rational(0,1);
}
std::string getStr() const
{
Inter_t::const_iterator it;
std::ostringstream os;
os<<"{";
for(it=as.begin(); it!=as.end(); ++it)
{
if(it!=as.begin())
os<<", ";
int num = it->second.getNom();
int denom = it->second.getDenom();
os<<*(it->first)<<"["<<num<<"/"<<denom<<"]";
}
os<<"}";
return os.str();
}
std::string getStrClean() const
{
Inter_t::const_iterator it;
std::ostringstream os;
os<<"{";
for(it=as.begin(); it!=as.end(); ++it)
{
std::string predName = it->first->getPredicateName();
if(predName.find("_NEW_")!=string::npos)
continue;
if(it!=as.begin())
os<<", ";
float truthval = it->second.getFloat();
os<<*(it->first)<<"["<<truthval<<"]";
}
os<<"}";
return os.str();
}
/* Translate any interpretation into a set of fuzzy facts
* a[x], b[y], ....
* into
* a <- #x.
* b <- #y.
* ....
*/
Program getFacts() const
{
Inter_t::const_iterator it;
Program result;
int c=0;
for(it=as.begin(); it!=as.end(); ++it)
{
HeadList_t hl;
hl.push_back(it->first);
AtomPtr b(new ConstantAtom(it->second));
LiteralPtr lptr (new Literal (b));
BodyList_t bl;
bl.push_back(lptr);
RulePtr rr(new
Rule(std::make_pair(hl, MAX), std::make_pair(bl, MAX), "_NEW_", c++));
result.addRule(rr);
}
return result;
}
bool operator<(const FAnswerSet& x) const
{
Inter_t::const_iterator it, itfind;
for(it=as.begin(); it!=as.end(); ++it)
{
itfind = x.as.find(it->first);
if(itfind==x.as.end() || itfind->second < it->second)
return false;
}
return true;
}
/* Merge two answer sets into one, finding the largest truth value I(a) from both
*
*/
FAnswerSet merge(const FAnswerSet& other) const
{
Inter_t o = other.as;
Inter_t res = as;
for(Inter_t::iterator it = o.begin(); it!= o.end(); ++it)
{
if(res.find(it->first) == res.end() || res[it->first] < it->second )
res[it->first] = it->second;
}
return FAnswerSet(res);
}
int getDenomLCM() const
{
Inter_t::const_iterator it;
int res = 1;
for(it=as.begin(); it!=as.end(); ++it)
res = lcm(res, it->second.getDenom());
return res;
}
Inter_t getInter() const
{
return as;
}
protected:
Inter_t as;
};
std::ostream& operator<<(std::ostream& os, const FAnswerSet& as);
#endif /* FANSWERSET_H_ */