Java Code Examples for org.numenta.nupic.Parameters#apply()

The following examples show how to use org.numenta.nupic.Parameters#apply() . 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: AlgorithmDeterminacyTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testTemporalMemoryExplicit() {
    final int[] input1 = new int[] { 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0 };
    final int[] input2 = new int[] { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0 };
    final int[] input3 = new int[] { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
    final int[] input4 = new int[] { 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0 };
    final int[] input5 = new int[] { 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0 };
    final int[] input6 = new int[] { 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 };
    final int[] input7 = new int[] { 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 };
    final int[][] inputs = { input1, input2, input3, input4, input5, input6, input7 };
    
    Parameters p = getParameters();
    Connections con = new Connections();
    p.apply(con);
    TemporalMemory tm = new TemporalMemory();
    TemporalMemory.init(con);
    
    ComputeCycle cc = null;
    for(int x = 0;x < 602;x++) {
        for(int[] i : inputs) {
            cc = tm.compute(con, ArrayUtils.where(i, ArrayUtils.WHERE_1), true);
        }
    }
    
    TEST_AGGREGATION[TM_EXPL] = SDR.asCellIndices(cc.activeCells);
}
 
Example 2
Source File: TemporalMemoryTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testAdaptSegmentToMax() {
    TemporalMemory tm = new TemporalMemory();
    Connections cn = new Connections();
    Parameters p = Parameters.getAllDefaultParameters();
    p.apply(cn);
    TemporalMemory.init(cn);
    
    DistalDendrite dd = cn.createSegment(cn.getCell(0));
    Synapse s1 = cn.createSynapse(dd, cn.getCell(23), 0.9);
    
    tm.adaptSegment(cn, dd, cn.getCellSet(23), cn.getPermanenceIncrement(), cn.getPermanenceDecrement());
    assertEquals(1.0, s1.getPermanence(), 0.1);
    
    // Now permanence should be at max
    tm.adaptSegment(cn, dd, cn.getCellSet(23), cn.getPermanenceIncrement(), cn.getPermanenceDecrement());
    assertEquals(1.0, s1.getPermanence(), 0.1);
}
 
Example 3
Source File: TemporalMemoryTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testLeastUsedCell() {
    TemporalMemory tm = new TemporalMemory();
    Connections cn = new Connections();
    Parameters p = getDefaultParameters(null, KEY.COLUMN_DIMENSIONS, new int[] { 2 });
    p = getDefaultParameters(p, KEY.CELLS_PER_COLUMN, 2);
    p.apply(cn);
    TemporalMemory.init(cn);
    
    DistalDendrite dd = cn.createSegment(cn.getCell(0));
    cn.createSynapse(dd, cn.getCell(3), 0.3);
    
    for(int i = 0;i < 100;i++) {
        assertEquals(1, tm.leastUsedCell(cn, cn.getColumn(0).getCells(), cn.getRandom()).getIndex());
    }
}
 
Example 4
Source File: MonitoredTemporalMemoryTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
public void prepConstructs(TemporalMemory tm, Connections cn) {
    Parameters p = getDefaultParameters();
    p.apply(cn);
    TemporalMemory.init(cn);
    
    int[] previousActiveColumns = { 0 };
    int[] activeColumns = { 1 };
    Cell cell4 = cn.getCell(4);
    Set<Cell> expectedActiveCells = Stream.of(cell4).collect(Collectors.toSet());
    
    DistalDendrite activeSegment = cn.createSegment(cell4);
    cn.createSynapse(activeSegment, cn.getCell(0), 0.5);
    cn.createSynapse(activeSegment, cn.getCell(1), 0.5);
    cn.createSynapse(activeSegment, cn.getCell(2), 0.5);
    cn.createSynapse(activeSegment, cn.getCell(3), 0.5);
    
    ComputeCycle cc = tm.compute(cn, previousActiveColumns, true);
    assertTrue(cc.predictiveCells().equals(expectedActiveCells));
    ComputeCycle cc2 = tm.compute(cn, activeColumns, true);
    assertTrue(cc2.activeCells().equals(expectedActiveCells));
}
 
