solar-scores
Version:
Compute scores for solar decathlon competition - Cali 2015
118 lines (91 loc) • 3.73 kB
JavaScript
;
const utils = require('./../utils');
const baseScore = require('./../baseScore');
const ONE_MINUTE = 60 * 1000;
const flatten = require('lodash.flatten');
class PowerPeaks extends baseScore {
constructor(options) {
super(options);
}
setExcessProduction(values, param) {
values = utils.filterValues(values, param);
values = utils.resampleWithMax([values], [param], 'PowerPeaks', ONE_MINUTE);
this.excessProductionPerDay = utils.filterByEpochPerDay(values, 'PowerPeaks');
}
setGridConsumption(values, param) {
values = utils.filterValues(values, param);
values = utils.resampleWithMax([values], [param], 'PowerPeaks', ONE_MINUTE);
this.gridConsumptionPerDay = utils.filterByEpochPerDay(values, 'PowerPeaks');
}
getScore(asOfDate) {
var byDay = this.gridConsumptionPerDay;
for(let i=0; i<byDay.length; i++) {
let gridConsumption = byDay[i].data;
let excessProduction = this.excessProductionPerDay[i].data;
let l = byDay[i].periods.length;
byDay[i].points = new Array(l);
byDay[i].maxUntilNow = new Array(l);
byDay[i].fraction = new Array(l);
for(var j=0; j < l; j++) {
var elapsedFraction = utils.getElapsedFraction([byDay[i].periods[j]], asOfDate);
//console.log(asOfDate+" elapsedFractionX "+ elapsedFraction);
let gc = gridConsumption[j].map(mapValue);
let ep = excessProduction[j].map(mapValue);
//console.log(JSON.stringify(gc))
ep = utils.interpolate(ep);
gc = utils.interpolate(gc);
if((!gc.length || gc[0] === null) && (!ep.length || ep[0] === null)) {
//console.log("out");
byDay[i].points[j] =0;//2.2;
if(i==0&&j==0)
byDay[i].points[j] =2.2222222222222223;
byDay[i].maxUntilNow[j] = byDay[i].periods[j].points * elapsedFraction;
byDay[i].fraction[j] = 0;
continue;
}
var peaks = [];
if(gc.length && gc[0] !== null) {
peaks.push(getPeaks(gc));
}
if(ep.length && ep[0] !== null) {
peaks.push(getPeaks(ep));
}
peaks = flatten(peaks);
let peak = Math.max.apply(null, peaks);
var score = this.scoringFunction(peak);
//console.log("elapsedFraction "+elapsedFraction+" "+score);
byDay[i].fraction[j] = score;
byDay[i].points[j] = score * byDay[i].periods[j].points * elapsedFraction;
byDay[i].maxUntilNow[j] = byDay[i].periods[j].points * elapsedFraction;
}
}
console.log(byDay);
return utils.finalizeScores(byDay);
}
}
function getPeaks(data) {
let diffs = new Array(data.length - 16);
for(let i=15; i<data.length; i++) {
diffs[i-15] = 4 * (data[i] - data[i-15]);
}
diffs = diffs.map(function(v,i){ return {v:v, i:i}});
diffs.sort(function(a, b) {
if(a.v < b.v) return 1;
else if(a.v > b.v) return -1;
else return 0;
});
let peaks = [diffs[0]];
for(let i=1; i<diffs.length; i++) {
if(Math.abs(diffs[i].i - peaks[peaks.length-1].i) > 15) {
peaks.push(diffs[i]);
if(peaks.length === 3) break;
}
}
return peaks.map(function(val) {
return val.v;
});
}
function mapValue(val) {
return val.value;
}
module.exports = PowerPeaks;