PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/edu/psu/chemxseer/structure/setcover/impl/MaxCoverSolver_Stream.java

https://github.com/Santa827/Chemxseer_subSearch
Java | 298 lines | 96 code | 16 blank | 186 comment | 19 complexity | 435f0b98b507e10e38ee196c57644416 MD5 | raw file
  1. package edu.psu.chemxseer.structure.setcover.impl;
  2. import edu.psu.chemxseer.structure.setcover.IO.IInputStream;
  3. import edu.psu.chemxseer.structure.setcover.IO.Input_DFSStream;
  4. import edu.psu.chemxseer.structure.setcover.interfaces.IBranchBoundCalculator;
  5. import edu.psu.chemxseer.structure.setcover.interfaces.IMaxCoverSolver;
  6. import edu.psu.chemxseer.structure.setcover.maxCoverStatus.ICoverStatusStream;
  7. import edu.psu.chemxseer.structure.setcover.sets.ICoverSet_FeatureWrapper;
  8. import edu.psu.chemxseer.structure.util.MemoryConsumptionCal;
  9. /**
  10. * The Implementation of the Streaming-Set-Cover Algorithms
  11. * Which use the streaming model, assuming # of items are already known, butthe sets comes one after another
  12. * The memory is big enough to hold "K" sets, but not more.
  13. * When a new CoverSet comes from the stream, the algorithm measures the contribution of the new CoverSet
  14. * Decide whether to select it or not.
  15. * @author dayuyuan
  16. *
  17. */
  18. public class MaxCoverSolver_Stream implements IMaxCoverSolver{
  19. private IInputStream input;
  20. private ICoverStatusStream status;
  21. private IBranchBoundCalculator bbCalculator;
  22. private double delta; // swap parameter
  23. private int swapType; // [type = 0, B_1 Swap, type =1 T_current swap]
  24. /**
  25. * Construct a Stream Max-Coverage Solver
  26. * @param input: Input_DFSStream, the stream input
  27. * @param delta: control the swap criterion
  28. * @param branchBound: can be null if no branch& bound is needed
  29. * @param type: type = 0 [B_1 swap], type = 1 [T_current swap]
  30. */
  31. public MaxCoverSolver_Stream(IInputStream input, ICoverStatusStream status,
  32. double delta, IBranchBoundCalculator branchBound, int swapType){
  33. this.input = input;
  34. this.delta = delta;
  35. this.bbCalculator = branchBound;
  36. this.swapType = swapType;
  37. this.status = status;
  38. }
  39. @Override
  40. public int[] runGreedy(int K) {
  41. long start = System.currentTimeMillis();
  42. double[] avgMem = new double[1];
  43. avgMem[0] = 0;
  44. //1. Select the First K sets
  45. ICoverSet_FeatureWrapper oneSet = null;
  46. for(int i = 0; i< K; i++){
  47. oneSet = input.nextSet();
  48. if(oneSet == null){
  49. System.out.println("Error in Stream_SetCover:run, insufficient sets");
  50. return null;
  51. }
  52. this.status.addNewSet(oneSet);
  53. }
  54. //2. Start Swapping The Sets:
  55. this.doSwap(K, avgMem);
  56. System.out.println("Average Space Complexity (MB): " + avgMem[0]);
  57. System.out.println("Total Time for Streamining Mining: " + (System.currentTimeMillis()-start));
  58. // return results
  59. ICoverSet_FeatureWrapper[] selectedSets = this.status.getSelectedSets();
  60. int[] results= new int[K];
  61. for(int i = 0; i< K; i++)
  62. results[i] = selectedSets[i].getFetureID();
  63. return results;
  64. }
  65. /**
  66. * This is optimized for the status implementation of
  67. * Do swap if and only if the difference is huge.
  68. * @param K
  69. */
  70. private void doSwap(int K, double[] avgMem){
  71. //STATISTICS ONLY
  72. int totalIteration = 0;
  73. int swapIteration = 0;
  74. //END OF STATISTICS
  75. double typeScore = 0;
  76. int[] minDecrease = new int[1];
  77. short minSetID = this.status.leastCoverSet(minDecrease);
  78. int totalCoveredCount = 0;
  79. if(this.swapType == 1){
  80. totalCoveredCount = this.status.getCoveredCount();
  81. typeScore = (delta*totalCoveredCount)/K;
  82. }
  83. else if(this.swapType == 0)
  84. typeScore = delta * minDecrease[0];
  85. ICoverSet_FeatureWrapper oneSet = null;
  86. int i = 0;
  87. System.out.println(totalIteration);
  88. while((oneSet = input.nextSet())!=null){
  89. totalIteration ++;
  90. // Test whether we should do the swap
  91. int increase = this.status.getGain(oneSet, minSetID);
  92. int difference = increase-minDecrease[0];
  93. boolean swap = false;
  94. if(difference > typeScore)
  95. swap = true;
  96. // Do the swap
  97. if(swap){
  98. swapIteration++;
  99. this.status.swap(minSetID, oneSet);
  100. // update the minDecrease, minSet, totalCoveredCount
  101. minSetID = status.leastCoverSet(minDecrease);
  102. if(this.swapType == 1){
  103. totalCoveredCount = totalCoveredCount + difference;
  104. typeScore = (delta*totalCoveredCount)/K;
  105. }
  106. else if(this.swapType == 0)
  107. typeScore = delta * minDecrease[0];
  108. }
  109. // Branch & Bound
  110. else if(this.bbCalculator!=null){
  111. double threshold = minDecrease[0] + typeScore;
  112. if(this.bbCalculator.getUpperBound(oneSet, minSetID) < threshold){
  113. input.pruneBranch();
  114. }
  115. }
  116. System.out.println(totalIteration);
  117. // double mem =MemoryConsumptionCal.usedMemoryinMB();
  118. // if(i == 0)
  119. // avgMem[0] = mem;
  120. // else{
  121. // avgMem[0] = (i*avgMem[0] + mem)/i+1;
  122. // }
  123. // i++;
  124. }
  125. System.out.println("TotalIteration: " + totalIteration + " SwapIteration: " + swapIteration);
  126. }
  127. // /**
  128. // * This is optimized for the SetCoverStatus_EWAH_Count & SetCoverStatus_Short
  129. // * We first remove the least used feature,
  130. // * then try to add the new set in, if they meet the criterion.
  131. // * Since getGain(newSet, exceptForID) is not implemented
  132. // * @param K
  133. // */
  134. // private void doSwap2(int K, double[] avgMem){
  135. // //STATISTICS ONLY
  136. // int totalIteration = 0;
  137. // int swapIteration = 0;
  138. // //END OF STATISTICS
  139. //
  140. // double typeScore = 0;
  141. // int[] minDecrease = new int[1];
  142. // int minSetID = this.status.leastCoverSet(minDecrease);
  143. // ICoverSet_FeatureWrapper minSet = this.status.getSelectedSet(minSetID);
  144. //
  145. // int totalCoveredCount = 0;
  146. // if(this.swapType == 1){
  147. // totalCoveredCount = this.status.getCoveredCount();
  148. // typeScore = (delta*totalCoveredCount)/K;
  149. // }
  150. // else if(this.swapType == 0)
  151. // typeScore = delta * minDecrease[0];
  152. // //1. First Remove the minSet
  153. // this.status.removeSet(minSetID);
  154. // //2. Iterative find a succefully swap
  155. // ICoverSet_FeatureWrapper oneSet = null;
  156. // int i = 0;
  157. // while((oneSet = input.nextSet())!=null){
  158. // totalIteration++;
  159. // // Test whether we should do the swap
  160. // int increase = this.status.getGain(oneSet);
  161. // int difference = increase-minDecrease[0];
  162. // boolean swap = false;
  163. // if(difference > typeScore)
  164. // swap = true;
  165. // // Do the swap
  166. // if(swap){
  167. // swapIteration++;
  168. // this.status.addSet(oneSet, minSetID);
  169. // // update the minDecrease, minSet, totalCoveredCount
  170. // minSetID = status.leastCoverSet(minDecrease);
  171. // minSet = this.status.getSelectedSet(minSetID);
  172. // if(this.swapType == 1){
  173. // totalCoveredCount = totalCoveredCount + difference;
  174. // typeScore = (delta*totalCoveredCount)/K;
  175. // }
  176. // else if(this.swapType == 0)
  177. // typeScore = delta * minDecrease[0];
  178. // //2.1 Remove the least again
  179. // this.status.removeSet(minSetID);
  180. // }
  181. // // Branch & Bound
  182. // else if(this.bbCalculator!=null){
  183. // double threshold = minDecrease[0] + typeScore;
  184. // if(this.bbCalculator.getUpperBound(oneSet, -1) < threshold)
  185. // input.prunBranches();
  186. // }
  187. // double mem =MemoryConsumptionCal.usedMemoryinMB();
  188. // if(i == 0)
  189. // avgMem[0] = mem;
  190. // else{
  191. // avgMem[0] = (i*avgMem[0] + mem)/i+1;
  192. // }
  193. // }
  194. // //add the wrongly delete min
  195. // this.status.addSet(minSet, minSetID);
  196. // System.out.println("TotalIteration: " + totalIteration + " SwapIteration: " + swapIteration);
  197. // }
  198. // private void doSwap2Test(int K){
  199. // //STATISTICS ONLY
  200. // int totalIteration = 0;
  201. // int swapIteration = 0;
  202. // //END OF STATISTICS
  203. //
  204. // double typeScore = 0;
  205. // int[] minDecrease = new int[1];
  206. // int minSetID = this.status.leastCoverSet(minDecrease);
  207. // int testMinSetID = this.testingStatus.leastCoverSet(minDecrease);
  208. // if(minSetID!=testMinSetID)
  209. // System.out.println("Lala");
  210. //
  211. // CoverSet_FeatureWrapper minSet = this.status.getSelectedSet(minSetID);
  212. //
  213. // int totalCoveredCount = 0;
  214. // if(this.swapType == 1){
  215. // totalCoveredCount = this.status.getCoveredCount();
  216. // typeScore = (delta*totalCoveredCount)/K;
  217. // }
  218. // else if(this.swapType == 0)
  219. // typeScore = delta * minDecrease[0];
  220. // //1. First Remove the minSet
  221. // this.status.removeFeature(minSetID);
  222. // this.testingStatus.removeFeature(minSetID);
  223. //
  224. // //2. Iterative find a succefully swap
  225. // CoverSet_FeatureWrapper oneSet = null;
  226. // while((oneSet = input.nextSet())!=null){
  227. // totalIteration++;
  228. // // Test whether we should do the swap
  229. // int increase = this.status.getGain(oneSet);
  230. // int testIncrease = this.testingStatus.getGain(oneSet);
  231. // if(increase!=testIncrease)
  232. // System.out.println("lala2");
  233. // int difference = increase-minDecrease[0];
  234. // boolean swap = false;
  235. // if(difference > typeScore)
  236. // swap = true;
  237. // // Do the swap
  238. // if(swap){
  239. // swapIteration++;
  240. // this.status.addFeature(oneSet, minSetID);
  241. // this.testingStatus.addFeature(oneSet, minSetID);
  242. // // update the minDecrease, minSet, totalCoveredCount
  243. // minSetID = status.leastCoverSet(minDecrease);
  244. // testMinSetID = this.testingStatus.leastCoverSet(minDecrease);
  245. // if(minSetID!=testMinSetID)
  246. // System.out.println("Lala");
  247. //
  248. // minSet = this.status.getSelectedSet(minSetID);
  249. // if(this.swapType == 1){
  250. // totalCoveredCount = totalCoveredCount + difference;
  251. // typeScore = (delta*totalCoveredCount)/K;
  252. // }
  253. // else if(this.swapType == 0)
  254. // typeScore = delta * minDecrease[0];
  255. // //2.1 Remove the least again
  256. // this.status.removeFeature(minSetID);
  257. // this.testingStatus.removeFeature(minSetID);
  258. // }
  259. // // Branch & Bound
  260. // else if(this.bbCalculator!=null){
  261. // double threshold = minDecrease[0] + typeScore;
  262. // int bUp = this.bbCalculator.getUpperBound(oneSet, -1);
  263. // int testBUP = this.testingBBCalculator.getUpperBound(oneSet, -1);
  264. // if(bUp!=testBUP){
  265. // bUp = this.bbCalculator.getUpperBound(oneSet, -1);
  266. // System.out.println("lala3");
  267. // }
  268. //// if(this.bbCalculator.getUpperBound(oneSet, -1) < threshold)
  269. //// input.prunBranches();
  270. // }
  271. // }
  272. // //add the wrongly delete min
  273. // this.status.addFeature(minSet, minSetID);
  274. // System.out.println("TotalIteration: " + totalIteration + " SwapIteration: " + swapIteration);
  275. // }
  276. public ICoverSet_FeatureWrapper[] getSelectedSets(){
  277. return this.status.getSelectedSets();
  278. }
  279. public int totalCoveredItems() {
  280. return this.status.getCoveredCount();
  281. }
  282. }