• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

numenta / htm.java / #1205

26 Jan 2015 12:57PM UTC coverage: 14.409% (+0.005%) from 14.404%
#1205

push

David Ray
Merge pull request #168 from cogmission/network_api_work

testing the RNG is not part of the scope

718 of 4983 relevant lines covered (14.41%)

0.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/org/numenta/nupic/model/DistalDendrite.java
1
/* ---------------------------------------------------------------------
2
 * Numenta Platform for Intelligent Computing (NuPIC)
3
 * Copyright (C) 2014, Numenta, Inc.  Unless you have an agreement
4
 * with Numenta, Inc., for a separate license for this software code, the
5
 * following terms and conditions apply:
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License version 3 as
9
 * published by the Free Software Foundation.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 * See the GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see http://www.gnu.org/licenses.
18
 *
19
 * http://numenta.org/licenses/
20
 * ---------------------------------------------------------------------
21
 */
22

23
package org.numenta.nupic.model;
24

25
import java.util.ArrayList;
26
import java.util.Collections;
27
import java.util.LinkedHashSet;
28
import java.util.List;
29
import java.util.Map;
30
import java.util.Random;
31
import java.util.Set;
32

33
import org.numenta.nupic.Connections;
34

35
/**
36
 * Represents a proximal or distal dendritic segment.
37
 * Segments are owned by {@link Cell}s and in turn own {@link Synapse}s
38
 * which are obversely connected to by a "source cell", which is the {@link Cell}
39
 * which will activate a given {@link Synapse} owned by this {@code Segment}.
40
 * 
41
 * @author Chetan Surpur
42
 * @author David Ray
43
 */
44
public class DistalDendrite extends Segment {
45
    private Cell cell;
46
    private int index;
47
    
48
    /**
49
     * Constructs a new {@code Segment} object with the specified
50
     * owner {@link Cell} and the specified index.
51
     * 
52
     * @param cell      the owner
53
     * @param index     this {@code Segment}'s index.
54
     */
55
    public DistalDendrite(Cell cell, int index) {
×
56
        this.cell = cell;
×
57
        this.index = index;
×
58
    }
×
59
    
60
    /**
61
     * Returns the owner {@link Cell} 
62
     * @return
63
     */
64
    public Cell getParentCell() {
65
        return cell;
×
66
    }
67
    
68
    /**
69
     * Creates and returns a newly created {@link Synapse} with the specified
70
     * source cell, permanence, and index.
71
     * 
72
     * @param c             the connections state of the temporal memory
73
     * @param sourceCell    the source cell which will activate the new {@code Synapse}
74
     * @param permanence    the new {@link Synapse}'s initial permanence.
75
     * @param index         the new {@link Synapse}'s index.
76
     * 
77
     * @return
78
     */
79
    public Synapse createSynapse(Connections c, Cell sourceCell, double permanence, int index) {
80
            Pool pool = new Pool(1);
×
81
            Synapse s = super.createSynapse(c, c.getSynapses(this), sourceCell, pool, index, sourceCell.getIndex());
×
82
            pool.setPermanence(c, s, permanence);
×
83
        return s;
×
84
    }
85
    
86
    /**
87
     * Returns all {@link Synapse}s
88
     * 
89
     * @param   c   the connections state of the temporal memory
90
     * @return
91
     */
92
    public List<Synapse> getAllSynapses(Connections c) {
93
        return c.getSynapses(this);
×
94
    }
95
    
96
    /**
97
     * Returns the synapses on a segment that are active due to lateral input
98
     * from active cells.
99
     * 
100
     * @param activeSynapsesForSegment
101
     * @param permanenceThreshold
102
     * @return
103
     */
104
    public Set<Synapse> getConnectedActiveSynapses(Map<DistalDendrite, Set<Synapse>> activeSynapsesForSegment, double permanenceThreshold) {
105
        Set<Synapse> connectedSynapses = new LinkedHashSet<Synapse>();
×
106
        
107
        if(!activeSynapsesForSegment.containsKey(this)) {
×
108
            return connectedSynapses;
×
109
        }
110
        
111
        for(Synapse s : activeSynapsesForSegment.get(this)) {
×
112
            if(s.getPermanence() >= permanenceThreshold) {
×
113
                connectedSynapses.add(s);
×
114
            }
115
        }
×
116
        return connectedSynapses;
×
117
    }
118
    
119
    /**
120
     * Called for learning {@code Segment}s so that they may
121
     * adjust the permanences of their synapses.
122
     * 
123
     * @param c                     the connections state of the temporal memory
124
     * @param activeSynapses        a set of active synapses owned by this {@code Segment} which
125
     *                              will have their permanences increased. All others will have their
126
     *                              permanences decreased.
127
     * @param permanenceIncrement   the increment by which permanences are increased.
128
     * @param permanenceDecrement   the increment by which permanences are decreased.
129
     */
130
    public void adaptSegment(Connections c, Set<Synapse> activeSynapses, double permanenceIncrement, double permanenceDecrement) {
131
        for(Synapse synapse : c.getSynapses(this)) {
×
132
            double permanence = synapse.getPermanence();
×
133
            if(activeSynapses.contains(synapse)) {
×
134
                permanence += permanenceIncrement;
×
135
            }else{
136
                permanence -= permanenceDecrement;
×
137
            }
138
            
139
            permanence = Math.max(0, Math.min(1.0, permanence));
×
140
            
141
            synapse.setPermanence(c, permanence);
×
142
        }
×
143
    }
×
144
    
145
    /**
146
     * Returns a {@link Set} of previous winner {@link Cell}s which aren't already attached to any
147
     * {@link Synapse}s owned by this {@code Segment}
148
     * 
149
     * @param   c               the connections state of the temporal memory
150
     * @param numPickCells      the number of possible cells this segment may designate
151
     * @param prevWinners       the set of previous winner cells
152
     * @param random            the random number generator
153
     * @return                  a {@link Set} of previous winner {@link Cell}s which aren't already attached to any
154
     *                          {@link Synapse}s owned by this {@code Segment}
155
     */
156
    public Set<Cell> pickCellsToLearnOn(Connections c, int numPickCells, Set<Cell> prevWinners, Random random) {
157
        //Create a list of cells that aren't already synapsed to this segment
158
        Set<Cell> candidates = new LinkedHashSet<Cell>(prevWinners);
×
159
        for(Synapse synapse : c.getSynapses(this)) {
×
160
            Cell sourceCell = synapse.getSourceCell();
×
161
            if(candidates.contains(sourceCell)) {
×
162
                candidates.remove(sourceCell);
×
163
            }
164
        }
×
165
        
166
        numPickCells = Math.min(numPickCells, candidates.size());
×
167
        List<Cell> cands = new ArrayList<Cell>(candidates);
×
168
        Collections.sort(cands);
×
169
        
170
        Set<Cell> cells = new LinkedHashSet<Cell>();
×
171
        for(int x = 0;x < numPickCells;x++) {
×
172
            int i = random.nextInt(cands.size());
×
173
            cells.add(cands.get(i));
×
174
            cands.remove(i);
×
175
        }
176
        
177
        return cells;
×
178
    }
179
    
180
    /**
181
     * {@inheritDoc}
182
     */
183
    public String toString() {
184
        return "" + index;
×
185
    }
186
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc