Package eas.users.students.fabian.diplomarbeit.EvolvableBoxAgents3D.Agents

Examples of eas.users.students.fabian.diplomarbeit.EvolvableBoxAgents3D.Agents.EvolvableBoxAgent3DGenome


          return null;
      }
     
      int maxNumOfBoxes = params.getParValueInt("maxNumberOfBoxesPerAgent");
     
    EvolvableBoxAgent3DGenome genome = null;
    LinkedList<EvolvableBoxAgent3DAppendixInfo> appendixInfoList = new LinkedList<EvolvableBoxAgent3DAppendixInfo>();
    EvoBoxSparseNet sparseNet = null;
   
    // Create and save random information about agent/core.
    Vector3f agentSize = new Vector3f();
    agentSize.x = .5f + (float) Math.abs(random.nextGaussian());
    agentSize.y = .5f + (float) Math.abs(random.nextGaussian());
    agentSize.z = .5f + (float) Math.abs(random.nextGaussian());

    // //Add appendix/appendices.
    // Always add at least one appendix to the list.
    int predecessorId = 0; // attached directly to the core
    Vector3f size = new Vector3f();
    size.x = .5f + (float) Math.abs(random.nextGaussian());
    size.y = .5f + (float) Math.abs(random.nextGaussian());
    size.z = .5f + (float) Math.abs(random.nextGaussian());

    Vector3f positionOnPredecessor = new Vector3f();
    positionOnPredecessor.x = (float) random.nextGaussian();
    positionOnPredecessor.y = (float) random.nextGaussian();
    positionOnPredecessor.z = (float) random.nextGaussian();

    // Defines free axis. 0: x, 1: y, 2: z. All others are locked.
   
   
    int freeAxis = random.nextInt(3);
    // BUG IN JBULLET?! Wenn die y-Achse (1) benutzt wird, kommt es zu
    // Sprüngen
    // und Aussetzern.
    if (Math.random() > .5) {
      freeAxis = 0;
    } else {
      freeAxis = 2;
    }
    // Provoke error with freeaxis = 1:
    // freeAxis = 1;

    EvolvableBoxAgent3DAppendixInfo ainfo = new EvolvableBoxAgent3DAppendixInfo(
        1, // 1st appendix
        0, // attached directly to the core
        new Vector3f(size), new Vector3f(positionOnPredecessor),
        freeAxis);

    appendixInfoList.add(ainfo);

    // Randomly generate the maximum allowed number of appendices.
    // 2 + r.nextInt(9) means a total of 10 boxes maximum.
    // r.nextInt(9) returns 0-8 not 0-9!
   
    int totalNumberOfBoxes = 2 + random.nextInt(maxNumOfBoxes-2);
    //totalNumberOfBoxes = 4;
    for (int currentAppendixId = 2; currentAppendixId < totalNumberOfBoxes; currentAppendixId++) {
      // Define predecessor to attach this appendix to.
      predecessorId = random.nextInt(currentAppendixId);

      size = new Vector3f();
      size.x = .5f + (float) Math.abs(random.nextGaussian());
      size.y = .5f + (float) Math.abs(random.nextGaussian());
      size.z = .5f + (float) Math.abs(random.nextGaussian());

      positionOnPredecessor = new Vector3f();
      positionOnPredecessor.x = (float) random.nextGaussian();
      positionOnPredecessor.y = (float) random.nextGaussian();
      positionOnPredecessor.z = (float) random.nextGaussian();

      // Bug, siehe oben.
      // freeAxis = r.nextInt(3);
      if (Math.random() > .5) {
        freeAxis = 0;
      } else {
        freeAxis = 2;
      }

      ainfo = new EvolvableBoxAgent3DAppendixInfo(currentAppendixId,
          predecessorId, new Vector3f(size), new Vector3f(
              positionOnPredecessor), freeAxis);

      appendixInfoList.add(ainfo);
    }
   
    // +4 wegen Quat4f
    //sparseNet = new EvoBoxSparseNet(0, appendixInfoList.size()+4, appendixInfoList.size(), 0, 20);
   
    int additionalNeuronsForSensors = 0;
    if (params.getParValueBoolean("useMotionSensor")) {
      additionalNeuronsForSensors += 3;
    }
    if (params.getParValueBoolean("useOrientationSensor")) {
      additionalNeuronsForSensors += 3;
    }
    sparseNet = new EvoBoxSparseNet(0, appendixInfoList.size()+additionalNeuronsForSensors, appendixInfoList.size(), 0, 100);
   
    //// MUTATE SPARSENET
    for (int i = 0; i < 6; i++) {
      sparseNet.mutate(params, random);
    }

    genome = new EvolvableBoxAgent3DGenome(agentSize);
    genome.setAppendices(appendixInfoList);
    genome.setSparseNet(sparseNet);

    return genome;
  }
