Пример #1
0
        public DiscreteTS4QAP(QAPInstance instance, double rclTreshold, 
		                       int tabuListLength, int neighborChecks)
            : base(tabuListLength, neighborChecks)
        {
            Instance = instance;
            RclTreshold = rclTreshold;
        }
Пример #2
0
        public DiscreteSA4QAP(QAPInstance instance, int initialSolutions, 
		                      int levelLength, double tempReduction)
            : base(initialSolutions, levelLength, tempReduction)
        {
            Instance = instance;
            generatedSolutions = 0;
        }
        public MaxMinAntSystem2OptBest4QAP(QAPInstance instance, int numberAnts, double rho, 
		                                   double alpha, double beta, int maxReinit)
            : base(instance.NumberFacilities, QAPUtils.Fitness(instance, QAPUtils.RandomSolution(instance)),
			       numberAnts, rho, alpha, beta, maxReinit)
        {
            Instance = instance;
        }
Пример #4
0
 public DiscretePSO4QAP(QAPInstance instance, int partsCount, double prevConf,
                         double neighConf, int[] lowerBounds, int[] upperBounds)
     : base(partsCount, prevConf, neighConf, lowerBounds, upperBounds)
 {
     Instance = instance;
     generatedSolutions = 0;
 }
Пример #5
0
        public DiscreteSS4QAP(QAPInstance instance, int poolSize, 
		                      int refSetSize, double explorationFactor)
            : base(poolSize, refSetSize, explorationFactor)
        {
            Instance = instance;
            generatedSolutions = 0;
        }
Пример #6
0
        // Implementation of the GRC solution's construction algorithm.
        public static int[] GRCSolution(QAPInstance instance, double rclThreshold)
        {
            int numFacilities = instance.NumberFacilities;
            int[] assigment = new int[numFacilities];
            int totalFacilities = numFacilities;
            int index = 0;
            double best = 0;
            double cost = 0;
            int facility = 0;
            // Restricted Candidate List.
            SortedList<double, int> rcl = new SortedList<double, int>();
            // Available cities.
            bool[] assigned = new bool[numFacilities];

            assigment[0] = Statistics.RandomDiscreteUniform(0, numFacilities-1);
            assigned[assigment[0]] = true;
            index++;
            numFacilities --;

            while (numFacilities > 0) {
                rcl = new SortedList<double, int>();
                for (int i = 0; i < totalFacilities; i++) {
                    if (!assigned[i]) {
                        cost = 0;
                        for (int j = 0; j < index; j++) {
                            cost += instance.Distances[j,index] * instance.Flows[assigment[j],i];
                        }
                        if(rcl.Count == 0) {
                            best = cost;
                            rcl.Add(cost, i);
                        }
                        else if( cost < best) {
                            // The new assignment is the new best;
                            best = cost;
                            for (int j = rcl.Count-1; j > 0; j--) {
                                if (rcl.Keys[j] > rclThreshold * best) {
                                    rcl.RemoveAt(j);
                                }
                                else {
                                    break;
                                }
                            }
                            rcl.Add(cost, i);
                        }
                        else if (cost < rclThreshold * best) {
                            // The new assigment is a mostly good candidate.
                            rcl.Add(cost, i);
                        }
                    }
                }
                facility = rcl.Values[Statistics.RandomDiscreteUniform(0, rcl.Count-1)];
                assigned[facility] = true;
                assigment[index] = facility;
                index++;
                numFacilities--;
            }

            return assigment;
        }
 public DiscretePSO2OptBest4QAP(QAPInstance instance, int partsCount, double prevConf,
                         double neighConf, int[] lowerBounds, int[] upperBounds)
     : base(partsCount, prevConf, neighConf, lowerBounds, upperBounds)
 {
     Instance = instance;
     LocalSearchEnabled = true;
     generatedSolutions = 0;
 }
        public DiscreteILS2OptFirst4QAP(QAPInstance instance, int restartIterations, 
		                                int[] lowerBounds, int[] upperBounds)
            : base(restartIterations, lowerBounds, upperBounds)
        {
            Instance = instance;
            RepairEnabled = true;
            generatedSolutions = 0;
        }
