From 3e8395b6d891c77d9c71d60b65d99861dc062f73 Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Tue, 10 Apr 2018 16:42:30 +0100 Subject: Initial Commit --- .gitignore | 4 ++++ nn.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 12 ++++++++++++ xml2json.rb | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 .gitignore create mode 100644 nn.js create mode 100644 package.json create mode 100644 xml2json.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..05a6d2b --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +node_modules/ +xport/ +json/ +*.ods diff --git a/nn.js b/nn.js new file mode 100644 index 0000000..45315fe --- /dev/null +++ b/nn.js @@ -0,0 +1,53 @@ +"use strict"; + +let synaptic = require('synaptic'); +let fs = require('fs'); +let doc = + JSON.parse( + fs.readFileSync( + process.argv[process.argv.length - 1] + ) + ); +let data = doc["data"]; +let max = doc["max"]; + +let trainingset = []; + +for(let i = 0; i < (data.length - 10) ; i++) { + trainingset.push({ + input: [ + data[i+0]["v"], data[i+1]["v"], + data[i+2]["v"], data[i+3]["v"], + data[i+4]["v"], data[i+5]["v"], + data[i+6]["v"], data[i+7]["v"], + data[i+8]["v"], data[i+9]["v"] + ], + output: [data[i+10]["v"]] + }); +} + +let trainingoptions = { + rate: .05, + iterations: 20000, + error: .001, + shuffle: true, +}; + +let net = new synaptic.Architect.Perceptron(10, 7, 3, 1); +let trainer = new synaptic.Trainer(net); + +console.error(trainer.train(trainingset, trainingoptions)); + +let predicteddata = data.slice(-10); + +for(let t = data.length ; t < (2 * data.length) ; t++) { + predicteddata.push({ + t: t, + v: net.activate(predicteddata.slice(-10).map(x=>x.v))[0] + }); +} + +predicteddata.splice(0, 10); + + data.forEach(x=>console.log(x.t + "," + (x.v * max))); +predicteddata.forEach(x=>console.log(x.t + "," + (x.v * max))); diff --git a/package.json b/package.json new file mode 100644 index 0000000..fd5bc37 --- /dev/null +++ b/package.json @@ -0,0 +1,12 @@ +{ + "name": "traffic-predictor", + "version": "0.0.1", + "description": "Neural network to try and predict traffic flows", + "main": "nn.js", + "dependencies": { + "synaptic": "^1.1.4", + "xml2json-cli": "^1.1.1" + }, + "author": "Nat Lasseter ", + "license": "BSD-2-Clause" +} diff --git a/xml2json.rb b/xml2json.rb new file mode 100644 index 0000000..aa1508d --- /dev/null +++ b/xml2json.rb @@ -0,0 +1,51 @@ +require 'nokogiri' +require 'json' + +FN = ARGV.shift.chomp + +doc = File.open("#{FN}.xml") { |f| Nokogiri::XML(f) } + +tstart = doc.css("xport>meta>start").text.to_i +tstep = doc.css("xport>meta>step").text.to_i + +rcvd = [] +rmax = 0 +sent = [] +smax = 0 + +doc.css("xport>data>row").each do |row| + t = (row.css("t").text.to_i - tstart) / tstep + r = row.css("v")[0].text.to_f + s = row.css("v")[1].text.to_f + + rmax = r if r > rmax + smax = s if s > smax + + rcvd << { t: t, v: r } + sent << { t: t, v: s } +end + +rmax *= 1.10 +smax *= 1.10 + +rcvd.each do |r| + r[:v] /= rmax +end + +sent.each do |s| + s[:v] /= smax +end + +File.open("#{FN}.rcvd.json", "w") do |f| + f.puts({ + max: rmax, + data: rcvd + }.to_json) +end + +File.open("#{FN}.sent.json", "w") do |f| + f.puts({ + max: smax, + data: sent + }.to_json) +end -- cgit v1.2.1