Java Code Examples for gnu.trove.list.TIntList#add()

The following examples show how to use gnu.trove.list.TIntList#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SortedNeighborhoodBlocking.java    From JedAIToolkit with Apache License 2.0 6 votes vote down vote up
protected int[] getMixedSortedEntities(String[] sortedTerms) {
    int datasetLimit = entityProfilesD1.size();
    final TIntList sortedEntityIds = new TIntArrayList();

    for (String blockingKey : sortedTerms) {
        final TIntList sortedIds = new TIntArrayList();
        final TIntList d1EntityIds = invertedIndexD1.get(blockingKey);
        if (d1EntityIds != null) {
            sortedIds.addAll(d1EntityIds);
        }

        final TIntList d2EntityIds = invertedIndexD2.get(blockingKey);
        if (d2EntityIds != null) {
            for (TIntIterator iterator = d2EntityIds.iterator(); iterator.hasNext();) {
                sortedIds.add(datasetLimit + iterator.next());
            }
        }

        sortedIds.shuffle(random);
        sortedEntityIds.addAll(sortedIds);
    }

    return sortedEntityIds.toArray();
}
 
Example 2
Source File: XMLUtils.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
private static TIntList indexPath(Element child, int size) {
    final Element parent = child.getParentElement();
    if(parent == null) {
        return new TIntArrayList(size);
    } else {
        final TIntList path = indexPath(parent, size + 1);
        final int index = ((BoundedElement) child).indexInParent();
        if(index < 0) {
            throw new IllegalStateException("Parent element " + parent + " does not contain its child element " + child);
        }
        path.add(index);
        return path;
    }
}
 
Example 3
Source File: BlockcentricEntityIndex.java    From JedAIToolkit with Apache License 2.0 5 votes vote down vote up
TIntList getCommonBlockIndices(int blockIndex, Comparison comparison) {
    int[] blocks1 = entityBlocks[comparison.getEntityId1()];
    int[] blocks2 = entityBlocks[comparison.getEntityId2() + datasetLimit];

    boolean firstCommonIndex = false;
    int noOfBlocks1 = blocks1.length;
    int noOfBlocks2 = blocks2.length;
    final TIntList indices = new TIntArrayList();

    int i = 0;
    int j = 0;

    while (i < noOfBlocks1) {
        while (j < noOfBlocks2) {
            if (blocks2[j] < blocks1[i]) {
                j++;
            } else if (blocks1[i] < blocks2[j]) {
                break;
            } else { //blocks1[i] == blocks2[j]
                if (!firstCommonIndex) {
                    firstCommonIndex = true;
                    if (blocks1[i] != blockIndex) {
                        return null;
                    }
                }
                indices.add(blocks1[i]);
                j++;
            }
        }
        i++;
    }

    return indices;
}
 
Example 4
Source File: BlockcentricEntityIndex.java    From JedAIToolkit with Apache License 2.0 5 votes vote down vote up
TIntList getTotalCommonIndices(Comparison comparison) {
    final TIntList indices = new TIntArrayList();

    int[] blocks1 = entityBlocks[comparison.getEntityId1()];
    int[] blocks2 = entityBlocks[comparison.getEntityId2() + datasetLimit];
    if (blocks1.length == 0 || blocks2.length == 0) {
        return indices;
    }

    int noOfBlocks1 = blocks1.length;
    int noOfBlocks2 = blocks2.length;

    int i = 0;
    int j = 0;

    while (i < noOfBlocks1) {
        while (j < noOfBlocks2) {
            if (blocks2[j] < blocks1[i]) {
                j++;
            } else if (blocks1[i] < blocks2[j]) {
                break;
            } else { //blocks1[i] == blocks2[j]
                i++;
                indices.add(blocks1[i]);
            }
        }
        i++;
    }

    return indices;
}
 
Example 5
Source File: GroundTruthIndex.java    From JedAIToolkit with Apache License 2.0 5 votes vote down vote up
public TIntList getCommonBlockIndices(int blockIndex, Comparison comparison) {
    final int[] blocks1 = entityBlocks[comparison.getEntityId1()];
    final int[] blocks2 = entityBlocks[comparison.getEntityId2() + datasetLimit];

    boolean firstCommonIndex = false;
    int noOfBlocks1 = blocks1.length;
    int noOfBlocks2 = blocks2.length;
    final TIntList indices = new TIntArrayList();
    for (int item : blocks1) {
        for (int value : blocks2) {
            if (value < item) {
                continue;
            }

            if (item < value) {
                break;
            }

            if (item == value) {
                if (!firstCommonIndex) {
                    firstCommonIndex = true;
                    if (item != blockIndex) {
                        return null;
                    }
                }
                indices.add(item);
            }
        }
    }

    return indices;
}
 
