Пример #1
0
        public void Train(FeedForwardTrainingSet trainingSet, int iterations)
        {
            //Initialize previous output array for each neuron's previous output
            float[][] previousOutput = new float[Weights.Length][];
            for (int i = 0; i < Weights.Length; i++)
            {
                previousOutput[i] = new float[Weights[i].Length];
            }

            for (int iteration = 0; iteration < iterations; iteration++)
            {
                for (int trainingIndex = 0; trainingIndex < trainingSet.TrainingData.Length; trainingIndex++)
                {
                    float[]   input  = trainingSet.TrainingData[trainingIndex];
                    float[]   target = trainingSet.TargetOutput[trainingIndex];
                    float[][] output = Calculate(input);

                    //Get error of output layer (output[output.Length - 1] == output layer neurons output)
                    float[] outputLayerError = new float[output[output.Length - 1].Length];
                    outputLayerError = NetMath.GetVectorError(target, output[output.Length - 1]);

                    //Compute network total error
                    float totalError = 0;
                    NetMath.VectorSumFast(outputLayerError, totalError);

                    AdjustWeights(input, BackpropagateNeuronError(output, input, target), output);
                }
            }
        }
Пример #2
0
 static public void Warn(string format, params object[] args)
 {
     if (NetMath.IsSet(enabled, WARN))
     {
         Write(WARN, String.Format(format, args));
     }
 }
Пример #3
0
 static public void Info(string format, params object[] args)
 {
     if (NetMath.IsSet(enabled, INFO))
     {
         Write(INFO, String.Format(format, args));
     }
 }
Пример #4
0
        private void CreateAccountRequest(string username, string password, NetConnection connection)
        {
            if (!InputValidator.IsValidEmail(username) || !InputValidator.IsValidPassword(password))
            {
                return;
            }
            if (!InputValidator.LowercaseOnly(username))
            {
                return;
            }
            foreach (Account account in accounts)
            {
                if (account.Username != username)
                {
                    continue;
                }
                SendEmailDuplicate(connection);
                return;
            }
            ulong randId = NetMath.RandomUlong();
            var   newAcc = new Account(randId, username, password);

            accounts.Add(newAcc);
            SendLoginSuccess(newAcc, connection);
        }
Пример #5
0
        protected override bool Intersects(SphereCollider collider)
        {
            //the sphere's center is a vector.
            //transform it from world coord-system to box's local coord-system

            Vector3 SphereCenter = Vector3.Transform(collider.Center - m_center,
                                                     Quaternion.Inverse(m_rotate));

            //you can think it is a AABB Box.

            //x-axis
            if (NetMath.Abs(SphereCenter.X) > collider.Radius + Radius.X)
            {
                return(false);
            }
            //y-axis
            if (NetMath.Abs(SphereCenter.Y) > collider.Radius + Radius.Y)
            {
                return(false);
            }
            //z-axis
            if (NetMath.Abs(SphereCenter.Z) > collider.Radius + Radius.Z)
            {
                return(false);
            }

            return(true);
        }
Пример #6
0
        public override void Transform(Matrix4x4 matrix)
        {
            Matrix4x4.Decompose(matrix, out Vector3 scale, out Quaternion rotation, out Vector3 translation);

            m_radius = m_radius * NetMath.Max(scale.X, NetMath.Max(scale.Y, scale.Z));
            m_center = m_center + translation;
        }
Пример #7
0
        private ulong CreateSession(Account account, NetConnection connection)
        {
            ulong sessionToken = NetMath.RandomUlong();

            sessions.Add(sessionToken, account);
            sessionLookup.Add(connection.Endpoint, sessionToken);
            return(sessionToken);
        }
Пример #8
0
        static public void Debug(string format, params object[] args)
        {
#if DEBUG
            if (NetMath.IsSet(enabled, DEBUG))
            {
                Write(DEBUG, String.Format(format, args));
            }
#endif
        }
Пример #9
0
        static public void Trace(string format, params object[] args)
        {
#if TRACE
            if (NetMath.IsSet(enabled, TRACE))
            {
                Write(TRACE, String.Format(format, args));
            }
#endif
        }
Пример #10
0
        /// <summary>
        /// This is the old way of updating a ping based on the original logic, this has been deprecated
        /// </summary>
        /// <param name="recvTime">Receive Tie</param>
        /// <param name="sendTime">Send Time</param>
        /// <param name="ackTime">Acknowledgement time / delay</param>
        public void UpdatePing(uint recvTime, uint sendTime, uint ackTime)
        {
            uint aliased = recvTime - sendTime;

            aliasedRtt = (aliasedRtt * 0.9f) + ((float)aliased / 1000f * 0.1f);

            uint network = aliased - NetMath.Clamp(ackTime, 0, aliased);

            networkRtt = (networkRtt * 0.9f) + ((float)network / 1000f * 0.1f);
        }
