Package eas.plugins.standard.eaPlugin

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

/*
* Dateiname:    EndlAut.java
* Autor(en):    Lukas König
* Java-Version: 6.0
* Erstellt:     01.05.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.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import javax.imageio.ImageIO;

import eas.miscellaneous.StaticMethods;
import eas.miscellaneous.system.FileNamePostfixFilter;
import eas.plugins.Plugin;
import eas.plugins.standard.eaPlugin.evolvableTranslators.EvolutionPluginCompletelyEvolvable;
import eas.plugins.standard.eaPlugin.xmlRecording.XMLAufnahmePlugin;
import eas.simulation.Wink;
import eas.simulation.agent.GenericSensor;
import eas.simulation.event.EASEvent;
import eas.simulation.spatial.sim2D.marbSimulation.EnvironmentEA;
import eas.simulation.spatial.sim2D.marbSimulation.OpsFactory;
import eas.simulation.spatial.sim2D.marbSimulation.RobEA;
import eas.simulation.spatial.sim2D.marbSimulation.endlAutomat.VisMantel;
import eas.simulation.spatial.sim2D.marbSimulation.fitness.FitnessNull;
import eas.simulation.spatial.sim2D.marbSimulation.mutation.CondMutVerfahren;
import eas.startSetup.ParCollection;
import eas.startSetup.SingleParameter;
import eas.startSetup.parameterDatatypes.ArrayListString;
import eas.startSetup.parameterDatatypes.Datatypes;

/**
* @author Lukas König
*/
public class EAPlugin extends StandardPluginForEA {

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

    /**
     * Der Name des Umgebungsbitmapattributs.
     */
    public static final String UMGEBUNG_ATTR = "umgebung";

    /**
     * Der Name der Umgebungsbitmap.
     */
    public static final String UMGEBUNG_DATNAME = "umgebung.bmp";
   
    /**
     * Das Attribut für die Art der Fitnessberechnung.
     */
    public static final String FIT_VERFAHREN_AT = "fitnessberechnung";

    /**
     * Der Name des Fitnessatributs.
     */
    public static final String FIT_ZYK_ATTR = "fitZyk";

    /**
     * Der Name des Fitnessreduktionsattributs.
     */
    public static final String FIT_EVAP_ZYK_ATTR = "fitRedZyk";

    /**
     * Der Name des Fitnessreduktionswertattributs.
     */
    public static final String FIT_EVAP_VAL_ATTR = "fitRedWert";

    /**
     * Name des Attributs für den Fitnessabzug im Fall eines Unfalls.
     */
    public static final String UNFALL_ABZUG = "unfall";

    /**
     * Zufallsgenerator.
     */
    private Random rand;
   
    /**
     * Parametersatz.
     */
    private ParCollection pars;
   
    /**
     * Liste der Plugins.
     */
    private LinkedList<Plugin<?>> plugins;

    /**
     * Liste von Robotern (im VisMantel), die verwaltet werden.
     */
    private LinkedList<VisMantel> graphen;

