public List <GeneSeq> ImportSeqsFromFile() { // System.IO.StreamReader readFile = // new System.IO.StreamReader("Strategy_Pool/info"); // int fileIndex = Int32.Parse (readFile.ReadLine ()); // readFile.Close (); // fileIndex -= 1; // readFile = new System.IO.StreamReader("Strategy_Pool/strategy_" + fileIndex); // string stream = readFile.ReadToEnd(); // readFile.Close (); System.IO.StreamReader readFile = new System.IO.StreamReader("Strategy_Pool/strategy"); string stream = readFile.ReadToEnd(); readFile.Close(); List <GeneSeq> pool = new List <GeneSeq> (); string[] seqStreams = stream.Split(';'); foreach (string part in seqStreams) { if (part.Trim() == "") { continue; } GeneSeq newSeq = new GeneSeq(); string newPart = part + ";\n"; // Debug.Log ("Gene STring : " + newPart); pool.Add(newSeq.Deserialize(newPart)); } return(pool); }
public GeneSeq(GeneSeq seq) { id = DateTime.Now.ToString("yyyyMMddHHmmssffff"); score = 0; this.seqList = seq.GetReverseNodeList(); // this.id = seq.id; // this.parentIds = seq.parentIds; }
public List <GeneSeq> InitSeqsRandom() { List <GeneSeq> pool = new List <GeneSeq> (); for (int i = 0; i < 2; i++) { GeneSeq newSeq = new GeneSeq(); newSeq.InitRandom(posList, towerSize, posList.Count * 3); pool.Add(newSeq); // Debug.Log ("init seq: " + posList.Count + " "+towerSize); } return(pool); }
// Dictionary<int[], Tower> map = new Dictionary<int[], Tower>(); public List <GeneSeq> CrossOverRes(GeneSeq seq1, GeneSeq seq2, int number) { List <GeneSeq> listGeneSeq = new List <GeneSeq> (); int length = (seq1.Size() > seq2.Size() ? seq2.Size() : seq1.Size()); System.Random rnd = new System.Random(); for (int i = 0; i < number; i++) { int index = rnd.Next(1, length); List <GeneNode> nodes = new List <GeneNode> (); int j = 0; for (; j <= index; j++) { nodes.Add(seq1.GetNodeByIndex(j)); } for (; j < seq2.Size(); j++) { nodes.Add(seq2.GetNodeByIndex(j)); } GeneSeq newSeq = new GeneSeq(nodes); listGeneSeq.Add(newSeq); } return(listGeneSeq); }
public List <GeneSeq> Mutate(GeneSeq seq, int number) { List <GeneSeq> listGeneSeq = new List <GeneSeq> (); int length = seq.Size(); System.Random rnd = new System.Random(); int[] pos = posList[rnd.Next(0, posList.Count)]; int towerIndex = rnd.Next(0, towerSize); int index = rnd.Next(0, length); for (int i = 0; i < number; i++) { GeneSeq newSeq = new GeneSeq(seq); newSeq.SetNode(index, pos, towerIndex); listGeneSeq.Add(newSeq); newSeq = new GeneSeq(seq); newSeq.InsertNodeAfterIndex(index, new GeneNode(pos, towerIndex)); listGeneSeq.Add(newSeq); newSeq = new GeneSeq(seq); newSeq.RemoveNodeAt(index); listGeneSeq.Add(newSeq); } return(listGeneSeq); }
public IEnumerator Run_EA_Loop() { // Debug.LogWarning ("In loop !!!!!!!!!!"); FilterGoodStrategy(); List <GeneSeq> geneSeqs = ImportSeqsFromFile(); double[] scores = new double[geneSeqs.Count]; List <GeneSeq> resPool = new List <GeneSeq> (); // scoreSize = geneSeqs.Count; // scores = new double[scoreSize]; for (int i = 0; i < geneSeqs.Count; i++) { curSeq = geneSeqs [i]; // Debug.Log ("!!!!!!!!!!!!!" + curSeq.Size()); StartCoroutine(ExecuteGeneSeq()); while (!doesFinishOneSeq) { yield return(new WaitForSeconds(60)); } doesFinishOneSeq = false; scores [i] = cur_Score; // Debug.LogWarning ("【Score parent "+i+"】: " + cur_Score); // Debug.LogError ("seqCount " + geneSeqs[0].Serialize() + " +++++\n"+geneSeqs[1].Serialize()); } // Debug.LogError ("seqCount " + geneSeqs[0].Serialize() + " ++++++\n"+geneSeqs[1].Serialize()); for (int i = 0; i < geneSeqs.Count; i++) { for (int j = i + 1; j < geneSeqs.Count; j++) { List <GeneSeq> children = CrossOverRes(geneSeqs[i], geneSeqs[j], 5); foreach (GeneSeq child in children) { curSeq = child; StartCoroutine(ExecuteGeneSeq()); while (!doesFinishOneSeq) { yield return(new WaitForSeconds(60)); } doesFinishOneSeq = false; // Debug.LogError ("【Score cross "+i+" " +j+"】: " + cur_Score); if (cur_Score > scores [i] && cur_Score > scores [j]) { // resPool.Add (child); AppendOneSeqToFile(curSeq); ScoreRecord(cur_Score); FilterGoodStrategy(); } } } } for (int i = 0; i < geneSeqs.Count; i++) { List <GeneSeq> children = Mutate(geneSeqs[i], 5); foreach (GeneSeq child in children) { curSeq = child; StartCoroutine(ExecuteGeneSeq()); while (!doesFinishOneSeq) { yield return(new WaitForSeconds(60)); } doesFinishOneSeq = false; // Debug.LogError ("【Score mutate "+i+"】: " + cur_Score); if (cur_Score > scores [i]) { // resPool.Add (child); AppendOneSeqToFile(curSeq); ScoreRecord(cur_Score); FilterGoodStrategy(); } } } // ExportSeqToFile(resPool); }
public void AppendOneSeqToFile(GeneSeq seq) { System.IO.StreamWriter writeFile = new System.IO.StreamWriter("Strategy_Pool/strategy", append: true); writeFile.WriteLine(seq.Serialize()); writeFile.Close(); }