Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Facemash ELO Rating Class/Function

I got the following ELO class from PHPClasses website.

<?php 
class elo_calculator {
    public function rating($S1, $S2, $R1, $R2) {
        if(empty($S1) or empty($S2) or empty($R1) or empty($R2))
            return null;
        if($S1 != $S2) {
            if($S1 > $S2) {
                $E = 120 - round(1 / (1 + pow(10, (($R2 - $R1) / 400))) * 120);
                $R['R3'] = $R1 + $E;
                $R['R4'] = $R2 - $E;
            } else {
                $E = 120 - round(1 / (1 + pow(10, (($R1 - $R2) / 400))) * 120);
                $R['R3'] = $R1 - $E;
                $R['R4'] = $R2 + $E;
            }
        } else {
            if($R1 == $R2) {
                $R['R3'] = $R1;
                $R['R4'] = $R2;
            } else {
                if($R1 > $R2) {
                    $E = (120 - round(1 / (1 + pow(10, (($R1 - $R2) / 400))) * 120)) - (120 - round(1 / (1 + pow(10, (($R2 - $R1) / 400))) * 120));
                    $R['R3'] = $R1 - $E;
                    $R['R4'] = $R2 + $E;
                } else {
                    $E = (120 - round(1 / (1 + pow(10, (($R2 - $R1) / 400))) * 120)) - (120 - round(1 / (1 + pow(10, (($R1 - $R2) / 400))) * 120));
                    $R['R3'] = $R1 + $E;
                    $R['R4'] = $R2 - $E;
                }
            }
        }
        $R['S1'] = $S1;
        $R['S2'] = $S2;
        $R['R1'] = $R1;
        $R['R2'] = $R2;
        $R['P1'] = ((($R['R3'] - $R['R1']) > 0)?"+" . ($R['R3'] - $R['R1']) : ($R['R3'] - $R['R1']));
        $R['P2'] = ((($R['R4'] - $R['R2']) > 0)?"+" . ($R['R4'] - $R['R2']) : ($R['R4'] - $R['R2']));
        return $R;
    }
} 
?>

I am trying to apply this to my food rating site.

Here is what i understand

  1. To start off with the system we need to assign a base score for all the dishes.
  2. We have 4 variables S1, S2, R1, R2 (S= score, R = rank)
  3. When rating between two dishes if i press the first dish. what will be the the S1 and S2 ? will it be 1-0 ?
  4. What if i add another dish after 10k battles ? since i will be assigning a base score for it will it fair better ?
  5. How can i stop a score of a Dish not to go below 0.

Here is PHP implementation of the same. Can you help me understand the 4 variables and how should i use it ?

like image 879
Harsha M V Avatar asked Apr 10 '11 07:04

Harsha M V


People also ask

How Elo rating is calculated?

According to this algorithm, performance rating for an event is calculated in the following way: For each win, add your opponent's rating plus 400, For each loss, add your opponent's rating minus 400, And divide this sum by the number of played games.

What algorithm is used in facemash?

It's time you create your own Facemash or a rating-based video game. Practice your coding skills by solving HackerEarth's challenges, which use the Elo algorithm.

What is Elo full form?

ELO stands for Expanded Learning Opportunities.

What is Elo chess rating?

Elo rating is used to determine the strength of chess players, and hence titles such as Grandmaster, International Master, and FIDE Master are awarded after players attain a certain level. The rating system is devised in a way which prevents a single player from taking a substantial lead in terms of points.


2 Answers

here on GitHub is the best php class for ELO rating system i've ever found: https://github.com/Chovanec/elo-rating

USAGE:

// player A elo = 1000
// player B elo = 2000
// player A lost
// player B win
$raging = new Rating(1000, 2000, 0, 1);

// player A elo = 1000
// player B elo = 2000
// player A draw
// player B draw
$raging = new Rating(1000, 2000, .5, .5);
like image 120
Michal Avatar answered Nov 09 '22 19:11

Michal


1.S1 should be the current score of the first dish and S2 for the second dish you are comparing with

2.R1 is the rank of the current dish and R2 is the current rank of the second dish

2.if its not fair this system wouldn't be used in global games

3.you probably are going to use a database to save scores so lets say it should be like this

if($elo_calcualtor->rating(1,2,1,2)['p1'] < 0){$current_dish_score = 0;}

finally you should make the rating starts from ten, so it won't go below 0 and most likely won't go higher than 20

hope this helps

like image 32
SAFAD Avatar answered Nov 09 '22 17:11

SAFAD