Guest User

Untitled

a guest
Apr 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.59 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.util.*;
  5.  
  6. public class test2 {
  7.  
  8. // selectionSortx3
  9. //
  10. // This method takes three arrays and sorts them according to the first array (x)
  11. //
  12. // In this program x will always be the Value / Mass ratios for a given problem set
  13. // thus Y and Z will always represent value array and mass array for the problem set
  14. //
  15. // Hence Y and Z will be sorted in a way to match the values with the V/M array thus
  16. // organizing the problem
  17.  
  18. public static void selectionSortx3(double[] x, int[] y, int[] z) {
  19. int temp;
  20. double dtemp;
  21. for (int i=0; i<x.length-1; i++) {
  22. for (int j=i+1; j<x.length; j++) {
  23. if (x[i] < x[j]) {
  24. //... Exchange elements
  25. dtemp = x[i];
  26. x[i] = x[j];
  27. x[j] = dtemp;
  28. temp = y[i];
  29. y[i] = y[j];
  30. y[j] = temp;
  31. temp = z[i];
  32. z[i] = z[j];
  33. z[j] = temp;
  34. }
  35. }
  36. }
  37. }
  38.  
  39. // Find the greedy result, since the elements are sorted just take the first elements until there's no more space
  40. // this is different from the FFC because the FFC finds the opposite of greedy.
  41. public static int greedy (double[] vmratio, int[]masses, int[]values, int[] capacity, int index){
  42. int FFCvar = 0;
  43. int load = capacity [0];
  44.  
  45. for (int i = index; i < values.length; i++){
  46. if (masses[i] <= load){
  47. load -= masses[i];
  48. } else {
  49. FFCvar += values[i];
  50. }
  51. }
  52. return FFCvar;
  53. }
  54.  
  55. // calculate the GFC, this is the value of any items that are individually
  56. // too heavy (too much mass)
  57. public static int GFC (int[]masses, int[]values, int capacity, int index){
  58. int GFCvar = 0;
  59. // int load = capacity;
  60.  
  61. for (int i = index; i < masses.length; i++){
  62. if (masses[index] > capacity){
  63. GFCvar += values[index];
  64. }
  65. }
  66. return GFCvar;
  67. }
  68.  
  69. // calculate the value of any object that you have previously decided
  70. // not to take
  71. public static int CSF (int[] havepicked, int[]valuesarr){
  72.  
  73. int CSFvar = 0;
  74. for (int i = 0; i < valuesarr.length; i++){
  75. if (havepicked[i+1] == 2){
  76. CSFvar += valuesarr[i];
  77. }
  78. }
  79.  
  80. return CSFvar;
  81. }
  82.  
  83.  
  84. public static int FFC (int[] havepicked, int[]valuesarr, int[]masses, int capacity, int index){
  85. int FFCvar = 0;
  86. int load = capacity;
  87.  
  88. for (int i = index; i < valuesarr.length; i++){
  89. if (masses[i] <= load){
  90. load -= masses[i];
  91. } else {
  92. FFCvar += valuesarr[i];
  93. }
  94. }
  95. return FFCvar;
  96. }
  97.  
  98. public static int[] solve (double[] vmratio, int[]masses, int[]values, int[] capacity){
  99. boolean notfound = true;
  100. int indexPtr = 0;
  101. int GUB;
  102. int thiscsf;
  103. int thisgfc;
  104. int thisffc;
  105. int tmpcnt;
  106. int []temp = new int[masses.length +2];
  107. int []dbug;
  108. int utemp;
  109. int wtemp;
  110. for (int i = 0; i < temp.length; i++)
  111. temp [i] = 0;
  112.  
  113. MinHeap psolutions = new MinHeap(temp);
  114. int[] chosen;
  115.  
  116. GUB = greedy (vmratio, masses, values, capacity, indexPtr);
  117.  
  118.  
  119.  
  120. // dbug = psolutions.peek();
  121. // for (int i = 0; i < dbug.length; i++)
  122. // System.out.printf("%d ", dbug[i]);
  123. // System.out.println("");
  124.  
  125. while (notfound){
  126.  
  127. temp = psolutions.remove().clone();
  128. utemp = temp[0];
  129. wtemp = temp[1];
  130. tmpcnt = 0;
  131. indexPtr = -1;
  132. while(indexPtr == -1){
  133. if (temp[tmpcnt+2] == 0){
  134. indexPtr = tmpcnt;
  135. } else {
  136. tmpcnt ++;
  137. }
  138.  
  139. if (tmpcnt +2 >= temp.length && (temp[temp.length-1] != 0)){
  140. indexPtr = 0;
  141. notfound = false;
  142. }
  143. }
  144.  
  145. if (notfound){
  146. if (temp[1] + masses[indexPtr] <= capacity[0]){
  147. temp[indexPtr+2] = 1;
  148. thiscsf = CSF(temp, values);
  149. thisgfc = GFC(masses, values, (capacity[0] - masses[indexPtr]) - temp[1], indexPtr+1);
  150. thisffc = FFC(temp, values, masses, (capacity[0] - masses[indexPtr]) - temp[1], indexPtr+1);
  151.  
  152. if (thiscsf+thisgfc == GUB){
  153.  
  154. temp[0] = thiscsf + thisgfc;
  155.  
  156. temp[1] += masses[indexPtr];
  157.  
  158. psolutions.add(temp);
  159.  
  160. if (thiscsf+thisffc < GUB){
  161. GUB = thiscsf+thisffc;
  162. }
  163.  
  164. } else if (thiscsf+thisgfc < GUB){
  165. temp[0] = thiscsf + thisgfc;
  166. temp[1] += masses[indexPtr];
  167. psolutions.add(temp.clone());
  168. if (thiscsf+thisffc < GUB){
  169. GUB = thiscsf+thisffc;
  170. }
  171. }
  172.  
  173. }
  174.  
  175. temp[0] = utemp;
  176. temp[1] = wtemp;
  177. temp[indexPtr+2] = 2;
  178.  
  179. thiscsf = CSF(temp, values);
  180.  
  181. thisgfc = GFC(masses, values, capacity[0] - temp[1], indexPtr+1);
  182.  
  183. thisffc = FFC(temp, values, masses, capacity[0]- temp[1], indexPtr+1);
  184.  
  185. if (thiscsf+thisgfc == GUB){
  186. temp[0] = thiscsf + thisgfc;
  187. psolutions.add(temp);
  188. if (thiscsf+thisffc < GUB){
  189. GUB = thiscsf+thisffc;
  190. }
  191. } else if (thiscsf+thisgfc < GUB){
  192. temp[0] = thiscsf + thisgfc;
  193. psolutions.add(temp);
  194. if (thiscsf+thisffc < GUB){
  195. GUB = thiscsf+thisffc;
  196. }
  197. }
  198.  
  199. }// end selection structure which wraps expansion
  200. // the selection structure prevents expansion after
  201. // an optimal, fully expanded solution is chosen.
  202.  
  203. }
  204.  
  205. chosen = temp.clone();
  206.  
  207. return chosen.clone();
  208. }
  209. /**
  210. * @param args
  211. * @throws FileNotFoundException
  212. */
  213. public static void main(String[] args) throws FileNotFoundException {
  214. // TODO Auto-generated method stub
  215.  
  216. int[] mutableCL = new int[1];
  217.  
  218. int instances;
  219. int[] carrylimit;
  220. int[] numobj;
  221. int[][]objmasses;
  222. int[][]objvalues;
  223. int[]solution;
  224.  
  225. double[][] vmr;
  226.  
  227. Scanner inputScan = new Scanner(new BufferedReader(new FileReader("Lab_9_data.txt")));
  228.  
  229. instances = inputScan.nextInt();
  230.  
  231. carrylimit = new int[instances];
  232. numobj = new int[instances];
  233. objmasses = new int[instances][];
  234. objvalues = new int[instances][];
  235. vmr = new double[instances][];
  236. for (int i = 0; i < instances; i++){
  237. carrylimit[i] = inputScan.nextInt();
  238. numobj[i] = inputScan.nextInt();
  239. objmasses[i] = new int[numobj[i]];
  240. objvalues[i] = new int[numobj[i]];
  241. vmr[i] = new double [numobj[i]];
  242.  
  243. for (int j = 0; j < numobj[i]; j++){
  244. objmasses[i][j] = inputScan.nextInt();
  245. } // initialize the masses for each problem
  246. for (int j = 0; j < numobj[i]; j++){
  247. objvalues[i][j] = inputScan.nextInt();
  248. } // initialize the values for each problem
  249.  
  250. } // initialize the data
  251.  
  252. inputScan.close();
  253.  
  254. for (int i = 0; i < instances; i++){
  255. for (int j = 0; j < numobj[i]; j++){
  256. vmr [i][j] = ((double)objvalues[i][j])/((double)objmasses[i][j]);
  257. }
  258. } // initialize the value/mass ratio array
  259.  
  260. // sort the value/mass ratio arrays and adjust the value and mass arrays
  261. // to match the v/m ratio array
  262. for (int i = 0; i < instances; i++){
  263.  
  264. for (int j = 0; j < numobj[i]; j++){
  265. vmr [i][j] = ((double)objvalues[i][j])/((double)objmasses[i][j]);
  266. }
  267.  
  268. }
  269.  
  270.  
  271. for (int i = 0; i < instances; i++)
  272. selectionSortx3(vmr[i], objmasses[i], objvalues[i]);
  273.  
  274.  
  275.  
  276.  
  277. for (int i = 0; i < instances; i++){
  278. mutableCL[0] = carrylimit[i];
  279.  
  280. solution = solve(vmr[i], objmasses[i], objvalues[i], mutableCL).clone();
  281.  
  282. System.out.printf("\nFor instance %d the solution is as follows:\n", i+1);
  283.  
  284. for (int j = 2; j < solution.length; j++){
  285. if (solution[j] == 1)
  286. System.out.printf("Take item #%d with %d mass and %d value\n", j-1, objmasses[i][j-2], objvalues[i][j-2]);
  287. }
  288.  
  289. }
  290.  
  291. }
  292.  
  293.  
  294. }
Add Comment
Please, Sign In to add comment