/*
* File name: NeuronBrain.java (package eas.simulation.brain.neural)
* Author(s): Lukas König
* Java version: 6.0
* Generation date: 02.08.2011 (15:31:36)
*
* (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 java.awt.image.BufferedImage;
import java.util.HashMap;
import eas.simulation.Wink;
import eas.simulation.agent.AbstractAgent;
import eas.simulation.agent.GenericActuator;
import eas.simulation.agent.GenericRealValueDrivenActuator;
import eas.simulation.agent.GenericRealValuedSensor;
import eas.simulation.agent.GenericSensor;
import eas.simulation.brain.AbstractBrain;
import eas.simulation.brain.neural.functions.ActivationFunctionConstant;
import eas.simulation.brain.neural.functions.ActivationFunctionSigmoid;
import eas.simulation.standardEnvironments.AbstractEnvironment;
import eas.startSetup.ParCollection;
/**
* @author Lukas König
*
*/
public class NeuroBrain<AgentType extends AbstractAgent<?>> extends AbstractBrain<AgentType> {
private static final long serialVersionUID = 3949741176477074588L;
private GeneralNeuralNetwork neuralNet;
private ParCollection pars;
private HashMap<Integer, GenericRealValueDrivenActuator<AbstractEnvironment<?>, AbstractAgent<?>>> actuators
= new HashMap<Integer, GenericRealValueDrivenActuator<AbstractEnvironment<?>,AbstractAgent<?>>>();
private HashMap<Integer, GenericRealValuedSensor<AbstractEnvironment<?>, AbstractAgent<?>>> sensors
= new HashMap<Integer, GenericRealValuedSensor<AbstractEnvironment<?>,AbstractAgent<?>>>();
@SuppressWarnings({ "unchecked", "rawtypes" })
public NeuroBrain(AgentType brainsBody, ParCollection params) {
super(brainsBody);
this.pars = params;
neuralNet = new GeneralNeuralNetwork(this.pars);
neuralNet.setStandardActFct(new ActivationFunctionSigmoid(1));
// Bias neuron.
this.neuralNet.addNeuron(new ActivationFunctionConstant(1), Neuron.INPUT_NEURON);
for (GenericSensor<Object, ?, AbstractAgent<?>> sens : this.getMyBody().getSensors()) {
try {
GenericRealValuedSensor<AbstractEnvironment<?>, AbstractAgent<?>> sensor
= (GenericRealValuedSensor) sens;
int id = neuralNet.addNeuron(Neuron.INPUT_NEURON);
sensors.put(id, sensor);
} catch (Exception e) {
}
}
this.neuralNet.addNeuron(new ActivationFunctionSigmoid(0.001), Neuron.HIDDEN_NEURON); // TODO: Delete this line!
for (GenericActuator<?, AbstractAgent<?>> akt : this.getMyBody().getActuators()) {
try {
GenericRealValueDrivenActuator<AbstractEnvironment<?>, AbstractAgent<?>> actuator
= (GenericRealValueDrivenActuator<AbstractEnvironment<?>, AbstractAgent<?>>) akt;
int id = neuralNet.addNeuron(Neuron.OUTPUT_NEURON);
actuator.setMaxRange(2);
actuator.setMinRange(-2);
actuators.put(id, actuator);
} catch (Exception e) {
}
}
this.neuralNet.addLink(1, 2, -2); // TODO: Delete this line!
this.neuralNet.addLink(2, 4, 1); // TODO: Delete this line!
this.neuralNet.addLink(1, 3, 0.5); // TODO: Delete this line!
}
@Override
public BufferedImage generateMRTImage() {
return this.neuralNet.generateNeuroImage(400);
}
@Override
public void decideAndReact(Wink time) {
for (int i : this.sensors.keySet()) {
this.neuralNet.setInput(i, this.sensors.get(i).sense(this.getMyBody().getEnvironment(), this.getMyBody()));
}
this.neuralNet.propagate();
for (int i : this.actuators.keySet()) {
GenericRealValueDrivenActuator<AbstractEnvironment<?>, AbstractAgent<?>> actuator = this.actuators.get(i);
actuator.setActuatorValue(neuralNet.getNeuron(i).getNetOutput());
actuator.actuate(this.getMyBody().getEnvironment(), this.getMyBody());
}
}
}