Package eas.users.students.dominikColling.rocket

Source Code of eas.users.students.dominikColling.rocket.SaveLoadPlugin

/*
* File name:        SaveLoadPlugin2.java (package eas.simulation.users.students.dominikColling.rocket)
* Author(s):        Dominik
* Java version:     6.0
* Generation date:  03.06.2011 (00:20:00)
*
* (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.students.dominikColling.rocket;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import eas.miscellaneous.StaticMethods;
import eas.plugins.AbstractDefaultPlugin;
import eas.simulation.Wink;
import eas.simulation.event.EASEvent;
import eas.startSetup.ParCollection;
import eas.startSetup.SingleParameter;
import eas.users.students.dominikColling.rocket.brain.DomPhysSparseNet;
import eas.users.students.dominikColling.rocket.brain.DominikEncoding1;

/**
* @author Dominik
*
*/
public class SaveLoadPlugin extends AbstractDefaultPlugin<DomPhysEnv> {

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

    int saveLoad; // 0 nichts 1 Speichern 2 Lesen
 
  /** Name des Verzeichnisses, in dem gespeichert werden soll */
  String verzeichnis;
  /** Dateiname fuer das Speichern der besten Netze */
  //String listeNetze;
  /** Dateiname fuer das Speichern der Fitnesswerte und des Diversitaetsmasses */
  String listeFitness;
  /** Dateiname fuer das Speichern der Netzte, die zum Ende der Simulation noch leben */
  String letztePop;
  /** Zahl fuer das Speichern mehrerer Laeufe ueber JoSchKa */
  String zahl;
  /** Dateiart */
  String dateiEndung = ".txt";
 
  DominikEncoding1 encoding = new DominikEncoding1();
 
  /** die besten Netzte jedes Zyklus werden gespeichert */
  DomPhysNetPopulation netPop;
  /** die Netzte, die zum Ende der Simulation noch leben */
  DomPhysNetPopulation netPop2;
  /** die Durchschnitte der Fitnesswerte in jedem Zyklus werden gespeichert */
  List<Double> durchschnittFitness;
 
  List<Integer> bestFitness;
 
  // Listen fuer Mittelwerte und Varianzen
  List<Double> mittelw1;
  List<Double> varw1;
  List<Double> mittelw2;
  List<Double> varw2;
  List<Double> mittelw3;
  List<Double> varw3;
 
  double antrieb;
  double drehung;
  int popSize;
  double abstand;
  double gravitationsKonstante;
  long rand;
 
  /** Zyklus, in dem gespeichert wird */
  double speichernNach = 100;

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

  @Override
  public List<SingleParameter> getParameters() {
    return null;
  }

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

  @Override
  public void runBeforeSimulation(DomPhysEnv env, ParCollection params) {
   
    zahl = env.getZahl();
   
    //listeNetze =env.getTabelleNetze() + zahl + dateiEndung;
    listeFitness = env.getDurchschnittFitness() + zahl + dateiEndung;
    letztePop = env.getLetztePop() + zahl + dateiEndung;
    verzeichnis = env.getVerzeichnis();
   
   

    saveLoad = env.getSaveLoad();

    if(saveLoad == 0 || saveLoad == 1) {
      netPop = new DomPhysNetPopulation(Integer.MAX_VALUE, env.getRand());
      netPop2 = env.getNetPop();
      durchschnittFitness = new ArrayList<Double>();
      bestFitness = new ArrayList<Integer>();
     
      mittelw1 = new ArrayList<Double>();
      varw1 = new ArrayList<Double>();
      mittelw2 = new ArrayList<Double>();
      varw2 = new ArrayList<Double>();
      mittelw3 = new ArrayList<Double>();
      varw3 = new ArrayList<Double>();

      // Für speichern
      DomPhysAgent agent = (DomPhysAgent) env.getAgent(1);
      antrieb = agent.getMaxActuatorWerte()[0];
      drehung = agent.getMaxActuatorWerte()[1];
      popSize = netPop2.getMaxSize();
      abstand = env.getAbstand();
      gravitationsKonstante = env.getGravitationskonstante();
      rand = params.getSeed();
    }

    if(saveLoad == 2) {
     
      LinkedList<String> array = StaticMethods.liesTextArray(verzeichnis, letztePop, params);
      env.setNetPop(this.loadNetPop(array, env));
    }
  }

