From 0de12a3a12597bd11aa087042abfddf0d932ac79 Mon Sep 17 00:00:00 2001 From: eugenefischer <66030419+eugenefischer@users.noreply.github.com> Date: Sat, 22 Oct 2022 14:58:40 -0500 Subject: [PATCH] Refactor to use selected algorithm type --- src/main/java/Simulator.java | 91 ++++++++++++++++++++++-------------- 1 file changed, 56 insertions(+), 35 deletions(-) diff --git a/src/main/java/Simulator.java b/src/main/java/Simulator.java index 12a75e9..c477592 100644 --- a/src/main/java/Simulator.java +++ b/src/main/java/Simulator.java @@ -183,36 +183,49 @@ public class Simulator implements GraphModificationFunctions { "removed");} //Find Maximum Weight Matching - //using jheaps library class PairingHeap for improved efficiency if(verbose){System.out.println("Finding maximum weight matching");} - - //NON-AUCTION ALGORITHM -// MaximumWeightBipartiteMatching maxWeightMatching; -// //Use correct heap type for priority queue -// String heapType = BiGpairSEQ.getPriorityQueueHeapType(); -// switch (heapType) { -// case "PAIRING" -> { -// maxWeightMatching = new MaximumWeightBipartiteMatching(graph, -// alphas, -// betas, -// i -> new PairingHeap(Comparator.naturalOrder())); -// } -// case "FIBONACCI" -> { -// maxWeightMatching = new MaximumWeightBipartiteMatching(graph, -// alphas, -// betas, -// i -> new FibonacciHeap(Comparator.naturalOrder())); -// } -// default -> { -// maxWeightMatching = new MaximumWeightBipartiteMatching(graph, -// alphas, -// betas); -// } -// } - //Auction algorithm version - MaximumWeightBipartiteAuctionMatching maxWeightMatching = new MaximumWeightBipartiteAuctionMatching(graph, alphas, betas); - //get the matching - MatchingAlgorithm.Matching graphMatching = maxWeightMatching.getMatching(); + //The matching object + MatchingAlgorithm graphMatchingObject; + //The matching weight (have to do this separately, because MatchingAlgorithm interface doesn't have a getMatchingWeight() method) + BigDecimal matchingWeight; + //Determine algorithm type + AlgorithmType algorithm = BiGpairSEQ.getMatchingAlgoritmType(); + switch (algorithm) { //Only two options now, but I have room to add more algorithms in the future this way + case AUCTION -> { + //create a new MaximumIntegerWeightBipartiteAuctionMatching + MaximumIntegerWeightBipartiteAuctionMatching maxWeightMatching = new MaximumIntegerWeightBipartiteAuctionMatching(graph, alphas, betas); + graphMatchingObject = maxWeightMatching; + matchingWeight = maxWeightMatching.getMatchingWeight(); + } + default -> { //HUNGARIAN + //Use JgraphT implementation + MaximumWeightBipartiteMatching maxWeightMatching; + //use selected heap type for priority queue + HeapType heap = BiGpairSEQ.getPriorityQueueHeapType(); + switch (heap) { + case PAIRING -> { + maxWeightMatching = new MaximumWeightBipartiteMatching(graph, + alphas, + betas, + i -> new PairingHeap(Comparator.naturalOrder())); + } + case FIBONACCI -> { + maxWeightMatching = new MaximumWeightBipartiteMatching(graph, + alphas, + betas, + i -> new FibonacciHeap(Comparator.naturalOrder())); + } + default -> { //I have since learned that the default implementation uses a fibonacci heap. + maxWeightMatching = new MaximumWeightBipartiteMatching(graph, + alphas, + betas); + } + } + graphMatchingObject = maxWeightMatching; + matchingWeight = maxWeightMatching.getMatchingWeight(); + } + } + MatchingAlgorithm.Matching matching = graphMatchingObject.getMatching(); if(verbose){System.out.println("Matching completed");} Instant stop = Instant.now(); @@ -230,7 +243,7 @@ public class Simulator implements GraphModificationFunctions { List> allResults = new ArrayList<>(); NumberFormat nf = NumberFormat.getInstance(Locale.US); MathContext mc = new MathContext(3); - Iterator weightIter = graphMatching.iterator(); + Iterator weightIter = matching.iterator(); DefaultWeightedEdge e; int trueCount = 0; int falseCount = 0; @@ -271,11 +284,18 @@ public class Simulator implements GraphModificationFunctions { } //Metadata comments for CSV file -// String algoType = "LEDA book with heap: " + heapType; - String algoType = "Basic Auction Algorithm"; + String algoType; + switch(algorithm) { + case AUCTION -> { + algoType = "Auction algorithm"; + } + default -> { //HUNGARIAN + algoType = "Hungarian algorithm with heap: " + BiGpairSEQ.getPriorityQueueHeapType().name(); + } + } + int min = Math.min(graphAlphaCount, graphBetaCount); - //matching weight - BigDecimal totalMatchingWeight = maxWeightMatching.getMatchingWeight(); + //rate of attempted matching double attemptRate = (double) (trueCount + falseCount) / min; BigDecimal attemptRateTrunc = new BigDecimal(attemptRate, mc); @@ -314,7 +334,7 @@ public class Simulator implements GraphModificationFunctions { metadata.put("sequence dropout rate", data.getDropoutRate().toString()); metadata.put("graph filename", dataFilename); metadata.put("MWM algorithm type", algoType); - metadata.put("matching weight", totalMatchingWeight.toString()); + metadata.put("matching weight", matchingWeight.toString()); metadata.put("well populations", wellPopulationsString); metadata.put("sequence read depth", data.getReadDepth().toString()); metadata.put("sequence read error rate", data.getReadErrorRate().toString()); @@ -352,6 +372,7 @@ public class Simulator implements GraphModificationFunctions { return output; } + //Commented out CDR1 matching until it's time to re-implement it // //Simulated matching of CDR1s to CDR3s. Requires MatchingResult from prior run of matchCDR3s. // public static MatchingResult[] matchCDR1s(List distinctCells,