Example 5
Source File: TemporalMemoryTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testDestroySegmentsWithTooFewSynapsesToBeMatching() {
    TemporalMemory tm = new TemporalMemory();
    Connections cn = new Connections();
    Parameters p = getDefaultParameters(null, KEY.INITIAL_PERMANENCE, .2);
    p = getDefaultParameters(p, KEY.MAX_NEW_SYNAPSE_COUNT, 4);
    p = getDefaultParameters(p, KEY.PREDICTED_SEGMENT_DECREMENT, 0.02);
    p.apply(cn);
    TemporalMemory.init(cn);
    
    int[] prevActiveColumns = { 0 };
    Cell[] prevActiveCells = { cn.getCell(0), cn.getCell(1), cn.getCell(2), cn.getCell(3) };
    int[] activeColumns = { 2 };
    Cell expectedActiveCell = cn.getCell(5);
    
    DistalDendrite matchingSegment = cn.createSegment(cn.getCell(5));
    cn.createSynapse(matchingSegment, prevActiveCells[0], .015);
    cn.createSynapse(matchingSegment, prevActiveCells[1], .015);
    cn.createSynapse(matchingSegment, prevActiveCells[2], .015);
    cn.createSynapse(matchingSegment, prevActiveCells[3], .015);
    
    tm.compute(cn, prevActiveColumns, true);
    tm.compute(cn, activeColumns, true);
    
    assertEquals(0, cn.numSegments(expectedActiveCell));
}
 
Example 6
Source File: TemporalMemoryTest.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testBurstUnpredictedColumns() {
    TemporalMemory tm = new TemporalMemory();
    Connections cn = new Connections();
    Parameters p = getDefaultParameters();
    p.apply(cn);
    TemporalMemory.init(cn);
    
    int[] activeColumns = { 0 };
    Set<Cell> burstingCells = cn.getCellSet(new int[] { 0, 1, 2, 3 });
    
    ComputeCycle cc = tm.compute(cn, activeColumns, true);
    
    assertTrue(cc.activeCells().equals(burstingCells));
}
 
Example 7
Source File: AbstractAlgorithmBenchmark.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
@Setup
public void init() {
    SDR = new int[2048];

    //Layer components
    ScalarEncoder.Builder dayBuilder = ScalarEncoder.builder()
        .n(8)
        .w(3)
        .radius(1.0)
        .minVal(1.0)
        .maxVal(8)
        .periodic(true)
        .forced(true)
        .resolution(1);
    encoder = dayBuilder.build();
    pooler = new SpatialPooler();

    memory = new Connections();
    Parameters params = getParameters();
    params.apply(memory);

    pooler = new SpatialPooler();
    pooler.init(memory);

    temporalMemory = new TemporalMemory();
    TemporalMemory.init(memory);
}
 
Example 8
Source File: TemporalMemoryTest.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testNumberOfCells() {
    Connections cn = new Connections();
    Parameters p = Parameters.getAllDefaultParameters();
    p.set(KEY.COLUMN_DIMENSIONS, new int[] { 64, 64 });
    p.set(KEY.CELLS_PER_COLUMN, 32);
    p.apply(cn);
    TemporalMemory.init(cn);
    
    assertEquals(64 * 64 * 32, cn.getCells().length);
}
 
Example 9
Source File: TemporalMemoryTest.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testZeroActiveColumns() {
    TemporalMemory tm = new TemporalMemory();
    Connections cn = new Connections();
    Parameters p = getDefaultParameters();
    p.apply(cn);
    TemporalMemory.init(cn);
    
    int[] previousActiveColumns = { 0 };
    Cell cell4 = cn.getCell(4);
    
    DistalDendrite activeSegment = cn.createSegment(cell4);
    cn.createSynapse(activeSegment, cn.getCell(0), 0.5);
    cn.createSynapse(activeSegment, cn.getCell(1), 0.5);
    cn.createSynapse(activeSegment, cn.getCell(2), 0.5);
    cn.createSynapse(activeSegment, cn.getCell(3), 0.5);
    
    ComputeCycle cc = tm.compute(cn, previousActiveColumns, true);
    assertFalse(cc.activeCells().size() == 0);
    assertFalse(cc.winnerCells().size() == 0);
    assertFalse(cc.predictiveCells().size() == 0);
    
    int[] zeroColumns = new int[0];
    ComputeCycle cc2 = tm.compute(cn, zeroColumns, true);
    assertTrue(cc2.activeCells().size() == 0);
    assertTrue(cc2.winnerCells().size() == 0);
    assertTrue(cc2.predictiveCells().size() == 0);
}
 
Example 10
Source File: ConnectionsTest.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Destroy some synapses then verify that the maxSynapsesPerSegment is
 * still correctly applied.
 */
