-
Notifications
You must be signed in to change notification settings - Fork 2
/
traversal.php
179 lines (152 loc) · 5.33 KB
/
traversal.php
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
<?php
require_once("vendor/autoload.php");
require_once("queryGraph.php");
require_once("scoring.php");
// TODO: Globals...
$population = 500;
$numMutants = 4;
// TODO: Some example inputs. These should be passed in differently.
//$inputs = array(1, 2, 3, 4);
//$outputs = array(array(1, 1), array(2, 4), array(3, 9), array(4, 16));
$inputs = array(array(1, 2), array(5, 5), array(2, 3));
$outputs = array(3, 10, 5);
//$inputs = array(array(1, 2), array(5, 2));
//$outputs = array(array(2, 1), array(2, 5));
class Candidate {
function __construct($t, $conn) {
global $inputs;
global $outputs;
$this->term = $t;
$query = $t->toQuery();
$this->printedTerm = (string)$query;
$this->score = 0.0;
$this->score += scoreQuery($query);
$this->results = "ERROR";
try {
$this->results = r\expr($inputs)->map($query)->run($conn);
// TODO: This scoring is bad. Also we should check if the result matched exactly
// to extract valid candidates.
$numCorrect = 0;
for ($i = 0; $i < count($inputs); ++$i) {
if (print_r($outputs[$i], true) == print_r($this->results[$i], true)) {
$numCorrect++;
}
$this->score += scoreResult($outputs[$i], $this->results[$i]);
}
if ($numCorrect == count($inputs)) {
// Found a valid candidate.
// Give it an extra score boost.
$this->score += 500.0;
}
} catch (r\Exceptions\RqlUserError $e) {
} catch (r\Exceptions\RqlServerError $e) {
}
}
var $term;
var $printedTerm;
var $score;
var $results;
}
function mutate($currentCandidates) {
global $numMutants;
// TODO: Hard-coded config
$conn = r\connect("localhost");
// Take each candidate from the current candidates list and mutate
// it with a chance propotional to its score.
$newCandidates = $currentCandidates;
$maxScore = 0.0;
foreach ($currentCandidates as $candidate) {
if ($candidate->score > $maxScore) {
$maxScore = $candidate->score;
}
}
foreach ($currentCandidates as $candidate) {
$TOP_LEVEL_MUTATION_CHANCE = 0.5;
for ($i = 0; $i < $numMutants; ++$i) {
// Always mutate the top candidate
$toss = rand(0, $maxScore);
if ($toss <= $candidate->score) {
$hasMutated = false;
$mutant = $candidate->term->createMutant($TOP_LEVEL_MUTATION_CHANCE, $hasMutated);
if ($hasMutated) {
$newCandidates[] = new Candidate($mutant, $conn);
}
}
}
}
$conn->close();
return $newCandidates;
}
function pruneCandidates($newCandidates) {
global $population;
// Sort the new candidates
usort($newCandidates, function ($a, $b) { if ($a->score > $b->score) return -1; if ($a->score == $b->score) return 0; return 1; });
// Remove duplicates and prune the low end (so we are left with at most $population)
$prev = null;
$result = array();
foreach ($newCandidates as $candidate) {
if (count($result) >= $population) {
break;
}
if ($prev != $candidate->printedTerm) {
$result[] = $candidate;
$prev = $candidate->printedTerm;
}
}
return $result;
}
class MutationWorker {
public function __construct($currentCandidates, $randomSeed) {
$this->currentCandidates = $currentCandidates;
$this->seed = $randomSeed;
}
public function start() {
$this->resultFifo = tempnam("/tmp", "reqlMagic");
$this->pid = pcntl_fork();
if ($this->pid == 0) {
srand($this->seed);
$this->run();
exit(0);
}
}
public function join() {
pcntl_waitpid($this->pid, $status);
// TODO: Using a regular file for this is pretty bad.
$contents = file_get_contents($this->resultFifo);
unlink($this->resultFifo);
$this->newCandidates = unserialize($contents);
}
public function run() {
$newCandidates = mutate($this->currentCandidates);
file_put_contents($this->resultFifo, serialize($newCandidates));
}
var $currentCandidates;
var $seed;
var $newCandidates;
var $pid;
var $resultFifo;
}
function computeNextGen($currentCandidates) {
$NUM_WORKERS = 4;
// Split the current population into $NUM_WORKERS parts.
$threadCandidates = array_fill(0, $NUM_WORKERS, array());
for ($i = 0; $i < count($currentCandidates); ++$i) {
$targetThread = $i % $NUM_WORKERS;
$threadCandidates[$targetThread][] = $currentCandidates[$i];
}
// Launch processes to generate mutants
$workers = array();
for ($i = 0; $i < $NUM_WORKERS; ++$i) {
$worker = new MutationWorker($threadCandidates[$i], rand(0, 10000000));
$worker->start();
$workers[] = $worker;
}
// Join the threads back and merge their results
$newCandidates = array();
for ($i = 0; $i < $NUM_WORKERS; ++$i) {
$workers[$i]->join();
$newCandidates = array_merge($newCandidates, $workers[$i]->newCandidates);
}
// Prune candidates
return pruneCandidates($newCandidates);
}