-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sequence.java
153 lines (126 loc) · 3.13 KB
/
Sequence.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
import java.util.ArrayList;
abstract class Element {
abstract void update();
//abstract void Display();
}
class Link extends Element{
Node n1;
Neuron n2;
double wt;
double v;
public Link(Node n1, Neuron n2, double wt)
{
this.n1 = n1;
this.n2 = n2;
this.wt = wt;
n2.attachInput(this);
}
public void SetWeight(double wt)
{
this.wt = wt;
}
public double getWeight() {
return this.wt;
}
public double getV() {
return v;
}
public void update() {
v = wt* n1.getV();
return;
}
}
abstract class Node extends Element {
public abstract double getV();
}
class InputSignal extends Node {
public int count = 0;
public double v = 1;
public double getV() {
return v;
}
public void update() {
count = count +1;
v = 0.001 * count * count;
return;
}
}
class Neuron extends Node{
private double v;
private ArrayList<Reporter> reporterList;
public Neuron() {
inputs = new ArrayList<Link>();
reporterList = new ArrayList<Reporter>();
}
private ArrayList<Link> inputs;
public void attachInput(Link l){
inputs.add(l);
}
public double getV() {
return v;
}
public void update() {
double v = 0;
double t = 0;
for (Link l : inputs) {
t = t + l.getV();
}
this.v = (2 /(1 + Math.exp(-2*t))) - 1;
for (Reporter r: reporterList) {
r.addValue(this.v);
}
}
public void attachReporter(Reporter r)
{
reporterList.add(r);
}
}
public class Sequence{
ArrayList<Element> elements;
public Sequence() {
elements = new ArrayList<Element>();
}
public void addElement(Element e) {
elements.add(e);
}
public void cycle()
{
for (Element e: elements ) {
e.update();
}
}
public static void main(String[] args) {
InputSignal s1 = new InputSignal();
Reporter rep = new LineReporter();
Reporter rep2 = new LineReporter();
rep.init();
rep2.init();
Neuron n1 = new Neuron();
Neuron n2 = new Neuron();
Neuron n3 = new Neuron();
Neuron n4 = new Neuron();
n3.attachReporter(rep2);
n4.attachReporter(rep);
Link l1= new Link(s1,n1,1.0);
Link l2= new Link(s1,n2,1.0);
Link l3= new Link(n2,n3,1.0);
Link l4= new Link(n1,n4,1.0);
Link l5= new Link(n3,n4,-1.0);
Sequence s = new Sequence();
s.addElement(n4);
s.addElement(l5);
s.addElement(l4);
s.addElement(n3);
s.addElement(l3);
s.addElement(n2);
s.addElement(n1);
s.addElement(l2);
s.addElement(l1);
s.addElement(s1);
for (int i=0; i < 100; i++) {
s.cycle();
}
rep.report();
rep2.report();
}
}