Ejemplo n.º 1
0
 public void Test_UpdateMin()
 {
     double[] min = new double[2].Fill(double.MaxValue);
     VectorS.UpdateMin(new double[] { 1, 3 }, min);
     Assert.AreEqual(new double[] { 1, 3 }, min);
     VectorS.UpdateMin(new double[] { 5, 0.4 }, min);
     Assert.AreEqual(new double[] { 1, 0.4 }, min);
 }
Ejemplo n.º 2
0
 public void Test_UpdateMax()
 {
     double[] max = new double[2].Fill(double.MinValue);
     VectorS.UpdateMax(new double[] { 1, 3 }, max);
     Assert.AreEqual(new double[] { 1, 3 }, max);
     VectorS.UpdateMax(new double[] { 5, 0.4 }, max);
     Assert.AreEqual(new double[] { 5, 3 }, max);
 }
Ejemplo n.º 3
0
 public void Test_NormalizeByDiff()
 {
     double[] min = new double[2] {
         1, 2
     };
     double[] diff = new double[2] {
         4, 5
     };
     double[] v = new double[2] {
         3, 4
     };
     VectorS.NormalizeByDiff(v, min, diff);
     Assert.AreEqual(new double[] { 0.5, 0.4 }, v);
 }
Ejemplo n.º 4
0
 public void Test_Normalize()
 {
     double[] min = new double[2] {
         1, 2
     };
     double[] max = new double[2] {
         5, 7
     };
     double[] v = new double[2] {
         3, 4
     };
     VectorS.Normalize(v, min, max);
     Assert.AreEqual(new double[] { 0.5, 0.4 }, v);
 }
