예제 #1
0
 private static List <EvolutionTreeNode> getAliveNodes(EvolutionTreeNode node, List <EvolutionTreeNode> r)
 {
     if (r == null)
     {
         r = new List <EvolutionTreeNode>();
     }
     if (node == null)
     {
         return(r);
     }
     if (node.extinct)
     {
         return(r);
     }
     r.Add(node);
     if (node.childs == null || node.childs.Count <= 0)
     {
         return(r);
     }
     foreach (EvolutionTreeNode child in node.childs)
     {
         r = getAliveNodes(child, r);
     }
     return(r);
 }
예제 #2
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="network"></param>
 /// <param name="parent"></param>
 /// <param name="childs"></param>
 public EvolutionTreeNode(Network network, EvolutionTreeNode parent = null, List <EvolutionTreeNode> childs = null)
 {
     this.network = network;
     this.parent  = parent;
     this.depth   = parent == null ? 0 : parent.depth + 1;
     if (childs != null)
     {
         this.childs.AddRange(childs);
     }
 }
예제 #3
0
        private void saveNetwork(DirectoryInfo directoryInfo, EvolutionTreeNode node)
        {
            if (node == null)
            {
                return;
            }
            String filename = node.network.GetFileName(directoryInfo.FullName);

            if (!Directory.Exists(filename))
            {
                node.network.Save(directoryInfo.FullName, node.network.Genome.generation);
            }

            foreach (EvolutionTreeNode child in node.childs)
            {
                saveNetwork(directoryInfo, child);
            }
        }
예제 #4
0
 /// <summary>
 /// 查找特定网络所在节点
 /// </summary>
 /// <param name="node"></param>
 /// <param name="network"></param>
 /// <returns></returns>
 public static EvolutionTreeNode search(EvolutionTreeNode node, Network network)
 {
     if (node == null)
     {
         return(null);
     }
     if (node.network == network)
     {
         return(node);
     }
     for (int i = 0; i < node.childs.Count; i++)
     {
         EvolutionTreeNode n = search(node.childs[i], network);
         if (n != null)
         {
             return(n);
         }
     }
     return(null);
 }
예제 #5
0
        private StringBuilder buildString(EvolutionTreeNode node, StringBuilder str)
        {
            if (str == null)
            {
                str = new StringBuilder();
            }
            if (node == null)
            {
                return(str);
            }

            foreach (EvolutionTreeNode child in node.childs)
            {
                str.Append(child.ToString() + System.Environment.NewLine);
            }

            foreach (EvolutionTreeNode child in node.childs)
            {
                str = buildString(child, str);
            }
            return(str);
        }
예제 #6
0
        public static int getDepth(EvolutionTreeNode node)
        {
            if (node == null)
            {
                return(0);
            }
            if (node.childs == null || node.childs.Count <= 0)
            {
                return(node.depth);
            }
            int d = node.depth;

            foreach (EvolutionTreeNode n in node.childs)
            {
                int t = getDepth(n);
                if (t > d)
                {
                    d = t;
                }
            }
            return(d);
        }
예제 #7
0
        /// <summary>
        /// 执行
        /// </summary>
        /// <param name="inds"></param>
        public void execute(List <Network> inds, Session session)
        {
            session.triggerEvent(Session.EVT_LOG, "population evoluting...");
            //1.将无效推理(可靠度小于阈值)的推理节点和有效推理节点向周边传播
            //  使得周围个体的下一代不会产生无效推理节点,并必然产生有效推理节点
            for (int i = 0; i < inds.Count; i++)
            {
                List <NodeGene> vaildInference = inds[i].FindVaildInferences();
                session.triggerEvent(Session.EVT_VAILD_GENE, inds[i], vaildInference);

                List <EvolutionTreeNode> nearest = EvolutionTreeNode.search(session.getEvolutionRootNode(), inds[i]).getNearestNode();
                nearest.ForEach(node => node.network.Genome.gene_drift(vaildInference));
            }


            //2.计算每个个体所有节点平均可靠度
            List <double> reability = inds.ConvertAll(ind => ind.Reability);


            //3.根据所有个体可靠度的均值和方差, 确定淘汰个体的可靠度下限:以可靠度均值和方差构成的高斯分布最大值的
            if (inds.Count >= Session.GetConfiguration().evolution.selection.min_population_capacity)
            {
                double reability_high_limit = Session.GetConfiguration().evaluation.gene_reability_range.Max;
                int    prevcount            = inds.Count;
                //计算分位数
                int            q                  = (int)(1 + (reability.Count - 1) * Session.GetConfiguration().evolution.selection.reability_selection_limit);
                List <int>     indexes            = reability.argsort();
                double         reability_lowlimit = reability[indexes[q]];
                List <Network> reversedinds       = new List <Network>();
                for (int k = 0; k < q; k++)
                {
                    EvolutionTreeNode.search(session.root, inds[indexes[k]])
                    .extinct = true;
                }
                for (int k = q; k < indexes.Count; k++)
                {
                    reversedinds.Add(inds[indexes[k]]);
                }

                inds.Clear();
                inds.AddRange(reversedinds);
                session.triggerEvent(Session.EVT_SELECTION, prevcount, inds.Count, reability_lowlimit);
            }

            //4.计算每个个体所有节点适应度总和所占全部的比例,该比例乘以基数为下一代数量
            int propagate_base_count = inds.Count * Session.GetConfiguration().evolution.propagate_base_count;

            if (propagate_base_count > Session.GetConfiguration().evolution.selection.max_population_capacity)
            {
                propagate_base_count = Session.GetConfiguration().evolution.selection.max_population_capacity;
            }
            List <double> fitnessList = inds.ConvertAll(ind => ind.Fitness);

            for (int i = 0; i < fitnessList.Count; i++)
            {
                if (fitnessList[i] == 0)
                {
                    fitnessList[i] = 0.000001;
                }
            }
            double totalFitness = fitnessList.Sum();

            if (totalFitness != 0)
            {
                fitnessList = fitnessList.ConvertAll(f => f / totalFitness);
            }
            List <int> fitnessIndex = fitnessList.argsort();

            fitnessIndex.Reverse();
            int[] planPropagateCount = new int[inds.Count];
            for (int i = 0; i < fitnessIndex.Count; i++)
            {
                planPropagateCount[fitnessIndex[i]] = (int)(fitnessList[fitnessIndex[i]] * propagate_base_count);
                if (planPropagateCount.Sum() >= Session.GetConfiguration().evolution.selection.max_population_capacity)
                {
                    break;
                }
            }

            //5.通过变异生成下一代
            List <Network> newinds = new List <Network>();

            for (int i = 0; i < inds.Count; i++)
            {
                EvolutionTreeNode node = EvolutionTreeNode.search(session.getEvolutionRootNode(), inds[i]);
                int childcount         = planPropagateCount[i];
                for (int j = 0; j < childcount; j++)
                {
                    NWSEGenome mutateGenome = inds[i].Genome.mutate(session);
                    while (inds.Exists(ind => ind.Genome.equiv(mutateGenome)))
                    {
                        mutateGenome = inds[i].Genome.mutate(session);
                    }
                    Network mutateNet = new Network(mutateGenome);
                    newinds.Add(mutateNet);
                    EvolutionTreeNode cnode = new EvolutionTreeNode(mutateNet, node);
                    node.childs.Add(cnode);

                    session.judgePaused();
                }
            }
            inds.AddRange(newinds);


            session.triggerEvent(Session.EVT_GENERATION_END);
        }