Beispiel #1
0
 private void tsbOpen_Click(object sender, EventArgs e)
 {
     TSPBenchmarkProblem.ImportATSPFile(true, true);                                                                 //from reference imported
     splitContainer2.Panel1.Invalidate();                                                                            //or refresh
     //reset pheromone map allocate memory
     pheromones              = new double[TSPBenchmarkProblem.numberOfObjects, TSPBenchmarkProblem.numberOfObjects]; //numberOfObjects = cities
     probability             = new double[TSPBenchmarkProblem.numberOfObjects];
     candidates              = new int[TSPBenchmarkProblem.numberOfObjects];
     txtKnownOptimalObj.Text = TSPBenchmarkProblem.minimalDistance.ToString();
     labBestRoute.Text       = "Best Route ( Number of cities: " + TSPBenchmarkProblem.numberOfObjects + " )";
     btnReset.Enabled        = true;
 }
Beispiel #2
0
 private void tsbOpen_Click(object sender, EventArgs e)
 {
     TSPBenchmarkProblem.ImportATSPFile(true, true); //from reference imported
     soFarTheBestRoute = new int[TSPBenchmarkProblem.numberOfObjects];
     splitContainer2.Panel1.Invalidate();            //or refresh
     numberOfCities = TSPBenchmarkProblem.numberOfObjects;
     if (TSPBenchmarkProblem.minimalDistance == 0)
     {
         txtKnownOptimalObj.Text = "n/a";
     }
     else
     {
         txtKnownOptimalObj.Text = TSPBenchmarkProblem.minimalDistance.ToString();
     }
     labBestRoute.Text = "Best Route ( Number of cities: " + TSPBenchmarkProblem.numberOfObjects + " )";
     btnReset.Enabled  = true;
 }
Beispiel #3
0
 private void ACOSolution_Paint(object sender, PaintEventArgs e)
 {
     TSPBenchmarkProblem.DrawCitesAndARoute(e.Graphics, ACO.Width, ACO.Height, null);
     if (theSolver != null | thePermutationGA != null)
     {
         if (rbtACOSolver.Checked)
         {
             TSPBenchmarkProblem.DrawCitesAndARoute(e.Graphics, ACO.Width,
                                                    ACO.Height, theSolver.SoFarTheBestSolution);
         }
         else
         {
             TSPBenchmarkProblem.DrawCitesAndARoute(e.Graphics, ACO.Width,
                                                    ACO.Height, thePermutationGA.SoFarTheBestSolution);
         }
     }
 }
Beispiel #4
0
        private void tsbOpen_Click(object sender, EventArgs e)
        {
            theSolver        = null;
            thePermutationGA = null;
            //open
            int status = TSPBenchmarkProblem.ImportATSPFile(true, true);

            if (status == -1)
            {
                return;
            }
            ACO.Refresh();

            title.Text     = $"Title: { TSPBenchmarkProblem.Name }";
            numCities.Text = $"Number Of Cities: { TSPBenchmarkProblem.NumberOfCities }";

            ShortestLen.Text        = $"Global Shortest Length: { TSPBenchmarkProblem.OptimumObjective:0.00}";
            txtGreedy.Text          = $"Greedy Route Length: {TSPBenchmarkProblem.ComputeRouteLength(TSPBenchmarkProblem.GetGreedyShortestRoute())}";
            rtbDataDescription.Text = TSPBenchmarkProblem.Comment;

            int[] cities = new int[TSPBenchmarkProblem.NumberOfCities];


            cities = TSPBenchmarkProblem.OptimalTour;
            if (cities != null)
            {
                rtbShort.AppendText("Global Shortest Route: \n");
                for (int c = 0; c < TSPBenchmarkProblem.NumberOfCities; c++)
                {
                    rtbShort.AppendText($"{cities[c]} ");
                }
            }
            cities = TSPBenchmarkProblem.GetGreedyShortestRoute();
            if (cities != null)
            {
                rtbGreedy.AppendText("Greedy Shortest Route: \n");
                for (int c = 0; c < TSPBenchmarkProblem.NumberOfCities; c++)
                {
                    rtbGreedy.AppendText($"{cities[c]} ");
                }
            }
            rbtCreateGA.Enabled  = true;
            rbtACOSolver.Enabled = true;
            btnReset.Enabled     = true;
        }
Beispiel #5
0
 private void splitContainer2_Panel1_Paint(object sender, PaintEventArgs e)                                                             //事件
 {
     TSPBenchmarkProblem.DrawCitiesOptimalRouteAndARoute(e.Graphics, e.ClipRectangle.Width, e.ClipRectangle.Height, soFarTheBestRoute); //都藏在e裡
 }
