-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.cpp
330 lines (224 loc) · 10.8 KB
/
manager.cpp
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//
// Created by Markus on 03.02.17.
//
#include <iostream>
#include <fstream>
#include <memory>
#include <string>
#include <map>
#include "Manage/Hypergraph.h"
#include "Trainer/TraceManager.h"
#include "util.h"
#include "Trainer/LatentAnnotation.h"
#include "Trainer/AnnotationProjection.h"
#include "Trainer/GeneticCrosser.h"
void test_main();
void test_subgraph();
void test_fp_io();
template<typename T1, typename T2>
using MAPTYPE = typename std::unordered_map<T1, T2>;
int main() {
test_main();
test_subgraph();
test_fp_io();
}
void test_main() {
std::vector<std::string> nLabels{"hello", "world", "this"};
auto const nLabelsPtr = std::make_shared<const std::vector<std::string>>(nLabels);
std::vector<EdgeLabelT> eLabels{42};
auto const eLabelsPtr = std::make_shared<const std::vector<EdgeLabelT>>(eLabels);
HypergraphPtr<std::string> hg = std::make_shared<Hypergraph<std::string>>(
nLabelsPtr
, eLabelsPtr
);
auto node1 = hg->create("hello");
auto node2 = hg->create("world");
auto node3 = hg->create("this");
std::clog << node1->get_label();
std::map<Element<Node<std::string>>, std::string> mymap;
mymap[node1] = "hallo";
mymap[node2] = "welt";
mymap[node3] = "Dies";
std::clog << mymap[node2] << std::endl;
std::vector<Element<Node<std::string>>> sources{node1, node2};
hg->add_hyperedge(42, node3, sources);
for (auto e : hg->get_outgoing_edges(node2))
std::clog << "(" << e.first << "," << e.second << ")";
std::clog << std::endl;
std::clog << "Fun with iterators:" << std::endl;
for (auto t : *hg) {
std::clog << t << " ";
}
std::clog << std::endl;
for (auto const_it = hg->cbegin(); const_it != hg->cend(); ++const_it) {
std::clog << *const_it;
}
std::clog << std::endl;
std::clog << typeid(std::iterator_traits<Manage::ManagerIterator<Node<std::string>>>::difference_type).name();
std::clog << std::endl;
auto it = hg->begin();
auto it2 = it + 1;
it += 2;
std::clog << *it;
std::clog << *it2;
it -= 1;
std::clog << *it;
std::clog << (it[2]);
std::clog << (hg->end() - it);
std::clog << std::endl;
Trainer::TraceManagerPtr<std::string, std::string> traceManager
= std::make_shared<Trainer::TraceManager2<std::string, std::string>>(
nLabelsPtr
, eLabelsPtr
);
traceManager->create("The best HG!", hg, node1);
std::filebuf fb;
if (fb.open("/tmp/manager.txt", std::ios::out)) {
std::ostream out(&fb);
traceManager->serialize(out);
fb.close();
}
if (fb.open("/tmp/manager.txt", std::ios::in)) {
std::istream in(&fb);
Trainer::TraceManagerPtr<std::string, std::string> tm2 = Trainer::TraceManager2<std::string
, std::string
>::deserialize(in);
std::clog << "Read in TraceManager with Trace#: " << tm2->size() << "and Trace 1 contrains "
<< (*tm2)[0].get_hypergraph()->size();
tm2->serialize(std::cout);
}
}
void test_subgraph() {
std::clog << "######################## \n ################## \n ##################### \n Testing Subgraph: \n";
std::vector<int> graphNLabels{1, 2, 3, 4, 5, 6, 7, 8};
auto graphNLabelsPtr = std::make_shared<std::vector<int>>(graphNLabels);
std::vector<EdgeLabelT> graphELabels{42, 43, 44, 45};
auto graphELabelsPtr = std::make_shared<std::vector<EdgeLabelT>>(graphELabels);
HypergraphPtr<int> graph = std::make_shared<Hypergraph<int>>(graphNLabelsPtr, graphELabelsPtr);
std::vector<Element<Node<int>>> graphNodes;
for (const auto l : graphNLabels)
graphNodes.push_back(graph->create(l));
graph->add_hyperedge(
42, graphNodes[0], std::vector<Element<Node<int>>>{graphNodes[2], graphNodes[1], graphNodes[3]}
);
graph->add_hyperedge(43, graphNodes[1], std::vector<Element<Node<int>>>{graphNodes[4], graphNodes[5]});
graph->add_hyperedge(44, graphNodes[3], std::vector<Element<Node<int>>>{graphNodes[6], graphNodes[5]});
graph->add_hyperedge(45, graphNodes[3], std::vector<Element<Node<int>>>{graphNodes[7]});
std::vector<int> subNLabels{1, 2, 3, 4, 5, 6, 7, 8};
auto subNLabelsPtr = std::make_shared<std::vector<int>>(subNLabels);
std::vector<EdgeLabelT> subELabels{42, 43, 44, 45};
auto subELabelsPtr = std::make_shared<std::vector<EdgeLabelT>>(subELabels);
HypergraphPtr<int> sub = std::make_shared<Hypergraph<int>>(subNLabelsPtr, subELabelsPtr);
std::vector<Element<Node<int>>> subNodes;
for (const auto l : subNLabels)
subNodes.push_back(sub->create(l));
std::clog << Manage::is_sub_hypergraph(graph, sub, graphNodes[0], subNodes[0]) << " should be true (empty)"
<< std::endl;
sub->add_hyperedge(42, subNodes[0], std::vector<Element<Node<int>>>{subNodes[3], subNodes[1], subNodes[2]});
std::clog << Manage::is_sub_hypergraph(graph, sub, graphNodes[0], subNodes[0]) << " should be true (42)"
<< std::endl;
sub->add_hyperedge(43, subNodes[1], std::vector<Element<Node<int>>>{subNodes[5], subNodes[4]});
std::clog << Manage::is_sub_hypergraph(graph, sub, graphNodes[0], subNodes[0]) << " should be true (43)"
<< std::endl;
sub->add_hyperedge(44, subNodes[3], std::vector<Element<Node<int>>>{subNodes[5], subNodes[6]});
std::clog << Manage::is_sub_hypergraph(graph, sub, graphNodes[0], subNodes[0]) << " should be true (44)"
<< std::endl;
sub->add_hyperedge(43, subNodes[3], std::vector<Element<Node<int>>>{subNodes[7]});
std::clog << Manage::is_sub_hypergraph(graph, sub, graphNodes[0], subNodes[0]) << " should be false (wrong edge)"
<< std::endl;
}
void test_fp_io() {
std::clog
<< "\n######################## \n ################## \n ##################### \n Testing Fixpoint IO: \n";
std::vector<int> graphNLabels{0, 1, 2, 3, 4, 5, 6, 7, 8};
auto graphNLabelsPtr = std::make_shared<std::vector<int>>(graphNLabels);
std::vector<EdgeLabelT> graphELabels{41, 42, 43, 44, 45, 46, 47};
auto graphELabelsPtr = std::make_shared<std::vector<EdgeLabelT>>(graphELabels);
HypergraphPtr<int> graph = std::make_shared<Hypergraph<int>>(graphNLabelsPtr, graphELabelsPtr);
std::vector<Element<Node<int>>> graphNodes;
for (const auto l : graphNLabels)
graphNodes.push_back(graph->create(l));
graph->add_hyperedge(41, graphNodes[0], std::vector<Element<Node<int>>>{graphNodes[1], graphNodes[2]});
graph->add_hyperedge(42, graphNodes[1], std::vector<Element<Node<int>>>{graphNodes[3]});
graph->add_hyperedge(43, graphNodes[2], std::vector<Element<Node<int>>>{graphNodes[3]});
graph->add_hyperedge(44, graphNodes[2], std::vector<Element<Node<int>>>{graphNodes[1], graphNodes[2]});
graph->add_hyperedge(45, graphNodes[3], std::vector<Element<Node<int>>>());
graph->add_hyperedge(46, graphNodes[0], std::vector<Element<Node<int>>>{graphNodes[4]});
graph->add_hyperedge(47, graphNodes[4], std::vector<Element<Node<int>>>());
std::vector<Double> ruleWeights {0.8, 1, 0.7, 0.3, 1, 0.2, 1};
auto tMPtr = std::make_shared<Trainer::TraceManager2<int, int>>(graphNLabelsPtr, graphELabelsPtr);
tMPtr->create(1, graph, graphNodes[0]);
auto w = (*tMPtr)[0].io_weights(ruleWeights);
std::cerr << "Inside Weights:\n";
for (auto i : w.first)
std::cerr << i.first << ": " << i.second << " ";
std::cerr << std::endl;
std::cerr << "Outside Weights:\n";
for (auto i : w.second)
std::cerr << i.first << ": " << i.second << " ";
std::cerr << std::endl;
auto top = (*tMPtr)[0].get_topological_order();
for(const auto &i : top){
std::cerr << i << " ";
}
// Latent annotations
std::cerr << "IO with Latent Annotations\n";
std::vector<Trainer::RuleTensor<double>> ruleWs {};
ruleWs.emplace_back(Trainer::RuleTensorRaw<double, 3>{1,1,1});
boost::get<Trainer::RuleTensorRaw<double, 3>>(ruleWs[0]).setValues({{{0.8}}});
ruleWs.emplace_back(Trainer::RuleTensorRaw<double, 2>{1,1});
boost::get<Trainer::RuleTensorRaw<double, 2>>(ruleWs[1]).setValues({{1}});
ruleWs.emplace_back(Trainer::RuleTensorRaw<double, 2>{1,1});
boost::get<Trainer::RuleTensorRaw<double, 2>>(ruleWs[2]).setValues({{0.7}});
ruleWs.emplace_back(Trainer::RuleTensorRaw<double, 3>{1,1,1});
boost::get<Trainer::RuleTensorRaw<double, 3>>(ruleWs[3]).setValues({{{0.3}}});
ruleWs.emplace_back(Trainer::RuleTensorRaw<double, 1>{1});
boost::get<Trainer::RuleTensorRaw<double, 1>>(ruleWs[4]).setValues({1.0});
ruleWs.emplace_back(Trainer::RuleTensorRaw<double, 2>{1,1});
boost::get<Trainer::RuleTensorRaw<double, 2>>(ruleWs[5]).setValues({{0.2}});
ruleWs.emplace_back(Trainer::RuleTensorRaw<double, 1>{1});
boost::get<Trainer::RuleTensorRaw<double, 1>>(ruleWs[6]).setValues({1.0});
std::cerr << "RuleWs done \n";
Eigen::Tensor<double, 1> rootWs(1);
rootWs.setValues({1.0});
Eigen::TensorRef<Eigen::Tensor<double,1>> root;
root = rootWs;
DCP::MAPTYPE<Element<Node<int>>, Trainer::WeightVector> insideWeights;
DCP::MAPTYPE<Element<Node<int>>, Trainer::WeightVector> outsideWeights;
for(auto n : *graph){
insideWeights[n] = Trainer::WeightVector{1};
outsideWeights[n] = Trainer::WeightVector{1};
}
std::cerr << "Starting IO by fixpoint_la\n";
(*tMPtr)[0].io_weights_la(ruleWs, root, insideWeights, outsideWeights);
std::cerr << "Inside: ";
for(auto n : *graph){
std::cerr << insideWeights.at(n)(0) << " ";
}
std::cerr << std::endl;
std::cerr << "Outside: ";
for(auto n : *graph){
std::cerr << outsideWeights.at(n)(0) << " ";
}
std::cerr << std::endl;
std::cerr << "#### Testing Projection ####\n";
std::vector<std::vector<size_t>> rules_to_nont {{0,1,2}, {1,3},{2,3},{2,1,2},{3},{0,4},{4}};
const std::vector<size_t> splits{1,1,1,1,1};
Trainer::LatentAnnotation lat(splits, std::move(rootWs), std::move(std::make_unique<std::vector<Trainer::RuleTensor<double>>>(ruleWs)));
Trainer::GrammarInfo2 gramInf(rules_to_nont, 0);
std::cerr << "before:\n";
for(const auto tensor : lat.ruleWeights){
std::cerr << tensor << "\n";
}
Trainer::LatentAnnotation projection = Trainer::project_annotation<size_t>(lat, gramInf);
std::cerr << "after:\n";
for(const auto tensor : projection.ruleWeights){
std::cerr << tensor << "\n";
}
// Genetic crossing of LAs
std::vector<bool> keepFromOne(lat.nonterminalSplits.size());
for(size_t i = 0; i < keepFromOne.size(); ++i)
keepFromOne[i] = i%2?true:false;
Trainer::mix_annotations<size_t>(lat, projection, gramInf, keepFromOne);
std::cerr << "end\n";
}