This Gist was automatically created by Carbide, a free online programming environment.
Created
April 20, 2017 13:51
-
-
Save carbide-public/a54d38391e5245f12e49440be4b8e450 to your computer and use it in GitHub Desktop.
popularity_rating
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var linearDecay = function(x) { return x*0.9; }; ///decay is applied each day before adding new data | |
var slowDecay = function(x) { return x*0.997; }; // score is 0.11 after 2 years (still relevant) - usefull for series or movies | |
class simulation | |
{ | |
constructor(color, eventDeterminator, canvas, decayFunction, chart) { | |
this.score = 0; | |
this.scoreIncrease = 1; | |
this.color = color || '#000000'; | |
this.eventDeterminaator = eventDeterminator; | |
this.reportDeterminator = function(time){return false; }; | |
this.simulationSteps = 100; | |
this.canvas = canvas | |
this.maxScore = 0 | |
this.decayFunction = decayFunction || linearDecay | |
this.chart = chart | |
} | |
run() | |
{ | |
var time = 0; ///starting day | |
var report = []; | |
if (!this.chart) this.chart = new chart(this.color, this.canvas) | |
// initialization | |
var point = this.chart.getPoint(time, this.score); | |
this.chart.startGraphLine(point); | |
this.decayCompletedAtStep = null; | |
for (;time < this.simulationSteps; ) { | |
// get next point | |
this.score = this.decayFunction(this.score); // decay | |
time += 1; // advance time | |
//var onEvent = events.indexOf(time) != -1 | |
var onEvent = this.eventDeterminator(time); | |
if (onEvent) this.score+= this.scoreIncrease; // add to score if an event exists | |
var point = this.chart.getPoint(time, this.score) | |
this.chart.drawDataPoint(point) | |
if (onEvent) { | |
this.chart.drawEventLine(point); | |
this.chart.startGraphLine(point); | |
} | |
var onReport = this.reportDeterminator(time) | |
if (onReport) report.push(this.score) | |
if (this.score < 0.1 && this.decayCompletedAtStep == null) | |
this.decayCompletedAtStep = time | |
this.maxScore = Math.max(this.maxScore, this.score) | |
} | |
// chart.finalize() | |
// ctx.stroke() | |
return report | |
} | |
} | |
class chart | |
{ | |
constructor(lineColor, canvas){ | |
this.lineColor = lineColor; | |
this.xOffset = 20.5; | |
this.yOffset = canvas.height - 20.5; | |
this.xFactor = 10; | |
this.yFactor = 30; | |
this.canvas = canvas || document.createElement('canvas'), ///Get the canvas | |
this.ctx = this.canvas.getContext('2d'); | |
this.drawAxis(); | |
} | |
drawAxis() { | |
this.ctx.strokeStyle = '#000000'; | |
this.ctx.setLineDash([5, 15]); | |
this.ctx.beginPath(); | |
this.ctx.moveTo(0, this.yOffset); | |
this.ctx.lineTo(1000, this.yOffset); | |
this.ctx.stroke(); | |
this.ctx.beginPath(); | |
this.ctx.moveTo(this.xOffset, 0); | |
this.ctx.lineTo(this.xOffset, 1000); | |
this.ctx.stroke(); | |
this.ctx.setLineDash([]); // reset | |
} | |
drawEventLine = function(point) | |
{ | |
return; // do not draw event line | |
this.ctx.strokeStyle = '#00FF00'; | |
this.ctx.beginPath(); | |
this.ctx.moveTo(this.xOffset, point.y); | |
this.ctx.lineTo(point.x, point.y); | |
this.ctx.lineTo(point.x, this.yOffset); | |
this.ctx.stroke(); | |
} | |
drawWeekLines(weekInterval, limit) | |
{ | |
this.ctx.strokeStyle = '#aaaaaa'; | |
this.ctx.setLineDash([2, 6]); | |
var xcursor = this.xOffset | |
var end = this.xOffset + limit * this.xFactor | |
while (xcursor <= end) | |
{ | |
xcursor+= this.xFactor*7*weekInterval | |
if (xcursor > end) break; | |
this.ctx.beginPath(); | |
this.ctx.moveTo(xcursor, this.yOffset); | |
this.ctx.lineTo(xcursor, 0); | |
this.ctx.stroke(); | |
} | |
this.ctx.setLineDash([]); // reset | |
} | |
startGraphLine(point) | |
{ | |
this.ctx.strokeStyle = this.lineColor; | |
this.ctx.beginPath(); | |
this.ctx.lineTo(point.x, point.y); | |
} | |
getPoint(x, y) { | |
return { | |
x: x * this.xFactor + this.xOffset, | |
y: this.yOffset - y * this.yFactor | |
}; | |
} | |
drawDataPoint(point) { | |
this.ctx.lineTo(point.x, point.y) | |
this.ctx.stroke(); | |
} | |
} | |
var canvas = document.createElement('canvas'); ///Get the canvas | |
canvas.width = 800 | |
canvas.height = 600 | |
canvas.style.width = "800px" | |
canvas.style.height = "600px" | |
///Each event is the number of the day and will add one point to the totalvar | |
var weeklyEvents = [1, 8, 15, 22, 29]; | |
var biweelkyEvents = weeklyEvents.concat([4, 12, 18, 25]); | |
// simulations | |
var s1 = new simulation( | |
'#ff0000', | |
function(time) { return time < 30; }, | |
canvas | |
); | |
s1.run() | |
//var canvas = s1.chart.canvas | |
var s2 = new simulation( | |
'#00ff00', | |
function(time) { return biweelkyEvents.indexOf(time) != -1; }, | |
canvas | |
); | |
s2.run() | |
var s3 = new simulation( | |
'#0000ff', | |
function(time) { return weeklyEvents.indexOf(time) != -1; }, | |
canvas | |
) | |
s3.run() | |
var s4 = new simulation( | |
'#ff8800', | |
function(time) { return time == 1; }, | |
canvas | |
) | |
s4.run() | |
var s5 = new simulation( | |
'#FF00FF', | |
function(time) { return time <30; }, | |
canvas | |
) | |
s5.scoreIncrease = 2 | |
s5.run() | |
//s1.chart.drawWeekLines(1, s1.simulationSteps) | |
// aggregating simulation info | |
var simulations = [s1, s2, s3, s4, s5] | |
var maxScores = [] | |
var decayCompleted = [] | |
for (let s=0, l=simulations.length; s < l; s++) | |
{ | |
maxScores[s] = simulations[s].maxScore | |
decayCompleted[s] = simulations[s].decayCompletedAtStep | |
} | |
// watches | |
maxScores; | |
decayCompleted; | |
// yearly decay | |
var yearlySimulationSteps = 365*2; | |
var yearlyCanvas = document.createElement('canvas'); ///Get the canvas | |
yearlyCanvas.width = 800 | |
yearlyCanvas.height = 600 | |
yearlyCanvas.style.width = "800px" | |
yearlyCanvas.style.height = "600px" | |
var yearlyColor = '#FF0000' | |
var yearlyChart = new chart(yearlyColor, yearlyCanvas) | |
yearlyChart.xFactor = 1 | |
yearlyChart.drawWeekLines(4, yearlySimulationSteps) | |
var s6 = new simulation( | |
'#FF0000', | |
function(time) { return time <=1; }, | |
yearlyCanvas, | |
slowDecay, | |
yearlyChart | |
) | |
s6.simulationSteps = yearlySimulationSteps; | |
s6.reportDeterminator = function(time){return time %28 == 0; }; | |
var yearlyReport = s6.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment