Exemple #1
0
        private bool CheckBestNeighbor(DeltaSolutionBenchmark benchmark)
        {
            int bestI     = -1;
            int bestJ     = -1;
            int bestScore = benchmark.ActualBestSolution.Score;

            for (int i = 0; i < benchmark.ActualBestSolution.Size - 1; i++)
            {
                for (int j = i + 1; j < benchmark.ActualBestSolution.Size; j++)
                {
                    int neighborScore = benchmark.RateSolutionChange(i, j);
                    if (neighborScore < bestScore)
                    {
                        bestScore = neighborScore;
                        bestI     = i;
                        bestJ     = j;
                    }
                }
            }
            Steps++;
            if (bestJ == -1 || bestI == -1)
            {
                return(false);
            }
            else
            {
                benchmark.ChangeSolution(bestI, bestJ);
                return(true);
            }
        }
Exemple #2
0
 private bool CheckIfBetterNeighborExist(DeltaSolutionBenchmark benchmark)
 {
     for (int i = 0; i < benchmark.ActualBestSolution.Size - 1; i++)
     {
         for (int j = i + 1; j < benchmark.ActualBestSolution.Size; j++)
         {
             CheckedElems++;
             if (benchmark.CheckIfSolutionChangeIsBetter(i, j))
             {
                 benchmark.ChangeSolution(i, j);
                 Steps++;
                 return(true);
             }
         }
     }
     return(false);
 }
Exemple #3
0
        public override int[] GetSolution()
        {
            var size = this.Data.Size;

            QapSolution solution = new QapSolution
            {
                Size     = Data.Distances.Length,
                Solution = this.GetList(this.GetRandomInitSolution())
            };

            FirstSolution = solution.Solution.ToArray();
            DeltaSolutionBenchmark benchmark = new DeltaSolutionBenchmark(Data, solution);
            QapSolutionBenchmark   solBen    = new QapSolutionBenchmark();
            var solutions     = new SortedList <int, Tuple <int, int> >(new DuplicateKeyComparer <int>());
            var currSolution  = solution.Solution.ToArray();
            var bestScore     = benchmark.ActualBestSolution.Score;
            var bestSolution  = new List <int>();
            int changeCounter = 0;

            Steps = 0;

            while (changeCounter < 20)
            {
                Steps++;
                changeCounter++;
                solutions.Clear();
                for (int i = 0; i < benchmark.ActualBestSolution.Size - 1; i++)
                {
                    for (int j = i + 1; j < benchmark.ActualBestSolution.Size; j++)
                    {
                        int neighborScore = benchmark.RateSolutionChange(i, j);
                        solutions.Add(neighborScore, Tuple.Create(i, j));
                        CheckedElems++;
                    }
                }

                bool changed = false;
                foreach (var entry in solutions)
                {
                    //if legal
                    var x             = entry.Value.Item1;
                    var y             = entry.Value.Item2;
                    var proposedScore = entry.Key;
                    if (Memory[x][y] == 0 || proposedScore < bestScore)
                    {
                        if (Memory[x][y] == 0 && proposedScore < bestScore)
                        {
                            changeCounter = 0;
                        }
                        Memory[x][y] = LengthOfMemory;
                        Memory[y][x] = LengthOfMemory;

                        benchmark.ChangeSolution(x, y);
                        //swap
                        int temp = currSolution[x];
                        currSolution[x] = currSolution[y];
                        currSolution[y] = temp;
                        changed         = true;
                        if (benchmark.ActualBestSolution.Score < bestScore)
                        {
                            bestSolution = benchmark.ActualBestSolution.Solution;
                            bestScore    = benchmark.ActualBestSolution.Score;
                        }

                        break;
                    }
                }
                if (!changed)
                {
                    //taki co najmniej psuje
                    var x = solutions.Values[0].Item1;
                    var y = solutions.Values[0].Item2;

                    Memory[x][y] = LengthOfMemory;
                    Memory[y][x] = LengthOfMemory;

                    benchmark.ChangeSolution(x, y);
                    //swap
                    int temp = currSolution[x];
                    currSolution[x] = currSolution[y];
                    currSolution[y] = temp;
                }
                TickMemoryDown();
            }
            return(bestSolution.ToArray());
        }
Exemple #4
0
        public override int[] GetSolution()
        {
            var currSolution = this.GetRandomInitSolution();
            //var currScore = this.SolutionBenchmark.RateSolution(currSolution, Data);
            int         currScore = 0;
            QapSolution solution  = new QapSolution
            {
                Size     = Data.Distances.Length,
                Solution = this.GetList(currSolution)
            };

            FirstSolution = solution.Solution.ToArray();
            DeltaSolutionBenchmark benchmark = new DeltaSolutionBenchmark(Data, solution);
            int bestEver = Int32.MaxValue;

            TempZero = currSolution.Length * 800;
            Temp     = TempZero;
            TempMin  = 1;
            Steps    = 0;
            while (Temp > TempMin)
            {
                for (int i = 0; i < 10; i++)
                {
                    int x = this.Rnd.Rnd.Next(0, currSolution.Length);
                    int y = this.Rnd.Rnd.Next(0, currSolution.Length);

                    currScore = benchmark.ActualBestSolution.Score;
                    var proposedScore = benchmark.RateSolutionChange(x, y);
                    if (proposedScore < bestEver)
                    {
                        bestEver = proposedScore;
                    }
                    if (AcceptSolution(proposedScore, currScore, Temp))
                    {
                        benchmark.ChangeSolution(x, y);
                        //swap
                        int temp = currSolution[x];
                        currSolution[x] = currSolution[y];
                        currSolution[y] = temp;
                        Steps++;
                    }
                    this.CheckedElems++;
                }
                if (Temp < 200)
                {
                    UpdateTemp();
                }
                else if (Temp < 10000)
                {
                    UpdateTemp3();
                }
                else
                {
                    UpdateTemp4();
                }
                //UpdateTemp();

                //Console.WriteLine(Temp);
            }
            //Console.WriteLine(bestEver);
            return(currSolution);
        }