Example #1
0
        // -----------------------------------------------------------------------------------
        public Dictionary <Case, double[, ]> AHP(List <Case> Cases)
        {
            double[,] Non_Results;
            Dictionary <Case, double[, ]> Results = new Dictionary <Case, double[, ]>();

            for (int u = 0; u < exp_random_cases.Count; u++)
            {
                Case _problem = exp_random_cases[u];
                // Criteria Comparsions------------------------
                MyAHPModel model = new MyAHPModel();

                double[,] cri_arr = model.Create_Criteria_Comparison_Array(count_citeria);
                MyAHPModel model1 = new MyAHPModel(count_citeria, Cases.Count);
                model1.AddCriteria(cri_arr);
                model1.CalculatedCriteria();
                // Choices Comparsions------------------------
                bool     consistency = false;
                double[] choices     = new double[Cases.Count];
                double[] tempresult  = new double[Cases.Count];
                for (int uu = 0; uu < count_citeria; uu++)
                {
                    double[,] comp_choice = model1.CreateOne_CeiteriaChoiceComp(uu, Cases);
                    while (!consistency)
                    {
                        tempresult = model1.Calculated_One__Choic(out consistency, model1.CriteriaWeights[uu], comp_choice);
                    }
                    for (int h = 0; h < tempresult.GetLength(0); h++)
                    {
                        choices[h] += tempresult[h];
                    }
                }
                int ii = 0;
                Non_Results = new double[Cases.Count, 2];
                foreach (Case c in Cases)
                {
                    Non_Results[ii, 0] = Convert.ToDouble(c.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;
                        }
                    }
                }
                Results.Add(exp_random_cases[u], Non_Results);
            }
            return(Results);
        }
Example #2
0
 private void button19_Click_1(object sender, EventArgs e)
 {
     double[] CriteriaWeights;
     if (MyAHPModel._CalculatedCriteria(CriteriaArr, out CriteriaWeights))
     {
         for (int i = 0; i < dG_Prob.Columns.Count; i++)
         {
             dG_Prob.Rows[0].Cells[i].Value = Math.Round(CriteriaWeights[i] * 100, 2);
         }
     }
 }
Example #3
0
        private void button13_Click_2(object sender, EventArgs e)
        {
            int c;

            double[] ranks;
            int      dG_With = 0;

            ahp_sol_no_clustering = new MyAHPModel(CriteriaArr.GetLength(0), mybestchoice.Cases.Count);
            ahp_sol_no_clustering.AddCriteria(CriteriaArr);
            ahp_sol_no_clustering.AddCriterionRatedChoices(ChoicesArr_Non);
            if (ahp_sol_no_clustering.CalculatedCriteria())
            {
                ranks = ahp_sol_no_clustering.CalculatedChoices(out c);
                if (c != -1)
                {
                    MessageBox.Show("عدم تناسق في تفضيلات البدائل المدخلة للمعيار رقم" + c.ToString());
                    return;
                }
            }

            else
            {
                MessageBox.Show("عدم تناسق في تفضيلات المعايير المدخلة");
                return;
            }



            dg_results_no_clustering.Columns.Add("SolutionID", "Solution ID");
            dg_results_no_clustering.Columns.Add("SolutionWeight", "Solution Weight");
            dg_results_no_clustering.Columns.Add("SolutionSim", "Solution Similarity");
            for (int i = 0; i < ranks.Length; i++)
            {
                dg_results_no_clustering.Rows.Add();
                dg_results_no_clustering.Rows[dG_With].Cells[0].Value = mybestchoice.Cases[i].GetFeature("id").GetFeatureValue().ToString();
                dg_results_no_clustering.Rows[dG_With].Cells[1].Value = Math.Round(ranks[i] * 100, 2);
                EuclideanSimilarity s    = new EuclideanSimilarity();
                List <Case>         sols = Db.get_cases_condition(mybestchoice.Tablename_Standardization, mybestchoice.CaseName);
                dg_results_no_clustering.Rows[dG_With].Cells[2].Value = Math.Round(s.Similarity(sols[i], mybestchoice.StandrizeProblem) * 100, 2);
                dG_With++;
            }

            dg_results_no_clustering.Sort(dg_results_no_clustering.Columns["SolutionWeight"], ListSortDirection.Descending);
        }
Example #4
0
        //---------------------------------------------------------------------------------------
        public void FindSolutions_FuzzyAHP()
        {
            bool consistency;

            double[] choices;
            double[] tempresult;
            AllAhpCriteria     = new Dictionary <Case, double[]>();
            AllFAhpCriteria    = new Dictionary <Case, double[]>();
            AllAhpResults      = new Dictionary <Case, double[, ]>();
            AllFuzzyAhpResults = new Dictionary <Case, double[, ]>();
            List <double[, ]> tempahp;
            List <double[, ]> tempfahp;
            List <Case>       AhpArrCases;

            double[,] Ahp_Results;
            double[,] Fahp_Results;

            //---------------------------------------------------------------------------------
            for (int u = 0; u < exp_random_cases.Count; u++)
            {
                Case _problem = exp_random_cases[u];
                // Criteria Comparsions------------------------
                MyAHPModel model = new MyAHPModel(count_citeria);
                tempahp           = new List <double[, ]>();
                tempfahp          = new List <double[, ]>();
                double[,] cri_arr = model.Create_Criteria_Comparison_Array(count_citeria);

                //-------Criteria Weights----------------------------------------------------------------
                model.AddCriteria(cri_arr);
                double[] cri_wgh = new double[count_citeria];
                bool     res     = false;
                while (!res)
                {
                    if (model.CalculatedCriteria())
                    {
                        res = true;
                    }
                }
                cri_wgh = model.CriteriaWeights;
                //-------------------------------AHP--------------------------------------------------
                AhpArrCases = FindAlternative_WithClustering(_problem);
                if (AhpArrCases.Count != 0)
                {
                    // non clustering
                    MyAHPModel model1 = new MyAHPModel(count_citeria, AhpArrCases.Count);
                    model1.AddCriteria(cri_arr);
                    model1.CalculatedCriteria();
                    AllAhpCriteria.Add(exp_random_cases[u], model1.CriteriaWeights);
                    // Choices Comparsions------------------------
                    consistency = false;
                    choices     = new double[AhpArrCases.Count];
                    tempresult  = new double[AhpArrCases.Count];
                    for (int uu = 0; uu < count_citeria; uu++)
                    {
                        double[,] comp_choice = model1.CreateOne_CeiteriaChoiceComp(uu, AhpArrCases);
                        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;
                    Ahp_Results = new double[AhpArrCases.Count, 2];
                    foreach (Case cc in AhpArrCases)
                    {
                        Ahp_Results[ii, 0] = Convert.ToDouble(cc.GetFeature("id").GetFeatureValue());
                        Ahp_Results[ii, 1] = choices[ii] * 100;
                        ii++;
                    }
                    // sort Clustered
                    for (int i = 0; i < Ahp_Results.GetLength(0) - 1; i++)
                    {
                        for (int j = 0; j < Ahp_Results.GetLength(0) - i - 1; j++)
                        {
                            if (Ahp_Results[j, 1] < Ahp_Results[j + 1, 1])
                            {
                                double temp = Ahp_Results[j, 1];
                                Ahp_Results[j, 1]     = Ahp_Results[j + 1, 1];
                                Ahp_Results[j + 1, 1] = temp;
                                temp = Ahp_Results[j, 0];
                                Ahp_Results[j, 0]     = Ahp_Results[j + 1, 0];
                                Ahp_Results[j + 1, 0] = temp;
                            }
                            else if (Ahp_Results[j, 1] == Ahp_Results[j + 1, 1])
                            {
                                EuclideanSimilarity s = new EuclideanSimilarity();
                                int    num1           = Convert.ToInt32(Ahp_Results[j, 0]);
                                int    num2           = Convert.ToInt32(Ahp_Results[j + 1, 0]);
                                double sim1           = s.Similarity(AhpArrCases[num1], _problem);
                                double sim2           = s.Similarity(AhpArrCases[num2], _problem);
                                if (sim2 > sim1)
                                {
                                    double temp = Ahp_Results[j, 1];
                                    Ahp_Results[j, 1]     = Ahp_Results[j + 1, 1];
                                    Ahp_Results[j + 1, 1] = temp;
                                    temp = Ahp_Results[j, 0];
                                    Ahp_Results[j, 0]     = Ahp_Results[j + 1, 0];
                                    Ahp_Results[j + 1, 0] = temp;
                                }
                            }
                        }
                    }


                    // Fuzzy
                    MyFuzzyAHP model2 = new MyFuzzyAHP(count_citeria, AhpArrCases.Count);
                    model2.AddCriteria(cri_arr);
                    model2.CalculatedCriteria();
                    AllFAhpCriteria.Add(exp_random_cases[u], model2.CriteriaWeights);
                    // Choices Comparsions------------------------
                    consistency = false;
                    choices     = new double[AhpArrCases.Count];
                    tempresult  = new double[AhpArrCases.Count];
                    for (int uu = 0; uu < count_citeria; uu++)
                    {
                        double[,] comp_choice = model2.CreateOne_CeiteriaChoiceComp(uu, AhpArrCases);

                        while (!consistency)
                        {
                            tempresult = model2.Calculated_One__Choice(out consistency, model2.CriteriaWeights[uu], comp_choice);
                        }
                        consistency = false;
                        for (int h = 0; h < tempresult.GetLength(0); h++)
                        {
                            choices[h] += tempresult[h];
                        }
                    }
                    ii           = 0;
                    Fahp_Results = new double[AhpArrCases.Count, 2];
                    foreach (Case cc in AhpArrCases)
                    {
                        Fahp_Results[ii, 0] = Convert.ToDouble(cc.GetFeature("id").GetFeatureValue());
                        Fahp_Results[ii, 1] = choices[ii] * 100;
                        ii++;
                    }

                    // sort Clustered
                    for (int i = 0; i < Fahp_Results.GetLength(0) - 1; i++)
                    {
                        for (int j = 0; j < Fahp_Results.GetLength(0) - i - 1; j++)
                        {
                            if (Fahp_Results[j, 1] < Fahp_Results[j + 1, 1])
                            {
                                double temp = Fahp_Results[j, 1];
                                Fahp_Results[j, 1]     = Fahp_Results[j + 1, 1];
                                Fahp_Results[j + 1, 1] = temp;
                                temp = Fahp_Results[j, 0];
                                Fahp_Results[j, 0]     = Fahp_Results[j + 1, 0];
                                Fahp_Results[j + 1, 0] = temp;
                            }
                        }
                    }
                    AllAhpResults.Add(exp_random_cases[u], Ahp_Results);
                    AllFuzzyAhpResults.Add(exp_random_cases[u], Fahp_Results);
                }
            } //end if
        }     // end for
Example #5
0
        //=======================================================================================================================
        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 button11_Click_1(object sender, EventArgs e)
        {
            // display


            int dG_NonR = 0;

            dG_Non.Columns.Clear();



            //dG_Non.Columns.Add("Porblem No ", "Porblem No  ");
            dG_Non.Columns.Add("SolNum", "Sol Num  ");
            dG_Non.Columns.Add("Weight  ", "Weight  ");
            dG_Non.Columns.Add("Sim  ", "Sim  ");



            List <Case> solutions = statistics.FindAlternative_WithClustering(statistics.exp_random_cases[problem_num]);

            // call method
            listBox1.Items.Add("Finding Alternatices by specifying best cluster");

            double[] ranks;
            int      cri = 0;
            Dictionary <int, double[, ]> choices;

            double[,] criteriaarr;

            MyAHPModel myahp = new MyAHPModel(statistics.count_citeria, solutions.Count);

            criteriaarr = GenerateComparison.CriteriaComparisonMatrix;
            myahp.AddCriteria(criteriaarr);



            if (myahp.CalculatedCriteria())
            {
                choices = GenerateComparison.Create_All_Criteria_Choice_Comparison_Array(solutions);
                myahp.AddCriterionRatedChoices(choices);
                ranks = myahp.CalculatedChoices(out cri);
                for (int i = 0; i < ranks.Length; i++)
                {
                    dG_Non.Rows.Add();
                    dG_Non.Rows[dG_NonR].Cells[0].Value = solutions[i].GetFeature("id").GetFeatureValue().ToString();
                    dG_Non.Rows[dG_NonR].Cells[1].Value = Math.Round(ranks[i] * 100, 2);
                    EuclideanSimilarity s = new EuclideanSimilarity();
                    dG_Non.Rows[dG_NonR].Cells[2].Value = Math.Round(s.Similarity(solutions[i], statistics.exp_random_cases[problem_num]) * 100, 2);
                    dG_NonR++;
                }
            }
            dG_Non.Sort(dG_Non.Columns[1], ListSortDirection.Descending);

            // display Criteria
            if (dG_Prob.Columns.Count == 0)
            {
                dG_Prob.Columns.Add("Criteria", " Criteria ");
                dG_Prob.Columns.Add("AHP", "AHP");
                dG_Prob.Columns.Add("FuzzyAHP", "Fuzzy AHP");
                for (int i = 1; i < solutions[0].GetFeatures().Count - 1; i++)
                {
                    dG_Prob.Rows.Add();
                    Feature f = (Feature)solutions[0].GetFeatures()[i];
                    dG_Prob.Rows[i - 1].Cells[0].Value = f.GetFeatureName().ToString();
                    dG_Prob.Rows[i - 1].Cells[1].Value = Math.Round(myahp.CriteriaWeights[i - 1] * 100, 2);
                }
            }
            else
            {
                for (int i = 1; i < solutions[0].GetFeatures().Count - 1; i++)
                {
                    Feature f = (Feature)solutions[0].GetFeatures()[i];
                    dG_Prob.Rows[i - 1].Cells[1].Value = Math.Round(myahp.CriteriaWeights[i - 1] * 100, 2);
                }
            }
            dG_Prob.AutoResizeColumns();
            dG_Prob.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            dG_Non.AutoResizeColumns();
            dG_Non.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
        }