Neural network perceptron simple example c#


Back to learning
Created: 25/05/2018
This is a very basic example how we can create a perceptron which will behave as an OR operator

0 or 0 = 0

1 or 0 = 1
0 or 1 = 1

1 or 1 = 1


Basically our perceptron will "fire" when:
(x1*w1 + x2*w2) > threashold



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace ai
{
    class Program
    {
        static void Main(string[] args)
        {
            // Declaration and initial values (random)
            Perceptron n = new Perceptron();
            n.w1 = 0.2;
            n.w2 = 0.2;
            n.threshold = 0.5;
            n.learnFactor = 0.2;

            // Perceptron training
            int iter = 0;
            while (!IsOr(n))
            {
                if (iter == 0)
                {
                    n.x1 = 0;
                    n.x2 = 0;
                    n.y = 0;

                    if (n.IsThreshold())
                        n.Adjust();
                }

                if (iter == 1)
                {
                    n.x1 = 1;
                    n.x2 = 0;
                    n.y = 1;

                    if (!n.IsThreshold())
                        n.Adjust();
                }

                if (iter == 2)
                {
                    n.x1 = 0;
                    n.x2 = 1;
                    n.y = 1;

                    if (!n.IsThreshold())
                        n.Adjust();
                }

                if (iter == 3)
                {
                    n.x1 = 1;
                    n.x2 = 1;
                    n.y = 1;

                    if (!n.IsThreshold())
                        n.Adjust();
                }

                if (iter == 3)
                    iter = 0;
                else
                    iter++;
            }

            // Testing
            int seguir = 1;
            while (seguir == 1)
            {
                Console.WriteLine("Type x1 value (0 or 1):");
                n.x1 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Type x2 value (0 or 1):");
                n.x2 = Convert.ToInt32(Console.ReadLine());

                if (n.IsThreshold())
                {
                    Console.WriteLine("Result: 1");
                }
                else
                {
                    Console.WriteLine("Resultado: 0");
                }

                Console.WriteLine("Do you want to continue? yes:1, no: <> 1");
                seguir = Convert.ToInt32(Console.ReadLine());
            }
        }

        /// <summary>
        /// This function verifies if our perceptron is ready to work as OR operator
        /// </summary>
        /// <param name="per"></param>
        /// <returns></returns>
        public static bool IsOr(Perceptron per)
        {
            bool _00 = false;
            bool _10 = false;
            bool _01 = false;
            bool _11 = false;

            per.x1 = 0;
            per.x2 = 0;
            if (!per.IsThreshold())
            {
                _00 = true;
            }

            per.x1 = 1;
            per.x2 = 0;
            if (per.IsThreshold())
            {
                _10 = true;
            }

            per.x1 = 0;
            per.x2 = 1;
            if (per.IsThreshold())
            {
                _01 = true;
            }

            per.x1 = 1;
            per.x2 = 1;
            if (per.IsThreshold())
            {
                _11 = true;
            }
            Console.WriteLine("Data: ");
            Console.WriteLine("w1: " + per.w1.ToString());
            Console.WriteLine("w2: " + per.w2.ToString());
            Console.WriteLine("threshold: " + per.threshold.ToString());
            // to be valid, must be:
            /*
             * 00=0
             * 01=1
             * 10=1
             * 11=1
             */
            return (_00 && _10 && _01 && _11) ? true : false;
        }
    }

    public class Perceptron
    {
        public double x1 { get; set; }
        public double x2 { get; set; }
        public double w1 { get; set; }
        public double w2 { get; set; }
        public double y { get; set; }
        public double threshold { get; set; }
        private double sum
        {
            get
            {
                return (x1 * w1) + (x2 * w2);
            }
        }
        public double learnFactor { get; set; }

        public void Adjust()
        {
            double e = y - sum; // expected - obtained
            threshold += -(learnFactor * e);
            w1 += learnFactor * e * x1;
            w2 += learnFactor * e * x2;
        }

        public bool IsThreshold()
        {
            if (sum > threshold)
                return true;
            else
                return false;
        }
    }
}