@Test
public void testDestroySynapsesThenReachLimit() {
    Parameters p = Parameters.getTemporalDefaultParameters();
    p.set(KEY.COLUMN_DIMENSIONS, new int[] { 32 });
    p.set(KEY.CELLS_PER_COLUMN, 32);
    p.set(KEY.MAX_SEGMENTS_PER_CELL, 2);
    p.set(KEY.MAX_SYNAPSES_PER_SEGMENT, 2);
    
    Connections connections = new Connections();
    p.apply(connections);
    TemporalMemory.init(connections);
    
    DistalDendrite segment = connections.createSegment(connections.getCell(10));
    
    Synapse synapse1 = connections.createSynapse(segment, connections.getCell(201), .85);
    Synapse synapse2 = connections.createSynapse(segment, connections.getCell(202), .85);
    
    assertEquals(2, connections.numSynapses());
    connections.destroySynapse(synapse1);
    connections.destroySynapse(synapse2);
    assertEquals(0, connections.numSynapses());
    
    connections.createSynapse(segment, connections.getCell(201), .85);
    assertEquals(1, connections.numSynapses());
    connections.createSynapse(segment, connections.getCell(202), .90);
    assertEquals(2, connections.numSynapses());
    connections.createSynapse(segment, connections.getCell(203), .8);
    assertEquals(2, connections.numSynapses());
}
 
Example 11
Source File: ConnectionsTest.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Destroy some segments then verify that the maxSegmentsPerCell is still
 * correctly applied.
 */
@Test
public void testDestroySegmentsThenReachLimit() {
    Parameters p = Parameters.getTemporalDefaultParameters();
    p.set(KEY.COLUMN_DIMENSIONS, new int[] { 32 });
    p.set(KEY.CELLS_PER_COLUMN, 32);
    p.set(KEY.MAX_SEGMENTS_PER_CELL, 2);
    p.set(KEY.MAX_SYNAPSES_PER_SEGMENT, 2);
    
    Connections connections = new Connections();
    p.apply(connections);
    TemporalMemory.init(connections);
    
    DistalDendrite segment1 = connections.createSegment(connections.getCell(11));
    DistalDendrite segment2 = connections.createSegment(connections.getCell(11));
    
    assertEquals(2, connections.numSegments());
    connections.destroySegment(segment1);
    connections.destroySegment(segment2);
    assertEquals(0, connections.numSegments());
    
    connections.createSegment(connections.getCell(11));
    assertEquals(1, connections.numSegments());
    connections.createSegment(connections.getCell(11));
    assertEquals(2, connections.numSegments());
    connections.createSegment(connections.getCell(11));
    assertEquals(2, connections.numSegments(connections.getCell(11)));
    assertEquals(2, connections.numSegments());
}
 
Example 12
Source File: ConnectionsTest.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Destroy a segment that has a destroyed synapse and a non-destroyed
 * synapse. Make sure nothing gets double-destroyed.
 */
@Test
public void testDestroySegmentWithDestroyedSynapses() {
    Parameters p = Parameters.getTemporalDefaultParameters();
    p.set(KEY.COLUMN_DIMENSIONS, new int[] { 32 });
    p.set(KEY.CELLS_PER_COLUMN, 32);
    
    Connections connections = new Connections();
    p.apply(connections);
    TemporalMemory.init(connections);
    
    DistalDendrite segment1 = connections.createSegment(connections.getCell(11));
    DistalDendrite segment2 = connections.createSegment(connections.getCell(12));
    
    connections.createSynapse(segment1, connections.getCell(101), .85);
    Synapse synapse2a = connections.createSynapse(segment2, connections.getCell(201), .85);
    connections.createSynapse(segment2, connections.getCell(202), .85);
    
    assertEquals(3, connections.numSynapses());
    
    connections.destroySynapse(synapse2a);
    
    assertEquals(2, connections.numSegments());
    assertEquals(2, connections.numSynapses());
    
    connections.destroySegment(segment2);
    
    assertEquals(1, connections.numSegments());
    assertEquals(1, connections.numSynapses());
}
 
Example 13
Source File: ConnectionsTest.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Creates segments and synapses, then destroys segments and synapses on
 * either side of them and verifies that existing Segment and Synapse
 * instances still point to the same segment / synapse as before.
 */
