使用某种算法找到最好的团队

时间:2018-04-17 23:19:43

标签: algorithm math combinations

对于7人的足球比赛,我有21名球员的位置和评分为#。我想通过编程找到最好的团队。是否有任何算法或数学公式我可以用来找到最好的'团队基于玩家的评​​级。 然后找到第二个'订购最好的团队等等。我很满意3-3-1格式(3前锋,3防守和1 GK)

我这里有21名球员

Player  Position   Rating

Vin      F          2.75
Max      D          3.5
Jack     F          4.25
Doen     D          3.75
Ter      F          2.85
Min      GK         3.55
Mor      F          4.25
TOY      D          3.85
Jut      D          2.25
Per      F          4.75
Tod      F          4.58    
GER      D          4.65
YAH      D          4.36
KET      F          3.95
FEL      D          2.82
MET      GK         3.56
Jee      F          2.99
LO       D          3.36
UT       F          3.88
GGT      D          4.15
UUT      GK         4.25

1 个答案:

答案 0 :(得分:2)

我不知道您将使用哪种编程语言,或者您将如何处理数据,但以下是我的快速解决方案。

BTW,我选择了Javascript,因为它很容易制作演示。

function getPos(pos) {
    return function(player) {
        return player['Position'] == pos;
    }
}
function selectSquad(el, t) {
    let teams = [],
        arr = t.slice(),
        i=0;
    while(arr.length >= 7) {
        let remains = [],
            selectedSquad = []; 
        Object.keys(el).forEach( function(k, v) {
            let playersInPositions = arr.filter(getPos(k)),
                selected = playersInPositions.sort(function compare(a, b){
                    let y = a.Rating, z = b.Rating;
                    return z - y;
                }).splice(0,el[k]);

            remains.push(playersInPositions);
            selectedSquad.push(selected);
        });
        arr = [].concat.apply([], remains);
        teams[i] = selectedSquad.slice();
        i++;
    }
    return teams;
}

let team = [
  {"Player":"Vin","Position":"F","Rating":2.75},
  {"Player":"Max","Position":"D","Rating":3.5},
  {"Player":"Jack","Position":"F","Rating":4.25},
  {"Player":"Doen","Position":"D","Rating":3.75},
  {"Player":"Ter","Position":"F","Rating":2.85},
  {"Player":"Min","Position":"GK","Rating":3.55},
  {"Player":"Mor","Position":"F","Rating":4.25},
  {"Player":"TOY","Position":"D","Rating":3.85},
  {"Player":"Jut","Position":"D","Rating":2.25},
  {"Player":"Per","Position":"F","Rating":4.75},
  {"Player":"Tod","Position":"F","Rating":4.58},
  {"Player":"GER","Position":"D","Rating":4.65},
  {"Player":"YAH","Position":"D","Rating":4.36},
  {"Player":"KET","Position":"F","Rating":3.95},
  {"Player":"FEL","Position":"D","Rating":2.82},
  {"Player":"MET","Position":"GK","Rating":3.56},
  {"Player":"Jee","Position":"F","Rating":2.99},
  {"Player":"LO","Position":"D","Rating":3.36},
  {"Player":"UT","Position":"F","Rating":3.88},
  {"Player":"GGT","Position":"D","Rating":4.15},
  {"Player":"UUT","Position":"GK","Rating":4.25}
]

let formation = {
    "GK": 1,
    "D" : 3,
    "F" : 3
}

// selectSquad(formation, team);

console.log(selectSquad(formation, team));

  

PS:您可以将阵型更改为您想要的任何内容,只需更改下面的行即可。

let formation = {
    "GK": 1,
    "D" : 3,
    "F" : 3
}
  

EDİT:如果你改变阵型,让我们说(1,4,2)系统仍将继续,但由于没有足够的防守者,最后一支球队将只有一名后卫。这就是为什么你需要添加某种验证来使系统正常工作的原因。