用于webapp的PHP背包式幻想运动功能
我正在为一个梦幻足球网络应用程序构建一个“阵容优化程序”,我只专注于一个特定的部分 我正试图根据我的sql表中的一列“估计的_点数”提取一个“最佳计划阵容”,但我需要将该阵容保持在一定的工资上限下,即100美元 我需要填写15个职位,如下所示(职位ID后跟职位标签,以及每个职位的球员人数): 11名球员将进入首发阵容,而另外4名球员将坐在板凳上 我很难填补每一个位置,同时保持整个球队的价值在100美元的最高价格。显然,每个玩家都有自己的价格 下面是我现在的sql查询:用于webapp的PHP背包式幻想运动功能,php,mysql,knapsack-problem,Php,Mysql,Knapsack Problem,我正在为一个梦幻足球网络应用程序构建一个“阵容优化程序”,我只专注于一个特定的部分 我正试图根据我的sql表中的一列“估计的_点数”提取一个“最佳计划阵容”,但我需要将该阵容保持在一定的工资上限下,即100美元 我需要填写15个职位,如下所示(职位ID后跟职位标签,以及每个职位的球员人数): 11名球员将进入首发阵容,而另外4名球员将坐在板凳上 我很难填补每一个位置,同时保持整个球队的价值在100美元的最高价格。显然,每个玩家都有自己的价格 下面是我现在的sql查询: SELECT id AS
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个防守球员的位置,那么我再多花10个球员就有68美元,如此等等,所以我总是只剩下12-13个球员,因为在某一点上,每一个球员都会把球队的价值放在我的最高工资上限之上
游戏中没有低于3.9美元的玩家,因此,如果我的阵型中有12名玩家,而我的团队总价值是97.8美元左右,那么就不可能再在那里塞满更多的玩家,因为预算中只剩下2.2美元
我听说过一些关于背包的问题,但是我不知道如何在PHP中实现它,或者这是否是正确的方法。我需要找到一种方法,在低价位球员和高价位球员之间取得一个良好的平衡,以在不超过工资上限的情况下填充整个15人阵容
我是否需要在每个查询中按位置一次选择一个玩家,并对循环中的每个玩家执行一些检查,而不是将所有玩家拉到一个查询中?任何想法或见解都将不胜感激。我有一个类似的问题,我试着这样解决它:它不是背包,但很有效 knapsack.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');
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
);
}
$lineup=newlineup();
$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_表示,$lineup->FOR,$where,1);
//全部
$players=新玩家();
$players->addPlayers($gks);
$players->addPlayers($defs);
$players->addPlayers($mfs);
$players->addPlayers($fors);
$steps=0;
$bestteam=新玩家();
$maxvalue=-5;
$squad=新阵容();
$value=背包($players,$squand,0,,);
回音“背包('.$steps.'):”$价值",;
回声“$bestteam->costs().”百万欧元;-“$bestteam->points().”P;
echo$bestteam->getInfo('Best');
api_knapsack.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');
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
);
}
定义(“POS_GK”,1);
定义(“位置定义”,2);
定义(“位置MF”,3);
定义(“位置”,4);
//奥夫斯特隆:Z.B.:1-3-5-2
定义(“SYS_352”,0);
定义(“系统451”,系统352+1);
定义(“系统442”,系统451+1);
定义(“系统433”,系统442+1);
定义(“系统343”,系统433+1);
职业选手{
public$player=array();
函数addPlayers($players){
$this->player=array\u merge($this->player,$player->player);
}
函数集玩家($players){
$this->player=array();
$this->addPlayers($players);
}
功能成本(){
美元欧元=0;
$count=count($this->player);
对于($i=0;$i<$count;$i++){
如果($this->player[$i]->lineup==1){
$euro+=$this->player[$i]->欧元;
}
}
返回美元欧元;
}
功能点(){
$points=0;
$count=count($this->player);
对于($i=0;$i<$count;$i++){
如果($this->player[$i]->lineup==1){
$points+=$this->player[$i]->点数;
}
}
返回$points;
}
函数addToTeam($no){
$this->player[$no]->lineup=1;
}
函数getPlayer($caption){
$count=count($this->player);
$result=“$caption($count):”;
$result.='NamePt.EuroPos.ClubLineUp';
对于($i=0;$i<$count;$i++){
$result.=''.$this->player[$i]->getInfoRow();
}
$result.=”;
返回$result;
}
函数getInfo($caption){
$count=count($this->player);
$inc=0;
$playerstext=“”;
对于($i=0;$i<$count;$i++){
如果($this->player[$i]->lineup==1){
$inc++;
$playerstext.=''.$this->player[$i]->getInfoRow();
}
}
$result=“$caption($inc):”;
$result.='NamePt.EuroPos.ClubLineUp';
$result.=$playerstext;
$result.=”;
返回$result;
}
函数getInfoCount($lineup,$pos){
$result=“”;
交换机($pos){
案例位置:
$result.=“守门员($lineup->gk):”;
打破
案例位置:
$result.=“防守($lineup->def):”;
打破
案例位置:
$result.=“中场($lineup->mf):”;
打破
案例位置:
$result.=“向前($lineup->for):”;
打破
}
返回$result.count($this->player)。“”;
}
}
职业选手{
public$name;//name
public$points;//预期点数
公共$euro;//成本
public$position;//position
公共俱乐部;
public$lineup;//球员是否在团队中?0/1
函数构造