当前位置: 首页 > 知识库问答 >
问题:

用于webapp的PHP背包式梦幻运动函数

陶法
2023-03-14

我正在为一个梦幻足球webapp构建一个“阵容优化器”,但我被一个特定的部分卡住了。

我试图根据我的sql表中的一列“Estimated_Points”提取一个“最佳计划阵容”,但我需要将该阵容保持在一定的工资上限下,即100美元。

1 (goalkeepers): 2
2 (defenders): 5
3 (midfielders): 5
4 (forwards): 3
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美元了。

我是否需要在每个查询中按位置每次选择一个玩家,并在循环中对每个玩家进行一些检查,而不是在一个查询中提取所有玩家?任何想法或见解都将不胜感激。

共有1个答案

葛玉堂
2023-03-14

我有一个模拟器问题,我试着这样解决它:它不是背包,但它工作!

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_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
    );
}

现在它可以工作了,但它不能处理浏览器中的大数据(播放器)。如果你愿意,我们可以一起改进算法

 类似资料:
  • 我是动态规划新手,已经尝试了我的第一个DP问题。问题陈述是 给定一个尺寸为C的背包,以及n个尺寸为s[]且值为v[]的物品,最大化可放入背包的物品的容量。一个物品可以重复任意次数。(允许重复物品)。 虽然我能够建立递归关系并创建DP表,最终得到背包中可以放入的最大值,但我无法设计一种方法来检索必须选择哪些值才能得到所需的和。 以下是我的解决方案: 在我的解决方案中,我尝试将选择的最大值项的位置存储

  • 我的Android应用程序的一个活动使用带有两个TextView和一个imageview的linearlayout。textviews和imageview从sqlite数据库获取数据。我还有两个按钮(上一个,下一个)在数据库中来回导航。工作正常。 现在,我想给我的应用程序添加动画:当我按下下一步按钮时,线性布局(带有文本视图和图像)应该向左滑动,包含下一行数据的线性布局应该向右滑动。另一种方法是上

  • 关于织梦: 织梦内容管理系统,它就是一种可以综合管理网站上各种栏目的通用工具,新闻、产品、文档、下载、音乐、教学视频……,通过模版技术,他们都在同一套系统里完成更新和维护。 网站的信息量越来越大,网页数目越来越多,网站需要内容管理。最简单也是最原始的情况是网站的管理员来完成管理的任务,他制作所有的页面,并检查其中的链接,然后使用FTp工具上传到服务器上。但是随着网页数量的增加,情况发生了变化,对于

  • 本文向大家介绍php运行时动态创建函数的方法,包括了php运行时动态创建函数的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了php运行时动态创建函数的方法。分享给大家供大家参考。具体分析如下: 一般的语言函数必须定义了在运行,而php支持在运行时动态创建函数,下面是一个简单的范例,在运动时根据不同的条件创建函数$a 希望本文所述对大家的php程序设计有所帮助。

  • 问题内容: 我想String[]用胶水线加入。是否有此功能? 问题答案: 从Java8开始,可以使用。 产生: 否则,具有一个类,该类具有将数组连接在一起以构成的功能。 例如: 生成以下内容:

  • 本文向大家介绍关于背包问题的一些理解和应用,包括了关于背包问题的一些理解和应用的使用技巧和注意事项,需要的朋友参考一下 1.背包问题介绍 背包问题不单单是一个简单的算法问题,它本质上代表了一大类问题,这类问题实际上是01线性规划问题,其约束条件和目标函数如下: 自从dd_engi在2007年推出《背包问题九讲》之后,背包问题的主要精髓基本已道尽。本文没有尝试对背包问题的本质进行扩展或深入挖掘,而只