2009-09-17 12 views
6

एक अन्य प्रश्न में, आपने सॉकर के लिए सिमुलेशन एल्गोरिदम बनाने में मेरी सहायता की। I got some very good answers there. फिर से धन्यवाद!सॉकर सिमुलेशन एल्गोरिदम में सुधार

अब मैंने इस एल्गोरिदम को कोड किया है। मैं इसे सुधारना चाहता हूं और इसमें छोटी गलतियों को ढूंढना चाहता हूं। मैं इस बात पर चर्चा नहीं करना चाहता हूं कि इसे कैसे हल किया जाए - जैसा कि हमने अंतिम प्रश्न में किया था। अब मैं केवल इसे सुधारना चाहता हूं। क्या आप कृपया मेरी मदद कर सकते हैं?

  1. क्या कोई गलती है?
  2. क्या नेस्टेड if-clauses की संरचना ठीक है? क्या यह सुधार किया जा सकता है?
  3. क्या रणनीति मेरे विवरण के अनुसार सही ढंग से एकीकृत है?

    • $ रणनीति [x] [0] समायोजन (1 = बचाव की मुद्रा में, 2 = तटस्थ, 3 = आक्रामक): जो अनियमितता पर एक प्रभाव होना चाहिए

सामरिक सेटिंग्स उच्च मूल्य कमजोर है और रक्षा मजबूत है

  • $ रणनीति x नाटक की गति (1 = धीमी, 2 = मध्यम, 3 = तेज): जितना अधिक होगा उतना ही बेहतर होगा अवसरों पर उच्च त्वरित काउंटर अटैक
  • प्राप्त करने का जोखिम है $ रणनीति x पास की दूरी (1 = शॉर्ट, 2 = मध्यम, 3 = लम्बाई): जितना अधिक होगा उतना ही कम होगा लेकिन बेहतर अवसर आपको मिलेगा और अधिक बार आप
  • $ रणनीति x परिवर्तनों का निर्माण (1) = सुरक्षित, 2 = मध्यम, 3 = जोखिम भरा): जितना अधिक होगा उतना ही बेहतर होगा आपके अवसर बेहतर हैं लेकिन उच्च काउंटर अटैक
  • $ रणनीति [x] [4] रक्षा में दबाव (1] = कम, 2 = मध्यम, 3 = उच्च): अधिक मूल्य जितना अधिक त्वरित काउंटर हमलों होगा
  • $ रणनीति [x] [5] आक्रामकता (1 = कम, 2 = मध्यम, 3 = उच्च) : जितना अधिक मूल्य उतना अधिक हमला होगा जिसे आप रोक देंगे
  • नोट: टैक्टिक 0 और रणनीति 4 आंशिक रूप से इंजन के बाकी हिस्सों में एकीकृत हैं, इस फ़ंक्शन में आवश्यक नहीं है।

    वर्तमान एल्गोरिथ्म:

    <?php 
    function tactics_weight($wert) { 
        $neuerWert = $wert*0.1+0.8; 
        return $neuerWert; 
    } 
    function strengths_weight($wert) { 
        $neuerWert = log10($wert+1)+0.35; 
        return $neuerWert; 
    } 
    function Chance_Percent($chance, $universe = 100) { 
        $chance = abs(intval($chance)); 
        $universe = abs(intval($universe)); 
        if (mt_rand(1, $universe) <= $chance) { 
         return true; 
        } 
        return false; 
    } 
    function simulate_attack($teamname_att, $teamname_def, $strength_att, $strength_def) { 
        global $minute, $goals, $_POST, $matchReport, $fouls, $yellowCards, $redCards, $offsides, $shots, $tactics; 
        // input values: attacker's name, defender's name, attacker's strength array, defender's strength array 
        // players' strength values vary from 0.1 to 9.9 
        $matchReport .= '<p>'.$minute.'\': '.comment_action($teamname_att, 'attack'); 
        $offense_strength = $strength_att['forwards']/$strength_def['defenders']; 
        $defense_strength = $strength_def['defenders']/$strength_att['forwards']; 
        if (Chance_Percent(50*$offense_strength*tactics_weight($tactics[$teamname_att][1])/tactics_weight($tactics[$teamname_att][2]))) { 
         // attacking team passes 1st third of opponent's field side 
         $matchReport .= ' '.comment_action($teamname_def, 'advance'); 
         if (Chance_Percent(25*tactics_weight($tactics[$teamname_def][5]))) { 
          // the defending team fouls the attacking team 
          $fouls[$teamname_def]++; 
          $matchReport .= ' '.comment_action($teamname_def, 'foul'); 
          if (Chance_Percent(43)) { 
           // yellow card for the defending team 
           $yellowCards[$teamname_def]++; 
           $matchReport .= ' '.comment_action($teamname_def, 'yellow'); 
          } 
          elseif (Chance_Percent(3)) { 
           // red card for the defending team 
           $redCards[$teamname_def]++; 
           $matchReport .= ' '.comment_action($teamname_def, 'red'); 
          } 
          // indirect free kick 
          $matchReport .= ' '.comment_action($teamname_att, 'iFreeKick'); 
          if (Chance_Percent(25*strengths_weight($strength_att['forwards']))) { 
           // shot at the goal 
           $shots[$teamname_att]++; 
           $matchReport .= ' '.comment_action($teamname_att, 'iFreeKick_shot'); 
           if (Chance_Percent(25/strengths_weight($strength_def['goalkeeper']))) { 
            // attacking team scores 
            $goals[$teamname_att]++; 
            $matchReport .= ' '.comment_action($teamname_att, 'shot_score'); 
           } 
           else { 
            // defending goalkeeper saves 
            $matchReport .= ' '.comment_action($teamname_def, 'iFreeKick_shot_save'); 
           } 
          } 
          else { 
           // defending team cleares the ball 
           $matchReport .= ' '.comment_action($teamname_def, 'iFreeKick_clear'); 
          } 
         } 
         elseif (Chance_Percent(17)*tactics_weight($tactics[$teamname_att][2])) { 
          // attacking team is caught offside 
          $offsides[$teamname_att]++; 
          $matchReport .= ' '.comment_action($teamname_def, 'offside'); 
         } 
         else { 
          // attack isn't interrupted 
          // attack passes the 2nd third of the opponent's field side - good chance 
          $matchReport .= ' '.comment_action($teamname_def, 'advance_further'); 
          if (Chance_Percent(25*tactics_weight($tactics[$teamname_def][5]))) { 
           // the defending team fouls the attacking team 
           $fouls[$teamname_def]++; 
           $matchReport .= ' '.comment_action($teamname_def, 'foul'); 
           if (Chance_Percent(43)) { 
            // yellow card for the defending team 
            $yellowCards[$teamname_def]++; 
            $matchReport .= ' '.comment_action($teamname_def, 'yellow'); 
           } 
           elseif (Chance_Percent(3)) { 
            // red card for the defending team 
            $redCards[$teamname_def]++; 
            $matchReport .= ' '.comment_action($teamname_def, 'red'); 
           } 
           if (Chance_Percent(19)) { 
            // penalty for the attacking team 
            $shots[$teamname_att]++; 
            $matchReport .= ' '.comment_action($teamname_att, 'penalty'); 
            if (Chance_Percent(77)) { 
             // attacking team scores 
             $goals[$teamname_att]++; 
             $matchReport .= ' '.comment_action($teamname_att, 'shot_score'); 
            } 
            elseif (Chance_Percent(50)) { 
             // shot misses the goal 
             $matchReport .= ' '.comment_action($teamname_att, 'penalty_miss'); 
            } 
            else { 
             // defending goalkeeper saves 
             $matchReport .= ' '.comment_action($teamname_def, 'penalty_save'); 
            } 
           } 
           else { 
            // direct free kick 
            $matchReport .= ' '.comment_action($teamname_att, 'dFreeKick'); 
            if (Chance_Percent(33*strengths_weight($strength_att['forwards']))) { 
             // shot at the goal 
             $shots[$teamname_att]++; 
             $matchReport .= ' '.comment_action($teamname_att, 'dFreeKick_shot'); 
             if (Chance_Percent(33/strengths_weight($strength_def['goalkeeper']))) { 
              // attacking team scores 
              $goals[$teamname_att]++; 
              $matchReport .= ' '.comment_action($teamname_att, 'shot_score'); 
             } 
             else { 
              // defending goalkeeper saves 
              $matchReport .= ' '.comment_action($teamname_def, 'dFreeKick_shot_save'); 
             } 
            } 
            else { 
             // defending team cleares the ball 
             $matchReport .= ' '.comment_action($teamname_def, 'dFreeKick_clear'); 
            } 
           } 
          } 
          elseif (Chance_Percent(62*strengths_weight($strength_att['forwards'])*tactics_weight($tactics[$teamname_att][2])*tactics_weight($tactics[$teamname_att][3]))) { 
           // shot at the goal 
           $shots[$teamname_att]++; 
           $matchReport .= ' '.comment_action($teamname_att, 'shot'); 
           if (Chance_Percent(30/strengths_weight($strength_def['goalkeeper']))) { 
            // the attacking team scores 
            $goals[$teamname_att]++; 
            $matchReport .= ' '.comment_action($teamname_att, 'shot_score'); 
           } 
           else { 
            if (Chance_Percent(50)) { 
             // the defending defenders block the shot 
             $matchReport .= ' '.comment_action($teamname_def, 'shot_block'); 
            } 
            else { 
             // the defending goalkeeper saves 
             $matchReport .= ' '.comment_action($teamname_def, 'shot_save'); 
            } 
           } 
          } 
          else { 
           // attack is stopped 
           $matchReport .= ' '.comment_action($teamname_def, 'stopped'); 
           if (Chance_Percent(15*$defense_strength*tactics_weight($tactics[$teamname_att][1])*tactics_weight($tactics[$teamname_att][3])*tactics_weight($tactics[$teamname_def][4]))) { 
            // quick counter attack - playing on the break 
            $strength_att['defenders'] = $strength_att['defenders']*0.8; // weaken the current attacking team's defense 
            $matchReport .= ' '.comment_action($teamname_def, 'quickCounterAttack'); 
            $matchReport .= ' ['.$goals[$_POST['team1']].':'.$goals[$_POST['team2']].']</p>'; // close comment line 
            return simulate_attack($teamname_def, $teamname_att, $strength_def, $strength_att); // new attack - this one is finished 
           } 
          } 
         } 
        } 
        // attacking team doesn't pass 1st third of opponent's field side 
        elseif (Chance_Percent(15*$defense_strength*tactics_weight($tactics[$teamname_att][1])*tactics_weight($tactics[$teamname_att][3])*tactics_weight($tactics[$teamname_def][4]))) { 
         // attack is stopped 
         // quick counter attack - playing on the break 
         $matchReport .= ' '.comment_action($teamname_def, 'stopped'); 
         $strength_att['defenders'] = $strength_att['defenders']*0.8; // weaken the current attacking team's defense 
         $matchReport .= ' '.comment_action($teamname_def, 'quickCounterAttack'); 
         $matchReport .= ' ['.$goals[$_POST['team1']].':'.$goals[$_POST['team2']].']</p>'; // close comment line 
         return simulate_attack($teamname_def, $teamname_att, $strength_def, $strength_att); // new attack - this one is finished 
        } 
        else { 
         // ball goes into touch - out of the field 
         $matchReport .= ' '.comment_action($teamname_def, 'throwIn'); 
         if (Chance_Percent(33)) { 
          // if a new chance is created 
          if (Chance_Percent(50)) { 
           // throw-in for the attacking team 
           $matchReport .= ' '.comment_action($teamname_def, 'throwIn_att'); 
           $matchReport .= ' ['.$goals[$_POST['team1']].':'.$goals[$_POST['team2']].']</p>'; // close comment line 
           return simulate_attack($teamname_att, $teamname_def, $strength_att, $strength_def); // new attack - this one is finished 
          } 
          else { 
           // throw-in for the defending team 
           $matchReport .= ' '.comment_action($teamname_def, 'throwIn_def'); 
           $matchReport .= ' ['.$goals[$_POST['team1']].':'.$goals[$_POST['team2']].']</p>'; // close comment line 
           return simulate_attack($teamname_def, $teamname_att, $strength_def, $strength_att); // new attack - this one is finished 
          } 
         } 
        } 
        $matchReport .= ' ['.$goals[$_POST['team1']].':'.$goals[$_POST['team2']].']</p>'; // close comment line 
        return TRUE; // finish the attack 
    } 
    

    अद्यतन (2014): कुछ साल बाद, मैं अब खेल के पूर्ण कोड बेस open-source on GitHub के रूप में जारी किया है। यदि कोई दिलचस्पी लेता है, तो आपको इस सिमुलेशन in this file का विशिष्ट कार्यान्वयन मिलेगा।

    +1

    मैं नहीं यकीन है कि मौसम Stackoverflow 191 एलओसी पर चर्चा के लिए सही जगह है। विशेष रूप से आप शायद ही अकेले हैं जो जानते हैं कि क्या आप कोड हैं 100% अर्थात् सही है। संकेत: एक भाषा का फैसला करें, अपने कोड में अंग्रेजी और जर्मन मिश्रण न करें। – middus

    +0

    @ मिडस: मुझे खेद है। मैंने जर्मन में कोड लिखा लेकिन मैंने आपके लिए सभी महत्वपूर्ण भागों का अंग्रेजी में अनुवाद किया है। शायद बेवकूफ सवाल: "1 9 1 एलओसी" क्या है? मैंने सोचा कि कोई मेरी मदद कर सकता है क्योंकि सभी आवश्यक डेटा प्रश्न में हैं। चलो देखते हैं ... – caw

    +1

    LOC = कोड की रेखा –

    उत्तर

    8

    सामान्यतः, ऐसा लगता है कि यह एक जटिल समस्या है, और मुझे यकीन नहीं है कि आप इसे कितना कुशल प्राप्त करेंगे।

    उसने कहा, मैंने कुछ चीजें देखी हैं जो निश्चित रूप से आपकी मदद करेंगे।

    सबसे पहले मैं पैरामीटर में चर टाइप करूंगा। यह आपके कोड को तेज़ी से नहीं बना सकता है, लेकिन इससे इसे पढ़ने और डीबग करना आसान हो जाएगा। इसके बाद, मैं $ teamname_att, $ teamname_def पैरामीटर को हटा दूंगा और बस उन्हें सहयोगी $ power_att, $ strength_def arrays में मान के रूप में रखूंगा। चूंकि इस डेटा को हमेशा वैसे भी जोड़ा जाता है, इससे दूसरी टीम के संदर्भ के रूप में गलती से एक टीम के नाम का उपयोग करने का खतरा कम हो जाएगा।

    यह कर देगा ताकि आप लगातार सरणियों में मानों को देखने के लिए नहीं करना होगा: इसका मतलब है कि

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

    के बाद से:

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

    आप जो सभी पैटर्न तीन सहायक कार्यों जब भी आप फ़ंक्शन चलाते हैं, तो आपको एक अतिरिक्त चर (कुछ महंगा हो सकता है) बनाना होगा, इसके बजाय इनका उपयोग करें:

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

    मैं मदद नहीं कर सकता है, लेकिन इस पद्धति लगातार आ रहा है नोटिस:

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

    मेरे सामान्य अनुभव है कि यदि आप comment_action में $ matchReport के संयोजन ले जाते हैं, तो यह सिर्फ थोड़ा तेजी से (आम तौर पर कम हो जाएगा एक दर्जन मिलीसेकंड से अधिक, लेकिन चूंकि आप उस कार्य को एक पुनरावर्ती कार्य के अंदर आधा दर्जन बार बुला रहे हैं, यह प्रति सेकंड एक सेकंड के दोवें हिस्से को दाढ़ी दे सकता है)।

    मुझे लगता है कि यह (काफी बेहतर प्रवाह होगा दोनों एक पाठक के नजरिए से, और

    अंत में से, वहाँ कई बार जहां एक ही पैरामीटर के साथ एक ही कार्य करने के लिए एक ही कॉल का उपयोग करेगा कर रहे हैं। कि कॉल करें सामने:।

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

    आशा इस मदद करता है

    +0

    बहुत बहुत धन्यवाद! आपकी युक्तियां स्क्रिप्ट को तेज करती हैं और इसे स्पष्ट बनाती हैं। मैं आपके सभी प्रस्तावों को लागू करूंगा जो मुझे लगता है। – caw

    0

    इन मानों को कितनी बार चेक किया जा रहा है? यदि यह बहुत से लोगों द्वारा उपयोग में जा रहा है और लगातार उन लोगों पर भर्ती कर रहा है यदि/और कथन, मैं देख सकता हूं कि आप बहुत सारी मेमोरी खा रहे हैं और धीरे-धीरे चल रहे हैं।

    शायद आप कुछ में से कुछ को बदलने के लिए वहां कुछ स्विच कर सकते हैं?

    मैं बस गति सुधार के लिए देख सकता हूं। एल्गोरिदम के लिए ही, मुझे थोड़ी देर बाद यह समझना होगा कि कोई और नहीं करता है।

    +0

    बहुत बहुत धन्यवाद, BraedenP! स्विच/अगर कथन से वास्तव में तेजी से स्विच कर रहे हैं? मुझे लगता है कि मैं स्विच का उपयोग नहीं कर सकता क्योंकि सभी सशर्त बयान घोंसला हैं। मुझे कोई विचार नहीं है कि यहां उचित रूप से स्विच का उपयोग कैसे करें। यह बहुत अच्छा होगा अगर आप बाद में एल्गोरिदम के लिए कुछ भी कह सकें। – caw

    +0

    अनुकूलन पर विचार क्यों करें? उन्होंने अपने एल्गोरिदम के लिए सुधार की मांग की। –

    +0

    और एक अनुकूलन कोई सुधार नहीं है? – BraedenP

    5

    मैं (जल्दी) के माध्यम से यह पढ़ सकते हैं और मैं चीजों की एक जोड़ी देखा:

    • फ़ील्ड के सभी तिहाई में लाल/पीला कार्ड सौंप दिया गया प्रतिशत, क्या यह जानबूझकर है? मैं एक फुटबॉल लड़का नहीं हूं, लेकिन मैं कहूंगा कि पहले के मुकाबले मैदान के आखिरी तीसरे हिस्से में अपराध होने की संभावना अधिक है। (क्योंकि यदि आप पहले हैं, तो आप संभावित रूप से बचाव कर रहे हैं)

    • यह निर्धारित करने के लिए कि प्रत्येक टीम के लिए जुर्माना स्कोर किया गया है, हालांकि कुछ टीमों या खिलाड़ियों को स्कोर करने की अधिक संभावना है दूसरों की तुलना में जुर्माना।

    • आप खाता कोने किक्स, एक फाउल के बाद संभावित चोटों, या सिर का उपयोग करके गोल किए गए लक्ष्यों को नहीं ले रहे हैं (जो रिपोर्ट में उल्लेखनीय हो सकता है)।

    इसके अलावा, आप सिर्फ इस अनुकरण समय की एक बहुत चलाने के लिए और अगर मान आपके द्वारा चुने गए सही हैं देखने के लिए की आवश्यकता होगी; एल्गोरिदम tweak। करने के लिए सबसे अच्छी बात यह है कि हाथ इसे ट्विक करें (उदाहरण के लिए, फाइल से सभी स्थिरांक पढ़ें और विभिन्न मूल्यों और विभिन्न टीमों के साथ कुछ सौ सिमुलेशन चलाएं), सबसे आसान काम संभवतया आनुवांशिक एल्गोरिदम को लागू करने और खोजने के लिए करना है बेहतर मूल्य

    असल में जो आपके पास है वह वास्तविक गेमप्ले/एआई कोड है, इसलिए आप इस प्रकार के कोड को प्रबंधित करने के लिए गेम स्टूडियो द्वारा उपयोग की जाने वाली तकनीकों पर पढ़ना चाहेंगे। (एक बात यह है कि चर को Google स्प्रेडशीट में रखना है जिसे आप अधिक आसानी से साझा/ट्विक कर सकते हैं, उदाहरण के लिए)।

    इसके अलावा, भले ही आप कुछ वास्तविक चीज़ों को याद कर रहे हों, असली वास्तविक मैच के रूप में, यथासंभव यथार्थवादी होने की कोशिश करने का कोई मतलब नहीं है क्योंकि आमतौर पर इन मामलों में सटीक प्रदान करने के लिए अच्छा गेमप्ले प्रदान करना अधिक महत्वपूर्ण है सिमुलेशन।

    +0

    धन्यवाद, बहुत अच्छे सुझाव! :) मैं उन सभी पर एक नज़र डालेगा। मुझे लगता है कि मैं आपकी युक्तियों के साथ सिमुलेशन की गुणवत्ता में सुधार कर सकता हूं। – caw

    +0

    "इस सिमुलेशन को कई बार चलाएं और देखें कि आपके द्वारा चुने गए मान सही हैं या नहीं," मैं यह कैसे कर सकता हूं? बस सभी मैच रिपोर्टों पर नज़र डालें और फैसला करें कि वे यथार्थवादी हैं या नहीं?- "सबसे आसान काम संभवतः एक बेहतर आनुवंशिक एल्गोरिदम को बेहतर मूल्यों को खोजने और खोजने के लिए लागू करना है" आनुवांशिक एल्गोरिदम को ऐसा कैसे करना चाहिए? प्रत्येक आबादी की गुणवत्ता/फिटनेस/सफलता को कैसे मापें? – caw

    +0

    आप जो कुछ भी करते हैं उसे कुछ प्रसिद्ध टीमों के सेट के लिए कुछ मूल्य मिलते हैं (या बल्कि, टीम जो अधिकतर समय और टीमों को जीतती हैं जिन्हें आप हर समय खो देते हैं)। और फिर आप तय करते हैं कि आपको कौन से नतीजे पसंद हैं। अब इसके साथ समस्या यह है कि, परिणामों की आपकी व्याख्या बहुत ही विषयपरक हो सकती है। –

    5

    Yous लापता हो रहे हैं: -

    #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; 
    } 
    
    +1

    : डी बहुत मजाकिया, शायद मैं इसे ईस्टर अंडे या मजेदार खेलों के रूप में कार्यान्वित करूंगा। ;) – caw

    संबंधित मुद्दे