/**
*
*/
package eas.users.students.christianNagel.coEvolutionChristian;
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.FileNotFoundException;
//import java.io.IOException;
//import java.io.ObjectInputStream;
import java.util.List;
import java.util.Random;
import eas.math.geometry.Vector2D;
import eas.plugins.masterScheduler.AbstractDefaultMaster;
import eas.simulation.spatial.sim2D.standardAgents.ObstacleAgent;
import eas.startSetup.ParCollection;
import eas.startSetup.SingleParameter;
import eas.startSetup.parameterDatatypes.Datatypes;
import eas.users.students.christianNagel.neuroBrain.NeuroBrainChristian;
/**
* @author Lukas König, christiannagel
*
*/
public class CoEvolutionMaster extends AbstractDefaultMaster<CoEvolutionEnv> {
/**
*
*/
private static final long serialVersionUID = -8240776322660347334L;
private Random random;
private final int defaultNumberOfSheep = 100;
private final int defaultNumberOfWolf = 20;
private final int defaultNumberOfDistanceSensors = 3;
private final int defaultNumberOfWolfDistanceSensors = 3;
private final double defaultWolfDetectionRadius = 50;
private final double defaultWolfCatchRadius = 5;
private final double defaultSheepSpeed = 1;
private final double defaultWolfSpeed = 1;
private final long defaultChooseSheepInterval = 20;
private ObstacleAgent[] obstacleAgents = new ObstacleAgent[4];
private SheepAgent[] sheepAgents;
private WolfAgent[] wolfAgents;
@Override
public List<SingleParameter> getParameters() {
List<SingleParameter> list = super.getParameters();
list.add(new SingleParameter("numberOfSheep", Datatypes.INTEGER,
defaultNumberOfSheep, "no additional information", this.id()
.toUpperCase()));
list.add(new SingleParameter("numberOfWolf", Datatypes.INTEGER,
defaultNumberOfWolf, "no additional information", this.id()
.toUpperCase()));
list.add(new SingleParameter("wolfDetectionRadius", Datatypes.DOUBLE,
defaultWolfDetectionRadius, "no additional information", this
.id().toUpperCase()));
list.add(new SingleParameter("wolfCatchRadius", Datatypes.DOUBLE,
defaultWolfCatchRadius, "no additional information", this.id()
.toUpperCase()));
list.add(new SingleParameter("sheepSpeed", Datatypes.DOUBLE,
defaultSheepSpeed, "no additional information", this.id()
.toUpperCase()));
list.add(new SingleParameter("wolfSpeed", Datatypes.DOUBLE,
defaultWolfSpeed, "no additional information", this.id()
.toUpperCase()));
list.add(new SingleParameter("numberOfDistanceSensors",
Datatypes.INTEGER, defaultNumberOfDistanceSensors,
"no additional information", this.id().toUpperCase()));
list.add(new SingleParameter(
"chooseSheepInterval",
Datatypes.LONG,
defaultChooseSheepInterval,
"interval after which a new sheep is chosen by the wolf (in ms)",
this.id().toUpperCase()));
return list;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public CoEvolutionEnv[] generateRunnables(ParCollection params) {
random = new Random(params.getSeed());
CoEvolutionEnv[] env = new CoEvolutionEnv[1];
env[0] = new CoEvolutionEnv(0, params, random);
/*
* ids 0...9 are reserved for ObstacleAgents even ids beginning from 10
* are reserved for SheepAgents (so, first possible id==10) uneven ids
* beginning from 10 are reserved for WolfAgents (so, first possible
* id==11)
*/
env[0].addCollidingAgent((obstacleAgents[0] = new ObstacleAgent(0,
env[0], params)), new Vector2D(250, -5), 0);
env[0].addCollidingAgent((obstacleAgents[1] = new ObstacleAgent(1,
env[0], params)), new Vector2D(250, 515), 0);
env[0].addCollidingAgent((obstacleAgents[2] = new ObstacleAgent(2,
env[0], params)), new Vector2D(5, 255), 90);
env[0].addCollidingAgent((obstacleAgents[0] = new ObstacleAgent(3,
env[0], params)), new Vector2D(495, 255), 90);
sheepAgents = new SheepAgent[params.getParValueInt("numberOfSheep")];
double sheepSpeed = (params.getParValueDouble("sheepSpeed"));
for (int i = 0; i < sheepAgents.length; i++) {
sheepAgents[i] = new SheepAgent(10 + 2 * i, env[0], sheepSpeed,
params.getParValueInt("numberOfDistanceSensors"), random,
0, params);
/*
* Area for deserialisation of existing NeuroBrains.
*/
// if(i==0){
// ObjectInputStream ois;
// SparseNet neuralNet = null;
// NeuroBrain neuroBrain=null;
// try {
// ois = new ObjectInputStream(new FileInputStream (new
// File("agent#" + 10 + ".jos")));
// neuralNet = (SparseNet)ois.readObject();
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// }
// neuroBrain=new NeuroBrain(sheepAgents[i], random);
// neuroBrain.net = neuralNet;
// sheepAgents[i].implantBrain(neuroBrain);
// }
//
//
// else{
// sheepAgents[i].implantBrain(new NeuroBrain(sheepAgents[i],
// random));
// }
sheepAgents[i].getBrainControlledAgent().implantBrain(
new NeuroBrainChristian(sheepAgents[i], random));
boolean sheepAgentCollides = true;
Vector2D position = null;
double angle = 0.0;
do {
try {
position = getRandomVector2D(random);
angle = random.nextDouble() * 360;
if (!env[0].collides(sheepAgents[i], position, angle, null)) {
sheepAgentCollides = false;
}
} catch (Exception e) {
sheepAgentCollides = false;
}
} while (sheepAgentCollides);
env[0].addCollidingAgent(sheepAgents[i], position, angle);
}
wolfAgents = new WolfAgent[params.getParValueInt("numberOfWolf")];
double wolfSpeed = params.getParValueDouble("wolfSpeed");
double detectionRadius = params
.getParValueDouble("wolfDetectionRadius");
double catchRadius = params.getParValueDouble("wolfCatchRadius");
for (int i = 0; i < wolfAgents.length; i++) {
wolfAgents[i] = new WolfAgent(10 + 2 * i + 1, env[0], wolfSpeed,
defaultNumberOfWolfDistanceSensors, detectionRadius,
catchRadius, random, params);
boolean wolfAgentCollides = true;
Vector2D position = null;
double angle = 0.0;
do {
position = getRandomVector2D(random);
angle = random.nextDouble() * 360;
if (!env[0].collides(wolfAgents[i], position, angle, null)) {
wolfAgentCollides = false;
}
} while (wolfAgentCollides);
env[0].addCollidingAgent(wolfAgents[i], position, angle);
}
return env;
}
@Override
public String id() {
return eas.simulation.ConstantsSimulation.DEFAULT_MASTER_SCHEDULER_ID
+ "-co";
}
public Vector2D getRandomVector2D(Random random) {
Vector2D result = null;
result = new Vector2D(24 + random.nextDouble() * (476 - 34), 14
+ random.nextDouble() * (496 - 24));
return result;
}
}