    @Override
    @SuppressWarnings("serial")
    public List<SingleParameter> getParameters() {
        ArrayList<SingleParameter> liste = new ArrayList<SingleParameter>(10);

        liste.add(new SingleParameter(
                EAPlugin.UMGEBUNG_ATTR,
                Datatypes.STRING,
                EAPlugin.UMGEBUNG_DATNAME,
                "Der Dateiname, aus dem die Umgebung gelesen wird.",
                this.id().toUpperCase()) {
                    @Override
                    public BufferedImage getImage(final ParCollection params) {
                        BufferedImage img = null;
                        try {
                            img = ImageIO.read(new File(
                                    params.getStdDirectory()
                                        + File.separator
                                        + this.getParValue()));
                        } catch (IOException e) {
                            img = new BufferedImage(
                                    400,
                                    100,
                                    BufferedImage.TYPE_INT_RGB);
                            Graphics2D g = img.createGraphics();
                            g.setColor(Color.red);
                            g.fillRect(0, 0, 400, 100);
                            g.setColor(Color.black);
                            g.drawString("Field bitmap not found: "
                                    + params.getStdDirectory()
                                    + File.separator
                                    + this.getParValue(), 50, 50);
                        }
                       
                        return img;
                    }
        });

        ArrayListString fitList = new ArrayListString(1);
        fitList.add(new FitnessNull().id());
        liste.add(new SingleParameter(
                EAPlugin.FIT_VERFAHREN_AT,
                Datatypes.STRING_ARR,
                fitList,
                "Array der Fitnessverfahren (1 / Automat; siehe Klasse "
                + "OpsFactory): "
                + "die einzelnen Fitnessverfahren "
                + "müssen durch Kommas ohne Leerzeichen voneinander getrennt "
                + "werden. ACHTUNG: mindestens ein Verfahren erforderlich.",
                this.id().toUpperCase()));

        liste.add(new SingleParameter(
                EAPlugin.FIT_ZYK_ATTR,
                Datatypes.LONG,
                50l,
                "Das Intervall für Fitness-Snapshots.",
                this.id().toUpperCase()));

        liste.add(new SingleParameter(
                EAPlugin.FIT_EVAP_ZYK_ATTR,
                Datatypes.LONG,
                300l,
                "Das Intervall für Fitness-Evaporation.",
                this.id().toUpperCase()));

        liste.add(new SingleParameter(
                EAPlugin.FIT_EVAP_VAL_ATTR,
                Datatypes.DOUBLE,
                2.0,
                "Der Divisionswert für die Fitness-Evaporation.",
                this.id().toUpperCase()));

        liste.add(new SingleParameter(
                EAPlugin.UNFALL_ABZUG,
                Datatypes.DOUBLE,
                3.0,
                "Der Fitness-Abzug bei Kollisionen.",
                this.id().toUpperCase()));

        liste.add(new SingleParameter(
                "UseOnlyStandardDecoding?",
                Datatypes.BOOLEAN,
                new Boolean(false),
                "Ob nur die Standardkodierung verwendet werden soll. "
                    + "Der Translator wird dabei NICHT zum Übersetzen "
                    + "verwendet.",
                this.id().toUpperCase()));

        liste.add(new SingleParameter(
                "showEALabels?",
                Datatypes.BOOLEAN,
                true,
                "Ob die Labels (Knoten / Kanten) in der grafischen Darstellung eines MARBs angezeigt werden.",
                this.id().toUpperCase()));

        liste.add(new SingleParameter(
                "UseTranslatorWITHCompletingTransitions",
                Datatypes.BOOLEAN,
                true,
                "Selects the translator mode (with - WC or without - WOC completing transitions)",
                this.id().toUpperCase()));
       
        return liste;
    }

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

    @Override
    public List<String> getRequiredPlugins() {
        return null;
    }

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

    /**
     * Konstruktor.
     */
    public EAPlugin() {
        this.graphen = new LinkedList<VisMantel>();
    }
   
    @Override
    public void runAfterSimulation(
            final EnvironmentEA env,
            final ParCollection params) {
        EnvironmentEA umg = env;

        if (umg.existsPlugin(new XMLAufnahmePlugin().id())) {
            umg.setSimEndeFlag();
        }
       
            Iterator<VisMantel> it = this.graphen.iterator();
            while (it.hasNext()) {
                it.next().getRob().simBeendet();
            }
    }

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

        this.pars = params;
        this.rand = new Random(this.pars.getSeed());
        this.plugins = new LinkedList<Plugin<?>>();
        LinkedList<Plugin<?>> plugs = umg.getPlugins();
        if (plugs != null) {
            for (Plugin<?> p : plugs) {
                plugins.add(p);
            }
        }

        this.ladeGraphen(umg);
        this.platziereRoboter(umg);