Пример #11
0
        public static SphereCollider Transform(SphereCollider collider, Matrix4x4 matrix)
        {
            SphereCollider result = new SphereCollider();

            Matrix4x4.Decompose(matrix, out Vector3 scale, out Quaternion rotation, out Vector3 translation);

            result.Radius = collider.m_radius * NetMath.Max(scale.X, NetMath.Max(scale.Y, scale.Z));
            result.Center = collider.m_center + translation;

            return(result);
        }
Пример #12
0
 public static Matrix4x4 Abs(Matrix4x4 matrix)
 {
     return(new Matrix4x4()
     {
         M11 = NetMath.Abs(matrix.M11), M12 = NetMath.Abs(matrix.M12),
         M13 = NetMath.Abs(matrix.M13), M14 = NetMath.Abs(matrix.M14),
         M21 = NetMath.Abs(matrix.M21), M22 = NetMath.Abs(matrix.M22),
         M23 = NetMath.Abs(matrix.M23), M24 = NetMath.Abs(matrix.M24),
         M31 = NetMath.Abs(matrix.M31), M32 = NetMath.Abs(matrix.M32),
         M33 = NetMath.Abs(matrix.M33), M34 = NetMath.Abs(matrix.M34),
         M41 = NetMath.Abs(matrix.M41), M42 = NetMath.Abs(matrix.M42),
         M43 = NetMath.Abs(matrix.M43), M44 = NetMath.Abs(matrix.M44)
     });
 }
Пример #13
0
        private bool Intersects(SphereCollider sphere, out float distance)
        {
            //from Real-Time Rendering
            //The ray is in world spcae
            distance = 0;

            Vector3 l = sphere.Center - m_position;
            Vector3 d = Vector3.Normalize(m_direction);

            float s   = Vector3.Dot(l, d);
            float l_2 = Vector3.Dot(l, l);
            float r_2 = sphere.Radius * sphere.Radius;

            if (s < 0 && l_2 > r_2)
            {
                return(false);
            }

            float m_2 = l_2 - s * s;

            if (m_2 > r_2)
            {
                return(false);
            }

            float q = (float)NetMath.Sqrt(r_2 - m_2);

            if (l_2 > r_2)
            {
                distance = s - q;
            }
            else
            {
                distance = s + q;
            }

            return(true);
        }
Пример #14
0
        public RecvBufferAddResult TryEnqueue(EventReliable value)
        {
            int distance = NetMath.SequenceDistance(value.Sequence, sequenceNext, sequenceShift);
            int index    = (tail + distance) & mask;

            if (distance <= -nodes.Length || distance >= nodes.Length)
            {
                return(RecvBufferAddResult.OutOfBounds);
            }

            if (distance < 0)
            {
                return(RecvBufferAddResult.Old);
            }

            if (nodes[index].Received)
            {
                return(RecvBufferAddResult.AlreadyExists);
            }

            nodes[index].Received = true;
            nodes[index].Value    = value;
            return(RecvBufferAddResult.Added);
        }
Пример #15
0
        private bool Intersects(BoxCollider box, out float distance)
        {
            //from Real-Time Rendering
            //The ray is in world spcae

            distance = 0;

            Vector3 d = Vector3.Normalize(m_direction);
            Vector3 p = box.Center - m_position;

            Matrix4x4 m = Matrix4x4.CreateFromQuaternion(box.Rotate);

            Vector3[] axis = new Vector3[3];

            axis[0] = new Vector3(m.M11, m.M12, m.M13);
            axis[1] = new Vector3(m.M21, m.M22, m.M23);
            axis[2] = new Vector3(m.M31, m.M32, m.M33);

            float[] h = new float[3] {
                box.Radius.X, box.Radius.Y, box.Radius.Z
            };

            float tmin = -float.MaxValue;
            float tmax = float.MaxValue;

            for (int i = 0; i < 3; i++)
            {
                Vector3 item   = axis[i];
                float   radius = h[i];

                float e = Vector3.Dot(item, p);
                float f = Vector3.Dot(item, d);

                float t1 = (e + radius) / f;
                float t2 = (e - radius) / f;

                if (t1 > t2)
                {
                    float temp = t1; t1 = t2; t2 = temp;
                }

                tmin = NetMath.Max(tmin, t1);
                tmax = NetMath.Min(tmax, t2);

                if (tmin > tmax)
                {
                    return(false);
                }
                if (tmax < 0)
                {
                    return(false);
                }
            }

            if (tmin > 0)
            {
                distance = tmin;
            }
            else
            {
                distance = tmax;
            }

            return(true);
        }