Example 6
Source File: GroundTruthIndex.java    From JedAIToolkit with Apache License 2.0 5 votes vote down vote up
public TIntList getTotalCommonIndices(Comparison comparison) {
    final TIntList indices = new TIntArrayList();

    final int[] blocks1 = entityBlocks[comparison.getEntityId1()];
    final int[] blocks2 = entityBlocks[comparison.getEntityId2() + datasetLimit];
    if (blocks1 == null || blocks2 == null) {
        return indices;
    }
    
    for (int item : blocks1) {
        for (int value : blocks2) {
            if (value < item) {
                continue;
            }

            if (item < value) {
                break;
            }

            if (item == value) {
                indices.add(item);
            }
        }
    }

    return indices;
}
 
Example 7
Source File: AbstractSparseBinaryMatrix.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Returns a sorted array of occupied indexes.
 * @return  a sorted array of occupied indexes.
 */
@Override
public int[] getSparseIndices() {
    TIntList indexes = new TIntArrayList();
    for (int i = 0; i <= getMaxIndex(); i ++) {
        if (get(i) > 0) {
            indexes.add(i);
        }
    }
    
    return indexes.toArray();
}
 
Example 8
Source File: AbstractSparseMatrix.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Recursively loops through the matrix dimensions to fill the results
 * array with flattened computed array indexes.
 * 
 * @param bounds
 * @param currentDimension
 * @param p
 * @param results
 */
private void visit(int[] bounds, int currentDimension, int[] p, TIntList results) {
    for (int i = 0; i < bounds[currentDimension]; i++) {
        p[currentDimension] = i;
        if (currentDimension == p.length - 1) {
            results.add(computeIndex(p));
        }
        else visit(bounds, currentDimension + 1, p, results);
    }
}
 
Example 9
Source File: SpatialPooler.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Performs inhibition. This method calculates the necessary values needed to
 * actually perform inhibition and then delegates the task of picking the
 * active columns to helper functions.
 * 
 * @param c         the {@link Connections} matrix
 * @param overlaps  an array containing the overlap score for each  column.
 *                  The overlap score for a column is defined as the number
 *                  of synapses in a "connected state" (connected synapses)
 *                  that are connected to input bits which are turned on.
 * @param density   The fraction of columns to survive inhibition. This
 *                  value is only an intended target. Since the surviving
 *                  columns are picked in a local fashion, the exact fraction
 *                  of surviving columns is likely to vary.
 * @return  indices of the winning columns
 */
public int[] inhibitColumnsLocal(Connections c, double[] overlaps, double density) {
    double addToWinners = ArrayUtils.max(overlaps) / 1000.0d;
    if(addToWinners == 0) {
        addToWinners = 0.001;
    }
    double[] tieBrokenOverlaps = Arrays.copyOf(overlaps, overlaps.length);
    
    TIntList winners = new TIntArrayList();
    double stimulusThreshold = c.getStimulusThreshold();
    int inhibitionRadius = c.getInhibitionRadius();
    for(int i = 0;i < overlaps.length;i++) {
        int column = i;
        if(overlaps[column] >= stimulusThreshold) {
           int[] neighborhood = getColumnNeighborhood(c, column, inhibitionRadius);
           double[] neighborhoodOverlaps = ArrayUtils.sub(tieBrokenOverlaps, neighborhood);
           
           long numBigger = Arrays.stream(neighborhoodOverlaps)
               .parallel()
               .filter(d -> d > overlaps[column])
               .count();
           
           int numActive = (int)(0.5 + density * neighborhood.length);
           if(numBigger < numActive) {
               winners.add(column);
               tieBrokenOverlaps[column] += addToWinners;
           }
        }
    }

    return winners.toArray();
}
 
