简体   繁体   English

如何根据 Java 中其他对象值的变化使 object 更新其值?

[英]How do I make object to update its value, based on other objects value change, in Java?

I'm trying to make basic neural.network simulation.我正在尝试进行基本的神经网络模拟。 It consists of Neurons and NeuronConnections.它由 Neurons 和 NeuronConnections 组成。 In code below, the value of neuron2, should change whenever value of neuron1 is updated:在下面的代码中,neuron2 的值应该在 neuron1 的值更新时发生变化:

public class Main {

    public static void main(String[] args) {
        Neuron neuron1 = new Neuron();
        Neuron neuron2 = new Neuron();

        NeuronConnection neuronConnection = new NeuronConnection(neuron1, neuron2);

        neuron1.addInput(20);
        System.out.println(neuron2.getOutput());
    }
}

For now, I get just "0" which is default value.现在,我只得到默认值“0”。

Here is code for Neuron and NeuronConncetion objects:这是 Neuron 和 NeuronConncetion 对象的代码:

public class Neuron {
    private double output;
    private List<Double> inputArray;

    public Neuron() {
        output = 0;
        inputArray = new LinkedList<>();
    }

    public Neuron (double input) {
        inputArray = new LinkedList<>();
        inputArray.add(input);
        output += input;
    }

    public void addInput(double input) {
        inputArray.add(input);
        output += input;
    }

    public void addMultipleInputs(List<Double> inputs) {
        inputArray.addAll(inputs);
        for (double input: inputs) {
            output += input;
        }
    }

    public double getOutput() {
        return output;
    }
}
public class NeuronConnection {
    private double weight;
    private Neuron inNeuron;
    private Neuron outNeuron;
    private double outValue;

    public NeuronConnection(Neuron inNeuron, Neuron outNeuron) {
        this.inNeuron = inNeuron;
        this.outNeuron = outNeuron;
        weight = Math.random();
        outValue = inNeuron.getOutput()*weight;
        outNeuron.addInput(outValue);
    }

    public double getOutValue() {
        return outValue;
    }
}

The questions is : How do I make neuron2 change its value, whenever I change input of neuron1?问题是:每当我改变 neuron1 的输入时,如何让 neuron2 改变它的值?

I am providing you a simple solution.我正在为您提供一个简单的解决方案。 You just have to keep a reference to NeuronConnection in Neuron class and add a method setConnection() and call NeuronConnection's update() method (which we'll add later in NeuronConnection ) from addInput() or addMultipleInputs() method of Neuron class .您只需在Neuron class 中保留对NeuronConnection的引用,并添加一个方法 setConnection() 并从Neuron class class 的addInput()addMultipleInputs()方法调用NeuronConnection's update() method (我们稍后将在NeuronConnection中添加)。 The new design of Neuron class: Neuron class的新设计:

public class Neuron {
    private NeuronConnection conn;
    private double output;
    private List<Double> inputArray;

    public Neuron() {
        output = 0;
        inputArray = new LinkedList<>();
    }

    public Neuron (double input) {
        inputArray = new LinkedList<>();
        inputArray.add(input);
        output += input;
    }

    public void addInput(double input) {
        inputArray.add(input);
        output += input;
        conn.update();
    }

    public void addMultipleInputs(List<Double> inputs) {
        inputArray.addAll(inputs);
        for (double input: inputs) {
            output += input;
        }
        conn.update();
    }

    public double getOutput() {
        return output;
    }

    // i've added this method
    public void setConnection(NeuronConnection conn) {
        this.conn = conn;
    }
}

Now, redesign your NeuronConnection class:现在,重新设计您的NeuronConnection class:

public class NeuronConnection {
    private double weight;
    private Neuron inNeuron;
    private Neuron outNeuron;
    private double outValue;

    public NeuronConnection(Neuron inNeuron, Neuron outNeuron) {
        this.inNeuron = inNeuron;
        this.outNeuron = outNeuron;

        // now, setConnection
        this.inNeuron.setConnection(this);
        this.outNeuron.setConnection(this);

        weight = Math.random();
        outValue = inNeuron.getOutput()*weight;
        outNeuron.addInput(outValue);
    }

    public double getOutValue() {
        return outValue;
    }

    // i've added this
    public void update() {
      // this calculation is little flawed
      // you've to edit/fix this as you think it will be
      // perfect for your neural network
      outValue = inNeuron.getOutput() * weight;
      outNeuron.addInput(outValue);
    }
}

Now, the Main class, there's no change...现在,Main class,没有变化......

public class Main {

    public static void main(String[] args) {
        Neuron neuron1 = new Neuron();
        Neuron neuron2 = new Neuron();

        NeuronConnection neuronConnection = new NeuronConnection(neuron1, neuron2);

        neuron1.addInput(20);
        System.out.println(neuron2.getOutput());
    }
}

I haven't tested the code, but it should get you on the right track...我没有测试代码,但它应该让你走上正确的轨道......

Let me know, if you've any question...让我知道,如果你有任何问题...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改 java Path 对象的值? - How do I change the value of a java Path object? 我如何更改其他类中的变量,该变量将更改Java所有类中的值 - how do i change a variable in an others class that will change its value in all class in java 如何根据键值对将JSON对象反序列化到接口的实现上? - How do I deserialize a JSON object onto an implementation of an interface based on its key-value pairs? JAVA:如何基于属性值合并arrayList中的对象? - JAVA: How do I merge objects from an arrayList based on a property value? 如何根据 POJO 中的另一个值更改变量的值? - How do I change the value of a variable based on another value in POJO? 如何在 Java 中选择一个对象来更改其字段? - How do I choose an object in Java to change its fields? 如何在不删除其他变量的情况下更新Java中的属性值 - How do I update a properties value in java without deleting other variables Java:如何将两个变量设置为相同的值,然后更改一个而不更改另一个? - Java: How do I set two variables to the same value, then change one without changing the other? 我应该如何在Java中存储基于两个对象的值? - How should I store a value based on two objects in Java? 如何根据 Java 对象的实例变量值检索它 - How do I retrieve a Java Object based on a value of it's instance variables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM