KnapsackSolution.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace MetaheuristicsPCL
  5. {
  6. public class KnapsackSolution : ISolution
  7. {
  8. public double Weight
  9. {
  10. get
  11. {
  12. return LoadedContent.Sum(x => x.Weight);
  13. }
  14. }
  15. public double Value
  16. {
  17. get
  18. {
  19. return LoadedContent.Sum(x => x.Value);
  20. }
  21. }
  22. List<Box> loadedContent;
  23. public List<Box> LoadedContent
  24. {
  25. get
  26. {
  27. return loadedContent;
  28. }
  29. set
  30. {
  31. loadedContent = value;
  32. }
  33. }
  34. public KnapsackSolution()
  35. {
  36. loadedContent = new List<Box>();
  37. }
  38. public KnapsackSolution(KnapsackSolution _solution)
  39. {
  40. loadedContent = new List<Box>();
  41. loadedContent.AddRange(_solution.loadedContent);
  42. }
  43. public override string ToString()
  44. {
  45. String res = "Value : " + Value + " - Weight : " + Weight + "\n";
  46. res += "Loaded : ";
  47. res += String.Join(" - ", loadedContent);
  48. return res;
  49. }
  50. public override bool Equals(object _object)
  51. {
  52. KnapsackSolution solution = (KnapsackSolution)_object;
  53. if (solution.loadedContent.Count != loadedContent.Count || solution.Value != Value || solution.Weight != Weight)
  54. {
  55. return false;
  56. }
  57. else
  58. {
  59. foreach (Box box in loadedContent) {
  60. if (!solution.loadedContent.Contains(box))
  61. {
  62. return false;
  63. }
  64. }
  65. }
  66. return true;
  67. }
  68. public override int GetHashCode()
  69. {
  70. return (int) (Value * Weight);
  71. }
  72. }
  73. }