public static HandlerGene parse(NWSEGenome genome, String s) { int t1 = s.IndexOf("info"); int t2 = s.IndexOf(":", t1 + 1); int t3 = s.IndexOf(";", t2 + 1); String info = s.Substring(t2 + 1, t3 - t2 - 1); //解析param部分 t1 = s.IndexOf("function"); t2 = s.IndexOf("=", t1 + 1); t3 = s.IndexOf(";", t2 + 1); String function = s.Substring(t2 + 1, t3 - t2 - 1).Trim(); t1 = s.IndexOf("inputs"); t2 = s.IndexOf("=", t1 + 1); t3 = s.IndexOf(";", t2 + 1); String inputstr = s.Substring(t2 + 1, t3 - t2 - 1).Trim(); List <int> inputids = Utility.parse(inputstr).ConvertAll(v => (int)v); //解析info HandlerGene gene = new HandlerGene(genome, function, inputids, null); gene.parseInfo(info); //解析参数 t1 = s.IndexOf("param"); t2 = s.IndexOf("=", t1 + 1); String paramtext = s.Substring(t2 + 1, s.Length - t2 - 1).Trim(); gene.param = Utility.parse(paramtext); return(gene); }
public NodeGene remove(int id) { ReceptorGene g1 = this.receptorGenes.FirstOrDefault(g => g.Id == id); if (g1 != null) { this.receptorGenes.Remove(g1); return(g1); } HandlerGene g2 = this.handlerGenes.FirstOrDefault(g => g.Id == id); if (g2 != null) { this.handlerGenes.Remove(g2); return(g2); } InferenceGene g3 = this.inferenceGenes.FirstOrDefault(g => g.Id == id); if (g3 != null) { this.inferenceGenes.Remove(g3); return(g3); } return(null); }
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); }
/// <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); }