List of usage examples for java.lang Double compare
public static int compare(double d1, double d2)
From source file:wa.was.blastradius.managers.TNTEffectsManager.java
public void loadEffect(File effectFile) { if (!(effectFile.exists())) { return;//from w w w . j a v a 2s. c o m } YamlConfiguration effect = new YamlConfiguration(); Map<String, Object> effectInfo = new HashMap<String, Object>(); try { effect.load(effectFile); String effectName = effect.getString("effect-name", "DEFAULT").toUpperCase(); effectInfo.put("type", effectName); effectInfo.put("fuseTicks", effect.getInt("fuse-ticks", 80)); effectInfo.put("vaultCost", effect.getDouble("cost", 10.0)); if (Double.compare(effect.getDouble("worth", 10), 0.1) > 0) { effectInfo.put("vaultWorth", effect.getDouble("worth", 0.1)); } else { Bukkit.getServer().getLogger().warning("Vault Worth cannot be less than 0.1. Vault cost entered: " + effect.get("worth") + " for TNT Effect: " + effectName + ". Defaulting..."); effectInfo.put("vaultWorth", 0.1); } int tTexture = effect.getInt("tnt-texture-value", 1); int dTexture = effect.getInt("detonator-texture-value", 1); short ntTexture = tTexture > Short.MAX_VALUE ? Short.MAX_VALUE : tTexture < Short.MIN_VALUE ? Short.MIN_VALUE : (short) tTexture; short ndTexture = dTexture > Short.MAX_VALUE ? Short.MAX_VALUE : dTexture < Short.MIN_VALUE ? Short.MIN_VALUE : (short) dTexture; effectInfo.put("explosiveTexture", ntTexture); effectInfo.put("detonatorTexture", ndTexture); effectInfo.put("remoteDetonation", effect.getBoolean("remote-detonation", false)); if (Material.valueOf(effect.getString("remote-detonator-material", "STONE_BUTTON")) != null) { effectInfo.put("remoteDetonator", Material.valueOf(effect.getString("remote-detonator-material", "STONE_BUTTON"))); } else { Bukkit.getServer().getLogger() .warning("Remote Detonator Material Invalid: " + effect.get("remote-detonator-material") + " for TNT Effect: " + effectName + ". Defaulting..."); effectInfo.put("remoteDetonator", Material.STONE_BUTTON); } effectInfo.put("displayName", ChatColor.translateAlternateColorCodes('&', effect.getString("display-name", "TNT"))); String detonatorName = effect.getString("display-name", "TNT") + " " + effect.getString("remote-detonator-tag", "&6Detonator"); effectInfo.put("remoteDetonatorName", ChatColor.translateAlternateColorCodes('&', detonatorName)); remoteDetonators.put(ChatColor.translateAlternateColorCodes('&', detonatorName), effectName); if (Sound.valueOf(effect.getString("detonator-effect", "BLOCK_NOTE_PLING")) != null) { effectInfo.put("detonatorEffect", Sound.valueOf(effect.getString("detonator-effect", "BLOCK_NOTE_PLING"))); } else { Bukkit.getServer().getLogger().warning("Detonator Effect Invalid: " + effect.get("detonator-effect") + " for TNT Effect: " + effectName + ". Defaulting..."); effectInfo.put("detonatorEffect", Sound.BLOCK_NOTE_PLING); } double effectDetonatorPitch = effect.getDouble("detonaor-effect-pitch", 1.5); if (Double.compare(effectDetonatorPitch, 2.0) > 0) { Bukkit.getServer().getLogger().warning("Detonator Effect Pitch is too high: " + effectDetonatorPitch + " for TNT Effect: " + effectName + ". Defaulting..."); effectInfo.put("detonatorEffectPitch", (float) 1.0); } else if (Double.compare(effectDetonatorPitch, 0.5) < 0) { Bukkit.getServer().getLogger().warning("Detonator Effect Pitch is too low: " + effectDetonatorPitch + " for TNT Effect: " + effectName + ". Defaulting..."); effectInfo.put("detonatorEffectPitch", (float) 1.5); } else { effectInfo.put("detonatorEffectPitch", (float) effectDetonatorPitch); } double effectDetonatorVolume = effect.getDouble("detonator-effect-volume", 1.0); if (Double.compare(effectDetonatorVolume, 12.0) > 0) { Bukkit.getServer().getLogger().warning("Detonator Effect Volume is too high: " + effectDetonatorVolume + " for TNT Effect: " + effectName + ". Defaulting..."); effectInfo.put("detontorEffectVolume", (float) 1.0); } else if (Double.compare(effectDetonatorVolume, 0.5) < 0) { Bukkit.getServer().getLogger().warning("Detonator Effect Volume is too high: " + effectDetonatorVolume + " for TNT Effect: " + effectName + ". Defaulting..."); effectInfo.put("detonatorEffectVolume", (float) 1.0); } else { effectInfo.put("detonatorEffectVolume", (float) effectDetonatorVolume); } effectInfo.put("tntReceivable", effect.getBoolean("tnt-receivable", true)); displayNames.put(ChatColor.translateAlternateColorCodes('&', (String) effectInfo.get("displayName")), effectName); List<String> effectLore = effect.getStringList("lore"); List<String> lores = new ArrayList<String>(); if (effectLore.size() > 0) { for (String line : effectLore) { lores.add(ChatColor.translateAlternateColorCodes('&', line)); } effectInfo.put("lore", lores); } if (Sound.valueOf(effect.getString("fuse-effect", "ENTITY_TNT_PRIMED")) != null) { effectInfo.put("fuseEffect", Sound.valueOf(effect.getString("fuse-effect", "ENTITY_TNT_PRIMED"))); } else { Bukkit.getServer().getLogger().warning("Fuse Effect Invalid: " + effect.get("fuse-effect") + " for TNT Effect: " + effectName + ". Defaulting..."); effectInfo.put("fuseEffect", Sound.ENTITY_TNT_PRIMED); } double effectPitch = effect.getDouble("fuse-effect-pitch", 1.0); if (Double.compare(effectPitch, 2.0) > 0) { Bukkit.getServer().getLogger().warning("Fuse Effect Pitch is too high: " + effectPitch + " for TNT Effect: " + effectName + ". Defaulting..."); effectInfo.put("fuseEffectPitch", (float) 1.0); } else if (Double.compare(effectPitch, 0.5) < 0) { Bukkit.getServer().getLogger().warning("Fuse Effect Pitch is too low: " + effectPitch + " for TNT Effect: " + effectName + ". Defaulting..."); effectInfo.put("fuseEffectPitch", (float) 1.0); } else { effectInfo.put("fuseEffectPitch", (float) effectPitch); } double effectVolume = effect.getDouble("fuse-effect-volume", 1.0); if (Double.compare(effectVolume, 12.0) > 0) { Bukkit.getServer().getLogger().warning("Fuse Effect Volume is too high: " + effectVolume + " for TNT Effect: " + effectName + ". Defaulting..."); effectInfo.put("fuseEffectVolume", (float) 1.0); } else if (Double.compare(effectVolume, 0.5) < 0) { Bukkit.getServer().getLogger().warning("Fuse Effect Volume is too high: " + effectVolume + " for TNT Effect: " + effectName + ". Defaulting..."); effectInfo.put("fuseEffectVolume", (float) 1.0); } else { effectInfo.put("fuseEffectVolume", (float) effectVolume); } double explosionVolume = effect.getDouble("sound-explosion-volume", 2); if (Double.compare(explosionVolume, 12.0) > 0) { Bukkit.getServer().getLogger().warning("Sound Explosion volume is too high: " + explosionVolume + " for TNT Effect: " + effectName + ". Defaulting..."); effectInfo.put("explosionVolume", (float) 2); } else { effectInfo.put("explosionVolume", (float) explosionVolume); } effectInfo.put("tamperProof", effect.getBoolean("tamper-proof", false)); effectInfo.put("doWaterDamage", effect.getBoolean("tnt-water-damage", false)); effectInfo.put("doFires", effect.getBoolean("blast-fires", true)); effectInfo.put("doSmoke", effect.getBoolean("blast-smoke", false)); effectInfo.put("obliterate", effect.getBoolean("obliterate-obliterables", false)); effectInfo.put("ellipsis", effect.getBoolean("elliptical-radius", true)); if (Double.compare(effect.getDouble("blast-yield-multiplier", 1F), 20.0) < 0) { effectInfo.put("yieldMultiplier", (float) effect.getDouble("blast-yield-multiplier", 1F)); } else { Bukkit.getServer().getLogger() .warning("Blast Multiplier out of Range: " + effect.get("blast-yield-multiplier") + " for TNT Effect: " + effectName + ". Defaulting..."); effectInfo.put("yieldMultiplier", (float) 1F); } if (effect.getInt("blast-radius", 10) <= 50) { effectInfo.put("blastRadius", effect.getInt("blast-radius", 10)); } else { Bukkit.getServer().getLogger().warning("Dead Zone out of Range: " + effect.get("blast-radius") + " for TNT Effect: " + effectName + ". Defaulting..."); effectInfo.put("blastRadius", (int) 10); } if (effect.getInt("fire-radius", 9) <= 50) { effectInfo.put("fireRadius", effect.getInt("fire-radius", 9)); } else { Bukkit.getServer().getLogger().warning("Fire Radius out of Range: " + effect.get("fire-radius") + " for TNT Effect: " + effectName + ". Defaulting..."); effectInfo.put("fireRadius", (int) 9); } if (effect.getInt("smoke-count", 10) <= 100) { effectInfo.put("smokeCount", effect.getInt("smoke-count", 10)); } else { Bukkit.getServer().getLogger().warning("Smoke Count out of Range: " + effect.get("smoke-count") + " for TNT Effect: " + effectName + ". Defaulting..."); effectInfo.put("smokeCount", (int) 10); } if (Double.compare(effect.getDouble("smoke-offset", 0.25), 10) < 0) { effectInfo.put("smokeOffset", effect.getDouble("smoke-offset", 0.25)); } else { Bukkit.getServer().getLogger().warning("Smoke Offset out of Range: " + effect.get("smoke-offset") + " for TNT Effect: " + effectName + ". Defaulting..."); effectInfo.put("smokeOffset", (double) 0.25); } effectInfo.put("tntTossable", effect.getBoolean("tnt-tossable", false)); if (effect.getInt("tnt-tossable-height", 3) < 256) { effectInfo.put("tossHeightGain", effect.getInt("tnt-tossed-height", 3)); } else { Bukkit.getServer().getLogger().warning("TNT Tossable Height gain out of Range: " + effect.get("tnt-tossable-height") + " for TNT Effect: " + effectName + ". Defaulting..."); effectInfo.put("tossHeightGain", (int) 3); } if (Double.compare(effect.getDouble("tnt-tossable-force", 1.5), 5.0) < 0) { effectInfo.put("tossForce", effect.getDouble("tnt-tossable-force", 1.5)); } else { Bukkit.getServer().getLogger().warning("TNT Force to Strong. Forced used: " + effect.get("tnt-tossable-force") + " for TNT Effect: " + effectName + ". Defaulting..."); effectInfo.put("tossForce", (double) 1.5); } if (effect.getInt("tnt-tossable-cooldown", 10) < 1) { Bukkit.getServer().getLogger() .warning("TNT Tossable Cooldown must be above 1. Value found: " + effect.get("tnt-tossable-cooldown") + " for TNT Effect: " + effectName + ". Defaulting..."); effectInfo.put("tossCooldown", (int) 10); } else { effectInfo.put("tossCooldown", effect.getInt("tnt-tossable-cooldown", 10)); } effectInfo.put("doCluster", effect.getBoolean("tnt-cluster-effect", false)); if (effect.getInt("tnt-cluster-effect-amount", 3) > 10) { Bukkit.getServer().getLogger() .warning("TNT Cluster Effect Amount must be between 1 - 10: " + effect.get("tnt-cluster-effect-amount") + " for TNT Effect: " + effectName + ". Defaulting..."); effectInfo.put("clusterAmount", (int) 3); } else { effectInfo.put("clusterAmount", effect.getInt("tnt-cluster-effect-amount", 3)); } effectInfo.put("clusterType", effect.getString("tnt-cluster-effect-type", "DEFAULT")); if (Double.compare(effect.getDouble("tnt-cluster-effect-height-offset", 10.0), 30.0) > 0) { Bukkit.getServer().getLogger() .warning("TNT Cluster Effect Amount must be between 1 - 10: " + effect.get("tnt-cluster-effect-amount") + " for TNT Effect: " + effectName + ". Defaulting..."); effectInfo.put("clusterOffset", (double) 10.0); } else { effectInfo.put("clusterOffset", effect.getDouble("tnt-cluster-effect-height-offset", 10.0)); } effectInfo.put("doPotions", effect.getBoolean("potion-effect", true)); effectInfo.put("consecPotions", effect.getBoolean("consecutive-potion-effects", false)); effectInfo.put("showPotionMsg", effect.getBoolean("show-potion-message", true)); effectInfo.put("potionMsg", effect.getString("potoin-message", "&1You have been &2effected &1with &c{TYPE} &1for &6{TIME} &rseconds")); List<PotionEffect> potionEffects = new ArrayList<PotionEffect>(); List<String> messages = new ArrayList<String>(); if (effect.getConfigurationSection("potion-effects").getKeys(false).size() > 0) { for (String type : effect.getConfigurationSection("potion-effects").getKeys(false)) { if (effectTypes .containsKey(effect.getString("potion-effects." + type + ".type").toUpperCase())) { if (effect.getString("potion-effects." + type + ".color") != null && colors.containsKey(effect.getString("potion-effects." + type + ".color"))) { potionEffects.add(new PotionEffect( effectTypes.get( effect.getString("potion-effects." + type + ".type").toUpperCase()), effect.getInt("potion-effects." + type + ".duration"), effect.getInt("potion-effects." + type + ".amplifier"), effect.getBoolean("potion-effects." + type + ".ambient"), effect.getBoolean("potion-effects." + type + ".particles"), colors.get(effect.getString("potion-effects." + type + ".color")))); } else { potionEffects.add(new PotionEffect( effectTypes.get( effect.getString("potion-effects." + type + ".type").toUpperCase()), effect.getInt("potion-effects." + type + ".duration"), effect.getInt("potion-effects." + type + ".amplifier"), effect.getBoolean("potion-effects." + type + ".ambient"), effect.getBoolean("potion-effects." + type + ".particles"))); } messages.add(effect .getString("potion-message", "&1You have been &2effected &1with &c{TYPE} &1for &6{TIME} &rseconds") .replace("{TYPE}", WordUtils.capitalize( effect.getString("potion-effects." + type + ".type").toLowerCase())) .replace("{TIME}", "" + (effect.getInt("potion-effects." + type + ".duration") / 20))); } } } potionEffectsManager.addMessages(effectName, messages); potionEffectsManager.addAllEffects(effectName, potionEffects); List<Material> innerMaterials = new ArrayList<Material>(); if (effect.getStringList("inner-blast-materials").size() > 0) { for (String mat : effect.getStringList("inner-blast-materials")) { if (Material.valueOf(mat) != null) { innerMaterials.add(Material.valueOf(mat)); } else { Bukkit.getServer().getLogger().warning("[BlastRadius] Invalid Inner Material: " + mat + " for TNT Effect: " + effectName + ". Skipping..."); } } } effectInfo.put("innerMaterials", innerMaterials); List<Material> outerMaterials = new ArrayList<Material>(); if (effect.getStringList("outer-blast-materials").size() > 0) { for (String mat : effect.getStringList("outer-blast-materials")) { if (Material.valueOf(mat) != null) { outerMaterials.add(Material.valueOf(mat)); } else { Bukkit.getServer().getLogger().warning("[BlastRadius] Invalid Outer Material: " + mat + " for TNT Effect: " + effectName + ". Skipping..."); } } } effectInfo.put("outerMaterials", outerMaterials); List<Material> protectedMaterials = new ArrayList<Material>(); if (effect.getStringList("protected-materials").size() > 0) { for (String mat : effect.getStringList("protected-materials")) { if (Material.valueOf(mat) != null) { protectedMaterials.add(Material.valueOf(mat)); } else { Bukkit.getServer().getLogger().warning("[BlastRadius] Invalid Protected Material: " + mat + " for TNT Effect: " + effectName + ". Skipping..."); } } } effectInfo.put("protectedMaterials", protectedMaterials); List<Material> obliterateMaterials = new ArrayList<Material>(); if (effect.getStringList("obliterate-materials").size() > 0) { for (String mat : effect.getStringList("obliterate-materials")) { if (Material.valueOf(mat) != null) { obliterateMaterials.add(Material.valueOf(mat)); } else { Bukkit.getServer().getLogger().warning("[BlastRadius] Invalid Obliterate Material: " + mat + " for TNT Effect: " + effectName + ". Skipping..."); } } } effectInfo.put("obliterateMaterials", obliterateMaterials); Bukkit.getServer().getLogger().warning("[BlastRadius] Added BlastR Brand TNT Effect: " + ConsoleColor.YELLOW + ConsoleColor.BOLD + effectName + ConsoleColor.RESET); effects.put(effectName, effectInfo); } catch (IOException | InvalidConfigurationException e) { e.printStackTrace(); } }
From source file:sadl.models.PDTTAold.java
public TimedWord sampleSequence() { int currentState = START_STATE; final List<String> eventList = new ArrayList<>(); final TIntList timeList = new TIntArrayList(); boolean choseFinalState = false; while (!choseFinalState) { final List<Transition> possibleTransitions = getTransitions(currentState, true); Collections.sort(possibleTransitions, (t1, t2) -> -Double.compare(t2.getProbability(), t1.getProbability())); final double random = r.nextDouble(); double summedProbs = 0; int index = -1; for (int i = 0; i < possibleTransitions.size(); i++) { summedProbs += possibleTransitions.get(i).getProbability(); if (random < summedProbs) { index = i;/*from www .j ava 2 s. co m*/ break; } } final Transition chosenTransition = possibleTransitions.get(index); if (chosenTransition.isStopTraversingTransition()) { choseFinalState = true; } else if (eventList.size() > MAX_SEQUENCE_LENGTH) { throw new IllegalStateException( "A sequence longer than " + MAX_SEQUENCE_LENGTH + " events should have been generated"); } else { currentState = chosenTransition.getToState(); final Distribution d = transitionDistributions.get(chosenTransition.toZeroProbTransition()); if (d == null) { // maybe this happens because the automaton is more general than the data. So not every possible path in the automaton is represented in // the training data. throw new IllegalStateException("This should never happen for transition " + chosenTransition); } final int timeValue = (int) d.sample(1, r)[0]; eventList.add(chosenTransition.getSymbol()); timeList.add(timeValue); } } return new TimedWord(eventList, timeList, ClassLabel.NORMAL); }
From source file:org.apache.pig.builtin.Utf8StorageConverter.java
@Override public Long bytesToLong(byte[] b) throws IOException { if (b == null || b.length == 0) { return null; }//w w w . jav a2 s . c om String s = new String(b).trim(); if (s.endsWith("l") || s.endsWith("L")) { s = s.substring(0, s.length() - 1); } // See PIG-2835. Using exception handling to check if it's a double is very expensive. // So we write our sanity check. Long ret = null; if (sanityCheckIntegerLong(s)) { try { ret = Long.valueOf(s); } catch (NumberFormatException nfe) { } } if (ret == null) { // It's possible that this field can be interpreted as a double. // Unfortunately Java doesn't handle this in Long.valueOf. So // we need to try to convert it to a double and if that works then // go to an long. try { Double d = Double.valueOf(s); // Need to check for an overflow error if (Double.compare(d.doubleValue(), mMaxLong.doubleValue() + 1) > 0 || Double.compare(d.doubleValue(), mMinLong.doubleValue() - 1) < 0) { LogUtils.warn(this, "Value " + d + " too large for long", PigWarning.TOO_LARGE_FOR_INT, mLog); return null; } return Long.valueOf(d.longValue()); } catch (NumberFormatException nfe2) { LogUtils.warn(this, "Unable to interpret value " + Arrays.toString(b) + " in field being " + "converted to long, caught NumberFormatException <" + nfe2.getMessage() + "> field discarded", PigWarning.FIELD_DISCARDED_TYPE_CONVERSION_FAILED, mLog); return null; } } return ret; }
From source file:org.um.feri.ears.algorithms.moo.dbea.DBEA.java
/** * Returns a copy of the population sorted by the sum-of-squares of all * but one objective./* w w w .j a v a 2 s. co m*/ * * @param objective the ignored objective * @param population the population * @return a copy of the population ordered by the sum-of-squares of all * but one objective */ private ParetoSolution<Type> orderBySmallestSquaredValue(final int objective, ParetoSolution<Type> population) { ParetoSolution<Type> result = new ParetoSolution<Type>(); result.addAll(population); result.sort(new Comparator<MOSolutionBase<Type>>() { @Override public int compare(MOSolutionBase<Type> s1, MOSolutionBase<Type> s2) { double sum1 = 0.0; double sum2 = 0.0; for (int i = 0; i < num_obj; i++) { if (i != objective) { sum1 += Math.pow(s1.getObjective(i), 2.0); sum2 += Math.pow(s2.getObjective(i), 2.0); } } return Double.compare(sum1, sum2); } }); return result; }
From source file:com.opengamma.analytics.financial.credit.creditdefaultswap.definition.vanilla.CreditDefaultSwapDefinition.java
@Override public boolean equals(final Object obj) { if (this == obj) { return true; }/* w w w . j a v a 2s .c om*/ if (!(obj instanceof CreditDefaultSwapDefinition)) { return false; } final CreditDefaultSwapDefinition other = (CreditDefaultSwapDefinition) obj; if (!ObjectUtils.equals(_creditKey, other._creditKey)) { return false; } if (Double.compare(_notional, other._notional) != 0) { return false; } if (!ObjectUtils.equals(_maturityDate, other._maturityDate)) { return false; } if (!ObjectUtils.equals(_startDate, other._startDate)) { return false; } if (!ObjectUtils.equals(_currency, other._currency)) { return false; } if (!ObjectUtils.equals(_protectionBuyer, other._protectionBuyer)) { return false; } if (!ObjectUtils.equals(_protectionSeller, other._protectionSeller)) { return false; } if (!ObjectUtils.equals(_referenceEntity, other._referenceEntity)) { return false; } if (Double.compare(_protectionOffset, other._protectionOffset) != 0) { return false; } if (_protectionStart != other._protectionStart) { return false; } if (_buySellProtection != other._buySellProtection) { return false; } if (Double.compare(_recoveryRate, other._recoveryRate) != 0) { return false; } if (ObjectUtils.equals(_effectiveDate, other._effectiveDate)) { return false; } if (_adjustEffectiveDate != other._adjustEffectiveDate) { return false; } if (_adjustMaturityDate != other._adjustMaturityDate) { return false; } if (!ObjectUtils.equals(_couponFrequency, other._couponFrequency)) { return false; } if (!ObjectUtils.equals(_businessdayAdjustmentConvention, other._businessdayAdjustmentConvention)) { return false; } if (!ObjectUtils.equals(_daycountFractionConvention, other._daycountFractionConvention)) { return false; } if (_debtSeniority != other._debtSeniority) { return false; } if (_immAdjustMaturityDate != other._immAdjustMaturityDate) { return false; } if (_includeAccruedPremium != other._includeAccruedPremium) { return false; } if (_restructuringClause != other._restructuringClause) { return false; } if (_stubType != other._stubType) { return false; } if (!ObjectUtils.equals(_calendar, other._calendar)) { return false; } return true; }
From source file:io.anserini.rerank.lib.AxiomReranker.java
/** * Calculate the scores (weights) of each term that occured in the reranking pool. * The Process:/* ww w .j a v a2 s.c om*/ * 1. For each query term, calculate its score for each term in the reranking pool. the score * is calcuated as * <pre> * P(both occurs)*log{P(both occurs)/P(t1 occurs)/P(t2 occurs)} * + P(both not occurs)*log{P(both not occurs)/P(t1 not occurs)/P(t2 not occurs)} * + P(t1 occurs t2 not occurs)*log{P(t1 occurs t2 not occurs)/P(t1 occurs)/P(t2 not occurs)} * + P(t1 not occurs t2 occurs)*log{P(t1 not occurs t2 occurs)/P(t1 not occurs)/P(t2 occurs)} * </pre> * 2. For each query term the scores of every other term in the reranking pool are stored in a * PriorityQueue, only the top {@code K} are kept. * 3. Add the scores of the same term together and pick the top {@code M} ones. * * @param termInvertedList A Map of <term -> Set<docId>> where the Set of docIds is where the term occurs * @param context An instance of RerankerContext * @return Map<String, Double> Top terms and their weight scores in a HashMap */ private Map<String, Double> computeTermScore(Map<String, Set<Integer>> termInvertedList, RerankerContext<T> context) throws IOException { class ScoreComparator implements Comparator<Pair<String, Double>> { public int compare(Pair<String, Double> a, Pair<String, Double> b) { int cmp = Double.compare(b.getRight(), a.getRight()); if (cmp == 0) { return a.getLeft().compareToIgnoreCase(b.getLeft()); } else { return cmp; } } } // get collection statistics so that we can get idf later on. IndexReader reader; if (this.externalIndexPath != null) { Path indexPath = Paths.get(this.externalIndexPath); if (!Files.exists(indexPath) || !Files.isDirectory(indexPath) || !Files.isReadable(indexPath)) { throw new IllegalArgumentException( this.externalIndexPath + " does not exist or is not a directory."); } reader = DirectoryReader.open(FSDirectory.open(indexPath)); } else { IndexSearcher searcher = context.getIndexSearcher(); reader = searcher.getIndexReader(); } final long docCount = reader.numDocs() == -1 ? reader.maxDoc() : reader.numDocs(); //calculate the Mutual Information between term with each query term List<String> queryTerms = context.getQueryTokens(); Map<String, Integer> queryTermsCounts = new HashMap<>(); for (String qt : queryTerms) { queryTermsCounts.put(qt, queryTermsCounts.getOrDefault(qt, 0) + 1); } Set<Integer> allDocIds = new HashSet<>(); for (Set<Integer> s : termInvertedList.values()) { allDocIds.addAll(s); } int docIdsCount = allDocIds.size(); // Each priority queue corresponds to a query term: The p-queue itself stores all terms // in the reranking pool and their reranking scores to the query term. List<PriorityQueue<Pair<String, Double>>> allTermScoresPQ = new ArrayList<>(); for (Map.Entry<String, Integer> q : queryTermsCounts.entrySet()) { String queryTerm = q.getKey(); long df = reader.docFreq(new Term(LuceneDocumentGenerator.FIELD_BODY, queryTerm)); if (df == 0L) { continue; } float idf = (float) Math.log((1 + docCount) / df); int qtf = q.getValue(); if (termInvertedList.containsKey(queryTerm)) { PriorityQueue<Pair<String, Double>> termScorePQ = new PriorityQueue<>(new ScoreComparator()); double selfMI = computeMutualInformation(termInvertedList.get(queryTerm), termInvertedList.get(queryTerm), docIdsCount); for (Map.Entry<String, Set<Integer>> termEntry : termInvertedList.entrySet()) { double score; if (termEntry.getKey().equals(queryTerm)) { // The mutual information to itself will always be 1 score = idf * qtf; } else { double crossMI = computeMutualInformation(termInvertedList.get(queryTerm), termEntry.getValue(), docIdsCount); score = idf * beta * qtf * crossMI / selfMI; } termScorePQ.add(Pair.of(termEntry.getKey(), score)); } allTermScoresPQ.add(termScorePQ); } } Map<String, Double> aggTermScores = new HashMap<>(); for (PriorityQueue<Pair<String, Double>> termScores : allTermScoresPQ) { for (int i = 0; i < Math.min(termScores.size(), this.K); i++) { Pair<String, Double> termScore = termScores.poll(); String term = termScore.getLeft(); Double score = termScore.getRight(); if (score - 0.0 > 1e-8) { aggTermScores.put(term, aggTermScores.getOrDefault(term, 0.0) + score); } } } PriorityQueue<Pair<String, Double>> termScoresPQ = new PriorityQueue<>(new ScoreComparator()); for (Map.Entry<String, Double> termScore : aggTermScores.entrySet()) { termScoresPQ.add(Pair.of(termScore.getKey(), termScore.getValue() / queryTerms.size())); } Map<String, Double> resultTermScores = new HashMap<>(); for (int i = 0; i < Math.min(termScoresPQ.size(), this.M); i++) { Pair<String, Double> termScore = termScoresPQ.poll(); String term = termScore.getKey(); double score = termScore.getValue(); resultTermScores.put(term, score); } return resultTermScores; }
From source file:ml.shifu.shifu.core.dtrain.lr.LogisticRegressionWorker.java
@Override public void load(GuaguaWritableAdapter<LongWritable> currentKey, GuaguaWritableAdapter<Text> currentValue, WorkerContext<LogisticRegressionParams, LogisticRegressionParams> context) { ++this.count; if ((this.count) % 100000 == 0) { LOG.info("Read {} records.", this.count); }//from www . j a v a 2 s . c o m String line = currentValue.getWritable().toString(); float[] inputData = new float[inputNum]; float[] outputData = new float[outputNum]; int index = 0, inputIndex = 0, outputIndex = 0; long hashcode = 0; double significance = CommonConstants.DEFAULT_SIGNIFICANCE_VALUE; boolean hasCandidates = CommonUtils.hasCandidateColumns(this.columnConfigList); String[] fields = Lists.newArrayList(this.splitter.split(line)).toArray(new String[0]); int pos = 0; for (pos = 0; pos < fields.length;) { String unit = fields[pos]; // check here to avoid bad performance in failed NumberFormatUtils.getFloat(input, 0f) float floatValue = unit.length() == 0 ? 0f : NumberFormatUtils.getFloat(unit, 0f); // no idea about why NaN in input data, we should process it as missing value TODO , according to norm type floatValue = (Float.isNaN(floatValue) || Double.isNaN(floatValue)) ? 0f : floatValue; if (pos == fields.length - 1) { // do we need to check if not weighted directly set to 1f; if such logic non-weight at first, then // weight, how to process??? if (StringUtils.isBlank(modelConfig.getWeightColumnName())) { significance = 1d; // break here if we reach weight column which is last column break; } // check here to avoid bad performance in failed NumberFormatUtils.getDouble(input, 1) significance = unit.length() == 0 ? 1f : NumberFormatUtils.getDouble(unit, 1d); // if invalid weight, set it to 1f and warning in log if (Double.compare(significance, 0d) < 0) { LOG.warn( "The {} record in current worker weight {} is less than 0f, it is invalid, set it to 1.", count, significance); significance = 1d; } // the last field is significance, break here break; } else { ColumnConfig columnConfig = this.columnConfigList.get(index); if (columnConfig != null && columnConfig.isTarget()) { outputData[outputIndex++] = floatValue; pos++; } else { if (this.inputNum == this.candidateNum) { // no variable selected, good candidate but not meta and not target choosed if (!columnConfig.isMeta() && !columnConfig.isTarget() && CommonUtils.isGoodCandidate(columnConfig, hasCandidates)) { inputData[inputIndex++] = floatValue; hashcode = hashcode * 31 + Float.valueOf(floatValue).hashCode(); } pos++; } else { if (columnConfig.isFinalSelect()) { if (columnConfig.isNumerical() && modelConfig.getNormalizeType().equals(ModelNormalizeConf.NormType.ONEHOT)) { for (int k = 0; k < columnConfig.getBinBoundary().size() + 1; k++) { String tval = fields[pos]; // check here to avoid bad performance in failed NumberFormatUtils.getFloat(input, 0f) float fval = tval.length() == 0 ? 0f : NumberFormatUtils.getFloat(tval, 0f); // no idea about why NaN in input data, we should process it as missing value TODO , // according to norm type fval = (Float.isNaN(fval) || Double.isNaN(fval)) ? 0f : fval; inputData[inputIndex++] = fval; pos++; } } else if (columnConfig.isCategorical() && (modelConfig.getNormalizeType() .equals(ModelNormalizeConf.NormType.ZSCALE_ONEHOT) || modelConfig.getNormalizeType().equals(ModelNormalizeConf.NormType.ONEHOT))) { for (int k = 0; k < columnConfig.getBinCategory().size() + 1; k++) { String tval = fields[pos]; // check here to avoid bad performance in failed NumberFormatUtils.getFloat(input, 0f) float fval = tval.length() == 0 ? 0f : NumberFormatUtils.getFloat(tval, 0f); // no idea about why NaN in input data, we should process it as missing value TODO , // according to norm type fval = (Float.isNaN(fval) || Double.isNaN(fval)) ? 0f : fval; inputData[inputIndex++] = fval; pos++; } } else { inputData[inputIndex++] = floatValue; pos++; } hashcode = hashcode * 31 + Double.valueOf(floatValue).hashCode(); } else { if (!CommonUtils.isToNormVariable(columnConfig, hasCandidates, modelConfig.isRegression())) { pos += 1; } else if (columnConfig.isNumerical() && modelConfig.getNormalizeType().equals(ModelNormalizeConf.NormType.ONEHOT) && columnConfig.getBinBoundary() != null && columnConfig.getBinBoundary().size() > 0) { pos += (columnConfig.getBinBoundary().size() + 1); } else if (columnConfig.isCategorical() && (modelConfig.getNormalizeType() .equals(ModelNormalizeConf.NormType.ZSCALE_ONEHOT) || modelConfig.getNormalizeType().equals(ModelNormalizeConf.NormType.ONEHOT)) && columnConfig.getBinCategory().size() > 0) { pos += (columnConfig.getBinCategory().size() + 1); } else { pos += 1; } } } } } index += 1; } if (index != this.columnConfigList.size() || pos != fields.length - 1) { throw new RuntimeException("Wrong data indexing. ColumnConfig index = " + index + ", while it should be " + columnConfigList.size() + ". " + "Data Pos = " + pos + ", while it should be " + (fields.length - 1)); } // output delimiter in norm can be set by user now and if user set a special one later changed, this exception // is helped to quick find such issue. if (inputIndex != inputData.length) { String delimiter = context.getProps().getProperty(Constants.SHIFU_OUTPUT_DATA_DELIMITER, Constants.DEFAULT_DELIMITER); throw new RuntimeException("Input length is inconsistent with parsing size. Input original size: " + inputData.length + ", parsing size:" + inputIndex + ", delimiter:" + delimiter + "."); } // sample negative only logic here if (modelConfig.getTrain().getSampleNegOnly()) { if (this.modelConfig.isFixInitialInput()) { // if fixInitialInput, sample hashcode in 1-sampleRate range out if negative records int startHashCode = (100 / this.modelConfig.getBaggingNum()) * this.trainerId; // here BaggingSampleRate means how many data will be used in training and validation, if it is 0.8, we // should take 1-0.8 to check endHashCode int endHashCode = startHashCode + Double.valueOf((1d - this.modelConfig.getBaggingSampleRate()) * 100).intValue(); if ((modelConfig.isRegression() || (modelConfig.isClassification() && modelConfig.getTrain().isOneVsAll())) // regression or // onevsall && (int) (outputData[0] + 0.01d) == 0 // negative record && isInRange(hashcode, startHashCode, endHashCode)) { return; } } else { // if not fixed initial input, and for regression or onevsall multiple classification (regression also). // if negative record if ((modelConfig.isRegression() || (modelConfig.isClassification() && modelConfig.getTrain().isOneVsAll())) // regression or // onevsall && (int) (outputData[0] + 0.01d) == 0 // negative record && Double.compare(Math.random(), this.modelConfig.getBaggingSampleRate()) >= 0) { return; } } } Data data = new Data(inputData, outputData, significance); // up sampling logic, just add more weights while bagging sampling rate is still not changed if (modelConfig.isRegression() && isUpSampleEnabled() && Double.compare(outputData[0], 1d) == 0) { // Double.compare(ideal[0], 1d) == 0 means positive tags; sample + 1 to avoids sample count to 0 data.setSignificance(data.significance * (this.upSampleRng.sample() + 1)); } boolean isValidation = false; if (context.getAttachment() != null && context.getAttachment() instanceof Boolean) { isValidation = (Boolean) context.getAttachment(); } boolean isInTraining = addDataPairToDataSet(hashcode, data, isValidation); // do bagging sampling only for training data if (isInTraining) { float subsampleWeights = sampleWeights(outputData[0]); if (isPositive(outputData[0])) { this.positiveSelectedTrainCount += subsampleWeights * 1L; } else { this.negativeSelectedTrainCount += subsampleWeights * 1L; } // set weights to significance, if 0, significance will be 0, that is bagging sampling data.setSignificance(data.significance * subsampleWeights); } else { // for validation data, according bagging sampling logic, we may need to sampling validation data set, while // validation data set are only used to compute validation error, not to do real sampling is ok. } }
From source file:sadl.models.PDFA.java
protected Transition chooseNextTransition(int currentState) { final List<Transition> possibleTransitions = getOutTransitions(currentState, true); Collections.sort(possibleTransitions, (t1, t2) -> -Double.compare(t2.getProbability(), t1.getProbability())); final double random = r.nextDouble(); double summedProbs = 0; int index = -1; for (int i = 0; i < possibleTransitions.size(); i++) { summedProbs += possibleTransitions.get(i).getProbability(); if (random < summedProbs) { index = i;//from w ww .ja v a2 s . c o m break; } } final Transition chosenTransition = possibleTransitions.get(index); return chosenTransition; }
From source file:sadl.models.TauPTA.java
private List<UntimedSequence> insertSequentialAnomaly(IntUnaryOperator f) { final Set<UntimedSequence> allSequences = getAllSequences(); final TObjectDoubleMap<UntimedSequence> sequenceProbabilities = computeEventProbabilities(allSequences); // this function may be one lamba with the streaming interface // List<UntimedSequence> abnormalSequences = getAllSequences().stream().sort(one way or the other depending on the type of anomaly).take First $K$ // elements.collect(as List) logger.debug("AllSequences.size()={}", allSequences.size()); logger.debug("Transitions.size()={}", getTransitionCount()); final Comparator<UntimedSequence> c = (s1, s2) -> { final int probCompare = Double.compare(sequenceProbabilities.get(s1), sequenceProbabilities.get(s2)); if (probCompare != 0) { return f.applyAsInt(probCompare); } else {// w w w. j a v a 2s . co m return f.applyAsInt(s1.toString().compareTo(s2.toString())); } }; logger.debug("Transitions.size()={}", transitions.size()); final int cap = Math.min(SEQUENTIAL_ANOMALY_K, allSequences.size()); return allSequences.stream().sorted(c).limit(cap).peek(s -> labelWithAnomaly(s, getAnomalyType())) .collect(Collectors.toList()); // allSequences.sort((t1, t2) -> Double.compare(sequenceProbabilities.get(t1), sequenceProbabilities.get(t2))); // final List<UntimedSequence> abnormalSequences = function.apply(allSequences); // abnormalSequences.forEach(s -> labelWithAnomaly(s,getAnomalyType())); }
From source file:ml.shifu.dtrain.DTrainRequestProcessor.java
private void validateDoubleAndRange(Params params, String str, Double start, Double end) { Object object = params.get(str); if (object != null) { try {//from w w w . j av a2 s .com Double dValue = Double.valueOf(object.toString()); if (Double.compare(dValue, start) < 0 || Double.compare(dValue, end) > 0) { throw new IllegalArgumentException( String.format("%s should be in range (%s, %s).", str, start, end)); } } catch (NumberFormatException e) { throw new IllegalArgumentException(String.format("%s should be a double field.", str)); } } }