Package eas.plugins.standard.eaPlugin

Source Code of eas.plugins.standard.eaPlugin.Zufallsgenom

/*
* Datei:            Zufallsgenom.java
* Autor(en):        Lukas König
* Java-Version:     6.0
* Erstellt:         22.04.2010
*
* (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.plugins.standard.eaPlugin;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import eas.miscellaneous.StaticMethods;
import eas.plugins.standard.eaPlugin.evolvableTranslators.EvolutionPluginCompletelyEvolvable;
import eas.simulation.Wink;
import eas.simulation.agent.AbstractAgent;
import eas.simulation.agent.GenericSensor;
import eas.simulation.event.EASEvent;
import eas.simulation.spatial.sim2D.marbSimulation.EnvironmentEA;
import eas.simulation.spatial.sim2D.marbSimulation.RobEA;
import eas.startSetup.ParCollection;
import eas.startSetup.SingleParameter;
import eas.startSetup.parameterDatatypes.Datatypes;

/**
* Plugin zur Generierung von Zufallsgenomen zu Beginn der Simulation
* für alle geladenen Roboter.
*
* @author Lukas König
*/
public class Zufallsgenom extends StandardPluginForEA {
   
    /**
     *
     */
    private static final long serialVersionUID = -3800274395399001174L;
   
    /**
     * Der Zufallsgenerator.
     */
    private Random rand;
   
    @Override
    public List<SingleParameter> getParameters() {
        LinkedList<SingleParameter> params = new LinkedList<SingleParameter>();
       
        params.add(new SingleParameter(
                "mingenomlaenge",
                Datatypes.INTEGER,
                0,
                "Die minimale Genomlänge der initialen Zufallsgenome.",
                this.id().toUpperCase()));
        params.add(new SingleParameter(
                "maxgenomlaenge",
                Datatypes.INTEGER,
                0,
                "Die maximale Genomlänge der initialen Zufallsgenome.",
                this.id().toUpperCase()));
       
        return params;
    }

    @Override
    public List<GenericSensor<?, ?, ?>> getGenericSensors() {
        return null;
    }

    @Override
    public String id() {
        return "zufallsgenom";
    }

    /**
     * Erzeugt eine Zufallssequenz.
     *
     * @param laenge  Die Länge der Sequenz.
     *
     * @return  Die Zufallssequenz.
     */
    private LinkedList<Integer> zufallsSequenz(final int laenge) {
        LinkedList<Integer> sequenz = new LinkedList<Integer>();
        final int maxNum = 255;
        double gauss;
       
        for (int i = 0; i < laenge; i++) {
            gauss = Math.min(Math.abs(this.rand.nextGaussian() * 10), maxNum);
            sequenz.add((int) gauss);
        }

        return sequenz;
    }

    /**
     * Setzt die Verhaltensgenome auf zufällige Sequenzen anzugebender Länge.
     *
     * @param laenge  Die minimale Länge der Genome.
     * @param laenge  Die maximale Länge der Genome.
     */
    @SuppressWarnings("unchecked")
    private void setzeVerhGenZuf(
            final int minlaenge,
            final int maxlaenge,
            final EnvironmentEA umg) {
        LinkedList<Integer>[] sequenzen;
        int laenge;
       
        for (AbstractAgent<?> r2 : umg.getAgents()) {
            RobEA r = (RobEA) r2;
            sequenzen = new LinkedList[r.getVAut().length];
           
            for (int i = 0; i < r.getVAut().length; i++) {
                laenge = this.rand.nextInt(maxlaenge - minlaenge + 1)
                        + minlaenge;
                sequenzen[i] = this.zufallsSequenz(laenge);
            }
           
            r.setVerhCodes(sequenzen);
            r.erzeugeAusSequenzen(
                    StaticMethods.stringAusListSeqs(sequenzen),
                    r.getConds(),
                    r.getTranslatoren(),
                    false);
        }
    }

    @Override
    public void runBeforeSimulation(
            final EnvironmentEA env,
            final ParCollection params) {
        EnvironmentEA umg = env;

        int minLen = (Integer) params.getParValue("mingenomlaenge");
        int maxLen = (Integer) params.getParValue("maxgenomlaenge");

        this.rand = new Random(params.getSeed());
       
        if (minLen >= 0 && maxLen >= minLen) {
            this.setzeVerhGenZuf(minLen, maxLen, umg);
        }

        StaticMethods.log(
                StaticMethods.LOG_INFO,
                "Verhaltensgenome mit Zufallsgenomen zwischen " + minLen
                    + " und " + maxLen + " Länge überschrieben.",
                params);
    }

    @Override
    public void runDuringSimulation(
            final EnvironmentEA umg,
            final Wink simZyk,
            final ParCollection params) {

    }

    @Override
    public void runAfterSimulation(
            final EnvironmentEA umg,
            final ParCollection params) {

    }
   
    /**
     * Über diese Methode können Abhängigkeiten zwischen Plugins definiert
     * werden. Die hier zurückgegebene Liste sollte die IDs aller Plugins
     * enthalten, die von diesem Plugin benötigt werden und ohne die kein
     * Start der Simulation möglich sein soll.
     *
     * @return Die Liste benötigter Plugins. Diese Liste kann <code>null</code>
     *         sein, wenn keine abhängigkeiten definiert werden sollen.
     */
    @Override
    public List<String> getRequiredPlugins() {
        LinkedList<String> list = new LinkedList<String>();
        list.add(new EAPlugin().id());
        list.add(new EvolutionPluginCompletelyEvolvable().id());
        return list;
    }

    @Override
    public void handleEvent(
            final EASEvent e,
            final EnvironmentEA env,
            final Wink lastTick,
            final ParCollection params) {
       
    }
}
TOP

Related Classes of eas.plugins.standard.eaPlugin.Zufallsgenom

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.