Ejemplo n.º 1
0
  /** Insert the method's description here. Creation date: (16/01/2002 9.53.37) */
  public static boolean xor_evaluate(Organism organism) {

    Network _net = null;
    boolean success = false;
    double errorsum = 0.0;
    double[] out = new double[4]; // The four outputs

    //   int numnodes = 0;
    int net_depth = 0; // The max depth of the network to be activated
    int count = 0;

    // The four possible input combinations to xor
    // The first number is for biasing

    double in[][] = {{1.0, 0.0, 0.0}, {1.0, 0.0, 1.0}, {1.0, 1.0, 0.0}, {1.0, 1.0, 1.0}};

    _net = organism.net;
    //   numnodes = organism.genome.nodes.size();

    net_depth = _net.max_depth();

    // for each example , 'count', propagate signal .... and compute results
    for (count = 0; count <= 3; count++) {

      // first activation from sensor to first next levelof neurons
      _net.load_sensors(in[count]);
      success = _net.activate();

      // next activation while last level is reached !
      // use depth to ensure relaxation

      for (int relax = 0; relax <= net_depth; relax++) success = _net.activate();

      // ok : the propagation is completed : repeat until all examples are presented
      out[count] = ((NNode) _net.getOutputs().firstElement()).getActivation();
      _net.flush();
    }

    // control the result
    if (success) {
      errorsum =
          (double)
              (Math.abs(out[0])
                  + Math.abs(1.0 - out[1])
                  + Math.abs(1.0 - out[2])
                  + Math.abs(out[3]));
      organism.setFitness(Math.pow((4.0 - errorsum), 2));
      organism.setError(errorsum);
    } else {
      errorsum = 999.0;
      organism.setFitness(0.001);
      organism.setError(errorsum);
    }
    String mask03 = "0.000";
    DecimalFormat fmt03 = new DecimalFormat(mask03);

    if ((out[0] < 0.5) && (out[1] >= 0.5) && (out[2] >= 0.5) && (out[3] < 0.5)) {
      organism.setWinner(true);
      return true;
    } else {
      organism.setWinner(false);
      return false;
    }
  }