Пример #1
0
        public DiscreteSANPS42SP(TwoSPInstance instance, int initialSolutions, 
		                         int levelLength, double tempReduction)
            : base(initialSolutions, levelLength, tempReduction)
        {
            Instance = instance;
            generatedSolutions = 0;
        }
Пример #2
0
        public static void BLLocalSearch2OptFirst(TwoSPInstance instance, int[] ordering)
        {
            int    tmp;
            double currentFitness, bestFitness;

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

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

                    // Undo the swap.
                    tmp         = ordering[j];
                    ordering[j] = ordering[i];
                    ordering[i] = tmp;
                }
            }
        }
Пример #3
0
        public DiscretePSONPS42SP(TwoSPInstance instance, int partsCount, double prevConf,
		                          double neighConf, int[] lowerBounds, int[] upperBounds)
            : base(partsCount, prevConf, neighConf, lowerBounds, upperBounds)
        {
            Instance = instance;
            generatedSolutions = 0;
        }
 public DiscreteSANPS42SP(TwoSPInstance instance, int initialSolutions,
                          int levelLength, double tempReduction)
     : base(initialSolutions, levelLength, tempReduction)
 {
     Instance           = instance;
     generatedSolutions = 0;
 }
Пример #5
0
 public DiscreteSSBL42SP(TwoSPInstance instance, int poolSize,
                         int refSetSize, double explorationFactor)
     : base(poolSize, refSetSize, explorationFactor)
 {
     Instance           = instance;
     generatedSolutions = 0;
 }
        public DiscreteSSBL2OptBest42SP(TwoSPInstance instance, int poolSize, 
		                                int refSetSize, double explorationFactor)
            : base(poolSize, refSetSize, explorationFactor)
        {
            Instance = instance;
            generatedSolutions = 0;
        }
Пример #7
0
        public static void BLLocalSearch2OptFirst(TwoSPInstance instance, int[] ordering)
        {
            int tmp;
            double currentFitness, bestFitness;

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

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

                    // Undo the swap.
                    tmp = ordering[j];
                    ordering[j] = ordering[i];
                    ordering[i] = tmp;
                }
            }
        }
Пример #8
0
        public static int[] GetNeighbor(TwoSPInstance instance, int[] solution)
        {
            int[] neighbor = new int[instance.NumberItems];
            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);
        }
 public DiscretePSONPS42SP(TwoSPInstance instance, int partsCount, double prevConf,
                           double neighConf, int[] lowerBounds, int[] upperBounds)
     : base(partsCount, prevConf, neighConf, lowerBounds, upperBounds)
 {
     Instance           = instance;
     generatedSolutions = 0;
 }
        public DiscreteILSBL2OptBest42SP(TwoSPInstance instance, int restartIterations, 
		                                 int[] lowerBounds, int[] upperBounds)
            : base(restartIterations, lowerBounds, upperBounds)
        {
            Instance = instance;
            RepairEnabled = true;
            generatedSolutions = 0;
        }
 public DiscreteUMDANPS42SP(TwoSPInstance instance, int popSize,
                            double truncFactor, int[] lowerBounds,
                            int[] upperBounds)
     : base(popSize, truncFactor, lowerBounds, upperBounds)
 {
     Instance      = instance;
     RepairEnabled = true;
 }
Пример #12
0
        public DiscreteUMDANPS42SP(TwoSPInstance instance, int popSize, 
		                           double truncFactor, int[] lowerBounds, 
		                           int[] upperBounds)
            : base(popSize, truncFactor, lowerBounds, upperBounds)
        {
            Instance = instance;
            RepairEnabled = true;
        }
        public DiscretePSOBL2OptBest42SP(TwoSPInstance instance, int partsCount, double prevConf,
		                                  double neighConf, int[] lowerBounds, int[] upperBounds)
            : base(partsCount, prevConf, neighConf, lowerBounds, upperBounds)
        {
            Instance = instance;
            LocalSearchEnabled = true;
            generatedSolutions = 0;
        }
Пример #14
0
 public DiscretePSOBL2OptBest42SP(TwoSPInstance instance, int partsCount, double prevConf,
                                  double neighConf, int[] lowerBounds, int[] upperBounds)
     : base(partsCount, prevConf, neighConf, lowerBounds, upperBounds)
 {
     Instance           = instance;
     LocalSearchEnabled = true;
     generatedSolutions = 0;
 }
Пример #15
0
 public DiscreteILSBL2OptBest42SP(TwoSPInstance instance, int restartIterations,
                                  int[] lowerBounds, int[] upperBounds)
     : base(restartIterations, lowerBounds, upperBounds)
 {
     Instance           = instance;
     RepairEnabled      = true;
     generatedSolutions = 0;
 }
Пример #16
0
 public void Start(string fileInput, string fileOutput, int timeLimit)
 {
     TwoSPInstance instance = new TwoSPInstance(fileInput);
     int[] ordering = TwoSPUtils.DecreasingArea(instance);
     int[,] coordinates = TwoSPUtils.NPSCoordinates(instance, ordering);
     TwoSPSolution solution = new TwoSPSolution(instance, coordinates);
     solution.Write(fileOutput);
 }
Пример #17
0
 public MaxMinAntSystemBL2OptBest42SP(TwoSPInstance instance, int numberAnts,
                                      double rho, double alpha, double beta,
                                      int maxReinit)
     : base(instance.NumberItems,
            TwoSPUtils.Fitness(instance, TwoSPUtils.BLCoordinates(instance, TwoSPUtils.RandomSolution(instance))),
            numberAnts, rho, alpha, beta, maxReinit)
 {
     Instance = instance;
 }
Пример #18
0
 public DiscreteGANPS42SP(TwoSPInstance instance, int popSize,
                          double mutationProbability,
                          int[] lowerBounds, int[] upperBounds)
     : base(popSize, mutationProbability, lowerBounds, upperBounds)
 {
     Instance           = instance;
     RepairEnabled      = true;
     generatedSolutions = 0;
 }
        public MaxMinAntSystemBL42SP(TwoSPInstance instance, int numberAnts, 
		                             double rho, double alpha, double beta, 
		                             int maxReinit)
            : base(instance.NumberItems, 
			       TwoSPUtils.Fitness(instance, TwoSPUtils.BLCoordinates(instance, TwoSPUtils.RandomSolution(instance))), 
			       numberAnts, rho, alpha, beta, maxReinit)
        {
            Instance = instance;
        }
Пример #20
0
 public void Start(string fileInput, string fileOutput, int timeLimit)
 {
     TwoSPInstance instance = new TwoSPInstance(fileInput);
     int[] ordering = TwoSPUtils.DecreasingWidth(instance);
     TwoSPUtils.BLLocalSearch2OptBest(instance, ordering);
     int[,] coordinates = TwoSPUtils.BLCoordinates(instance, ordering);
     TwoSPSolution solution = new TwoSPSolution(instance, coordinates);
     solution.Write(fileOutput);
 }
Пример #21
0
 public void Start(string inputFile, string outputFile, int timeLimit)
 {
     TwoSPInstance instance = new TwoSPInstance(inputFile);
     DiscreteSS ss = new DiscreteSSNPS42SP(instance, poolSize, refSetSize, explorationFactor);
     ss.Run(timeLimit - timePenalty);
     int[,] coordinates = TwoSPUtils.NPSCoordinates(instance, ss.BestSolution);
     TwoSPSolution solution = new TwoSPSolution(instance, coordinates);
     solution.Write(outputFile);
 }
Пример #22
0
        public DiscreteGABL42SP(TwoSPInstance instance, int popSize, 
		                         double mutationProbability,
		                          int[] lowerBounds, int[] upperBounds)
            : base(popSize, mutationProbability, lowerBounds, upperBounds)
        {
            Instance = instance;
            RepairEnabled = true;
            generatedSolutions = 0;
        }
Пример #23
0
 public void Start(string fileInput, string fileOutput, int timeLimit)
 {
     TwoSPInstance instance = new TwoSPInstance(fileInput);
     int levelLength = (int) Math.Ceiling(levelLengthFactor * (2 * instance.NumberItems));
     DiscreteSA sa = new DiscreteSABL42SP(instance, initialSolutions, levelLength, tempReduction);
     sa.Run(timeLimit - timePenalty);
     int[,] coordinates = TwoSPUtils.BLCoordinates(instance, sa.BestSolution);
     TwoSPSolution solution = new TwoSPSolution(instance, coordinates);
     solution.Write(fileOutput);
 }
Пример #24
0
 public void Start(string inputFile, string outputFile, int timeLimit)
 {
     TwoSPInstance instance = new TwoSPInstance(inputFile);
     MaxMinAntSystem aco = new MaxMinAntSystemBL2OptBest42SP(instance, numberAnts, rho, alpha, beta, maxReinit);
     // Solving the problem and writing the best solution found.
     aco.Run(timeLimit - timePenalty);
     int[,] coordinates = TwoSPUtils.BLCoordinates(instance, aco.BestSolution);
     TwoSPSolution solution = new TwoSPSolution(instance, coordinates);
     solution.Write(outputFile);
 }
Пример #25
0
        public void Start(string fileInput, string fileOutput, int timeLimit)
        {
            TwoSPInstance instance = new TwoSPInstance(fileInput);

            int[] ordering = TwoSPUtils.DecreasingArea(instance);
            int[,] coordinates = TwoSPUtils.NPSCoordinates(instance, ordering);
            TwoSPSolution solution = new TwoSPSolution(instance, coordinates);

            solution.Write(fileOutput);
        }
Пример #26
0
 public void Start(string inputFile, string outputFile, int timeLimit)
 {
     TwoSPInstance instance = new TwoSPInstance(inputFile);
     int neighborChecks = (int) Math.Ceiling(neighborChecksFactor * (2 * instance.NumberItems));
     int tabuListLength = (int) Math.Ceiling(tabuListFactor * instance.NumberItems);
     DiscreteTS ts = new DiscreteTSBL42SP(instance, tabuListLength, neighborChecks);
     ts.Run(timeLimit - timePenalty);
     int[,] coordinates = TwoSPUtils.BLCoordinates(instance, ts.BestSolution);
     TwoSPSolution solution = new TwoSPSolution(instance, coordinates);
     solution.Write(outputFile);
 }
        public void Start(string fileInput, string fileOutput, int timeLimit)
        {
            TwoSPInstance instance = new TwoSPInstance(fileInput);

            int[] ordering = TwoSPUtils.DecreasingWidth(instance);
            TwoSPUtils.BLLocalSearch2OptFirst(instance, ordering);
            int[,] coordinates = TwoSPUtils.BLCoordinates(instance, ordering);
            TwoSPSolution solution = new TwoSPSolution(instance, coordinates);

            solution.Write(fileOutput);
        }
Пример #28
0
        public void Start(string inputFile, string outputFile, int timeLimit)
        {
            TwoSPInstance instance = new TwoSPInstance(inputFile);
            DiscreteSS    ss       = new DiscreteSSBL42SP(instance, poolSize, refSetSize, explorationFactor);

            ss.Run(timeLimit - timePenalty);
            int[,] coordinates = TwoSPUtils.BLCoordinates(instance, ss.BestSolution);
            TwoSPSolution solution = new TwoSPSolution(instance, coordinates);

            solution.Write(outputFile);
        }
Пример #29
0
        // Bottom-Left (BL) placement heuristic.
        public static int[,] BLCoordinates(TwoSPInstance instance, int[] ordering)
        {
            int[,] coordinates = new int[instance.NumberItems,2];
            List<int> placedItems = new List<int>(instance.NumberItems);

            for (int i = 0; i < instance.NumberItems; i++) {
                BLPlaceItem(instance, coordinates, placedItems, ordering[i]);
                placedItems.Add(ordering[i]);
            }

            return coordinates;
        }
Пример #30
0
        public void Start(string inputFile, string outputFile, int timeLimit)
        {
            TwoSPInstance   instance = new TwoSPInstance(inputFile);
            MaxMinAntSystem aco      = new MaxMinAntSystemNPS42SP(instance, numberAnts, rho, alpha, beta, maxReinit);

            // Solving the problem and writing the best solution found.
            aco.Run(timeLimit - timePenalty);
            int[,] coordinates = TwoSPUtils.NPSCoordinates(instance, aco.BestSolution);
            TwoSPSolution solution = new TwoSPSolution(instance, coordinates);

            solution.Write(outputFile);
        }
Пример #31
0
        public void Start(string fileInput, string fileOutput, int timeLimit)
        {
            TwoSPInstance instance    = new TwoSPInstance(fileInput);
            int           levelLength = (int)Math.Ceiling(levelLengthFactor * (2 * instance.NumberItems));
            DiscreteSA    sa          = new DiscreteSABL42SP(instance, initialSolutions, levelLength, tempReduction);

            sa.Run(timeLimit - timePenalty);
            int[,] coordinates = TwoSPUtils.BLCoordinates(instance, sa.BestSolution);
            TwoSPSolution solution = new TwoSPSolution(instance, coordinates);

            solution.Write(fileOutput);
        }
Пример #32
0
        // Bottom-Left (BL) placement heuristic.
        public static int[,] BLCoordinates(TwoSPInstance instance, int[] ordering)
        {
            int[,] coordinates = new int[instance.NumberItems, 2];
            List <int> placedItems = new List <int>(instance.NumberItems);

            for (int i = 0; i < instance.NumberItems; i++)
            {
                BLPlaceItem(instance, coordinates, placedItems, ordering[i]);
                placedItems.Add(ordering[i]);
            }

            return(coordinates);
        }
Пример #33
0
        public void Start(string inputFile, string outputFile, int timeLimit)
        {
            TwoSPInstance instance       = new TwoSPInstance(inputFile);
            int           neighborChecks = (int)Math.Ceiling(neighborChecksFactor * (2 * instance.NumberItems));
            int           tabuListLength = (int)Math.Ceiling(tabuListFactor * instance.NumberItems);
            DiscreteTS    ts             = new DiscreteTSNPS42SP(instance, tabuListLength, neighborChecks);

            ts.Run(timeLimit - timePenalty);
            int[,] coordinates = TwoSPUtils.NPSCoordinates(instance, ts.BestSolution);
            TwoSPSolution solution = new TwoSPSolution(instance, coordinates);

            solution.Write(outputFile);
        }
Пример #34
0
        public static double Distance(TwoSPInstance 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);
        }
Пример #35
0
 public void Start(string inputFile, string outputFile, int timeLimit)
 {
     TwoSPInstance instance = new TwoSPInstance(inputFile);
     int[] lowerBounds = new int[instance.NumberItems];
     int[] upperBounds = new int[instance.NumberItems];
     for (int i = 0; i < instance.NumberItems; i++) {
         lowerBounds[i] = 0;
         upperBounds[i] = instance.NumberItems - 1;
     }
     DiscreteILS ils = new DiscreteILSBL2OptBest42SP(instance, restartIterations, lowerBounds, upperBounds);
     ils.Run(timeLimit - timePenalty);
     int[,] coordinates = TwoSPUtils.BLCoordinates(instance, ils.BestSolution);
     TwoSPSolution solution = new TwoSPSolution(instance, coordinates);
     solution.Write(outputFile);
 }
Пример #36
0
        public static int Fitness(TwoSPInstance instance, int[,] coordinates)
        {
            int totalHeight = 0;

            for (int item = 0; item < coordinates.GetLength(0); item++)
            {
                int itemHeight = coordinates[item, 1] + instance.ItemsHeight[item];
                if (itemHeight > totalHeight)
                {
                    totalHeight = itemHeight;
                }
            }

            return(totalHeight);
        }
Пример #37
0
        public static void Repair(TwoSPInstance instance, int[] individual)
        {
            int itemsAllocatedCount = 0;

            bool[] itemsAllocated = new bool[instance.NumberItems];
            bool[] itemsRepeated  = new bool[instance.NumberItems];

            // Get information to decide if the individual is valid.
            for (int item = 0; item < instance.NumberItems; item++)
            {
                if (!itemsAllocated[individual[item]])
                {
                    itemsAllocatedCount += 1;
                    itemsAllocated[individual[item]] = true;
                }
                else
                {
                    itemsRepeated[item] = true;
                }
            }

            // If the individual is invalid, make it valid.
            if (itemsAllocatedCount != instance.NumberItems)
            {
                for (int item = 0; item < itemsRepeated.Length; item++)
                {
                    if (itemsRepeated[item])
                    {
                        int count = Statistics.RandomDiscreteUniform(1, instance.NumberItems - itemsAllocatedCount);
                        for (int i = 0; i < itemsAllocated.Length; i++)
                        {
                            if (!itemsAllocated[i])
                            {
                                count -= 1;
                                if (count == 0)
                                {
                                    individual[item]     = i;
                                    itemsRepeated[item]  = false;
                                    itemsAllocated[i]    = true;
                                    itemsAllocatedCount += 1;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #38
0
        public static int[] DecreasingArea(TwoSPInstance instance)
        {
            int[] solution = new int[instance.NumberItems];
            List <Tuple <int, int> > sorting = new List <Tuple <int, int> >();

            for (int i = 0; i < instance.NumberItems; i++)
            {
                int area = instance.ItemsHeight[i] * instance.ItemsWidth[i];
                sorting.Add(new Tuple <int, int>(-area, i));
            }
            sorting.Sort();
            for (int i = 0; i < instance.NumberItems; i++)
            {
                solution[i] = sorting[i].Val2;
            }

            return(solution);
        }
Пример #39
0
        public static int[] RandomSolution(TwoSPInstance instance)
        {
            int[]      solution = new int[instance.NumberItems];
            List <int> items    = new List <int>();

            for (int item = 0; item < instance.NumberItems; item++)
            {
                items.Add(item);
            }
            for (int i = 0; i < instance.NumberItems; i++)
            {
                int itemIndex = Statistics.RandomDiscreteUniform(0, items.Count - 1);
                int item      = items[itemIndex];
                items.RemoveAt(itemIndex);
                solution[i] = item;
            }

            return(solution);
        }
Пример #40
0
        public void Start(string inputFile, string outputFile, int timeLimit)
        {
            TwoSPInstance instance = new TwoSPInstance(inputFile);

            int[] lowerBounds = new int[instance.NumberItems];
            int[] upperBounds = new int[instance.NumberItems];
            for (int i = 0; i < instance.NumberItems; i++)
            {
                lowerBounds[i] = 0;
                upperBounds[i] = instance.NumberItems - 1;
            }
            DiscreteILS ils = new DiscreteILSBL2OptBest42SP(instance, restartIterations, perturbations, lowerBounds, upperBounds);

            ils.Run(timeLimit - timePenalty);
            int[,] coordinates = TwoSPUtils.BLCoordinates(instance, ils.BestSolution);
            TwoSPSolution solution = new TwoSPSolution(instance, coordinates);

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

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

            // Solving the problem and writing the best solution found.
            pso.Run(timeLimit - (int)timePenalty);
            int[,] coordinates = TwoSPUtils.BLCoordinates(instance, pso.BestPosition);
            TwoSPSolution solution = new TwoSPSolution(instance, coordinates);
            solution.Write(fileOutput);
        }
Пример #42
0
        public static bool IsFeasible(TwoSPInstance instance, int[,] coordinates)
        {
            bool[] allocatedItems = new bool[instance.NumberItems];

            for (int item = 0; item < allocatedItems.Length; item++)
            {
                allocatedItems[item] = true;
            }
            for (int item = 0; item < coordinates.GetLength(0); item++)
            {
                allocatedItems[item] = false;
                if (!IsFeasible(instance, coordinates, allocatedItems, item))
                {
                    return(false);
                }
                allocatedItems[item] = true;
            }
            return(true);
        }
Пример #43
0
        public void Start(string fileInput, string fileOutput, int timeLimit)
        {
            TwoSPInstance instance = new TwoSPInstance(fileInput);

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

            // Solving the problem and writing the best solution found.
            memetic.Run(timeLimit - (int)timePenalty);
            int[,] coordinates = TwoSPUtils.BLCoordinates(instance, memetic.BestIndividual);
            TwoSPSolution solution = new TwoSPSolution(instance, coordinates);
            solution.Write(fileOutput);
        }
Пример #44
0
        public void Start(string fileInput, string fileOutput, int timeLimit)
        {
            TwoSPInstance instance = new TwoSPInstance(fileInput);

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

            // Solving the problem and writing the best solution found.
            umda.Run(timeLimit - timePenalty);
            int[,] coordinates = TwoSPUtils.BLCoordinates(instance, umda.BestIndividual);
            TwoSPSolution solution = new TwoSPSolution(instance, coordinates);
            solution.Write(fileOutput);
        }
Пример #45
0
        public void Start(string fileInput, string fileOutput, int timeLimit)
        {
            TwoSPInstance instance = new TwoSPInstance(fileInput);

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

            // Solving the problem and writing the best solution found.
            memetic.Run(timeLimit - (int)timePenalty);
            int[,] coordinates = TwoSPUtils.BLCoordinates(instance, memetic.BestIndividual);
            TwoSPSolution solution = new TwoSPSolution(instance, coordinates);

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

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

            // Solving the problem and writing the best solution found.
            pso.Run(timeLimit - (int)timePenalty);
            int[,] coordinates = TwoSPUtils.BLCoordinates(instance, pso.BestPosition);
            TwoSPSolution solution = new TwoSPSolution(instance, coordinates);

            solution.Write(fileOutput);
        }
Пример #47
0
        public static void BLLocalSearch2OptBest(TwoSPInstance instance, int[] ordering)
        {
            int    tmp;
            int    firstSwapItem = 0, secondSwapItem = 0;
            double currentFitness, bestFitness;

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

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

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

            // Use the best assignment.
            if (firstSwapItem != secondSwapItem)
            {
                tmp = ordering[firstSwapItem];
                ordering[firstSwapItem]  = ordering[secondSwapItem];
                ordering[secondSwapItem] = tmp;
            }
        }
Пример #48
0
        private static double NPSQuality(TwoSPInstance instance, int[,] coordinates, bool[] allocatedItem, int item)
        {
            int    y = 1;
            double numerator = 0, denominator;
            double currentItemHeight, maxHeight = 0;

            for (int currentItem = 0; currentItem < instance.NumberItems; currentItem++)
            {
                if (allocatedItem[currentItem] || currentItem == item)
                {
                    currentItemHeight = coordinates[currentItem, y] + instance.ItemsHeight[currentItem];
                    numerator        += instance.ItemsWidth[currentItem] * currentItemHeight;
                    if (currentItemHeight > maxHeight)
                    {
                        maxHeight = currentItemHeight;
                    }
                }
            }
            denominator = instance.StripWidth * maxHeight;

            return(numerator / denominator);
        }
Пример #49
0
        public void Start(string fileInput, string fileOutput, int timeLimit)
        {
            TwoSPInstance instance = new TwoSPInstance(fileInput);

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

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

            // Solving the problem and writing the best solution found.
            umda.Run(timeLimit - timePenalty);
            int[,] coordinates = TwoSPUtils.NPSCoordinates(instance, umda.BestIndividual);
            TwoSPSolution solution = new TwoSPSolution(instance, coordinates);

            solution.Write(fileOutput);
        }
Пример #50
0
        private static bool IsFeasible(TwoSPInstance instance, int[,] coordinates, bool[] allocatedItem, int item)
        {
            int x = 0, y = 1;
            int itemXStart = coordinates[item, x];
            int itemXEnd   = coordinates[item, x] + instance.ItemsWidth[item];
            int itemYStart = coordinates[item, y];
            int itemYEnd   = coordinates[item, y] + instance.ItemsHeight[item];

            // Checking if the item is located inside the strip.
            if (itemXStart < 0 || itemXEnd > instance.StripWidth)
            {
                return(false);
            }

            // Check if the item collapses with other item.
            for (int otherItem = 0; otherItem < coordinates.GetLength(0); otherItem++)
            {
                if (allocatedItem[otherItem])
                {
                    int otherItemXStart = coordinates[otherItem, x];
                    int otherItemXEnd   = coordinates[otherItem, x] + instance.ItemsWidth[otherItem];
                    int otherItemYStart = coordinates[otherItem, y];
                    int otherItemYEnd   = coordinates[otherItem, y] + instance.ItemsHeight[otherItem];

                    if (((otherItemXStart >= itemXStart && otherItemXStart < itemXEnd) ||
                         (otherItemXEnd > itemXStart && otherItemXEnd <= itemXEnd)) &&
                        ((otherItemYStart >= itemYStart && otherItemYStart < itemYEnd) ||
                         (otherItemYEnd > itemYStart && otherItemYEnd <= itemYEnd)))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Пример #51
0
        public static void BLLocalSearch2OptBest(TwoSPInstance instance, int[] ordering)
        {
            int tmp;
            int firstSwapItem = 0, secondSwapItem = 0;
            double currentFitness, bestFitness;

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

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

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

            // Use the best assignment.
            if (firstSwapItem != secondSwapItem) {
                tmp = ordering[firstSwapItem];
                ordering[firstSwapItem] = ordering[secondSwapItem];
                ordering[secondSwapItem] = tmp;
            }
        }
Пример #52
0
        private static bool IsFeasible(TwoSPInstance instance, int[,] coordinates, bool[] allocatedItem, int item)
        {
            int x = 0, y = 1;
            int itemXStart = coordinates[item,x];
            int itemXEnd = coordinates[item,x] + instance.ItemsWidth[item];
            int itemYStart = coordinates[item,y];
            int itemYEnd = coordinates[item,y] + instance.ItemsHeight[item];

            // Checking if the item is located inside the strip.
            if (itemXStart < 0 || itemXEnd > instance.StripWidth) {
                return false;
            }

            // Check if the item collapses with other item.
            for (int otherItem = 0; otherItem < coordinates.GetLength(0); otherItem++) {
                if (allocatedItem[otherItem]) {
                    int otherItemXStart = coordinates[otherItem,x];
                    int otherItemXEnd = coordinates[otherItem,x] + instance.ItemsWidth[otherItem];
                    int otherItemYStart = coordinates[otherItem,y];
                    int otherItemYEnd = coordinates[otherItem,y] + instance.ItemsHeight[otherItem];

                    if (((otherItemXStart >= itemXStart && otherItemXStart < itemXEnd) ||
                         (otherItemXEnd > itemXStart && otherItemXEnd <= itemXEnd)) &&
                        ((otherItemYStart >= itemYStart && otherItemYStart < itemYEnd) ||
                         (otherItemYEnd > itemYStart && otherItemYEnd <= itemYEnd))) {
                        return false;
                    }
                }
            }

            return true;
        }
Пример #53
0
        public static bool IsFeasible(TwoSPInstance instance, int[,] coordinates)
        {
            bool[] allocatedItems = new bool[instance.NumberItems];

            for (int item = 0; item < allocatedItems.Length; item++) {
                allocatedItems[item] = true;
            }
            for (int item = 0; item < coordinates.GetLength(0); item++) {
                allocatedItems[item] = false;
                if (!IsFeasible(instance, coordinates, allocatedItems, item)) {
                    return false;
                }
                allocatedItems[item] = true;
            }
            return true;
        }
Пример #54
0
        public static int[] DecreasingArea(TwoSPInstance instance)
        {
            int[] solution = new int[instance.NumberItems];
            List<Tuple<int,int>> sorting = new List<Tuple<int,int>>();

            for (int i = 0; i < instance.NumberItems; i++) {
                int area = instance.ItemsHeight[i] * instance.ItemsWidth[i];
                sorting.Add(new Tuple<int,int>(-area, i));
            }
            sorting.Sort();
            for (int i = 0; i < instance.NumberItems; i++) {
                solution[i] = sorting[i].Val2;
            }

            return solution;
        }
Пример #55
0
        private static double NPSQuality(TwoSPInstance instance, int[,] coordinates, bool[] allocatedItem, int item)
        {
            int y = 1;
            double numerator = 0, denominator;
            double currentItemHeight, maxHeight = 0;

            for (int currentItem = 0; currentItem < instance.NumberItems; currentItem++) {
                if (allocatedItem[currentItem] || currentItem == item) {
                    currentItemHeight = coordinates[currentItem,y] + instance.ItemsHeight[currentItem];
                    numerator += instance.ItemsWidth[currentItem] * currentItemHeight;
                    if (currentItemHeight > maxHeight) {
                        maxHeight = currentItemHeight;
                    }
                }
            }
            denominator = instance.StripWidth * maxHeight;

            return numerator / denominator;
        }
Пример #56
0
        // Normal Pattern Shifting (NPS) placement heuristic.
        public static int[,] NPSCoordinates(TwoSPInstance instance, int[] ordering)
        {
            int x = 0, y = 1;
            int xpos, ypos;
            int currentItem;
            int[,] coordinates = new int[instance.NumberItems,2];
            List<int[]> topLeftSeeds = new List<int[]>(instance.NumberItems);
            List<int[]> bottomRightSeeds = new List<int[]>(instance.NumberItems);
            bool[] allocatedItems = new bool[instance.NumberItems];
            double finalQuality, currentQuality;
            int[] finalPosition = new int[2];

            // Set the coordinates of the first item.
            currentItem = ordering[0];
            coordinates[currentItem,x] = 0;
            coordinates[currentItem,y] = 0;
            allocatedItems[currentItem] = true;
            topLeftSeeds.Add(new int[] {0, instance.ItemsHeight[currentItem]});
            bottomRightSeeds.Add(new int[] {instance.ItemsWidth[currentItem], 0});

            // Set the coordinates of the rest of the items.
            for (int i = 1; i < instance.NumberItems; i++) {
                currentItem = ordering[i];
                finalQuality = double.MinValue;

                foreach (int[] position in topLeftSeeds) {
                    // Move left the item as much as possible.

                    // Check if we can put the item next to the left border of the strip.
                    coordinates[currentItem,x] = 0;
                    coordinates[currentItem,y] = position[y];
                    if (NPSFeasible(instance, coordinates, allocatedItems, currentItem)) {
                        // If this is valid, it's the best position.
                        finalPosition[x] = coordinates[currentItem,x];
                        finalPosition[y] = coordinates[currentItem,y];
                        finalQuality = NPSQuality(instance, coordinates, allocatedItems, currentItem);
                    }
                    else {
                        // Try to put the item next to other items.
                        xpos = int.MaxValue;
                        for (int otherItem = 0; otherItem < instance.NumberItems; otherItem++) {
                            coordinates[currentItem,x] = coordinates[otherItem,x] + instance.ItemsWidth[otherItem];
                            if (allocatedItems[otherItem] && coordinates[currentItem,x] < xpos) {
                                if (NPSFeasible(instance, coordinates, allocatedItems, currentItem)) {
                                    currentQuality = NPSQuality(instance, coordinates, allocatedItems, currentItem);
                                    if (currentQuality > finalQuality || (currentQuality == finalQuality &&
                                                                          coordinates[currentItem,x] <= finalPosition[x] &&
                                                                          coordinates[currentItem,y] <= finalPosition[y])) {
                                        xpos = coordinates[currentItem,x];
                                        finalPosition[x] = coordinates[currentItem,x];
                                        finalPosition[y] = coordinates[currentItem,y];
                                        finalQuality = currentQuality;
                                    }
                                }
                            }
                        }
                    }
                }

                foreach (int[] position in bottomRightSeeds) {
                    // Move down the item as much as possible.

                    // Check if we can put the item at the bottom of the strip.
                    coordinates[currentItem,x] = position[x];
                    coordinates[currentItem,y] = 0;
                    if (NPSFeasible(instance, coordinates, allocatedItems, currentItem)) {
                        // If this is valid, it's the best position.
                        finalPosition[x] = coordinates[currentItem,x];
                        finalPosition[y] = coordinates[currentItem,y];
                        finalQuality = NPSQuality(instance, coordinates, allocatedItems, currentItem);
                    }
                    else {
                        // Try to put the item on top of other item.
                        ypos = int.MaxValue;
                        for (int otherItem = 0; otherItem < instance.NumberItems; otherItem++) {
                            coordinates[currentItem,y] = coordinates[otherItem,y] + instance.ItemsHeight[otherItem];
                            if (allocatedItems[otherItem] && coordinates[currentItem,y] < ypos) {
                                if (NPSFeasible(instance, coordinates, allocatedItems, currentItem)) {
                                    currentQuality = NPSQuality(instance, coordinates, allocatedItems, currentItem);
                                    if (currentQuality > finalQuality || (currentQuality == finalQuality &&
                                                                          coordinates[currentItem,x] <= finalPosition[x] &&
                                                                          coordinates[currentItem,y] <= finalPosition[y])) {
                                        ypos = coordinates[currentItem,y];
                                        finalPosition[x] = coordinates[currentItem,x];
                                        finalPosition[y] = coordinates[currentItem,y];
                                        finalQuality = currentQuality;
                                    }
                                }
                            }
                        }
                    }
                }

                // Set the position of the current item.
                allocatedItems[currentItem] = true;
                coordinates[currentItem,x] = finalPosition[x];
                coordinates[currentItem,y] = finalPosition[y];

                // Update the lists with the seed positions.
                topLeftSeeds.Add(new int[] {coordinates[currentItem,x], coordinates[currentItem,y] + instance.ItemsHeight[currentItem]});
                bottomRightSeeds.Add(new int[] {coordinates[currentItem,x] + instance.ItemsWidth[currentItem], coordinates[currentItem,y]});
            }

            return coordinates;
        }
Пример #57
0
        private static bool NPSFeasible(TwoSPInstance instance, int[,] coordinates, bool[] allocatedItems, int item)
        {
            int x = 0, y = 1;
            int itemXStart = coordinates[item,x];
            int itemXEnd = coordinates[item,x] + instance.ItemsWidth[item];
            int itemYStart = coordinates[item,y];
            int itemYEnd = coordinates[item,y] + instance.ItemsHeight[item];
            bool leftAdjacent = false;
            bool bottomAdjacent = false;

            // The left-hand edge and the bottom edges should be both adjacent to other
            // items or to the edges of the strip.
            if (itemXStart == 0) leftAdjacent = true;
            if (itemYStart == 0) bottomAdjacent = true;

            // Checking if the item is located inside the strip.
            if (itemXStart < 0 || itemXEnd > instance.StripWidth) {
                return false;
            }

            // Check if the item collapses with other item.
            for (int otherItem = 0; otherItem < coordinates.GetLength(0); otherItem++) {
                if (allocatedItems[otherItem]) {
                    int otherItemXStart = coordinates[otherItem,x];
                    int otherItemXEnd = coordinates[otherItem,x] + instance.ItemsWidth[otherItem];
                    int otherItemYStart = coordinates[otherItem,y];
                    int otherItemYEnd = coordinates[otherItem,y] + instance.ItemsHeight[otherItem];

                    if ((((otherItemXStart >= itemXStart && otherItemXStart < itemXEnd) ||
                          (otherItemXEnd > itemXStart && otherItemXEnd <= itemXEnd)) &&
                         ((otherItemYStart >= itemYStart && otherItemYStart < itemYEnd) ||
                          (otherItemYEnd > itemYStart && otherItemYEnd <= itemYEnd))) ||
                        (((otherItemXStart >= itemXStart && otherItemXStart < itemXEnd) ||
                          (otherItemXEnd > itemXStart && otherItemXEnd <= itemXEnd)) &&
                         (otherItemYStart < itemYStart && otherItemYEnd > itemYEnd)) ||
                        (((otherItemYStart >= itemYStart && otherItemYStart < itemYEnd) ||
                          (otherItemYEnd > itemYStart && otherItemYEnd <= itemYEnd)) &&
                         (otherItemXStart < itemXStart && otherItemXEnd > itemXEnd))) {
                        return false;

                    }

                    // The left-hand edge and the bottom edges should be both adjacent to other
                    // items or to the edges of the strip.
                    if (((otherItemXStart >= itemXStart && otherItemXStart < itemXEnd) ||
                         (otherItemXEnd > itemXStart && otherItemXEnd <= itemXEnd)) &&
                        (itemYStart == otherItemYEnd)) {
                        bottomAdjacent = true;
                    }

                    if (((otherItemYStart >= itemYStart && otherItemYStart < itemYEnd) ||
                         (otherItemYEnd > itemYStart && otherItemYEnd <= itemYEnd)) &&
                        (itemXStart == otherItemXEnd)) {
                        leftAdjacent = true;
                    }
                }
            }

            return (leftAdjacent && bottomAdjacent);
        }
Пример #58
0
        private static void BLPlaceItem(TwoSPInstance instance, int[,] coordinates, List<int> placedItems, int item)
        {
            int x = 0, y = 1;
            int[] currentPosition = new int[2];
            int[] oldPosition = new int[2];
            bool moveLeft = false, moveDown = true;
            int itemXStart, itemXEnd, itemYStart, itemYEnd;
            int otherItemXStart, otherItemXEnd, otherItemYStart, otherItemYEnd;

            // Setting the starting position.
            currentPosition[x] = instance.StripWidth - instance.ItemsWidth[item];
            currentPosition[y] = int.MaxValue - instance.ItemsHeight[item];

            while (moveLeft || moveDown) {
                if (moveDown) {
                    itemXStart = currentPosition[x];
                    itemXEnd = itemXStart + instance.ItemsWidth[item];
                    itemYStart = currentPosition[y];
                    itemYEnd = itemYStart + instance.ItemsHeight[item];

                    oldPosition[y] = currentPosition[y];

                    currentPosition[y] = 0;
                    foreach (int otherItem in placedItems) {
                        otherItemXStart = coordinates[otherItem,x];
                        otherItemXEnd = otherItemXStart + instance.ItemsWidth[otherItem];
                        otherItemYStart = coordinates[otherItem,y];
                        otherItemYEnd = otherItemYStart + instance.ItemsHeight[otherItem];

                        if ((otherItemXStart >= itemXStart && otherItemXStart < itemXEnd) ||
                            (otherItemXEnd > itemXStart && otherItemXEnd <= itemXEnd) ||
                            (otherItemXStart < itemXStart && otherItemXEnd > itemXEnd)) {
                            if (otherItemYEnd > currentPosition[y] && otherItemYEnd <= oldPosition[y]) {
                                currentPosition[y] = otherItemYEnd;
                            }
                        }
                    }

                    moveDown = false;
                    moveLeft = currentPosition[y] != oldPosition[y];
                }

                if (moveLeft) {
                    itemXStart = currentPosition[x];
                    itemXEnd = itemXStart + instance.ItemsWidth[item];
                    itemYStart = currentPosition[y];
                    itemYEnd = itemYStart + instance.ItemsHeight[item];

                    oldPosition[x] = currentPosition[x];

                    currentPosition[x] = 0;
                    foreach (int otherItem in placedItems) {
                        otherItemXStart = coordinates[otherItem,x];
                        otherItemXEnd = otherItemXStart + instance.ItemsWidth[otherItem];
                        otherItemYStart = coordinates[otherItem,y];
                        otherItemYEnd = otherItemYStart + instance.ItemsHeight[otherItem];

                        if ((otherItemYStart >= itemYStart && otherItemYStart < itemYEnd) ||
                            (otherItemYEnd > itemYStart && otherItemYEnd <= itemYEnd) ||
                            (otherItemYStart < itemYStart && otherItemYEnd > itemYEnd)) {
                            if (otherItemXEnd > currentPosition[x] && otherItemXEnd <= oldPosition[x]) {
                                currentPosition[x] = otherItemXEnd;
                            }
                        }
                    }

                    moveLeft = false;
                    moveDown = currentPosition[x] != oldPosition[x];
                }
            }

            coordinates[item,x] = currentPosition[x];
            coordinates[item,y] = currentPosition[y];
        }
Пример #59
0
        public static void Repair(TwoSPInstance instance, int[] individual)
        {
            int itemsAllocatedCount = 0;
            bool[] itemsAllocated = new bool[instance.NumberItems];
            bool[] itemsRepeated = new bool[instance.NumberItems];

            // Get information to decide if the individual is valid.
            for (int item = 0; item < instance.NumberItems; item++) {
                if (!itemsAllocated[individual[item]]) {
                    itemsAllocatedCount += 1;
                    itemsAllocated[individual[item]] = true;
                }
                else {
                    itemsRepeated[item] = true;
                }
            }

            // If the individual is invalid, make it valid.
            if (itemsAllocatedCount != instance.NumberItems) {
                for (int item = 0; item < itemsRepeated.Length; item++) {
                    if (itemsRepeated[item]) {
                        int count = Statistics.RandomDiscreteUniform(1, instance.NumberItems - itemsAllocatedCount);
                        for (int i = 0; i < itemsAllocated.Length; i++) {
                            if (!itemsAllocated[i]) {
                                count -= 1;
                                if (count == 0) {
                                    individual[item] = i;
                                    itemsRepeated[item] = false;
                                    itemsAllocated[i] = true;
                                    itemsAllocatedCount += 1;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #60
0
        public static int[] RandomSolution(TwoSPInstance instance)
        {
            int[] solution = new int[instance.NumberItems];
            List<int> items = new List<int>();

            for (int item = 0; item < instance.NumberItems; item++) {
                items.Add(item);
            }
            for (int i = 0; i < instance.NumberItems; i++) {
                int itemIndex = Statistics.RandomDiscreteUniform(0, items.Count - 1);
                int item = items[itemIndex];
                items.RemoveAt(itemIndex);
                solution[i] = item;
            }

            return solution;
        }