View Full Code Here


   * @return The created genome which has been parsed from the String.
   */
  public static EvolvableBoxAgent3DGenome parseGenomeFromString(
      String genomeString) {

    EvolvableBoxAgent3DGenome genome = null;
    EvoBoxSparseNet sparseNet = null;

    LinkedList<EvolvableBoxAgent3DAppendixInfo> appendixInfoList = new LinkedList<EvolvableBoxAgent3DAppendixInfo>();

    try {
      // Use StringTokenizer to get info from genome.
      // Tokens are separated by ':' .
      StringTokenizer mainST = new StringTokenizer(genomeString, ":");
      // First read agent size.
      Vector3f agentSize = getVector3fFromString(mainST.nextToken());

      String currentToken = "";
      // Then read all the appendices infos.
      while (mainST.hasMoreTokens()
          && !((currentToken = mainST.nextToken()).equals("///"))) {
        // System.out.println("currentToken: " + currentToken);
        // Check for '/' because appendices are separated by '/'.
        char separator = currentToken.charAt(0);
        if (separator != '/') {
          return null;
        }

        int appendixId = Integer.parseInt(mainST.nextToken());
        int predecessorId = Integer.parseInt(mainST.nextToken());
        Vector3f positionOnPredecessor = getVector3fFromString(mainST
            .nextToken());
        Vector3f appendixSize = getVector3fFromString(mainST
            .nextToken());
        int freeAxis = Integer.parseInt(mainST.nextToken());

        EvolvableBoxAgent3DAppendixInfo ainfo = new EvolvableBoxAgent3DAppendixInfo(
            appendixId, predecessorId, appendixSize,
            positionOnPredecessor, freeAxis);
        appendixInfoList.add(ainfo);
        /*
         * if (!mainST.hasMoreTokens()) { moreAppendices = false; } /*
         * System.out.println("Successfully read: " + agentSize + ":" +
         * separator + ":" + appendixId + ":" + predecessorId + ":" +
         * positionOnPredecessor + ":" + appendixSize + ":" + freeAxis);
         */
      }

      if (currentToken.equals("///")) {
        // System.out.println ("Here comes the brain.");
        StringBuffer brainSB = new StringBuffer(mainST.nextToken("#"));
        // Delete : on first String position.
        brainSB.deleteCharAt(0);
        // Add # to the end.
        brainSB.append("#");
        String brainString = brainSB.toString();
        // System.out.println (brainString);
        // Parse brain.
        sparseNet = new EvoBoxSparseNet(0, 0, brainString,
            new EvoBoxBrainEncoding());
        // System.out.println(sparseNet.generateGenome(new
        // EvoBoxBrainEncoding()));
      }

      genome = new EvolvableBoxAgent3DGenome(agentSize);
      genome.setAppendices(appendixInfoList);
      genome.setSparseNet(sparseNet);

    } catch (Exception e) {
      System.out
          .println("Trying to convert String to Genome was not successful!\n");
      e.printStackTrace();
View Full Code Here

    double portionSparseNet = params.getParValueDouble("portionSparseNet");
    double portionBody = params.getParValueDouble("portionBody");
    double portionSum = portionSparseNet + portionBody;
   
    EvolvableBoxAgent3DGenome newGenome = null;

    Vector3f newAgentSize = new Vector3f(genomeToBeMutated.getAgentSize());
   
    EvoBoxSparseNet newSparseNet = genomeToBeMutated.getSparseNet().createOffspring(0);

    // Make an exact copy of the old genome's appendices info.
    LinkedList<EvolvableBoxAgent3DAppendixInfo> newAppendixInfoList = new LinkedList<EvolvableBoxAgent3DAppendixInfo>();
    Iterator<EvolvableBoxAgent3DAppendixInfo> iterGenomeToBeMutated = genomeToBeMutated
        .getAppendixInfoList().iterator();
    while (iterGenomeToBeMutated.hasNext()) {
      EvolvableBoxAgent3DAppendixInfo ainfoOld = iterGenomeToBeMutated
          .next();
      EvolvableBoxAgent3DAppendixInfo ainfoNew = new EvolvableBoxAgent3DAppendixInfo(
          ainfoOld.getAppendixId(), ainfoOld.getPredecessorId(),
          ainfoOld.getSize(), ainfoOld.getPositionOnPredecessor(),
          ainfoOld.getFreeAxis());
      newAppendixInfoList.add(ainfoNew);
    }

    //float typeOfMutation = random.nextFloat();
    //if (typeOfMutation < .95) {
    if (random.nextDouble()*portionSum < portionSparseNet) {
      //System.out.println("Mutate the brain!");
      newSparseNet.mutate(params, random);
    }

    else {
      //System.out.println("Mutate the body!");
      // Define type of mutation.
      // 0: change box size, 1: change position on
      // predecessor, 2: change free axis,
      // 3: add new appendix, 4: delete one appendix
      //int pos = neurons.size() - outputs + outputNr;
      int typeOfBodyMutation = random.nextInt(5);
      //System.out.println("Type of mutation: " + typeOfBodyMutation);

      switch (typeOfBodyMutation) {
      // //////////////////////
      // Mutate the box size.
      case 0: {

        int numberOfBoxToChange = random
            .nextInt(newAppendixInfoList.size() + 1);

        // Mutate the core.
        if (numberOfBoxToChange == 0) {
          newAgentSize = mutateVector3fKeepPositive(newAgentSize, random);
        }

        // Mutate one of the appendices.
        else {
          EvolvableBoxAgent3DAppendixInfo ainfoOld = newAppendixInfoList
              .remove(numberOfBoxToChange - 1);
          Vector3f newBoxSize = mutateVector3fKeepPositive(ainfoOld
              .getSize(), random);
          EvolvableBoxAgent3DAppendixInfo ainfoNew = new EvolvableBoxAgent3DAppendixInfo(
              ainfoOld.getAppendixId(),
              ainfoOld.getPredecessorId(), new Vector3f(
                  newBoxSize),
              ainfoOld.getPositionOnPredecessor(),
              ainfoOld.getFreeAxis());
          newAppendixInfoList.add(numberOfBoxToChange - 1, ainfoNew);
        }
        //System.out.println("Box size mutated.");
        break;
      }

        // /////////////
        // Mutate position on predecessor
      case 1: {

        int numberOfAppendixToChange = random.nextInt(newAppendixInfoList
            .size());

        EvolvableBoxAgent3DAppendixInfo ainfoOld = newAppendixInfoList
            .remove(numberOfAppendixToChange);
        Vector3f newPositionOnPredecessor = mutateVector3fAllowNegative(ainfoOld
            .getPositionOnPredecessor(), random);
        EvolvableBoxAgent3DAppendixInfo ainfoNew = new EvolvableBoxAgent3DAppendixInfo(
            ainfoOld.getAppendixId(), ainfoOld.getPredecessorId(),
            ainfoOld.getSize(), new Vector3f(
                newPositionOnPredecessor),
            ainfoOld.getFreeAxis());
        newAppendixInfoList.add(numberOfAppendixToChange, ainfoNew);
        //System.out.println("Position on predecessor mutated.");
        break;
      }

        // /////////////
        // Change freeAxis
      case 2: {
        int numberOfAppendixToChange = random.nextInt(newAppendixInfoList
            .size());

        EvolvableBoxAgent3DAppendixInfo ainfoOld = newAppendixInfoList
            .remove(numberOfAppendixToChange);
        int newFreeAxis = random.nextInt(3);
        int oldFreeAxis = ainfoOld.getFreeAxis();
        // AVOID THE BUGGY y-AXIS!!! FIX THIS LATER!
        while ((newFreeAxis == 1) || newFreeAxis == oldFreeAxis) {
          newFreeAxis = random.nextInt(3);
        }
        EvolvableBoxAgent3DAppendixInfo ainfoNew = new EvolvableBoxAgent3DAppendixInfo(
            ainfoOld.getAppendixId(), ainfoOld.getPredecessorId(),
            ainfoOld.getSize(),
            ainfoOld.getPositionOnPredecessor(), newFreeAxis);
        newAppendixInfoList.add(numberOfAppendixToChange, ainfoNew);
        //System.out.println("FreeAxis mutated");
        break;
      }

        // ///////////////////
        // Add new appendix.
      case 3: {

        // DEFINE UPPER LIMIT
        if (newAppendixInfoList.size() >= 9) {
          //System.out.println("Already 10 boxes, no boxes were added.");
          //break;
        }

        int newAppendixId = newAppendixInfoList.size() + 1;
        int newPredecessorId = random.nextInt(newAppendixInfoList.size());
        Vector3f newSize = new Vector3f();
        newSize.x = .5f + (float) Math.abs(random.nextGaussian());
        newSize.y = .5f + (float) Math.abs(random.nextGaussian());
        newSize.z = .5f + (float) Math.abs(random.nextGaussian());

        Vector3f newPositionOnPredecessor = new Vector3f();
        newPositionOnPredecessor.x = (float) random.nextGaussian();
        newPositionOnPredecessor.y = (float) random.nextGaussian();
        newPositionOnPredecessor.z = (float) random.nextGaussian();

        // Defines free axis. 0: x, 1: y, 2: z. All others are locked.
        int newFreeAxis = random.nextInt(3);
        // BUG IN JBULLET?! Wenn die y-Achse (1) benutzt wird, kommt es
        // zu
        // Sprüngen
        // und Aussetzern. So ein $§&%$§&)$%&§$=!!!!!
        //if (Math.random() > .5) {
          //newFreeAxis = 0;
        //} else {
          //newFreeAxis = 2;
        //}
        EvolvableBoxAgent3DAppendixInfo ainfoNew = new EvolvableBoxAgent3DAppendixInfo(
            newAppendixId, newPredecessorId, newSize,
            newPositionOnPredecessor, newFreeAxis);
        //System.out.println("2222222222222222222appendix list old: " + genomeToBeMutated.getAppendixInfoList().size() + " and new: " + newAppendixInfoList.size());
        newAppendixInfoList.add(ainfoNew);
        //System.out.println("3333333333333appendix list old: " + genomeToBeMutated.getAppendixInfoList().size() + " and new: " + newAppendixInfoList.size());
       
        newSparseNet = newSparseNet.createOffspring(0);
        newSparseNet.insertInputNeuron(newAppendixId-1);
        newSparseNet.insertOutputNeuron(newAppendixId-1);
       
        //System.out.println("Appendix added. newAppendixInfoList.size: " +newAppendixInfoList.size());
        //System.out.println("\n\nDEBUG:" + newSparseNet.generateGenome(new EvoBoxBrainEncoding()) + "\n\n");
        //newSparseNet.dump();
        //System.out.println("\n\n-----------------------\n\n");
        break;
      }

        // ////////////////
        // Remove one appendix
      case 4: {
        if (newAppendixInfoList.size() == 1) {
          //System.out.println("Nothing removed because only 2 boxes are left.");
          break;
        }

        int idOfAppendixToBeRemoved = random.nextInt(genomeToBeMutated
            .getAppendixInfoList().size() - 1) + 1;

        EvolvableBoxAgent3DAppendixInfo appendixToBeRemoved = genomeToBeMutated
            .getAppendixInfoList().get(idOfAppendixToBeRemoved - 1);

        int predecessorIdOfAppendixToBeRemoved = appendixToBeRemoved
            .getPredecessorId();

        newAppendixInfoList.remove(idOfAppendixToBeRemoved - 1);

        for (int currentId = 0; currentId < genomeToBeMutated
            .getAppendixInfoList().size() - 1; currentId++) {
          EvolvableBoxAgent3DAppendixInfo ainfoOld = genomeToBeMutated
              .getAppendixInfoList().get(currentId);

          int newPredecessorId;

          if (ainfoOld.getPredecessorId() == idOfAppendixToBeRemoved) {
            newPredecessorId = predecessorIdOfAppendixToBeRemoved;
          } else {
            newPredecessorId = ainfoOld.getPredecessorId();
          }

          EvolvableBoxAgent3DAppendixInfo ainfoNew = new EvolvableBoxAgent3DAppendixInfo(
              currentId + 1, newPredecessorId,
              ainfoOld.getSize(),
              ainfoOld.getPositionOnPredecessor(),
              ainfoOld.getFreeAxis());
          newAppendixInfoList.remove(currentId);
          newAppendixInfoList.add(currentId, ainfoNew);
        }

        newSparseNet.deleteInputNeuron(idOfAppendixToBeRemoved-1);
        newSparseNet.deleteOutputNeuron(idOfAppendixToBeRemoved-1);
        //System.out.println("Appendix removed");

        break;
      }
      }
    }

    newGenome = new EvolvableBoxAgent3DGenome(newAgentSize);
    newGenome.setAppendices(newAppendixInfoList);
    newGenome.setSparseNet(newSparseNet);
    //System.out.println("Old sparsenet: " + genomeToBeMutated.getSparseNet().createOffspring(0).generateGenome(new EvoBoxBrainEncoding()));
    //System.out.println("New sparsenet: " + newGenome.getSparseNet().generateGenome(new EvoBoxBrainEncoding()));
    return newGenome;
  }
View Full Code Here

    EvolvableBoxAgent3DGenome[] newParents = new EvolvableBoxAgent3DGenome[numberOfNewParents];   
   
    System.out.println("selected parents: ");
    for (int i = 0; i < numberOfNewParents; i++) {
      EvolvableBoxAgent3D bestAgent = Collections.max(oldPopulation);
      EvolvableBoxAgent3DGenome genome = EvolvableBoxAgent3DGenomeHandler.parseGenomeFromString(bestAgent.getGenome().toString());
      oldPopulation.remove(bestAgent);
      newParents[i] = genome;   
      System.out.println(newParents[i]);
    }
   
View Full Code Here

    }
   
    int i;
   
    for (i = 0; i < parents.length; i++) {
      EvolvableBoxAgent3DGenome genome = EvolvableBoxAgent3DGenomeHandler.parseGenomeFromString(parents[i].toString());
      newGeneration[i] = EvolvableBoxAgent3DGenomeHandler.parseGenomeFromString(genome.toString());
      System.out.println("old:" +i+":"+ newGeneration[i]);     
      EvolvableBoxAgent3DGenome genomeToBeMutated = EvolvableBoxAgent3DGenomeHandler.parseGenomeFromString(genome.toString());     
      newGeneration[i+parents.length] = EvolvableBoxAgent3DGenomeHandler.mutate(genomeToBeMutated, params, r);
      System.out.println("new:" +(i+parents.length)+":"+ newGeneration[i+parents.length] + "\n\n");
    }
   
    while (i < newPopulationSize) {
      EvolvableBoxAgent3DGenome genome = EvolvableBoxAgent3DGenomeHandler.parseGenomeFromString(parents[r.nextInt(parents.length)].toString());
      genome = EvolvableBoxAgent3DGenomeHandler.mutate(EvolvableBoxAgent3DGenomeHandler.parseGenomeFromString(genome.toString()), params, r);
      newGeneration[i] = EvolvableBoxAgent3DGenomeHandler.parseGenomeFromString(genome.toString());
      i++;
    }
   
    //System.out.println("new pop:");
    for (int j = 0; j < newGeneration.length; j++) {
View Full Code Here

      System.out.println(population[i]);
    }
   
   
    ////////////////////////
    EvolvableBoxAgent3DGenome genome = EvolvableBoxAgent3DGenomeHandler.parseGenomeFromString("(1.9621484, 1.3617647, 1.7533932):/:1:0:(-1.5, 0.9830455, -0.13471706):(1.1001612, 0.76795936, 1.2154827):2:/:2:0:(1.5, 0.96324617, -1.3455193):(1.4607434, 1.9559069, 2.7058382):0:/:3:1:(1.5, -0.7901391, 1.2753913):(0.86123216, 0.6057988, 1.2413982):0:/:4:1:(-1.5, -0.35224316, -0.3084212):(1.3591347, 2.120702, 1.0885925):0:/:5:3:(1.5, -1.3044969, -0.4783278):(0.5226963, 0.9785385, 2.3250906):2:///:Inputs: 8XXXOutputs: 5;N00:0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0;N01:0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0;N02:0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0;N03:0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0;N04:0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0;N05:0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0;N06:0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0;N07:0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0;N08:0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0;N09:0.0I-1.2929279930901523I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0;N10:-3.7491875968551156I-1.044554445954294I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0;N11:0.0I1.7546499088438945I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0;N12:0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I-1.9697823640341512I0.0I0.0I0.0I0.0I0.0;N13:2.417422204704372I0.46564696617257234I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0I0.0;#");
    for (int i = 0; i < populationSize; i++) {
      population[i] = EvolvableBoxAgent3DGenomeHandler
          .parseGenomeFromString(genome.toString());
      System.out.println(population[i]);
    }
   
    ////////////////////////
   
