gpt4 book ai didi

用于webapp的PHP背包式梦幻运动功能

转载 作者:行者123 更新时间:2023-11-28 23:15:34 25 4
gpt4 key购买 nike

我正在为一个梦幻足球网络应用程序构建一个“阵容优化器”,我被困在一个特定的部分上。

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

我需要填写15个位置,如下(位置ID后跟位置标签,每个位置的玩家数):

1 (goalkeepers): 2
2 (defenders): 5
3 (midfielders): 5
4 (forwards): 3

11 名球员将进入首发阵容,而另外 4 名球员将坐在替补席上。

我在尝试填补每个位置时遇到困难,同时又要将整个团队的值(value)保持在 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

所以现在我正在遍历所有结果,跟踪我拥有的每个位置有多少球员,以及我的球队总值(value)。如果我在该位置的玩家数量达到最大值,或者当前团队总值(value)将超过 100 美元的最大团队值(value),那么我将跳过并移动到下一个玩家并对他们进行相同的检查。

我遇到的问题是,一个位置会被昂贵的球员填满——比如如果我最终用花费 6.4 美元的球员填满所有 5 个后卫位置,那么我还有 68 美元可以雇用另外 10 名球员,依此类推,所以我最终总是只有 12-13 名球员,因为在某个时候,每个球员都会将球队值(value)置于我的最高工资上限之上。

游戏中没有低于 3.9 美元的玩家,所以如果我的阵列中有 12 名玩家并且我的团队总值(value)为 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_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背包式梦幻运动功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44029337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com