示例#1
0
        /// <summary>
        /// Remplace une variable lié ou libre aléatoire par une variable anonyme.
        /// Echoue si l'argument séléctionné ne peut être une variable anonyme.
        /// </summary>
        /// <param name="index">Index dans l'ensemble d'action.</param>
        /// <param name="actionSet">Ensemble d'action à traiter.</param>
        /// <param name="env">Environnement à prendre en compte.</param>
        /// <returns>Le classifieur fils muté, null si la mutation échoue.</returns>
        private Classifier VarToAnonym(int index, List <Classifier> parentActionSet, PerceivedEnvironnement env)
        {
            List <Classifier> actionSet = ObjectCopier.Clone(parentActionSet);
            Dictionary <ArgType, Dictionary <string, List <int[]> > > varList = GetAllValues(ArgMode.BOUND, actionSet, index);
            Random rand = new Random();

            if (varList.Keys.Count != 0)
            {
                ArgType randArgType           = new List <ArgType>(varList.Keys)[rand.Next(varList.Keys.Count)];
                string  randArgVar            = new List <string>(varList[randArgType].Keys)[rand.Next(varList[randArgType].Keys.Count)];
                int[]   randPredicateArgIndex = varList[randArgType][randArgVar][rand.Next(varList[randArgType][randArgVar].Count)];

                if (randPredicateArgIndex[0] != -1)
                {
                    if (actionSet[index].rule.body[randPredicateArgIndex[0]].predOp.argsOptions[randPredicateArgIndex[1]].argsMode.Contains(ArgMode.ANONYMOUS))
                    {
                        string     newArg  = "_";
                        HornClause newRule = actionSet[index].rule;
                        newRule.body[randPredicateArgIndex[0]].values[randPredicateArgIndex[1]] = newArg;
                        return(new Classifier(currentTime, newRule, fo, actionSet[index]));
                    }
                }
                else
                {
                    if (actionSet[index].rule.head.predOp.argsOptions[randPredicateArgIndex[1]].argsMode.Contains(ArgMode.ANONYMOUS))
                    {
                        string     newArg  = "_";
                        HornClause newRule = actionSet[index].rule;
                        newRule.head.values[randPredicateArgIndex[1]] = newArg;
                        return(new Classifier(currentTime, newRule, fo, actionSet[index]));
                    }
                }
            }
            return(null);
        }
示例#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;
 }
示例#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;
 }
示例#4
0
        /// <summary>
        /// Remplace une variable anonyme aléatoire par une variable libre ou lié.
        /// Echoue si la variable ne peut être une variable libre ou lié.
        /// </summary>
        /// <param name="index">Index dans l'ensemble d'action.</param>
        /// <param name="actionSet">Ensemble d'action à traiter.</param>
        /// <param name="env">Environnement à prendre en compte.</param>
        /// <returns>Le classifieur fils muté, null si la mutation échoue.</returns>
        public Classifier AnonymToVar(int index, List <Classifier> parentActionSet, PerceivedEnvironnement env)
        {
            List <Classifier> actionSet = ObjectCopier.Clone(parentActionSet);
            Dictionary <ArgType, Dictionary <string, List <int[]> > > anonymList = GetAllValues(ArgMode.ANONYMOUS, actionSet, index);
            Dictionary <ArgType, Dictionary <string, List <int[]> > > varList    = GetAllValues(ArgMode.BOUND, actionSet, index);
            Random rand = new Random();

            if (anonymList.Keys.Count != 0 && varList.Keys.Count != 0)
            {
                ArgType randArgType           = new List <ArgType>(anonymList.Keys)[rand.Next(anonymList.Keys.Count)];
                string  randArgAnonym         = new List <string>(anonymList[randArgType].Keys)[rand.Next(anonymList[randArgType].Keys.Count)];
                int[]   randPredicateArgIndex = anonymList[randArgType][randArgAnonym][rand.Next(anonymList[randArgType][randArgAnonym].Count)];

                if (randPredicateArgIndex[0] != -1)
                {
                    if (actionSet[index].rule.body[randPredicateArgIndex[0]].predOp.argsOptions[randPredicateArgIndex[1]].argsMode.Contains(ArgMode.BOUND))
                    {
                        int randVar = rand.Next(varList[randArgType].Keys.Count
                                                + (actionSet[index].rule.body[randPredicateArgIndex[0]].predOp.argsOptions[randPredicateArgIndex[1]].argsMode.Contains(ArgMode.VAR) ? 1 : 0));
                        string newArg;
                        if (randVar == varList[randArgType].Keys.Count || !actionSet[index].rule.body[randPredicateArgIndex[0]].predOp.argsOptions[randPredicateArgIndex[1]].argsMode.Contains(ArgMode.BOUND))
                        {
                            newArg = randArgType.ToString() + varCount[randArgType];
                            varCount[randArgType]++;
                        }
                        else
                        {
                            newArg = new List <string>(varList[randArgType].Keys)[randVar];
                        }

                        HornClause newRule = actionSet[index].rule;
                        newRule.body[randPredicateArgIndex[0]].values[randPredicateArgIndex[1]] = newArg;
                        return(new Classifier(currentTime, newRule, fo, actionSet[index]));
                    }
                }

                else
                {
                    if (actionSet[index].rule.head.predOp.argsOptions[randPredicateArgIndex[1]].argsMode.Contains(ArgMode.BOUND))
                    {
                        int randVar = rand.Next(varList[randArgType].Keys.Count
                                                + (actionSet[index].rule.head.predOp.argsOptions[randPredicateArgIndex[1]].argsMode.Contains(ArgMode.VAR) ? 1 : 0));
                        string newArg;
                        if (randVar == varList[randArgType].Keys.Count || !actionSet[index].rule.head.predOp.argsOptions[randPredicateArgIndex[1]].argsMode.Contains(ArgMode.BOUND))
                        {
                            newArg = randArgType.ToString() + varCount[randArgType];
                            varCount[randArgType]++;
                        }
                        else
                        {
                            newArg = new List <string>(varList[randArgType].Keys)[randVar];
                        }

                        HornClause newRule = actionSet[index].rule;
                        newRule.head.values[randPredicateArgIndex[1]] = newArg;
                        return(new Classifier(currentTime, newRule, fo, actionSet[index]));
                    }
                }
            }
            return(null);
        }