View Full Code Here

    if (simZyk.getLastTick() == params.getParValueLong("simulationlength")) {
      EvolvableBoxAgent3DGenomeHandler.savePopulationToFile(params.getParValueInt("sameParamsId")
          + File.separator + fileName + "_" + params.getParValueString("folderName").toString()
          + "_"+ params.getParValueInt("sameParamsId")
          + "_finalpopulation", population, params);
      EvolvableBoxAgent3DGenome championGenome = EvolvableBoxAgent3DGenomeHandler.parseGenomeFromString(Collections.max(currentAgents).getGenome().toString());
      env.resetEnvironment();
      EvolvableBoxAgent3D championAgent = EvolvableBoxAgent3DGenomeHandler
          .createAgentFromGenome(0, null, env,
              championGenome);
      env.addAgent(championAgent);
View Full Code Here

    LinkedList<EvolvableBoxAgent3D> currentAgents = new LinkedList<EvolvableBoxAgent3D>();
   
    // Simulate all agents.
    for (int i = 0; i < population.length; i++) {
      //System.out.println("Simulating agent " + i + " in generation " + simZyk.getLastTick() + ".");
      EvolvableBoxAgent3DGenome genome = population[i];
      currentAgents.add(EvolvableBoxAgent3DGenomeHandler
          .createAgentFromGenome(0, null, env, genome));
      env.addAgent(currentAgents.getLast());
      env.stepPhysicalSimulationBy10Seconds();
      env.getAgent(0).setFitnessRelevantStartingPosition();
      // 600 steps of 1/60 s = 10 seconds.
      for (int j = 0; j < 1200; j++) {
        env.stepPhysicalSimulationAt60Hertz();
        LinkedList<EvolvableBoxAgent3DAppendix> appendices = new LinkedList<EvolvableBoxAgent3DAppendix>(
            env.getAgent(0).getAppendices().values());
        Iterator<EvolvableBoxAgent3DAppendix> iter = appendices
            .iterator();
        while (iter.hasNext()) {
          EvolvableBoxAgent3DAppendix a = iter.next();
          float desiredAngle = (j % 120) / 120.f;
          desiredAngle = (float) (desiredAngle * Math.PI * 2);
          desiredAngle = (float) Math.sin(desiredAngle);
          desiredAngle = desiredAngle + 1;
          desiredAngle = desiredAngle / 2f;
          a.getAngleActuator().setNormalizedAngle(desiredAngle);
          String currentSensor = "EvolvableBoxAgent3DActuatorServo"
              + a.getAppendixId();
          env.getAgent(0).actuate(currentSensor);
        }
      }
      //System.out.println("Agent " + i + " has fitness of "
      //    + env.getAgent(0).getFitness());
      if (env.getAgent(0).getFitness() > highestFitness) {
        System.out.println("New highest fitness: "
            + env.getAgent(0).getFitness());
        System.out.println("Genome: "
            + env.getAgent(0).getGenome().toString());
        bestGenome = env.getAgent(0).getGenome();
        highestFitness = env.getAgent(0).getFitness();
        EvolvableBoxAgent3DGenomeHandler.savePopulationToFile("GENOMEScurrentBest",
            new EvolvableBoxAgent3DGenome[] { bestGenome }, params);
      }
      env.removeAgent(0);
    }
    // End after 50 generations.
    if (simZyk.getLastTick() == 49) {
      System.out.println("Best achieving genome: "
          + bestGenome.toString() + " with fitness of: " + highestFitness + ".");
      EvolvableBoxAgent3DGenome[] save = new EvolvableBoxAgent3DGenome[] { bestGenome };
      EvolvableBoxAgent3DGenomeHandler.savePopulationToFile("GENOMESbestDuringWholeRun",
          save, params);
      env.getSimTime().timeTerminate();
    }
   
    //
    else {
      EvolvableBoxAgent3DGenome[] newPopulation = new EvolvableBoxAgent3DGenome[population.length];

      // remove the bottom 10 of the population
      for (int i = 0; i < 10; i++) {
        currentAgents.remove(Collections.min(currentAgents));
      }
     
      // add 8 new random genomes
      for (int i = 0; i < 8; i++) {
        EvolvableBoxAgent3DGenome g = EvolvableBoxAgent3DGenomeHandler.createRandomGenome(params, new Random());
        newPopulation[i] = g;
      }
     
      // allow the next 35 in new population
      for (int i = 8; i < 44; i++) {
        EvolvableBoxAgent3D a = Collections.min(currentAgents);
        EvolvableBoxAgent3DGenome origGenome = a.getGenome();
        newPopulation[i] = origGenome;
        currentAgents.remove(a);
      }

      // add the next 5 in new population and 5 mutated, too
View Full Code Here

TOP

Related Classes of eas.users.students.fabian.diplomarbeit.EvolvableBoxAgents3D.Agents.EvolvableBoxAgent3DGenome

Copyright © 2018 www.massapicom. 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.