Example #1
0
        /// <summary>
        /// Constructeur d'une instance de FOXCS et initialise ses variables.
        /// </summary>
        /// <param name="fo"> </param>
        public FOXCS(FOXCSOptions fo)
        {
            this.env           = new PerceivedEnvironnement();
            this.prevEnv       = new PerceivedEnvironnement();
            this.currentTime   = 0;
            this.popSet        = new List <Classifier>();
            this.matchSet      = new Dictionary <Attribute, List <Classifier> >();
            this.actionSet     = new List <Classifier>();
            this.prevActionSet = new List <Classifier>();
            this.varCount      = new Dictionary <ArgType, int>();
            this.boundVarList  = new Dictionary <ArgType, Dictionary <string, string> >();
            this.reward        = 0;
            this.prevReward    = 0;
            this.fo            = fo;

            if (!File.Exists(fo.classifierFilePath))
            {
                FileStream fsCreate = File.Create(fo.classifierFilePath);
                fsCreate.Close();
            }

            LoadFOXCSData();

            foreach (ArgType argType in (ArgType[])Enum.GetValues(typeof(ArgType)))
            {
                if (!varCount.Keys.Contains(argType))
                {
                    varCount.Add(argType, 0);
                }
                if (!boundVarList.Keys.Contains(argType))
                {
                    boundVarList.Add(argType, new Dictionary <string, string>());
                }
            }
        }
Example #2
0
 /// <summary>
 /// Constructeur d'un classifieur à partir d'un classifieur parent.
 /// </summary>
 /// <param name="timeStamp"></param>
 /// <param name="rule"></param>
 /// <param name="fo"></param>
 /// <param name="parent"></param>
 public Classifier(int timeStamp, HornClause rule, FOXCSOptions fo, Classifier parent)
 {
     this.payOffPred = parent.payOffPred;
     this.errorPred  = parent.errorPred;
     this.fitness    = 0.1 * parent.fitness;
     this.exp        = 0;
     this.timeStamp  = timeStamp;
     this.averSize   = 1;
     this.numerosity = 1;
     this.rule       = rule;
 }
