Esempio n. 1
0
        /// <summary>
        /// 变异
        /// </summary>
        /// <param name="session"></param>
        /// <returns></returns>
        public NWSEGenome mutate(Session session)
        {
            NWSEGenome genome = this.clone();

            genome.generation = session.Generation;

            //若有效基因库不空,生成有效基因
            List <NodeGene> vaildGenes = genome.validInferenceGenes;

            if (vaildGenes != null && vaildGenes.Count > 0)
            {
                foreach (NodeGene g in vaildGenes)
                {
                    if (genome.exist(g.Id))
                    {
                        continue;
                    }
                    genome.put(g.clone <NodeGene>());
                }
            }

            //添加一个处理器
            List <NodeGene> inputGenes    = null;
            HandlerGene     newHandleGene = null;
            int             maxcount      = 8;

            if (rng.NextDouble() < Session.config.evolution.mutate.newHandleGeneProb)
            {
                int num = 0;
                while (++num <= maxcount)
                {
                    //随机选择处理器
                    double[] handler_selection_prob = Session.GetConfiguration().evolution.mutate.Handlerprob.ToArray();
                    Configuration.Handler cHandler  = Session.GetConfiguration().random_handler(handler_selection_prob);
                    //选择感知组
                    List <String> cataories = genome.GetAllCataory("rotate");
                    String        cataory   = cataories[rng.Next(0, cataories.Count)];
                    inputGenes = genome.GetAllInputGenes(cataory);
                    inputGenes.shuffle();
                    //确定输入数
                    int inputcount = rng.Next(cHandler.mininputcount, cHandler.maxinputcount + 1);
                    if (inputGenes.Count < inputcount)
                    {
                        continue;
                    }

                    //确定输入
                    List <int> inputids = new List <int>();
                    for (int i = 0; i < inputcount; i++)
                    {
                        inputids.Add(inputGenes[i].Id);
                    }

                    //生成
                    double[]    ps = cHandler.randomParam();
                    HandlerGene g  = new HandlerGene(genome, cHandler.name, inputids, ps);
                    g.Generation = session.Generation;
                    g.Cataory    = inputGenes[0].Cataory;
                    g.Group      = inputGenes[0].Group;
                    g.sortInput();

                    g.Id = session.GetIdGenerator().getGeneId(g);
                    if (genome.exist(g.Id))
                    {
                        continue;
                    }
                    newHandleGene = g;
                    genome.handlerGenes.Add(newHandleGene);
                    session.triggerEvent(Session.EVT_LOG, "A new handler gene is produced in " + genome.id.ToString() + ":" + newHandleGene.Text);
                    break;
                }
            }

            //如果生成了新hanler,生成后置变量
            InferenceGene inf = null;

            if (newHandleGene != null)
            {
                List <int> conditions = new List <int>();
                List <int> variables  = new List <int>();
                int        timediff   = 1;
                variables.Add(newHandleGene.Id);
                List <ReceptorGene> actionSensorGenes = genome.getActionSensorGenes();
                for (int i = 0; i < actionSensorGenes.Count; i++)
                {
                    conditions.Add(actionSensorGenes[i].Id);
                }

                inf            = new InferenceGene(genome, timediff, conditions, variables);
                inf.Cataory    = newHandleGene.Cataory;
                inf.Generation = session.Generation;
                inf.Id         = Session.idGenerator.getGeneId(inf);
                genome.inferenceGenes.Add(inf);
                session.triggerEvent(Session.EVT_LOG, "A inference gene is added in " + genome.id.ToString() + ":" + inf.Text);
            }

            //选择需要变异的推理节点(按照可靠度排序,可靠度越小变异概率越大)
            List <InferenceGene> infGenes = genome.inferenceGenes.FindAll(g => g.reability <= 0.5);

            if (infGenes == null || infGenes.Count <= 0)
            {
                infGenes = genome.inferenceGenes.FindAll(g => g.reability <= 0.98);
            }
            //没有需要变异的推理节点
            inputGenes = genome.GetAllInputGenes(null);
            int    mutateinfcount      = 0;
            double infMutateProb       = Session.config.evolution.mutate.infMutateProb;
            double infMutateAddProb    = Session.config.evolution.mutate.infMutateAddProb;
            double infMutateRemoveProb = Session.config.evolution.mutate.infMutateRemoveProb;

            for (int i = 0; i < infGenes.Count; i++)
            {
                if (rng.NextDouble() >= infMutateProb)
                {
                    continue;
                }
                inf       = infGenes[i].clone <InferenceGene>();
                inf.owner = genome;
                int        oldinfid = inf.Id;
                String     prevText = inf.Text;
                List <int> condIds  = inf.conditions;


                int num = 0;
                maxcount = 5;
                while (++num <= maxcount)
                {
                    //删除
                    if (condIds.Count > 2 && (condIds.Count == inputGenes.Count || rng.NextDouble() <= infMutateRemoveProb))
                    {
                        condIds = inf.conditions.FindAll(c => !genome[c].IsActionSensor());
                        int index = rng.Next(0, condIds.Count);
                        inf.conditions.RemoveAt(index);
                        if (Session.IsInvaildGene(inf))
                        {
                            inf = infGenes[i].clone <InferenceGene>(); inf.owner = genome; continue;
                        }
                        inf.Id = Session.idGenerator.getGeneId(inf);
                        genome.replaceGene(oldinfid, inf);
                        mutateinfcount += 1;
                        session.triggerEvent(Session.EVT_LOG, "conditions of inference gene is removed(" + genome.id.ToString() + "):" + prevText + "--->" + inf.Text);
                        break;
                    }
                    //添加
                    else if (condIds.Count < inputGenes.Count && rng.NextDouble() <= infMutateAddProb)
                    {
                        List <NodeGene> temp  = inputGenes.FindAll(g => !condIds.Contains(g.Id));
                        int             index = rng.Next(0, temp.Count);
                        inf.conditions.Add(temp[index].Id);
                        inf.Sortdimension();
                        if (Session.IsInvaildGene(inf))
                        {
                            inf = infGenes[i].clone <InferenceGene>(); inf.owner = genome; continue;
                        }
                        inf.Id = Session.idGenerator.getGeneId(inf);
                        genome.replaceGene(oldinfid, inf);
                        mutateinfcount += 1;
                        session.triggerEvent(Session.EVT_LOG, "conditions of inference gene is added(" + genome.id.ToString() + "):" + prevText + "--->" + inf.Text);
                        break;
                    }
                    else  //修改
                    {
                        condIds = inf.conditions.FindAll(c => !genome[c].IsActionSensor());
                        if (condIds.Count <= 0)
                        {
                            continue;
                        }
                        int             index1 = rng.Next(0, condIds.Count);
                        List <NodeGene> temp   = inputGenes.FindAll(g => !condIds.Contains(g.Id) || g.Id != condIds[index1]);
                        int             index2 = rng.Next(0, temp.Count);
                        inf.conditions[index1] = temp[index2].Id;
                        inf.Sortdimension();
                        if (Session.IsInvaildGene(inf))
                        {
                            inf = infGenes[i].clone <InferenceGene>(); inf.owner = genome; continue;
                        }
                        inf.Id = Session.idGenerator.getGeneId(inf);
                        genome.replaceGene(oldinfid, inf);
                        mutateinfcount += 1;
                        session.triggerEvent(Session.EVT_LOG, "conditions of inference gene is modified(" + genome.id.ToString() + "):" + prevText + "--->" + inf.Text);
                        break;
                    }
                }
            }

            genome.computeNodeDepth();
            genome.id = Session.idGenerator.getGenomeId();
            return(genome);
        }