Jump to content

This script is driving me insane


dzhax

Recommended Posts

I have been working with an algorithm that a mathematician came up with and trying to convert it into a useable php script for about a month now.I just can not seem to grasp how to do it.If someone is willing to take a look at this and make it work I will pay up to $100.I was givin this PDF to start withhttp://combatsportsrank.com/rank/mmaRanking.pdfit outlines the functions needed to be done in order to get the Rank.I have attempted to convert the majority of it into PHP but I have no idea how to implement it.From what I see it needs like 2 or 3 loops to just get one rank and its got to take a lot of time to get the info.Here is what I have so far: http://w3schools.invisionzone.com/index.php?showtopic=38459So i am thinking it might have to be a bunch of little cron jobs that can be ran to gather info and post it into a database so it can be grabbed faster when needed.Ultimately we are making a website that is a social network and doubles as a ranking site for MMA fighters. Fight data is entered then parsed to determine a specific fighters rank.This rank is used in dynamic images that can be used on the site and posted on other sites such as Facebook, Myspace, Etc.If anyone is up to the challenge I would greatly appreciate it. I would like to be all said and done with it in 2 weeks. definitely before the end of the month.

Link to comment
Share on other sites

(based on your last post's code in the other thread)in the code at the top, you had $gamma1 assigned to number of wins by decision, and $gamma0 to number of losses by decision,but the PDF says $gamma1 is losses, and $gamma0 is wins, it would be best to keep consistent with that.i also stuck them in a class seeing as the other functions work with the -> operator.

class Fighter {	$numOpponents = 0;	$kappa0 = 0; //number of wins by knockout or submission	$kappa1 = 0; //number of losses by knockout or submission	$tau0   = 0; //number of wins by TKO	$tau1   = 0; //number of losses by TKO	$gamma0 = 0; //number of wins by decision	$gamma1 = 0; //number of losses by decision	$delta  = 0; //8 lookin symbol - number of draws}

"weighted sum of fighter f’s wins."notice how you don't need the $ sign directly after the -> operator, also gamma0 is now consistent.

function beta($fighter){	return (1.15*$fighter->kappa0 + $fighter->tau0 + 0.85*$fighter->gamma0);}

"fighter f’s weighted winning percentage"this function was missing the + t0 in the top part of the division.

function littleLambda($fighter){	return ( (1.5*$fighter->kappa0 + $fighter->tau0 + 0.5*$fighter->gamma0 + 0.1*$fighter->delta) / ( 1.5*($fighter->kappa0 + $fighter->kappa1) + ($fighter->tau0 + $fighter->tau1) + 0.5*($fighter->gamma0 + $fighter->gamma1) + 0.1*$fighter->delta ) );}

your bigLambda function is missing.

function bigLambda($fighter){	$lambda = 0;	for ($i=0; $i < $fighter->numOpponents; $i++){		$lambda += omega($fighter->opponent[$i]);	}	return $lambda / $fighter->numOpponents;}

in the rho function, variable $i needs a $ sign before it, also you shouldn't have "int" before it in php.

function rho($fighter){	$rhoStep1 = 0;	for($i=0; $i < $fighter->numOpponents; $i++){		$rhoStep1 += bigLambda($fighter->opponent[$i]);	}	$rhoStep1 /= $fighter->numOpponents;	return (0.65*omega($fighter) + 0.25*bigLambda($fighter) + 0.1*$rhoStep1);}

in your 3rd post of the other thread, it has this code in C:

class Fighter {public:	Fighter(int numOpps){		numOpponents = numOpps;		opponent = new Fighter*[numOpponents];	}	Fighter **opponent;	int numOpponents;	int kappa0,kappa1,tau0,tau1,gamma1,gamma0,delta;};

i don't know C and i'm not too good with oop, but as a guess, and i'm probably wrong on this:when an object gets constructed of the Fighter class, the opponent property contains an array of other Fighter objects, where the array size is number of opponents.if thats the case, you can probably do something like this in php in the __construct method:

class Fighter {	$numOpponents = 0;	$kappa0 = 0; //number of wins by knockout or submission	$kappa1 = 0; //number of losses by knockout or submission	$tau0   = 0; //number of wins by TKO	$tau1   = 0; //number of losses by TKO	$gamma0 = 0; //number of wins by decision	$gamma1 = 0; //number of losses by decision	$delta  = 0; //8 lookin symbol - number of draws		function __construct($numOpps) {		$this->numOpponents = $numOpps;				$this->opponent = array();		for ($i=0; $i<$numOpps; $i++) {			$this->opponent[$i] = new Fighter(0);		}	}}

but yeah that really is just a guess.

Link to comment
Share on other sites

A big problem I am having is there are hundreds maybe thousands of fighters each having multiple fights which means multiple opponents.So I would have to load the top data for the current fighter.Find out who else he has fought.Load those fighters data.And loop it thorough the functions to determine this fighters rank over all other fighters, not just his opponents.

Link to comment
Share on other sites

firstly id recommend deciding on how the data will be stored in the database,if you haven't done this yet you could have 2 tables structured like so:fightersfighter_idnamerankk_winsk_lossest_winst_lossesd_winsd_lossesdrawsfightsfighter_id1fighter_id2next here's some code:

class Fighter {	$fighterID = 0;		$opponent = array();	$numOpponents = 0;		$kappa0 = 0; //number of wins by knockout or submission	$kappa1 = 0; //number of losses by knockout or submission	$tau0   = 0; //number of wins by TKO	$tau1   = 0; //number of losses by TKO	$gamma0 = 0; //number of wins by decision	$gamma1 = 0; //number of losses by decision	$delta  = 0; //8 lookin symbol - number of draws		function __construct($fighterID) {		$this->fighterID = $fighterID;	}		function loadStats() {		global $con;				$query = 'SELECT * FROM fighters WHERE fighter_id='.$this->fighterID;		$result = mysql_query($query, $con);		if (!$result) return false;				$row = mysql_fetch_assoc($result);		if (!$row) return false;				$this->kappa0 = (int) $row['k_wins'];		$this->kappa1 = (int) $row['k_losses'];		$this->tau0   = (int) $row['t_wins'];		$this->tau1   = (int) $row['t_losses'];		$this->gamma0 = (int) $row['d_wins'];		$this->gamma1 = (int) $row['d_losses'];		$this->delta  = (int) $row['draws'];	}	function loadOpponents() {		global $con;				$query = 'SELECT * FROM fights WHERE fighter_id1='.$this->fighterID.' OR fighter_id2='.$this->fighterID;		$result = mysql_query($query, $con);		if (!$result) return false;				$this->numOpponents = mysql_num_rows($result);				$key = 0;		while (($row = mysql_fetch_assoc($result)) !== false) {			$opponentFighterID = (int) (($this->fighterID === (int) $row['fighter_id1']) ? $row['fighter_id2'] : $row['fighter_id1']);						$this->opponent[$key] = new Fighter($opponentFighterID);			$this->opponent[$key]->loadStats();						$key++;		}	}}$fighterID = 1;$fighter = new Fighter($fighterID);$fighter->loadStats();$fighter->loadOpponents();

the loadStats() method loads top data for a fighterthe loadOpponents() method loads all opponents that the fighter has fought, and fetches the top data for those opponents$con would have to be a global variable storing a mysql handle.

Link to comment
Share on other sites

class Fighter {	$fighterID = 0;		$opponent = array();	$numOpponents = 0;		$kappa0 = 0; //number of wins by knockout or submission	$kappa1 = 0; //number of losses by knockout or submission...	}}$fighterID = 1;$fighter = new Fighter($fighterID);$fighter->loadStats();$fighter->loadOpponents();

So with this, say i want to echo fighter 2's rank what would i call?

Link to comment
Share on other sites

well to calculate the score, after loading the stats and opponents, you should be able to just do:

$score = rho($fighter);

for the rank, i take it this is unique per fighter, between 1 and the total amount of fighters, and ordered by the score where rank 1 is highest score?(if so then obviously the score will also need storing in the database.)

function getFighterScoreDB($fighterID) {	$query = 'SELECT score FROM fighters WHERE fighter_id = '.$fighterID.' LIMIT 1';	$result = mysql_query($query);	$row = mysql_fetch_row($result);	return ($row !== false) ? $row[0] : false;}function getFighterRank($fighterID, $score) {	$currentScore = getFighterScoreDB($fighterID);		$query = 'SELECT COUNT(*) FROM fighters WHERE score >= '.$score;	$result = mysql_query($query);	$row = mysql_fetch_row($result);	$rank = $row[0] + (($currentScore === false || $score > $currentScore) ? 1 : 0);		return $rank;}

so after calculating the score, you can call getFighterRank($fighterID, $score) which should give you the rank number.note that the rank is possible to be the same as 1 other fighter's rank, but thats normal, as i assume the other ranks will be updated in the database.

Link to comment
Share on other sites

Ok I peiced it together and I am getting this

Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in ************************/csr-rank/cron-job.php on line 5

this is what I put in, which from what I see should print the score for fighter 1.

<?$con = mysql_connect('localhost','USER','PASS') or die('Unable to connect');class Fighter {	$fighterID = 0;		$opponent = array();	$numOpponents = 0;		$kappa0 = 0; //number of wins by knockout or submission	$kappa1 = 0; //number of losses by knockout or submission	$tau0   = 0; //number of wins by TKO	$tau1   = 0; //number of losses by TKO	$gamma0 = 0; //number of wins by decision	$gamma1 = 0; //number of losses by decision	$delta  = 0; //8 lookin symbol - number of draws		function __construct($fighterID) {		$this->fighterID = $fighterID;	}		function loadStats() {		global $con;				$query = 'SELECT * FROM rank_fighter_stats WHERE fighterID='.$this->fighterID;		$result = mysql_query($query, $con);		if (!$result) return false;				$row = mysql_fetch_assoc($result);		if (!$row) return false;				$this->kappa0 = (int) $row['k_wins'];		$this->kappa1 = (int) $row['k_losses'];		$this->tau0   = (int) $row['t_wins'];		$this->tau1   = (int) $row['t_losses'];		$this->gamma0 = (int) $row['d_wins'];		$this->gamma1 = (int) $row['d_losses'];		$this->delta  = (int) $row['draws'];	}	function loadOpponents() {		global $con;				$query = 'SELECT * FROM rank_fight_list WHERE fighterID1='.$this->fighterID.' OR fighterID2='.$this->fighterID;		$result = mysql_query($query, $con);		if (!$result) return false;				$this->numOpponents = mysql_num_rows($result);				$key = 0;		while (($row = mysql_fetch_assoc($result)) !== false) {			$opponentFighterID = (int) (($this->fighterID === (int) $row['fighterID1']) ? $row['fighterID2'] : $row['fighterID1']);						$this->opponent[$key] = new Fighter($opponentFighterID);			$this->opponent[$key]->loadStats();						$key++;		}	}}function beta($fighter){	return (1.15*$fighter->kappa0 + $fighter->tau0 + 0.85*$fighter->gamma0);}function littleLambda($fighter){	return ( (1.5*$fighter->kappa0 + $fighter->tau0 + 0.5*$fighter->gamma0 + 0.1*$fighter->delta) / ( 1.5*($fighter->kappa0 + $fighter->kappa1) + ($fighter->tau0 + $fighter->tau1) + 0.5*($fighter->gamma0 + $fighter->gamma1) + 0.1*$fighter->delta ) );}function sigma($x){	return (log(10.0 * $x + 12.0) / 6.5);}function omega($fighter){	return (0.6*sigma(beta($fighter)) + 0.4*littleLambda($fighter));}function bigLambda($fighter){	$lambda = 0;	for ($i=0; $i < $fighter->numOpponents; $i++){		$lambda += omega($fighter->opponent[$i]);	}	return $lambda / $fighter->numOpponents;}function rho($fighter){	$rhoStep1 = 0;	for($i=0; $i < $fighter->numOpponents; $i++){		$rhoStep1 += bigLambda($fighter->opponent[$i]);	}	$rhoStep1 /= $fighter->numOpponents;	return (0.65*omega($fighter) + 0.25*bigLambda($fighter) + 0.1*$rhoStep1);}$fighterID = 1;$fighter = new Fighter($fighterID);$fighter->loadStats();$fighter->loadOpponents();$score = rho($fighter);echo $score;?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...