My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
package hef.neuralnet.bitmap.v1;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;



public class Neuron
{
private int inputCount;
private Map<int[], Integer> memory;

/**
* Constructs a neuron
*
* @param inputCount Specifies how many inputs this neuron should accept when the <code>teach</code> or
* <code>use</code> methods are invoked
*/
public Neuron(int inputCount) {
this.inputCount = inputCount;
this.memory = new LinkedHashMap<int[], Integer>();
}

public void teach(int[] inputs, int output) {
assert inputs.length == this.inputCount;

this.memory.put(Arrays.copyOf(inputs, inputs.length), output);
}

public int use(int[] inputs) {
int bestDist = Integer.MAX_VALUE;
int bestOut = -1;

for (Map.Entry<int[], Integer> kv : this.memory.entrySet()) {
int dist = distance(inputs, kv.getKey());
if (dist <= bestDist) {
bestDist = dist;
bestOut = kv.getValue();
}
}

if (bestDist != 0) this.teach(inputs, bestOut);

return bestOut;
}

private static int distance(int[] a, int[] b) {
assert a.length == b.length;

int dist = 0;
for (int i = 0; i < a.length; i += 1)
dist += Math.abs(a[i] - b[i]);

return dist;
}
}

Change log

r10 by jonathan.hefner on Jul 4, 2008   Diff
sync
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 1474 bytes, 57 lines
Powered by Google Project Hosting