public bool put(NodeGene gene)
        {
            if (gene == null)
            {
                return(false);
            }
            if (this.exist(gene.Id))
            {
                return(false);
            }
            if (this.exist(gene))
            {
                return(false);
            }
            if (gene is ReceptorGene)
            {
                this.receptorGenes.Add((ReceptorGene)gene);
                return(true);
            }
            else if (gene is HandlerGene)
            {
                this.handlerGenes.Add((HandlerGene)gene);
                return(true);
            }
            else if (gene is InferenceGene)
            {
                this.inferenceGenes.Add((InferenceGene)gene);
                return(true);
            }

            return(false);
        }
        public static NWSEGenome Parse(String str)
        {
            if (str == null || str.Trim() == "")
            {
                return(null);
            }
            List <String> s1 = str.Split(new String[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();

            if (s1 == null || s1.Count <= 0)
            {
                return(null);
            }

            NWSEGenome genome = new NWSEGenome();

            genome.id = int.Parse(s1[0]);
            s1.RemoveAt(0);

            if (int.TryParse(s1[0], out genome.generation))
            {
                s1.RemoveAt(0);
            }

            foreach (String s in s1)
            {
                if (s == null || s.Trim() == "")
                {
                    continue;
                }
                if (s.StartsWith("ReceptorGene"))
                {
                    genome.receptorGenes.Add(ReceptorGene.parse(genome, s));
                }
                else if (s.StartsWith("HandlerGene"))
                {
                    genome.handlerGenes.Add(HandlerGene.parse(genome, s));
                }
                else if (s.StartsWith("InferenceGene"))
                {
                    genome.inferenceGenes.Add(InferenceGene.parse(genome, s));
                }
                else if (s.StartsWith("handlerSelectionProb"))
                {
                    int s2 = s.IndexOf("handlerSelectionProb");
                    s2 = s.IndexOf("=", s2 + 1);
                    String s3 = s.Substring(s2 + 1).Trim();
                    genome.handlerSelectionProb.Clear();
                    genome.handlerSelectionProb.AddRange(Utility.parse(s3));
                }
                else if (s.StartsWith("vaild"))
                {
                    String s2 = s.Substring(s.IndexOf("="));
                    genome.validInferenceGenes.Add(NodeGene.parseGene(genome, s2));
                }
            }

            genome.computeNodeDepth();
            return(genome);
        }
        public int computeNodeDepth(NodeGene gene)
        {
            List <NodeGene> inputs = this.getInputs(gene);

            if (inputs == null || inputs.Count <= 0)
            {
                gene.Depth = 0; return(0);
            }
            gene.Depth = inputs.ConvertAll(i => i.Depth).Max() + 1;
            return(gene.Depth);
        }
 public bool exist(NodeGene g)
 {
     if (g.GetType() == typeof(HandlerGene))
     {
         return(this.handlerGenes.Exists(h => this.equiv(h, g)));
     }
     else if (g.GetType() == typeof(InferenceGene))
     {
         return(this.inferenceGenes.Exists(i => this.equiv(i, g)));
     }
     return(false);
 }
 /// <summary>
 /// 查找某节点的传入节点
 /// </summary>
 /// <param name="gene"></param>
 /// <returns></returns>
 public List <NodeGene> getInputs(NodeGene gene)
 {
     if (gene is ReceptorGene)
     {
         return(new List <NodeGene>());
     }
     if (gene is HandlerGene)
     {
         return(((HandlerGene)gene).inputs.ConvertAll(g => this[g]));
     }
     if (gene is InferenceGene)
     {
         return(((InferenceGene)gene).Dimensions.ConvertAll(x => this[x]));
     }
     return(new List <NodeGene>());
 }
 public NodeGene this[String name]
 {
     get
     {
         NodeGene gene = receptorGenes.FirstOrDefault(g => g.Name == name);
         if (gene != null)
         {
             return(gene);
         }
         gene = handlerGenes.FirstOrDefault(g => g.Name == name);
         if (gene != null)
         {
             return(gene);
         }
         gene = inferenceGenes.FirstOrDefault(g => g.Name == name);
         return(gene);
     }
 }
 /// <summary>
 /// 根据Id查找
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public NodeGene this[int id]
 {
     get
     {
         NodeGene gene = receptorGenes.FirstOrDefault(g => g.Id == id);
         if (gene != null)
         {
             return(gene);
         }
         gene = handlerGenes.FirstOrDefault(g => g.Id == id);
         if (gene != null)
         {
             return(gene);
         }
         gene = inferenceGenes.FirstOrDefault(g => g.Id == id);
         return(gene);
     }
 }
 public void replaceGene(int oldid, NodeGene gene)
 {
     remove(oldid);
     if (gene == null)
     {
         return;
     }
     if (gene is ReceptorGene)
     {
         this.receptorGenes.Add((ReceptorGene)gene);
     }
     else if (gene is HandlerGene)
     {
         this.handlerGenes.Add((HandlerGene)gene);
     }
     else if (gene is InferenceGene)
     {
         this.inferenceGenes.Add((InferenceGene)gene);
     }
 }
 /// <summary>
 /// 取得基因Id
 /// </summary>
 /// <param name="genome"></param>
 /// <param name="gene"></param>
 /// <returns></returns>
 public int getGeneId(NodeGene gene)
 {
     return(this.getGeneId(gene.Text));
 }
Exemple #10
0
 public bool isVaildGene(NodeGene gene)
 {
     return(this.validInferenceGenes.Exists(n => n.Id == gene.Id));
 }
Exemple #11
0
 /// <summary>
 /// 是否等价
 /// </summary>
 /// <param name="gene"></param>
 /// <returns></returns>
 public bool equiv(NodeGene g1, NodeGene g2)
 {
     return(g1.Text == g2.Text);
 }