Beispiel #1
0
        public Values buildsolution(double alpha, double beta, Values val)
        {
            int i, j;
            double probability = 0, numerator = 0, denominator = 0, rndvalue;
            Random rnd = new Random();
            val.taboo = new int[15];
            double[] range = new double[7] { 0, 0, 0, 0, 0, 0, 0 };

            for (j = 0; j < 15; j++)
            {
                range[0] = 0;
                for (i = 0; i < 6; i++)
                {
                    numerator = caculatenumerator(i, j, alpha, beta, val);
                    denominator = caculatedenominator(i, j, alpha, beta, val);
                    probability = numerator / denominator;
                    range[i+1] = range[i] + probability;
                }
                range[6] = 1;

                rndvalue = rnd.NextDouble();
                for (i = 1; i < 7; i++)
                {
                    if (rndvalue < range[i] && rndvalue > range[i - 1])
                    {
                        val.taboo[j] = i-1;
                        break;
                    }
                }
            }
            return val;
        }
Beispiel #2
0
 public double caculatedenominator(int i, int j, double alpha, double beta, Values val)
 {
     double denominator = 0;
     for (int k = 0; k < 6; k++)
     {
         denominator += ((Math.Pow(val.tablepheromones[i, j], alpha)) * (Math.Pow(val.tablevisibility[i, j], beta)));
     }
     return denominator;
 }
Beispiel #3
0
 public Values updatepheromone(double rho, Values val)
 {
     double numerator = 0, denominator = 0;
     for (int i = 0; i < 15; i++)
     {
         numerator = val.tablepheromones[val.bestsolution[i], i];
         denominator = val.tablepheromones[val.bestsolutiontotal[i], i];
         val.tablepheromones[val.bestsolutiontotal[i], i] = (1 - rho) * val.tablepheromones[val.bestsolutiontotal[i], i] + denominator / numerator;
     }
     return val;
 }
Beispiel #4
0
        public Values initializearrays(int[,] table)
        {
            Values val = new Values();
            val.tablevisibility = new double[6, 15];
            val.tablepheromones = new double[6, 15];
            int i, j;

            for (i = 0; i < 6; i++)
            {
                for (j = 0; j < 15; j++)
                {
                    val.tablevisibility[i, j] = 1 / (double)table[i, j];
                    val.tablepheromones[i, j] = 0.01;
                }
            }
            return val;
        }
Beispiel #5
0
 public double caculatenumerator(int i, int j, double alpha, double beta, Values val)
 {
     double numerator = 0;
     numerator = ((Math.Pow(val.tablepheromones[i, j], alpha)) * (Math.Pow(val.tablevisibility[i, j], beta)));
     return numerator;
 }