  @Override
  public void runAfterSimulation(DomPhysEnv env, ParCollection params) {
    if (saveLoad == 1) {

      netPop2 = env.getNetPop();
      System.out.println("NETPOP SIZE: " + netPop.getSize());

      for (int i = 0; i < netPop2.getSize(); i++) {
        netPop.add(netPop2.getNet(i), netPop2.getFitness(i), netPop2.getMinAbstand(i), netPop2.getMaxAbstand(i), netPop2.getGeburt(i));
        //System.out.println("SIZE: " + netPop.getSize());
      }

      //String [] array = this.saveNetPop(netPop);
      //StaticMethods.speichereTextAusArray(verzeichnis, listeNetze, array , params);

      String [] array2 = this.saveNetPopEnd(netPop2);
      StaticMethods.speichereTextAusArray(verzeichnis, letztePop, array2 , params);
     
      String [] array3 = this.saveFitnessDiv(netPop2, true, params);
      StaticMethods.speichereTextAusArray(verzeichnis, listeFitness, array3 , params);
    }
  }

  @Override
  public void runDuringSimulation(DomPhysEnv env, Wink simZyk, ParCollection params) {
    if (saveLoad == 1 && (simZyk.getCurrentTime() % 100) == 0) {
     
      System.out.println("TIME: "+ simZyk.getCurrentTime());

      netPop2 = env.getNetPop();
     
      DomPhysSparseNet hirn = netPop2.getBestesNetz();
      int index = netPop2.getIndexBestesNetz();
      double groesseBestes = hirn.getDivZahlen().get(0);
     
      netPop.add(netPop2.getNet(index), netPop2.getFitness(index), netPop2.getMinAbstand(index), netPop2.getMaxAbstand(index), netPop2.getGeburt(index));
     
     
      for (int i = 0; i < netPop2.getSize(); i++) {
        if (netPop2.getNet(i).getDivZahlen().get(0) != groesseBestes) {
          netPop.add(netPop2.getNet(i), netPop2.getFitness(i), netPop2.getMinAbstand(i), netPop2.getMaxAbstand(i), netPop2.getGeburt(i));
        }
      }
     

      //String [] array = this.saveNetPop(netPop);
      //StaticMethods.speichereTextAusArray(verzeichnis, listeNetze, array , params);
     
      String [] array3 = this.saveFitnessDiv(netPop2, false, params);
      StaticMethods.speichereTextAusArray(verzeichnis, listeFitness, array3 , params);
    }
  }

  @Override
  public void handleEvent(EASEvent e, DomPhysEnv env, Wink lastTimeStep,
      ParCollection params) {
        // TODO Comment.
  }

  public DomPhysNetPopulation getNetPop () {
    return this.netPop;
  }
 
