private void AddBlock() { var key = new Random().Next(7); AbstractBlock b = new TBlock(0, 5); if (key == 1) { b = new SBlock(0, 5); } if (key == 2) { b = new ZBlock(0, 5); } if (key == 3) { b = new LBlock(0, 5); } if (key == 4) { b = new JBlock(0, 5); } if (key == 5) { b = new Square(0, 5); } if (key == 6) { b = new Straight(0, 5); } blockQueue.Enqueue(b); }
public static void P2() { double TempWeight; //5.0 0.5 0.2 -8.0 -0.5 -0.5 default GetWeights.Add(TWeight.AttackLine); GetWeights.Add(TWeight.AdjacentWall); GetWeights.Add(TWeight.Adjacent); GetWeights.Add(TWeight.BlockEmpty); GetWeights.Add(TWeight.EmptyBlock); GetWeights.Add(TWeight.MaxHeight); GetWeights.Add(TWeight.fourway); GetWeights.Add(TWeight.threeway); int i, j, k, ii, jj, kk, tc, t, top = 0, flag = 0, godown, sel, si, sj; List <Point> ls = new List <Point>(); List <data> Rank = new List <data>(); Random r = new Random(); //가중치가 없어서 잠시 1로 top = 30; int Gene = 1, tom; long cnt = 0, maxcnt = 0; double p; while (Gene <= MaxGene) { //입력받은 세대 수 보다 현재 세대가 작은 동안 //initialize maxcnt = 0; Rank.Clear(); StreamReader sr = new StreamReader(@"2P.txt"); for (i = 0; i < 30; i++) { SumUp[i] = 0; nl[i] = new List <double>(); l[i] = new List <double>(); } top = 0; while (sr.Peek() >= 0 && top < 30) { foreach (string s in sr.ReadLine().Split()) { try { // Console.WriteLine((double)Convert.ToDouble(s)); l[top].Add(Convert.ToDouble(s)); } catch (Exception e) { //Console.WriteLine(e); } } top++; } //파일 입력 sr.Close(); for (tc = 0; tc < top; tc++) //개체 수가 최대 개체 수 보다 작으 동안 { Console.Clear(); TMap.Initialize(); TMap.maxheight = 20; Console.ForegroundColor = ConsoleColor.Gray; Console.SetCursorPosition(0, 0); for (i = 1; i <= 12; i++) { Console.Write("■"); } if (show != 0) { TMap.Print(); //debug } Console.ForegroundColor = ConsoleColor.Gray; for (i = 1; i <= 12; i++) { Console.Write("■"); } TMap.maxheight = 0; Console.SetCursorPosition(2, 22); //Print //Console.WriteLine(Gene + "세대, " + tc + "번째 개체 : "); while (!TMap.isDied()) { Thread.Sleep(300); ////set set set TBlock.Read(); PlayOneBlock(tc); cnt--; cnt += TWeight.AttackLine() * 30; tom = TMap.cll(); if (show != 0 && tom > 0) { TMap.Print(); } TMap.maxheight -= tom; TMap.ColorSet(0); Console.SetCursorPosition(20, 22); Console.Write(cnt + " "); } cnt /= 30; //Thread.Sleep(203); //show if (show != 0) { Console.SetCursorPosition(12, 23); Console.Write("DIED"); } Rank.Add(new data(tc, cnt)); if (tc != 0) { SumUp[tc] += SumUp[tc - 1]; } SumUp[tc] += cnt; if (maxcnt < cnt) { maxcnt = cnt; } cnt = 0; } //Print Console.Write("\n 최고 기록 : " + maxcnt); Rank.Sort(delegate(data A, data B) //점수 내림차순으로 정렬 { if (A.num < B.num) { return(1); } else if (A.num > B.num) { return(-1); } return(0); }); tc = 0; //유전자 변이 ! //1. 가장 좋은 해 5개 교차 /*for (i = 0; i < 3; i++) * { * nl[tc++]= * }*/ for (i = 0; i < 5; i++) { for (k = 0; k < l[i].Count(); k++) { si = Rank[i].i; nl[tc].Add(l[si][k]); } tc++; } for (i = 0; i < 5; i++) { for (j = 0; j < i; j++) { for (k = 0; k < l[i].Count(); k++) { si = Rank[i].i; sj = Rank[j].i; p = (double)Rank[si].num / (Rank[si].num + Rank[sj].num); //랭크가 높은 것이 더 많은 영향 미침. nl[tc].Add(l[si][k] * p + l[sj][k] * (1 - p)); } tc++; } } //요소간 룰렛 휠 선택 (5개) //1. 룰렛 휠 선택 & 변이 (10개) for (i = 0; i < 15; i++) { t = r.Next(1, (int)SumUp[top - 1]); for (j = 0; j < top; j++) { if (t < SumUp[j]) { break; } } if (j == top) { sel = 1; } sel = j; sel = Rank[sel].i; /*if (SumUp[j]-t <= 100) //변이 , 처음에는 변이할 확률이 높고 점점 변이할 확률이 낮아지는 방식 * {*/ for (k = 0; k < l[sel].Count(); k++) { p = 0.5 + (r.NextDouble() + r.NextDouble() + r.NextDouble() + r.NextDouble()) / 4; //0.5~1.5배로 변이 nl[tc].Add(l[sel][k] * p); } tc++; continue; /*} * sel=j; * nl[tc++]=l[sel];*/ } Gene++; FileIO.FWrite(2); } return; }