示例#5
0
        /// <summary>
        /// Remplace chaque occurence d'une constante aléatoire par une variable libre ou lié aléatoire.
        /// Echoue si l'argument ne peut être une variable lié.
        /// </summary>
        /// <param name="index">Index dans l'ensemble d'action.</param>
        /// <param name="actionSet">Ensemble d'action à traiter.</param>
        /// <param name="env">Environnement à prendre en compte.</param
        /// <returns>Le classifieur fils muté, null si la mutation échoue.</returns>
        private Classifier ConstToVar(int index, List <Classifier> parentActionSet, PerceivedEnvironnement env)
        {
            List <Classifier> actionSet = ObjectCopier.Clone(parentActionSet);
            Dictionary <ArgType, Dictionary <string, List <int[]> > > constList = GetAllValues(ArgMode.CONST, actionSet, index);
            Dictionary <ArgType, Dictionary <string, List <int[]> > > varList   = GetAllValues(ArgMode.BOUND, actionSet, index);
            Random rand = new Random();

            Attribute newHead = ObjectCopier.Clone(actionSet)[index].rule.head;

            Attribute[] newBody = ObjectCopier.Clone(actionSet)[index].rule.body;

            if (constList.Keys.Count != 0)
            {
                ArgType randArgType  = constList.Keys.ToArray()[rand.Next(constList.Keys.Count)];
                string  randArgConst = constList[randArgType].Keys.ToArray()[rand.Next(constList[randArgType].Keys.Count)];

                int randVar = -1;
                if (varList.Keys.Contains(randArgType))
                {
                    randVar = rand.Next(varList[randArgType].Keys.Count + 1);
                }
                string newArg;
                if (randVar == -1 || randVar == varList[randArgType].Keys.Count)
                {
                    newArg = randArgType.ToString() + varCount[randArgType];
                    varCount[randArgType]++;
                }
                else
                {
                    newArg = varList[randArgType].Keys.ToArray()[randVar];
                }

                HornClause newRule = actionSet[index].rule;
                foreach (int[] predicateArgIndex in constList[randArgType][randArgConst])
                {
                    if (predicateArgIndex[0] != -1)
                    {
                        if (actionSet[index].rule.body[predicateArgIndex[0]].predOp.argsOptions[predicateArgIndex[1]].argsMode.Contains(ArgMode.BOUND) ||
                            actionSet[index].rule.body[predicateArgIndex[0]].predOp.argsOptions[predicateArgIndex[1]].argsMode.Contains(ArgMode.VAR) &&
                            randVar == varList[randArgType].Keys.Count)
                        {
                            newRule.body[predicateArgIndex[0]].values[predicateArgIndex[1]] = newArg;
                        }
                        else
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        if (actionSet[index].rule.head.predOp.argsOptions[predicateArgIndex[1]].argsMode.Contains(ArgMode.BOUND) ||
                            actionSet[index].rule.head.predOp.argsOptions[predicateArgIndex[1]].argsMode.Contains(ArgMode.VAR) &&
                            randVar == varList[randArgType].Keys.Count)
                        {
                            newRule.head.values[predicateArgIndex[1]] = newArg;
                        }
                        else
                        {
                            return(null);
                        }
                    }
                }
                return(new Classifier(currentTime, newRule, fo, actionSet[index]));
            }
            return(null);
        }