Exemple #1
0
        private void btn_algorithm2_Click(object sender, EventArgs e)
        {
            //get values frome user:
            int    N_LENGTH, W_LENGTH, K_CENTERS;
            string file_name;

            //starting time
            getValuesFromUsers(out N_LENGTH, out W_LENGTH, out K_CENTERS, out file_name);

            var watch = System.Diagnostics.Stopwatch.StartNew();///calc execution time

            //call algorithm2
            Console.WriteLine("Calling Algorithm 2...");
            RawDataType         data;
            Tuple <double, int> result = BitClusterDiscord.bitClusterDiscord(out data, N_LENGTH, W_LENGTH, K_CENTERS, file_name);



            watch.Stop();//stop timer
            var elapsedMs = watch.ElapsedMilliseconds;


            this.txt_time.Text             = elapsedMs.ToString();
            this.best_so_far_dist_txt.Text = result.Item1.ToString();
            this.best_so_far_loc_txt.Text  = result.Item2.ToString();

            Console.WriteLine("Writing to file...");
            IOFunctions.writeFile(data, N_LENGTH, result.Item2);
            //Done
            Console.WriteLine(this.Text + " : Done Algorithm 2. Best_so_far_loc = " + result.Item2 + ". Time: " + elapsedMs);
        }
        private void btn_Squeezer_Click(object sender, EventArgs e)
        {
            //get values frome user:
            int    N_LENGTH, W_LENGTH;
            double threshold;
            string file_name;

            //starting time
            getValuesFromUsers_Enhancement(out N_LENGTH, out W_LENGTH, out threshold, out file_name);

            var watch = System.Diagnostics.Stopwatch.StartNew();///calc execution time

            //call the function
            Console.WriteLine("Calling BitClustering + Squeezer ...");

            RawDataType         data;
            List <int>          cluster_belong;
            Tuple <double, int> result = Discord_Time_Series.squeezer(out cluster_belong, out data, N_LENGTH, W_LENGTH, threshold, file_name, Discord_Time_Series.BIT_SERIES);

            watch.Stop();//stop timer
            var elapsedMs = watch.ElapsedMilliseconds;

            this.txt_time.Text             = elapsedMs.ToString();
            this.txt_best_so_far_dist.Text = result.Item1.ToString();
            this.txt_best_so_far_loc.Text  = result.Item2.ToString();

            Console.WriteLine("Writing to file...");
            IOFunctions.writeFile_Cluster(data, N_LENGTH, result.Item2, cluster_belong);
            //Done
            Console.WriteLine(this.Text + " : \n\tDone \"BitClustering + Squeezer\" algorithm. Best_so_far_loc = " + result.Item2 + ". Time: " + elapsedMs);
            Console.WriteLine("====================================================================");
        }
        private void btn_HotSax_Click(object sender, EventArgs e)
        {
            //get values frome user:
            int    N_LENGTH, W_LENGTH;
            string file_name;

            //starting time
            getValuesFromUsers_HOTSAX(out N_LENGTH, out W_LENGTH, out file_name);

            //fixed: hard code
            W_LENGTH = 3;

            var watch = System.Diagnostics.Stopwatch.StartNew();///calc execution time

            //call the function
            Console.WriteLine("Calling HOTSAX ...");

            RawDataType         data;
            Tuple <double, int> result = Heuristic.Heuristic_Function(N_LENGTH, W_LENGTH, file_name, out data);

            watch.Stop();//stop timer
            var elapsedMs = watch.ElapsedMilliseconds;

            this.txt_time.Text             = elapsedMs.ToString();
            this.txt_best_so_far_dist.Text = result.Item1.ToString();
            this.txt_best_so_far_loc.Text  = result.Item2.ToString();

            Console.WriteLine("Writing to file...");
            IOFunctions.writeFile(data, N_LENGTH, result.Item2);

            //Done
            Console.WriteLine(this.Text + " : \n\tDone HOTSAX. Best_so_far_loc = " + result.Item2 + ". Time: " + elapsedMs);
            Console.WriteLine("====================================================================");
        }
