Package eas.users.lukas.neuroCEGPM

Source Code of eas.users.lukas.neuroCEGPM.NeuralMaster

/*
* File name:        NeuralMaster.java (package eas.users.lukas.neuroCEGPM)
* Author(s):        Lukas König
* Java version:     7.0
* Generation date:  28.02.2014 (13:26:44)
*
* (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.neuroCEGPM;

import java.awt.Color;
import java.awt.Rectangle;
import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import eas.math.MiscMath;
import eas.miscellaneous.StaticMethods;
import eas.miscellaneous.useful.strokes.ShapeStroke;
import eas.plugins.masterScheduler.AbstractDefaultMaster;
import eas.plugins.standard.visualization.chartPlugin.ChartEvent;
import eas.simulation.ConstantsSimulation;
import eas.simulation.Wink;
import eas.startSetup.ParCollection;
import eas.users.lukas.neuroCEGPM.selection.Selection;
import eas.users.lukas.neuroCEGPM.selection.SelectionTournament;
import eas.users.lukas.neuroCEGPM.simpleNeural.NeuralNetwork;

/**
* @author Lukas König
*/
public class NeuralMaster extends AbstractDefaultMaster<EnvironmentNeural> {

    private static final long serialVersionUID = -7370208199754791387L;

    @Override
    public EnvironmentNeural[] generateRunnables(ParCollection params) {
        Random rand = new Random(params.getSeed());
       
        EnvironmentNeural env = new EnvironmentNeural(
                rand,
                0,
                params,
                "NeuralEnv");

        return new EnvironmentNeural[] {env};
    }

    @Override
    public String id() {
        return ConstantsSimulation.DEFAULT_MASTER_SCHEDULER_ID + "-Neural-CEGPM";
    }
   
    @Override
    public void runBeforeSimulation(EnvironmentNeural env, ParCollection params) {
        super.runBeforeSimulation(env, params);
        sel = new SelectionTournament(env.getRand());
        LinkedList<Thread> threads = new LinkedList<Thread>();
        LinkedList<RobNeural> robs = new LinkedList<RobNeural>();
       
        for (int i = 0; i < 50; i++) {
            RobNeural rob = new RobNeural(100, 100, 10, 20, 20, env, i, params, env.getRand(), false);
            robs.add(rob);
            List<BigDecimal> genome = NeuralNetwork.randSeq(1200, env.getRand());
            @SuppressWarnings("unused")
            List<BigDecimal> genomeTrans = NeuralNetwork.randSeq(300, env.getRand()); // TODO: Hier musst du ansetzen!
            threads.add(rob.setController(genome, null, env.getRand(), true));
        }

        StaticMethods.joinThreads(threads);
        params.logInfo(robs.size() + " robots initialized.");
       
        robs.forEach(r -> env.hinzuRobotRand(r));
    }
   
    private Selection sel;
    private final int movAvgWindowSize = 50;
   
