我正在为一个梦幻足球webapp构建一个“队列优化器”,我被一个特定的部分卡住了。
我正试图根据我的sql表中的一列“估计值”提取一个“最佳计划列表”,但我需要将该列表保持在一定的薪资上限(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个后卫的位置,那么我就要为10个球员多出68美元等等,所以我总是只得到12-13个球员,因为在某一点上,每个球员都会把球队的价值超过我的最高工资上限。
游戏中没有低于3.9美元的玩家,所以如果我有12个玩家在我的阵营里,我的团队总价值是97.8美元左右,那就不可能再在里面塞满玩家了,因为预算只剩下2.2美元了。
我听说过一些关于背包的问题,但是我不知道如何在我的PHP中实现,或者这是否是正确的方法。我需要找到一种方法,让低价球员和高价球员在不超过工资上限的情况下,平衡好整个15人阵容。
我是否需要按位置每次查询选择一个播放器,并对循环中的每个播放器进行一些检查,而不是将所有播放器放入一个查询中?任何想法或见解都将不胜感激。

最佳答案

我有一个模拟的问题,我试图这样解决:它不是背包,但它的工作!
背包.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. &euro; - ".$bestteam->points()." P</h3>";
echo $bestteam->getInfo('Best');

api_背包.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
    );
}

现在它可以工作了,但是它不能在浏览器中处理大数据[玩家]。如果你愿意,我们可以一起改进算法。

关于php - Web应用程序的PHP背包型幻想运动功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44029337/

10-10 18:59
查看更多