Package eas.users.lukas.marbImplicitEvolution.movingFitnessNoMutation

Source Code of eas.users.lukas.marbImplicitEvolution.movingFitnessNoMutation.JasmineRobotWithoutMutationRegularSelection

/*
* 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.HashSet;
import java.util.List;
import java.util.Random;

import eas.math.matrix.Matrix;
import eas.miscellaneous.StaticMethods;
import eas.miscellaneous.datatypes.Integer2D;
import eas.miscellaneous.system.FileNamePrefixFilter;
import eas.simulation.Wink;
import eas.simulation.spatial.sim2D.standardAgents.jasmine.JasmineRobot;
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 JasmineRobotWithoutMutationRegularSelection extends JasmineRobot {

    /**
     *
     */
    private static final long serialVersionUID = 100318223517393216L;

    private boolean good;
   
    private String schlechtGen;
   
    private ParCollection pars;
   
    /**
     * @return Returns the good.
     */
    public boolean isGood() {
        if (myGenome == null) {
            myGenome = this.getBrain().getMarb().erzeugeStringSeq();
        }

        if (schlechtGen.equals(myGenome)) {
            this.good = false;
        } else {
            this.good = true;
        }

        return this.good;
    }

    public JasmineRobotWithoutMutationRegularSelection(
            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) {
        super(id, env, random, params, lightSensors, unfallWinkel);
        schlechtGen = schlechtesGenom;
        this.good = false;
        this.datNamGood = params.getParValueString("DateinameGood");
        this.datNamMatrix = params.getParValueString("DateinameMatrix");
        this.pars = params;
    }
   
    @Override
    public Color getAgentColor() {
        if (good) {
            return Color.green;
        } else {
            return Color.red;
        }
    }
   
    @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("DateinameGood", Datatypes.STRING, "GoodOnes.txt", "", "JASMINE_ROBOT-WITHOUT-MUTATION-REG-SEL"));
        paramList.add(new SingleParameter("DateinameMatrix", Datatypes.STRING, "SelektionsMatrix.txt", "", "JASMINE_ROBOT-WITHOUT-MUTATION-REG-SEL"));
       
        return paramList;
    }
   
    List<JasmineRobot> jasmineSet;
   
    private int numberGood(HashSet<JasmineRobot> robots) {
        int good = 0;
       
        for (JasmineRobot r : robots) {
            JasmineRobotWithoutMutationRegularSelection realRobot = (JasmineRobotWithoutMutationRegularSelection) r;
           
            if (realRobot.isGood()) {
                good++;
            }
        }
       
        return good;
    }

    private static Matrix speicherMatrix;
    private String datNamMatrix;
    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 (simTime.getLastTick() == 0) {
            int tourGr = this.tournamentGroesse;
            this.tournamentGroesse = 1000000;
            super.selection(simTime);
            this.tournamentGroesse = tourGr;
        }
       
//        if (simTime.getLastTick() < 50) {
//            this.myGenome = null;
//            return;
//        }
       
        selCounter++;
        if (selCounter < 50) {
            return;
        }
       
        int n = ((MARBFastEnvironment) this.getEnvironment()).getJasmineAgents().size();
        super.selection(simTime);
        int anzahlGdPop = this.numberGoodsInPopBeforeLastSelection;
        int anzahlGdTurnier = this.numberGoodsInLastTournament;
      
        int k = this.tournamentGroesse;
        if (speicherMatrix == null) {
            speicherMatrix = new Matrix(k + 1, n + 1);
            for (int i = 0; i < speicherMatrix.getColumnCount(); i++) {
                speicherMatrix.setColumnTitle(i, new Integer2D(i, k - i));
            }
            for (int j = 0; j < speicherMatrix.getRowCount(); j++) {
                speicherMatrix.setRowTitle(j, new Integer2D(j, n - j));
            }
            speicherMatrix.set(0, 0, 1);
            speicherMatrix.set(speicherMatrix.getColumnCount() - 1, speicherMatrix.getRowCount() - 1, 1);
        }
       
        if (this.selectionTookPlace) {
            this.myGenome = null;

            speicherMatrix.set(anzahlGdTurnier, anzahlGdPop,
                    speicherMatrix.get(anzahlGdTurnier, anzahlGdPop) + 1);
        }

        if (simTime.getLastTick() % 1000 == 999 || anzahlGdPop == 0 || anzahlGdPop == ((MARBFastEnvironment) this.getEnvironment()).getJasmineAgents().size()) {
            StaticMethods.speichereTextAusArray(pars.getStdDirectory(), datNamGood, goodOnes, pars);
            if (speicherMatrix != null) {
                StaticMethods.speichereTextDatei(pars.getStdDirectory(), datNamMatrix, speicherMatrix.toString(), pars);
            }
           
            if (anzahlGdPop == 0 || anzahlGdPop == ((MARBFastEnvironment) this.getEnvironment()).getJasmineAgents().size()) {
                this.getEnvironment().getSimTime().timeTerminate();
                return;
            }
        }
       
        // Speichern.
        int anzahlGood = this.numberGood(((MARBFastEnvironment) this.getEnvironment()).getJasmineAgents());
        goodOnes.add("" + anzahlGood);
       
        return;
    }
   
    // Wichtig: Bei Veränderung des Brains auf null setzen!
    private String myGenome = null;
   
    @Override
    public void step(Wink simTime) {
        super.step(simTime);
    }
   
    @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) {
        File path = new File("C:/Eig/Forschung/Paper_Vortraege/Paper_13_ECTA11/Experimente/NoMutation/simWithoutMutationRegularSelection");
        File[] files = path.listFiles(new FileNamePrefixFilter("SelektionsMatrix"));
        Matrix gesamtMatrix = null;
        Matrix zwischenMatrix = null;
        String storeName = "C:/Eig/Forschung/Paper_Vortraege/Paper_13_ECTA11/Experimente/NoMutation/simWithoutMutationRegularSelection/gesamtMatrix.txt";
       
        System.out.println("Analyse von " + files.length + " Dateien gestartet.");
       
        for (int i = 0; i < files.length; i++) {
            System.out.println("Bearbeite Datei " + files[i]);
            if (i == 0) {
                gesamtMatrix = new Matrix(files[i]);
            } else {
                zwischenMatrix = new Matrix(files[i]);
                gesamtMatrix.add(zwischenMatrix);
            }
            if (i % 100 == 99) {
                System.out.println();
                System.out.println(gesamtMatrix);
                System.out.println();
            }
        }

        Matrix.normalizeRowwise(gesamtMatrix);
       
        gesamtMatrix.storeMatrix(new File(
                storeName),
                null);
       
        System.out.println("Gespeichert unter: " + new File(storeName));
        System.out.println("Analyse von " + files.length + " Dateien beendet.");
    }
}
TOP

Related Classes of eas.users.lukas.marbImplicitEvolution.movingFitnessNoMutation.JasmineRobotWithoutMutationRegularSelection

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.