//================================================================================= public List <Case> FindAlternative_WithClustering(Case problem) { Kmeans kmeans = new Kmeans(); // know which cluster int t = kmeans.findCluster_customize(problem, centroids); // get particular cluster return(Db.get_cases_condition_cluster(standrize_table, casename, "cluster", t.ToString())); }
private void button3_Click_1(object sender, EventArgs e) { if (add_file()) { List <Case> centroids = new List <Case>(); centroids = Db.get_Centroids(choice.Tablename_Standardization, choice.CaseName); for (int i = 0; i < ListCases.Count; i++) { // Case s_case = new Case(0, ListCases[0].GetCaseName(), ListCases[0].GetCaseDescription()); List <Case> restandrize_array = choice.Re_standrize(ListCases[i]);//, out s_case); Case s_case = restandrize_array.Last(); Case result_centroid = choice.Updated_cluster_Min_Centroid(s_case, centroids, restandrize_array); if (result_centroid != null) { Feature f = (Feature)result_centroid.GetFeatures()[0]; s_case.GetFeature("cluster").SetFeatureValue(f.GetFeatureValue()); // اسناد العنقود المناسب للعنصر الجديد Db.update_cluster(choice.Tablename_Standardization, s_case.GetFeature("id").GetFeatureValue().ToString(), Convert.ToInt32(f.GetFeatureValue())); Db.update_cluster(choice.TableName, s_case.GetFeature("id").GetFeatureValue().ToString(), Convert.ToInt32(f.GetFeatureValue())); double ess = 0; // من ضمن الداتا سيت المقييسة يتم حساب المراكز لجميع العناقيد for (int k = 0; k < Convert.ToInt32(choice.Clusters_Num); k++) { List <Case> S_Arr_Cluster = Db.get_cases_condition_cluster(choice.Tablename_Standardization, choice.CaseName, "cluster", k.ToString()); Feature cen = S_Arr_Cluster[0].GetFeature("cluster"); Case new_centroid = Kmeans.ReCalcCentroid(S_Arr_Cluster, Convert.ToInt32(cen.GetFeatureValue())); Db.insert_case_to_table(new_centroid, choice.Tablename_Standardization); EuclideanSimilarity es = new EuclideanSimilarity(); double d = 0; foreach (Case c in S_Arr_Cluster) { d += es.Dissimilarity(c, new_centroid); } ess += d; } MessageBox.Show(ess.ToString()); } } dataGridView_old.DataSource = Db.get_table_contetnt(choice.TableName); } }
//================================================================================= public List <Case> FindAlternative_SMF(Case problem, string sim) { List <Case> ClusterdArrCases = new List <Case>(); DefaultEngine _engine = new DefaultEngine(); Db.set_xml(sim, casename, tablename); // centroids = new List<Case>(); Kmeans kmeans = new Kmeans(); ArrayList current_cases_partial = new ArrayList(); // get centroids //centroids = Db.get_Centroids(standrize_table, casename); // know which cluster ClusterdArrCases = new List <Case>(); current_cases_partial.Clear(); int t = kmeans.findCluster_customize(problem, centroids); // get particular cluster current_cases_partial = Db.get_cases_condition_cluster_arraylist(standrize_table, casename, "cluster", t.ToString()); _engine.SetEnvironmentVariable(_env); _engine.SetProblem(problem); // Run the reasoning engine _engine.Startup(); ArrayList mcases = _engine.Run_caseRetrieval_partial(current_cases_partial); int count_solution = mcases.Count; if (count_solution != 0) { Stat ss = null; Case cc = null; for (int i = 0; i < mcases.Count; i++) { ss = (Stat)(mcases[i]); cc = ss.GetCBRCase(); ClusterdArrCases.Add(cc); } } return(ClusterdArrCases); }
private void button4_Click_1(object sender, EventArgs e) { List <string> ids = new List <string>(); List <Case> standrize_array = new List <Case>(); //get ids od cases to delete for (int i = 0; i < dataGridView_old.SelectedRows.Count; i++) { ids.Add(dataGridView_old.SelectedRows[i].Cells["id"].Value.ToString()); } // delete cases from tables Db.del_case_id(choice.TableName, ids); Db.del_case_id(choice.Tablename_Standardization, ids); // re_standrize all tables ArrayList array = Db.get_cases_as_arraylist_condition(choice.Tablename_Standardization, choice.CaseName); standrize_array = choice.StandardizeData(array); Db.del_data_table(choice.Tablename_Standardization); standrize_array.Add(choice.Case_Max_Mean); standrize_array.Add(choice.Case_Min_SD); int num = Convert.ToInt32(choice.Clusters_Num); for (int i = 0; i < num; i++) // recalc centroids { List <Case> cases_cluster = standrize_array.FindAll( delegate(Case ca) { return(ca.GetFeature("cluster").GetFeatureValue().ToString() == i.ToString()); } ); Case centroid = Kmeans.ReCalcCentroid(cases_cluster, i); standrize_array.Add(centroid); } // add all after restandrize Db.insert_data_to_standrize_caseTable_noID(choice.Tablename_Standardization, choice.get_rows_as_string(standrize_array)); dataGridView_old.DataSource = Db.get_table_contetnt(choice.TableName); }
private void button2_Click_1(object sender, EventArgs e) { if (add_file()) { List <Case> centroids = new List <Case>(); centroids = Db.get_Centroids(choice.Tablename_Standardization, choice.CaseName); List <string> Ids = new List <string>(); for (int i = 0; i < ListCases.Count; i++) { //Case s_case = new Case(0, ListCases[0].GetCaseName(), ListCases[0].GetCaseDescription()); // تقييس الداتا سيت بناءا على العنصر الجديد var watch = System.Diagnostics.Stopwatch.StartNew(); List <Case> restandrize_array = choice.Re_standrize(ListCases[i]); // العنصر الجديد بعد التقييس Case s_case = restandrize_array.Last(); // نرى إلى أي عنقود ستمم اضافته Case result_centroid = choice.Updated_cluster(s_case, centroids, restandrize_array); watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; label2.Text = "with clustring time " + elapsedMs.ToString(); // سيتم انضمامه لعنقود موجود if (result_centroid != null) { Feature f = (Feature)result_centroid.GetFeatures()[0]; s_case.GetFeature("cluster").SetFeatureValue(f.GetFeatureValue()); // اسناد العنقود المناسب للعنصر الجديد Db.update_cluster(choice.Tablename_Standardization, s_case.GetFeature("id").GetFeatureValue().ToString(), Convert.ToInt32(f.GetFeatureValue())); Db.update_cluster(choice.TableName, s_case.GetFeature("id").GetFeatureValue().ToString(), Convert.ToInt32(f.GetFeatureValue())); Ids.Add(f.GetFeatureValue().ToString()); } // end if else // عنقود جديد { int k = Convert.ToInt32(choice.Clusters_Num) + 1; // اضافة عنقود جديد Db.update_cluster(choice.Tablename_Standardization, s_case.GetFeature("id").GetFeatureValue().ToString(), k); Db.update_cluster(choice.TableName, s_case.GetFeature("id").GetFeatureValue().ToString(), k); Case new_centroid = s_case; new_centroid.GetFeature("cluster").SetFeatureValue(-1); Db.insert_case_to_table(new_centroid, choice.Tablename_Standardization); choice.Clusters_Num = k.ToString(); Db.update_clusterNum(choice.CaseName, k); } } // end for all new cases if (Ids != null) { Db.del_centroids(choice.Tablename_Standardization, Ids); double ess = 0; // من ضمن الداتا سيت المقييسة يتم حساب المراكز لجميع العناقيد foreach (string id in Ids) { List <Case> S_Arr_Cluster = Db.get_cases_condition_cluster(choice.Tablename_Standardization, choice.CaseName, "cluster", id); Feature cen = S_Arr_Cluster[0].GetFeature("cluster"); Case new_centroid = Kmeans.ReCalcCentroid(S_Arr_Cluster, Convert.ToInt32(cen.GetFeatureValue())); EuclideanSimilarity es = new EuclideanSimilarity(); double d = 0; foreach (Case c in S_Arr_Cluster) { d += es.Dissimilarity(c, new_centroid); } ess += d; Db.insert_case_to_table(new_centroid, choice.Tablename_Standardization); } // حساب distorion error MessageBox.Show(ess.ToString()); } dataGridView_old.DataSource = Db.get_table_contetnt(choice.TableName); } }
//======================================================================================================================= public void FindSolutions_AHP() { bool consistency; double[] choices; double[] tempresult; AllNonClusterdResults = new Dictionary <Case, Dictionary <double, double[, ]> >(); AllClusterdResults = new Dictionary <Case, Dictionary <double, double[, ]> >(); criteria_weights = new Dictionary <Case, double[]>(); Dictionary <string, double> cri_dic; Dictionary <double, double[, ]> tempNon; Dictionary <double, double[, ]> tempWith; List <Case> ClusterdArrCases; List <Case> NonClusterdArrCases; double[,] Non_Results; double[,] With_Results; ArrayList current_cases_partial = new ArrayList(); //List<Case> centroids = new List<Case>(); Kmeans kmeans = new Kmeans(); Dictionary <int, List <Case> > cases_by_centroids = new Dictionary <int, List <Case> >(); // get centroids for case for (int i = 0; i < centroids.Count; i++) { int k = Convert.ToInt32(centroids[i].GetFeature("id").GetFeatureValue()); cases_by_centroids.Add(k, Db.get_cases_condition_cluster(tablename, casename, "cluster", k.ToString())); } //--------------------------------------------------------------------------------- for (int u = 0; u < exp_random_cases.Count; u++) { cri_dic = new Dictionary <string, double>(); double sim = Math.Round(0.10, 2); Case _problem = exp_random_cases[u]; // Criteria Comparsions------------------------ MyAHPModel model = new MyAHPModel(count_citeria); tempNon = new Dictionary <double, double[, ]> (); tempWith = new Dictionary <double, double[, ]>(); double[,] cri_arr = model.Create_Criteria_Comparison_Array(count_citeria); //-------Criteria Weights---------------------------------------------------------------- model.AddCriteria(cri_arr); double[] cri_wgh = new double[count_citeria]; List <string> criteria_names = Db.get_criteria(casename); bool res = false; while (!res) { if (model.CalculatedCriteria()) { res = true; } } cri_wgh = model.CriteriaWeights; criteria_weights.Add(_problem, model.CriteriaWeights); //-------------------------------AHP-------------------------------------------------- while (sim < 1) { NonClusterdArrCases = FindAlternative_NonClustering(_problem, sim.ToString()); ClusterdArrCases = FindAlternative_WithClustering(_problem, sim.ToString()); if (NonClusterdArrCases.Count != 0) { // non clustering MyAHPModel model1 = new MyAHPModel(count_citeria, NonClusterdArrCases.Count); model1.AddCriteria(cri_arr); model1.CalculatedCriteria(); // Choices Comparsions------------------------ consistency = false; choices = new double[NonClusterdArrCases.Count]; tempresult = new double[NonClusterdArrCases.Count]; for (int uu = 0; uu < count_citeria; uu++) { double[,] comp_choice = model1.CreateOne_CeiteriaChoiceComp(uu, NonClusterdArrCases); while (!consistency) { tempresult = model1.Calculated_One__Choic(out consistency, model1.CriteriaWeights[uu], comp_choice); } consistency = false; for (int h = 0; h < tempresult.GetLength(0); h++) { choices[h] += tempresult[h]; } } int ii = 0; Non_Results = new double[NonClusterdArrCases.Count, 2]; foreach (Case cc in NonClusterdArrCases) { Non_Results[ii, 0] = Convert.ToDouble(cc.GetFeature("id").GetFeatureValue()); Non_Results[ii, 1] = choices[ii] * 100; ii++; } // sort Clustered for (int i = 0; i < Non_Results.GetLength(0) - 1; i++) { for (int j = 0; j < Non_Results.GetLength(0) - i - 1; j++) { if (Non_Results[j, 1] < Non_Results[j + 1, 1]) { double temp = Non_Results[j, 1]; Non_Results[j, 1] = Non_Results[j + 1, 1]; Non_Results[j + 1, 1] = temp; temp = Non_Results[j, 0]; Non_Results[j, 0] = Non_Results[j + 1, 0]; Non_Results[j + 1, 0] = temp; } } } tempNon.Add(sim, Non_Results); } if (ClusterdArrCases.Count != 0) { // Clustering MyAHPModel model2 = new MyAHPModel(count_citeria, ClusterdArrCases.Count); model2.AddCriteria(cri_arr); model2.CalculatedCriteria(); // Choices Comparsions------------------------ consistency = false; choices = new double[ClusterdArrCases.Count]; tempresult = new double[ClusterdArrCases.Count]; for (int uu = 0; uu < count_citeria; uu++) { double[,] comp_choice = model2.CreateOne_CeiteriaChoiceComp(uu, ClusterdArrCases); while (!consistency) { tempresult = model2.Calculated_One__Choic(out consistency, model2.CriteriaWeights[uu], comp_choice); } consistency = false; for (int h = 0; h < tempresult.GetLength(0); h++) { choices[h] += tempresult[h]; } } int ii = 0; With_Results = new double[ClusterdArrCases.Count, 2]; foreach (Case cc in ClusterdArrCases) { With_Results[ii, 0] = Convert.ToDouble(cc.GetFeature("id").GetFeatureValue()); With_Results[ii, 1] = choices[ii] * 100; ii++; } // sort Clustered for (int i = 0; i < With_Results.GetLength(0) - 1; i++) { for (int j = 0; j < With_Results.GetLength(0) - i - 1; j++) { if (With_Results[j, 1] < With_Results[j + 1, 1]) { double temp = With_Results[j, 1]; With_Results[j, 1] = With_Results[j + 1, 1]; With_Results[j + 1, 1] = temp; temp = With_Results[j, 0]; With_Results[j, 0] = With_Results[j + 1, 0]; With_Results[j + 1, 0] = temp; } } } tempWith.Add(sim, With_Results); } // end if sim += Math.Round(0.05, 2); sim = Math.Round(sim, 2); } // end sim AllNonClusterdResults.Add(exp_random_cases[u], tempNon); AllClusterdResults.Add(exp_random_cases[u], tempWith); } // end for }
private void button3_Click(object sender, EventArgs e) { var watch = System.Diagnostics.Stopwatch.StartNew(); string Standrize_Type = ""; if (rbtn_normalize.Checked) { Standrize_Type = "Q"; } else if (rbtn_minmax.Checked) { Standrize_Type = "MN"; } else { Standrize_Type = "none"; } string Kmeans_Method = ""; if (rbtn_kmeansPlus.Checked) { Kmeans_Method = "KPP"; } else if (rbtn_simpleKmean.Checked) { Kmeans_Method = "K"; } else { Kmeans_Method = "none"; } string Clusters_num = txt_cluster.Text; mybestchoice = new BestChoice(ArrayCases, Standrize_Type, Kmeans_Method, Clusters_num); standardize_current_cases = mybestchoice.StandardizeData(ArrayCases); if (rbtn_simpleKmean.Checked) { // Kmeans kmeans = new Kmeans(int.Parse(txt_cluster.Text), standardize_current_cases); // kmeans.run(); // Number of Attributes List <Cluster> AllClusters = new List <Cluster>(); AllClusters.Add(null); Cluster myc = new Cluster(); int Nd = standardize_current_cases[0].GetFeatures().Count - 2;//except id , cluster Kmeans kmeans = new Kmeans(1, standardize_current_cases); kmeans.run(); myc.clusters = kmeans.clusters; foreach (double sk in kmeans.distances) { myc.sk += sk; } myc.fk = 1; myc.k = 1; AllClusters.Add(myc); for (int ik = 2; ik <= 5; ik++)//19 { myc = new Cluster(); kmeans = new Kmeans(ik, standardize_current_cases); kmeans.run(); myc.k = ik; myc.clusters = kmeans.clusters; foreach (double sk in kmeans.distances) { myc.sk += sk; } if (AllClusters[ik - 1].sk == 0) { myc.fk = 1; } else { double ak = 1; if (Nd > 1) { ak = Convert.ToDouble(kmeans.Ak(ik, Nd)); } myc.fk = myc.sk / (ak * AllClusters[ik - 1].sk); } AllClusters.Add(myc); } double min = double.MaxValue; int kk = 0; for (int ik = 1; ik <= 19; ik++) { if (AllClusters[ik].fk < min) { kk = AllClusters[ik].k; kmeans.clusters = AllClusters[ik].clusters; min = AllClusters[ik].fk; } } watch.Stop(); textBox1.Text = min.ToString(); txt_cluster.Text = kk.ToString(); var elapsedMs = watch.ElapsedMilliseconds; label3.Text = "with clustring time " + elapsedMs.ToString(); int t = 0; int i = 0; dataGridView1.Rows.Clear(); dataGridView1.Columns.Clear(); for (int j = 0; j < ListCases[0].GetFeatures().Count; j++) { Feature feature = (Feature)ListCases[0].GetFeatures()[j]; dataGridView1.Columns.Add(feature.GetFeatureName().ToString(), feature.GetFeatureName().ToString()); } dataGridView2.Rows.Clear(); dataGridView2.Columns.Clear(); dataGridView1.Rows.Add(ArrayCases.Count); dataGridView2.Columns.Add("cluster", "cluster"); dataGridView2.Columns.Add("count", "count"); dataGridView2.Rows.Add(kmeans.clusters.Count); foreach (List <Case> list in kmeans.clusters) // for each cluster, define a new centroid { kmeans.centroids[t].GetFeature("id").SetFeatureValue(t); standardize_current_cases.Add(kmeans.centroids[t]); dataGridView2.Rows[t].Cells[0].Value = t.ToString(); dataGridView2.Rows[t].Cells[1].Value = list.Count; mybestchoice.Cost_Function += kmeans.distances[t]; foreach (Case c in list) // Determine the poit on clusters { Case realcase = ListCases.Find( delegate(Case ca) { return(ca.GetFeature("id").GetFeatureValue() == c.GetFeature("id").GetFeatureValue()); } ); realcase.GetFeature("cluster").SetFeatureValue(t); c.GetFeature("cluster").SetFeatureValue(t); for (int j = 0; j < c.GetFeatures().Count; j++) { Feature f = (Feature)realcase.GetFeatures()[j]; dataGridView1.Rows[i].Cells[j].Value = f.GetFeatureValue(); } i++; } t++; } } else if (rbtn_kmeansPlus.Checked) { KmeansPlus mykp = new KmeansPlus(); KmeansPlus.PointClusters myclusters; List <KmeansPlus.PointClusters> Allmyclusters = new List <KmeansPlus.PointClusters>(); // myclusters = mykp.GetKMeansPP(standardize_current_cases,int.Parse( Clusters_num)); double min_fk = Double.MaxValue; int min_i = 0; for (int j = 0; j < 100; j++) { myclusters = new KmeansPlus.PointClusters(); myclusters = mykp.GetKMeansPP_K(standardize_current_cases); Allmyclusters.Add(myclusters); if (myclusters.Fk < min_fk) { min_i = j; } } myclusters = Allmyclusters[min_i]; // KmeansPlus mykp = new KmeansPlus(); // KmeansPlus.PointClusters myclusters =new KmeansPlus.PointClusters();; // myclusters = mykp.GetKMeansPP_K(standardize_current_cases); textBox1.Text = myclusters.Fk.ToString(); txt_cluster.Text = myclusters.PC.Count.ToString(); watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; label3.Text = "with clustring time " + elapsedMs.ToString(); foreach (Case centroid in myclusters.PC.Keys) { standardize_current_cases.Add(centroid); } // intialize db grids int t = 0; int i = 0; int countcluster = myclusters.PC.Count; dataGridView1.Rows.Clear(); dataGridView1.Columns.Clear(); for (int j = 0; j < ListCases[0].GetFeatures().Count; j++) { Feature feature = (Feature)ListCases[0].GetFeatures()[j]; dataGridView1.Columns.Add(feature.GetFeatureName().ToString(), feature.GetFeatureName().ToString()); } dataGridView2.Rows.Clear(); dataGridView2.Columns.Clear(); dataGridView1.Rows.Add(ArrayCases.Count); dataGridView2.Columns.Add("cluster", "cluster"); dataGridView2.Columns.Add("count", "count"); dataGridView2.Rows.Add(countcluster); foreach (List <Case> cluster in myclusters.PC.Values) { dataGridView2.Rows[t].Cells[0].Value = t.ToString(); dataGridView2.Rows[t].Cells[1].Value = cluster.Count; foreach (Case c in cluster) { Case realcase = ListCases.Find( delegate(Case ca) { return(ca.GetFeature("id").GetFeatureValue() == c.GetFeature("id").GetFeatureValue()); } ); realcase.GetFeature("cluster").SetFeatureValue(t); c.GetFeature("cluster").SetFeatureValue(t); for (int j = 0; j < c.GetFeatures().Count; j++) { Feature f = (Feature)realcase.GetFeatures()[j]; dataGridView1.Rows[i].Cells[j].Value = f.GetFeatureValue(); } i++; } // end feautres t++; } // end clusters } // end if else if (rbtn_nonekmeans.Checked) { dataGridView1.DataSource = Db.get_table_contetnt(mybestchoice.TableName); } // for sorting comboBox1.Items.Clear(); for (int i = 0; i < dataGridView1.Columns.Count; i++) { comboBox1.Items.Add(dataGridView1.Columns[i].HeaderText); } comboBox1.Text = "id"; dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; }