-
Notifications
You must be signed in to change notification settings - Fork 0
/
CANTPattern.java
executable file
·158 lines (139 loc) · 4.77 KB
/
CANTPattern.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
import java.util.*;
public class CANTPattern {
private String name;
private int id;
private CANTNet net;
private int[] patternIndexes;
private int size;
private java.util.Vector miniPatterns = new java.util.Vector();
public int size() {return size; }
public int getPatternIndex(int i) {return patternIndexes[i];}
public String getName() {return name;}
public int getId() {return id;}
public CANTPattern(CANTNet net ,String name, int id, String[] points ) {
this.net = net;
this.id = id;
this.name = name;
init(points);
}
//create a pattern from a set of rectangles.
public CANTPattern(CANTNet net ,String name, int id, int cPoints, int[] points ) {
this.net = net;
this.id = id;
this.name = name;
init(cPoints, points);
}
private void init(String[] points){
Vector indexVector = new Vector();
for(int i=0; i<points.length; i++){
int index = points[i].indexOf(",");
int startIndex = Integer.parseInt(points[i].substring(0,index));
int endIndex = Integer.parseInt(points[i].substring(index+1));
Assert (startIndex <= endIndex);
if (startIndex > endIndex)
System.out.println(startIndex + " " + endIndex);
Rectangle rectangle = new Rectangle(startIndex,endIndex);
int[] tempPatternIndexes = rectangle.initPatternIndexes();
for(int j=0; j<tempPatternIndexes.length; j++)
indexVector.add(new Integer(tempPatternIndexes[j]));
}
size = indexVector.size();
patternIndexes = new int[size];
for(int i=0; i<size; i++){
patternIndexes[i] = ((Integer)indexVector.get(i)).intValue();
}
}
//make a pattern out of an array of points
private void init(int cPoints, int[] points){
size=cPoints;
patternIndexes = new int[size];
for(int i=0; i<cPoints; i++){
patternIndexes[i] = points[i];
}
}
private boolean Assert(boolean test) {
int x = -1;
if (! test)
try{
x = (1 / (1 +x));
}
catch(Exception e){
System.out.println("Pattern Assert= ");
return false;
}
return true;
}
public void arrange(int numberToActivate) {
if (numberToActivate > size) numberToActivate=size;
Assert (numberToActivate <= size);
for (int i= 0; i < numberToActivate; i++) {
int randomIndex = (int)(CANT23.random.nextFloat() * size);
int swap = patternIndexes[i];
patternIndexes[i] = patternIndexes[randomIndex];
patternIndexes[randomIndex] = swap;
}
}
//not tested.
private void removeAllPoints() {
for (int i = 0; i < size; i++)
patternIndexes[i]= -1;
}
//not tested.
public void replaceAllPoints(int size, int[] patternPoints) {
removeAllPoints();
patternIndexes = new int[size];
for (int i = 0; i < size; i++)
patternIndexes[i] = patternPoints[i];
}
public void print(){
// System.out.println("Name = " + name + "; Id = " + id + "; startIndex = " + startIndex + "; EndnIndex = " + endIndex);
System.out.print("size = " + size + "; ");
for(int i=0; i<size; i++){
System.out.print(" " + i + " = " + patternIndexes[i] + "; ");
}
System.out.println("");
}
//embedded class
//undone there are some bugs here. Problems with rectangles like 0-300 in a net
//with 20 columns, and 19-25 in a similar net.
public class Rectangle{
private int startIndex;
private int endIndex;
private int size;
public int getStartIndex() {return startIndex;}
public int getEndIndex() {return endIndex;}
public Rectangle(int startIndex, int endIndex){
this.startIndex = startIndex;
this.endIndex = endIndex;
setSize();
}
private void setSize() {
int cols = net.getCols();
int startColumn = startIndex % cols;
int startRow = startIndex / cols;
int endColumn = endIndex % cols;
int endRow = endIndex / cols;
// crh change 12-01-08 size = (endRow - startRow + 1) * (endColumn - startColumn + 1);
size = (endRow - startRow ) * net.getCols();
size += (endColumn - startColumn + 1);
//System.out.println(startColumn+" "+startRow+" "+endColumn+" "+endRow);
}
private boolean containsNeuron(int neuronId){
int cols = net.getCols();
if ((neuronId >=startIndex) && (neuronId <=endIndex))
//removed crh 12-01-08 && ((neuronId % cols) >=startIndex%cols) && ((neuronId % cols) <=endIndex%cols))
return true;
else
return false;
}
public int[] initPatternIndexes(){
int[] patternIndexes = new int[size];
for(int index = 0,i=startIndex; index<size && i<=endIndex; i++){
if (containsNeuron(i)) {
patternIndexes[index++] = i;
}
}
return patternIndexes;
}
}//embedded rectangle class
}