List of usage examples for com.google.common.collect Multiset add
@Override
boolean add(E element);
From source file:bots.mctsbot.ai.bots.bot.gametree.search.expander.SamplingExpander.java
public List<Pair<ActionWrapper, WeightedNode>> getWeightedChildren(boolean uniformTokens) { List<ProbabilityAction> probActions = new ArrayList<ProbabilityAction>(getProbabilityActions()); double[] cumulProb = new double[probActions.size()]; for (int i = 0; i < probActions.size(); i++) { cumulProb[i] = (i > 0 ? cumulProb[i - 1] : 0) + probActions.get(i).getProbability(); }// w w w.j ava2s . c o m if (logger.isTraceEnabled()) { for (int i = 0; i < probActions.size(); i++) { logger.trace("cumulProb[" + i + "]=" + cumulProb[i] + " for action " + probActions.get(i)); } } // ordening for sexy debugging output Multiset<ProbabilityAction> samples = TreeMultiset.create(new Comparator<ProbabilityAction>() { @Override public int compare(ProbabilityAction o1, ProbabilityAction o2) { if (o2.getProbability() < o1.getProbability()) { return -1; } if (o2.getProbability() > o1.getProbability()) { return 1; } if (o1.getAction() instanceof RaiseAction && o2.getAction() instanceof RaiseAction) { return ((RaiseAction) o2.getAction()).amount - ((RaiseAction) o1.getAction()).amount; } if (o1.getAction() instanceof BetAction && o2.getAction() instanceof BetAction) { return ((BetAction) o2.getAction()).amount - ((BetAction) o1.getAction()).amount; } // if probabilities are equal for different classes, // objects are NOT equal per se // go alphabetically? return o1.toString().compareTo(o2.toString()); } }); // Multiset<ProbabilityAction> samples = new // HashMultiset<ProbabilityAction>(); int nbSamples = Math.min(Max_Granularity, tokens); for (int i = 0; i < nbSamples; i++) { ProbabilityAction sampledAction = sampleAction(probActions, cumulProb); samples.add(sampledAction); } Set<Entry<ProbabilityAction>> entrySet = samples.entrySet(); ImmutableList.Builder<Pair<ActionWrapper, WeightedNode>> childrenBuilder = ImmutableList.builder(); for (Entry<ProbabilityAction> entry : entrySet) { int tokensShare = uniformTokens ? tokens / entrySet.size() : tokens * entry.getCount() / nbSamples; // childrenBuilder.add(new Pair<ActionWrapper, WeightedNode>(entry.getElement(), new WeightedNode( node.getChildAfter(entry.getElement(), tokensShare), entry.getCount() / (double) nbSamples))); } return childrenBuilder.build(); }
From source file:it.units.malelab.ege.ge.mapper.SGEMapper.java
public Node<T> map(SGEGenotype<T> genotype, Map<String, Object> report) throws MappingException { int[] usages = new int[genotype.size()]; //map// w w w. j ava 2s . c o m Multiset<Pair<T, Integer>> expandedSymbols = LinkedHashMultiset.create(); Node<Pair<T, Integer>> tree = new Node<>(nonRecursiveGrammar.getStartingSymbol()); while (true) { Node<Pair<T, Integer>> nodeToBeReplaced = null; for (Node<Pair<T, Integer>> node : tree.leafNodes()) { if (nonRecursiveGrammar.getRules().keySet().contains(node.getContent())) { nodeToBeReplaced = node; break; } } if (nodeToBeReplaced == null) { break; } //get codon List<Integer> values = genotype.getGenes().get(nodeToBeReplaced.getContent()); int value = values.get(expandedSymbols.count(nodeToBeReplaced.getContent())); int usageIndex = geneFirstIndexes.get(nodeToBeReplaced.getContent()) + expandedSymbols.count(nodeToBeReplaced.getContent()); usages[usageIndex] = usages[usageIndex] + 1; List<List<Pair<T, Integer>>> options = nonRecursiveGrammar.getRules() .get(nodeToBeReplaced.getContent()); int optionIndex = value; //add children for (Pair<T, Integer> symbol : options.get(optionIndex)) { Node<Pair<T, Integer>> newChild = new Node<>(symbol); nodeToBeReplaced.getChildren().add(newChild); } expandedSymbols.add(nodeToBeReplaced.getContent()); } report.put(BIT_USAGES_INDEX_NAME, usages); return transform(tree); }
From source file:de.hzi.helmholtz.Compare.PathwayComparisonUsingModules.java
public Multimap<Double, String> SubsetIdentification(PathwayUsingModules firstPathway, PathwayUsingModules secondPathway, BiMap<String, Integer> newSourceGeneIdToPositionMap, BiMap<String, Integer> newTargetGeneIdToPositionMap, int Yes) { Multimap<Double, String> result = TreeMultimap.create(Ordering.natural().reverse(), Ordering.natural()); Iterator<Module> sourceGeneIt = firstPathway.geneIterator(); int currentQueryGene = 0; while (sourceGeneIt.hasNext()) { currentQueryGene++;/* w w w. j a v a 2 s . c o m*/ Module queryGene = sourceGeneIt.next(); Multimap<Integer, String> resultr = TreeMultimap.create(Ordering.natural(), Ordering.natural()); int currentTargetGene = 0; Multiset<String> qfunction = LinkedHashMultiset.create(); List<String> qfunctionList = new ArrayList<String>(); List<String> qactivity = new ArrayList<String>(); List<Set<String>> qsubstrate = new ArrayList<Set<String>>(); for (Domain d : queryGene.getDomains()) { qfunction.add(d.getDomainFunctionString()); qfunctionList.add(d.getDomainFunctionString()); qactivity.add(d.getStatus().toString()); qsubstrate.add(d.getSubstrates()); } List<String> TargenesSelected = new ArrayList<String>(); Iterator<Module> targetGeneIt = secondPathway.geneIterator(); while (targetGeneIt.hasNext()) { currentTargetGene++; Module targetGene = targetGeneIt.next(); Multiset<String> tfunction = LinkedHashMultiset.create(); List<String> tactivity = new ArrayList<String>(); List<Set<String>> tsubstrate = new ArrayList<Set<String>>(); List<String> tfunctionList = new ArrayList<String>(); Iterator<Domain> dIter = targetGene.domainIterator(); while (dIter.hasNext()) { Domain d = dIter.next(); tfunction.add(d.getDomainFunctionString()); tfunctionList.add(d.getDomainFunctionString()); tactivity.add(d.getStatus().toString()); tsubstrate.add(d.getSubstrates()); } Multiset<String> DomainsCovered = Multisets.intersection(qfunction, tfunction); int Differences = Math.max(Math.abs(DomainsCovered.size() - tfunction.size()), Math.abs(DomainsCovered.size() - qfunction.size())); if (DomainsCovered.size() == tfunction.size() && tfunction.size() > 4) { TargenesSelected.add(Integer.toString(currentTargetGene)); } else { resultr.put(Differences, Integer.toString(currentTargetGene)); } } int count = 0; if (resultr.size() > 0) { int tsize = 0; if ((firstPathway.size() > 8 && firstPathway.size() < 10) || (secondPathway.size() > 8 && secondPathway.size() < 10)) { tsize = 2; } else if ((firstPathway.size() > 2 && firstPathway.size() < 8) && (secondPathway.size() > 2 && secondPathway.size() < 8)) { tsize = 4; } else { tsize = 1; } while (TargenesSelected.size() < tsize) { Multiset<String> k = LinkedHashMultiset.create(resultr.values()); Multiset<String> t = LinkedHashMultiset.create(TargenesSelected); Multiset<String> Covered = Multisets.intersection(k, t); if (Covered.size() == k.size()) { break; } try { TargenesSelected.addAll( resultr.get(Integer.parseInt(resultr.keySet().toArray()[count].toString()))); } catch (Exception ds) { } count = count + 1; } } // ////System.out.println(TargenesSelected); // Permutation perm = new Permutation(); // List<String> perms = perm.run(TargenesSelected); CombinationGenerator c = new CombinationGenerator(10, 10); List<String> perms = c.GenerateAllPossibleCombinations(TargenesSelected); myFunction sim = new myFunction(); double score = 0; String targetIdentified = ""; List<Module> targetGenesList = secondPathway.getModules(); for (String permu : perms) { String[] values = permu.replace("[", "").replace("]", "").split(","); List<String> mergedTargetgenes = new ArrayList<String>(); List<Integer> ToRemove = new ArrayList<Integer>(); List<String> tactivity = new ArrayList<String>(); List<Set<String>> tsubstrate = new ArrayList<Set<String>>(); for (String j : values) { ToRemove.add(Integer.parseInt(j.trim())); for (Domain i : targetGenesList.get(Integer.parseInt(j.trim()) - 1).getDomains()) { mergedTargetgenes.add(i.getDomainFunctionString()); tactivity.add(i.getStatus().toString()); tsubstrate.add(i.getSubstrates()); } } Multimap<Double, Multimap<String, Integer>> FunctionScores = sim.calculate(qfunctionList, mergedTargetgenes); Multimap<Double, Multimap<String, Integer>> activityscores = myFunction.calculate(qactivity, tactivity); Multimap<Double, Multimap<String, Integer>> substratescores = myFunction .calculate(getSubstrateList(qsubstrate), getSubstrateList(tsubstrate)); Object FunctionScore = FunctionScores.asMap().keySet().toArray()[0]; Object activityScore = activityscores.asMap().keySet().toArray()[0]; Object substrateScore = substratescores.asMap().keySet().toArray()[0]; double finalScore = Math .round((((2.9 * Double.parseDouble(FunctionScore.toString().trim())) + (0.05 * Double.parseDouble(activityScore.toString().trim())) + (0.05 * Double.parseDouble(substrateScore.toString().trim()))) / 3) * 100.0) / 100.0; targetIdentified = permu.replace(",", "+"); String ConvertedGeneIDs = ""; if (Yes == 0) { ConvertedGeneIDs = reconstructWithGeneId(Integer.toString(currentQueryGene), newSourceGeneIdToPositionMap) + "->" + reconstructWithGeneId(targetIdentified.replace("[", "").replace("]", ""), newTargetGeneIdToPositionMap); } else { ConvertedGeneIDs = reconstructWithGeneId(targetIdentified.replace("[", "").replace("]", ""), newTargetGeneIdToPositionMap) + "->" + reconstructWithGeneId(Integer.toString(currentQueryGene), newSourceGeneIdToPositionMap); } // String ConvertedGeneIDs = reconstructWithGeneId(Integer.toString(currentQueryGene), newSourceGeneIdToPositionMap) + "->" + reconstructWithGeneId(targetIdentified.replace("[", "").replace("]", ""), newTargetGeneIdToPositionMap); result.put(finalScore, ConvertedGeneIDs); ScoreFunctionMatchMisMatch.putAll(ConvertedGeneIDs, FunctionScores.values()); ScoreStatusMatchMisMatch.putAll(ConvertedGeneIDs, activityscores.values()); ScoreSubstrateMatchMisMatch.putAll(ConvertedGeneIDs, substratescores.values()); } } return result; }
From source file:org.dllearner.algorithms.pattern.PatternBasedAxiomLearningAlgorithm.java
private Set<OWLAxiom> applyPattern(OWLAxiom pattern, OWLClass cls, Model fragment) { Map<OWLAxiom, Score> axioms2Score = new HashMap<>(); OWLClassExpression patternSubClass = null; OWLClassExpression patternSuperClass = null; if (pattern.isOfType(AxiomType.EQUIVALENT_CLASSES)) { Set<OWLSubClassOfAxiom> subClassOfAxioms = ((OWLEquivalentClassesAxiom) pattern) .asOWLSubClassOfAxioms(); for (OWLSubClassOfAxiom axiom : subClassOfAxioms) { if (!axiom.getSubClass().isAnonymous()) { patternSubClass = axiom.getSubClass(); patternSuperClass = axiom.getSuperClass(); break; }// ww w .ja va 2 s . c o m } } else if (pattern.isOfType(AxiomType.SUBCLASS_OF)) { patternSubClass = ((OWLSubClassOfAxiom) pattern).getSubClass(); patternSuperClass = ((OWLSubClassOfAxiom) pattern).getSuperClass(); } else { logger.warn("Pattern " + pattern + " not supported yet."); return Collections.emptySet(); } Set<OWLEntity> signature = patternSuperClass.getSignature(); signature.remove(patternSubClass.asOWLClass()); Query query = converter.asQuery("?x", dataFactory.getOWLObjectIntersectionOf(cls, patternSuperClass), signature); logger.info("Running query\n" + query); Map<OWLEntity, String> variablesMapping = converter.getVariablesMapping(); org.apache.jena.query.ResultSet rs = QueryExecutionFactory.create(query, fragment).execSelect(); QuerySolution qs; Set<String> resources = new HashSet<>(); Multiset<OWLAxiom> instantiations = HashMultiset.create(); while (rs.hasNext()) { qs = rs.next(); resources.add(qs.getResource("x").getURI()); // get the IRIs for each variable Map<OWLEntity, IRI> entity2IRIMap = new HashMap<>(); entity2IRIMap.put(patternSubClass.asOWLClass(), cls.getIRI()); boolean skip = false; for (OWLEntity entity : signature) { String var = variablesMapping.get(entity); if (qs.get(var) == null) { logger.warn("Variable " + var + " is not bound."); skip = true; break; } if (qs.get(var).isLiteral()) { skip = true; break; } Resource resource = qs.getResource(var); if (entity.isOWLObjectProperty() && resource.hasURI(RDF.type.getURI())) { skip = true; break; } entity2IRIMap.put(entity, IRI.create(resource.getURI())); } if (!skip) { // instantiate the pattern OWLObjectDuplicator duplicator = new OWLObjectDuplicator(entity2IRIMap, dataFactory); OWLAxiom patternInstantiation = duplicator.duplicateObject(pattern); instantiations.add(patternInstantiation); } } // compute the score int total = resources.size(); for (OWLAxiom axiom : instantiations.elementSet()) { int frequency = instantiations.count(axiom); // System.out.println(axiom + ":" + frequency); Score score = computeScore(total, Math.min(total, frequency)); axioms2Score.put(axiom, score); } return asAnnotatedAxioms(axioms2Score); }
From source file:it.units.malelab.ege.ge.mapper.BitsSGEMapper.java
@Override public Node<T> map(BitsGenotype genotype, Map<String, Object> report) throws MappingException { int[] bitUsages = new int[genotype.size()]; //transform genotypes in ints if (genotype.size() < overallSize) { throw new MappingException(String.format("Short genotype (%d<%d)", genotype.size(), overallSize)); }/* w w w.java2 s. com*/ Map<Pair<T, Integer>, List<Range<Integer>>> codonRanges = new LinkedHashMap<>(); List<Range<Integer>> nonTerminalRanges = Utils.slices(Range.closedOpen(0, genotype.size()), nonTerminalSizes); for (int i = 0; i < nonTerminals.size(); i++) { //int codonSize = (int) Math.max(Math.ceil(Math.log10(nonRecursiveGrammar.getRules().get(nonTerminals.get(i)).size()) / Math.log10(2)), 1); List<Range<Integer>> boundaries = Utils.slices(nonTerminalRanges.get(i), nonTerminalCodonsNumbers.get(i)); codonRanges.put(nonTerminals.get(i), boundaries); } //map Multiset<Pair<T, Integer>> expandedSymbols = LinkedHashMultiset.create(); Node<Pair<T, Integer>> tree = new Node<>(nonRecursiveGrammar.getStartingSymbol()); while (true) { Node<Pair<T, Integer>> nodeToBeReplaced = null; for (Node<Pair<T, Integer>> node : tree.leafNodes()) { if (nonRecursiveGrammar.getRules().keySet().contains(node.getContent())) { nodeToBeReplaced = node; break; } } if (nodeToBeReplaced == null) { break; } //get codon Range<Integer> range = codonRanges.get(nodeToBeReplaced.getContent()) .get(expandedSymbols.count(nodeToBeReplaced.getContent())); List<List<Pair<T, Integer>>> options = nonRecursiveGrammar.getRules() .get(nodeToBeReplaced.getContent()); int codonSize = (int) Math.max(Math.ceil(Math.log10(options.size()) / Math.log10(2)), 1); int codonValue = genotype.slice(range).compress(codonSize).toInt(); int optionIndex = codonValue % options.size(); //update usages for (int i = range.lowerEndpoint(); i < range.upperEndpoint(); i++) { bitUsages[i] = bitUsages[i] + 1; } //add children for (Pair<T, Integer> p : options.get(optionIndex)) { Node<Pair<T, Integer>> newChild = new Node<>(p); nodeToBeReplaced.getChildren().add(newChild); } expandedSymbols.add(nodeToBeReplaced.getContent()); } report.put(BIT_USAGES_INDEX_NAME, bitUsages); //transform tree return transform(tree); }
From source file:tufts.vue.ds.DataAction.java
/** * Make links from the given node, which is a value node for the given Field, * to any nodes in linkTargets for which a Relation can be found. *//*w ww . ja v a 2s . c om*/ private static List<LWLink> makeValueNodeLinks(final Collection<? extends LWComponent> linkTargets, final LWComponent node, final Field field, // todo: remove arg? is not immediately clear this MUST be the Field in node (which MUST be a value node, yes?) final Multiset<LWComponent> targetsUsed) { if (DEBUG.SCHEMA || DEBUG.WORK) { Log.debug("makeValueNodeLinks:" + "\n\t field: " + quoteKey(field) + "\n\t node: " + node + "\n\ttargets: " + Util.tags(linkTargets)); if (node.getDataValueField() != field) Util.printStackTrace("field mis-match: nodeField=" + node.getDataValueField() + "; argField=" + field + "; node=" + node); } final List<LWLink> links = Util.skipNullsArrayList(); final String fieldName = field.getName(); final String fieldValue = node.getDataValue(fieldName); final Schema dragSchema = field.getSchema(); for (LWComponent target : linkTargets) { if (target == node) continue; if (DEBUG.DATA) Log.debug("makeValueNodeLinks: processing " + target); try { // TODO: NEEDS TO USE ASSOCIATIONS // HANDLE VIA RELATIONS??? // This is where ALSO where a JOIN needs to take place. We want a way to do // that which is generic to schemas instead of just here, so dropping the // Rockwell.Medium FIELD on a Rockwell.Painting ROW will extract the right // value, as well as be discovered later here to create the link. if (target.hasDataValue(fieldName, fieldValue)) { // if the target node c is schematic at all, it should only have // one piece of meta-data, and it should be an exact match already //boolean sameField = fieldName.equals(c.getSchematicFieldName()); final boolean sameField = target.isDataValueNode(); links.add(makeLink(node, target, fieldName, fieldValue, sameField ? Color.red : null)); if (targetsUsed != null) targetsUsed.add(target); } final Relation relation = Relation.getCrossSchemaRelation(field, target.getRawData(), fieldValue); if (relation != null) { if (!CREATE_COUNT_LINKS && relation.type == Relation.COUNT) { // We'll get here if we're ignoring the creation of count links. // This is the kind of link that makes being able to analyize an // iTunes library even possible -- e.g., there are zillions of // row nodes and you really don't want to see any of them -- // just the relationships between them. } else { links.add(makeLink(node, target, relation)); } } // final String relatedValue = Relation.getCrossSchemaRelation(field, target.getRawData(), fieldValue); // if (relatedValue != null) { // final String relation = String.format("matched joined value \"%s\"", relatedValue); // links.add(makeLink(node, target, null, relation, Color.green)); // } } catch (Throwable t) { Log.error("exception scanning for links from " + field + " to " + target + ":", t); } } if (DEBUG.SCHEMA || DEBUG.WORK) Log.debug("makeValueNodeLinks: returning:\n\t" + Util.tags(links) + "\n\n"); return links; }
From source file:tufts.vue.ds.DataAction.java
/** make links from row nodes (full data nodes) to any schematic field nodes found in the link targets, or between row nodes from different schema's that are considered "auto-joined" (e.g., a matching key field appears) */ private static List<LWLink> makeRowNodeLinks(final Collection<? extends LWComponent> linkTargets, final LWComponent rowNode, final Multiset<LWComponent> targetsUsed) { if (!rowNode.isDataRowNode()) Log.warn("making row links to non-row node: " + rowNode, new Throwable("FYI")); final Schema sourceSchema = rowNode.getDataSchema(); final MetaMap sourceRow = rowNode.getRawData(); if (DEBUG.Enabled) { String targets;/*from ww w . j a v a 2 s .c om*/ if (linkTargets.size() == 1) targets = Util.getFirst(linkTargets).toString(); else targets = Util.tags(linkTargets); Log.debug("makeRowNodeLinks: " + rowNode + "; " + rowNode.getRawData() + "; " + targets); } final List<LWLink> links = Util.skipNullsArrayList(); final List<LWComponent> singletonTargetList = new ArrayList(2); singletonTargetList.add(null); for (LWComponent target : linkTargets) { if (target == rowNode) // never link to ourself continue; try { final Schema targetSchema = target.getDataSchema(); if (targetSchema == null) { //----------------------------------------------------------------------------- // CHECK FOR RESOURCE META-DATA AND LABEL META-DATA //----------------------------------------------------------------------------- continue; } final Field singleValueField = target.getDataValueField(); if (singleValueField != null) { singletonTargetList.set(0, rowNode); final List<LWLink> valueLinks = makeValueNodeLinks(singletonTargetList, target, singleValueField, null); // do NOT accrue reverse targets! //= makeValueNodeLinks(singletonTargetList, target, singleValueField, targetsUsed); if (valueLinks.size() > 0) { targetsUsed.add(target); if (valueLinks.size() > 1) Log.warn("more than 1 link added for single value node: " + Util.tags(valueLinks), new Throwable("HERE")); } links.addAll(valueLinks); } // final String singleValueFieldName = c.getDataValueFieldName(); // if (singleValueFieldName != null) { // //----------------------------------------------------------------------------- // // The target being inspected is a value node - create a link // // if there's any matching value in the row node. We don't // // currently care if it's from the same schema or not: identical // // field names currently always provide a match (sort of a weak auto-join) // //----------------------------------------------------------------------------- // final String fieldValue = c.getDataValue(singleValueFieldName); // // TODO: USE ASSOCIATIONS // if (rowNode.hasDataValue(singleValueFieldName, fieldValue)) { // links.add(makeLink(c, rowNode, singleValueFieldName, fieldValue, null)); // } // } else if (sourceSchema == targetSchema) { final MetaMap targetRow = target.getRawData(); if (Relation.isSameRow(targetSchema, targetRow, sourceRow)) { links.add(makeLink(rowNode, target, null, null, Color.orange)); targetsUsed.add(target); } } else { // if (sourceSchema != targetSchema) { final MetaMap targetRow = target.getRawData(); //Log.debug("looking for x-schema relation: " + sourceRow + "; " + targetRow); final Relation relation = Relation.getRelation(sourceRow, targetRow); if (relation != null) { links.add(makeLink(rowNode, target, relation)); targetsUsed.add(target); } //makeCrossSchemaRowNodeLinks(links, sourceSchema, targetSchema, rowNode, c); } } catch (Throwable t) { Log.warn("makeRowNodeLinks: processing target: " + target, t); } } return links; }
From source file:org.dllearner.algorithms.qtl.experiments.PRConvergenceExperiment.java
private RDFResourceTree applyBaseLine(ExamplesWrapper examples, Baseline baselineApproach) { logger.info("Computing baseline..."); Collection<RDFResourceTree> posExamples = examples.posExamplesMapping.values(); Collection<RDFResourceTree> negExamples = examples.negExamplesMapping.values(); RDFResourceTree solution = null;//from w ww .jav a 2 s.c om switch (baselineApproach) { case RANDOM:// 1. String query = "SELECT ?cls WHERE {?cls a owl:Class .} ORDER BY RAND() LIMIT 1"; QueryExecution qe = qef.createQueryExecution(query); ResultSet rs = qe.execSelect(); if (rs.hasNext()) { QuerySolution qs = rs.next(); Resource cls = qs.getResource("cls"); solution = new RDFResourceTree(); solution.addChild(new RDFResourceTree(cls.asNode()), RDF.type.asNode()); } break; case MOST_POPULAR_TYPE_IN_KB:// 2. query = "SELECT ?cls WHERE {?cls a owl:Class . ?s a ?cls .} ORDER BY DESC(COUNT(?s)) LIMIT 1"; qe = qef.createQueryExecution(query); rs = qe.execSelect(); if (rs.hasNext()) { QuerySolution qs = rs.next(); Resource cls = qs.getResource("cls"); solution = new RDFResourceTree(); solution.addChild(new RDFResourceTree(cls.asNode()), RDF.type.asNode()); } break; case MOST_FREQUENT_TYPE_IN_EXAMPLES:// 3. Multiset<Node> types = HashMultiset.create(); for (RDFResourceTree ex : posExamples) { List<RDFResourceTree> children = ex.getChildren(RDF.type.asNode()); for (RDFResourceTree child : children) { types.add(child.getData()); } } Node mostFrequentType = Ordering.natural().onResultOf(new Function<Multiset.Entry<Node>, Integer>() { @Override public Integer apply(Multiset.Entry<Node> entry) { return entry.getCount(); } }).max(types.entrySet()).getElement(); solution = new RDFResourceTree(); solution.addChild(new RDFResourceTree(mostFrequentType), RDF.type.asNode()); break; case MOST_FREQUENT_EDGE_IN_EXAMPLES:// 4. Multiset<Pair<Node, Node>> pairs = HashMultiset.create(); for (RDFResourceTree ex : posExamples) { SortedSet<Node> edges = ex.getEdges(); for (Node edge : edges) { List<RDFResourceTree> children = ex.getChildren(edge); for (RDFResourceTree child : children) { pairs.add(new Pair<>(edge, child.getData())); } } } Pair<Node, Node> mostFrequentPair = Ordering.natural() .onResultOf(new Function<Multiset.Entry<Pair<Node, Node>>, Integer>() { @Override public Integer apply(Multiset.Entry<Pair<Node, Node>> entry) { return entry.getCount(); } }).max(pairs.entrySet()).getElement(); solution = new RDFResourceTree(); solution.addChild(new RDFResourceTree(mostFrequentPair.getValue()), mostFrequentPair.getKey()); break; case MOST_INFORMATIVE_EDGE_IN_EXAMPLES: // get all p-o in pos examples Multiset<Pair<Node, Node>> edgeObjectPairs = HashMultiset.create(); for (RDFResourceTree ex : posExamples) { SortedSet<Node> edges = ex.getEdges(); for (Node edge : edges) { List<RDFResourceTree> children = ex.getChildren(edge); for (RDFResourceTree child : children) { edgeObjectPairs.add(new Pair<>(edge, child.getData())); } } } double bestAccuracy = -1; solution = new RDFResourceTree(); for (Pair<Node, Node> pair : edgeObjectPairs.elementSet()) { Node edge = pair.getKey(); Node childValue = pair.getValue(); // compute accuracy int tp = edgeObjectPairs.count(pair); int fn = posExamples.size() - tp; int fp = 0; for (RDFResourceTree ex : negExamples) { // compute false positives List<RDFResourceTree> children = ex.getChildren(edge); if (children != null) { for (RDFResourceTree child : children) { if (child.getData().equals(childValue)) { fp++; break; } } } } int tn = negExamples.size() - fp; double accuracy = Heuristics.getPredictiveAccuracy(posExamples.size(), negExamples.size(), tp, tn, 1.0); // update best solution if (accuracy >= bestAccuracy) { solution = new RDFResourceTree(); solution.addChild(new RDFResourceTree(childValue), edge); bestAccuracy = accuracy; } } break; case LGG: LGGGenerator lggGenerator = new LGGGeneratorSimple(); solution = lggGenerator.getLGG(Lists.newArrayList(posExamples)); break; default: break; } logger.info("Baseline solution:\n" + owlRenderer.render(QueryTreeUtils.toOWLClassExpression(solution))); return solution; }
From source file:com.facebook.presto.raptor.storage.BucketBalancer.java
private static Multimap<String, BucketAssignment> computeAssignmentChanges(ClusterState clusterState) { Multimap<String, BucketAssignment> sourceToAllocationChanges = HashMultimap.create(); Map<String, Long> allocationBytes = new HashMap<>(clusterState.getAssignedBytes()); Set<String> activeNodes = clusterState.getActiveNodes(); for (Distribution distribution : clusterState.getDistributionAssignments().keySet()) { // number of buckets in this distribution assigned to a node Multiset<String> allocationCounts = HashMultiset.create(); Collection<BucketAssignment> distributionAssignments = clusterState.getDistributionAssignments() .get(distribution);/*from w ww . ja v a2 s .c o m*/ distributionAssignments.stream().map(BucketAssignment::getNodeIdentifier) .forEach(allocationCounts::add); int currentMin = allocationBytes.keySet().stream().mapToInt(allocationCounts::count).min().getAsInt(); int currentMax = allocationBytes.keySet().stream().mapToInt(allocationCounts::count).max().getAsInt(); int numBuckets = distributionAssignments.size(); int targetMin = (int) Math.floor((numBuckets * 1.0) / clusterState.getActiveNodes().size()); int targetMax = (int) Math.ceil((numBuckets * 1.0) / clusterState.getActiveNodes().size()); log.info("Distribution %s: Current bucket skew: min %s, max %s. Target bucket skew: min %s, max %s", distribution.getId(), currentMin, currentMax, targetMin, targetMax); for (String source : ImmutableSet.copyOf(allocationCounts)) { List<BucketAssignment> existingAssignments = distributionAssignments.stream() .filter(assignment -> assignment.getNodeIdentifier().equals(source)).collect(toList()); for (BucketAssignment existingAssignment : existingAssignments) { if (activeNodes.contains(source) && allocationCounts.count(source) <= targetMin) { break; } // identify nodes with bucket counts lower than the computed target, and greedily select from this set based on projected disk utilization. // greediness means that this may produce decidedly non-optimal results if one looks at the global distribution of buckets->nodes. // also, this assumes that nodes in a cluster have identical storage capacity String target = activeNodes.stream() .filter(candidate -> !candidate.equals(source) && allocationCounts.count(candidate) < targetMax) .sorted(comparingInt(allocationCounts::count)) .min(Comparator.comparingDouble(allocationBytes::get)) .orElseThrow(() -> new VerifyException("unable to find target for rebalancing")); long bucketSize = clusterState.getDistributionBucketSize().get(distribution); // only move bucket if it reduces imbalance if (activeNodes.contains(source) && (allocationCounts.count(source) == targetMax && allocationCounts.count(target) == targetMin)) { break; } allocationCounts.remove(source); allocationCounts.add(target); allocationBytes.compute(source, (k, v) -> v - bucketSize); allocationBytes.compute(target, (k, v) -> v + bucketSize); sourceToAllocationChanges.put(existingAssignment.getNodeIdentifier(), new BucketAssignment( existingAssignment.getDistributionId(), existingAssignment.getBucketNumber(), target)); } } } return sourceToAllocationChanges; }
From source file:org.splevo.jamopp.vpm.analyzer.programdependency.JaMoPPProgramDependencyVPMAnalyzer.java
private List<VPMEdgeDescriptor> identifyRelatedVPsForReferencedElement(List<String> edgeRegistry, Commentable referencedElement, ReferenceSelector referenceSelector, VPReferenceIndex index, Multiset<DependencyType> statistics) { List<VPMEdgeDescriptor> referencedElementEdges = Lists.newArrayList(); Set<VariationPoint> referencingVPs = index.referencedElementsIndex.get(referencedElement); if (referencingVPs.size() > 1) { VariationPoint[] vpList = referencingVPs.toArray(new VariationPoint[referencingVPs.size()]); for (int i = 0; i < vpList.length; i++) { for (int j = i + 1; j < vpList.length; j++) { VariationPoint vp1 = vpList[i]; VariationPoint vp2 = vpList[j]; if (vp1 == null || vp2 == null || vp2 == vp1) { continue; }/*from w w w . ja v a2s.c om*/ // every VP can have more than one reference to a referenced element // so we need to search for acceptable ones // To track statistics about the analysis, we must scan all dependencies even // if the first one would be enough for identifying a dependency. // if more than one reference exists between two VPs, the last one will be kept // FIXME: Implementation must be cleaned up DependencyType dependencyType = DependencyType.IGNORE; Reference referenceVP1 = null; Reference referenceVP2 = null; for (Reference refVP1 : index.getIndexedReferences(vp1, referencedElement)) { for (Reference refVP2 : index.getIndexedReferences(vp2, referencedElement)) { DependencyType type = referenceSelector.getDependencyType(refVP1, refVP2, referencedElement); if (isDesiredType(type)) { statistics.add(type); dependencyType = type; referenceVP1 = refVP1; referenceVP2 = refVP2; } } } // if we did not find a reasonable VP dependency continue with the next VP pair. if (dependencyType == DependencyType.IGNORE) { continue; } Node nodeVP1 = index.vp2GraphNodeIndex.get(vp1); Node nodeVP2 = index.vp2GraphNodeIndex.get(vp2); String vp1ID = nodeVP1.getId(); String vp2ID = nodeVP2.getId(); // the source of the second VP forms the target label as we know // talk about source and target of the VP relationship and no longer about // dependencies between the software elements. String sourceLabel = JaMoPPElementUtil.getLabel(referenceVP1.getSource()); String targetLabel = JaMoPPElementUtil.getLabel(referenceVP2.getSource()); String subLabel = getSubLabel(referencedElement, referenceVP1.getSource(), referenceVP2.getSource(), sourceLabel, targetLabel); VPMEdgeDescriptor edge = buildEdgeDescriptor(nodeVP1, nodeVP2, subLabel, edgeRegistry); if (edge != null) { edge.getRelationShipInfos().put(EDGE_INFO_DEPENDENCY_TYPE, dependencyType); logAnalysisInfo(vp1ID, vp2ID, sourceLabel, targetLabel, subLabel); referencedElementEdges.add(edge); } } } } return referencedElementEdges; }