Ejemplo n.º 1
0
 public DiscreteTS4SPP(SPPInstance instance, double rclTreshold,
                       int tabuListLength, int neighborChecks)
     : base(tabuListLength, neighborChecks)
 {
     Instance    = instance;
     RclTreshold = rclTreshold;
 }
        public DiscreteSS2OptFirst4SPP(SPPInstance instance, int poolSize, 
		                               int refSetSize, double explorationFactor)
            : base(poolSize, refSetSize, explorationFactor)
        {
            Instance = instance;
            generatedSolutions = 0;
        }
 public DiscreteGA4SPP(SPPInstance instance, int popSize, double mutationProbability,
                       int[] lowerBounds, int[] upperBounds)
     : base(popSize, mutationProbability, lowerBounds, upperBounds)
 {
     Instance           = instance;
     generatedSolutions = 0;
 }
Ejemplo n.º 4
0
 public MaxMinAntSystem2OptBest4SPP(SPPInstance instance, int numberAnts, double rho,
                                    double alpha, double beta, int maxReinit)
     : base(instance.NumberItems, SPPUtils.Fitness(instance, SPPUtils.RandomSolution(instance)),
            numberAnts, rho, alpha, beta, maxReinit)
 {
     Instance = instance;
 }
 public DiscreteSA4SPP(SPPInstance instance, int initialSolutions,
                       int levelLength, double tempReduction)
     : base(initialSolutions, levelLength, tempReduction)
 {
     Instance           = instance;
     generatedSolutions = 0;
 }
 public DiscreteSS2OptFirst4SPP(SPPInstance instance, int poolSize,
                                int refSetSize, double explorationFactor)
     : base(poolSize, refSetSize, explorationFactor)
 {
     Instance           = instance;
     generatedSolutions = 0;
 }
Ejemplo n.º 7
0
        public MaxMinAntSystem4SPP(SPPInstance instance, int numberAnts, double rho, 
		                           double alpha, double beta, int maxReinit)
            : base(instance.NumberItems, SPPUtils.Fitness(instance, SPPUtils.RandomSolution(instance)),
			       numberAnts, rho, alpha, beta, maxReinit)
        {
            Instance = instance;
        }
Ejemplo n.º 8
0
 public DiscreteUMDA4SPP(SPPInstance instance, int popSize,
                         double truncFactor, int[] lowerBounds,
                         int[] upperBounds)
     : base(popSize, truncFactor, lowerBounds, upperBounds)
 {
     Instance = instance;
 }
Ejemplo n.º 9
0
        public DiscreteUMDA4SPP(SPPInstance instance, int popSize, 
		                        double truncFactor, int[] lowerBounds, 
		                        int[] upperBounds)
            : base(popSize, truncFactor, lowerBounds, upperBounds)
        {
            Instance = instance;
        }
Ejemplo n.º 10
0
        // Implementation of the 2-opt (first improvement) local search algorithm.
        public static void LocalSearch2OptFirst(SPPInstance 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++)
                {
                    if (assignment[i] != assignment[j])
                    {
                        // 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;
                    }
                }
            }
        }
Ejemplo n.º 11
0
        public DiscreteGA4SPP(SPPInstance instance, int popSize, double mutationProbability,
		                      int[] lowerBounds, int[] upperBounds)
            : base(popSize, mutationProbability, lowerBounds, upperBounds)
        {
            Instance = instance;
            generatedSolutions = 0;
        }
Ejemplo n.º 12
0
        public static int[] GetNeighbor(SPPInstance instance, int[] solution)
        {
            int[] neighbor  = new int[instance.NumberItems];
            int   index     = Statistics.RandomDiscreteUniform(0, solution.Length - 1);
            int   oldSubset = solution[index];
            int   newSubset = oldSubset;

            while (newSubset == oldSubset)
            {
                newSubset = Statistics.RandomDiscreteUniform(0, instance.NumberSubsets - 1);
            }
            for (int i = 0; i < solution.Length; i++)
            {
                if (i == index)
                {
                    neighbor[i] = newSubset;
                }
                else
                {
                    neighbor[i] = solution[i];
                }
            }

            return(neighbor);
        }
Ejemplo n.º 13
0
        public DiscreteSA4SPP(SPPInstance instance, int initialSolutions, 
		                      int levelLength, double tempReduction)
            : base(initialSolutions, levelLength, tempReduction)
        {
            Instance = instance;
            generatedSolutions = 0;
        }
Ejemplo n.º 14
0
        public DiscreteTS4SPP(SPPInstance instance, double rclTreshold, 
		                       int tabuListLength, int neighborChecks)
            : base(tabuListLength, neighborChecks)
        {
            Instance = instance;
            RclTreshold = rclTreshold;
        }
Ejemplo n.º 15
0
 public DiscreteUMDA2OptBest4SPP(SPPInstance instance, int popSize,
                                 double truncFactor, int[] lowerBounds,
                                 int[] upperBounds)
     : base(popSize, truncFactor, lowerBounds, upperBounds)
 {
     Instance           = instance;
     LocalSearchEnabled = true;
 }
 public DiscreteGA2OptFirst4SPP(SPPInstance instance, int popSize, double mutationProbability,
                                int[] lowerBounds, int[] upperBounds)
     : base(popSize, mutationProbability, lowerBounds, upperBounds)
 {
     Instance           = instance;
     LocalSearchEnabled = true;
     generatedSolutions = 0;
 }
Ejemplo n.º 17
0
 public DiscreteILS2OptFirst4SPP(SPPInstance instance, int restartIterations,
                                 int[] lowerBounds, int[] upperBounds)
     : base(restartIterations, lowerBounds, upperBounds)
 {
     Instance           = instance;
     RepairEnabled      = false;
     generatedSolutions = 0;
 }
        public DiscreteUMDA2OptFirst4SPP(SPPInstance instance, int popSize, 
		                                 double truncFactor, int[] lowerBounds, 
		                                 int[] upperBounds)
            : base(popSize, truncFactor, lowerBounds, upperBounds)
        {
            Instance = instance;
            LocalSearchEnabled = true;
        }
        public DiscreteGA2OptBest4SPP(SPPInstance instance, int popSize, double mutationProbability,
		                              int[] lowerBounds, int[] upperBounds)
            : base(popSize, mutationProbability, lowerBounds, upperBounds)
        {
            Instance = instance;
            LocalSearchEnabled = true;
            generatedSolutions = 0;
        }
Ejemplo n.º 20
0
 public void Start(string fileInput, string fileOutput, int timeLimit)
 {
     SPPInstance instance = new SPPInstance(fileInput);
     int[] assignment = SPPUtils.GRCSolution(instance, 1.0);
     SPPUtils.LocalSearch2OptFirst(instance, assignment);
     SPPSolution solution = new SPPSolution(instance, assignment);
     solution.Write(fileOutput);
 }
Ejemplo n.º 21
0
 public void Start(string inputFile, string outputFile, int timeLimit)
 {
     SPPInstance instance = new SPPInstance(inputFile);
     DiscreteSS ss = new DiscreteSS4SPP(instance, poolSize, refSetSize, explorationFactor);
     ss.Run(timeLimit - timePenalty);
     SPPSolution solution = new SPPSolution(instance, ss.BestSolution);
     solution.Write(outputFile);
 }
        public DiscreteILS2OptBest4SPP(SPPInstance instance, int restartIterations, 
		                                 int[] lowerBounds, int[] upperBounds)
            : base(restartIterations, lowerBounds, upperBounds)
        {
            Instance = instance;
            RepairEnabled = false;
            generatedSolutions = 0;
        }
Ejemplo n.º 23
0
 public DiscreteHMTSwGRASP2OptFirst4SPP(SPPInstance instance, double rclThreshold,
                                        int graspIterations, int tabuListLength,
                                        int neighborChecks)
     : base(tabuListLength, neighborChecks)
 {
     GRASP           = new DiscreteGRASP2OptFirst4SPP(instance, rclThreshold);
     Instance        = instance;
     GRASPIterations = graspIterations;
 }
 public void Start(string inputFile, string outputFile, int timeLimit)
 {
     SPPInstance instance = new SPPInstance(inputFile);
     int levelLength = (int) Math.Ceiling(levelLengthFactor * (instance.NumberSubsets - 1));
     DiscreteHMSAwGRASP2OptBest4SPP hm = new DiscreteHMSAwGRASP2OptBest4SPP(instance, rclTreshold, graspIterations, initialSolutions, levelLength, tempReduction);
     hm.Run(timeLimit - timePenalty);
     SPPSolution solution = new SPPSolution(instance, hm.BestSolution);
     solution.Write(outputFile);
 }
 public DiscreteHMSAwGRASP2OptFirst4SPP(SPPInstance instance, double rclThreshold,
                                        int graspIterations, int initialSolutions,
                                        int levelLength, double tempReduction)
     : base(initialSolutions, levelLength, tempReduction)
 {
     GRASP           = new DiscreteGRASP2OptFirst4SPP(instance, rclThreshold);
     Instance        = instance;
     GRASPIterations = graspIterations;
 }
Ejemplo n.º 26
0
 public void Start(string inputFile, string outputFile, int timeLimit)
 {
     SPPInstance instance = new SPPInstance(inputFile);
     MaxMinAntSystem aco = new MaxMinAntSystem2OptFirst4SPP(instance, numberAnts, rho, alpha, beta, maxReinit);
     // Solving the problem and writing the best solution found.
     aco.Run(timeLimit - timePenalty);
     SPPSolution solution = new SPPSolution(instance, aco.BestSolution);
     solution.Write(outputFile);
 }
        public DiscreteHMTSwGRASP2OptFirst4SPP(SPPInstance instance, double rclThreshold, 
		                                       int graspIterations, int tabuListLength, 
		                                       int neighborChecks)
            : base(tabuListLength, neighborChecks)
        {
            GRASP = new DiscreteGRASP2OptFirst4SPP(instance, rclThreshold);
            Instance = instance;
            GRASPIterations = graspIterations;
        }
        public DiscreteHMSAwGRASP2OptFirst4SPP(SPPInstance instance, double rclThreshold, 
		                                       int graspIterations, int initialSolutions, 
		                                       int levelLength, double tempReduction)
            : base(initialSolutions, levelLength, tempReduction)
        {
            GRASP = new DiscreteGRASP2OptFirst4SPP(instance, rclThreshold);
            Instance = instance;
            GRASPIterations = graspIterations;
        }
Ejemplo n.º 29
0
 public void Start(string fileInput, string fileOutput, int timeLimit)
 {
     SPPInstance instance = new SPPInstance(fileInput);
     int levelLength = (int) Math.Ceiling(levelLengthFactor * (instance.NumberSubsets - 1));
     DiscreteSA sa = new DiscreteSA4SPP(instance, initialSolutions, levelLength, tempReduction);
     sa.Run(timeLimit - timePenalty);
     SPPSolution solution = new SPPSolution(instance, sa.BestSolution);
     solution.Write(fileOutput);
 }
        public void Start(string fileInput, string fileOutput, int timeLimit)
        {
            SPPInstance instance = new SPPInstance(fileInput);

            int[] assignment = SPPUtils.GRCSolution(instance, 1.0);
            SPPUtils.LocalSearch2OptFirst(instance, assignment);
            SPPSolution solution = new SPPSolution(instance, assignment);

            solution.Write(fileOutput);
        }
        public void Start(string inputFile, string outputFile, int timeLimit)
        {
            SPPInstance instance = new SPPInstance(inputFile);
            DiscreteSS  ss       = new DiscreteSS2OptBest4SPP(instance, poolSize, refSetSize, explorationFactor);

            ss.Run(timeLimit - timePenalty);
            SPPSolution solution = new SPPSolution(instance, ss.BestSolution);

            solution.Write(outputFile);
        }
Ejemplo n.º 32
0
 public void Start(string inputFile, string outputFile, int timeLimit)
 {
     SPPInstance instance = new SPPInstance(inputFile);
     int neighborChecks = (int) Math.Ceiling(neighborChecksFactor * (instance.NumberSubsets - 1));
     int tabuListLength = (int) Math.Ceiling(tabuListFactor * instance.NumberItems);
     DiscreteTS ts = new DiscreteTS4SPP(instance, rclTreshold, tabuListLength, neighborChecks);
     ts.Run(timeLimit - timePenalty);
     SPPSolution solution = new SPPSolution(instance, ts.BestSolution);
     solution.Write(outputFile);
 }
