public void CreateRandom(ClusterTable ct, int seed)
        {
            Dictionary<Category, Category> cDic = new Dictionary<Category, Category>();
            foreach (var item in ct.Categories)
            {
                var c = new Category() { KeyName = item.KeyName, Name = item.Name };
                Categories.Add(c);
                cDic.Add(item, c);
                item.Update();
            }

            random = new Random(seed);

            foreach (var item in ct.LayerGroup)
            {
                List<Models.Comunity> comunityList = new List<Models.Comunity>();
                List<Models.Layer> layerList = new List<Models.Layer>();
                foreach (var layer in item.Items)
                {
                    comunityList.AddRange(layer.Comunities);
                    // layer.Comunities.Clear();
                }
                foreach (var item2 in Categories)
                {
                    var l = item2.GetLayer(item.Name);
                    layerList.Add(l);
                }
                int c = 0;
                foreach (var comunity in comunityList.OrderBy(n => random.NextDouble()))
                {
                    var cc = new ComunityEx(comunity);

                    if (cc.Lock)
                    {
                        cDic[cc.OrignalComunity.Category].GetLayer(item.Name).Comunities.Add(cc);
                    }
                    else
                    {
                        layerList[c % layerList.Count].Comunities.Add(cc);
                        c++;
                    }
                    comunityList.Add(cc);
                    ComunityDic.Add(comunity.Id, cc);
                    ComunityList.Add(cc);
                }
                LayerGroup.Add(item.Name, new LayerGroup() { Name = item.Name, Items = new System.Collections.ObjectModel.ObservableCollection<Layer>(layerList) });
            }
        }
        private ChageCategoryData DivideCategory(Category category)
        {
            Category c1 = new Category();
            Category c2 = new Category();
            List<Category> categoryList = new List<Category>() { c1, c2 };
            List<ComunityEx> comunityList = new List<ComunityEx>();
            List<ComunityEx> comunityUnLockList = new List<ComunityEx>();

            bool flag = true;
            foreach (ComunityEx item in category.GetComuity().OfType<ComunityEx>().OrderBy(n => random.NextDouble()))
            {
                if (item.Lock)
                {
                    if (flag) c1.GetLayer(item.Layer.Name).Comunities.Add(item);
                    else
                    {
                        c2.GetLayer(item.Layer.Name).Comunities.Add(item);
                    }
                    flag = !flag;
                    comunityList.Add(item);
                }
                else
                {
                    comunityUnLockList.Add(item);
                }
            }
            foreach (var item in categoryList)
            {
                item.Update();
            }

            for (int i = 0; i < 500; i++)
            {
                bool endFlag = true;
                foreach (var item in comunityList)
                {
                    Dictionary<Models.Category, double> countDic = new Dictionary<Models.Category, double>();

                    foreach (var item2 in categoryList)
                    {
                        double sum = 0;
                        foreach (var item3 in item2.GetComuity())
                        {
                            if (item3.Relations.Where(n => n.ItemId == item.Id).Any())
                            {
                                sum += item3.Relations.Where(n => n.ItemId == item.Id).First().補正確信度;
                            }
                        }
                        countDic.Add(item2, sum);
                    }

                    {
                        var selectedCategory = countDic.OrderByDescending(n => n.Value / (double)n.Key.GetTmpComuity().Where(m => m != item).Count()).Select(n => n.Key).First();
                        if (selectedCategory == item.Category)
                        {
                            item.Layer.Tmp2Comunities.Add(item);
                        }
                        else
                        {
                            var layer = selectedCategory.GetLayer(item.Layer.Name);
                            layer.Tmp2Comunities.Add(item);
                            endFlag = false;
                        }
                    }
                }

                foreach (var item in categoryList)
                {
                    item.UpdateTmp();
                }
                if (endFlag) break;
            }

            foreach (var item in categoryList)
            {
                item.UpDateView();
            }

            if (c1.GetComuity().Count() >= MinCategoryComunityCount && c2.GetComuity().Count() >= MinCategoryComunityCount)
            {
                ChageCategoryData ccd = new ChageCategoryData() { IsChanged = true, OrignalCategory = category, ChangedCategory = new List<Category>() { c1, c2 } };
                flag = true;
                foreach (var item in comunityUnLockList)
                {
                    if (flag) c1.GetLayer(item.Layer.Name).Comunities.Add(item);
                    else
                    {
                        c2.GetLayer(item.Layer.Name).Comunities.Add(item);
                    }
                    flag = !flag;
                }
                return ccd;
            }
            return new ChageCategoryData() { IsChanged = false };
        }
        public ClusteringResult Clone()
        {
            ClusteringResult target = new ClusteringResult();
            target.Parent = this.Parent;
            HashSet<string> layerNames = new HashSet<string>();
            List<Category> categoryList = new List<Category>();
            target.checkDividCategoryDic = this.checkDividCategoryDic;
            target.random = new Random(random.Next());
            foreach (var category in Categories)
            {
                var ca = new Category();
                target.Categories.Add(ca);
                categoryList.Add(ca);
                foreach (var item in category.Layer)
                {
                    var layer = ca.GetLayer(item.Key);
                    layerNames.Add(item.Key);
                    foreach (ComunityEx comuinity in item.Value.Comunities)
                    {
                        layer.Comunities.Add(comuinity);
                        target.ComunityList.Add(comuinity);
                        target.ComunityDic.Add(comuinity.Id, comuinity);
                    }
                }
            }

            foreach (var item in layerNames)
            {
                LayerGroup lg = new LayerGroup()
                {
                    Name = item,
                    Items = new System.Collections.ObjectModel.ObservableCollection<Layer>()
                };
                target.LayerGroup.Add(item, lg);
                foreach (var item2 in Categories)
                {
                    lg.Items.Add(item2.GetLayer(item));
                }
            }
            return target;
        }
 public void AddNewCategory()
 {
     var category = new Category();
     category.Name = "残余";
     foreach (var item in this.ComunityDic.Values.Where(n => n.Lock == false))
     {
         category.GetLayer(item.Layer.Name).Comunities.Add(item);
     }
     this.Categories.Add(category);
 }
 public void RightShift(Category c)
 {
     int postinon = this.Categories.IndexOf(c);
     if (postinon < this.Categories.Count - 1)
     {
         int postinon2 = postinon + 1;
         this.Categories.Remove(c);
         this.Categories.Insert(postinon2, c);
         foreach (var item in LayerGroup)
         {
             var tmp = item.Items[postinon];
             item.Items.RemoveAt(postinon);
             item.Items.Insert(postinon2, tmp);
         }
         //カテゴリの追加時にバグる
         //foreach (var item in CategoryAttributeGroup)
         //{
         //    var tmp = item.Items[postinon];
         //    item.Items.RemoveAt(postinon);
         //    item.Items.Insert(postinon2, tmp);
         //}
     }
 }
 public void RemoveCategory(Category category)
 {
     foreach (var item in LayerGroup)
     {
         var layer = category.GetLayer(item.Name);
         item.Items.Remove(layer);
     }
     Categories.Remove(category);
 }
        public void AddCategroy()
        {
            var category = new Category();
            foreach (var item in LayerGroup)
            {
                var layer = category.GetLayer(item.Name);
                item.Items.Add(layer);
            }
            int count = 1;
            while (true)
            {
                if (this.Categories.Where(n => n.KeyName == "C_" + count).Any())
                {
                    count++;
                }
                else
                {
                    category.KeyName = "C_" + count.ToString();
                    break;
                }
            }

            Categories.Add(category);
        }