-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgenerator.cpp
More file actions
374 lines (354 loc) · 12.1 KB
/
generator.cpp
File metadata and controls
374 lines (354 loc) · 12.1 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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "gen/generator.hpp"
#include <algorithm>
#include "gen/generator_v1.hpp"
#include "gen/generator_v2.hpp"
#include "gen/generator_v3.hpp"
#include "gen/generator_v4.hpp"
#include "gen/generator_v5.hpp"
#include "gen/generator_v6.hpp"
#include "gen/generator_v7.hpp"
#include "gen/generator_v8.hpp"
#include "lang/program_util.hpp"
#include "math/semantics.hpp"
#include "mine/config.hpp"
#include "sys/log.hpp"
#include "sys/util.hpp"
Generator::UPtr Generator::Factory::createGenerator(const Config& config,
const Stats& stats) {
Generator::UPtr generator;
switch (config.version) {
case 1: {
generator.reset(new GeneratorV1(config, stats));
break;
}
case 2: {
generator.reset(new GeneratorV2(config, stats));
break;
}
case 3: {
generator.reset(new GeneratorV3(config, stats));
break;
}
case 4: {
generator.reset(new GeneratorV4(config, stats));
break;
}
case 5: {
generator.reset(new GeneratorV5(config, stats));
break;
}
case 6: {
generator.reset(new GeneratorV6(config, stats));
break;
}
case 7: {
generator.reset(new GeneratorV7(config, stats));
break;
}
case 8: {
generator.reset(new GeneratorV8(config, stats));
break;
}
default: {
Log::get().error(
"Unknown generator version: " + std::to_string(config.version), true);
break;
}
}
return generator;
}
Generator::Generator(const Config& config, const Stats& stats)
: config(config), random_program_ids(stats) {}
void Generator::generateStateless(Program& p, size_t num_operations) {
// fill program with random operations
size_t nops = 0;
while (p.ops.size() + nops < num_operations) {
auto next_op = generateOperation();
if (next_op.first.type == Operation::Type::NOP ||
next_op.first.type == Operation::Type::LPE) {
nops++;
continue;
}
size_t position = (next_op.second * (p.ops.size() + 1));
p.ops.emplace(p.ops.begin() + position, Operation(next_op.first));
if (next_op.first.type == Operation::Type::LPB) {
position = ((position + p.ops.size()) / 2) + 1;
p.ops.emplace(p.ops.begin() + position, Operation(Operation::Type::LPE));
}
}
}
void Generator::applyPostprocessing(Program& p) {
auto written_cells = fixCausality(p);
fixSingularities(p);
fixCalls(p);
ensureSourceNotOverwritten(p);
ensureTargetWritten(p, written_cells);
ensureMeaningfulLoops(p);
}
std::vector<int64_t> Generator::fixCausality(Program& p) {
// fix causality of read operations
std::vector<int64_t> written_cells;
written_cells.push_back(0);
int64_t new_cell;
for (size_t position = 0; position < p.ops.size(); position++) {
auto& op = p.ops[position];
const auto& meta = Operation::Metadata::get(op.type);
// fix source operand in new operation
if (meta.num_operands == 2 && op.source.type == Operand::Type::DIRECT &&
std::find(written_cells.begin(), written_cells.end(),
op.source.value.asInt()) == written_cells.end()) {
new_cell = op.source.value.asInt() %
written_cells.size(); // size of written_cells is >=1
if (Number(written_cells[new_cell]) == op.target.value) {
new_cell = (new_cell + 1) % written_cells.size();
}
op.source.value = Number(written_cells[new_cell]);
}
// fix target operand in new operation
if (meta.num_operands > 0 && meta.is_reading_target &&
op.type != Operation::Type::ADD &&
op.target.type == Operand::Type::DIRECT &&
std::find(written_cells.begin(), written_cells.end(),
op.target.value.asInt()) == written_cells.end()) {
new_cell = op.target.value.asInt() % written_cells.size();
if (op.source.type == Operand::Type::DIRECT &&
Number(written_cells[new_cell]) == op.source.value) {
new_cell = (new_cell + 1) % written_cells.size();
}
op.target.value = Number(written_cells[new_cell]);
}
// check if target cell not written yet
if (meta.is_writing_target && op.target.type == Operand::Type::DIRECT &&
std::find(written_cells.begin(), written_cells.end(),
op.target.value.asInt()) == written_cells.end()) {
// update written cells
written_cells.push_back(op.target.value.asInt());
}
}
return written_cells;
}
void Generator::fixSingularities(Program& p) {
static const Operand tmp(Operand::Type::DIRECT, 26); // magic number
static const int64_t max_exponent = 5; // magic number
for (size_t i = 0; i < p.ops.size(); i++) {
if ((p.ops[i].type == Operation::Type::DIV ||
p.ops[i].type == Operation::Type::DIF ||
p.ops[i].type == Operation::Type::MOD) &&
(p.ops[i].source.type == Operand::Type::DIRECT)) {
auto divisor = p.ops[i].source;
p.ops.insert(p.ops.begin() + i,
Operation(Operation::Type::MOV, tmp, divisor));
p.ops.insert(p.ops.begin() + i + 1,
Operation(Operation::Type::EQU, tmp,
Operand(Operand::Type::CONSTANT, 0)));
p.ops.insert(p.ops.begin() + i + 2,
Operation(Operation::Type::ADD, divisor, tmp));
i += 3;
} else if (p.ops[i].type == Operation::Type::POW) {
if (p.ops[i].source.type == Operand::Type::CONSTANT &&
(p.ops[i].source.value < Number::TWO ||
Number(max_exponent) < p.ops[i].source.value)) {
p.ops[i].source.value = (Random::get().gen() % (max_exponent - 2)) + 2;
} else if (p.ops[i].source.type == Operand::Type::DIRECT &&
Random::get().gen() % 5 > 0) // magic number
{
p.ops[i].source.type = Operand::Type::CONSTANT;
}
} else if (p.ops[i].type == Operation::Type::SEQ) {
auto target = p.ops[i].target;
p.ops.insert(p.ops.begin() + i,
Operation(Operation::Type::MAX, target,
Operand(Operand::Type::CONSTANT, Number::ZERO)));
i++;
}
}
}
void Generator::fixCalls(Program& p) {
for (auto& op : p.ops) {
if (op.type != Operation::Type::SEQ) {
continue;
}
bool reset = false;
if (op.source.type == Operand::Type::CONSTANT) {
try {
auto id = UID::castFromInt(op.source.value.asInt());
reset = !random_program_ids.exists(id);
} catch (const std::exception& e) {
reset = true;
}
} else {
reset = true;
}
if (reset) {
auto id_num = random_program_ids.get().castToInt();
op.source = Operand(Operand::Type::CONSTANT, Number(id_num));
}
}
}
void Generator::ensureSourceNotOverwritten(Program& p) {
// make sure that the initial value does not get overridden immediately
bool resets;
for (auto& op : p.ops) {
if (op.target.type == Operand::Type::DIRECT &&
op.target.value == Program::INPUT_CELL) {
resets = false;
if (op.type == Operation::Type::MOV ||
ProgramUtil::isWritingRegion(op.type)) {
resets = true;
} else if ((op.source == op.target) &&
(op.type == Operation::Type::SUB ||
op.type == Operation::Type::TRN ||
op.type == Operation::Type::DIV ||
op.type == Operation::Type::DIF ||
op.type == Operation::Type::MOD)) {
resets = true;
}
if (resets) {
op.target.value = (Random::get().gen() % 4) + 1;
}
} else if (op.source.type == Operand::Type::DIRECT &&
op.source.value == Program::INPUT_CELL) {
break;
}
}
}
void Generator::ensureTargetWritten(Program& p,
const std::vector<int64_t>& written_cells) {
// make sure that the target value gets written
bool written = false;
for (auto& op : p.ops) {
if (op.type != Operation::Type::LPB &&
Operation::Metadata::get(op.type).num_operands == 2 &&
op.target.type == Operand::Type::DIRECT &&
op.target.value == Program::OUTPUT_CELL) {
written = true;
break;
}
}
if (!written) {
int64_t source = Program::INPUT_CELL;
if (!written_cells.empty()) {
source = written_cells.at(Random::get().gen() % written_cells.size());
}
p.ops.push_back(
Operation(Operation::Type::MOV,
Operand(Operand::Type::DIRECT, Program::OUTPUT_CELL),
Operand(Operand::Type::DIRECT, source)));
}
}
void Generator::ensureMeaningfulLoops(Program& p) {
// make sure loops do something
Operand mem;
int64_t num_ops = 0;
bool can_descent = false;
for (size_t i = 0; i < p.ops.size(); i++) {
switch (p.ops[i].type) {
case Operation::Type::LPB: {
mem = p.ops[i].target;
can_descent = false;
num_ops = 0;
break;
}
case Operation::Type::ADD:
case Operation::Type::MUL:
case Operation::Type::POW:
num_ops++;
break;
case Operation::Type::SUB:
case Operation::Type::MOV:
case Operation::Type::DIV:
case Operation::Type::DIF:
case Operation::Type::MOD:
case Operation::Type::GCD:
case Operation::Type::LEX:
case Operation::Type::BIN:
case Operation::Type::EQU:
case Operation::Type::NEQ:
case Operation::Type::LEQ:
case Operation::Type::GEQ:
num_ops++;
if (p.ops[i].target == mem) {
can_descent = true;
}
break;
case Operation::Type::LPE: {
if (!can_descent) {
Operation dec;
dec.target = mem;
dec.source =
Operand(Operand::Type::CONSTANT, (Random::get().gen() % 9) + 1);
switch (Random::get().gen() % 4) {
case 0:
dec.type = Operation::Type::TRN;
break;
case 1:
dec.type = Operation::Type::DIV;
dec.source.value = Semantics::add(dec.source.value, 1);
break;
case 2:
dec.type = Operation::Type::DIF;
dec.source.value = Semantics::add(dec.source.value, 1);
break;
case 3:
dec.type = Operation::Type::MOD;
dec.source.value = Semantics::add(dec.source.value, 1);
break;
}
p.ops.insert(p.ops.begin() + i, dec);
i++;
}
if (num_ops < 2) {
for (int64_t j = (Random::get().gen() % 3) + 3; j > 0; j--) {
auto op = generateOperation();
if (op.first.type != Operation::Type::LPB &&
op.first.type != Operation::Type::LPE) {
p.ops.insert(p.ops.begin() + i, op.first);
i++;
}
}
}
break;
}
default:
break;
}
}
}
MultiGenerator::MultiGenerator(const Settings& settings, const Stats& stats)
: Generator(Generator::Config(), stats) {
const auto config = ConfigLoader::load(settings);
configs.clear();
generators.clear();
for (auto c : config.generators) {
try {
auto gen = Generator::Factory::createGenerator(c, stats);
generators.emplace_back(std::move(gen));
configs.push_back(c);
} catch (std::exception& e) {
Log::get().warn(std::string(e.what())); // treat only as warning
}
}
if (generators.empty()) {
Log::get().error("No valid generators configurations found", true);
}
current_generator = Random::get().gen() % generators.size();
}
Program MultiGenerator::generateProgram() {
current_generator = (current_generator + 1) % generators.size();
return generators[current_generator]->generateProgram();
}
std::pair<Operation, double> MultiGenerator::generateOperation() {
return generators[current_generator]->generateOperation();
}
bool MultiGenerator::supportsRestart() const {
// all generator need to support restart
return std::all_of(
generators.begin(), generators.end(),
[](const Generator::UPtr& gen) { return gen->supportsRestart(); });
}
bool MultiGenerator::isFinished() const {
// finished if all are finished
return std::all_of(
generators.begin(), generators.end(),
[](const Generator::UPtr& gen) { return gen->isFinished(); });
};