@Test
public void testPathsNotInvalidatedByOtherDestroys() {
    Parameters p = Parameters.getTemporalDefaultParameters();
    p.set(KEY.COLUMN_DIMENSIONS, new int[] { 32 });
    p.set(KEY.CELLS_PER_COLUMN, 32);
    
    Connections connections = new Connections();
    p.apply(connections);
    TemporalMemory.init(connections);
    
    DistalDendrite segment1 = connections.createSegment(connections.getCell(11));
    connections.createSegment(connections.getCell(12));
    DistalDendrite segment3 = connections.createSegment(connections.getCell(13));
    connections.createSegment(connections.getCell(14));
    DistalDendrite segment5 = connections.createSegment(connections.getCell(15));
    
    Cell cell203 = connections.getCell(203);
    Synapse synapse1 = connections.createSynapse(segment3, connections.getCell(201), .85);
    Synapse synapse2 = connections.createSynapse(segment3, connections.getCell(202), .85);
    Synapse synapse3 = connections.createSynapse(segment3, cell203, .85);
    Synapse synapse4 = connections.createSynapse(segment3, connections.getCell(204), .85);
    Synapse synapse5 = connections.createSynapse(segment3, connections.getCell(205), .85);
    
    assertEquals(cell203, synapse3.getPresynapticCell());
    connections.destroySynapse(synapse1);
    assertEquals(cell203, synapse3.getPresynapticCell());
    connections.destroySynapse(synapse5);
    assertEquals(cell203, synapse3.getPresynapticCell());
    
    connections.destroySegment(segment1);
    List<Synapse> l234 = Arrays.stream(new Synapse[] { synapse2, synapse3, synapse4 }).collect(Collectors.toList());
    assertEquals(connections.getSynapses(segment3), l234);
    connections.destroySegment(segment5);
    assertEquals(connections.getSynapses(segment3), l234);
    assertEquals(cell203, synapse3.getPresynapticCell());
}
 
Example 14
Source File: TemporalMemoryTest.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testMatchingSegmentAddSynapsesToSubsetOfWinnerCells() {
    TemporalMemory tm = new TemporalMemory();
    Connections cn = new Connections();
    Parameters p = getDefaultParameters(null, KEY.CELLS_PER_COLUMN, 1);
    p = getDefaultParameters(p, KEY.MIN_THRESHOLD, 1);
    p.apply(cn);
    TemporalMemory.init(cn);
    
    int[] previousActiveColumns = { 0, 1, 2, 3 };
    Set<Cell> prevWinnerCells = cn.getCellSet(new int[] { 0, 1, 2, 3 });
    int[] activeColumns = { 4 };
    
    DistalDendrite matchingSegment = cn.createSegment(cn.getCell(4));
    cn.createSynapse(matchingSegment, cn.getCell(0), 0.5);
    
    ComputeCycle cc = tm.compute(cn, previousActiveColumns, true);
    assertTrue(cc.winnerCells().equals(prevWinnerCells));
    cc = tm.compute(cn, activeColumns, true);
    
    List<Synapse> synapses = cn.getSynapses(matchingSegment);
    assertEquals(3, synapses.size());
    
    Collections.sort(synapses);
    for(Synapse synapse : synapses) {
        if(synapse.getPresynapticCell().getIndex() == 0) continue;
        
        assertEquals(0.21, synapse.getPermanence(), 0.01);
        assertTrue(synapse.getPresynapticCell().getIndex() == 1 ||
                   synapse.getPresynapticCell().getIndex() == 2 ||
                   synapse.getPresynapticCell().getIndex() == 3);
    }
}
 
Example 15
Source File: ConnectionsTest.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testDestroySegment() {
    Parameters retVal = Parameters.getTemporalDefaultParameters();
    retVal.set(KEY.COLUMN_DIMENSIONS, new int[] { 32 });
    retVal.set(KEY.CELLS_PER_COLUMN, 4);
    
    Connections connections = new Connections();

    retVal.apply(connections);
    TemporalMemory.init(connections);
    
    connections.createSegment(connections.getCell(10));
    DistalDendrite segment2 = connections.createSegment(connections.getCell(20));
    connections.createSegment(connections.getCell(30));
    connections.createSegment(connections.getCell(40));
    
    connections.createSynapse(segment2, connections.getCell(80), 0.85);
    connections.createSynapse(segment2, connections.getCell(81), 0.85);
    connections.createSynapse(segment2, connections.getCell(82), 0.15);
    
    assertEquals(4, connections.numSegments());
    assertEquals(3, connections.numSynapses());
    
    connections.destroySegment(segment2);
    
    assertEquals(3, connections.numSegments());
    assertEquals(0, connections.numSynapses());
    
    Activity activity = connections.computeActivity(
        IntStream.rangeClosed(80, 82)
            .mapToObj(i -> connections.getCell(i)).collect(Collectors.toList()),
                0.5D);
    
    assertEquals(0, activity.numActiveConnected[segment2.getIndex()]);
    assertEquals(0, activity.numActivePotential[segment2.getIndex()]);
}
 
