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 { class AIWrapper {
get training_mode() {
return this.index.properties.training_mode;
}
constructor() { constructor() {
this.setup_complete = false; 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) { for(let lng = this.map_bounds.west; lng < this.map_bounds.east; lng += this.Config.step.lng) {
let max_predicted_rssi = -Infinity; let max_predicted_rssi = -Infinity;
let chance = Math.random() < 0.01; for(let [/*gateway_id*/, gateway] of this.gateways) {
let log_line = ""; // Generate the input data
for(let [gateway_id, gateway] of this.gateways) {
let distance_from_gateway = haversine( let distance_from_gateway = haversine(
{ latitude: lat, longitude: lng }, { latitude: lat, longitude: lng },
gateway gateway
); );
let next_value = gateway.ai.run({
let input_data = {
latitude: normalise_lat(lat), latitude: normalise_lat(lat),
longitude: normalise_lng(lng), longitude: normalise_lng(lng)
distance: normalise_gateway_distance( };
distance_from_gateway if(this.training_mode !== "unified")
), input_data.distance = normalise_gateway_distance(distance_from_gateway);
});
if(isNaN(next_value[0])) { // 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); console.log(next_value);
throw new Error("Error: Neural Network returned NaN"); throw new Error("Error: Neural network returned NaN");
} }
if(chance) { // Operate on the output
log_line += `${gateway_id}: ${next_value[0]}\n`;
}
// console.log(next_value);
max_predicted_rssi = Math.max( max_predicted_rssi = Math.max(
max_predicted_rssi, max_predicted_rssi,
next_value[0] 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) if(max_predicted_rssi > stats.rssi_max)
stats.rssi_max = max_predicted_rssi; stats.rssi_max = max_predicted_rssi;
if(max_predicted_rssi < stats.rssi_min) if(max_predicted_rssi < stats.rssi_min)
stats.rssi_min = max_predicted_rssi; stats.rssi_min = max_predicted_rssi;
result.push(max_predicted_rssi); result.push(max_predicted_rssi);
} }