Skip to content

Instantly share code, notes, and snippets.

@jparreira
Created January 26, 2015 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jparreira/081aeaddc592696c0222 to your computer and use it in GitHub Desktop.
Save jparreira/081aeaddc592696c0222 to your computer and use it in GitHub Desktop.
Heuristic function to calculate the "value" of a prediction against a given score
// Heuristic function to calculate the "value" of a prediction against a given score
// basically it measures the distance of the prediction to the given score as an integer number
// a negative scoring means that a match with this prediction is already impossible
function scoring(score, prediction) {
res = 0;
// calculates the diatance of prediction for the house result
house_distance = prediction.house - score.house;
if(house_distance > 0)
res += Math.abs(house_distance);
else
res -= Math.abs(house_distance);
// calculates the distance of prediction for the away result
away_distance = prediction.away - score.away;
if(away_distance > 0)
res += Math.abs(away_distance);
else
res -= Math.abs(away_distance);
return res;
}
// a series of score to test
scores = [
{ house:0, away:0 },
{ house:1, away:0 },
{ house:1, away:1 },
{ house:2, away:1}
];
// the predictions to evaluate
predictions = [
{ house:0, away:0 },
{ house:1, away:0 },
{ house:1, away:1 },
{ house:2, away:1 }
];
// iterate through the scores and evaluate each prediction
for(s=0; s < scores.length; s++) {
console.log("******************************************");
console.log ("CALCULATING HEURISTICS FOR SCORE: " + scores[s].house + "-" + scores[s].away);
user_winner = null;
winner_scoring = null;
// iterate through the predictions
for(i=0; i < predictions.length; i++) {
user = i + 1;
// calculate this prediction scoring
prediction_scoring = scoring(scores[s], predictions[i]);
console.log(" prediction of user U" + user + " " + predictions[i].house + "-" + predictions[i].away + " got scoring of " + prediction_scoring);
if(prediction_scoring >= 0) {
// prediction is an exact match or at least is still possible
if(user_winner == null || prediction_scoring < winner_scoring) {
// first evaluation or current prediction is better than the current winner
user_winner = i;
winner_scoring = prediction_scoring;
}
}
}
// shows the current winner
console.log("CURRENT WINNER FOR THIS SCORE IS U" + (user_winner + 1));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment