Permutation.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package pathgame.algorithm;
  2. /** Class for generating all possible permutations of travelling salesman paths
  3. *
  4. */
  5. public class Permutation {
  6. private int size;
  7. private int[] vals;
  8. private int[] minVals;
  9. private int minCost = Integer.MAX_VALUE;
  10. private int thingsPerPos;
  11. /** Creates a new Permutation based on the given parameters
  12. *
  13. * @param listSize size of the list the permutation is for
  14. * @param thingsPerPos number of list items per permutation position - for pairs: 2, for single items: 1
  15. */
  16. public Permutation(int listSize, int thingsPerPos) {
  17. this.thingsPerPos = thingsPerPos;
  18. this.size = (listSize/thingsPerPos) - 1;
  19. vals = new int[size];
  20. }
  21. /** Returns the offset stored at the given position
  22. *
  23. * @param pos position of the permutation from which the value should be returned
  24. * @return an offset of the Permutation
  25. */
  26. public int getValAtPos (int pos) {
  27. return vals[pos];
  28. }
  29. /** Returns true if the offset value at the given position can't be increased further
  30. *
  31. * @param pos position of the permutation that should be checked
  32. * @return whether offset at the given position is at its maximum
  33. */
  34. public boolean isPosAtMax (int pos) {
  35. if(vals[pos] == getMaxOfPos(pos)) {
  36. return true;
  37. }
  38. return false;
  39. }
  40. /** Returns the maximum value an offset can have at the given position
  41. *
  42. * @param pos position of the permutation that should be checked
  43. * @return the maximum value an offset can have at the given position
  44. */
  45. public int getMaxOfPos (int pos) {
  46. return thingsPerPos * (size - pos);
  47. }
  48. /** Returns the size of the permutation
  49. *
  50. * @return the size of the permutation
  51. */
  52. public int size() {
  53. return size;
  54. }
  55. /** Increases the offset at the given position by 1 and sets all offsets of higher positions back to 0
  56. *
  57. * @param pos position of the permutation that should be increased
  58. */
  59. public void increaseAtPos (int pos) {
  60. vals[pos]++;
  61. for(int i = pos+1; i < size; i++) {
  62. vals[i] = 0;
  63. }
  64. }
  65. /** Returns the minimum cost stored in this Permutation
  66. *
  67. * @return the minimum cost stored in this Permutation
  68. */
  69. public int getMinCost() {
  70. return minCost;
  71. }
  72. /** Sets the minimum cost stored in this Permutation
  73. *
  74. * @param minCost the new minimum cost to be set
  75. */
  76. public void setMinCost(int minCost) {
  77. this.minCost = minCost;
  78. minVals = vals.clone();
  79. }
  80. /** Makes the current permutation the new minimum permutation to be stored
  81. *
  82. */
  83. public void makePermutMinimal() {
  84. vals = minVals.clone();
  85. }
  86. /** Prints the current permutation
  87. *
  88. */
  89. public void printPermut() {
  90. for(int i = 0; i < size; i++) {
  91. System.out.print(vals[i] + " ");
  92. }
  93. System.out.println();
  94. }
  95. /** Prints the stored minimum permutation
  96. *
  97. */
  98. public void printMinPermut() {
  99. for(int i = 0; i < size; i++) {
  100. System.out.print(minVals[i] + " ");
  101. }
  102. System.out.println();
  103. }
  104. }