/// <summary> /// Takes the weights and the bias. /// Calculates the decision boundary for the perceptron. /// </summary> /// <returns></returns> private void getFunction() { float xIntercept = -weights[0] / weights[2]; float yIntercept = -weights[0] / weights[1]; float m = yIntercept / xIntercept; this.Solution = new Straightline(m, xIntercept); }
/// <summary> /// Creates the line which divides the points. /// TODO call the training method. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { try { //solves the point list to a 95% accuracy Solution = S.FindSolutionFor(modelGenerator.points, Debug); if (Debug) { Console.WriteLine(Solution.ToString()); } RedrawModel(modelGenerator.points, Solution, -1, -1, Color.Black); } catch (NoSolutionException noSolutionException) { MessageBox.Show(noSolutionException.Message); } }
private void RedrawModel(List <ModelPoint> model, Straightline decisionBoundary, int newPointX, int newPointy, Color newPointColor) { //draws the dataset on the canvas g = this.CreateGraphics(); g.Clear(Color.White); Pen p = new Pen(Color.Black); Pen blueP = new Pen(Color.Blue); Pen redP = new Pen(Color.Red); //draws the coordinate system g.DrawLine(p, 25, 700, 25, 0); g.DrawLine(p, 25, 700, 725, 700); //draws the points List <ModelPoint> pointList = modelGenerator.points; if (pointList.Count > 0) { for (int i = 0; i < pointList.Count; ++i) { if (pointList[i].Color == Color.Blue) { g.DrawEllipse(blueP, 25 + (pointList[i].X * 7), 700 - (pointList[i].Y * 7), 7, 7); } else { g.DrawEllipse(redP, 25 + (pointList[i].X * 7), 700 - (pointList[i].Y * 7), 7, 7); } } } //paints the straight line on the canvas if (decisionBoundary != null) { float[] line = new float[4]; for (int i = 0; i <= 100; ++i) { float yZero = decisionBoundary.F(i); if (yZero >= 0 && yZero <= 1) { line[0] = i; line[1] = yZero; for (int j = 0; j <= 100; ++j) { float yHundred = decisionBoundary.F(j); if (yHundred >= 100 && yHundred <= 101) { line[2] = j; line[3] = yHundred; break; } } break; } } Pen pen = new Pen(Color.Blue); Point a = new Point((int)((line[0] * 7) + 25), (int)((line[1] * 7) + 25)); //point for 0 <= f(x) <= 1 Point b = new Point((int)((line[2] * 7) + 25), (int)((line[3] * 7) + 25)); //point for 100 <= f(x) <= 101 if (Debug) { Console.WriteLine("Translated Points:\t" + a.ToString() + "\t" + b.ToString()); } g.DrawLine(pen, a, b); } if (newPointX >= 0 && newPointy >= 0) { SolidBrush brush = new SolidBrush(newPointColor); g.FillEllipse(brush, 25 + (newPointX * 7), 700 - (newPointy * 7), 7, 7); brush.Dispose(); } }