Package eas.users.lukas.marbImplicitEvolution.simpleMazeEnvironment

Source Code of eas.users.lukas.marbImplicitEvolution.simpleMazeEnvironment.MARBFastEnvironment

/*
* File name:        MARBFastEnvironment.java (package eas.simulation.users.lukas.marbImplicitEvolution)
* Author(s):        Lukas König
* Java version:     6.0
* Generation date:  28.02.2011 (10:35:05)
*
* (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.simpleMazeEnvironment;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.HashSet;
import java.util.List;

import eas.math.geometry.Polygon2D;
import eas.math.geometry.Rectangle2D;
import eas.math.geometry.Vector2D;
import eas.simulation.agent.AbstractAgent;
import eas.simulation.agent.Evolvable;
import eas.simulation.spatial.sim2D.standardAgents.AbstractAgent2D;
import eas.simulation.spatial.sim2D.standardAgents.jasmine.JasmineRobot;
import eas.simulation.spatial.sim2D.standardEnvironments.AbstractEnvironment2DFast;
import eas.simulation.spatial.sim2D.standardScenes.Scene2D;
import eas.startSetup.ParCollection;
import eas.startSetup.SingleParameter;
import eas.startSetup.parameterDatatypes.Datatypes;

/**
* @author Lukas König
*
*/
public class MARBFastEnvironment extends AbstractEnvironment2DFast<AbstractAgent2D<?>> {

    /**
     *
     */
    private static final long serialVersionUID = -2445757701635014704L;

    /**
     * @param ident
     * @param params
     * @param normal  Bei Angabe von null wird keine Szene eingefüt, sonst
     *                entweder SimpleMazeScene (normal) oder SimpleMazeScene2
     *                (!normal).
     */
    public MARBFastEnvironment(int ident, ParCollection params, final Boolean normal) {
        super(ident, params);
        Scene2D<AbstractAgent2D<?>> s;
       
        if (normal != null) {
            if (normal) {
                s = new SimpleMazeScene("Simple Maze Scene", params);
            } else {
                s = new SimpleMazeScene2("Simple Maze Scene2", params);
            }
           
            this.addScene(s);
        }
       
        pars = params;
    }

    ParCollection pars;
   
    @Override
    public List<SingleParameter> getParameters() {
        List<SingleParameter> list = super.getParameters();
        list.add(new SingleParameter("ShowSelectionSquare", Datatypes.BOOLEAN, false));
        return list;
    }
   
    @Override
    public synchronized BufferedImage getOutsideView() {
        BufferedImage img = super.getOutsideView();

        if (pars.getParValueBoolean("ShowSelectionSquare")) {
            Graphics2D g = img.createGraphics();
            double width = pars
                    .getParValueDouble("SelektionsQuadratKantenlaenge");
            Rectangle2D rect;
            Polygon2D pol;

            g.setColor(Color.LIGHT_GRAY);
            for (AbstractAgent<?> a : this.getAgents()) {
                if (JasmineRobot.isJasmineRobot(a)) {
                    rect = new Rectangle2D(
                            new Vector2D(this.getAgentPosition(a.id()).x
                                    - width / 2,
                                    this.getAgentPosition(a.id()).y - width / 2),
                            new Vector2D(this.getAgentPosition(a.id()).x
                                    + width / 2,
                                    this.getAgentPosition(a.id()).y + width / 2));
                    pol = rect.toPol2D();
                    pol = this.getPolygonInVisualization(pol);
                    g.drawPolygon(pol.toPol());
                }
            }
        }

        return img;
    }

    public HashSet<JasmineRobot> getJasmineAgents() {
        HashSet<JasmineRobot> set = new HashSet<JasmineRobot>();
       
        for (AbstractAgent2D<?> agent : this.getAgents()) {
            if (JasmineRobot.class.isAssignableFrom(agent.getClass())) {
                set.add((JasmineRobot) agent);
            }
        }
       
        return set;
    }
   
    public double getFitnessSum() {
        double fitness = 0;
       
        for (AbstractAgent<?> a : this.getAgents()) {
            try {
                fitness += ((Evolvable<?>) a).getEvaluation();
            } catch (Exception e) {
            }
        }
       
        return fitness;
    }
}
TOP

Related Classes of eas.users.lukas.marbImplicitEvolution.simpleMazeEnvironment.MARBFastEnvironment

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.