Example #3
0
 /// <summary>
 /// Constructeur d'un classifieur.
 /// </summary>
 /// <param name="timeStamp">Moment de création</param>
 /// <param name="rule">Règle définissant le classifieur></param>
 /// <param name="fo"></param>
 public Classifier(int timeStamp, HornClause rule, FOXCSOptions fo)
 {
     this.payOffPred = 0;
     this.errorPred  = 0.01 * (fo.rewardMode == true ? 1 : 504);
     this.fitness    = 0;
     this.exp        = 0;
     this.timeStamp  = timeStamp;
     this.averSize   = 1;
     this.numerosity = 1;
     this.rule       = rule;
 }
        /// <summary>
        /// Met à jour les informations d'états et d'actions à partir du tableau courant.
        /// </summary>
        public void UpdateSensors(Board board, FOXCSOptions fo)
        {
            this.states.Clear();
            this.actions.Clear();
            this.board = AIHandler.CloneBoard(board);
            this.tokenOnBoard.Clear();
            List <Token> tokenList = _GameManager.GetAllTokens();

            if (_GameManager.toPromoteToken != null)
            {
                this.actions.Add(new Attribute("promote", new string[] { _GameManager.toPromoteToken.ToPrologCode(), FOXCS.ToPrologCode(true) }, fo.actionPredicateOptions["promote"]));
                this.actions.Add(new Attribute("promote", new string[] { _GameManager.toPromoteToken.ToPrologCode(), FOXCS.ToPrologCode(false) }, fo.actionPredicateOptions["promote"]));
            }

            foreach (Token tok in tokenList)
            {
                Attribute acc;

                if (!tok.isCaptured)
                {
                    tokenOnBoard.Add(tok.ToPrologCode());
                    this.states.Add(new Attribute("onTile", new string[] { tok.ToPrologCode(), "c" + tok.box.getCoord().x.ToString(), "l" + tok.box.getCoord().y.ToString() }, fo.statePredicateOptions["onTile"]));
                    List <Coordinates> tokLegalMoves    = !tok.isPromoted ? tok.legalMoves(this.board) : tok.legalMovesPlus(this.board);
                    List <Coordinates> tokPossibleMoves = !tok.isPromoted ? tok.possibleMoves(this.board) : tok.possibleMovesPlus(this.board);

                    foreach (Coordinates cord in tokLegalMoves)
                    {
                        acc = new Attribute("legalMove", new string[] { tok.ToPrologCode(), "c" + cord.x.ToString(), "l" + cord.y.ToString() }, fo.statePredicateOptions["legalMove"]);
                        if (!states.Contains(acc))
                        {
                            this.states.Add(acc);
                        }



                        if (tok.owner.Equals(_GameManager.players[_GameManager.currentPlayerIndex]) && _GameManager.toPromoteToken == null)
                        {
                            acc = new Attribute("move", new string[] { tok.ToPrologCode(), "c" + cord.x.ToString(), "l" + cord.y.ToString() }, fo.actionPredicateOptions["move"]);
                            if (!actions.Contains(acc))
                            {
                                this.actions.Add(acc);
                            }
                        }
                    }

                    /*
                     * tok.getTokensToEat();
                     * foreach (Coordinates cord in tok.possibleEats)
                     * {
                     *  acc = new Attribute("inRange", new string[] { tok.ToPrologCode(), this.board.boxes[cord.getIndex()].getToken().ToPrologCode(), "c" + cord.x.ToString(), "l" + cord.y.ToString() }, fo.statePredicateOptions["inRange"]);
                     *  if (!states.Contains(acc))
                     *      this.states.Add(acc);
                     * }
                     */
                }

                else
                {
                    foreach (Coordinates cord in tok.legalDrops(board))
                    {
                        if (tok.owner.Equals(_GameManager.players[_GameManager.currentPlayerIndex]) && _GameManager.toPromoteToken == null)
                        {
                            acc = new Attribute("drop", new string[] { tok.ToPrologCode(), "c" + cord.x.ToString(), "l" + cord.y.ToString() }, fo.actionPredicateOptions["drop"]);
                            if (!actions.Contains(acc))
                            {
                                this.actions.Add(acc);
                            }
                        }

                        acc = new Attribute("legalDrop", new string[] { tok.ToPrologCode(), "c" + cord.x.ToString(), "l" + cord.y.ToString() }, fo.statePredicateOptions["legalDrop"]);
                        if (!states.Contains(acc))
                        {
                            this.states.Add(acc);
                        }
                    }
                }
            }
        }
            public KeyValuePair <List <Attribute>, KeyValuePair <List <Attribute>, Dictionary <string, int> > > AssertEnvironnement(PerceivedEnvironnement env, List <HornClause> knowledgeBase, Dictionary <ArgType, Dictionary <string, string> > boundVarList, FOXCSOptions fo)
            {
                List <Attribute>         newStates     = new List <Attribute>();
                List <Attribute>         deleteStates  = new List <Attribute>();
                Dictionary <string, int> usedPredicate = new Dictionary <string, int>();

                foreach (Attribute action in env.actions)
                {
                    if (!usedPredicate.ContainsKey(action.name))
                    {
                        usedPredicate.Add(action.name, action.arity);
                    }
                    action.YPassert();
                }

                foreach (Attribute state in env.states)
                {
                    if (!usedPredicate.ContainsKey(state.name))
                    {
                        usedPredicate.Add(state.name, state.arity);
                    }
                    state.YPassert();
                }


                if (knowledgeBase != null || knowledgeBase.Count == 0)
                {
                    foreach (HornClause clause in knowledgeBase)
                    {
                        if (!usedPredicate.ContainsKey(clause.head.name))
                        {
                            usedPredicate.Add(clause.head.name, clause.head.arity);
                        }
                        if (clause.clause == null)
                        {
                            clause.YPwriteAndCompile();
                        }

                        foreach (string tok1 in boundVarList[ArgType.TOKEN].Keys)
                        {
                            foreach (string tok2 in boundVarList[ArgType.TOKEN].Keys)
                            {
                                if (tok1 != tok2 && tok1.ToCharArray()[0] != tok2.ToCharArray()[0])
                                {
                                    foreach (string x in boundVarList[ArgType.X].Keys)
                                    {
                                        foreach (string y in boundVarList[ArgType.Y].Keys)
                                        {
                                            foreach (bool l1 in clause.Match(new object[] { tok1, tok2, x, y }))
                                            {
                                                Attribute acc;
                                                if (!newStates.Contains(acc = new Attribute("ennemyInRange", new string[] { tok1, tok2, x, y }, fo.statePredicateOptions["ennemyInRange"])))
                                                {
                                                    newStates.Insert(0, new Attribute("ennemyInRange", new string[] { tok1, tok2, x, y }, fo.statePredicateOptions["ennemyInRange"]));
                                                    newStates[0].YPassert();
                                                }
                                                if (!deleteStates.Contains(acc = new Attribute("legalMove", new string[] { tok1, x, y }, fo.statePredicateOptions["legalMove"])))
                                                {
                                                    deleteStates.Insert(0, new Attribute("legalMove", new string[] { tok1, x, y }, fo.statePredicateOptions["legalMove"]));
                                                }
                                                if (!deleteStates.Contains(acc = new Attribute("legalMove", new string[] { tok1, x, y }, fo.statePredicateOptions["legalMove"])))
                                                {
                                                    deleteStates.Insert(0, new Attribute("inRange", new string[] { tok1, tok2, x, y }, fo.statePredicateOptions["inRange"]));
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }


                return(new KeyValuePair <List <Attribute>, KeyValuePair <List <Attribute>, Dictionary <string, int> > >
                           (newStates,
                           new KeyValuePair <List <Attribute>, Dictionary <string, int> >
                               (deleteStates, usedPredicate)));
            }