/*
* File name: HopfieldNetwork.java (package eas.simulation.brain.neural)
* Author(s): lko
* Java version: 7.0
* Generation date: 26.04.2013 (21:46:57)
*
* (c) This file and the EAS (Easy Agent Simulation) framework containing it
* is protected by Creative Commons by-nc-sa license. Any altered or
* further developed versions of this file have to meet the agreements
* stated by the license conditions.
*
* In a nutshell
* -------------
* You are free:
* - to Share -- to copy, distribute and transmit the work
* - to Remix -- to adapt the work
*
* Under the following conditions:
* - Attribution -- You must attribute the work in the manner specified by the
* author or licensor (but not in any way that suggests that they endorse
* you or your use of the work).
* - Noncommercial -- You may not use this work for commercial purposes.
* - Share Alike -- If you alter, transform, or build upon this work, you may
* distribute the resulting work only under the same or a similar license to
* this one.
*
* + Detailed license conditions (Germany):
* http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* + Detailed license conditions (unported):
* http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
*
* This header must be placed in the beginning of any version of this file.
*/
package eas.simulation.brain.neural;
import eas.simulation.brain.neural.functions.ActivationFunction;
import eas.simulation.brain.neural.functions.ActivationFunctionStep;
import eas.simulation.brain.neural.functions.TransitionFunction;
import eas.simulation.brain.neural.functions.TransitionFunctionWeightedSum;
import eas.startSetup.ParCollection;
/**
* @author Lukas König
*/
public class HopfieldNetwork extends GeneralNeuralNetwork {
private static final long serialVersionUID = -5424155853694112457L;
private boolean initializationPhase;
private double threashold;
public HopfieldNetwork(ParCollection params, double threashold) {
super(params);
this.initializationPhase = true;
this.threashold = threashold;
this.setStandardActFct(new ActivationFunctionStep(this.threashold, -1, 1));
this.setStandardTrnFct(new TransitionFunctionWeightedSum());
this.setAllowLinksFromOutputs(true);
this.setAllowLinksToInputs(true);
this.setAllowRecurrentLinks(true);
this.initializationPhase = false;
}
@Override
public void setStandardActFct(ActivationFunction actFct) {
if (!this.initializationPhase) {
throw new RuntimeException("Activation function cannot be changed for Hopfield network.");
}
super.setStandardActFct(actFct);
}
@Override
public void setStandardTrnFct(TransitionFunction trnFct) {
if (!this.initializationPhase) {
throw new RuntimeException("Transition function cannot be changed for Hopfield network.");
}
super.setStandardTrnFct(trnFct);
}
@Override
public int addNeuron(int neuronType) {
return super.addNeuron(neuronType);
}
@Override
public int addNeuron(int neuronID, int neuronType) {
return super.addNeuron(neuronID, this.getStandardActFct(), this.getStandardTrnFct(), neuronType);
}
@Override
public int addNeuron(ActivationFunction actFctPhi, int neuronType) {
throw new RuntimeException("Activation function cannot be changed for Hopfield network.");
}
@Override
public int addNeuron(ActivationFunction actFctPhi,
TransitionFunction trnFctSigma, int neuronType) {
throw new RuntimeException("Activation function cannot be changed for Hopfield network.");
}
@Override
public int addNeuron(int neuronID, ActivationFunction actFctPhi,
int neuronType) {
throw new RuntimeException("Activation function cannot be changed for Hopfield network.");
}
@Override
public int addNeuron(int neuronID, ActivationFunction actFctPhi,
TransitionFunction trnFctSigma, int neuronType) {
throw new RuntimeException("Activation function cannot be changed for Hopfield network.");
}
@Override
public boolean addLink(int sourceNeuron, int targetNeuron, double weight2) {
double weight = weight2;
if (sourceNeuron == targetNeuron) {
weight = 0;
}
if (super.addLink(sourceNeuron, targetNeuron, weight)
& super.addLink(targetNeuron, sourceNeuron, weight)) {
return true;
} else {
this.removeLink(sourceNeuron, targetNeuron);
return false;
}
}
@Override
public boolean removeLink(int sourceID, int targetID) {
if (super.removeLink(sourceID, targetID)
& super.removeLink(targetID, sourceID)) {
return true;
} else {
return true;
}
}
@Override
public void setAllowLinksToInputs(boolean allowLinksToInputs) {
super.setAllowLinksToInputs(allowLinksToInputs);
}
@Override
public void setAllowRecurrentLinks(boolean allowRecurrentLinks) {
if (!allowRecurrentLinks) {
throw new RuntimeException("Recurrent links required in Hopfield network.");
}
super.setAllowRecurrentLinks(true);
}
@Override
public void setAllowLinksFromOutputs(boolean allowLinksFromOutputs) {
super.setAllowLinksFromOutputs(allowLinksFromOutputs);
}
public void setThreadsholdForNeuron(int neuronID, double threashold) {
this.getNeuron(neuronID).setActivationFunctionPhi(new ActivationFunctionStep(threashold, -1, 1));
}
@Override
public boolean setWeight(int sourceID, int targetID, double weight2) {
double weight = weight2;
if (sourceID == targetID) {
weight = 0;
}
return super.setWeight(sourceID, targetID, weight)
&& super.setWeight(targetID, sourceID, weight);
}
}