Пример #1
0
        public override int[] GetSolution()
        {
            QapSolution solution = new QapSolution
            {
                Size     = Data.Distances.Length,
                Solution = this.GetList(this.GetRandomInitSolution())
            };

            FirstSolution = solution.Solution.ToArray();
            DeltaSolutionBenchmark benchmark = new DeltaSolutionBenchmark(Data, solution);

            CheckedElems = 0;
            bool isLocalMinimum = false;

            while (!isLocalMinimum)
            {
                if (!CheckBestNeighbor(benchmark))
                {
                    isLocalMinimum = true;
                }
            }
            CheckedElems = Steps * Data.Size * (Data.Size - 1);
            //Steps = benchmark.SwapCounter;
            return(benchmark.ActualBestSolution.Solution.ToArray());
        }
        public DeltaSolutionBenchmark(QapData data, QapSolution solution)
        {
            QapSolutionBenchmark bench = new QapSolutionBenchmark();

            ActualBestSolution = solution;
            Data        = data;
            SwapCounter = 0;
            ActualBestSolution.Score = Convert.ToInt32(bench.RateSolution(solution.Solution.ToArray(), data));
            CalcDeltaTable();
        }
Пример #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());
        }
Пример #4
0
        private void runSolver(QapSolver solver, QapData data, QapSolutionBenchmark benchmark, QapSolution optimalSolution, String outputFileName)
        {
            List <long> solutionsScore    = new List <long>();
            ulong       bestSolutionScore = 0;
            //LocalOptimumValidator validator = new LocalOptimumValidator();
            List <ulong[]> list = new List <ulong[]>();

            for (int i = 0; i < RepetitionsNo; i++)
            {
                var lastSolution      = solver.GetSolution();
                var lastSolutionScore = benchmark.RateSolution(lastSolution, data);
                if ((bestSolutionScore == 0) || (bestSolutionScore > lastSolutionScore))
                {
                    bestSolutionScore = lastSolutionScore;
                }
                solutionsScore.Add(((long)lastSolutionScore));

                list.Add(new ulong[] { Convert.ToUInt64(optimalSolution.Score), lastSolutionScore, (ulong)solutionsScore.Average(), bestSolutionScore });
            }

            using (StreamWriter file = File.AppendText(outputFileName))
            {
                foreach (ulong[] line in list)
                {
                    file.WriteLine(line[0].ToString() + ',' + line[1].ToString() + ',' + line[2].ToString() + ',' + line[3].ToString() + ';');
                }
            }
        }
Пример #5
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);
        }
Пример #6
0
        private void runSolver(QapSolver solver, QapData data, QapSolutionBenchmark benchmark, QapSolution optimalSolution, String outputFileName)
        {
            //LocalOptimumValidator validator = new LocalOptimumValidator();
            List <ulong[]> list = new List <ulong[]>();

            for (int i = 0; i < RepetitionsNo; i++)
            {
                var lastSolution = solver.GetSolution();

                //Console.WriteLine(validator.CheckLocalOptimum(lastSolution, data, false));
                var firstSolution     = solver.FirstSolution;
                var lastSolutionScore = benchmark.RateSolution(lastSolution, data);
                //Console.WriteLine("last : "+lastSolutionScore);
                var firstSolutionScore = benchmark.RateSolution(firstSolution, data);

                list.Add(new ulong[] { Convert.ToUInt64(optimalSolution.Score), firstSolutionScore, lastSolutionScore });
            }

            using (StreamWriter file = File.AppendText(outputFileName))
            {
                foreach (ulong[] line in list)
                {
                    file.WriteLine(line[0].ToString() + ',' + line[1].ToString() + ',' + line[2].ToString() + ';');
                }
            }
        }
Пример #7
0
        private void RunSolver(QapSolver solver, QapData data, QapSolutionBenchmark benchmark, QapSolution optimalSolution, String outputFileName)
        {
            ulong           bestSolutionScore          = 0;
            int             repetitionsWithoutProgress = 0;
            List <double[]> list = new List <double[]>();
            Stopwatch       sw   = new Stopwatch();

            for (int i = 0; i < Repetitions; i++)
            {
                while (true)
                {
                    sw.Start();
                    var lastSolution = solver.GetSolution();
                    sw.Stop();
                    var lastSolutionScore = benchmark.RateSolution(lastSolution, data);
                    if ((bestSolutionScore == 0) || (bestSolutionScore > lastSolutionScore))
                    {
                        bestSolutionScore          = lastSolutionScore;
                        repetitionsWithoutProgress = 0;
                    }
                    else
                    {
                        repetitionsWithoutProgress++;
                    }
                    var efficiency = (double)(ulong)optimalSolution.Score / bestSolutionScore;
                    var time       = sw.Elapsed.TotalMilliseconds;
                    list.Add(new double[] { efficiency, time });

                    if (repetitionsWithoutProgress > MaxRepetitionsWithoutImprove)
                    {
                        break;
                    }
                    if (efficiency > 0.99999999999999999999)
                    {
                        break;
                    }
                }
            }
            using (StreamWriter file = File.AppendText(outputFileName))
            {
                foreach (double[] line in list)
                {
                    file.WriteLine(line[0].ToString() + ';' + line[1].ToString() + ';');
                }
            }
        }