/*
* File name: BraitenbergMove.java (package eas.users.tobias)
* Author(s): tobias
* Java version: 6.0
* Generation date: 25.04.2012 (18:51:01)
*
* (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.users.ocScenarios.oclrss12.gruppe4;
import java.util.Iterator;
import java.util.LinkedList;
import eas.math.geometry.Vector2D;
import eas.simulation.agent.GenericActuator;
import eas.simulation.spatial.sim2D.standardAgents.AbstractAgent2D;
/**
* @author tobias
*
*/
public class BraitenbergMove extends GenericActuator<BraitenbergEnvironment, AbstractAgent2D<?>>
{
/**
*
*/
private static final long serialVersionUID = 7255414524521842204L;
/* (non-Javadoc)
* @see eas.simulation.agent.GenericActuator#actuate(eas.simulation.environment.AbstractEnvironment, eas.simulation.agent.AbstractAgent)
*/
@Override
@SuppressWarnings("unchecked")
public void actuate(BraitenbergEnvironment env, AbstractAgent2D<?> agent)
{
LinkedList<Double> data = (LinkedList<Double>) agent.sense("Braitenberg");
// Signalstaerken in den 4 Richtungen.
double[] vektor = new double[4];
Iterator<Double> it = data.iterator();
//die Werte fuer Lieben in Vektor schreiben
for(int i = 0; i < 4; i++)
{
vektor[i] = 2*it.next();
}
// die Werte fuer Hassen abziehen
for(int i = 0; i < 4; i++)
{
vektor[i] = vektor[i] - it.next();
}
Vector2D position = env.getAgentPosition(agent.id());
makePositive(vektor);
fahren(vektor, getDirection(vektor), false, env, agent, position );
}
private void fahren(double[] vektor, int direction, boolean finish, BraitenbergEnvironment env, AbstractAgent2D<?> agent, Vector2D position)
{
switch (direction) //hoffe, dass Kollisionen && rausfahren automatisch verhindert wird;
{
case 0://hoch
if (env.getLight(position.x, position.y - 1) == null){
boolean b = env.setAgentPosition(agent.id(), new Vector2D(position.x, position.y - 1));
env.setAgentAngle(agent.id(), 180);
if(b == false && !finish)
{
fahren(vektor, getDirectionOhne(vektor, direction), true, env, agent, position);
}
}
break;
case 1://runter
if (env.getLight(position.x, position.y + 1) == null){
boolean b = env.setAgentPosition(agent.id(), new Vector2D(position.x, position.y + 1));
env.setAgentAngle(agent.id(), 0);
if(b == false && !finish)
{
fahren(vektor, getDirectionOhne(vektor, direction), true, env, agent, position);
}
}
break;
case 2://links
if (env.getLight(position.x - 1, position.y) == null){
boolean b = env.setAgentPosition(agent.id(), new Vector2D(position.x - 1, position.y));
env.setAgentAngle(agent.id(), 90);
if(b == false && !finish)
{
fahren(vektor, getDirectionOhne(vektor, direction), true, env, agent, position);
}
}
break;
case 3://rechts
if (env.getLight(position.x + 1, position.y ) == null){
env.setAgentAngle(agent.id(), 270);
boolean b = env.setAgentPosition(agent.id(), new Vector2D(position.x + 1, position.y));
if(b == false && !finish)
{
fahren(vektor, getDirectionOhne(vektor, direction), true, env, agent, position);
}
}
break;
}
}
/**
* Richtung: die mit dem staerksten Signal
* @param vektor
* @return Richtung
*/
private int getDirection(double[] vektor)
{
int result = -1;
double value = 1.0;
//fahre im Zweifel nach links oder oben.
for(int index = 0; index < vektor.length; index++)
{
if(vektor[index] > value)
{
result = index;
value = vektor[index];
}
}
return result;
}
/**
* schliesst eine Richtung aus.
* @param vektor
* @param noDirection wird ausgeschlossen
* @return
*/
private int getDirectionOhne(double[] vektor, int noDirection)
{
int result = -1;
double value = 1.0;
//fahre im Zweifel nach links oder oben.
for(int index = 0; index < vektor.length; index++)
{
if(vektor[index] > value && index != noDirection)
{
result = index;
value = vektor[index];
}
}
return result;
}
/**
* addiert so viel zum Vektor hinzu, das Kleinster Wert mindenst 1.0 ist.
* es wird jeder Komponenten die gleiche Zahl addiert.
* @param vektor
*/
private void makePositive(double[] vektor)
{
double minValue = 0.00001;
for(int index = 0; index < vektor.length; index++)
{
if(vektor[index] <= minValue)
{
minValue = vektor[index];
}
}
double toAdd = 0;
if(minValue <= 0.00001)
{
toAdd = 1 - minValue;
}
for(int i = 0; i < vektor.length; i++)
{
vektor[i] = vektor[i] + toAdd;
}
}
/* (non-Javadoc)
* @see eas.simulation.agent.GenericActuator#id()
*/
@Override
public String id()
{
return "Braitenberg+Move";
}
}