Package eas.simulation.brain.neural.neuroSimulation

Source Code of eas.simulation.brain.neural.neuroSimulation.NeuroAgent

/*
* File name:        NeuroAgent.java (package eas.simulation.brain.neural.neuroSimulation)
* Author(s):        lko
* Java version:     7.0
* Generation date:  19.04.2013 (10:44:53)
*
* (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.simulation.brain.neural.neuroSimulation;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import eas.math.geometry.Polygon2D;
import eas.math.geometry.Rectangle2D;
import eas.math.geometry.Vector2D;
import eas.simulation.Wink;
import eas.simulation.agent.GenericSensor;
import eas.simulation.brain.neural.Neuron;
import eas.simulation.spatial.sim2D.standardAgents.AbstractAgent2D;
import eas.startSetup.ParCollection;

/**
* @author lko
*/
public class NeuroAgent extends AbstractAgent2D<NeuroEnvironment> {

    private static final long serialVersionUID = 1323809833697266179L;
    private Neuron neuron;
    private int width = 100, height = 100;
    private transient BufferedImage neuroImage;
    private boolean wasVisible = false;
   
    public void setVisualizationSize(int width, int height) {
        this.width = width;
        this.height = height;
    }
   
    public NeuroAgent(int id, NeuroEnvironment env, ParCollection params, Neuron neuron) {
        super(id, env, params);
        this.neuron = neuron;
        this.neuron.setPaintBlueBorder(false);
       
        this.addSensor(new GenericSensor<Double, NeuroEnvironment, NeuroAgent>() {
            /**
             *
             */
            private static final long serialVersionUID = -1069270495922554567L;
            @Override public Double sense(NeuroEnvironment env, NeuroAgent agent) {return NeuroAgent.this.neuron.getNetOutput();}
            @Override public String id() {return "Net output";}
        });
        this.addSensor(new GenericSensor<Double, NeuroEnvironment, NeuroAgent>() {
            /**
             *
             */
            private static final long serialVersionUID = -967938299928316055L;

            @Override
            public Double sense(NeuroEnvironment env, NeuroAgent agent) {
                try {
                    return NeuroAgent.this.neuron.getInput();
                } catch (Exception e) {
                    return Double.NaN;
                }
            }
           
            @Override public String id() {return "Input";}
        });
        this.addSensor(new GenericSensor<Double, NeuroEnvironment, NeuroAgent>() {
            /**
             *
             */
            private static final long serialVersionUID = -3401543628285647303L;

            @Override
            public Double sense(NeuroEnvironment env, NeuroAgent agent) {
                try {
                    return NeuroAgent.this.neuron.getOutput();
                } catch (Exception e) {
                    return Double.NaN;
                }
            }
           
            @Override public String id() {return "Output";}
        });
    }

    private void refreshNeuroImage(int width, int height) {
        this.wasVisible = true;
        this.neuroImage = this.neuron.generateNeuronView(width, height);
    }
   
    @Override
    public BufferedImage getInsideView() {
        BufferedImage img = super.getInsideView();
        BufferedImage img2 = new BufferedImage(250, 500, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = img2.createGraphics();
       
        g.setColor(Color.white);
        g.fillRect(0, 0, img2.getWidth(), img2.getHeight());
        g.drawImage(img, 0, 0, null);
        g.drawImage(this.neuron.generateNeuronView(200, 200), 0, 250, null);
       
        return img2;
    }
   
    @Override
    public BufferedImage getOutsideView() {
        return this.neuroImage;
    }
   
    private boolean isVisible(Rectangle2D rect) {
        return rect.intersect(new Rectangle2D(new Vector2D(0, 0), new Vector2D(1600, 1280)))
                || new Rectangle2D(new Vector2D(0, 0), new Vector2D(1600, 1280)).isPointInside(rect.middle());
    }
   
    @Override
    public void step(Wink simTime) {
        super.step(simTime);
        Polygon2D pol = this.getEnvironment().getAgentShapeInVisualization(this.id());
        int width = (int) pol.getBoundingBox().getWidth();
        int height = (int) pol.getBoundingBox().getHeight();
        width = Math.min(width, 1000);
        height = Math.min(height, 1000);

        this.wasVisible = false;
        if (this.isVisible(pol.getBoundingBox())) {
            if  (this.width != width || this.height != height || !this.wasVisible) {
                this.refreshNeuroImage(width, height);
            }
        }
       
        this.width = width;
        this.height = height;
    }
}
TOP

Related Classes of eas.simulation.brain.neural.neuroSimulation.NeuroAgent

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.