Make the client-side AIWrapper more robust

This commit is contained in:
Starbeamrainbowlabs 2019-08-06 12:44:18 +01:00
parent 0738f8a983
commit ef515f631f

View file

@ -14,6 +14,10 @@ import {
class AIWrapper {
get training_mode() {
return this.index.properties.training_mode;
}
constructor() {
this.setup_complete = false;
@ -61,46 +65,55 @@ class AIWrapper {
for(let lng = this.map_bounds.west; lng < this.map_bounds.east; lng += this.Config.step.lng) {
let max_predicted_rssi = -Infinity;
let chance = Math.random() < 0.01;
let log_line = "";
for(let [gateway_id, gateway] of this.gateways) {
for(let [/*gateway_id*/, gateway] of this.gateways) {
// Generate the input data
let distance_from_gateway = haversine(
{ latitude: lat, longitude: lng },
gateway
);
let next_value = gateway.ai.run({
let input_data = {
latitude: normalise_lat(lat),
longitude: normalise_lng(lng),
distance: normalise_gateway_distance(
distance_from_gateway
),
});
if(isNaN(next_value[0])) {
longitude: normalise_lng(lng)
};
if(this.training_mode !== "unified")
input_data.distance = normalise_gateway_distance(distance_from_gateway);
// Validate the input data
if(Number.isNaN(input_data.latitude)
|| Number.isNaN(input_data.longitude)
|| (
this.training_mode !== "unified"
&& Number.isNaN(input_data.distance))
) {
console.error(input_data);
throw new Error("Error: Invalid neural network input.");
}
// Run the input through the neural network
let next_value = gateway.ai.run(input_data);
if(Number.isNaN(next_value[0])) {
console.log(next_value);
throw new Error("Error: Neural Network returned NaN");
throw new Error("Error: Neural network returned NaN");
}
if(chance) {
log_line += `${gateway_id}: ${next_value[0]}\n`;
}
// console.log(next_value);
// Operate on the output
max_predicted_rssi = Math.max(
max_predicted_rssi,
next_value[0]
);
}
if(chance) console.log(`${log_line}----`);
// let chance = Math.random() < 0.01;
// if(chance) console.log(max_predicted_rssi);
max_predicted_rssi = unnormalise_rssi(max_predicted_rssi);
// if(chance) console.log(max_predicted_rssi);
// Un-normalise the output of the neural nentwork to something sensible
max_predicted_rssi = unnormalise_rssi(max_predicted_rssi);
// Record the statistics
if(max_predicted_rssi > stats.rssi_max)
stats.rssi_max = max_predicted_rssi;
if(max_predicted_rssi < stats.rssi_min)
stats.rssi_min = max_predicted_rssi;
result.push(max_predicted_rssi);
}