    @Override
    public synchronized void runDuringSimulation(EnvironmentNeural env,
            Wink simZyk, ParCollection params) {
        super.runDuringSimulation(env, simZyk, params);
       
        if (simZyk.getLastTick() % 200 == 199) {
            sel.select(env.getAgents());
        }
       
        if (simZyk.getLastTick() % 200 == 199) {
            int memory = 0;
            int gates = 0;
           
            for (RobNeural r : env.getAgents()) {
                memory += r.getMemoryCount();
                r.resetMemoryCount();
                gates += r.getGatePassings();
                r.resetGatePassings();
            }
           
            double fitVal = env.getFitnessSum() * env.getFitnessSum() / 30000 * 12 / env.getAgents().size();
            double collisions = env.getAnzUnfaelle() * 10;
            double gatePassings = gates * 100.0;
           
            Double movAvgFit = MiscMath.movingAverage("Fitness", fitVal, this.movAvgWindowSize);
            Double movAvgColl = MiscMath.movingAverage("Collisions", collisions, this.movAvgWindowSize);
            Double movAvgGate = MiscMath.movingAverage("Gates", gatePassings, this.movAvgWindowSize);
           
            if (movAvgFit == null) {
                movAvgFit = 0.0;
                movAvgColl = 0.0;
                movAvgGate = 0.0;
            }
           
            ChartEvent eventMovAvgFit = new ChartEvent("Evolution", "Fitness MovAvg", movAvgFit);
            eventMovAvgFit.setLineStroke(new ShapeStroke(new Rectangle(3, 3), 1));
            eventMovAvgFit.setLineColor(Color.red);
            env.getSimTime().broadcastEvent(eventMovAvgFit);
       
            ChartEvent eventMovAvgColl = new ChartEvent("Evolution", "Coll MovAvg", movAvgColl);
            eventMovAvgColl.setLineStroke(new ShapeStroke(new Rectangle(3, 3), 1));
            eventMovAvgColl.setLineColor(Color.blue);
            env.getSimTime().broadcastEvent(eventMovAvgColl);
       
            ChartEvent eventMovAvgGate = new ChartEvent("Evolution", "Gate MovAvg", movAvgGate);
            eventMovAvgGate.setLineStroke(new ShapeStroke(new Rectangle(3, 3), 1));
            eventMovAvgGate.setLineColor(Color.black);
            env.getSimTime().broadcastEvent(eventMovAvgGate);
           
            params.logInfo("Simulation cycle: " + simZyk);
            ChartEvent event0 = new ChartEvent("Evolution", "FitnessSum (quad)", fitVal);
            event0.setLineStroke(new ShapeStroke(new Rectangle(1, 1), 1));
            env.getSimTime().broadcastEvent(event0);
            ChartEvent event1 = new ChartEvent("Evolution", "Collisions * 10", collisions);
            event1.setLineStroke(new ShapeStroke(new Rectangle(1, 1), 1));
            env.getSimTime().broadcastEvent(event1);
            ChartEvent event2 = new ChartEvent("Evolution", "Mem-Genome * 100", (memory) * 100.0);
            event2.setLineStroke(new ShapeStroke(new Rectangle(1, 1), 1));
            env.getSimTime().broadcastEvent(event2);
            ChartEvent event3 = new ChartEvent("Evolution", "GP * 100", gatePassings);
            event3.setLineColor(Color.black);
            env.getSimTime().broadcastEvent(event3);

//            List<RobNeural> agents = env.getAgents();
//            Collections.sort(agents, (r1, r2) -> -new Double(r1.getGlobalBestFitness()).compareTo(r2.getGlobalBestFitness()));
//            double globalBestFit = agents.get(0).getGlobalBestFitness() * agents.get(0).getGlobalBestFitness() / 30000 * 150 / env.getAgents().size();
//            ChartEvent event3 = new ChartEvent("Evolution", "AlltimeMax", globalBestFit);
//            event3.setLineStroke(new TextStroke(" " + Math.round(agents.get(0).getGlobalBestFitness()), new Font("", 1, 7), true, true));
//            event3.setLineColor(Color.black);
//            event3.setDrawSmoothSplinesInLineCharts(false);
//            env.getSimTime().broadcastEvent(event3);
//
//            double globalWorstFit = agents.get(agents.size() - 1).getGlobalBestFitness() * agents.get(agents.size() - 1).getGlobalBestFitness() / 30000 * 150 / env.getAgents().size();
//            ChartEvent event4 = new ChartEvent("Evolution", "AlltimeMin", globalWorstFit);
//            event4.setLineStroke(new TextStroke(" " + Math.round(agents.get(agents.size() - 1).getGlobalBestFitness()), new Font("", 1, 7), true, true));
//            event4.setLineColor(Color.blue);
//            env.getSimTime().broadcastEvent(event4);
        }
    }
   
    @Override
    public boolean isTerminationRequested(EnvironmentNeural runnable,
            Wink currentTime, ParCollection params) {
        return false;
    }
}
TOP

Related Classes of eas.users.lukas.neuroCEGPM.NeuralMaster

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.