Beispiel #6
0
        private void RunOneIteration()
        {
            iterationBestObj = 1000000000;
            double iterationAverageObj = 0.0;
            int    iterationBestAnt    = 0;

            // all ants create their solution
            for (int k = 0; k < numberOfAnts; k++)
            {
                // initialize candidate set
                for (int j = 0; j < candidates.Length; j++)
                {
                    candidates[j] = j;
                }
                candidateNumber = TSPBenchmarkProblem.numberOfObjects;
                // First city is randomly selected
                ants[k][0] = rnd.Next(TSPBenchmarkProblem.numberOfObjects);
                int istar   = ants[k][0];
                int istarID = 0;
                // remove istar from candidates and pack candidates
                //find istar ID
                for (int i = 0; i < candidateNumber; i++)
                {
                    if (istar == candidates[i])
                    {
                        istarID = i;
                    }
                }
                //istar ID 後的全部往前移
                for (int i = istarID; i < candidateNumber - 1; i++)
                {
                    candidates[i] = candidates[i + 1];
                }
                candidateNumber--;
                //往下走
                int jstar = istar;
                for (int j = 1; j < TSPBenchmarkProblem.numberOfObjects; j++)
                {
                    for (int i = 0; i < candidateNumber; i++)
                    {
                        probability[i] = Math.Pow(pheromones[jstar, candidates[i]], alpha) *
                                         Math.Pow(1.0 / TSPBenchmarkProblem.fromToMatrix[jstar, candidates[i]], beta);
                    }
                    // get next jstar
                    int candidateID = 0;
                    if (radiobtnDeterministic.Checked == true)
                    {
                        candidateID = DeterministicSelect(probability, candidateNumber);
                    }
                    else if (radiobtnStochastic.Checked == true)
                    {
                        candidateID = StochasticSelect(probability, candidateNumber);
                    }
                    jstar      = candidates[candidateID];
                    ants[k][j] = jstar;
                    int jstarID = 0;
                    // remove jstar from candidates and pack candidates
                    //find jstar ID
                    for (int i = 0; i < candidateNumber; i++)
                    {
                        if (jstar == candidates[i])
                        {
                            jstarID = i;
                        }
                    }
                    //jstar ID 後的全部往前移
                    for (int i = jstarID; i < candidateNumber - 1; i++)
                    {
                        candidates[i] = candidates[i + 1];
                    }
                    candidateNumber--;
                }

                // evaluate objectives
                objectives[k]        = TSPBenchmarkProblem.ComputeObjectiveValue(ants[k]);
                iterationAverageObj += objectives[k];

                // update iteration best
                if (objectives[k] < iterationBestObj)
                {
                    iterationBestObj = objectives[k];
                    iterationBestAnt = k;
                }

                // update pheromone map
                for (int j = 0; j < TSPBenchmarkProblem.numberOfObjects - 1; j++)
                {
                    int r = ants[k][j];
                    int c = ants[k][j + 1];
                    pheromones[r, c] += droppingAmount / objectives[k];
                }
                for (int r = 0; r < TSPBenchmarkProblem.numberOfObjects; r++)
                {
                    for (int c = 0; c < TSPBenchmarkProblem.numberOfObjects; c++)
                    {
                        pheromones[r, c] *= evaporateRate;
                    }
                }
                funtionCallCount++;
                txtNumberOfFunctionCalls.Text = funtionCallCount.ToString();
            }

            //update so far the best
            if (iterationBestObj < soFarTheBestObj)
            {
                soFarTheBestObj = iterationBestObj;
                for (int j = 0; j < TSPBenchmarkProblem.numberOfObjects; j++)
                {
                    soFarTheBestRoute[j] = ants[iterationBestAnt][j];
                }
                soFarTheBestInterationID = iterationCount;
                txtSoFarTheBest.Text     = soFarTheBestObj.ToString();

                //畫圖
                splitContainer2.Panel1.Refresh();
            }
            else
            {
                iterationWithoutImprovementCount++;
            }

            //畫線
            iterationAverageObj /= numberOfAnts;
            averageLine.Points.AddXY(iterationCount, iterationAverageObj);
            iterationBestLine.Points.AddXY(iterationCount, iterationBestObj);
            soFarTheBestLine.Points.AddXY(iterationCount, soFarTheBestObj);
        }
Beispiel #7
0
        void ResetACO()
        {
            //clear
            iterationCount = 0;
            iterationWithoutImprovementCount = 0;
            funtionCallCount = 0;
            chartObj.Series.Clear();

            //get UI
            numberOfAnts            = Convert.ToInt16(txtNumberOfAnts.Text);
            alpha                   = Convert.ToDouble(txtPheromoneFacter.Text);
            beta                    = Convert.ToDouble(txtHeuristicFactor.Text);
            initialValueOfPheromome = Convert.ToDouble(txtInitialPheromoneValue.Text);
            evaporateRate           = Convert.ToDouble(txtEvaporationRate.Text);
            droppingAmount          = Convert.ToDouble(txtDroppingAmount.Text);

            //setUI
            txtSoFarTheBest.Text          = "";
            txtIterationID.Text           = "0";
            txtNumberOfFunctionCalls.Text = "0";
            txtAtIteration.Text           = "0";
            txtNumberOfFunctionCalls.Text = "0";

            //initailize pheromone map
            for (int i = 0; i < pheromones.GetLength(0); i++)
            {
                for (int j = 0; j < pheromones.GetLength(1); j++)
                {
                    pheromones[i, j] = initialValueOfPheromome;
                }
            }

            //allocate ants
            ants       = new int[numberOfAnts][];
            objectives = new double[numberOfAnts];
            for (int i = 0; i < numberOfAnts; i++)
            {
                ants[i] = new int[TSPBenchmarkProblem.numberOfObjects];
                for (int j = 0; j < TSPBenchmarkProblem.numberOfObjects; j++)
                {
                    ants[i][j] = j;
                }
                shuffleRoute(ants[i]);
                objectives[i] = TSPBenchmarkProblem.ComputeObjectiveValue(ants[i]);
            }

            //so far the best route
            iterationBestObj = 100000000;
            int bestID = 0;

            for (int i = 0; i < numberOfAnts; i++)
            {
                if (objectives[i] < iterationBestObj)
                {
                    objectives[i] = iterationBestObj;
                    bestID        = i;
                }
            }
            soFarTheBestObj   = iterationBestObj;
            soFarTheBestRoute = new int[TSPBenchmarkProblem.numberOfObjects];
            for (int j = 0; j < TSPBenchmarkProblem.numberOfObjects; j++)
            {
                soFarTheBestRoute[j] = ants[bestID][j];
            }
            //txtSoFarTheBest.Text = soFarTheBestObj.ToString();

            //chart setting
            averageLine                  = new Series();
            averageLine.ChartType        = SeriesChartType.Line;
            averageLine.Name             = "Average";
            iterationBestLine            = new Series();
            iterationBestLine.ChartType  = SeriesChartType.Line;
            iterationBestLine.Name       = "Iteration Best";
            soFarTheBestLine             = new Series();
            soFarTheBestLine.ChartType   = SeriesChartType.Line;
            soFarTheBestLine.Name        = "So Far the Best";
            soFarTheBestLine.BorderWidth = 3;
            chartObj.Series.Add(averageLine);
            chartObj.Series.Add(iterationBestLine);
            chartObj.Series.Add(soFarTheBestLine);

            //protection
            btnRunOneIteration.Enabled = true;
            btnRunToEnd.Enabled        = true;
        }
Beispiel #8
0
        void ResetACO()
        {
            //clear
            iterationCount     = 0;
            NoImprovementCount = 0;
            chartObj.Series.Clear();

            //get UI
            numberOfHM = Convert.ToInt16(txtNumberOfAnts.Text);
            HMCR       = Convert.ToDouble(txtHMCR.Text);
            PAR        = Convert.ToDouble(txtPAR.Text);

            //setUI
            txtSoFarTheBest.Text = "";
            txtIterationID.Text  = "0";
            txtNumberOfIterationWithoutImprovement.Text = "0";
            progressBar1.Value = 0;

            //allocate HM
            HSforTSP = new HarmonySearch(numberOfHM, numberOfCities);
            HSforTSP.iterationWithoutImprovementCount = 0;
            for (int i = 0; i < numberOfHM; i++)
            {
                for (int j = 0; j < numberOfCities; j++)
                {
                    HSforTSP.HM[i][j] = rnd.NextDouble(); //random 0~1
                }
            }
            //temp = 排序用
            double[][] temp = new double[numberOfHM][];
            for (int i = 0; i < numberOfHM; i++)
            {
                temp[i] = new double[numberOfCities];
                for (int j = 0; j < numberOfCities; j++)
                {
                    temp[i][j] = HSforTSP.HM[i][j];
                }
            }
            //initialize realRoute
            realRoute = new int[numberOfHM][];
            for (int i = 0; i < numberOfHM; i++)
            {
                realRoute[i] = new int[numberOfCities];
                for (int j = 0; j < numberOfCities; j++)
                {
                    realRoute[i][j] = j;
                }
            }
            //convert to real route and calculate evaluation in this step
            for (int i = 0; i < numberOfHM; i++)
            {
                Array.Sort(temp[i], realRoute[i]);
                HSforTSP.evaluation[i] = TSPBenchmarkProblem.ComputeObjectiveValue(realRoute[i]);
            }
            HSforTSP.CountPenalty(500);

            //so far the best route
            HSforTSP.Sort();
            soFarTheBestObj   = HSforTSP.evaluation[HSforTSP.BestID];
            soFarTheBestRoute = new int[TSPBenchmarkProblem.numberOfObjects];
            for (int j = 0; j < TSPBenchmarkProblem.numberOfObjects; j++)
            {
                soFarTheBestRoute[j] = realRoute[HSforTSP.BestID][j];
            }

            //chart setting
            averageLine                  = new Series();
            averageLine.ChartType        = SeriesChartType.Line;
            averageLine.Name             = "Average";
            soFarTheBestLine             = new Series();
            soFarTheBestLine.ChartType   = SeriesChartType.Line;
            soFarTheBestLine.Name        = "So Far the Best";
            soFarTheBestLine.BorderWidth = 3;
            chartObj.Series.Add(averageLine);
            chartObj.Series.Add(soFarTheBestLine);

            //protection
            btnRunOneIteration.Enabled = true;
            btnRunToEnd.Enabled        = true;
        }
