-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.js
62 lines (55 loc) · 1.94 KB
/
train.js
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
var fetch = require('./app/fetch').fetch
var optimize = require('./app/optimize')
var extract = require('./app/extract')
var Process = require('./app/process').process
var process = new Process();
process.crunch(extract.parseDom);
process.crunch(extract.getPosition);
var costFunction = function(options) {
var c=0;
var fO = function(q,v) {
var o = {
host:options.host,
port:options.port,
path:options.path
};
o.path = o.path + encodeURI(q) + options.fields;
for(var i=0;i< v.length;i++) {
o.path = o.path.replace(new RegExp('__'+i+'__'), v[i])
}
return o;
};
var cache={};
var costf = function(v, emit) {
var sum = 0, cacheKey= v.join(';');
if (cache.hasOwnProperty(cacheKey)) {
emit({v:v, c:cache[cacheKey]});
return;
}
var repeater = function(i, sum) {
if (i >= options.data.length) {
cache[cacheKey]=sum/options.data.length;
console.info("C:" + c);
emit({v:v, c:cache[cacheKey]});
return;
};
c=c+1;
fetch(options.data[i].doc, function(content) {
var d = process.run(options.data[i].doc, content);
sum=sum+Math.pow(d.position<options.data[i].pos?0:(d.position-options.data[i].pos), 2);
repeater(i+1,sum);
}, null, fO(options.data[i].search, v));
}
repeater(0, sum);
};
return costf;
};
/* popsize, step, mutprod, elite, maxiter */
exports.train = function(options, emit) {
var type = !options.type ? 'gen' : options.type;
optimize[type+'Optimize'].call(this, options.domain, costFunction(options), emit, 20, 50, 0.3, 0.2, parseInt(options.iterations));
};
exports.test = function(options, emit) {
var costf = costFunction(options);
costf(options.vector, function(o) {o.done = true; emit(o); });
};