Improving soccer simulation algorithm

后端 未结 4 475
伪装坚强ぢ
伪装坚强ぢ 2021-01-31 07:00

In another question, you helped me to build a simulation algorithm for soccer. I got some very good answers there. Thanks again!

Now I\'ve coded this algorithm. I would

相关标签:
4条回答
  • 2021-01-31 07:04

    In general, it looks like this is a fairly complicated problem, and I'm not sure how efficient you'll get it.

    That said, I have seen some things which would decidedly help you.

    First I would type the variables in the parameters. This may not necessarily make your code faster, but it would make it easier to read and debug. Next, I would remove the $teamname_att, $teamname_def parameters and simply have those as values in the associative $strength_att, $strength_def arrays. Since this data is always paired up anyway, this will reduce the risk of accidentally using one team's name as a reference to the other team.

    This will make it so you will not have to continually look up values in arrays:

    // replace all $tactics[$teamname_att] with $attackers
    $attackers = $tactics[$teamname_att]; 
    $defenders = $tactics[$teamname_def];
    // Now do the same with arrays like $_POST[ "team1" ];
    

    You have three helper functions which all have the pattern:

    function foo( $arg ){
        $bar = $arg * $value;
        return $bar;
    }
    

    Since this means that you have to create an extra variable (something which can be costly) each time you run the function, use these instead:

    function tactics_weight($wert) {
        return $wert*0.1+0.8;
    }
    
    function strengths_weight($wert) {
        return log10($wert+1)+0.35;
    }
    
    /*
     Perhaps I missed it, but I never saw Chance_Percent( $num1, $num2 )
     consider using this function instead: (one line instead of four, it also
     functions more intuitively, Chance_Percent is your chance out of 100 
     (or per cent)
    
     function Chance_Percent( $chance ) {
         return (mt_rand(1, 100) <= $chance);
     }    
    
    */
    function Chance_Percent($chance, $universe = 100) {
        $chance = abs(intval($chance)); // Will you always have a number as $chance?
                                        // consider using only abs( $chance ) here.
        $universe = abs(intval($universe));
        return (mt_rand(1, $universe) <= $chance);
    }
    

    I couldn't help but notice this pattern coming up consistently:

    $matchReport .= ' ' . comment_action($teamname_att, 'attack');
    

    My general experience is that if you move the concatenation of $matchReport into comment_action, then it will be just slightly faster (Generally less than a dozen milliseconds, but since you're calling that function a half-dozen times inside of a recursive function, this could shave a couple tenths of a second per running).

    I think that this would flow much better (both from a reader's perspective, and from

    Finally, there are several times where you will use the same call to the same function with the same parameter. Make that call up front:

    $goalieStrength = strengths_weight($strength_def['goalkeeper']);
    

    Hope this helps.

    0 讨论(0)
  • 2021-01-31 07:18

    Yous seem to be missing:-

    #include oscar.h;
    void function dive (int ball_location, int[] opposition, int[] opposition_loc) {
        if (this.location != PenaltyBox || ball_location != PenatlyBox)
           return;
        } else {
           for (x = 0; x < 11; x++) {
               if ( opposition_loc[x] = PenaltyBox ) {
                   scream(loudly);
                   falldown();
                   roll_around();
                   cry();
                   roll_around();
                   scream(patheticaly);
                   plead_with_ref();
                   return;
                }
         }
         return;
    }
    
    0 讨论(0)
  • 2021-01-31 07:24

    How often are these values going to be checked? If it's going to be in use by a lot of people and constantly recursing over those if/else statements, I can see you eating up a lot of memory and running quite slowly.

    Perhaps you could but a few switches in there to replace some of the if's?

    That's all I can see for speed improvement. As for the algorithm itself, I'll have to peruse over that a bit later if no one else does.

    0 讨论(0)
  • 2021-01-31 07:27

    I (quickly) read through it and I noticed a couple of things:

    • The percentage a red / yellow card is handed out is the same in all thirds of the field, is this intentional? I'm not a soccer guy, but I'd say that offences are more likely to happen on the last third of the field, than on the first. (Because if you're on the first, you're likely defending)

    • The percentage to determine that a penalty is scored is the same for each team, however some teams, or rather players, are more likely to score a penalty than others.

    • You're not taking into account corner kicks, possible injuries after a foul, or goals scored using the head (which might be worth mentioning in the report).

    Apart from that, you'll just need to run this simulation a lot of times and see if the values you chose are correct; tweak the algorithm. The best thing to do is hand tweak it (eg. read all the constants from a file and run a couple of hundred simulations with different values and different teams), the easiest thing to do is probably to implement a Genetic Algorithm to try and find better values.

    Basically what you have here is genuine gameplay / ai code, so you might want to read up on techniques used by game studios to manage this type of code. (One thing is to put the variables in a google spreadsheet which you can then share / tweak more easily, for example).

    Also, even though you're missing some things that a real soccer match has, there's no point trying to be as realistic as possible because generally in these cases it's more important to provide nice gameplay than it is to provide an accurate simulation.

    0 讨论(0)
提交回复
热议问题