Exemple #4
0
        }//end bitClusterDiscord()

        static public Tuple <double, int> squeezer(out List <int> cluster_belong, out RawDataType data, int N_LENGTH, int W_LENGTH, double threshold, string file_name, int type_convert_raw_data)
        {
            //read data
            data = IOFunctions.readFile(file_name); //raw timeseries data

            List <Cluster_Squeezer> b_cluster;


            var watch = System.Diagnostics.Stopwatch.StartNew();///calc execution time

            //call the function
            BitCluster.squeezerCluster(data, type_convert_raw_data, out b_cluster, out cluster_belong, threshold, N_LENGTH, W_LENGTH, file_name);

            watch.Stop();//stop timer
            var elapsedMs = watch.ElapsedMilliseconds;

            Console.WriteLine("________squeezerCluster is Done in " + elapsedMs + ".\nKeep going...Please wait");



            // get Outer loop:
            List <int> outer_loop = getOuterLoop(b_cluster, b_cluster.Count); //get outer

            List <int> inner_loop;
            bool       continue_to_outer_loop = false;

            double nearest_neighbor_dist = 0;
            double dist = 0;

            double best_so_far_dist = 0;
            int    best_so_far_loc  = 0;

            RawDataType p_center, q_center;

            bool[] is_skip_at_p = new bool[outer_loop.Count];
            for (int i = 0; i < outer_loop.Count; i++)
            {
                is_skip_at_p[i] = false;
            }

            int cluster_of_cur_outer; //thang p dang nam o cluster nao
            int cluster_of_cur_inner; // tracking for q's cluster at inner loop

            //test
            int count_skip_at_p               = 0;
            int count_break_sub_2_dis         = 0;
            int count_dis_smaller_best_so_far = 0;
            int count_center_p_diff_center_q  = 0;

            foreach (int p in outer_loop)
            {
                if (is_skip_at_p[p])
                {
                    count_skip_at_p++;
                    //p was visited at inner loop before
                    continue;
                }
                else
                {
                    nearest_neighbor_dist = INFINITE;

                    cluster_of_cur_outer = cluster_belong[p];

                    inner_loop = getInnerLoop(b_cluster, b_cluster.Count, cluster_of_cur_outer);

                    foreach (int q in inner_loop)// inner loop
                    {
                        if (Math.Abs(p - q) < N_LENGTH)
                        {
                            continue;// self-match => skip to the next one
                        }
                        else
                        {
                            /*cluster_of_cur_inner = cluster_belong[q];
                             *
                             *                          if (cluster_of_cur_inner != cluster_of_cur_outer)
                             *                          {
                             *  count_center_p_diff_center_q++;
                             *                                  p_center = data.GetRange(b_cluster[cluster_of_cur_outer].getCluster().getCenterIndex(), N_LENGTH);
                             *                                  q_center = data.GetRange(b_cluster[cluster_of_cur_inner].getCluster().getCenterIndex(), N_LENGTH);
                             *
                             *                                  if (distanceBetween2Clusters(p_center, q_center, b_cluster[cluster_of_cur_outer].getCluster().getRadius(), b_cluster[cluster_of_cur_inner].getCluster().getRadius()) >= best_so_far_dist)
                             *                                  {
                             *                                          continue_to_outer_loop = true;
                             *      count_break_sub_2_dis++;
                             *                                          break;
                             *                                  }
                             *                          }
                             */

                            //calculate the Distance between p and q
                            dist = HelperFunctions.gaussDistance(data.GetRange(p, N_LENGTH), data.GetRange(q, N_LENGTH));

                            if (dist < best_so_far_dist)
                            {
                                //skip the element q at oute_loop, 'cuz if (p,q) is not a solution, so does (q,p).
                                is_skip_at_p[q] = true;
                                count_dis_smaller_best_so_far++;

                                continue_to_outer_loop = true; //break, to the next loop at outer_loop
                                break;                         // break at inner_loop first
                            }

                            if (dist < nearest_neighbor_dist)
                            {
                                nearest_neighbor_dist = dist;
                            }
                        } //end else
                    }     //end inner
                    if (continue_to_outer_loop)
                    {
                        continue_to_outer_loop = false; //reset
                        continue;                       //go to the next p in outer loop
                    }

                    if (nearest_neighbor_dist > best_so_far_dist)
                    {
                        best_so_far_dist = nearest_neighbor_dist;
                        best_so_far_loc  = p;
                    }
                } //end else
            }     //end outter

            Console.WriteLine("count_skip_at_p =" + count_skip_at_p);
            Console.WriteLine("=======count_break_sub_2_dis =" + count_break_sub_2_dis);
            Console.WriteLine("count_dis_smaller_best_so_far =" + count_dis_smaller_best_so_far);
            Console.WriteLine("count_center_p_diff_center_q=" + count_center_p_diff_center_q);


            return(new Tuple <double, int>(best_so_far_dist, best_so_far_loc));
        }//end bitClusterDiscord_Enhancement()
        }//end innerArrangement

        // Heuristic function
        public static Tuple <double, int> Heuristic_Function(int N_LENGTH, int W_DIMENSION, string file_name, out List <double> data)
        {
            data = IOFunctions.readFile(file_name);
            Dictionary <string, int> count_table;
            Dictionary <int, string> total_table;
            AugmentedTrie            tree;

            MakeTree_And_Table_Function(data, N_LENGTH, out tree, out count_table, out total_table, W_DIMENSION);

            double best_so_far_dist = 0;
            int    best_so_far_loc  = 0;

            double nearest_neighbor_dist = 0;
            double dist = 0;

            List <int> outer_list, inner_list;

            outer_list = OuterArrangement(total_table, count_table);
            bool break_to_outer_loop = false;

            bool[] is_skip_at_p = new bool[outer_list.Count];
            for (int i = 0; i < outer_list.Count; i++)
            {
                is_skip_at_p[i] = false;
            }

            foreach (int p in outer_list)
            {
                //if (is_skip_at_p[p] || Math.Abs(p - 10867) < N_LENGTH || Math.Abs(p - 3994) < N_LENGTH
                //    || Math.Abs(p - 13492) < N_LENGTH)//

                if (is_skip_at_p[p])
                {
                    //p was visited at inner loop before
                    continue;
                }
                else
                {
                    nearest_neighbor_dist = INFINITE;
                    string word = total_table[p];

                    inner_list = InnerArrangement(total_table, count_table, tree, word);

                    foreach (int q in inner_list)// inner loop
                    {
                        if (Math.Abs(p - q) < N_LENGTH)
                        {
                            continue;// self-match => skip to the next one
                        }
                        else
                        {
                            //calculate the Distance between p and q
                            dist = Distance(data.GetRange(p, N_LENGTH), data.GetRange(q, N_LENGTH));

                            if (dist < best_so_far_dist)
                            {
                                //skip the element q at oute_loop, 'cuz if (p,q) is not a solution, so does (q,p).
                                is_skip_at_p[q] = true;

                                break_to_outer_loop = true; //break, to the next loop at outer_loop
                                break;                      // break at inner_loop first
                            }

                            if (dist < nearest_neighbor_dist)
                            {
                                nearest_neighbor_dist = dist;
                            }
                        }
                    }//end inner
                    if (break_to_outer_loop)
                    {
                        break_to_outer_loop = false; //reset
                        continue;                    //go to the next p in outer loop
                    }

                    if (nearest_neighbor_dist > best_so_far_dist)
                    {
                        best_so_far_dist = nearest_neighbor_dist;
                        best_so_far_loc  = p;
                    }
                } //end else
            }     //end outter

            Tuple <double, int> result = new Tuple <double, int>(best_so_far_dist, best_so_far_loc);

            //Console.WriteLine("skip = " + number_skip);

            return(result);
        }// end Heuristic function
        static public void bitCluster(out List <Cluster> b_cluster, out List <int> cluster_belong, int K_CENTERS, int N_LENGTH, int W_LENGTH, string file_name)
        {
            //read data
            RawDataType data = IOFunctions.readFile(file_name);

            Dictionary <int, BitseriesType> bit_series_data;                              //store Bit series data

            bit_series_data = HelperFunctions.bitSeriesDataset(data, N_LENGTH, W_LENGTH); //get bit series data from original data


            //set some variables:
            int bit_series_data_length = bit_series_data.Count;



            b_cluster = new List <Cluster>();  //store k clusters

            List <int> k_centers;              //store k centers

            cluster_belong = new List <int>(); //store the cluster whose each point belong to

            double dist_i, dist_p;

            for (int i = 0; i < bit_series_data_length; i++)
            {
                cluster_belong.Add(-1);
            }

            //initialize K centers
            k_centers = getKRandomCenter(bit_series_data_length, K_CENTERS);
            //k_centers = getKCentersContinuously(K_CENTERS);



            //(line 1 - 4 of the paper): initialize k cluster, then append them to 'b_cluster'
            for (int i = 0; i < K_CENTERS; i++)
            {
                Cluster one_cluster = new Cluster(k_centers[i], 1);
                b_cluster.Add(one_cluster);
            }


            //(line 5-15 in the paper):
            for (int j = 0; j < bit_series_data_length; j++)//go through the BD data
            {
                //get each element:
                if (cluster_belong[j] == (-1)) //if the point hasn't belong any cluster yet
                {
                    int p = 0;
                    for (int i = 1; i < K_CENTERS; i++)
                    {
                        dist_i = HelperFunctions.bitSeriesDistance(bit_series_data[j], bit_series_data[b_cluster[i].getCenterIndex()]);
                        dist_p = HelperFunctions.bitSeriesDistance(bit_series_data[j], bit_series_data[b_cluster[p].getCenterIndex()]);


                        if (dist_i < dist_p)
                        {
                            p = i;
                        }
                    }//end for

                    // line 12-14 in the paper
                    cluster_belong[j] = p;
                    b_cluster[p].plusOneToNumberOfMembers();
                    b_cluster[p].addToListMemberIndice(j);
                }
            }//end of for

            double        radius;
            List <double> center_value;

            for (int i = 0; i < K_CENTERS; i++)
            {
                center_value = data.GetRange(b_cluster[i].getCenterIndex(), N_LENGTH);
                radius       = HelperFunctions.calculateRadius(data, b_cluster[i].getListMemberIndice(), center_value, N_LENGTH);
                b_cluster[i].setRadius(radius);
            }
        }//end of function
        //algorithm 2:
        static public Tuple <double, int> bitClusterDiscord(out RawDataType data, int N_LENGTH, int W_LENGTH, int K_CENTERS, string file_name)
        {
            //read data
            data = IOFunctions.readFile(file_name); //raw timeseries data

            //store Bit series data
            Dictionary <int, BitseriesType> bit_series_data = HelperFunctions.bitSeriesDataset(data, N_LENGTH, W_LENGTH);//get bit series data from original data

            //line 6: run algorithm 1 to have b_cluster
            List <Cluster> b_cluster;
            List <int>     cluster_belong;

            BitCluster.bitCluster(out b_cluster, out cluster_belong, K_CENTERS, N_LENGTH, W_LENGTH, file_name);

            Console.WriteLine("Algorithm1 is Done !\nKeep going...Please wait");



            // get Outer loop:
            List <int> outer_loop = getOuterLoop(b_cluster, K_CENTERS); //get outer

            List <int> inner_loop;
            bool       continue_to_outer_loop = false;

            double nearest_neighbor_dist = 0;
            double dist = 0;

            double best_so_far_dist = 0;
            int    best_so_far_loc  = 0;

            RawDataType p_center, q_center;

            bool[] is_skip_at_p = new bool[outer_loop.Count];
            for (int i = 0; i < outer_loop.Count; i++)
            {
                is_skip_at_p[i] = false;
            }

            int cluster_of_cur_outer; //thang p dang nam o cluster nao
            int cluster_of_cur_inner; // tracing for q's cluster at inner loop

            foreach (int p in outer_loop)
            {
                //if (is_skip_at_p[p] || Math.Abs(p - 10867) < N_LENGTH || Math.Abs(p - 3994) < N_LENGTH
                //    || Math.Abs(p - 13492) < N_LENGTH)//

                if (is_skip_at_p[p])
                {
                    //p was visited at inner loop before
                    continue;
                }
                else
                {
                    nearest_neighbor_dist = INFINITE;

                    cluster_of_cur_outer = cluster_belong[p];

                    inner_loop = getInnerLoop(b_cluster, K_CENTERS, cluster_of_cur_outer);

                    foreach (int q in inner_loop)// inner loop
                    {
                        if (Math.Abs(p - q) < N_LENGTH)
                        {
                            continue;// self-match => skip to the next one
                        }
                        else
                        {
                            cluster_of_cur_inner = cluster_belong[q];

                            p_center = data.GetRange(b_cluster[cluster_of_cur_outer].getCenterIndex(), N_LENGTH);
                            q_center = data.GetRange(b_cluster[cluster_of_cur_inner].getCenterIndex(), N_LENGTH);

                            if (distanceBetween2Clusters(p_center, q_center, b_cluster[cluster_of_cur_outer].getRadius(), b_cluster[cluster_of_cur_inner].getRadius()) >= best_so_far_dist)
                            {
                                continue_to_outer_loop = true;
                                break;
                            }
                            //calculate the Distance between p and q
                            dist = HelperFunctions.gaussDistance(data.GetRange(p, N_LENGTH), data.GetRange(q, N_LENGTH));

                            if (dist < best_so_far_dist)
                            {
                                //skip the element q at oute_loop, 'cuz if (p,q) is not a solution, so does (q,p).
                                is_skip_at_p[q] = true;

                                continue_to_outer_loop = true; //break, to the next loop at outer_loop
                                break;                         // break at inner_loop first
                            }

                            if (dist < nearest_neighbor_dist)
                            {
                                nearest_neighbor_dist = dist;
                            }
                        } //end else
                    }     //end inner
                    if (continue_to_outer_loop)
                    {
                        continue_to_outer_loop = false; //reset
                        continue;                       //go to the next p in outer loop
                    }

                    if (nearest_neighbor_dist > best_so_far_dist)
                    {
                        best_so_far_dist = nearest_neighbor_dist;
                        best_so_far_loc  = p;
                    }
                } //end else
            }     //end outter
            return(new Tuple <double, int>(best_so_far_dist, best_so_far_loc));
        }