webapp的PHP背包式奇幻运动功能
Posted
技术标签:
【中文标题】webapp的PHP背包式奇幻运动功能【英文标题】:PHP knapsack-type fantasy sports function for webapp 【发布时间】:2017-05-17 15:40:16 【问题描述】:我正在为一个梦幻足球网络应用程序构建一个“阵容优化器”,但我被困在一个特定的部分。
我正在尝试根据我的 sql 表中的“estimated_points”列提取“最佳预测阵容”,但我需要将阵容保持在一定的工资帽之下,即 100 美元。
我需要填补15个位置,如下(位置ID后跟位置标签,每个位置的玩家数):
1 (goalkeepers): 2
2 (defenders): 5
3 (midfielders): 5
4 (forwards): 3
11 名球员将在首发阵容中,而另外 4 名球员最终坐在替补席上。
我在尝试填补每个位置同时保持整个团队价值低于 100 美元的最高价格时遇到了麻烦。显然,每个玩家都有自己的价格。
这是我现在的 sql 查询:
SELECT
id AS player_id,
first_name,
last_name,
position,
estimated_points_this,
price
FROM
players
ORDER BY estimated_points_this DESC, price ASC
所以现在我正在循环查看所有结果,跟踪每个位置有多少球员,以及我的球队总价值。如果该位置的球员人数达到上限,或者当前球队总价值超过了球队最大价值 100 美元,那么我会跳过并移动到下一个球员身上,并对他们进行相同的检查。
我遇到的问题是,一个位置会被昂贵的球员填补 - 比如如果我最终用 6.4 美元的球员填补了所有 5 个后卫位置,那么我就剩下 68 美元来换 10 名球员等等,所以我最后总是只有 12-13 名球员,因为在某个时候,每个球员都会把球队价值超过我的最高工资帽。
游戏中没有低于 3.9 美元的球员,所以如果我的阵列中有 12 名球员,而我的球队总价值为 97.8 美元左右,就不可能再塞满球员了预算中只剩下 2.2 美元。
我听说过一些关于背包问题的信息,但我不知道如何在 php 中实现它,或者这是否是正确的方法。我需要找到一种方法来平衡低价球员和高价球员,以在不超过工资帽的情况下填补整个 15 人阵容。
我是否需要在每次查询时按位置一次选择一名球员,并在循环中对每个球员进行一些检查,而不是将所有球员都拉到一个查询中?任何想法或见解将不胜感激。
【问题讨论】:
【参考方案1】:我有一个类似的问题,我试图这样解决它:它不是背包,但它有效!
背包.php:
$lineup = new LineUp();
$lineup->setSystem($system);
$lineup->budget = $budget;
$gks = getMinPlayer(POS_GK, $lineup->gk, $where, 1);
$defs = getMinPlayer(POS_DEF, $lineup->def, $where, 1);
$mfs = getMinPlayer(POS_MF, $lineup->mf, $where, 2);
$fors = getMinPlayer(POS_FOR, $lineup->for, $where, 1);
// ALL
$players = new Players();
$players->addPlayers($gks);
$players->addPlayers($defs);
$players->addPlayers($mfs);
$players->addPlayers($fors);
$steps = 0;
$bestteam = new Players();
$maxvalue = -5;
$squad = new LineUp();
$value = knapSack($players, $squad, 0, '');
echo '<h2>KnapSack ('.$steps.'): ' . $value . '</h2>';
echo "<h3>".$bestteam->costs()." Mio. € - ".$bestteam->points()." P</h3>";
echo $bestteam->getInfo('Best');
api_knapsack.php:
define("POS_GK", 1);
define("POS_DEF", 2);
define("POS_MF", 3);
define("POS_FOR", 4);
// Aufstellung: Z.B.: 1-3-5-2
define("SYS_352", 0);
define("SYS_451", SYS_352 + 1);
define("SYS_442", SYS_451 + 1);
define("SYS_433", SYS_442 + 1);
define("SYS_343", SYS_433 + 1);
class Players
public $player = array();
function addPlayers($players)
$this->player = array_merge( $this->player, $players->player);
function setPlayers($players)
$this->player = array();
$this->addPlayers($players);
function costs()
$euro = 0;
$count = count($this->player);
for ($i=0; $i < $count; $i++)
if($this->player[$i]->lineup == 1)
$euro += $this->player[$i]->euro;
return $euro;
function points()
$points = 0;
$count = count($this->player);
for ($i=0; $i < $count; $i++)
if($this->player[$i]->lineup == 1)
$points += $this->player[$i]->points;
return $points;
function addToTeam($no)
$this->player[$no]->lineup = 1;
function getPlayer($caption)
$count = count($this->player);
$result = "<h2>$caption ($count):</h2>";
$result .= '<table class=\"striped\"><tr><th>Name</th><th>Pt.</th><th>Euro</th><th>Pos.</th><th>Club</th><th>LineUp</th></tr>';
for ($i=0; $i < $count; $i++)
$result .= "<tr>".$this->player[$i]->getInfoRow()."</tr>";
$result .= "</table>";
return $result;
function getInfo($caption)
$count = count($this->player);
$inc = 0;
$playerstext = "";
for ($i=0; $i < $count; $i++)
if($this->player[$i]->lineup == 1)
$inc++;
$playerstext .= "<tr>" . $this->player[$i]->getInfoRow() . "</tr>";
$result = "<h2>$caption ($inc):</h2>";
$result .= '<table class="striped"><tr><th>Name</th><th>Pt.</th><th>Euro</th><th>Pos.</th><th>Club</th><th>LineUp</th></tr>';
$result .= $playerstext;
$result .= "</table>";
return $result;
function getInfoCount($lineup, $pos)
$result = "<h3>";
switch ($pos)
case POS_GK:
$result .= "Goalkeeper ($lineup->gk): ";
break;
case POS_DEF:
$result .= "Defense ($lineup->def): ";
break;
case POS_MF:
$result .= "Midfield ($lineup->mf): ";
break;
case POS_FOR:
$result .= "Forward ($lineup->for): ";
break;
return $result . count($this->player) . '</h3>';
class Player
public $name; // Name
public $points; // Expected Points
public $euro; // Cost
public $position; // Position
public $club;
public $lineup; // Is Player positioned in team? 0/1
function __construct($name)
$this->name = $name;
$this->points = 0;
$this->euro = 0;
$this->position = 0;
$this->club = 0;
$this->lineup = 0;
function getInfoRow()
return "<td>$this->name</td>" .
"<td>$this->points</td>" .
"<td>$this->euro</td>" .
"<td>$this->position</td>" .
"<td>$this->club</td>" .
"<td>$this->lineup</td>";
class LineUp
public $gk; // Goalkeeper
public $def; // Defense
public $mf; // Midfield
public $for; // Forward
public $budget;
function __construct()
$this->reset();
function setSystem($system)
$this->gk = 1;
$this->def = 3;
$this->mf = 5;
$this->for = 2;
switch ($system)
case SYS_451:
$this->def = 4;
$this->mf = 5;
$this->for = 1;
break;
case SYS_442:
$this->def = 4;
$this->mf = 4;
$this->for = 2;
break;
case SYS_433:
$this->def = 4;
$this->mf = 3;
$this->for = 3;
break;
case SYS_343:
$this->def = 3;
$this->mf = 4;
$this->for = 3;
break;
function reset()
$this->gk = 0;
$this->def = 0;
$this->mf = 0;
$this->for = 0;
$this->budget = 0;
function addPlayer($player)
$this->budget = $this->budget + $player->euro;
switch ($player->position)
case POS_GK:
$this->gk++;
break;
case POS_DEF:
$this->def++;
break;
case POS_MF:
$this->mf++;
break;
case POS_FOR:
$this->for++;
break;
function full($lineup)
return
($this->gk == $lineup->gk) and
($this->def == $lineup->def) and
($this->mf == $lineup->mf) and
($this->for == $lineup->for);
function fuller($lineup)
return
($this->gk > $lineup->gk) or
($this->def > $lineup->def) or
($this->mf > $lineup->mf) or
($this->for > $lineup->for);
function costly($lineup)
return (($this->budget) > ($lineup->budget));
//var_dump($this);
//var_dump($lineup);
//return true;
function getMinPlayer($pos, $count, $where, $add)
$players = new Players();
$whereClause = "PositionID = $pos " . $where;
$sql = "Select B.* from (Select * from View_Kicker where $whereClause";
$sql .= " order by Punkte desc limit 0, " . ($count + $add);
$sql .= ") B order by B.Euro desc";
$erg = mysql_query($sql);
while ($adr = mysql_fetch_array($erg))
$player = new Player($adr['Name']);
$player->points = $adr['Punkte'];
$player->euro = $adr['Euro'];
$player->position = $pos;
$players->player[] = $player;
return $players;
function getPlayer($pos, $count, $where)
$players = new Players();
$whereClause = "PositionID = $pos " . $where;
$sql = "Select * from View_Kicker where $whereClause";
$sql .= " and PunkteVJ > 0";
$sql .= " order by Euro";
$erg = mysql_query($sql);
while ($adr = mysql_fetch_array($erg))
$euro = $adr['Euro'];
$points = $adr['PunkteVJ'];
$sql2 = mysql_query("Select count(*) as Anz from View_Kicker where $whereClause and Euro < $euro and PunkteVJ > $points");
$erg2 = mysql_fetch_object($sql2);
$no = $erg2->Anz;
if ($no < $count)
$player = new Player($adr['Name']);
$player->points = $adr['PunkteVJ'];
$player->euro = $adr['Euro'] * 10;
$player->position = $pos;
$players->player[] = $player;
return $players;
/**
* @param $players - all players to handle
* @param $squad - actual lineup ( how many players are in team)
* @param $count - whicht player to handle
* @param $deep - only for output
* @return int|mixed
*/
function knapSack($players, $squad, $count, $deep)
global $lineup; // global lineup like 3-5-2 and 42.5mio budget
global $steps; // only for output: method requests
global $bestteam;
global $maxvalue;
$steps++;
//echo '<br>No.: ' . $steps . ' ('.$deep.')';
// Last Child in Tree
if ($count > count($players->player) - 1)
//echo ' -3';
return -3;
// actual Team to expensive
if ($squad->costly($lineup))
//echo ' -2';
return -2;
// Team to full
if ($squad->fuller($lineup))
//echo ' -1';
return -1;
// PERFECT TEAM
if ($squad->full($lineup))
$points = $players->points();
if ($points > $maxvalue)
$maxvalue = $points;
$bestteam->setPlayers($players);
return $points;
// To many players from a club
//TODO
$player = $players->player[$count];
// echo '- handle '.$player->name.' ('.$count.')<br>';
$newplayers = unserialize(serialize($players));
$newsquad = unserialize(serialize($squad));
$newplayers->addToTeam($count);
$newsquad->addPlayer($player);
return max(
knapSack($players, $squad, $count + 1, $deep . '0'), // Add Player NOT to team
knapSack($newplayers, $newsquad, $count + 1, $deep . '1') // Add Player to team
);
现在它可以工作了,但它无法在浏览器中处理大数据 [玩家]。如果你愿意,我们可以一起改进算法。
【讨论】:
以上是关于webapp的PHP背包式奇幻运动功能的主要内容,如果未能解决你的问题,请参考以下文章
评测:低调携枪首选VERTX EDC Gamut Plus背包
Unity UGUI有趣应用 -------------------- 背包系统(上)之简易单页背包系统及检索功能的实现