Example 10
Source File: Main.java    From JedAIToolkit with Apache License 2.0 4 votes vote down vote up
private static TIntList readMultipleInt(boolean optional, String message, String[] array) {
    System.out.println("\n\n" + message);
    for (int i = 0; i < array.length; i++) {
        System.out.println((i + 1) + " - " + array[i]);
    }
    if (optional) {
        System.out.println("This is an optional step. You can select none or all options. Choose -1 to terminate this step!");
    } else {
        System.out.println("Please select one or more of the available options. Choose -1 to terminate this step!");
    }

    final TIntList selectedIds = new TIntArrayList();
    while (true) {
        int userInt;
        Scanner keyboard = new Scanner(System.in);

        try {
            userInt = keyboard.nextInt();
        } catch (Exception ex) {
            System.out.println("Invalid input. Please choose between 1 and " + array.length);
            continue;
        }

        if (userInt == -1) {
            break;
        }

        if (userInt < 1 || userInt > array.length) {
            System.out.println("Invalid input. Please choose between 1 and " + array.length);
            continue;
        }

        if (selectedIds.contains(userInt)) {
            System.out.println("You have already selected this option!");
            continue;
        }

        selectedIds.add(userInt);
        System.out.println(array[userInt - 1] + " has been selected!");
    }

    return selectedIds;
}
 
Example 11
Source File: Category.java    From JedAIToolkit with Apache License 2.0 4 votes vote down vote up
public Category(int len, float threshold, int categoryN) {
    Category.THRESHOLD = threshold;
    Category.N = categoryN;
    s_len = len;
    e_len = (int) ((float) (s_len / THRESHOLD));
    K = (int) (2 * (1 - THRESHOLD) / (1 + THRESHOLD) * (float) e_len);
    N1 = K + 1;
    N2 = 2;

    K2 = (K + 1) / N1 - 1;

    // important fix
    if ((K + 1) % N1 != 0) {
        K2++;
    }

    if (N1 > K + 1 || N1 * N2 <= K + 1) {
        return;
    }

    subs = new ArrayList<>();
    int n = N2;
    int k = N2 - K2;
    TIntList sub = new TIntArrayList();
    int s;
    for (s = 0; s < k; s++) {
        sub.add(s);
    }
    subs.add(sub);

    while (sub.get(0) < n - k) {
        for (s = 0; s < k; s++) {
            if (sub.get(k - s - 1) < n - s - 1) {
                break;
            }
        }
        s = k - s - 1;
        sub.set(s, sub.get(s) + 1);
        s++;
        for (; s < k; s++) {
            sub.set(s, sub.get(s - 1) + 1);
        }
        subs.add(sub);
    }

    sig_len = N1 * subs.size();
    //System.out.println("ss "+sig_len);
    sig_map = new HashMap[sig_len];
    for (int is = 0; is < sig_len; is++) {
        sig_map[is] = new HashMap<>();
    }

    int[] t = new int[N1 * N2];
    range_start = new int[N1][N2];
    for (int kk = 0; kk < N1; kk++) {
        range_start[kk][0] = t[kk * N2];
    }

    t = new int[N1 * N2];
    range_end = new int[N1][N2];
    for (int kk = 0; kk < N1; kk++) {
        range_end[kk][0] = t[kk * N2];
    }

    //System.out.println("n1 n2 "+N+" "+N1+" "+N2);
    for (int i = 0; i < N1; i++) {
        for (int j = 0; j < N2; j++) {
            range_start[i][j] = N * (N2 * i + j) / N1 / N2;
            range_end[i][j] = N * (N2 * i + j + 1) / N1 / N2;
            /*System.out.println("rs "+range_start[i][j]);
            System.out.println("re "+range_end[i][j]);*/
        }
    }
}
 
Example 12
Source File: ArrayUtils.java    From htm.java with GNU Affero General Public License v3.0 3 votes vote down vote up
/**
 * Returns an array which starts from lowerBounds (inclusive) and
 * ends at the upperBounds (exclusive).
 *
 * @param lowerBounds
 * @param upperBounds
 * @return
 */
public static int[] range(int lowerBounds, int upperBounds) {
    TIntList ints = new TIntArrayList();
    for (int i = lowerBounds; i < upperBounds; i++) {
        ints.add(i);
    }
    return ints.toArray();
}
 
Example 13
Source File: ArrayUtils.java    From htm.java with GNU Affero General Public License v3.0 3 votes vote down vote up
/**
 * Returns an array which starts from lowerBounds (inclusive) and
 * ends at the upperBounds (exclusive).
 *
 * @param lowerBounds the starting value
 * @param upperBounds the maximum value (exclusive)
 * @param interval    the amount by which to increment the values
 * @return
 */
public static int[] xrange(int lowerBounds, int upperBounds, int interval) {
    TIntList ints = new TIntArrayList();
    for (int i = lowerBounds; i < upperBounds; i += interval) {
        ints.add(i);
    }
    return ints.toArray();
}