Partage
  • Partager sur Facebook
  • Partager sur Twitter

Dérécursifier l'Algorithme Alpha-beta

    15 octobre 2015 à 0:23:25

    J'ai trouvé l' algorithme  "Alpha-beta" sur le site "http://www.ffothello.org" qui se présente comme suit:

    int alphabêta(int depth, int alpha, int bêta)
    {
       if (game over or depth <= 0)
          return winning score or eval();
       move bestMove;
       if(nœud == MAX) { //Programme
          for (each possible move m) {
             make move m;
             int score = alphabêta(depth - 1, alpha, bêta)
             unmake move m;
             if (score > alpha) {
                alpha = score;
                bestMove = m ;
                if (alpha >= bêta)
                   break;
             }
          }
          return alpha ;
       } 
       else { //type MIN = adversaire
          for (each possible move m) {
             make move m;
             int score = alphabêta(depth - 1, alpha, bêta)
             unmake move m;
             if (score < bêta) {
                bêta = score;
                bestMove = m ;
                if (alpha >= bêta)
                   break;
             }
          }
          return bêta;
       }
    }

     Je l'ai appliqué en javascript mais en l'exécutant, le navigateur affiche la boîte de dialogue qui demande de continuer, de débuger ou d'arrêter le script. J'essaie, sans succès, de réécrire l'alpha beta sans récursion.

    Je vais coller à la suite de ce message la transciption en javascript  de ma version sans récursion.

    Plateau=function(numJoueur,id_Partie){
        this.lstJoueur=[];
        this.passe=0;
        this.tour=1;
        this.numJoueur=(typeof(numJoueur)=="undefined")?1:numJoueur;
        this.id_Partie=(typeof(id_Partie)=="undefined")?1:id_Partie;
        this.finJeu=false;
    }
    Plateau.prototype.joue=function(x,y,numJoueur,a_envoyer){...}
    Plateau.prototype.deJoue=function(x,y,numJoueur,a_envoyer){...}
    
    Plateau.prototype.alphabeta = function(depth,alpha,beta){
        var noeud;
        var fin;
        var that=this;
        var t=this.getTour();
        var lstMvt=[];
        var mvt={
                pos:{x:-1,y:0,n:that.getTour()},
                bst:{},
                scr:-1000000,
                alp:alpha,
                bta:beta,
                evl:false,
                fin:false
                }
        fin=sC.svt(mvt.pos);//Le premier point accessible
        if (fin) mvt.fin=true;
        lstMvt.push(mvt);
        ////////////////////////////////////////////////////
        var d=0;
        var ct=0;
        var cpt=0;
        var tm=0;
        ///////////////////
        var    fn=function(){
            if (ct!=cpt) return;
                if (ct%100==0) document.getElementById("comment").innerHTML=ct+"-"+cpt;
            ct++;
            if (!lstMvt[0].fin){
                var r;
                // /*Pour aller au suivant*/
                if (!lstMvt[d].fin){
                    hh+="<br/>"+ct+"<br/>";
                    if (!lstMvt[d].fin){
                        that.joue(lstMvt[d].pos.x,lstMvt[d].pos.y,lstMvt[d].pos.n,false);
                        sC.calcUnLstPos(lstMvt[d].pos.x,lstMvt[d].pos.y,1);
                        hh+="<br/>"+ct+TAB+"-"+TAB+cpt+" - d = "+d+
                            "<br/>"+(d>0?(JSON.stringify(lstMvt[d-1])+"<br/>"+" -- > "+"<br/>"):"")+
                            JSON.stringify(lstMvt[d]);
                        // /*AJOUTER UN NIVEAU EN DESSOUS*/
                        if (!that.finJeu && d<depth-1){
                            mvt={
                            pos:{x:-1,y:0,n:that.getTour()},
                            bst:{},
                            scr:-1000000,
                            alp:lstMvt[d].alp,
                            bta:lstMvt[d].bta,
                            evl:false,
                            fin:false
                            }
                        } else {
                            mvt={
                            pos:{x:-1,y:0,n:that.getTour()},
                            bst:{},
                            scr:that.eval(t),
                            alp:lstMvt[d].alp,
                            bta:lstMvt[d].bta,
                            evl:true,
                            fin:true
                            }
                        }
                        fin=sC.svt(mvt.pos);
                        if (fin) mvt.fin=true;
                        lstMvt.push(mvt);    d++;
                        nn=that.nbrePlacesOcc;
                    } else {
                    }
                }
                if (lstMvt[d].fin){
                    if (d>0){
                        mvt=lstMvt.pop();
                        d--;
                       
                        that.deJoue(lstMvt[d].pos.x,lstMvt[d].pos.y,lstMvt[d].pos.n,false);
                        sC.calcUnLstPos(lstMvt[d].pos.x,lstMvt[d].pos.y,-1);
                       
                        if (that.getTour()==t){
                            if (mvt.scr > lstMvt[d].alp){
                                lstMvt[d].bst = {
                                x:lstMvt[d].pos.x,
                                y:lstMvt[d].pos.y,
                                n:lstMvt[d].pos.n
                                };
                                lstMvt[d].alp = mvt.scr;
                            }
                        } else {
                            if (mvt.scr < lstMvt[d].bta){
                                lstMvt[d].bst = {
                                x:lstMvt[d].pos.x,
                                y:lstMvt[d].pos.y,
                                n:lstMvt[d].pos.n
                                };
                                lstMvt[d].bta = mvt.scr;
                            }
                        }
                        if (lstMvt[d].alp >= lstMvt[d].bta){
                            lstMvt[d].fin=true;
                        }
                        if (!lstMvt[d].fin){
                            fin=sC.svt(lstMvt[d].pos);
                            if (fin) lstMvt[d].fin=true;
                        }
                        if (lstMvt[d].fin){
                            if (!lstMvt[d].evl)
                                lstMvt[d].scr=(that.getTour()==t)?lstMvt[d].alp :lstMvt[d].bta;
                        }
                    }
                }
            } else {
                mvt=lstMvt[0];
                that.joue(mvt.bst.x,mvt.bst.y,mvt.bst.n,true);
                clearInterval(tm);
                le_jeu.pltJeu.paint({numJoueur:mvt.bst.n,i:mvt.bst.x,j:mvt.bst.y});
            }
            cpt++;
            if (ct>50000){
                clearInterval(tm);
            }
        }
        tm=setInterval(fn,0);
    }

    J'espère que vous pourrez vous y retrouvez et m'aider. Merci d'avance.

    -
    Edité par GodNudybee 17 octobre 2015 à 0:25:13

    • Partager sur Facebook
    • Partager sur Twitter

    Dérécursifier l'Algorithme Alpha-beta

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown