KnapsackProblem.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace MetaheuristicsPCL
  5. {
  6. public class KnapsackProblem : IProblem
  7. {
  8. protected List<Box> boxes = null;
  9. public double MaxWeight { get; set; }
  10. public static Random randomGenerator = null;
  11. public const int NB_NEIGHBOURS = 30;
  12. public KnapsackProblem()
  13. {
  14. boxes = new List<Box>();
  15. boxes.Add(new Box("A", 4, 15));
  16. boxes.Add(new Box("B", 7, 15));
  17. boxes.Add(new Box("C", 10, 20));
  18. boxes.Add(new Box("D", 3, 10));
  19. boxes.Add(new Box("E", 6, 11));
  20. boxes.Add(new Box("F", 12, 16));
  21. boxes.Add(new Box("G", 11, 12));
  22. boxes.Add(new Box("H", 16, 22));
  23. boxes.Add(new Box("I", 5, 12));
  24. boxes.Add(new Box("J", 14, 21));
  25. boxes.Add(new Box("K", 4, 10));
  26. boxes.Add(new Box("L", 3, 7));
  27. MaxWeight = 20;
  28. if (randomGenerator == null)
  29. {
  30. randomGenerator = new Random();
  31. }
  32. }
  33. public KnapsackProblem(int _nbBoxes, double _maxWeight, double _maxValue)
  34. {
  35. boxes = new List<Box>();
  36. MaxWeight = _maxWeight;
  37. if (randomGenerator == null)
  38. {
  39. randomGenerator = new Random();
  40. }
  41. for (int i = 0; i < _nbBoxes; i++)
  42. {
  43. boxes.Add(new Box(i.ToString(), randomGenerator.NextDouble() * _maxWeight, randomGenerator.NextDouble() * _maxValue));
  44. }
  45. }
  46. public List<Box> Boxes() {
  47. return boxes;
  48. }
  49. public List<ISolution> Neighbourhood(ISolution _currentSolution)
  50. {
  51. List<ISolution> neighbours = new List<ISolution>();
  52. List<Box> possibleBoxes = boxes;
  53. for (int i = 0; i < NB_NEIGHBOURS; i++)
  54. {
  55. KnapsackSolution newSol = new KnapsackSolution((KnapsackSolution)_currentSolution);
  56. int index = randomGenerator.Next(0, newSol.LoadedContent.Count);
  57. newSol.LoadedContent.RemoveAt(index);
  58. double enableSpace = MaxWeight - newSol.Weight;
  59. List<Box> availableBoxes = possibleBoxes.Except(newSol.LoadedContent).Where(x => (x.Weight <= enableSpace)).ToList();
  60. while (enableSpace > 0 && availableBoxes.Count != 0)
  61. {
  62. index = randomGenerator.Next(0, availableBoxes.Count);
  63. newSol.LoadedContent.Add(availableBoxes.ElementAt(index));
  64. enableSpace = MaxWeight - newSol.Weight;
  65. availableBoxes = possibleBoxes.Except(newSol.LoadedContent).Where(x => (x.Weight <= enableSpace)).ToList();
  66. }
  67. neighbours.Add(newSol);
  68. }
  69. return neighbours;
  70. }
  71. public ISolution RandomSolution()
  72. {
  73. KnapsackSolution solution = new KnapsackSolution();
  74. List<Box> possibleBoxes = boxes;
  75. double enableSpace = MaxWeight;
  76. List<Box> availableBoxes = possibleBoxes.Where(x => (x.Weight <= enableSpace)).ToList();
  77. while (enableSpace > 0 && availableBoxes.Count != 0)
  78. {
  79. int index = randomGenerator.Next(0, availableBoxes.Count);
  80. solution.LoadedContent.Add(availableBoxes.ElementAt(index));
  81. enableSpace = MaxWeight - solution.Weight;
  82. availableBoxes = possibleBoxes.Except(solution.LoadedContent).Where(x => (x.Weight <= enableSpace)).ToList();
  83. }
  84. return solution;
  85. }
  86. public ISolution BestSolution(List<ISolution> _listSolutions)
  87. {
  88. return _listSolutions.Where(x => x.Value.Equals(_listSolutions.Max(y => y.Value))).FirstOrDefault();
  89. }
  90. }
  91. }