public void MoveTo(ClusterList s,int toID)
 {//Move element operation
     Cluster from = s.getClusterbyID(ClusterID);
     Cluster to = s.getClusterbyID(toID);
     //if it's the first element
     if (Before == null)
     {
         from.First = After;
         if (After != null) {
             After.Before = null;
         }
     }
     else
     {
         Before.After = After;
         if (After != null)
         {
             After.Before = Before;
         }
     }
     from.num--;
     from.NumberLinks =from.NumberLinks- from.num;
     Before = null;
     After = null;
     //remove then insert
     to.Add(this);
 }
        private void bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker bk = sender as BackgroundWorker;

            clusterlist = new ClusterList();
            StreamReader sr = new StreamReader(filepath);
            string line, FileName;
            int ElementNum, ClusterNum;
            try
            {

                FileName = sr.ReadLine();
                string[] filepathname = FileName.Split('.');
                filename = filepathname[0];
                line = sr.ReadLine();
                string[] information = line.Split(' ');
                // reject the duplicate ' '
                int i = 0;
                while (information[i] == "")
                {
                    i++;
                }
                ElementNum = Convert.ToInt32(information[i]);
                i++;
                while (information[i] == "")
                {
                    i++;
                }
                ClusterNum = Convert.ToInt32(information[i]);
            }
            catch (Exception)
            {

                throw;
            }


            double[,] inputScore = new double[ElementNum + 1, ElementNum + 1];
            bk.ReportProgress(10, "Loading Elements & Scores...");
            for (int i = 0; i < ElementNum; i++)
            {
                clusterlist.AddElement2defaultCluster(new Element());
            }

            while (!(line = sr.ReadLine()).Contains("-999"))
            {
                string[] sarray = line.Split(' ');
                int temp = sarray.Count();
                int x = 0, y = 0;
                double value = 0;
                int count = 0;
                for (int i = 0; i < temp; i++)
                {
                    if (sarray[i] != "" && count == 0)
                    {
                        x = Convert.ToInt32(sarray[i]);
                        count++;
                    }
                    else if (sarray[i] != "" && count == 1)
                    {
                        y = Convert.ToInt32(sarray[i]);
                        count++;
                    }
                    else if (sarray[i] != "" && count == 2)
                    {
                        value = Convert.ToDouble(sarray[i]);
                        count++;
                    }
                }
                if (value > 0)
                {
                    inputScore[x, y] = value;
                    inputScore[y, x] = value;
                }
                else
                {
                    inputScore[x, y] = -value;
                    inputScore[y, x] = -value;
                }
            }



            clusterlist.ImportScore(inputScore,ifnormalization,ElementNum);
            
            clusterlist.InitClusters(ClusterNum, UBound, LBound, a1, a2);
            if (sr != null) sr.Close();




            try
            {
                bk.ReportProgress(30, "Initializing the initial solution..."); Thread.Sleep(500);
                clusterlist.Create2EleClusters();
                clusterlist.InitTSPhase();
                bk.ReportProgress(40, "Executing TS...");
                clusterlist.ExecTSPhase();

                ArrayList arr = clusterlist.GetMoveLogList();
                bk.ReportProgress(85, "Writing LogFile..."); Thread.Sleep(500);
                FileStream cFile = new FileStream(outputpath + "\\log_" + filename + "_" + TimeStamp + ".txt", FileMode.OpenOrCreate);
                using (StreamWriter sw = new StreamWriter(cFile))
                {
                    for (int i = 0; i < arr.Count; i++)
                    {
                        sw.WriteLine(arr[i].ToString());
                    }
                }
            }
            catch (Exception exc)
            {

                FileStream cFile = new FileStream(outputpath + "\\log_" + filename + "_" + TimeStamp + ".txt", FileMode.OpenOrCreate);
                using (StreamWriter sw = new StreamWriter(cFile))
                {
                    sw.WriteLine(exc.Message + "\r\n");
                }
            }





           
            string time = System.DateTime.Now.ToString();
            TimeSpan ts1 = new TimeSpan(timestart.Ticks);
            TimeSpan ts2 = new TimeSpan(System.DateTime.Now.Ticks);
            TimeSpan ts = ts1.Subtract(ts2).Duration();
            string sthours, stminutes, stseconds, stmseconds;
            int ihours = Convert.ToInt32(ts.Hours);
            int iminutes = Convert.ToInt32(ts.Minutes);
            int iseconds = Convert.ToInt32(ts.Seconds);
            int imseconds = Convert.ToInt32(ts.Milliseconds);
            if (ihours < 10)
            {
                sthours = "0" + ts.Hours.ToString();
            }
            else
            {
                sthours = ts.Hours.ToString();
            }
            if (iminutes < 10)
            {
                stminutes = "0" + ts.Minutes.ToString();
            }
            else
            {
                stminutes = ts.Minutes.ToString();
            }
            if (iseconds < 10)
            {
                stseconds = "0" + ts.Seconds.ToString();
            }
            else
            {
                stseconds = ts.Seconds.ToString();
            }
            if (imseconds < 10)
            {
                stmseconds = "0" + ts.Milliseconds.ToString();
            }
            else
            {
                stmseconds = ts.Milliseconds.ToString();
            }
            timeusing = sthours + ":"
                    + stminutes + ":"
                    + stseconds + ":"
                    + stmseconds;


            bk.ReportProgress(90, "Writing ClusterFile..."); Thread.Sleep(500);
            
            FileStream aFile = new FileStream(outputpath + "\\cluster_" + filename + "_" + TimeStamp + ".txt", FileMode.OpenOrCreate);
            try
            {
                ArrayList arr = clusterlist.GetClusterLogList();
                using (StreamWriter sw = new StreamWriter(aFile))
                {
                    for (int i = 0; i < arr.Count; i++)
                    {
                        sw.WriteLine(arr[i].ToString());
                    }
                }
            }
            catch (Exception exc)
            {
                using (StreamWriter sw = new StreamWriter(aFile))
                {
                    sw.WriteLine(exc.Message + "\r\n");
                }
            }

            bk.ReportProgress(95, "Writing ElementFile...");
            Thread.Sleep(500);
            FileStream bFile = new FileStream(outputpath + "\\element_" + filename + "_" + TimeStamp + ".txt", FileMode.OpenOrCreate);
            using (StreamWriter sw = new StreamWriter(bFile))
            {
                sw.WriteLine("CID\t EID" + '\n');
                //for (int i = 1; i <= ElementNum; i++)
                //{
                //        sw.WriteLine(clusterlist.getElementbyID(i).ElementID.ToString() + " ," + clusterlist.getElementbyID(i).ClusterID.ToString() + '\n');
                //}
                for (int i = 1; i <= ClusterNum; i++)
                {
                    for (int j = 1; j <= ElementNum; j++)
                    {
                        if (clusterlist.getElementbyID(j).ClusterID == i)
                        {
                            sw.WriteLine(clusterlist.getElementbyID(j).ClusterID.ToString() + "\t" + clusterlist.getElementbyID(j).ElementID.ToString() + '\n');
                        }
                    }
                }
            }

            

        }