Pointing one object to another

I have an object, Neuron with properties

public Neuron(String id,double membranePotential,double threshold,double weight,

boolean fired)

Say I have a neuron and I want another one to 'listen' to it would this be appropriate

i.e.publicvoid connect(Neuron input, Neuron output){

output.equals(input);

}

I have a series of input neurons in one layer and a single output neuron, but I'm not sure how to set these up so that the output neuron is being pointed to by the input neurons - essentially when a value in the input neuron changes I want this change reflected in the output neuron i.e.

output.setValue(input.getValue());

Is what I'm trying to do tenable?

[1011 byte] By [dhaval_shaha] at [2007-11-26 23:59:58]
# 1
My recommendation would be to use the Observable/Observer pattern http://java.sun.com/javase/6/docs/api/java/util/Observable.htmlmake sure you call setChanged before you call notifyObservers
tjacobs01a at 2007-7-11 15:49:28 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks for that, but is there no way I can use the object I've created and achieve what I wish to do like that? I'm gathering you can as it is a subclass of object, but how would I go about doing this? Something like

class Neuron extends Observable

seems wrong because Observable is a subclass so I wouldn't see why you'd extend it. I'm confused - please help!

dhaval_shaha at 2007-7-11 15:49:28 > top of Java-index,Java Essentials,Java Programming...
# 3

You could store a reference to all objects that wish to be notified in a collection within the neuron class. You could then iterate through these objects and notify them when you wish.

However this is pretty much what Observable does.

Observable is a subclass of object but would be the superclass of Neuron which in turn would be a subclass of Observable.

The Observable/Observer pattern is exactly what you need anything you do yourself would be re-inventing the wheel.

ChristopherAngela at 2007-7-11 15:49:28 > top of Java-index,Java Essentials,Java Programming...
# 4

Right, that's what I do already - I put all my input neurons into a vector and when I need to retrieve I iterate through and get whatever I need from them using the appropriate getter method. I just thought there might actually be a way of pointing to directly rather than passing by reference.

"Observable is a subclass of object but would be the superclass of Neuron which in turn would be a subclass of Observable."

Are you saying that that object would be the Neuron superclass or Observable would be? Don't really follow what you're getting at, sorry.

dhaval_shaha at 2007-7-11 15:49:28 > top of Java-index,Java Essentials,Java Programming...
# 5

Observable would be the superclass of neuron if you extend it as in your code snippet above.

I'm poorly trying to clarify your statement that Observable is a subclass. This is true but only in the sense that all objects except java.lang.Object are subclasses.

> Right, that's what I do already - I put all my input neurons into a vector and when I need to retrieve I iterate through and get whatever I need from them using the appropriate getter method. I just thought there might actually be a way of pointing to directly rather than passing by reference.

This isn't what I was describing or what Observable does:

Each Neuron would hold a list of other neurons which were interested in receiving updates.

ChristopherAngela at 2007-7-11 15:49:28 > top of Java-index,Java Essentials,Java Programming...
# 6
Ok, so if I wanted to do it would it look something like:Observable o = new Neuron();o.addObserver(output neuron which wishes to receive updates);I'm aware that I might be talking rubbish here - please correct me if I'm wrong.
dhaval_shaha at 2007-7-11 15:49:28 > top of Java-index,Java Essentials,Java Programming...
# 7

To clarify, this what I currently do:

Object in1 = new Object(some params);

Object in2 = new Object(some params);

Object out1 = new Object(some params);

List list = new ArrayList();

connect(in1, out1);

connect(in2, out1);

public void connect(Object input, Object output){

list.add(input);

}

Then when I need to retrieve something I go:

for(int i = 0; i < list.size(); i++){

//iterate through and get a parameter of input neuron

//so that

Object o = (Object)list.get(i);

double temp = o.getSomeValue();

output.setSomeValue() = temp;

}

Cumbersome I know, hence why I want to improve on it. In short I'm building a neural network so that's why I want to join/point objects to each other

Message was edited by:

dhaval_shah

dhaval_shaha at 2007-7-11 15:49:28 > top of Java-index,Java Essentials,Java Programming...
# 8

You're on the right track:

I hope this simple example helps:

import java.util.Observable;

import java.util.Observer;

public class Neuron extends Observable implements Observer {

private static int neuronCount = 0;

private int id;

private double threshold;

public Neuron() {

id = ++neuronCount;

}

public void setInput(double value) {

if (value > threshold) {

System.err.println("Neuron "+id+" fired.");

this.setChanged();

this.notifyObservers(value);

}

}

public void update(Observable o, Object arg) {

Neuron srcNeuron = (Neuron) o;

System.err.println("Neuron "+id+" notified by "+srcNeuron.id);

Double value = (Double) arg;

setInput(value);

}

public static void main(String[] args) {

Neuron n1 = new Neuron();

Neuron n2 = new Neuron();

Neuron n3 = new Neuron();

Neuron n4 = new Neuron();

n1.addObserver(n2);

n1.addObserver(n3);

n1.addObserver(n4);

n2.addObserver(n4);

n1.setInput(10);

}

}

The Neuron not only wants to notify other objects but in turn be notified by objects hence the use of both Observable and Observer.

ChristopherAngela at 2007-7-11 15:49:28 > top of Java-index,Java Essentials,Java Programming...
# 9
Thanks, much appreciated
dhaval_shaha at 2007-7-11 15:49:28 > top of Java-index,Java Essentials,Java Programming...
# 10

The Observer pattern is EXACTLY the way to go here. Putting your notification receivers into a collection and then handling the notification yourself is both cumbersome and risky in that entirely too many things can go wrong that you will now be taking responsibility for handling.

Not to mention (ok I'm going to mention it after all) the time to spin through the collection and do the notifications yourself while manageable at low levels would become onerous if you suddenly ended up with say several million neurons in your collection. Admittedly if you have that many you're going to see some performance degredation anyway, but not as badly to be sure.

Bottom line, the Observer does it for you, use the blocks you already have to work with rather than building your own if at all possible.

Hope this helps,

PS.

puckstopper31a at 2007-7-11 15:49:28 > top of Java-index,Java Essentials,Java Programming...