-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlanguage.h++
262 lines (253 loc) · 7.91 KB
/
language.h++
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#ifndef LANGUAGE_HPP
#define LANGUAGE_HPP
#include "enumvector.h++"
#include "probvector.h++"
#include "experience.h++"
#include <utility>
#include <memory>
namespace Language {
static const char LANGUAGE_HPP_SCCS_ID[] __attribute__((used)) = "@(#)language.h++: $Id$";
template<typename mprobvector, typename lprobvector>
class Language: public Enumvector::Enumvector<typename mprobvector::Index,typename lprobvector::Probvector> {
public:
using Lexeme = typename lprobvector::Index;
using Meme = typename mprobvector::Index;
using lgenerator = typename lprobvector::Generator;
using mgenerator = typename mprobvector::Generator;
using Mprobvector = typename mprobvector::Probvector;
using Lprobvector = typename lprobvector::Probvector;
using base_Enumvector = typename Language::Enumvector;
Language(void) {extractmarginal();}
Language(const base_Enumvector& e):
base_Enumvector(e)
{extractmarginal();}
Language(base_Enumvector&& e):
base_Enumvector
(std::forward<decltype(e)>(e))
{extractmarginal();}
Language(const Mprobvector &marg, mgenerator &r, const int mask=-1):
marginal(marg)
{
for(auto &m: *this)
m = Lprobvector(r,mask);
newmarginal();
}
Language(Mprobvector &&marg, mgenerator &r, const int mask=-1):
marginal(std::forward<Mprobvector>(marg))
{
for(auto &m: *this)
m = Lprobvector(r,mask);
newmarginal();
}
Language(const Mprobvector &marg, const int mask=-1):
marginal(marg)
{
if(mask>0)
for(auto &m: *this)
m = Lprobvector(mask);
newmarginal();
}
Language(Mprobvector &&marg, const int mask=-1):
marginal(std::forward<Mprobvector>(marg))
{
if(mask>0)
for(auto &m: *this)
m = Lprobvector(mask);
newmarginal();
}
Language(const Mprobvector &marg, const Language &lang):
base_Enumvector(lang),
marginal(marg)
{newmarginal();}
Language(const Mprobvector &marg, Language &&lang):
base_Enumvector(std::forward<Language>(lang)),
marginal(marg)
{newmarginal();}
Language(Mprobvector &&marg, const Language &lang):
base_Enumvector(lang),
marginal(std::forward<Mprobvector>(marg))
{newmarginal();}
Language(Mprobvector &&marg, Language &&lang):
base_Enumvector(std::forward<Language>(lang)),
marginal(std::forward<Mprobvector>(marg))
{newmarginal();}
Language(const Language &lang, const int shift=0):
base_Enumvector(lang),
marginal(lang.marginal)
{
for (auto a: indices(lang.cache))
cache[a]=lang.cache[a]?std::make_shared<Mprobvector>(*lang.cache[a]):nullptr;
cshift(shift);
}
Language(const Language &lang, lgenerator &g):
base_Enumvector(lang),
marginal(lang.marginal)
{
permute(g);
}
Language(Language &&lang, const int shift=0):
base_Enumvector(std::forward<Language>(lang)),
marginal(std::forward<decltype(lang.marginal)>(lang.marginal)),
cache(std::forward<decltype(lang.cache)>(lang.cache))
{
cshift(shift);
lang.initCache();
}
Language(Language &&lang, lgenerator &g):
base_Enumvector(std::forward<Language>(lang)),
marginal(std::forward<decltype(lang.marginal)>(lang.marginal)),
cache(std::forward<decltype(lang.cache)>(lang.cache))
{
permute(g);
lang.initCache();
}
Language& cshift(const int shift=1) {
if (shift != 0) {
for (auto &a: *this)
a.cshift(shift);
cache.cshift(shift);
}
return *this;
}
Language& permute(lgenerator &g) {
Enumvector::Enumvector<Lexeme,Lexeme> p(static_cast<Lexeme>(0));
for (auto a: indices(p)) p[a] = a;
p.shuffle(g);
for (auto &a: *this)
a.permute(p);
cache.permute(p);
return *this;
}
virtual Language& operator=(const Language & l) {
base_Enumvector::operator=(l);
marginal = l.marginal;
deleteCache();
if (l.cachedead) {
cache = std::move(l.cache);
l.initCache();
}
return *this;
}
virtual Language& operator=(Language &&l) {
base_Enumvector::operator=(std::forward<decltype(l)>(l));
marginal = std::move(l.marginal);
// deleteCache();
cache = std::move(l.cache);
l.initCache();
return *this;
}
virtual Language& operator=(base_Enumvector&& e) override {
base_Enumvector::operator=(std::forward<decltype(e)>(e));
extractmarginal();
return *this;
}
virtual Language& operator=(const base_Enumvector& e) override {
base_Enumvector::operator=(e);
extractmarginal();
return *this;
}
// virtual ~Language(void) override = default; // {/*deleteCache();*/}
virtual Meme memegen(mgenerator &r) const {
return marginal.generate(r);
}
virtual Meme randommeme(mgenerator &r,
const Experience::Experience<Meme,Lexeme> &experiences = Experience::Experience<Meme,Lexeme>()) const {
return memegen(r);
}
virtual Lexeme lexgen(const Meme m, lgenerator &r) const {
return (*this)[m].generate(r);
}
virtual Meme memegen(const Lexeme l, mgenerator &r) const {
return Cachelookup(l).generate(r);
}
virtual void mememutate(const double sigma, mgenerator &r) {
marginal.mutate(sigma,r);
newmarginal();
}
virtual void lexmutate(const double sigma, lgenerator &r,
const Experience::Experience<Meme,Lexeme> &experience = Experience::Experience<Meme,Lexeme>()) {
// for (auto& m: *this)
// m.mutate(sigma,r);
(*this)[randommeme(r)].mutate(sigma,r);
deleteCache();
}
virtual const Language& decache(void) const {
cachedead = true;
return *this;
}
virtual Language& decache(void) {
cachedead = true;
return *this;
}
template<typename Network>
double match(const Network &memes,
const Meme &m1, const Meme &m2, const Language &) const {
return memes.match(m1,m2);
}
template<typename Network>
auto transmit(const Network &lexemes,
const Lexeme &l1, lgenerator &r, const Language &) const
-> typename std::remove_reference<decltype(lexemes.neighbor(l1,r))>::type {
return lexemes.neighbor(l1,r);
}
private:
Mprobvector marginal;
constexpr const Mprobvector& getmarginal(void) const {
return marginal;
}
mutable bool cachedead = false;
// So that cache can be copied, we need shared_ptr, not unique_ptr
mutable Enumvector::Enumvector<Lexeme,std::shared_ptr<Mprobvector>> cache;
void initCache(void) const {
for (auto& p: cache)
p = nullptr;
// Need to change deleteCache if initCache does anything else
// initCache and deleteCache do the same thing in this implementation
}
Mprobvector& Cachelookup(const Lexeme l) const {
cachedead = false;
if (!cache[l]) {
Enumvector::Enumvector<Meme,double> p;
for (auto m: indices(p))
p[m] = marginal[m]*(*this)[m][l];
cache[l] = std::make_shared<Mprobvector>(std::move(p));
}
return *cache[l];
}
protected: // Expose this to subclasses to use in lexmutate
void deleteCache(void) const {
for (auto& c:cache)
c = nullptr;
// Must call initCache if that is nontrivial
// initCache and deleteCache do the same thing in this implementation
}
private:
void newmarginal() {
for(auto m: indices(*this))
(*this)[m].norm() = static_cast<const decltype(marginal)>(marginal)[m];
deleteCache();
}
void extractmarginal() {
typename Mprobvector::Enumvector temp;
for (auto m: indices(*this))
temp[m] = (*this)[m].norm();
marginal = std::move(temp);
marginal.norm() = 1;
newmarginal();
}
};
template<typename mprobvector, typename lprobvector>
inline const Language<mprobvector,lprobvector> unitlang(Language<mprobvector,lprobvector>*r=nullptr) {
Language<mprobvector,lprobvector> retval;
using Lprobvector=typename lprobvector::Probvector;
Lprobvector lprob(Probvector::unitprob<typename lprobvector::Index, typename lprobvector::Generator>());
lprob += Lprobvector()*(1.0/retval.numsize()/retval.numsize());
for (auto i: indices(retval)) {
retval[i] = lprob;
lprob.cshift();
}
retval.decache();
return r?*r = std::move(retval):retval;
}
}
#endif