Пример #16
0
        protected override bool Intersects(BoxCollider collider)
        {
            // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
            // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
            // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
            // PARTICULAR PURPOSE.
            //
            // Copyright (c) Microsoft Corporation. All rights reserved.

            Quaternion quaternion = m_rotate * Quaternion.Conjugate(collider.m_rotate);
            Matrix4x4  R          = Matrix4x4.CreateFromQuaternion(quaternion);
            Matrix4x4  AR         = Math.TMatrix.Abs(R);

            Vector3 t = Vector3.Transform(collider.m_center - m_center,
                                          Quaternion.Inverse(m_rotate));

            float d, d_A, d_B;

            // l = a(u) = (1, 0, 0)
            // t dot l = t.x
            // d(A) = h(A).x
            // d(B) = h(B) dot abs(r00, r01, r02)
            d   = t.X;
            d_A = m_radius.X;
            d_B = Vector3.Dot(collider.m_radius, new Vector3(AR.M11, AR.M12, AR.M13));
            if (NetMath.Abs(d) > NetMath.Abs(d_A) + NetMath.Abs(d_B))
            {
                return(false);
            }

            // l = a(v) = (0, 1, 0)
            // t dot l = t.y
            // d(A) = h(A).y
            // d(B) = h(B) dot abs(r10, r11, r12)
            d   = t.Y;
            d_A = m_radius.Y;
            d_B = Vector3.Dot(collider.m_radius, new Vector3(AR.M21, AR.M22, AR.M23));
            if (NetMath.Abs(d) > NetMath.Abs(d_A) + NetMath.Abs(d_B))
            {
                return(false);
            }

            // l = a(w) = (0, 0, 1)
            // t dot l = t.z
            // d(A) = h(A).z
            // d(B) = h(B) dot abs(r20, r21, r22)
            d   = t.Z;
            d_A = m_radius.Z;
            d_B = Vector3.Dot(collider.m_radius, new Vector3(AR.M31, AR.M32, AR.M33));
            if (NetMath.Abs(d) > NetMath.Abs(d_A) + NetMath.Abs(d_B))
            {
                return(false);
            }

            // l = b(u) = (r00, r10, r20)
            // d(A) = h(A) dot abs(r00, r10, r20)
            // d(B) = h(B).x
            d   = Vector3.Dot(t, new Vector3(R.M11, R.M21, R.M31));
            d_A = Vector3.Dot(m_radius, new Vector3(AR.M11, AR.M21, AR.M31));
            d_B = collider.m_radius.X;
            if (NetMath.Abs(d) > NetMath.Abs(d_A) + NetMath.Abs(d_B))
            {
                return(false);
            }

            // l = b(v) = (r01, r11, r21)
            // d(A) = h(A) dot abs(r01, r11, r21)
            // d(B) = h(B).y
            d   = Vector3.Dot(t, new Vector3(R.M12, R.M22, R.M32));
            d_A = Vector3.Dot(m_radius, new Vector3(AR.M12, AR.M22, AR.M32));
            d_B = collider.m_radius.Y;
            if (NetMath.Abs(d) > NetMath.Abs(d_A) + NetMath.Abs(d_B))
            {
                return(false);
            }

            // l = b(w) = (r02, r12, r22)
            // d(A) = h(A) dot abs(r02, r12, r22)
            // d(B) = h(B).z
            d   = Vector3.Dot(t, new Vector3(R.M13, R.M23, R.M33));
            d_A = Vector3.Dot(m_radius, new Vector3(AR.M13, AR.M23, AR.M33));
            d_B = collider.m_radius.Z;
            if (NetMath.Abs(d) > NetMath.Abs(d_A) + NetMath.Abs(d_B))
            {
                return(false);
            }

            // l = a(u) x b(u) = (0, -r20, r10)
            // d(A) = h(A) dot abs(0, r20, r10)
            // d(B) = h(B) dot abs(0, r02, r01)
            d   = Vector3.Dot(t, new Vector3(0, -R.M31, R.M21));
            d_A = Vector3.Dot(m_radius, new Vector3(0, AR.M31, AR.M21));
            d_B = Vector3.Dot(collider.m_radius, new Vector3(0, AR.M13, AR.M12));
            if (NetMath.Abs(d) > NetMath.Abs(d_A) + NetMath.Abs(d_B))
            {
                return(false);
            }

            // l = a(u) x b(v) = (0, -r21, r11)
            // d(A) = h(A) dot abs(0, r21, r11)
            // d(B) = h(B) dot abs(r02, 0, r00)
            d   = Vector3.Dot(t, new Vector3(0, -R.M32, R.M22));
            d_A = Vector3.Dot(m_radius, new Vector3(0, AR.M32, AR.M22));
            d_B = Vector3.Dot(collider.m_radius, new Vector3(AR.M13, 0, AR.M11));
            if (NetMath.Abs(d) > NetMath.Abs(d_A) + NetMath.Abs(d_B))
            {
                return(false);
            }

            // l = a(u) x b(w) = (0, -r22, r12)
            // d(A) = h(A) dot abs(0, r22, r12)
            // d(B) = h(B) dot abs(r01, r00, 0)
            d   = Vector3.Dot(t, new Vector3(0, -R.M33, -R.M23));
            d_A = Vector3.Dot(m_radius, new Vector3(0, AR.M33, AR.M23));
            d_B = Vector3.Dot(collider.m_radius, new Vector3(AR.M12, AR.M11, 0));
            if (NetMath.Abs(d) > NetMath.Abs(d_A) + NetMath.Abs(d_B))
            {
                return(false);
            }

            // l = a(v) x b(u) = (r20, 0, -r00)
            // d(A) = h(A) dot abs(r20, 0, r00)
            // d(B) = h(B) dot abs(0, r12, r11)
            d   = Vector3.Dot(t, new Vector3(R.M31, 0, -R.M11));
            d_A = Vector3.Dot(m_radius, new Vector3(AR.M31, 0, AR.M11));
            d_B = Vector3.Dot(collider.m_radius, new Vector3(0, AR.M23, AR.M22));
            if (NetMath.Abs(d) > NetMath.Abs(d_A) + NetMath.Abs(d_B))
            {
                return(false);
            }

            // l = a(v) x b(v) = (r21, 0, -r01)
            // d(A) = h(A) dot abs(r21, 0, r01)
            // d(B) = h(B) dot abs(r12, 0, r10)
            d   = Vector3.Dot(t, new Vector3(R.M32, 0, -R.M12));
            d_A = Vector3.Dot(m_radius, new Vector3(AR.M32, 0, AR.M12));
            d_B = Vector3.Dot(collider.m_radius, new Vector3(AR.M23, 0, AR.M21));
            if (NetMath.Abs(d) > NetMath.Abs(d_A) + NetMath.Abs(d_B))
            {
                return(false);
            }

            // l = a(v) x b(w) = (r22, 0, -r02)
            // d(A) = h(A) dot abs(r22, 0, r02)
            // d(B) = h(B) dot abs(r11, r10, 0)
            d   = Vector3.Dot(t, new Vector3(R.M33, 0, -R.M13));
            d_A = Vector3.Dot(m_radius, new Vector3(AR.M33, 0, AR.M13));
            d_B = Vector3.Dot(collider.m_radius, new Vector3(AR.M22, AR.M21, 0));
            if (NetMath.Abs(d) > NetMath.Abs(d_A) + NetMath.Abs(d_B))
            {
                return(false);
            }

            // l = a(w) x b(u) = (-r10, r00, 0)
            // d(A) = h(A) dot abs(r10, r00, 0)
            // d(B) = h(B) dot abs(0, r22, r21)
            d   = Vector3.Dot(t, new Vector3(-R.M21, R.M11, 0));
            d_A = Vector3.Dot(m_radius, new Vector3(AR.M21, AR.M11, 0));
            d_B = Vector3.Dot(collider.m_radius, new Vector3(0, AR.M33, AR.M32));
            if (NetMath.Abs(d) > NetMath.Abs(d_A) + NetMath.Abs(d_B))
            {
                return(false);
            }

            // l = a(w) x b(v) = (-r11, r01, 0)
            // d(A) = h(A) dot abs(r11, r01, 0)
            // d(B) = h(B) dot abs(r22, 0, r20)
            d   = Vector3.Dot(t, new Vector3(-R.M22, R.M12, 0));
            d_A = Vector3.Dot(m_radius, new Vector3(AR.M22, AR.M12, 0));
            d_B = Vector3.Dot(collider.m_radius, new Vector3(AR.M33, 0, AR.M31));
            if (NetMath.Abs(d) > NetMath.Abs(d_A) + NetMath.Abs(d_B))
            {
                return(false);
            }

            // l = a(w) x b(w) = (-r12, r02, 0)
            // d(A) = h(A) dot abs(r12, r02, 0)
            // d(B) = h(B) dot abs(r21, r20, 0)
            d   = Vector3.Dot(t, new Vector3(-R.M23, R.M13, 0));
            d_A = Vector3.Dot(m_radius, new Vector3(AR.M23, AR.M13, 0));
            d_B = Vector3.Dot(collider.m_radius, new Vector3(AR.M32, AR.M31, 0));
            if (NetMath.Abs(d) > NetMath.Abs(d_A) + NetMath.Abs(d_B))
            {
                return(false);
            }

            return(true);
        }
Пример #17
0
 public override void Transform(Vector3 translation, Quaternion rotation, Vector3 scale)
 {
     m_radius = m_radius * NetMath.Max(scale.X, NetMath.Max(scale.Y, scale.Z));
     m_center = m_center + translation;
 }