public override void PrintOnCanvas(Canvas canvas) { float height; int iHeight; if (values == null || values.Length != size.X) { values = new float[size.X]; } float step = 1 / (float)size.X; float tempVal; values = PiwotToolsLib.Data.Arrays.BuildArray(size.X, (x) => func.Invoke(x * step)); float maxval = 0; int maxvalid = 0; for (int x = size.X - 1; x >= 0; x--) { if ((tempVal = func.Invoke(x * step)) != values[x]) { values[x] = tempVal; } if (tempVal > maxval) { maxval = tempVal; maxvalid = x; } } float halfStep = step / 2; string forwardCol = ""; for (int x = size.X - 1; x >= 0; x--) { height = values[x] * size.Y; iHeight = (int)height; iHeight = Arit.Clamp(iHeight, size.Y); height -= iHeight; forwardCol = (x == maxvalid ? "FF0000" : "FFFFFF"); for (int y = 0; y < size.Y - iHeight; y++) { canvas.DrawOnCanvas(" ", forwardCol, "000000", position.X + x, position.Y + y); } if (height >= 0.5f && iHeight != size.Y) { canvas.DrawOnCanvas("▄", forwardCol, "000000", position.X + x, position.Y + size.Y - iHeight - 1); } else if (iHeight == 0 && height >= 0.1f) { canvas.DrawOnCanvas("_", forwardCol, "000000", position.X + x, position.Y + size.Y - 1); } for (int y = size.Y - iHeight; y < size.Y; y++) { canvas.DrawOnCanvas("█", forwardCol, "000000", position.X + x, position.Y + y); } } }
static Bitmap VectorToBitmap(Vector <double> v) { Bitmap b = new Bitmap(pWidth, pHeight); int xpos = 0; int ypos = 0; for (int i = 0; i < pTotal; i++) { xpos = i % pWidth; ypos = i / pWidth; b.SetPixel(xpos, ypos, Color.FromArgb(255, Arit.Clamp((int)(v[i] * 255), 0, 255), Arit.Clamp((int)(v[i + pTotal] * 255), 0, 255), Arit.Clamp((int)(v[i + pTotal * 2] * 255), 0, 255))); } return(b); }
public void AddCanvas(Canvas canvas, int x, int y, int fromX, int fromY, int fromWidth, int fromHeight) { Int2 otherSize = canvas.Size; if (x < 0) { x = 0; } int xStart = x; if (y < 0) { y = 0; } int otherX; int otherY = fromY; fromHeight = Arit.Clamp(fromHeight + fromY, 0, canvas.Size.Y); fromWidth = Arit.Clamp(fromWidth + fromX, 0, canvas.Size.X); while (y < Size.Y && otherY < fromHeight) { otherX = fromX; x = xStart; while (x < Size.X && otherX < fromWidth) { frameFrontColorMap[y, x] = canvas.canvasFrontColorMap[otherY, otherX]; frameBackColorMap[y, x] = canvas.canvasBackColorMap[otherY, otherX]; frameCharMap[y][x] = canvas.canvasCharMap[otherY][otherX]; otherX++; x++; } refreshMap[y, Size.X] = true; otherY++; y++; } canvasUpToDate = false; }
static void Main(string[] args) { // Renderer.FrameLenght = 30; displaySize = new Int2(100, 10); memory = new float[displaySize.X - 2]; for (int i = 0; i < memory.Length; i++) { memory[i] = 0; } memoryLong = new List <float>(); Renderer.WindowSize = new Int2(Arit.Clamp(Arit.Clamp(pWidth * 2, 12, int.MaxValue) + 9 + displaySize.X, 20, int.MaxValue), Arit.Clamp(pHeight + 4 + pHeight % 2, displaySize.Y * 3, int.MaxValue)); Renderer.AsyncMode = false; Renderer.WindowTitle = "FlagLearning"; string[] filePaths = Directory.GetFiles("flags"); Bitmap[] flagBitmaps = new Bitmap[filePaths.Length]; flagMatrixes = new Vector <double> [filePaths.Length]; PiwotDrawingLib.UI.Containers.PictureBox pb = new PiwotDrawingLib.UI.Containers.PictureBox(new Int2(1, 0), new Int2(pWidth + 2, pHeight / 2 + 2 + pHeight % 2), "OrgFlg", PiwotDrawingLib.Misc.Boxes.BoxType.round, new Bitmap(10, 10)); PiwotDrawingLib.UI.Containers.PictureBox pb2 = new PiwotDrawingLib.UI.Containers.PictureBox(new Int2(pWidth + 5, 0), new Int2(pWidth + 2, pHeight / 2 + 2 + pHeight % 2), "GenFlg", PiwotDrawingLib.Misc.Boxes.BoxType.round, new Bitmap(10, 10)); pb.SizeDifferenceHandling = PiwotDrawingLib.UI.Containers.Container.ContentHandling.CropContent; pb2.SizeDifferenceHandling = PiwotDrawingLib.UI.Containers.Container.ContentHandling.CropContent; pb.Draw(); pb2.Draw(); for (int i = 0; i < filePaths.Length; i++) { flagBitmaps[i] = PiwotToolsLib.PGraphics.Bitmaper.CutColorBits(PiwotToolsLib.PGraphics.Bitmaper.StreachToSize(new Bitmap(filePaths[i]), pWidth, pHeight), PiwotToolsLib.PGraphics.Coloring.ColorEncoding.Bit9); flagMatrixes[i] = Vector <double> .Build.Dense(pTotal *3, (x) => { int xpoint = (x % pTotal); int ypoint = xpoint / pWidth; xpoint = xpoint % pWidth; switch (x / pTotal) { case 0: return((double)flagBitmaps[i].GetPixel(xpoint, ypoint).R / 255); case 1: return((double)flagBitmaps[i].GetPixel(xpoint, ypoint).G / 255); default: return((double)flagBitmaps[i].GetPixel(xpoint, ypoint).B / 255); } }); } //PiwotBrainLib.BrainCore bc = new PiwotBrainLib.BrainCore(pTotal * 3, new int[5] { 128, 64, 32, 64, 128 }, pTotal * 3); PiwotBrainLib.BrainCore bc = new PiwotBrainLib.BrainCore("brain_mid_103.txt"); bc.StreachLayer(0, 2, 2, 12, 0.25); bc.StreachLayer(6, 2, 2, 12, 0.25); PiwotDrawingLib.UI.Containers.SimpleFunctionDisplay f = new PiwotDrawingLib.UI.Containers.SimpleFunctionDisplay( new Int2(Arit.Clamp(pb2.Position.X + pb2.Size.X + 1, 20, int.MaxValue), pb2.Position.Y), displaySize, "Moment Error", PiwotDrawingLib.Misc.Boxes.BoxType.doubled, ErrorFunc); PiwotDrawingLib.UI.Containers.SimpleFunctionDisplay f2 = new PiwotDrawingLib.UI.Containers.SimpleFunctionDisplay( f.Position + new Int2(0, f.Size.Y), displaySize, "Moment error zoom", PiwotDrawingLib.Misc.Boxes.BoxType.doubled, ErrorFunc2); PiwotDrawingLib.UI.Containers.SimpleFunctionDisplay f3 = new PiwotDrawingLib.UI.Containers.SimpleFunctionDisplay( f2.Position + new Int2(0, f2.Size.Y), displaySize, "History error", PiwotDrawingLib.Misc.Boxes.BoxType.doubled, ErrorFuncLong); PiwotDrawingLib.Misc.Boxes.DrawBox(PiwotDrawingLib.Misc.Boxes.BoxType.normal, 1, pHeight / 2 + 2, f.Position.X - 1, Renderer.WindowSize.Y - (pHeight / 2 + 2)); PiwotBrainLib.LearningBrain learningBrain = new PiwotBrainLib.LearningBrain(bc) { ExampleBlockSize = 1, DataExtractor = RandomData, Accuracy = 100000, ErrorMemoryLenght = 200, Momentum = 0.1, NeuronActivation = new PiwotBrainLib.SechActivation() }; Vector <double> ranfFlag; Stopwatch stopwatch = new Stopwatch(); uint lTime = 0, cTime = 0; int saved = 0; long times = 0; while (true) { stopwatch.Restart(); learningBrain.LearnBlocks(100); stopwatch.Stop(); lTime = (uint)stopwatch.ElapsedMilliseconds; ranfFlag = RandomFlag(); pb.Image = VectorToBitmap(ranfFlag); stopwatch.Restart(); pb2.Image = VectorToBitmap(learningBrain.Calculate(ranfFlag)); stopwatch.Stop(); cTime = (uint)stopwatch.ElapsedMilliseconds; pb.RefreshContent(); pb2.RefreshContent(); memory[memoryPoint] = (float)learningBrain.MeanSquaredError; if (memoryLong.Contains(0)) { System.Console.ReadKey(true); } memoryPoint++; if (memoryPoint >= displaySize.X - 2) { memoryPoint = 0; } GetMaxErr(); Renderer.Draw($"AvgErr: {learningBrain.MeanSquaredError.ToString("0.0000")} -", 2, pHeight / 2 + 3); Renderer.Draw($"MaxErr: {maxErr.ToString("0.0000")} -", 2, pHeight / 2 + 4); Renderer.Draw($"MinErr: {minErr.ToString("0.0000")} -", 2, pHeight / 2 + 5); Renderer.Draw($"Max-Min: {(maxErr - minErr).ToString("0.0000")} -", 2, pHeight / 2 + 6); Renderer.Draw($"HMaxErr: {maxErrLong.ToString("0.0000")} -", 2, pHeight / 2 + 7); Renderer.Draw($"Learned: {learningBrain.ExamplesDone} -", 2, pHeight / 2 + 8); Renderer.Draw($"Learn T: {lTime} -", 2, pHeight / 2 + 9); Renderer.Draw($"Calc T: {cTime} -", 2, pHeight / 2 + 10); Renderer.Draw($"Acc: {learningBrain.Accuracy} -", 2, pHeight / 2 + 11); Renderer.Draw($"Mom: {learningBrain.Momentum} -", 2, pHeight / 2 + 12); f.Draw(); f2.Draw(); f3.Draw(); f.RefreshContent(); f2.RefreshContent(); f3.RefreshContent(); Renderer.ForcePrint(); times++; if (times % 10 == 0) { memoryLong.Add((float)learningBrain.MeanSquaredError); } if (times > 100) { times = 0; learningBrain.SaveToFile("", $"brain_mid_{saved}"); saved++; } //Console.ReadKey(true); } }