public override DecisionNetDNA <T> crossover(DecisionNetDNA <T> p_crossover_object)
                {
                    DecisionNetDNA <T> crossovered = Clone();

                    crossovered.m_weights = MatrixCalc.crossover(m_weights, p_crossover_object.m_weights);
                    return(crossovered);
                }
 /// <summary> Set Perpective mode (true) or Othographic mode (false) </summary>
 public void ChangePerspectiveMode(bool on)
 {
     MatrixCalc.InPerspectiveMode = on;
     MatrixCalc.CalculateModelMatrix(PosCamera.LookAt, PosCamera.EyePosition, PosCamera.CameraRotation);
     MatrixCalc.CalculateProjectionMatrix();
     glwin.Invalidate();
 }
        /// <summary> Start the class. Pass the GL window control, and the initial lookat/cameradirection and zoom.
        /// Control if registration of mouse and keyboard UI is performed with GL window control
        /// </summary>
        public void Start(GLWindowControl win, Vector3 lookat, Vector3 cameradirdegrees, float zoomn, bool registermouseui = true, bool registerkeyui = true)
        {
            glwin       = win;
            win.Resize += glControl_Resize;
            win.Paint  += glControl_Paint;

            if (registermouseui)
            {
                win.MouseDown  += MouseDown;
                win.MouseUp    += MouseUp;
                win.MouseMove  += MouseMove;
                win.MouseWheel += MouseWheel;
            }

            if (registerkeyui)
            {
                win.KeyDown += KeyDown;
                win.KeyUp   += KeyUp;
            }

            MatrixCalc.ResizeViewPort(this, win.Size);               // inform matrix calc of window size

            PosCamera.SetPositionZoom(lookat, new Vector2(cameradirdegrees.X, cameradirdegrees.Y), zoomn, cameradirdegrees.Z);
            MatrixCalc.CalculateModelMatrix(PosCamera.LookAt, PosCamera.EyePosition, PosCamera.CameraRotation);
            MatrixCalc.CalculateProjectionMatrix();
        }
                public override DecisionNetDNA <T> mutate()
                {
                    DecisionNetDNA <T> mutated = Clone();

                    mutated.m_weights = MatrixCalc.elementwiseRandomMultiply(m_weights, m_mutation_multiplier);
                    mutated.m_weights = MatrixCalc.columnNormalize(mutated.m_weights);
                    return(mutated);
                }
 // from the window, a resize event. Must have the correct context, if multiple, set glwin.EnsureCurrentPaintResize
 private void glControl_Resize(object sender)
 {
     //System.Diagnostics.Debug.WriteLine("Controller3d Resize" + glwin.Size);
     MatrixCalc.ResizeViewPort(this, glwin.Size);
     MatrixCalc.CalculateModelMatrix(PosCamera.LookAt, PosCamera.EyePosition, PosCamera.CameraRotation); // non perspective viewport changes also can affect model matrix
     MatrixCalc.CalculateProjectionMatrix();
     glwin.Invalidate();
 }
                public void receiveLearnedMatrix(Matrix <float> p_matrix)
                {
                    if (!MatrixCalc.isSameSize(p_matrix, m_weights))
                    {
                        Debug.LogError("Learned matrix must be same size as non-learned matrix");
                    }

                    m_weights = MatrixCalc.shallowClone(p_matrix);
                }
        /// <summary>Recalc matrix if moved </summary>
        public bool RecalcMatrixIfMoved(float minmove = 0.01f, float mincamera = 1.0f)
        {
            bool moved = PosCamera.IsMoved(minmove, mincamera);

            if (moved)
            {
                MatrixCalc.CalculateModelMatrix(PosCamera.LookAt, PosCamera.EyePosition, PosCamera.CameraRotation);
            }

            return(moved);
        }
                public DecisionNet(DInput[] p_inputs, DOutput[] p_outputs, Matrix <float> p_weights)
                {
                    if (!MatrixCalc.isSize(p_weights, p_inputs.Length, p_outputs.Length))
                    {
                        Debug.LogError("Constructing DecisionNet with wrong sized matrix");
                    }

                    m_inputs  = p_inputs;
                    m_outputs = p_outputs;
                    m_weights = MatrixCalc.shallowClone(p_weights);
                }
                private void performOutputs(Matrix <float> p_output_values)
                {
                    if (!MatrixCalc.isSize(p_output_values, 1, m_outputs.Length))
                    {
                        Debug.LogError("Trying to perform outputs in Decision net with malformed output_values matrix");
                    }

                    for (int i = 0; i < m_outputs.Length; i++)
                    {
                        m_outputs[i](p_output_values[0, i]);
                    }
                }
        /// <summary>Recalc matrix if moved </summary>
        public bool RecalcMatrixIfMoved(double minmove = 0.01f, double mincamera = 1.0f)
        {
            bool moved = PosCamera.IsMoved(minmove, mincamera);

            if (moved)
            {
                //System.Diagnostics.Debug.WriteLine("Changed");
                MatrixCalc.CalculateModelMatrix(PosCamera.LookAt, PosCamera.EyePosition, PosCamera.CameraRotation);
            }

            return(moved);
        }
 /// <summary> Set position camera (lookat and eye) from this setting string</summary>
 public bool SetPositionCamera(string s)     // String holds pos/eye
 {
     if (PosCamera.SetPositionCamera(s))
     {
         MatrixCalc.CalculateModelMatrix(PosCamera.LookAt, PosCamera.EyePosition, PosCamera.CameraRotation);
         return(true);
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 12
0
                public NeuralNet(DInput[] p_inputs, DOutput[] p_outputs, Matrix <float>[] p_weights)
                {
                    m_inputs  = p_inputs;
                    m_outputs = p_outputs;

                    m_weights = new Matrix <float> [p_weights.Length];

                    for (int i = 0; i < p_weights.Length; i++)
                    {
                        m_weights[i] = MatrixCalc.shallowClone(p_weights[i]);
                    }
                }
Exemplo n.º 13
0
                public override NeuralNetDNA <T> crossover(NeuralNetDNA <T> p_crossover_object)
                {
                    //CAN optimize with another private function. CLone copies all weights then they get overriden
                    NeuralNetDNA <T> crossovered = Clone();

                    for (int i = 0; i < m_weights.Length; i++)
                    {
                        crossovered.m_weights[i] = MatrixCalc.crossover(m_weights[i], p_crossover_object.m_weights[i]);
                    }

                    return(crossovered);
                }
                public DecisionNetDNA(int p_id, DInputFactory <T>[] p_inputs, DOutputFactory <T>[] p_outputs, Matrix <float> p_weights, Range <float> p_mutation_multiplier)
                {
                    if (!MatrixCalc.isSize(p_weights, p_inputs.Length, p_outputs.Length))
                    {
                        Debug.LogError("DecisionNetDNA requires Matrix input size inputs by outputs");
                    }

                    m_id                  = p_id;
                    m_inputs              = ArrayCalc.shallowClone(p_inputs);
                    m_outputs             = ArrayCalc.shallowClone(p_outputs);
                    m_weights             = MatrixCalc.shallowClone(MatrixCalc.columnNormalize(p_weights));
                    m_mutation_multiplier = p_mutation_multiplier;
                }
Exemplo n.º 15
0
                public override NeuralNetDNA <T> mutate()
                {
                    NeuralNetDNA <T> mutated = Clone();

                    for (int i = 0; i < m_weights.Length; i++)
                    {
                        bool          test = BoolCalc.random();
                        Range <float> mut  = test ? m_mutation_multiplier : new Range <float>(-m_mutation_multiplier.Min, -m_mutation_multiplier.Max);
                        mutated.m_weights[i] = MatrixCalc.elementwiseRandomMultiply(m_weights[i], mut);
                    }

                    return(mutated);
                }
Exemplo n.º 16
0
                public NeuralNetDNA <T> Clone()
                {
                    NeuralNetDNA <T> clone = new NeuralNetDNA <T>(m_id, m_inputs, m_outputs, m_mutation_multiplier);

                    clone.m_weights = new Matrix <float> [m_weights.Length];

                    for (int i = 0; i < m_weights.Length; i++)
                    {
                        clone.m_weights[i] = MatrixCalc.shallowClone(m_weights[i]);
                    }

                    return(clone);
                }
Exemplo n.º 17
0
                public void brainAction()
                {
                    Matrix <float>      current_matrix = getInputValueMatrix() * m_weights[0];
                    DActivationFunction activator      = ActivationFactory.generateSigmoid(2, 2, true, false, false);

                    MatrixCalc.activate(activator, current_matrix);

                    for (int i = 1; i < m_weights.Length; i++)
                    {
                        current_matrix = current_matrix * m_weights[i];
                        MatrixCalc.activate(activator, current_matrix);
                    }

                    performOutputs(current_matrix);
                }
Exemplo n.º 18
0
    static void Main(string[] args)
    {
        const int N = 5;

        double[,] a = new double[N, N], b = new double[N, N], c = new double[N, N];
        MatrixCalc calc = new MatrixCalc(a, b, c);

        // Fill in some values into arrays
        for (int i = 0; i < N; i++)
        {
            a[i, i] = 1;
            b[i, i] = 1;
            if (i > 0)
            {
                b[i, 0] = -i;
                a[0, i] = i;
            }
        }
        calc.Multiply();
        // Debug.WriteLine("Result: {0}", calc.OK);
    }
 public override string ToString()
 {
     return("DECISION NET DNA:: ID: " + m_id + " - Inputs: " + m_inputs.Length + " - Outputs: " + m_outputs.Length + " - Weights: " + MatrixCalc.sum(m_weights) + " - Mutation Rate: " + m_mutation_multiplier.Min + " to " + m_mutation_multiplier.Max);
 }
Exemplo n.º 20
0
 void Start()
 {
     mCalc = gameObject.AddComponent <MatrixCalc>();
 }
 public override string ToString()
 {
     return("DECISION NET :: Inputs: " + m_inputs.Length + " - Outputs: " + m_outputs.Length + " - Weight Sum: " + MatrixCalc.sum(m_weights));
 }
 /// <summary> Recalculate matrix - only use if changed a fundamental in matrixcalc </summary>
 public void RecalcMatrices()
 {
     MatrixCalc.CalculateModelMatrix(PosCamera.LookAt, PosCamera.EyePosition, PosCamera.CameraRotation);
     MatrixCalc.CalculateProjectionMatrix();
 }
Exemplo n.º 23
0
        private void EllipseRegression(out float Xc, out float Yc, out float a, out float b, out float theta)
        {
            int   n        = nPoints; //Minimum 5
            float sum_x    = 0;
            float sum_y    = 0;
            float sum_xy   = 0;
            float sum_x2y  = 0;
            float sum_x3y  = 0;
            float sum_xy2  = 0;
            float sum_xy3  = 0;
            float sum_x2y2 = 0;
            float sum_x2   = 0;
            float sum_x3   = 0;
            float sum_x4   = 0;
            float sum_y2   = 0;
            float sum_y3   = 0;
            float sum_y4   = 0;

            for (int i = 0; i < n; i++)
            {
                sum_x    = sum_x + _CurvePoints[i].X;
                sum_y    = sum_y + _CurvePoints[i].Y;
                sum_xy   = sum_xy + _CurvePoints[i].X * _CurvePoints[i].Y;
                sum_x2y  = sum_x2y + (float)Math.Pow(_CurvePoints[i].X, 2) * _CurvePoints[i].Y;
                sum_x3y  = sum_x3y + (float)Math.Pow(_CurvePoints[i].X, 3) * _CurvePoints[i].Y;
                sum_xy2  = sum_xy2 + _CurvePoints[i].X * (float)Math.Pow(_CurvePoints[i].Y, 2);
                sum_xy3  = sum_xy3 + _CurvePoints[i].X * (float)Math.Pow(_CurvePoints[i].Y, 3);
                sum_x2y2 = sum_x2y2 + (float)Math.Pow(_CurvePoints[i].X, 2) * (float)Math.Pow(_CurvePoints[i].Y, 2);
                sum_x2   = sum_x2 + (float)Math.Pow(_CurvePoints[i].X, 2);
                sum_x3   = sum_x3 + (float)Math.Pow(_CurvePoints[i].X, 3);
                sum_x4   = sum_x4 + (float)Math.Pow(_CurvePoints[i].X, 4);
                sum_y2   = sum_y2 + (float)Math.Pow(_CurvePoints[i].Y, 2);
                sum_y3   = sum_y3 + (float)Math.Pow(_CurvePoints[i].Y, 3);
                sum_y4   = sum_y4 + (float)Math.Pow(_CurvePoints[i].Y, 4);
            }

            double[][] m = MatrixCalc.MatrixCreate(5, 5);

            m[0][0] = sum_y4; m[0][1] = sum_x2y2; m[0][2] = sum_xy3; m[0][3] = sum_y3; m[0][4] = sum_xy2;
            m[1][0] = sum_x2y2; m[1][1] = sum_x4; m[1][2] = sum_x3y; m[1][3] = sum_x2y; m[1][4] = sum_x3;
            m[2][0] = sum_xy3; m[2][1] = sum_x3y; m[2][2] = sum_x2y2; m[2][3] = sum_xy2; m[2][4] = sum_x2y;
            m[3][0] = sum_y3; m[3][1] = sum_x2y; m[3][2] = sum_xy2; m[3][3] = sum_y2; m[3][4] = sum_xy;
            m[4][0] = sum_xy2; m[4][1] = sum_x3; m[4][2] = sum_x2y; m[4][3] = sum_xy; m[4][4] = sum_x2;

            double[][] vars = MatrixCalc.MatrixCreate(5, 1);

            vars[0][0] = -sum_y2; //Cy2
            vars[1][0] = -sum_x2; //Ax2
            vars[2][0] = -sum_xy; //Bxy
            vars[3][0] = -sum_y;  //Ey
            vars[4][0] = -sum_x;  //Dx

            double[][] inv = MatrixCalc.MatrixInverse(m);

            double[][] m1 = MatrixCalc.MatrixProduct(inv, vars);

            //Ay2 + Bx2 + Cxy + Dy + Ex + 1 = 0

            float C = (float)m1[0][0];
            float A = (float)m1[1][0];
            float B = (float)m1[2][0];
            float E = (float)m1[3][0];
            float D = (float)m1[4][0];
            float F = 1;

            //Ax2 + Bxy + Cy2 + Dx + Ey + F = 0

            Xc = ((2 * C * D) - (B * E)) / ((float)Math.Pow(B, 2) - 4 * A * C);
            Yc = ((2 * A * E) - (B * D)) / ((float)Math.Pow(B, 2) - 4 * A * C);

            a = -(float)Math.Sqrt(2 * (A * (float)Math.Pow(E, 2) + C * (float)Math.Pow(D, 2) - B * D * E + ((float)Math.Pow(B, 2) - 4 * A * C) * F) * (A + C + (float)Math.Sqrt((float)Math.Pow((A - C), 2) + (float)Math.Pow(B, 2)))) / ((float)Math.Pow(B, 2) - 4 * A * C);
            b = -(float)Math.Sqrt(2 * (A * (float)Math.Pow(E, 2) + C * (float)Math.Pow(D, 2) - B * D * E + ((float)Math.Pow(B, 2) - 4 * A * C) * F) * (A + C - (float)Math.Sqrt((float)Math.Pow((A - C), 2) + (float)Math.Pow(B, 2)))) / ((float)Math.Pow(B, 2) - 4 * A * C);

            if (a < b)
            {
                float c = a;
                a = b;
                b = c;
            }

            if (B == 0)
            {
                if (A < C)
                {
                    theta = 0;
                }
                else
                {
                    theta = (float)Math.PI / 2;
                }
            }
            else
            {
                theta = (float)(Math.Atan((C - A - (float)Math.Sqrt((float)Math.Pow((A - C), 2) + (float)Math.Pow(B, 2)))) / B);
            }
            theta = theta * 180 / (float)Math.PI;
        }