Example 16
Source File: TemporalMemoryTest.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testAdaptSegmentToMin() {
    TemporalMemory tm = new TemporalMemory();
    Connections cn = new Connections();
    Parameters p = Parameters.getAllDefaultParameters();
    p.apply(cn);
    TemporalMemory.init(cn);
    
    DistalDendrite dd = cn.createSegment(cn.getCell(0));
    Synapse s1 = cn.createSynapse(dd, cn.getCell(23), 0.1);
    cn.createSynapse(dd, cn.getCell(1), 0.3);
    
    tm.adaptSegment(cn, dd, cn.getCellSet(), cn.getPermanenceIncrement(), cn.getPermanenceDecrement());
    assertFalse(cn.getSynapses(dd).contains(s1));
}
 
Example 17
Source File: TemporalMemoryTest.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testNoChangeToNonSelectedMatchingSegmentsInBurstingColumn() {
    TemporalMemory tm = new TemporalMemory();
    Connections cn = new Connections();
    Parameters p = getDefaultParameters(null, KEY.PERMANENCE_DECREMENT, 0.08);
    p.apply(cn);
    TemporalMemory.init(cn);
    
    int[] previousActiveColumns = { 0 };
    int[] activeColumns = { 1 };
    Cell[] previousActiveCells = {cn.getCell(0), cn.getCell(1), cn.getCell(2), cn.getCell(3) };
    Cell[] burstingCells = {cn.getCell(4), cn.getCell(5) };
    
    DistalDendrite selectedMatchingSegment = cn.createSegment(burstingCells[0]);
    cn.createSynapse(selectedMatchingSegment, previousActiveCells[0], 0.3);
    cn.createSynapse(selectedMatchingSegment, previousActiveCells[1], 0.3);
    cn.createSynapse(selectedMatchingSegment, previousActiveCells[2], 0.3);
    cn.createSynapse(selectedMatchingSegment, cn.getCell(81), 0.3);
    
    DistalDendrite otherMatchingSegment = cn.createSegment(burstingCells[1]);
    Synapse as1 = cn.createSynapse(otherMatchingSegment, previousActiveCells[0], 0.3);
    Synapse as2 = cn.createSynapse(otherMatchingSegment, previousActiveCells[1], 0.3);
    Synapse is1 = cn.createSynapse(otherMatchingSegment, cn.getCell(81), 0.3);
    
    tm.compute(cn, previousActiveColumns, true);
    tm.compute(cn, activeColumns, true);
    
    assertEquals(0.3, as1.getPermanence(), 0.01);
    assertEquals(0.3, as2.getPermanence(), 0.01);
    assertEquals(0.3, is1.getPermanence(), 0.01);
}
 
Example 18
Source File: SpatialPoolerCompatibilityTest.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Creates a {@link SpatialPooler} with predetermined parameters.
 * @return
 */
private Tuple createSP() {
    Parameters parameters = Parameters.getAllDefaultParameters();
    parameters.set(KEY.INPUT_DIMENSIONS, new int[] { 4, 4 });
    parameters.set(KEY.COLUMN_DIMENSIONS, new int[] { 5, 3 });
    parameters.set(KEY.CELLS_PER_COLUMN, 1);
    parameters.set(KEY.RANDOM, new UniversalRandom(42));

    //SpatialPooler specific
    parameters.set(KEY.POTENTIAL_RADIUS, 20);//3
    parameters.set(KEY.POTENTIAL_PCT, 0.5);//0.5
    parameters.set(KEY.GLOBAL_INHIBITION, true);
    parameters.set(KEY.LOCAL_AREA_DENSITY, 0.0);
    parameters.set(KEY.NUM_ACTIVE_COLUMNS_PER_INH_AREA, 5.0);
    parameters.set(KEY.STIMULUS_THRESHOLD, 0.0);
    parameters.set(KEY.SYN_PERM_INACTIVE_DEC, 0.01);
    parameters.set(KEY.SYN_PERM_ACTIVE_INC, 0.1);
    parameters.set(KEY.SYN_PERM_TRIM_THRESHOLD, 0.05);
    parameters.set(KEY.SYN_PERM_CONNECTED, 0.1);
    parameters.set(KEY.MIN_PCT_OVERLAP_DUTY_CYCLES, 0.001);
    parameters.set(KEY.MIN_PCT_ACTIVE_DUTY_CYCLES, 0.001);
    parameters.set(KEY.DUTY_CYCLE_PERIOD, 30);
    parameters.set(KEY.MAX_BOOST, 10.0);
    
    Connections conn = new Connections();
    parameters.apply(conn);
    
    SpatialPooler sp = new SpatialPooler();
    sp.init(conn);
    
    return new Tuple(conn, sp);
}
 
Example 19
Source File: ConnectionsTest.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testSegmentPositionForSortKey() {
    Parameters retVal = Parameters.getTemporalDefaultParameters();
    retVal.set(KEY.COLUMN_DIMENSIONS, new int[] { 32 });
    retVal.set(KEY.CELLS_PER_COLUMN, 4);

    Connections connections = new Connections();

    retVal.apply(connections);
    TemporalMemory.init(connections);
    
    Cell cell10 = connections.getCell(10);
    DistalDendrite segment0 = connections.createSegment(cell10);
    
    Cell cell9 = connections.getCell(9);
    DistalDendrite segment1 = connections.createSegment(cell9);
    
    Cell cell11 = connections.getCell(11);
    DistalDendrite segment2 = connections.createSegment(cell11);
    DistalDendrite segment3 = connections.createSegment(cell11);
    DistalDendrite segment4 = connections.createSegment(cell11);
   
    List<DistalDendrite> expected = Arrays.asList(segment1, segment0, segment2, segment3, segment4);
    List<DistalDendrite> segments = Arrays.asList(segment3, segment2, segment0, segment4, segment1);
    assertFalse(DeepEquals.deepEquals(expected, segments));
    
    Collections.sort(segments, connections.segmentPositionSortKey);
    assertTrue(DeepEquals.deepEquals(expected, segments));
}
 
Example 20
Source File: ConnectionsTest.java    From htm.java with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void testDoSpatialPoolerPostInit() {
    Parameters p = getParameters();
    p.set(KEY.SYN_PERM_CONNECTED, 0.2);
    p.set(KEY.SYN_PERM_ACTIVE_INC, 0.003);
    
    ///////////////////// First without Post Init /////////////////////
    SpatialPooler sp = new SpatialPooler();
    @SuppressWarnings("serial")
    Connections conn = new Connections() {
        @Override
        public void doSpatialPoolerPostInit() {
            // Override to do nothing
        }
    };
    p.apply(conn);
    sp.init(conn);
    
    double synPermConnected = conn.getSynPermConnected();
    double synPermActiveInc = conn.getSynPermActiveInc();
    double synPermBelowStimulusInc = conn.getSynPermBelowStimulusInc();
    double synPermTrimThreshold = conn.getSynPermTrimThreshold();
   
    // Assert that static values (synPermConnected & synPermActiveInc) don't change,
    // and that synPermBelowStimulusInc & synPermTrimThreshold are the defaults
    assertEquals(0.2, synPermConnected, 0.001);
    assertEquals(0.003, synPermActiveInc, 0.001);
    assertEquals(0.01, synPermBelowStimulusInc, 0.001);
    assertEquals(0.025, synPermTrimThreshold, 0.0001);
    
    
    ///////////////////// Now with Post Init /////////////////////
    sp = new SpatialPooler();
    conn = new Connections();
    p.apply(conn);
    sp.init(conn);
    
    synPermConnected = conn.getSynPermConnected();
    synPermActiveInc = conn.getSynPermActiveInc();
    synPermBelowStimulusInc = conn.getSynPermBelowStimulusInc();
    synPermTrimThreshold = conn.getSynPermTrimThreshold();
    
    // Assert that static values (synPermConnected & synPermActiveInc) don't change,
    // and that synPermBelowStimulusInc & synPermTrimThreshold change due to postInit()
    assertEquals(0.2, synPermConnected, 0.001);
    assertEquals(0.003, synPermActiveInc, 0.001);
    assertEquals(0.02, synPermBelowStimulusInc, 0.001); // affected by postInit()
    assertEquals(0.0015, synPermTrimThreshold, 0.0001);   // affected by postInit()
}