Ejemplo n.º 5
0
        private static void MainCatch(ref DictStatstic DICT, ref List <string> Blong, int startPo, int endPo, string[,] data, ref string[,] Table2)
        {
            MatrixS Mdata = new MatrixS(data);
            int     kk    = 0;

            for (int i = startPo; i <= endPo; i++)
            {
                /// 欲處理資料
                string[] content = data[i, 10].Split(' ');

                /// 至原始資料之字串矩陣取除目標列數,並將原始之Work與Stone工時與加班轉換並儲存至向量oriVWS中
                VectorS Vcontent = Mdata.GetVector(i, 1);
                Vector  oriVWS   = new Vector(new double[] { Convert.ToDouble(Vcontent.Data[1]), Convert.ToDouble(Vcontent.Data[2]), Convert.ToDouble(Vcontent.Data[3]), Convert.ToDouble(Vcontent.Data[4]) });

                /// 用來統計每一天所有之Work與Stone工時與加班
                Vector VWS = new Vector(new double[4]);

                /// 用來儲存每一天之歸屬資料
                string blong = "";

                foreach (string ff in content)
                {
                    if (ff.IndexOf(")") != -1)
                    {
                        Table2[kk, 0] = i.ToString();
                        Table2[kk, 0] = data[i, 0];
                        Table2[kk, 1] = ff.Split('.')[1];
                        string   catData      = ff.Substring(ff.IndexOf("(") + 1, ff.IndexOf(")") - ff.IndexOf("(") - 1);
                        string   CompanyName  = null;
                        double[] WorkAndStone = CatDataProcess(catData, ref CompanyName, ref blong);
                        Vector   vws          = new Vector(WorkAndStone);
                        DICT.Add(CompanyName, WorkAndStone);
                        VWS += vws;
                        kk++;
                    }
                }
                Vector ResVWS = oriVWS - VWS;
                blong = OriComBlongProcess(ResVWS, blong);

                Blong.Add(blong);
                DICT.Add("利晉", ResVWS.Data);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Returns index of the child closest to the point.
        /// </summary>
        public int FindClosestChild(double [] point, bool normalize)
        {
            double[] normalizedPoint = point.ShallowCopy();
            if (normalize)
            {
                VectorS.NormalizeByDiff(normalizedPoint, ValueMin, ValueBounds);
            }

            double minDist = double.MaxValue;
            int    best    = -1;

            for (int c = 0; c < Children.Length; ++c)
            {
                double dist = VectorS.SquaredDistance(Children[c].Center, normalizedPoint);
                if (dist < minDist)
                {
                    minDist = dist;
                    best    = c;
                }
            }
            return(best);
        }
Ejemplo n.º 7
0
 /// <summary>Returns the singular values as a <see cref="Vector{T}"/>.</summary>
 /// <returns>the singular values as a <see cref="Vector{T}"/>.</returns>
 public Vector <T> S()
 {
     return(VectorS.Clone());
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Run k-means.
        /// Returns hand values.
        /// </summary>
        protected double[][] CalcValuesAndKMeans(KMeansNode parentKmNode, int round, McHand[] hands, out double[][] centers)
        {
            double [][] values = new double[hands.Length][].Fill(i => new double[Dim]);

            double[] min = new double[Dim].Fill(double.MaxValue);
            double[] max = new double[Dim].Fill(double.MinValue);

            for (int i = 0; i < hands.Length; ++i)
            {
                CalculateValue(hands[i].Cards, hands[i].Length, values[i]);
                VectorS.UpdateMin(values[i], min);
                VectorS.UpdateMax(values[i], max);
            }
            double [] delta = VectorS.Sub(max, min);

            if (_normalizeHandValues)
            {
                for (int i = 0; i < values.Length; ++i)
                {
                    VectorS.NormalizeByDiff(values[i], min, delta);
                }
                min.CopyTo(parentKmNode.ValueMin, 0);
                delta.CopyTo(parentKmNode.ValueBounds, 0);
            }

            if (_printHandValues || _printHands)
            {
                for (int i = 0; i < values.Length; ++i)
                {
                    if (_printHands)
                    {
                        Console.Write("{0} ", StdDeck.Descriptor.GetCardNames(hands[i].Cards, 0, hands[i].Length));
                    }
                    if (_printHandValues)
                    {
                        PrintVector(values[i]);
                    }
                    Console.WriteLine();
                }
            }

            // Calcualate number of clusters.
            int k           = MaxBucketCounts[round];
            int adaptingDim = -1;

            for (int d = 0; d < Dim; ++d)
            {
                double clusterSize = ClusterSizes[d][round];
                if (clusterSize != 0)
                {
                    int clCount = (int)Math.Round(delta[d] / clusterSize, 0);
                    if (clCount < k)
                    {
                        adaptingDim = d;
                        k           = clCount;
                    }
                }
            }
            // Make sure number of clusters is in the given range.
            k = Math.Max(MinBucketCounts[round], k);

            if (IsVerbose)
            {
                Console.Write("Min: ");
                PrintVector(min);
                Console.Write(" Max: ");
                PrintVector(max);
                Console.Write(" Delta: ");
                PrintVector(delta);
                if (k < MaxBucketCounts[round])
                {
                    Console.Write(" K adapted to {0} by dim: {1}", k, adaptingDim);
                }
                Console.WriteLine();
            }

            double [][] differentValues      = new double[k][].Fill(i => new double[Dim]);
            int         differentValuesCount = 0;

            for (int i = 0; i < values.Length; ++i)
            {
                for (int j = 0; j < differentValuesCount; ++j)
                {
                    if (VectorS.AreEqual(values[i], differentValues[j]))
                    {
                        goto RepeatedValue;
                    }
                }
                values[i].CopyTo(differentValues[differentValuesCount], 0);
                differentValuesCount++;
                if (differentValuesCount == k)
                {
                    break;
                }
                RepeatedValue :;
            }
            if (differentValuesCount < k)
            {
                // Too few different values to build k clusters. Do not run k-means, it may hang.
                centers = differentValues;
                Array.Resize(ref centers, differentValuesCount);
                if (IsVerbose)
                {
                    Console.WriteLine("Only {0} different values found. Set cluster count, do not run kmeans", differentValuesCount);
                }
                return(values);
            }

            _kmParameters.k = k;
            _kmParameters.n = values.Length;

            _kmParameters.Allocate();

            for (int i = 0; i < values.Length; ++i)
            {
                for (int d = 0; d < Dim; ++d)
                {
                    *_kmParameters.GetPoint(i, d) = values[i][d];
                }
            }

            fixed(Kml.Parameters *kmlp = &_kmParameters)
            {
                Kml.KML_Hybrid(kmlp);
            }

            centers = new double[_kmParameters.k][].Fill(i => new double[Dim]);
            for (int c = 0; c < _kmParameters.k; ++c)
            {
                for (int d = 0; d < Dim; ++d)
                {
                    centers[c][d] = *_kmParameters.GetCenter(c, d);
                }
            }
            _kmParameters.Free();

            return(values);
        }
Ejemplo n.º 9
0
 public void Test_Sub()
 {
     Assert.AreEqual(new double[] { 2, 3 }, VectorS.Sub(new double[] { -1, 2 }, new double[] { -3, -1 }));
 }
Ejemplo n.º 10
0
 public void Test_Add()
 {
     Assert.AreEqual(new double[] { 2, 3 }, VectorS.Add(new double[] { -1, 2 }, new double[] { 3, 1 }));
 }
Ejemplo n.º 11
0
 public void Test_Distance()
 {
     Assert.AreEqual(5, VectorS.Distance(new double[] { -1, 2 }, new double[] { 2, 6 }));
 }
Ejemplo n.º 12
0
 public void Test_SquaredDistance()
 {
     Assert.AreEqual(2, VectorS.SquaredDistance(new double[] { 1, 3 }, new double[] { 2, 4 }));
 }
Ejemplo n.º 13
0
 public void Test_AreEqual()
 {
     Assert.IsTrue(VectorS.AreEqual(new double[] { 1, 3 }, new double[] { 1, 3 }));
     Assert.IsFalse(VectorS.AreEqual(new double[] { 1, 3 }, new double[] { 1, 3, 3 }));
     Assert.IsFalse(VectorS.AreEqual(new double[] { 1, 3 }, new double[] { 1, 2 }));
 }
Ejemplo n.º 14
0
 public void Test_Volume()
 {
     Assert.AreEqual(2, VectorS.Volume(new double[] { 2 }));
     Assert.AreEqual(6, VectorS.Volume(new double[] { 2, 3 }));
     Assert.AreEqual(30, VectorS.Volume(new double[] { 2, 5, 3 }));
 }