forked from MrBly/WalnutiQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSegment.java
166 lines (146 loc) · 5.25 KB
/
Segment.java
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package model.MARK_II;
import java.util.HashSet;
import java.util.Set;
/**
* Provides the base implementation for a DistalSegment and ProximalSegment.
*
* @author Quinn Liu ([email protected])
* @author Michael Cogswell ([email protected])
* @version July 22, 2013
*/
public class Segment {
protected Set<Synapse<Cell>> synapses;
protected boolean isActive;
/**
* Minimal percent of active Synapses out of total Synapses needed for a
* Segment to become active.
*/
public static double PERCENT_ACTIVE_SYNAPSES_THRESHOLD = 0.2;
/**
* Provides three enums to be used as parameters in the method
* updateSynapsePermanences(enumParameter). These 3 enums describe the three
* different ways that Synapse permanences on a Segment can be updated.
*
* @author Quinn Liu ([email protected])
* @author Michael Cogswell ([email protected])
*/
public enum SynapseUpdateState {
/**
* Increase permanence of Synapses with an active Cell on one Segment.
*/
INCREASE_ACTIVE,
/**
* Increase permanence of all Synapses on one Segment.
*/
INCREASE_ALL,
/**
* Decrease permanence of all Synapses on one Segment.
*/
DECREASE_ALL
}
public Segment() {
this.synapses = new HashSet<Synapse<Cell>>();
this.isActive = false;
}
/**
* @return true if this Segment has more active Synapses than the minimal
* number of active Synapses needed to activate this Segment based
* on PERCENT_ACTIVE_SYNAPSES_THRESHOLD.
*/
public boolean getActiveState() {
int numberOfActiveSynapses = 0;
for (Synapse<Cell> synapse : this.synapses) {
Cell abstractCell = synapse.getConnectedCell();
if (synapse.isConnected() && abstractCell.getActiveState()) {
numberOfActiveSynapses++;
}
}
int minimalNumberOfActiveSynapses = (int) (this.synapses.size() * PERCENT_ACTIVE_SYNAPSES_THRESHOLD);
if (numberOfActiveSynapses > minimalNumberOfActiveSynapses) {
this.isActive = true;
return true;
} else {
this.isActive = false;
return false;
}
}
/**
* @param updateState This enum parameter determines how permanence of all Synapses
* on a Segment will be updated.
*/
public void updateSynapsePermanences(SynapseUpdateState updateState) {
if (updateState == null) {
throw new IllegalArgumentException(
"updateState in Segment method updateSynapsePermanences cannot be null");
}
for (Synapse<Cell> synapse : this.synapses) {
switch (updateState) {
case INCREASE_ACTIVE:
if (synapse.isConnected()
&& synapse.getConnectedCell().getActiveState()) {
synapse.increasePermanence();
}
break;
case INCREASE_ALL:
synapse.increasePermanence();
break;
case DECREASE_ALL:
synapse.decreasePermanence();
break;
}
}
}
public void addSynapse(Synapse<Cell> synapse) {
if (synapse == null) {
throw new IllegalArgumentException(
"Synapse in Segment class method addSynapse cannot be null");
}
this.synapses.add((Synapse<Cell>) synapse);
}
public Set<Synapse<Cell>> getSynapses() {
return this.synapses;
}
public int getNumberOfActiveSynapses() {
int numberOfActiveSynapses = 0;
for (Synapse synapse : this.synapses) {
if (synapse.isConnected() && synapse.getConnectedCell().getActiveState()) {
numberOfActiveSynapses++;
}
}
return numberOfActiveSynapses;
}
public boolean removeSynapse(Synapse synapseToRemove) {
for (Synapse synapse : this.synapses) {
if (synapseToRemove.getConnectedCell().getClass()
.equals(synapse.getConnectedCell().getClass())
&& synapseToRemove.getPermanenceValue() == synapse
.getPermanenceValue()
&& synapseToRemove.getCellColumn() == synapse
.getCellColumn()
&& synapseToRemove.getCellRow() == synapse
.getCellRow()) {
this.synapses.remove(synapse);
return true;
}
}
return false;
}
public Synapse getSynapse(int cellXPosition, int cellYPosition) {
for (Synapse synapse : this.synapses) {
if (synapse.getCellColumn() == cellXPosition
&& synapse.getCellRow() == cellYPosition) {
return synapse;
}
}
return null;
}
public Set<Synapse<Cell>> getConnectedSynapses() {
Set<Synapse<Cell>> connectedSynapes = new HashSet<Synapse<Cell>>();
for (Synapse<Cell> synapse : this.synapses) {
if (synapse.getConnectedCell() != null) {
connectedSynapes.add(synapse);
}
}
return connectedSynapes;
}
}