• 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/Pool.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 gnu.trove.list.TIntList;
26
import gnu.trove.list.array.TIntArrayList;
27
import gnu.trove.map.TIntObjectMap;
28
import gnu.trove.map.TObjectDoubleMap;
29
import gnu.trove.map.hash.TIntObjectHashMap;
30
import gnu.trove.map.hash.TObjectDoubleHashMap;
31

32
import java.util.Arrays;
33
import java.util.LinkedHashSet;
34
import java.util.Set;
35

36
import org.numenta.nupic.Connections;
37

38
/**
39
 * Convenience container for "bound" {@link Synapse} values
40
 * which can be dereferenced from both a Synapse and the 
41
 * {@link Connections} object. All Synapses will have a reference
42
 * to a {@code Pool} to retrieve relevant values. In addition, that
43
 * same pool can be referenced from the Connections object externally
44
 * which will update the Synapse's internal reference.
45
 * 
46
 * @author David Ray
47
 * @see Synapse
48
 * @see Connections
49
 */
50
public class Pool {
51
        int size;
52
        
53
        TObjectDoubleMap<Synapse> synapsePermanences = new TObjectDoubleHashMap<Synapse>();
×
54
        TIntArrayList synapseConnections = new TIntArrayList();
×
55
        Set<Synapse> synapseOrdering = new LinkedHashSet<Synapse>();
×
56
        
57
        TIntObjectMap<SynapsePair> connectionPerms = new TIntObjectHashMap<SynapsePair>();
×
58
        
59
        public Pool(int size) {
×
60
                this.size = size;
×
61
        }
×
62
        
63
        /**
64
         * Returns the permanence value for the {@link Synapse} specified.
65
         * 
66
         * @param s        the Synapse
67
         * @return        the permanence
68
         */
69
        public double getPermanence(Synapse s) {
70
                return synapsePermanences.get(s);
×
71
        }
72
        
73
        /**
74
         * Sets the specified  permanence value for the specified {@link Synapse}
75
         * @param s
76
         * @param permanence
77
         */
78
        public void setPermanence(Connections c, Synapse s, double permanence) {
79
                SynapsePair synPerm = null;
×
80
                if((synPerm = connectionPerms.get(s.getInputIndex())) == null) {
×
81
                        connectionPerms.put(s.getInputIndex(), synPerm = new SynapsePair(s, permanence));
×
82
                        synapseOrdering.add(s);
×
83
                }
84
                if(permanence > c.getSynPermConnected()) {
×
85
                        synapseConnections.add(s.getInputIndex());
×
86
                }
87
                synapsePermanences.put(s, permanence);
×
88
                synPerm.setPermanence(permanence);
×
89
        }
×
90
        
91
        /**
92
         * Resets the current connections in preparation for new permanence
93
         * adjustments.
94
         */
95
        public void resetConnections() {
96
                synapseConnections.clear();
×
97
        }
×
98
        
99
        /**
100
         * Returns the {@link Synapse} connected to the specified input bit
101
         * index.
102
         * 
103
         * @param inputIndex        the input vector connection's index.
104
         * @return
105
         */
106
        public Synapse getSynapseWithInput(int inputIndex) {
107
                return connectionPerms.get(inputIndex).getSynapse();
×
108
        }
109
        
110
        /**
111
         * Returns an array of permanence values
112
         * @return
113
         */
114
        public double[] getSparsePermanences() {
115
                int i = 0;
×
116
                double[] retVal = new double[size];
×
117
                for(Synapse s : synapseOrdering) {
×
118
                        retVal[i++] = connectionPerms.get(s.getInputIndex()).getPermanence();
×
119
                }
×
120
                return retVal;
×
121
        }
122
        
123
        /**
124
         * Returns the a dense array representing the potential pool permanences
125
         * 
126
         * Note: Only called from tests for now...
127
         * @param c
128
         * @return
129
         */
130
        public double[] getDensePermanences(Connections c) {
131
                double[] retVal = new double[c.getNumInputs()];
×
132
                Arrays.fill(retVal, 0);
×
133
                for(int inputIndex : connectionPerms.keys()) {
×
134
                        retVal[inputIndex] = connectionPerms.get(inputIndex).getPermanence();
×
135
                }
136
                return retVal;
×
137
        }
138
        
139
        /**
140
         * Returns an array of input bit indexes.
141
         * @return
142
         */
143
        public int[] getSparseConnections() {
144
                TIntList l = new TIntArrayList(connectionPerms.keys());
×
145
                l.reverse();
×
146
                return l.toArray();
×
147
        }
148
        
149
        /**
150
         * Returns the a dense array representing the potential pool bits
151
         * with the connected bits set to 1. 
152
         * 
153
         * Note: Only called from tests for now...
154
         * @param c
155
         * @return
156
         */
157
        public int[] getDenseConnections(Connections c) {
158
                int[] retVal = new int[c.getNumInputs()];
×
159
                Arrays.fill(retVal, 0);
×
160
                for(int inputIndex : synapseConnections.toArray()) {
×
161
                        retVal[inputIndex] = 1;
×
162
                }
163
                return retVal;
×
164
        }
165
        
166
        /**
167
         * Used internally to associated a {@link Synapse} with its current
168
         * permanence value.
169
         * 
170
         * @author David Ray
171
         */
172
        private class SynapsePair {
173
                private Synapse synapse;
174
                private double permanence;
175
                
176
                public SynapsePair(Synapse s, double p) {
×
177
                        this.synapse = s;
×
178
                        this.permanence = p;
×
179
                }
×
180
                
181
                public Synapse getSynapse() {
182
                        return synapse;
×
183
                }
184
                
185
                public double getPermanence() {
186
                        return permanence;
×
187
                }
188
                
189
                public void setPermanence(double permanence) {
190
                        this.permanence = permanence;
×
191
                }
×
192
        }
193
}
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

© 2026 Coveralls, Inc