Ejemplo n.º 33
0
        public static void PerturbateSolution(SPPInstance instance, int[] solution, int perturbations)
        {
            int point1 = 0;

            for (int i = 0; i < perturbations; i++)
            {
                point1           = Statistics.RandomDiscreteUniform(0, solution.Length - 1);
                solution[point1] = Statistics.RandomDiscreteUniform(0, instance.NumberSubsets - 1);
            }
        }
        public void Start(string inputFile, string outputFile, int timeLimit)
        {
            SPPInstance instance              = new SPPInstance(inputFile);
            int         levelLength           = (int)Math.Ceiling(levelLengthFactor * (instance.NumberSubsets - 1));
            DiscreteHMSAwGRASP2OptBest4SPP hm = new DiscreteHMSAwGRASP2OptBest4SPP(instance, rclTreshold, graspIterations, initialSolutions, levelLength, tempReduction);

            hm.Run(timeLimit - timePenalty);
            SPPSolution solution = new SPPSolution(instance, hm.BestSolution);

            solution.Write(outputFile);
        }
        public void Start(string inputFile, string outputFile, int timeLimit)
        {
            SPPInstance     instance = new SPPInstance(inputFile);
            MaxMinAntSystem aco      = new MaxMinAntSystem2OptBest4SPP(instance, numberAnts, rho, alpha, beta, maxReinit);

            // Solving the problem and writing the best solution found.
            aco.Run(timeLimit - timePenalty);
            SPPSolution solution = new SPPSolution(instance, aco.BestSolution);

            solution.Write(outputFile);
        }
Ejemplo n.º 36
0
        public static int[] RandomSolution(SPPInstance instance)
        {
            int[] solution = new int[instance.NumberItems];

            for (int i = 0; i < instance.NumberItems; i++)
            {
                solution[i] = Statistics.RandomDiscreteUniform(0, instance.NumberSubsets - 1);
            }

            return(solution);
        }
Ejemplo n.º 37
0
        public void Start(string fileInput, string fileOutput, int timeLimit)
        {
            SPPInstance instance    = new SPPInstance(fileInput);
            int         levelLength = (int)Math.Ceiling(levelLengthFactor * (instance.NumberSubsets - 1));
            DiscreteSA  sa          = new DiscreteSA4SPP(instance, initialSolutions, levelLength, tempReduction);

            sa.Run(timeLimit - timePenalty);
            SPPSolution solution = new SPPSolution(instance, sa.BestSolution);

            solution.Write(fileOutput);
        }
Ejemplo n.º 38
0
        public static double Distance(SPPInstance 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;
        }
Ejemplo n.º 39
0
        // Implementation of the GRC solution's construction algorithm.
        public static int[] GRCSolution(SPPInstance instance, double rclThreshold)
        {
            int numItems = instance.NumberItems;
            int numSets = instance.NumberSubsets;
            int[] assigment = new int[numItems];
            int index = 0;
            double best = 0;
            double cost = 0;
            int setItem = 0;
            double[] setWeigths = new double[instance.NumberSubsets];
            instance.SubsetsWeight.CopyTo(setWeigths, 0);
            // Restricted Candidate List.
            SortedList<double, int> rcl = new SortedList<double, int>();

            assigment[0] = Statistics.RandomDiscreteUniform(0, numSets-1);

            index++;
            numItems --;

            while (numItems > 0) {
                rcl = new SortedList<double, int>();
                for (int i = 0; i < numSets; i++) {
                    cost = Math.Abs(setWeigths[i] - instance.ItemsWeight[index]);
                    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);
                    }
                }
                setItem = rcl.Values[Statistics.RandomDiscreteUniform(0, rcl.Count-1)];
                assigment[index] = setItem;
                setWeigths[setItem] -= instance.ItemsWeight[index];
                index++;
                numItems--;
            }
            return assigment;
        }
Ejemplo n.º 40
0
        public void Start(string fileInput, string fileOutput, int timeLimit)
        {
            SPPInstance instance = new SPPInstance(fileInput);

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

            // Solving the problem and writing the best solution found.
            grasp.Run(timeLimit - (int)timePenalty, RunType.TimeLimit);
            SPPSolution solution = new SPPSolution(instance, grasp.BestSolution);
            solution.Write(fileOutput);
        }
Ejemplo n.º 41
0
        public void Start(string inputFile, string outputFile, int timeLimit)
        {
            SPPInstance instance       = new SPPInstance(inputFile);
            int         neighborChecks = (int)Math.Ceiling(neighborChecksFactor * (instance.NumberSubsets - 1));
            int         tabuListLength = (int)Math.Ceiling(tabuListFactor * instance.NumberItems);
            DiscreteTS  ts             = new DiscreteTS4SPP(instance, rclTreshold, tabuListLength, neighborChecks);

            ts.Run(timeLimit - timePenalty);
            SPPSolution solution = new SPPSolution(instance, ts.BestSolution);

            solution.Write(outputFile);
        }
        public void Start(string fileInput, string fileOutput, int timeLimit)
        {
            SPPInstance instance = new SPPInstance(fileInput);

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

            // Solving the problem and writing the best solution found.
            grasp.Run(timeLimit - (int)timePenalty, RunType.TimeLimit);
            SPPSolution solution = new SPPSolution(instance, grasp.BestSolution);

            solution.Write(fileOutput);
        }
Ejemplo n.º 43
0
        public static double Distance(SPPInstance 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);
        }
Ejemplo n.º 44
0
 public void Start(string inputFile, string outputFile, int timeLimit)
 {
     SPPInstance instance = new SPPInstance(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.NumberSubsets - 1;
     }
     DiscreteILS ils = new DiscreteILS2OptFirst4SPP(instance, restartIterations, lowerBounds, upperBounds);
     ils.Run(timeLimit - timePenalty);
     SPPSolution solution = new SPPSolution(instance, ils.BestSolution);
     solution.Write(outputFile);
 }
Ejemplo n.º 45
0
        public static double Fitness(SPPInstance instance, int[] assignment)
        {
            double deviation = 0;

            for (int subset = 0; subset < instance.NumberSubsets; subset++) {
                double subsetWeight = 0;
                for (int item = 0; item < instance.NumberItems; item++) {
                    if (subset == assignment[item]) {
                        subsetWeight += instance.ItemsWeight[item];
                    }
                }
                deviation += Math.Abs(subsetWeight - instance.SubsetsWeight[subset]);
            }

            return deviation;
        }
Ejemplo n.º 46
0
        public void Start(string fileInput, string fileOutput, int timeLimit)
        {
            SPPInstance instance = new SPPInstance(fileInput);

            // Setting the parameters of the GA 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.NumberSubsets - 1;
            }
            DiscreteGA genetic = new DiscreteGA2OptBest4SPP(instance, (int)popSize, mutProbability, lowerBounds, upperBounds);

            // Solving the problem and writing the best solution found.
            genetic.Run(timeLimit);
            SPPSolution solution = new SPPSolution(instance, genetic.BestIndividual);
            solution.Write(fileOutput);
        }
Ejemplo n.º 47
0
        public void Start(string inputFile, string outputFile, int timeLimit)
        {
            SPPInstance instance = new SPPInstance(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.NumberSubsets - 1;
            }
            DiscreteILS ils = new DiscreteILS2OptFirst4SPP(instance, restartIterations, lowerBounds, upperBounds);

            ils.Run(timeLimit - timePenalty);
            SPPSolution solution = new SPPSolution(instance, ils.BestSolution);

            solution.Write(outputFile);
        }
Ejemplo n.º 48
0
        public static double Fitness(SPPInstance instance, int[] assignment)
        {
            double deviation = 0;

            for (int subset = 0; subset < instance.NumberSubsets; subset++)
            {
                double subsetWeight = 0;
                for (int item = 0; item < instance.NumberItems; item++)
                {
                    if (subset == assignment[item])
                    {
                        subsetWeight += instance.ItemsWeight[item];
                    }
                }
                deviation += Math.Abs(subsetWeight - instance.SubsetsWeight[subset]);
            }

            return(deviation);
        }
Ejemplo n.º 49
0
        public void Start(string fileInput, string fileOutput, int timeLimit)
        {
            SPPInstance instance = new SPPInstance(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.NumberSubsets - 1;
            }
            DiscreteUMDA umda = new DiscreteUMDA4SPP(instance, popSize, truncFactor, lowerBounds, upperBounds);

            // Solving the problem and writing the best solution found.
            umda.Run(timeLimit - (int) timePenalty);
            SPPSolution solution = new SPPSolution(instance, umda.BestIndividual);
            solution.Write(fileOutput);
        }
Ejemplo n.º 50
0
        // Implementation of the 2-opt (best improvement) local search algorithm.
        public static void LocalSearch2OptBest(SPPInstance 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++)
                {
                    if (assignment[i] != assignment[j])
                    {
                        // 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;
            }
        }
        public void Start(string fileInput, string fileOutput, int timeLimit)
        {
            SPPInstance instance = new SPPInstance(fileInput);

            // Setting the parameters of the GA 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.NumberSubsets - 1;
            }
            DiscreteGA genetic = new DiscreteGA2OptFirst4SPP(instance, (int)popSize, mutProbability, lowerBounds, upperBounds);

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

            solution.Write(fileOutput);
        }
Ejemplo n.º 52
0
        public static int[] GetNeighbor(SPPInstance instance, int[] solution)
        {
            int[] neighbor = new int[instance.NumberItems];
            int index = Statistics.RandomDiscreteUniform(0, solution.Length - 1);
            int oldSubset = solution[index];
            int newSubset = oldSubset;
            while (newSubset == oldSubset) {
                newSubset = Statistics.RandomDiscreteUniform(0, instance.NumberSubsets - 1);
            }
            for (int i = 0; i < solution.Length; i++) {
                if (i == index) {
                    neighbor[i] = newSubset;
                }
                else {
                    neighbor[i] = solution[i];
                }
            }

            return neighbor;
        }
        public void Start(string fileInput, string fileOutput, int timeLimit)
        {
            SPPInstance instance = new SPPInstance(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.NumberSubsets - 1;
            }
            DiscreteUMDA umda = new DiscreteUMDA2OptBest4SPP(instance, popSize, truncFactor, lowerBounds, upperBounds);

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

            solution.Write(fileOutput);
        }
Ejemplo n.º 54
0
        // Implementation of the GRC solution's construction algorithm.
        public static int[] GRCSolution(SPPInstance instance, double rclThreshold)
        {
            int numItems = instance.NumberItems;
            int numSets  = instance.NumberSubsets;

            int[]  assigment = new int[numItems];
            int    index     = 0;
            double best      = 0;
            double cost      = 0;
            int    setItem   = 0;

            double[] setWeigths = new double[instance.NumberSubsets];
            instance.SubsetsWeight.CopyTo(setWeigths, 0);
            // Restricted Candidate List.
            SortedList <double, int> rcl = new SortedList <double, int>();

            assigment[0] = Statistics.RandomDiscreteUniform(0, numSets - 1);

            index++;
            numItems--;

            while (numItems > 0)
            {
                rcl = new SortedList <double, int>();
                for (int i = 0; i < numSets; i++)
                {
                    cost = Math.Abs(setWeigths[i] - instance.ItemsWeight[index]);
                    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);
                    }
                }
                setItem              = rcl.Values[Statistics.RandomDiscreteUniform(0, rcl.Count - 1)];
                assigment[index]     = setItem;
                setWeigths[setItem] -= instance.ItemsWeight[index];
                index++;
                numItems--;
            }
            return(assigment);
        }
Ejemplo n.º 55
0
 public SPPSolution(SPPInstance instance, int[] assignment)
 {
     Instance   = instance;
     Assignment = assignment;
 }
Ejemplo n.º 56
0
        // Implementation of the 2-opt (best improvement) local search algorithm.
        public static void LocalSearch2OptBest(SPPInstance 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++) {
                    if (assignment[i] != assignment[j]) {
                        // 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;
            }
        }
Ejemplo n.º 57
0
        // Implementation of the 2-opt (first improvement) local search algorithm.
        public static void LocalSearch2OptFirst(SPPInstance 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++) {
                    if (assignment[i] != assignment[j]) {
                        // 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;
                    }
                }
            }
        }
Ejemplo n.º 58
0
        public static int[] RandomSolution(SPPInstance instance)
        {
            int[] solution = new int[instance.NumberItems];

            for (int i = 0; i < instance.NumberItems; i++) {
                solution[i] = Statistics.RandomDiscreteUniform(0, instance.NumberSubsets - 1);
            }

            return solution;
        }
Ejemplo n.º 59
0
 public SPPSolution(SPPInstance instance, int[] assignment)
 {
     Instance = instance;
     Assignment = assignment;
 }
Ejemplo n.º 60
0
        public static void PerturbateSolution(SPPInstance instance, int[] solution, int perturbations)
        {
            int point1 = 0;

            for (int i = 0; i < perturbations; i++) {
                point1 = Statistics.RandomDiscreteUniform(0, solution.Length -1 );
                solution[point1] = Statistics.RandomDiscreteUniform(0, instance.NumberSubsets - 1);
            }
        }