Beispiel #9
0
        private void RunOneIteration()
        {
            //Step2 : improvise new vector
            double[] newV = new double[numberOfCities];
            for (int j = 0; j < numberOfCities; j++)
            {
                if (rnd.NextDouble() < HMCR)               //有0.99的機會從中選一個
                {
                    int selectedHM = rnd.Next(numberOfHM); //0~20
                    if (rnd.NextDouble() < (1 - PAR))      //有0.95的機率選一個
                    {
                        newV[j] = HSforTSP.HM[selectedHM][j];
                    }
                    else //有0.05的機率往上/往下
                    {
                        if (rnd.NextDouble() < 0.5) //有一半機率往上
                        {
                            newV[j] = HSforTSP.HM[selectedHM][j] + 1.0 / numberOfCities;
                        }
                        else //一半機率往下
                        {
                            newV[j] = HSforTSP.HM[selectedHM][j] - 1.0 / numberOfCities;
                        }
                    }
                }
                else
                {
                    newV[j] = rnd.NextDouble();
                }
            }
            //calculate the evaluation of newVector
            double[] temp            = new double[numberOfCities]; //排序用
            int[]    realRouteOfNewV = new int[numberOfCities];
            for (int j = 0; j < numberOfCities; j++)
            {
                temp[j]            = newV[j];
                realRouteOfNewV[j] = j;
            }
            Array.Sort(temp, realRouteOfNewV);
            double evaluationOfNewV = TSPBenchmarkProblem.ComputeObjectiveValue(realRouteOfNewV);

            //Step 3: update HM
            HSforTSP.UpdateHM(newV, evaluationOfNewV);
            HSforTSP.CountPenalty(500);

            //average
            double iterationAverageObj = 0;

            for (int i = 0; i < numberOfHM; i++)
            {
                iterationAverageObj += HSforTSP.evaluation[i];
            }
            iterationAverageObj /= numberOfHM;

            //update so far the best
            for (int j = 0; j < numberOfCities; j++)
            {
                temp[j] = HSforTSP.HM[HSforTSP.BestID][j];
                soFarTheBestRoute[j] = j;
            }
            Array.Sort(temp, soFarTheBestRoute);
            soFarTheBestObj = HSforTSP.evaluation[HSforTSP.BestID];

            //UI
            progressBar1.Value = iterationCount;
            NoImprovementCount = HSforTSP.iterationWithoutImprovementCount;
            txtNumberOfIterationWithoutImprovement.Text = NoImprovementCount.ToString();
            txtIterationID.Text = iterationCount.ToString();
            txtNumberOfIterationWithoutImprovement.Text = HSforTSP.iterationWithoutImprovementCount.ToString();
            txtSoFarTheBest.Text = soFarTheBestObj.ToString();

            //畫線
            averageLine.Points.AddXY(iterationCount, iterationAverageObj);
            soFarTheBestLine.Points.AddXY(iterationCount, HSforTSP.evaluation[HSforTSP.BestID]);
            if (checkBoxShowAnimation.Checked == true)
            {
                if (iterationCount % 100 == 0)
                {
                    splitContainer2.Panel1.Refresh();
                }
                txtNumberOfIterationWithoutImprovement.Refresh();
                txtIterationID.Refresh();
                txtSoFarTheBest.Refresh();
                chartObj.Update();
            }
        }
Beispiel #10
0
 private void Greedy_Paint(object sender, PaintEventArgs e)
 {
     TSPBenchmarkProblem.DrawCitiesOptimalRouteAndARoute(e.Graphics, Greedy.Width,
                                                         Greedy.Height, TSPBenchmarkProblem.GetGreedyShortestRoute());
 }
Beispiel #11
0
 private void Optimal_Paint(object sender, PaintEventArgs e)
 {
     TSPBenchmarkProblem.DrawCitiesOptimalRouteAndARoute(e.Graphics, Optimal.Width,
                                                         Optimal.Height, TSPBenchmarkProblem.OptimalTour);
 }