Beispiel #6
0
        private void button1_Click(object sender, EventArgs e)
        {
            ACO objACO = new ACO();
            Values val = new Values();
            val.tablevisibility = new double[6, 15];
            val.tablepheromones = new double[6, 15];
            int i, j, k, flag = 0;

            int[,] table = new int[6, 15];
            string x;
            for (i = 0; i < 6; i++)
            {
                for (j = 0; j < 15; j++)
                {
                    x = dataGridView1.Rows[i].Cells[j].Value.ToString();
                    table[i, j] = int.Parse(x);
                }
            }
            val = objACO.initializearrays(table);
            richTextBox1.Clear();
            AppendText(this.richTextBox1, Color.Black, "Table of Visibility\n".ToString());
            for (i = 0; i < 6; i++)
            {
                for (j = 0; j < 15; j++)
                {
                    AppendText(this.richTextBox1, Color.Black, String.Format("{0:0.0000}", val.tablevisibility[i, j]) + "     ".ToString());
                    if (j / 14 == 1) AppendText(this.richTextBox1, Color.Black, '\n'.ToString());
                }
            }

            int sumbesttotal = 0, sumbest = 0, sumnormal = 0, n;
            int[] psumbest = new int[6] { 0, 0, 0, 0, 0, 0 };
            int[] psumnormal = new int[6] { 0, 0, 0, 0, 0, 0 };
            int[] psumbesttotal = new int[6] { 0, 0, 0, 0, 0, 0 };
            int big, bigger, biggest, bestsolution = 0;
            for (n = 0; n < numericUpDown5.Value; n++)
            {
                for (i = 0; i < numericUpDown4.Value; i++)
                {
                    val = objACO.buildsolution((double)numericUpDown1.Value, (double)numericUpDown2.Value, val);
                    if (i == 0)
                    {
                        val.bestsolution = val.taboo;
                        if (n == 0)
                        {
                            val.bestsolutiontotal = val.bestsolution;
                        }
                    }
                    else
                    {
                        sumbesttotal = sumbest = sumnormal = big = bigger = biggest = 0;
                        for (j = 0; j < 6; j++)
                        {
                            psumnormal[j] = psumbest[j] = psumbesttotal[j] = 0;
                        }
                        for (j = 0; j < 15; j++)
                        {
                            sumbesttotal = sumbesttotal + table[val.bestsolutiontotal[j], j];
                            sumbest = sumbest + table[val.bestsolution[j], j];
                            sumnormal = sumnormal + table[val.taboo[j], j];

                            psumbesttotal[val.bestsolutiontotal[j]] = psumbesttotal[val.bestsolutiontotal[j]] + table[val.bestsolutiontotal[j], j];
                            psumbest[val.bestsolution[j]] = psumbest[val.bestsolution[j]] + table[val.bestsolution[j], j];
                            psumnormal[val.taboo[j]] = psumnormal[val.taboo[j]] + table[val.taboo[j], j];
                        }
                        for (j = 0; j < 6; j++)
                        {
                            if (j == 0)
                            {
                                biggest = psumbesttotal[j];
                                bestsolution = biggest;
                                bigger = psumbest[j];
                                big = psumnormal[j];
                            }
                            else
                            {
                                if (bigger < psumbest[j])
                                {
                                    bigger = psumbest[j];
                                }
                                if (biggest < psumbesttotal[j])
                                {
                                    biggest = psumbesttotal[j];
                                    bestsolution = biggest;
                                }
                                if (big < psumnormal[j])
                                {
                                    big = psumnormal[j];
                                }
                            }
                        }
                        if (big < bigger)
                        {
                            val.bestsolution = val.taboo;
                        }
                        if (bigger < biggest)
                        {
                            val.bestsolutiontotal = val.bestsolution;
                        }
                    }
                    if (i == numericUpDown4.Value - 1)
                    {
                        if (flag == 0)
                        {
                            AppendText(this.richTextBox1, Color.Black, "\n\nTable of Pheromones\n".ToString());
                            for (j = 0; j < 6; j++)
                            {
                                for (k = 0; k < 15; k++)
                                {
                                    AppendText(this.richTextBox1, Color.Black, String.Format("{0:00.000}", val.tablepheromones[j, k]) + "     ".ToString());
                                    if (k / 14 == 1) AppendText(this.richTextBox1, Color.Black, '\n'.ToString());
                                }
                            }
                            AppendText(this.richTextBox1, Color.Black, "\n\nBest Solution (Generation)\n".ToString());
                            for (j = 0; j < 15; j++)
                            {
                                AppendText(this.richTextBox1, Color.Black, "(T"+(j+1)+", P"+(val.bestsolution[j]+1)+") ".ToString());
                            }
                            AppendText(this.richTextBox1, Color.Black, "\n\nTotal time:" + sumbest.ToString());
                            AppendText(this.richTextBox1, Color.Black, "\n\nFinal Time:" + psumbest.Max() + '\n');
                            flag = 1;
                        }
                        else
                        {
                            AppendText(this.richTextBox1, Color.DarkBlue, "\n\nTable of Pheromones\n".ToString());
                            for (j = 0; j < 6; j++)
                            {
                                for (k = 0; k < 15; k++)
                                {
                                    AppendText(this.richTextBox1, Color.DarkBlue, String.Format("{0:0.000}", val.tablepheromones[j, k]) + "     ".ToString());
                                    if (k / 14 == 1) AppendText(this.richTextBox1, Color.DarkBlue, '\n'.ToString());
                                }
                            }
                            AppendText(this.richTextBox1, Color.DarkBlue, "\n\nBest Solution (Generation)\n".ToString());
                            for (j = 0; j < 15; j++)
                            {
                                AppendText(this.richTextBox1, Color.DarkBlue, "(T" + (j + 1) + ", P" + (val.bestsolution[j] + 1) + ") ".ToString());
                            }
                            AppendText(this.richTextBox1, Color.DarkBlue, "\n\nTotal time:" + sumbest.ToString());
                            AppendText(this.richTextBox1, Color.DarkBlue, "\n\nFinal Time:" + psumbest.Max() + '\n');
                            flag = 0;
                        }
                    }
                }
                objACO.updatepheromone((double)numericUpDown3.Value, val);
            }

            AppendText(this.richTextBox1, Color.Red, "\nBEST SOLUTION\n".ToString());
            for (i = 0; i < 6; i++)
            {
                AppendText(this.richTextBox1, Color.Black, "P" + (i+ 1) + ": ".ToString());
                for (j = 0; j < 15; j++)
                {
                    if (i == val.bestsolutiontotal[j])
                    {
                        AppendText(this.richTextBox1, Color.Black, "(T" + (j + 1) + ") ".ToString());
                    }
                }
                AppendText(this.richTextBox1, Color.Black, "\n".ToString());
                //AppendText(this.richTextBox1, Color.Black, "(T" + (j + 1) + ", P" + (val.bestsolution[j] + 1) + ") ".ToString());
            }
            AppendText(this.richTextBox1, Color.Black, "Best Time:"+bestsolution.ToString());
            MessageBox.Show("End of the Process");
        }