public override long Evaluate(Genome g, DataDictionary dd) { Data2048Dictionary d2d = dd as Data2048Dictionary; long best = long.MinValue; long sum = 0; dd.CreateScore(g.GenomeId, Execute); dd.CreateLifetime(g.GenomeId, Execute); int lifetime; for (int i = 0; i < Execute; ++i) { LinkedList <CreatedCells> cellLog = new LinkedList <CreatedCells>(); Initialize(cellLog); lifetime = 0; while (!Gameover) { double[] output = g.EvaluateNetwork(GetInputs()); int[] order = new int[4] { 0, 1, 2, 3 }; Array.Sort(output, order); int index = 3; while (!Shift((Direction)order[index])) { if (++FailCount >= FailLimit) { Gameover = true; break; } --index; } if (Gameover) { break; } CreateRandom(SizeEmpty(), cellLog); SetGameover(); ++lifetime; } sum += GetFitness(); if (GetFitness() > best) { best = GetFitness(); d2d.AddCells(g.GenomeId, cellLog); } dd.AddScore(g.GenomeId, Score, i); } return(sum / Execute); }
public override void Display(Genome g, DataDictionary dd) { base.Display(g, dd); Data2048Dictionary d2d = dd as Data2048Dictionary; Cells = new int[16]; Score = 0; LogScore = 0; FailCount = 0; Gameover = false; LinkedList <CreatedCells> cc = d2d.getCells(g.GenomeId); LinkedListNode <CreatedCells> cnode = cc.First; Cells[cnode.Value.index] = cnode.Value.isTwo ? 1 : 2; cnode = cnode.Next; Cells[cnode.Value.index] = cnode.Value.isTwo ? 1 : 2; while (!Gameover) { double[] output = g.EvaluateNetwork(GetInputs()); int[] order = new int[4] { 0, 1, 2, 3 }; Array.Sort(output, order); int index = 3; while (!Shift((Direction)order[index])) { if (++FailCount >= FailLimit) { Gameover = true; break; } --index; } if (Gameover) { break; } cnode = cnode.Next; Cells[cnode.Value.index] = cnode.Value.isTwo ? 1 : 2; SetGameover(); PrevDirection = (Direction)order[index]; Drawer(); Thread.Sleep(DisplayDelay); } }