        StaticMethods.log(
                StaticMethods.LOG_INFO,
                "Roboter in Simulation: "
                    + umg.getRobAgents().size(),
                this.pars);
    }

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

    }

    /**
     * @param params  Der Parametersatz.
     * @return  Die Anzahl an Robotern, die vom Plugin geladen werden.
     */
    public int anzRob(final ParCollection params) {
        File verz = new File(params.getStdDirectory());
        String endung = eas.startSetup.marbBuilder.ConstantsGraphVis.GRAPH_ENDUNG;
        String[] gespGr = verz.list(new FileNamePostfixFilter(endung));
        if (gespGr == null) {
            return 0;
        } else {
            return gespGr.length;
        }
    }
   
    /**
     * Lädt gespeicherte Graphen aus dem im Parameterobjekt angegebenen
     * Verzeichnis.
     */
    private void ladeGraphen(final EnvironmentEA umg) {
        File verz = new File(this.pars.getStdDirectory());
        String endung = eas.startSetup.marbBuilder.ConstantsGraphVis.GRAPH_ENDUNG;
        String[] gespGr = verz.list(new FileNamePostfixFilter(endung));
        String grNam;
        VisMantel visRob = null;
        boolean sel = false;
       
        if (gespGr != null) {
            for (int i = 0; i < gespGr.length; i++) {
                grNam = gespGr[i].substring(
                                0,
                                gespGr[i].length() - endung.length() - 1);
               
//                if (this.pars.getGraphisch().booleanValue()) {
//                    for (int j = 0; j < this.visObserver.length; j++) {
//                        if (this.visObserver[j].getGraphName().equals(grNam)) {
//                            obs = this.visObserver[j];
//                            sel = this.visObserver[j].isVisible();
//                            break;
//                        }
//                    }
//                }

                CondMutVerfahren mutVerh = null;
                CondMutVerfahren mutTrans = null;
                if (umg.existsPlugin(new EvolutionPluginCompletelyEvolvable().id())) {
                    mutVerh = OpsFactory.getKonstMut(
                            this.pars.getParValueString(EvolutionPluginCompletelyEvolvable.MUT_ART_VE),
                            this.rand,
                            this.pars);
                    mutTrans = OpsFactory.getKonstMut(
                            this.pars.getParValueString(EvolutionPluginCompletelyEvolvable.MUT_ART_TR),
                            this.rand,
                            this.pars);
                }
               
                visRob = new VisMantel(grNam,
                        umg,
                        mutVerh,
                        mutTrans,
                        this.rand,
                        this.kleinsteFreieID(),
                        this.pars,
                        sel,
                        this.plugins);

                this.graphen.add(visRob);
            }
        }
    }
   
    /**
     * Platziert die Roboter.
     *
     * @param umg  Die zu den Robotern gehörende Umgebung.
     */
    private void platziereRoboter(final EnvironmentEA umg) {
        Iterator<VisMantel> it;
        RobEA rob;

        it = this.graphen.iterator();
        while (it.hasNext()) {
            rob = it.next().getRob();
            rob.zuruecksetzen();

            umg.hinzuRobotRand(rob);
        }
    }

    /**
     * Speichert alle Graphen in der Liste <code>this.graphen</code>.
     */
    public void speichereAlleGraphen() {
        Iterator<VisMantel> it = this.graphen.iterator();
        VisMantel aktGr;

        while (it.hasNext()) {
            aktGr = it.next();
            aktGr.speichereGraph();
        }
    }

    /**
     * Gibt die kleinste noch nicht von einem der aktuellen Graphen verwendete
     * ID zurück.
     *
     * @return  Die kleinste noch nicht verwendete ID.
     */
    public int kleinsteFreieID() {
        int kleinste = 0;
        Iterator<VisMantel> it = this.graphen.iterator();
        VisMantel aktGr;
       
        while (it.hasNext()) {
            aktGr = it.next();
            if (kleinste <= aktGr.getRob().id()) {
                kleinste = aktGr.getRob().id() + 1;
            }
        }
       
        return kleinste;
    }

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

    @Override
    public void onSimulationResumed(EnvironmentEA env, Wink resumeTime,
            ParCollection params) {
       
    }
}
TOP

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

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.