  public DomPhysNetPopulation loadNetPop (LinkedList<String> array, DomPhysEnv env) {
    netPop = env.getNetPop();
    String liste = "";
   
    for (int i = 0; i < array.size(); i++){
      liste = liste + array.get(i) + "\n";
      //System.out.println(" Array: " + array.get(i));
    }

    String[] liste4 = liste.split("ZXY");
    String[] liste2 = liste4[1].split("#");
    for (int i = 0; i < liste2.length - 1; i++) {
      String[] liste3 = liste2[i].split("/");
      //DomPhysSparseNet net = new DominikEncoding1().decode(liste2[i]);
      String fitness = liste3[0].replace("Fitness: ", "");
      //System.out.println(" 3/1: " + liste3[1]);
      String id = liste3[1].replace("ID: ", "");

      //System.out.println(id);
      double fitness2 = Double.valueOf(fitness);
      double id2 = Double.valueOf(id);
     
      DomPhysSparseNet net = new DomPhysSparseNet( (int)id2 , 0, liste3[3], encoding);
      //System.out.println(net.generateGenome(new DominikEncoding1())+ " Fitness: " + fitness2 + " ID: " + id2);
      //System.out.println(net.generateGenome(new DominikEncoding1())+ " Fitness: " + fitness2 + " ID: " + id2);
      double minAbstand = Double.MAX_VALUE;
      double maxAbstand = Double.MIN_VALUE;
      //System.out.println(netPop.getSize());
      netPop.add(net, (int) fitness2, minAbstand, maxAbstand, 0);
    }
    return netPop;   
  }
 
 
  public String[] saveNetPop (DomPhysNetPopulation netPop) {

    String [] array = new String[netPop.getSize() + 1];

    array[0] = "NetPopMaxSize: "+ popSize + ", " +
    "Abstand"+ abstand + ", " +
    "Maximaler Antrieb: " + antrieb + ", " +
    "Maximale Drehung: " + drehung + ", " +
    "Gravitationskonstante: "+ gravitationsKonstante + ", " +
    "Seed: " + rand + " ZXY\n";

    for (int i = 0; i < array.length - 1; i++) {
      DomPhysSparseNet net = netPop.getNet(i);
      int fitness = netPop.getFitness(i);
      double minAbstand = netPop.getMinAbstand(i);
      double maxAbstand = netPop.getMaxAbstand(i);
      int geburt = netPop.getGeburt(i);
      int generation = net.getGeneration();
      int id = net.getId();
      int vaterId = net.getVaterId();

      array [i + 1] ="Fitness: " + fitness + "/ " +
      "ID: " + id + "/ " +
      "Geburt: " + geburt + ", " +
      "VaterID: " + vaterId + ", " +
      "Generation: " + generation + ", " +
      "Minimaler Abstand: " + minAbstand + ", " +
      "Maximaler Abstand: " + maxAbstand + "/\n" +
      net.generateGenome(encoding);

    }
    return array;
  }
 
  public String[] saveNetPopEnd (DomPhysNetPopulation netPop2) {
    String [] array = new String[netPop2.getSize() + 1];

    array[0] = "NetPopMaxSize: "+ popSize + ", " +
    "Abstand: "+ abstand + ", " +
    "Maximaler Antrieb: " + antrieb + ", " +
    "Maximale Drehung: " + drehung + ", " +
    "Gravitationskonstante: "+ gravitationsKonstante + ", " +
    "Seed: " + rand + " ZXY\n";

    for (int i = 0; i < array.length - 1; i++) {
      DomPhysSparseNet net = netPop2.getNet(i);
      int fitness = netPop2.getFitness(i);
      double minAbstand = netPop2.getMinAbstand(i);
      double maxAbstand = netPop2.getMaxAbstand(i);
      int geburt = netPop2.getGeburt(i);
      int generation = net.getGeneration();
      int id = net.getId();
      int vaterId = net.getVaterId();

      array [i + 1] ="Fitness: " + fitness + "/ " +
      "ID: " + id + "/ " +
      "Geburt: " + geburt + ", " +
      "VaterID: " + vaterId + ", " +
      "Generation: " + generation + ", " +
      "Minimaler Abstand: " + minAbstand + ", " +
      "Maximaler Abstand: " + maxAbstand + ", " +
      "Gravitationskonstante"+ gravitationsKonstante + "/\n" +
      net.generateGenome(encoding);
    }
    return array;
  }
 