Пример #9
0
 public void Start(string inputFile, string outputFile, int timeLimit)
 {
     QAPInstance instance = new QAPInstance(inputFile);
     DiscreteSS ss = new DiscreteSS4QAP(instance, poolSize, refSetSize, explorationFactor);
     ss.Run(timeLimit - timePenalty);
     QAPSolution solution = new QAPSolution(instance, ss.BestSolution);
     solution.Write(outputFile);
 }
Пример #10
0
 public void Start(string fileInput, string fileOutput, int timeLimit)
 {
     QAPInstance instance = new QAPInstance(fileInput);
     int[] assignment = QAPUtils.GRCSolution(instance, 1.0);
     QAPUtils.LocalSearch2OptBest(instance, assignment);
     QAPSolution solution = new QAPSolution(instance, assignment);
     solution.Write(fileOutput);
 }
Пример #11
0
        public DiscreteUMDA4QAP(QAPInstance instance, int popSize,
		                        double truncFactor, int[] lowerBounds, 
		                        int[] upperBounds)
            : base(popSize, truncFactor, lowerBounds, upperBounds)
        {
            Instance = instance;
            RepairEnabled = true;
        }
        public DiscreteHMTSwGRASP2OptBest4QAP(QAPInstance instance, double rclThreshold, 
		                                      int graspIterations, int tabuListLength, 
		                                      int neighborChecks)
            : base(tabuListLength, neighborChecks)
        {
            GRASP = new DiscreteGRASP2OptBest4QAP(instance, rclThreshold);
            Instance = instance;
            GRASPIterations = graspIterations;
        }
 public void Start(string inputFile, string outputFile, int timeLimit)
 {
     QAPInstance instance = new QAPInstance(inputFile);
         int levelLength = (int) Math.Ceiling(levelLengthFactor * (instance.NumberFacilities * (instance.NumberFacilities - 1)));
     DiscreteHMSAwGRASP2OptFirst4QAP hm = new DiscreteHMSAwGRASP2OptFirst4QAP(instance, rclTreshold, graspIterations, initialSolutions, levelLength, tempReduction);
     hm.Run(timeLimit - timePenalty);
     QAPSolution solution = new QAPSolution(instance, hm.BestSolution);
     solution.Write(outputFile);
 }
Пример #14
0
        public DiscreteGA4QAP(QAPInstance instance, int popSize, 
		                       double mutationProbability,
		                       int[] lowerBounds, int[] upperBounds)
            : base(popSize, mutationProbability, lowerBounds, upperBounds)
        {
            Instance = instance;
            RepairEnabled = true;
            generatedSolutions = 0;
        }
        public DiscreteHMSAwGRASP2OptBest4QAP(QAPInstance instance, double rclThreshold, 
		                                      int graspIterations, int initialSolutions, 
		                                      int levelLength, double tempReduction)
            : base(initialSolutions, levelLength, tempReduction)
        {
            GRASP = new DiscreteGRASP2OptBest4QAP(instance, rclThreshold);
            Instance = instance;
            GRASPIterations = graspIterations;
        }
Пример #16
0
 public void Start(string inputFile, string outputFile, int timeLimit)
 {
     QAPInstance instance = new QAPInstance(inputFile);
     MaxMinAntSystem aco = new MaxMinAntSystem2OptBest4QAP(instance, numberAnts, rho, alpha, beta, maxReinit);
     // Solving the problem and writing the best solution found.
     aco.Run(timeLimit - timePenalty);
     QAPSolution solution = new QAPSolution(instance, aco.BestSolution);
     solution.Write(outputFile);
 }
Пример #17
0
 public void Start(string fileInput, string fileOutput, int timeLimit)
 {
     QAPInstance instance = new QAPInstance(fileInput);
     int levelLength = (int) Math.Ceiling(levelLengthFactor * (instance.NumberFacilities * (instance.NumberFacilities - 1)));
     DiscreteSA sa = new DiscreteSA4QAP(instance, initialSolutions, levelLength, tempReduction);
     sa.Run(timeLimit - timePenalty);
     QAPSolution solution = new QAPSolution(instance, sa.BestSolution);
     solution.Write(fileOutput);
 }
 public void Start(string inputFile, string outputFile, int timeLimit)
 {
     QAPInstance instance = new QAPInstance(inputFile);
     int neighborChecks = (int) Math.Ceiling(neighborChecksFactor * (instance.NumberFacilities * (instance.NumberFacilities - 1)));
     int tabuListLength = (int) Math.Ceiling(tabuListFactor * instance.NumberFacilities);
     DiscreteHMTSwGRASP2OptBest4QAP hm = new DiscreteHMTSwGRASP2OptBest4QAP(instance, rclTreshold, graspIterations, tabuListLength, neighborChecks);
     hm.Run(timeLimit - timePenalty);
     QAPSolution solution = new QAPSolution(instance, hm.BestSolution);
     solution.Write(outputFile);
 }
Пример #19
0
        public static double Distance(QAPInstance instance, int[] a, int[] b)
        {
            double distance = 0;

            for (int i = 0; i < a.Length; i++) {
                if (a[i] != b[i]) {
                    distance += 1;
                }
            }

            return distance;
        }
Пример #20
0
        public void Start(string fileInput, string fileOutput, int timeLimit)
        {
            QAPInstance instance = new QAPInstance(fileInput);

            // Setting the parameters of the GRASP for this instance of the problem.
            DiscreteGRASP grasp = new DiscreteGRASP2OptBest4QAP(instance, rclThreshold);

            // Solving the problem and writing the best solution found.
            grasp.Run(timeLimit - (int)timePenalty, RunType.TimeLimit);
            QAPSolution solution = new QAPSolution(instance, grasp.BestSolution);
            solution.Write(fileOutput);
        }
Пример #21
0
        public static double Fitness(QAPInstance instance, int[] assignment)
        {
            double cost = 0;

            for (int i = 0; i < instance.NumberFacilities; i++) {
                for (int j = 0; j < instance.NumberFacilities; j++) {
                    cost += instance.Distances[i,j] * instance.Flows[assignment[i],assignment[j]];
                }
            }

            return cost;
        }
Пример #22
0
 public void Start(string inputFile, string outputFile, int timeLimit)
 {
     QAPInstance instance = new QAPInstance(inputFile);
     int[] lowerBounds = new int[instance.NumberFacilities];
     int[] upperBounds = new int[instance.NumberFacilities];
     for (int i = 0; i < instance.NumberFacilities; i++) {
         lowerBounds[i] = 0;
         upperBounds[i] = instance.NumberFacilities - 1;
     }
     DiscreteILS ils = new DiscreteILS2OptFirst4QAP(instance, restartIterations, lowerBounds, upperBounds);
     ils.Run(timeLimit - timePenalty);
     QAPSolution solution = new QAPSolution(instance, ils.BestSolution);
     solution.Write(outputFile);
 }
Пример #23
0
        public void Start(string fileInput, string fileOutput, int timeLimit)
        {
            QAPInstance instance = new QAPInstance(fileInput);

            // Setting the parameters of the MA for this instance of the problem.
            int[] lowerBounds = new int[instance.NumberFacilities];
            int[] upperBounds = new int[instance.NumberFacilities];
            for (int i = 0; i < instance.NumberFacilities; i++) {
                lowerBounds[i] = 0;
                upperBounds[i] = instance.NumberFacilities - 1;
            }
            DiscreteMA memetic = new DiscreteMA4QAP(instance, (int)popSize, mutProbability, lowerBounds, upperBounds);

            // Solving the problem and writing the best solution found.
            memetic.Run(timeLimit - (int)timePenalty);
            QAPSolution solution = new QAPSolution(instance, memetic.BestIndividual);
            solution.Write(fileOutput);
        }
Пример #24
0
        public void Start(string fileInput, string fileOutput, int timeLimit)
        {
            QAPInstance instance = new QAPInstance(fileInput);

            // Setting the parameters of the PSO for this instance of the problem.
            int[] lowerBounds = new int[instance.NumberFacilities];
            int[] upperBounds = new int[instance.NumberFacilities];
            for (int i = 0; i < instance.NumberFacilities; i++) {
                lowerBounds[i] = 0;
                upperBounds[i] = instance.NumberFacilities - 1;
            }
            DiscretePSO pso = new DiscretePSO2OptBest4QAP(instance, (int)particlesCount, prevConf, neighConf, lowerBounds, upperBounds);

            // Solving the problem and writing the best solution found.
            pso.Run(timeLimit - (int)timePenalty);
            QAPSolution solution = new QAPSolution(instance, pso.BestPosition);
            solution.Write(fileOutput);
        }
Пример #25
0
        public void Start(string fileInput, string fileOutput, int timeLimit)
        {
            QAPInstance instance = new QAPInstance(fileInput);

            // Setting the parameters of the UMDA for this instance of the problem.
            int popSize = (int) Math.Ceiling(popFactor * instance.NumberFacilities);
            int[] lowerBounds = new int[instance.NumberFacilities];
            int[] upperBounds = new int[instance.NumberFacilities];
            for (int i = 0; i < instance.NumberFacilities; i++) {
                lowerBounds[i] = 0;
                upperBounds[i] = instance.NumberFacilities - 1;
            }
            DiscreteUMDA umda = new DiscreteUMDA2OptBest4QAP(instance, popSize, truncFactor, lowerBounds, upperBounds);

            // Solving the problem and writing the best solution found.
            umda.Run(timeLimit - timePenalty);
            QAPSolution solution = new QAPSolution(instance, umda.BestIndividual);
            solution.Write(fileOutput);
        }
        public void Start(string fileInput, string fileOutput, int timeLimit)
        {
            QAPInstance instance = new QAPInstance(fileInput);

            // Setting the parameters of the PSO for this instance of the problem.
            int[] lowerBounds = new int[instance.NumberFacilities];
            int[] upperBounds = new int[instance.NumberFacilities];
            for (int i = 0; i < instance.NumberFacilities; i++)
            {
                lowerBounds[i] = 0;
                upperBounds[i] = instance.NumberFacilities - 1;
            }
            DiscretePSO pso = new DiscretePSO2OptFirst4QAP(instance, (int)particlesCount, prevConf, neighConf, lowerBounds, upperBounds);

            // Solving the problem and writing the best solution found.
            pso.Run(timeLimit - (int)timePenalty);
            QAPSolution solution = new QAPSolution(instance, pso.BestPosition);

            solution.Write(fileOutput);
        }
Пример #27
0
        public void Start(string fileInput, string fileOutput, int timeLimit)
        {
            QAPInstance instance = new QAPInstance(fileInput);

            // Setting the parameters of the GA for this instance of the problem.
            int[] lowerBounds = new int[instance.NumberFacilities];
            int[] upperBounds = new int[instance.NumberFacilities];
            for (int i = 0; i < instance.NumberFacilities; i++)
            {
                lowerBounds[i] = 0;
                upperBounds[i] = instance.NumberFacilities - 1;
            }
            DiscreteGA genetic = new DiscreteGA2OptBest4QAP(instance, (int)popSize, mutProbability, lowerBounds, upperBounds);

            // Solving the problem and writing the best solution found.
            genetic.Run(timeLimit - (int)timePenalty);
            QAPSolution solution = new QAPSolution(instance, genetic.BestIndividual);

            solution.Write(fileOutput);
        }
Пример #28
0
        // Implementation of the 2-opt (best improvement) local search algorithm.
        public static void LocalSearch2OptBest(QAPInstance instance, int[] assignment)
        {
            int    tmp;
            int    firstSwapItem = 0, secondSwapItem = 0;
            double currentFitness, bestFitness;

            bestFitness = Fitness(instance, assignment);
            for (int j = 1; j < assignment.Length; j++)
            {
                for (int i = 0; i < j; i++)
                {
                    // Swap the items.
                    tmp           = assignment[j];
                    assignment[j] = assignment[i];
                    assignment[i] = tmp;

                    // Evaluate the fitness of this new solution.
                    currentFitness = Fitness(instance, assignment);
                    if (currentFitness < bestFitness)
                    {
                        firstSwapItem  = j;
                        secondSwapItem = i;
                        bestFitness    = currentFitness;
                    }

                    // Undo the swap.
                    tmp           = assignment[j];
                    assignment[j] = assignment[i];
                    assignment[i] = tmp;
                }
            }

            // Use the best assignment.
            if (firstSwapItem != secondSwapItem)
            {
                tmp = assignment[firstSwapItem];
                assignment[firstSwapItem]  = assignment[secondSwapItem];
                assignment[secondSwapItem] = tmp;
            }
        }
Пример #29
0
        public static int[] GetNeighbor(QAPInstance instance, int[] solution)
        {
            int[] neighbor = new int[instance.NumberFacilities];
            int a = Statistics.RandomDiscreteUniform(0, solution.Length - 1);
            int b = a;
            while (b == a) {
                b = Statistics.RandomDiscreteUniform(0, solution.Length - 1);
            }
            for (int i = 0; i < solution.Length; i++) {
                if (i == a) {
                    neighbor[i] = solution[b];
                }
                else if (i == b) {
                    neighbor[i] = solution[a];
                }
                else {
                    neighbor[i] = solution[i];
                }
            }

            return neighbor;
        }
Пример #30
0
        public void Start(string fileInput, string fileOutput, int timeLimit)
        {
            QAPInstance instance = new QAPInstance(fileInput);

            // Setting the parameters of the UMDA for this instance of the problem.
            int popSize = (int)Math.Ceiling(popFactor * instance.NumberFacilities);

            int[] lowerBounds = new int[instance.NumberFacilities];
            int[] upperBounds = new int[instance.NumberFacilities];
            for (int i = 0; i < instance.NumberFacilities; i++)
            {
                lowerBounds[i] = 0;
                upperBounds[i] = instance.NumberFacilities - 1;
            }
            DiscreteUMDA umda = new DiscreteUMDA4QAP(instance, popSize, truncFactor, lowerBounds, upperBounds);

            // Solving the problem and writing the best solution found.
            umda.Run(timeLimit - (int)timePenalty);
            QAPSolution solution = new QAPSolution(instance, umda.BestIndividual);

            solution.Write(fileOutput);
        }
Пример #31
0
 public QAPSolution(QAPInstance instance, int[] assignment)
 {
     Instance   = instance;
     Assignment = assignment;
 }
Пример #32
0
 public DiscreteGRASP2OptFirst4QAP(QAPInstance instance, double rclThreshold)
     : base(rclThreshold)
 {
     Instance = instance;
 }
Пример #33
0
        public static int[] RandomSolution(QAPInstance instance)
        {
            int[] solution = new int[instance.NumberFacilities];
            List<int> facilities = new List<int>();

            for (int facility = 0; facility < instance.NumberFacilities; facility++) {
                facilities.Add(facility);
            }
            for (int i = 0; i < instance.NumberFacilities; i++) {
                int facilityIndex = Statistics.RandomDiscreteUniform(0, facilities.Count - 1);
                int facility = facilities[facilityIndex];
                facilities.RemoveAt(facilityIndex);
                solution[i] = facility;
            }

            return solution;
        }
Пример #34
0
        // Implementation of the 2-opt (first improvement) local search algorithm.
        public static void LocalSearch2OptFirst(QAPInstance instance, int[] assignment)
        {
            int tmp;
            double currentFitness, bestFitness;

            bestFitness = Fitness(instance, assignment);
            for (int j = 1; j < assignment.Length; j++) {
                for (int i = 0; i < j; i++) {
                    // Swap the items.
                    tmp = assignment[j];
                    assignment[j] = assignment[i];
                    assignment[i] = tmp;

                    // Evaluate the fitness of this new solution.
                    currentFitness = Fitness(instance, assignment);
                    if (currentFitness < bestFitness) {
                        return;
                    }

                    // Undo the swap.
                    tmp = assignment[j];
                    assignment[j] = assignment[i];
                    assignment[i] = tmp;
                }
            }
        }
 public DiscreteGRASP2OptBest4QAP( QAPInstance instance, double rclThreshold)
     : base(rclThreshold)
 {
     Instance = instance;
 }
Пример #36
0
        public static void Repair(QAPInstance instance, int[] individual)
        {
            int facilitiesCount = 0;
            bool[] facilitiesUsed = new bool[instance.NumberFacilities];
            bool[] facilitiesRepeated = new bool[instance.NumberFacilities];

            // Get information to decide if the individual is valid.
            for (int i = 0; i < instance.NumberFacilities; i++) {
                if (!facilitiesUsed[individual[i]]) {
                    facilitiesCount += 1;
                    facilitiesUsed[individual[i]] = true;
                }
                else {
                    facilitiesRepeated[i] = true;
                }
            }

            // If the individual is invalid, make it valid.
            if (facilitiesCount != instance.NumberFacilities) {
                for (int i = 0; i < facilitiesRepeated.Length; i++) {
                    if (facilitiesRepeated[i]) {
                        int count = Statistics.RandomDiscreteUniform(1, instance.NumberFacilities - facilitiesCount);
                        for (int f = 0; f < facilitiesUsed.Length; f++) {
                            if (!facilitiesUsed[f]) {
                                count -= 1;
                                if (count == 0) {
                                    individual[i] = f;
                                    facilitiesRepeated[i] = false;
                                    facilitiesUsed[f] = true;
                                    facilitiesCount += 1;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #37
0
        // Implementation of the GRC solution's construction algorithm.
        public static int[] GRCSolution(QAPInstance instance, double rclThreshold)
        {
            int numFacilities = instance.NumberFacilities;

            int[]  assigment       = new int[numFacilities];
            int    totalFacilities = numFacilities;
            int    index           = 0;
            double best            = 0;
            double cost            = 0;
            int    facility        = 0;
            // Restricted Candidate List.
            SortedList <double, int> rcl = new SortedList <double, int>();

            // Available cities.
            bool[] assigned = new bool[numFacilities];

            assigment[0]           = Statistics.RandomDiscreteUniform(0, numFacilities - 1);
            assigned[assigment[0]] = true;
            index++;
            numFacilities--;

            while (numFacilities > 0)
            {
                rcl = new SortedList <double, int>();
                for (int i = 0; i < totalFacilities; i++)
                {
                    if (!assigned[i])
                    {
                        cost = 0;
                        for (int j = 0; j < index; j++)
                        {
                            cost += instance.Distances[j, index] * instance.Flows[assigment[j], i];
                        }
                        if (rcl.Count == 0)
                        {
                            best = cost;
                            rcl.Add(cost, i);
                        }
                        else if (cost < best)
                        {
                            // The new assignment is the new best;
                            best = cost;
                            for (int j = rcl.Count - 1; j > 0; j--)
                            {
                                if (rcl.Keys[j] > rclThreshold * best)
                                {
                                    rcl.RemoveAt(j);
                                }
                                else
                                {
                                    break;
                                }
                            }
                            rcl.Add(cost, i);
                        }
                        else if (cost < rclThreshold * best)
                        {
                            // The new assigment is a mostly good candidate.
                            rcl.Add(cost, i);
                        }
                    }
                }
                facility           = rcl.Values[Statistics.RandomDiscreteUniform(0, rcl.Count - 1)];
                assigned[facility] = true;
                assigment[index]   = facility;
                index++;
                numFacilities--;
            }

            return(assigment);
        }