/*
* File name: JasmineRobotWithoutMutation.java (package eas.simulation.users.lukas.marbImplicitEvolution.gateWithoutMutation)
* Author(s): Lukas König
* Java version: 6.0
* Generation date: 01.04.2011 (11:22:03)
*
* (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.lukas.marbImplicitEvolution.movingFitnessNoMutation;
import java.awt.Color;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import eas.math.MiscMath;
import eas.math.geometry.Vector2D;
import eas.math.matrix.Matrix;
import eas.miscellaneous.StaticMethods;
import eas.miscellaneous.datatypes.Integer2D;
import eas.simulation.Wink;
import eas.simulation.spatial.sim2D.standardAgents.jasmine.JasmineRobot;
import eas.simulation.spatial.sim2D.standardEnvironments.AbstractEnvironment2D;
import eas.simulation.spatial.sim2D.standardEnvironments.AbstractEnvironment2DFast;
import eas.startSetup.ParCollection;
import eas.startSetup.SingleParameter;
import eas.startSetup.parameterDatatypes.Datatypes;
import eas.users.lukas.marbImplicitEvolution.simpleMazeEnvironment.MARBFastEnvironment;
/**
* @author Lukas König
*
*/
public class JasmineRobotWithoutMutationMatrixSelection extends JasmineRobot {
/**
*
*/
private static final long serialVersionUID = -4163074651031707203L;
private boolean good;
private String schlechtGen;
private long selectionInterval;
private Random rand;
private ParCollection pars;
private Matrix wkeitsVerteilungSel;
/**
* @return Returns the good.
*/
public boolean isGood() {
return this.good;
}
public JasmineRobotWithoutMutationMatrixSelection(
final int id,
final AbstractEnvironment2DFast<?> env,
final Random random,
final ParCollection params,
final boolean lightSensors,
final double unfallWinkel,
@SuppressWarnings("unused") final String gutesGenom,
final String schlechtesGenom,
final Matrix wkeitsVerteilungSelektion) {
super(id, env, random, params, lightSensors, unfallWinkel);
schlechtGen = schlechtesGenom;
this.good = false;
this.selectionInterval = params.getParValueLong("SelectionInterval");
this.datNamGood = params.getParValueString("DateinameGood");
this.datNamStat = params.getParValueString("DateinameStats");
rand = new Random(params.getSeed());
this.pars = params;
this.wkeitsVerteilungSel = wkeitsVerteilungSelektion;
}
@Override
public Color getAgentColor() {
if (good) {
return Color.green;
} else {
return Color.red;
}
}
@Override
protected void fitness(Wink simTime) {
// Fitness Entfernung.
if (super.fitnessEntfernung) {
if (simTime.getLastTick() % this.selectionInterval == this.selectionInterval - 1) {
@SuppressWarnings("rawtypes")
Vector2D currentPos = ((AbstractEnvironment2D) this
.getEnvironment()).getAgentPosition(this.id());
if (super.lastPos == null) {
this.lastPos = new Vector2D(currentPos);
}
fitness = this.lastPos.distance(currentPos);
this.lastPos = currentPos;
}
}
}
@Override
protected void pfuschFitnessEntfernung(Wink simTime) {
}
@Override
protected void mutation(Wink simTime) {
}
@Override
public List<SingleParameter> getParameters() {
List<SingleParameter> paramList = super.getParameters();
paramList.add(new SingleParameter("SelectionInterval", Datatypes.LONG, 100l, "Anzahl Zyklen zwischen Selektion", "JASMINE_ROBOT-WITHOUT-MUTATION"));
paramList.add(new SingleParameter("DateinameGood", Datatypes.STRING, "GoodOnes.txt", "", "JASMINE_ROBOT-WITHOUT-MUTATION"));
paramList.add(new SingleParameter("DateinameStats", Datatypes.STRING, "Statistiken.txt", "", "JASMINE_ROBOT-WITHOUT-MUTATION"));
return paramList;
}
List<JasmineRobot> jasmineSet;
private int numberGood(HashSet<JasmineRobot> robots) {
int good = 0;
for (JasmineRobot r : robots) {
JasmineRobotWithoutMutationMatrixSelection realRobot = (JasmineRobotWithoutMutationMatrixSelection) r;
if (realRobot.isGood()) {
good++;
}
}
return good;
}
private LinkedList<JasmineRobotWithoutMutationMatrixSelection> goods(Collection<JasmineRobot> robots) {
LinkedList<JasmineRobotWithoutMutationMatrixSelection> goods = new LinkedList<JasmineRobotWithoutMutationMatrixSelection>();
for (JasmineRobot r : robots) {
JasmineRobotWithoutMutationMatrixSelection realRob = (JasmineRobotWithoutMutationMatrixSelection) r;
if (realRob.isGood()) {
goods.add(realRob);
}
}
return goods;
}
private LinkedList<JasmineRobotWithoutMutationMatrixSelection> bads(Collection<JasmineRobot> robots) {
LinkedList<JasmineRobotWithoutMutationMatrixSelection> bads = new LinkedList<JasmineRobotWithoutMutationMatrixSelection>();
for (JasmineRobot r : robots) {
JasmineRobotWithoutMutationMatrixSelection realRob = (JasmineRobotWithoutMutationMatrixSelection) r;
if (!realRob.isGood()) {
bads.add(realRob);
}
}
return bads;
}
private ArrayList<String> speicherEintraege = new ArrayList<String>();
private String datNamStat;
private ArrayList<String> goodOnes = new ArrayList<String>();
private String datNamGood;
/**
* Note that this selection is NOT decentralized. It is implemented in
* this class to avoid creating a new environment class, but it is not the
* intuitive way of implementing this.
* @return
*/
@Override
protected void selection(Wink simTime) {
if (jasmineSet == null) {
jasmineSet = new LinkedList<JasmineRobot>();
jasmineSet.addAll(((MARBFastEnvironment) this.getEnvironment()).getJasmineAgents());
}
int someJasmineID = jasmineSet.get(0).id();
if (this.id() == someJasmineID && simTime.getLastTick() % this.selectionInterval == this.selectionInterval - 1) {
LinkedList<JasmineRobot> turnier = new LinkedList<JasmineRobot>();
JasmineRobotWithoutMutationMatrixSelection realRob;
HashSet<JasmineRobot> allRobots = ((MARBFastEnvironment) this.getEnvironment()).getJasmineAgents();
int anzahlGd = this.numberGood(allRobots);
LinkedList<JasmineRobotWithoutMutationMatrixSelection> goods = goods(allRobots);
LinkedList<JasmineRobotWithoutMutationMatrixSelection> bads = bads(allRobots);
if (anzahlGd == 0 || anzahlGd == ((MARBFastEnvironment) this.getEnvironment()).getJasmineAgents().size()) {
StaticMethods.speichereTextAusArray(pars.getStdDirectory(), datNamGood, goodOnes, pars);
StaticMethods.speichereTextAusArray(pars.getStdDirectory(), datNamStat, this.speicherEintraege, pars);
this.getEnvironment().getSimTime().timeTerminate();
return;
}
// Turnier zusammenstellen.
// System.out.println();
// System.out.println(goods.size());
// System.out.println(bads.size());
Matrix richtigeZeile = wkeitsVerteilungSel.erzeugeZeilenMatrix(new int[] {goods.size()});
// System.out.println(richtigeZeile);
LinkedList<Double> verteilung1 = new LinkedList<Double>();
LinkedList<Integer2D> tupel = new LinkedList<Integer2D>();
Integer2D ausgewaehlt;
for (int i = 0; i < richtigeZeile.getColumnCount(); i++) {
verteilung1.add(richtigeZeile.get(i, 0));
tupel.add(richtigeZeile.getColumnTitle(i));
}
ausgewaehlt = (Integer2D) MiscMath.randVerteilungDoubleAngenaehert(
tupel,
verteilung1,
rand);
// System.out.println(verteilung1);
// System.out.println(tupel);
// System.out.println("Ausgewählt: " + ausgewaehlt);
int randRobotNum;
for (int i = 0; i < ausgewaehlt.gute; i++) {
randRobotNum = rand.nextInt(goods.size());
turnier.add(goods.get(randRobotNum));
goods.remove(randRobotNum);
}
for (int i = 0; i < ausgewaehlt.schlechte; i++) {
randRobotNum = rand.nextInt(bads.size());
turnier.add(bads.get(randRobotNum));
bads.remove(randRobotNum);
}
// System.out.println(turnier);
// Auswählen aus Turnier.
LinkedList<Double> verteilung2 = new LinkedList<Double>();
for (JasmineRobot rob : turnier) {
verteilung2.add(rob.getEvaluation());
}
JasmineRobot selected = (JasmineRobot) MiscMath.randVerteilungDoubleAngenaehert(turnier, verteilung2, rand);
// System.out.println(selected);
String selectedGenome = selected.getBrain().getMarb().erzeugeStringSeq();
for (JasmineRobot rob : turnier) {
realRob = (JasmineRobotWithoutMutationMatrixSelection) rob;
realRob.getBrain().getMarb().erzeugeAusStdSequenz(selectedGenome);
realRob.myGenome = selectedGenome;
}
// Speichern.
int anzahlGood = this.numberGood(((MARBFastEnvironment) this.getEnvironment()).getJasmineAgents());
String eintrag = "";
eintrag += anzahlGood;
eintrag += "\t" + turnier;
eintrag += "\t" + selected;
speicherEintraege.add(eintrag);
goodOnes.add("" + anzahlGood);
return;
}
return;
}
// Bei Veränderung des Brains auf null setzen!
private String myGenome = null;
@Override
public void step(Wink simTime) {
super.step(simTime);
if (myGenome == null) {
myGenome = this.getBrain().getMarb().erzeugeStringSeq();
}
if (schlechtGen.equals(myGenome)) {
this.good = false;
} else {
this.good = true;
}
}
@Override
public String toString() {
String isGood;
if (this.isGood()) {
isGood = "G(" + this.id() + ")";
} else {
isGood = "B(" + this.id() + ")";
}
return isGood + ":" + this.getEvaluation();
}
public static void main(String[] args) {
// Gleichverteilt.
// createMatlabMatrix(new int[] { 0, 52, 157, 207, 232, 280, 300, 320,
// 349, 358, 368, 376, 375, 384, 383, 390, 398, 394, 394, 399,
// 396, 399, 398, 400, 397, 400, 400, 400, 400, 400, 400 }, 400);
// Gut, kubisch.
// createMatlabMatrix(new int[] { 0, 26, 73, 107, 141, 143, 162, 165, 170,
// 183, 189, 194, 193, 195, 196, 196, 198, 196, 200, 197, 199,
// 196, 200, 200, 200, 200, 200, 200, 200, 200, 200 }, 200);
// System.exit(0);
// Sondermatrix (Erzeugen aus tatsächlichen Daten).
String path = "C:/Eig/Forschung/Paper_Vortraege/Paper_13_ECTA11/Experimente/NoMutation/simWithoutMutationRegularSelection";
String[] fileList = new File(path).list();
int id;
int numGoodsBeginning;
int numAnalyzed = 0;
// num*End[i] refers to the * agents at all runs with i good agents in the beginning.
int[] numGoodsEnd = new int[31];
int[] numBadsEnd = new int[31];
// Independent values.
numGoodsEnd[0] = 0;
numGoodsEnd[30] = 400;
numBadsEnd[0] = 400;
numBadsEnd[30] = 0;
System.out.println("Starting analysis of " + fileList.length + " potential files.");
for (String file : fileList) {
if (file.startsWith("GoodOnes") && file.endsWith(".txt")) {
id = Integer.parseInt(file.replace("GoodOnes", "").replace(".txt", ""));
boolean endedGood;
// System.out.println(file);
numGoodsBeginning = 1 + id / 1000;
List<String> data = StaticMethods.liesTextArray(path, file, null);
if (data.size() > 0) {
endedGood = Integer.parseInt(data.get(data.size() - 1)) >= 15;
} else {
endedGood = numGoodsBeginning >= 15;
System.out.print("No data: ");
}
System.out.println(
"ID "
+ id
+ " (Good: "
+ numGoodsBeginning
+ ") - "
+ endedGood);
if (endedGood) {
numGoodsEnd[numGoodsBeginning]++;
} else {
numBadsEnd[numGoodsBeginning]++;
}
numAnalyzed++;
}
}
System.out.println("\nAnalysis completed for " + numAnalyzed + " files.");
System.out.print("Goods Win: ");
for (int i = 0; i < numGoodsEnd.length; i++) {
System.out.print(numGoodsEnd[i] + "\t");
}
System.out.print("\nBads Win: ");
for (int i = 0; i < numBadsEnd.length; i++) {
System.out.print(numBadsEnd[i] + "\t");
}
createMatlabMatrix(numGoodsEnd, numBadsEnd[0]);
}
/**
* @param numGoodsEnd
* @param length
*/
private static void createMatlabMatrix(
int[] numGoodsEnd,
int length) {
System.out.println("\n\nMatlab-Matrix:\n");
System.out.print("[");
for (int i = 0; i < length; i++) {
String komma0 = "";
if (i < length - 1) {
komma0 = ";";
}
System.out.print("[");
for (int j = 0; j < numGoodsEnd.length; j++) {
String komma = "";
if (j < numGoodsEnd.length - 1) {
komma = ",";
}
if (numGoodsEnd[j] > 0) {
System.out.print("1" + komma);
numGoodsEnd[j]--;
} else {
System.out.print("0" + komma);
}
}
System.out.print("]" + komma0);
}
System.out.println("]");
}
}