📄 hardpairwiseselector.java
字号:
pairList.add(pair); checksumList.add(diffInstance); } } else { // this checksum has not been encountered before pairList.add(pair); ArrayList checksumList = new ArrayList(); checksumList.add(diffInstance); checksumMap.put(new Double(checksum), checksumList); } } else { // this is not a LearnableMetric pairList.add(pair); } } return pairList; } /** Add a pair to the set so that there are no collisions * @param set a set to which a new pair should be added * @param pair a new pair that is to be added; value is the distance between the instances * @return the unique value of the distance (possibly perturbed) with which the pair was added */ protected double addUniquePair(TreeSet set, TrainingPair pair) { Random random = new Random(); double epsilon = 0.00001; int counter = 0; while (set.contains(pair)) { double perturbation; if (pair.value == 0) { perturbation = Double.MIN_VALUE * random.nextInt(m_numPotentialPositives); } else { perturbation = pair.value * epsilon * ((random.nextDouble() > 0.5) ? 1 : -1); } pair.value += perturbation; counter++; if (counter % 10 == 0) { epsilon *= 10; } } set.add(pair); return pair.value; } /** Populate a treeset with all positive TrainingPair's * @param metric a metric that will be used to calculate distance * @param pairSet an empty set that will be populated * @return an array with distance values of the created pairs */ protected double[] populatePositivePairSet(Metric metric, TreeSet pairSet) throws Exception { // Create a map with *all* positives double [] posPairDistances = new double[m_numPotentialPositives]; int posCounter = 0; // go through lists of instances for each class Iterator iterator = m_classInstanceMap.values().iterator(); while (iterator.hasNext()) { ArrayList instanceList = (ArrayList) iterator.next(); for (int i = 0; i < instanceList.size(); i++) { Instance instance1 = (Instance) instanceList.get(i); for (int j = i+1; j < instanceList.size(); j++) { Instance instance2 = (Instance) instanceList.get(j); TrainingPair pair = new TrainingPair(instance1, instance2, true, metric.distance(instance1, instance2)); // add the pair to the set posPairDistances[posCounter++] = addUniquePair(pairSet, pair); } } } return posPairDistances; } /** Populate a treeset with all negative TrainingPair's * @param metric a metric that will be used to calculate distance * @param pairSet an empty set that will be populated * @return an array with distance values of the created pairs */ protected double[] populateNegativePairSet(Metric metric, TreeSet pairSet) throws Exception { double [] negPairDistances = new double[m_numPotentialNegatives]; int negCounter = 0; // go through lists of instances for each class for (int i = 0; i < m_classValueList.size(); i++) { ArrayList instanceList1 = (ArrayList) m_classInstanceMap.get(m_classValueList.get(i)); for (int j = 0; j < instanceList1.size(); j++) { Instance instance1 = (Instance) instanceList1.get(j); for (int k = i+1; k < m_classValueList.size(); k++) { ArrayList instanceList2 = (ArrayList) m_classInstanceMap.get(m_classValueList.get(k)); for (int l = 0; l < instanceList2.size(); l++) { Instance instance2 = (Instance) instanceList2.get(l); TrainingPair pair = new TrainingPair(instance1, instance2, false, metric.distance(instance1, instance2)); negPairDistances[negCounter++] = addUniquePair(pairSet, pair); } } } } return negPairDistances; } /** Given a set, return a TreeSet whose items are accessed in descending order * @param set any set containing Comparable objects * @return a new ordered set with those objects in reverse order */ public TreeSet reverseCopy(Set set) { TreeSet reverseSet = new TreeSet(new ReverseComparator()); reverseSet.addAll(set); return reverseSet; } /** Set the selection mode for positives * @param mode selection mode */ public void setPositivesMode(SelectedTag mode) { if (mode.getTags() == TAGS_PAIR_SELECTION_MODE) { m_positivesMode = mode.getSelectedTag().getID(); } } /** * return the selection mode for positives * @return one of the selection modes */ public SelectedTag getPositivesMode() { return new SelectedTag(m_positivesMode, TAGS_PAIR_SELECTION_MODE); } /** Set the selection mode for negatives * @param mode selection mode */ public void setNegativesMode(SelectedTag mode) { if (mode.getTags() == TAGS_PAIR_SELECTION_MODE) { m_negativesMode = mode.getSelectedTag().getID(); } } /** * return the selection mode for negatives * @return one of the selection modes */ public SelectedTag getNegativesMode() { return new SelectedTag(m_negativesMode, TAGS_PAIR_SELECTION_MODE); } /** * Gets the current settings of WeightedDotP. * * @return an array of strings suitable for passing to setOptions() */ public String [] getOptions() { String [] options = new String [5]; int current = 0; options[current++] = "-P"; switch(m_positivesMode) { case PAIRS_RANDOM: options[current++] = "-r"; break; case PAIRS_HARDEST: options[current++] = "-h"; break; case PAIRS_EASIEST: options[current++] = "-e"; break; case PAIRS_INTERVAL: options[current++] = "-i"; break; } options[current++] = "-N"; switch(m_negativesMode) { case PAIRS_RANDOM: options[current++] = "-r"; break; case PAIRS_HARDEST: options[current++] = "-h"; break; case PAIRS_EASIEST: options[current++] = "-e"; break; case PAIRS_INTERVAL: options[current++] = "-i"; break; } while (current < options.length) { options[current++] = ""; } return options; } /** * Parses a given list of options. Valid options are:<p> * */ public void setOptions(String[] options) throws Exception { } /** * Returns an enumeration describing the available options. * * @return an enumeration of all the available options. */ public Enumeration listOptions() { Vector newVector = new Vector(0); return newVector.elements(); } /** * get an array of numIdxs random indeces out of n possible values. * if the number of requested indeces is larger then maxIdx, returns * maxIdx permuted values * @param maxIdx - the maximum index of the set * @param numIdxs number of indexes to return * @return an array of indexes */ public static int[] randomSubset(int numIdxs, int maxIdx) { Random r = new Random(maxIdx + numIdxs); int[] indexes = new int[maxIdx]; for (int i = 0; i < maxIdx; i++) { indexes[i] = i; } // permute the indeces randomly for (int i = 0; i < maxIdx; i++) { int idx = r.nextInt (maxIdx - i); int temp = indexes[i + idx]; indexes[i + idx] = indexes[i]; indexes[i] = temp; } int []returnIdxs = new int[Math.min(numIdxs,maxIdx)]; for (int i = 0; i < returnIdxs.length; i++) { returnIdxs[i] = indexes[i]; } return returnIdxs; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -