forked from MrBly/WalnutiQ
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNeocortex.java
More file actions
128 lines (114 loc) · 5.31 KB
/
Neocortex.java
File metadata and controls
128 lines (114 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package model.MARK_II;
import model.MARK_II.connectTypes.AbstractRegionToRegionConnect;
import model.MARK_II.region.Region;
import model.MARK_II.util.Rectangle;
/**
* Neocortex is a directed graph of Regions. Each Region in the Neocortex
* can have as many children Regions as necessary. Each Region can receive
* input from input SensorCellLayers, a lower Region, or a higher Region.
*
* Input to Neocortex: activity of Cells within VisionCellLayer, AudioCellLayer,
* etc.
*
* Output from Neocortex: activity of Cells/Columns within all Regions.
*
* @author Quinn Liu (quinnliu@vt.edu)
* @author Michael Cogswell (cogswell@vt.edu)
* @author Nathan Waggoner(nwagg14@vt.edu)
* @version 12/18/2014
*/
public class Neocortex {
private Region rootRegion;
private Region currentRegion;
private AbstractRegionToRegionConnect connectType;
private int totalNumberOfRegions;
public Neocortex(Region rootRegion, AbstractRegionToRegionConnect neocortexRegionToNeocortexRegion) {
if (rootRegion == null) {
throw new IllegalArgumentException(
"rootRegion in Neocortex constructor cannot be null");
}
this.rootRegion = rootRegion;
this.currentRegion = this.rootRegion;
if (neocortexRegionToNeocortexRegion == null) {
throw new IllegalArgumentException(
"connectType in class Neocortex constructor cannot be null");
}
this.connectType = neocortexRegionToNeocortexRegion;
}
/**
* Traverses the region within this neocortex to find the region the desired biological name
* and changes the currentRegion to point to this region.
*
* @param newCurrentRegionBiologicalName name of Region in Neocortex
*/
public void changeCurrentRegionTo(String newCurrentRegionBiologicalName) {
if (newCurrentRegionBiologicalName == null) {
throw new IllegalArgumentException(
"newCurrentRegionBiologicalName in class Neocortex method changeCurrentRegionTo() cannot be null");
}
Region newCurrentRegion = this.getRegion(newCurrentRegionBiologicalName);
if (newCurrentRegion == null) {
throw new IllegalArgumentException("newCurrentRegionBiologicalName = " + newCurrentRegionBiologicalName
+ " in class Neocortex method changeCurrentRegionTo() does not exist in the Neocortex");
}
this.currentRegion = newCurrentRegion;
}
public Region getRegion(String regionBiologicalName) {
if (regionBiologicalName == null) {
throw new IllegalArgumentException(
"newCurrentRegionBiologicalName in class Neocortex method getRegion() cannot be null");
}
// search the neocortex for region
return this.recursiveFind(regionBiologicalName, this.rootRegion, 0);
}
Region recursiveFind(String regionBiologicalName, Region current, int numberOfRegionsVisited) {
if (numberOfRegionsVisited > this.totalNumberOfRegions) {
return null;
}
else if(current.getBiologicalName().equals(regionBiologicalName)) {
return current;
}
else{
for(Region child : current.getChildRegions()){
Region possible = recursiveFind(regionBiologicalName, child, numberOfRegionsVisited + 1);
if(possible != null){
return possible;
}
}
}
return null;
}
public void addToCurrentRegion(Rectangle rectanglePartOfParentRegionToConnectTo, Region childRegion,
int numberOfColumnsToOverlapAlongNumberOfRows,
int numberOfColumnsToOverlapAlongNumberOfColumns) {
if (childRegion == null) {
throw new IllegalArgumentException(
"childRegion in class Neocortex method addToCurrentRegion cannot be null");
}
Region regionAlreadyInNeocortex = this.getRegion(childRegion.getBiologicalName());
// NOTE: The first if and else if statement ARE necessary and
// it is important to understand why nothing should be done
if (regionAlreadyInNeocortex == null) {
// childRegion is new so we can add given childRegion to current
// region. Note this is not an error.
} else if (regionAlreadyInNeocortex.equals(childRegion)) {
// the user is trying to make a cycle connection within region in
// the Neocortex which is allowed
} else if (regionAlreadyInNeocortex != null) {
throw new IllegalArgumentException(
"childRegion in class Neocortex method addToCurrentRegion" +
" already exists within the Neocortex as another region " +
"with the same name");
}
this.currentRegion.addChildRegion(childRegion);
this.totalNumberOfRegions++;
// connect currentRegion to childRegion
this.connectType.connect(childRegion.getColumns(),
this.currentRegion.getColumns(rectanglePartOfParentRegionToConnectTo),
numberOfColumnsToOverlapAlongNumberOfRows,
numberOfColumnsToOverlapAlongNumberOfColumns);
}
public Region getCurrentRegion() {
return this.currentRegion;
}
}