  public String[] saveFitnessDiv (DomPhysNetPopulation netPop2, boolean end, ParCollection params) {
    double[] divers1 = new double[netPop2.getSize()];
    double[] divers2 = new double[netPop2.getSize()];
    double[] divers3 = new double[netPop2.getSize()];
   
    double mittel1 = 0;
    double mittel2 = 0;
    double mittel3 = 0;
   
    for (int i = 0; i < netPop2.getSize(); i++) {
      List<Double> divZahlen = netPop2.getNet(i).getDivZahlen();
      divers1[i] = divZahlen.get(0);
      divers2[i] = divZahlen.get(1);
      divers3[i] = divZahlen.get(2);
     
      mittel1 = mittel1 + divZahlen.get(0);
      mittel2 = mittel2 + divZahlen.get(1);
      mittel3 = mittel3 + divZahlen.get(2);
    }
   
    mittel1 = mittel1 / netPop2.getSize();
    mittel2 = mittel2 / netPop2.getSize();
    mittel3 = mittel3 / netPop2.getSize();
   
    double var1 = 0;
    double var2 = 0;
    double var3 = 0;
   
    for (int i = 0; i < netPop2.getSize(); i++) {
      List<Double> divZahlen = netPop2.getNet(i).getDivZahlen();
      var1 = var1 + (mittel1 - divZahlen.get(0)) * (mittel1 - divZahlen.get(0)) / netPop.getSize();
      var2 = var2 + (mittel1 - divZahlen.get(1)) * (mittel1 - divZahlen.get(1)) / netPop.getSize();
      var3 = var3 + (mittel1 - divZahlen.get(2)) * (mittel1 - divZahlen.get(2)) / netPop.getSize();
    }
   
    mittelw1.add(mittel1);
    varw1.add(var1);
    mittelw2.add(mittel2);
    varw2.add(var2);
    mittelw3.add(mittel3);
    varw3.add(var3);
   
    //Fitness-Durchschnitt
    double durchschnitt = 0;

    for (int k = 0; k < netPop2.getSize(); k++) {
      durchschnitt = durchschnitt + netPop2.getFitness(k);
    }

    durchschnitt = durchschnitt / netPop2.getSize();

    durchschnittFitness.add (durchschnitt);
    bestFitness.add(netPop2.getFitness(netPop2.getIndexBestesNetz()));

    String [] array = new String[durchschnittFitness.size() + 1];

    array[0] = "DURCHSCHNITTFITNESS: \n" + "NetPopMaxSize: "+ popSize + ", " +
    "Abstand"+ abstand + ", " +
    "Maximaler Antrieb: " + antrieb + ", " +
    "Maximale Drehung: " + drehung + ", " +
    "Gravitationskonstante: "+ gravitationsKonstante + ", " +
    "Seed: " + rand + " ZXY\n";

    for (int j = 0; j < array.length - 1; j++) {
      array [j + 1] = "DurchschnittFitness: " + durchschnittFitness.get(j)+ " BestFitness: "+ bestFitness.get(j) + " Diversitaet (MW/Var) - " + mittelw1.get(j) +":" + varw1.get(j) + " / " + mittelw2.get(j)+":" + varw2.get(j) + " / "+ mittelw3.get(j)+ ":" + varw3.get(j) + " Tick: "+ speichernNach * j;
     
    }
   
    if (end) {
      array [array.length - 1] = "Fitness: " + durchschnittFitness.get(array.length - 2) + " BestFitness: "+ bestFitness.get(array.length - 2) +" Diversitaet (MW/Var) - " + mittelw1.get(array.length - 2)+":" + varw1.get(array.length - 2) + " / "+ mittelw2.get(array.length - 2)+":" + varw2.get(array.length - 2) + " / "+ mittelw3.get(array.length - 2)+ ":" + varw3.get(array.length - 2) + " Tick: "+ params.getParValueLong("simulationlength");
    }
   
    return array;
  }

    /* (non-Javadoc)
     * @see eas.plugins.Plugin#getSupportedPlugins()
     */
    @Override
    public List<String> getSupportedPlugins() {
        // TODO Auto-generated method stub
        return null;
    }

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

Related Classes of eas.users.students.dominikColling.rocket.SaveLoadPlugin

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.