Skip to content

Instantly share code, notes, and snippets.

@cogmission
Created July 31, 2016 19:58
Show Gist options
  • Save cogmission/1978d2c3aae10c1fa65592dcec964d29 to your computer and use it in GitHub Desktop.
Save cogmission/1978d2c3aae10c1fa65592dcec964d29 to your computer and use it in GitHub Desktop.
```excitedColumnsGenerator()``` Method in Java
/* ---------------------------------------------------------------------
* Numenta Platform for Intelligent Computing (NuPIC)
* Copyright (C) 2016, Numenta, Inc. Unless you have an agreement
* with Numenta, Inc., for a separate license for this software code, the
* following terms and conditions apply:
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero Public License for more details.
*
* You should have received a copy of the GNU Affero Public License
* along with this program. If not, see http://www.gnu.org/licenses.
*
* http://numenta.org/licenses/
* ---------------------------------------------------------------------
*/
package org.numenta.nupic.util;
import java.util.List;
import org.numenta.nupic.Connections;
import org.numenta.nupic.model.Column;
import org.numenta.nupic.model.DistalDendrite;
/**
* Just a snippet to contain the method {@link #excitedColumnsGenerator(List, List, List, Connections)}
* @author cogmission
*/
public class Snippet {
private Generator<NamedTuple> excitedColumnsGenerator(
List<Column> activeColumns, List<DistalDendrite> activeSegments, List<DistalDendrite> matchingSegments,
Connections connections) {
return new AbstractGenerator<NamedTuple>() {
int activeColumnsProcessed = 0;
int activeSegmentsProcessed = 0;
int matchingSegmentsProcessed = 0;
int activeColumnsNum = activeColumns.size();
int activeSegmentsNum = activeSegments.size();
int matchingSegmentsNum = matchingSegments.size();
boolean isActiveColumn;
@Override
public void exec() {
while((activeColumnsProcessed < activeColumnsNum ||
activeSegmentsProcessed < activeSegmentsNum ||
matchingSegmentsProcessed < matchingSegmentsNum) && !haltRequested()) {
int currentColumn = Integer.MAX_VALUE;
if(activeSegmentsProcessed < activeSegmentsNum) {
currentColumn = Math.min(
currentColumn,
activeSegments.get(activeSegmentsProcessed).getPresynapticColumnIndex());
}
if(matchingSegmentsProcessed < matchingSegmentsNum) {
currentColumn = Math.min(
currentColumn,
matchingSegments.get(matchingSegmentsProcessed).getPresynapticColumnIndex());
}
if(activeColumnsProcessed < activeColumnsNum &&
activeColumns.get(activeColumnsProcessed).getIndex() <= currentColumn) {
currentColumn = activeColumns.get(activeColumnsProcessed).getIndex();
isActiveColumn = true;
activeColumnsProcessed += 1;
} else {
isActiveColumn = false;
}
int activeSegmentsBegin = activeSegmentsProcessed;
int activeSegmentsEnd = activeSegmentsProcessed;
for(int i = activeSegmentsProcessed;i < activeSegmentsNum;i++) {
if(activeSegments.get(i).getPresynapticColumnIndex() == currentColumn) {
activeSegmentsProcessed += 1;
activeSegmentsEnd += 1;
} else {
break;
}
}
int matchingSegmentsBegin = matchingSegmentsProcessed;
int matchingSegmentsEnd = matchingSegmentsProcessed;
for(int i = matchingSegmentsProcessed;i < matchingSegmentsNum;i++) {
if(matchingSegments.get(i).getPresynapticColumnIndex() == currentColumn) {
matchingSegmentsProcessed += 1;
matchingSegmentsEnd += 1;
} else {
break;
}
}
Generator<Integer> asIndexGenerator = IntGenerator.of(activeSegmentsBegin, activeSegmentsEnd);
Generator<Integer> msIndexGenerator = IntGenerator.of(matchingSegmentsBegin, matchingSegmentsEnd);
yield(
new NamedTuple(
new String[] {
"column",
"isActiveColumn",
"activeSegments",
"activeSegmentsCount",
"matchingSegments",
"matchingSegmentsCount"
},
currentColumn,
isActiveColumn,
SegmentGenerator.of(activeSegments, asIndexGenerator),
activeSegmentsEnd - activeSegmentsBegin,
SegmentGenerator.of(matchingSegments, msIndexGenerator),
matchingSegmentsEnd - matchingSegmentsBegin
)
);
}
}
@Override
public boolean isConsumed() {
return activeColumnsProcessed >= activeColumnsNum &&
activeSegmentsProcessed >= activeSegmentsNum &&
matchingSegmentsProcessed >= matchingSegmentsNum;
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment