/** The main loop for the background thread. It is here that most of the work os orchestrated. */
  public void run() {

    double thisCost = 500.0;
    double oldCost = 0.0;
    double dcost = 500.0;
    int countSame = 0;

    map.update(map.getGraphics());

    while (countSame < 100) {

      generation++;

      int ioffset = matingPopulationSize;
      int mutated = 0;

      // Mate the chromosomes in the favoured population
      // with all in the mating population
      for (int i = 0; i < favoredPopulationSize; i++) {
        Chromosome cmother = chromosomes[i];
        // Select partner from the mating population
        int father = (int) (0.999999 * Math.random() * (double) matingPopulationSize);
        Chromosome cfather = chromosomes[father];

        mutated += cmother.mate(cfather, chromosomes[ioffset], chromosomes[ioffset + 1]);
        ioffset += 2;
      }

      // The new generation is in the matingPopulation area
      // move them to the correct area for sort.
      for (int i = 0; i < matingPopulationSize; i++) {
        chromosomes[i] = chromosomes[i + matingPopulationSize];
        chromosomes[i].calculateCost(cities);
      }

      // Now sort the new mating population
      Chromosome.sortChromosomes(chromosomes, matingPopulationSize);

      double cost = chromosomes[0].getCost();
      dcost = Math.abs(cost - thisCost);
      thisCost = cost;
      double mutationRate = 100.0 * (double) mutated / (double) matingPopulationSize;

      NumberFormat nf = NumberFormat.getInstance();
      nf.setMinimumFractionDigits(2);
      nf.setMinimumFractionDigits(2);

      status.setText(
          "Generation "
              + generation
              + " Cost "
              + (int) thisCost
              + " Mutated "
              + nf.format(mutationRate)
              + "%");

      if ((int) thisCost == (int) oldCost) {
        countSame++;
      } else {
        countSame = 0;
        oldCost = thisCost;
      }
      map.update(map.getGraphics());
    }
    status.setText("Solution found after " + generation + " generations.");
  }
Exemple #2
0
 static {
   Stroke stroke = new BasicStroke(2);
   selectionShape = stroke.createStrokedShape(hitRect);
   format.setMinimumIntegerDigits(1);
   format.setMinimumFractionDigits(1);
   format.setMaximumFractionDigits(2);
 }
Exemple #3
0
  public void updateStates() {
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(2);
    nf.setMinimumFractionDigits(2);

    try {
      // System.out.println("getting variables.");
      status = cryo.cryoGetStatusCORBA();
      frame.lblStatus.setText(statusString[status] + " ");
      heater = cryo.cryoGetHeaterCORBA();
      frame.lblHeater.setText(nf.format(heater) + " watts");
      temp = cryo.cryoGetTempCORBA();
      frame.lblTemp.setText(nf.format(temp) + " deg");
      cli = cryo.cryoGetCliCORBA();
      if (Double.isNaN(cli)) {
        frame.lblCli.setText(cli + " ");
      } else frame.lblCli.setText(nf.format(cli) + " ");

    } catch (org.omg.CORBA.COMM_FAILURE cf) {
      // stop thread and try to reconnect to the server
      frame.lblStatus.setText("FAILURE!! Server connected?");
      stop = true;
      return;
    }
  }
Exemple #4
0
  //
  // Sets the frame rate text displayed in the lower left corner.
  //
  void setFrameRateText() {
    NumberFormat nf = NumberFormat.getInstance();

    nf.setMinimumFractionDigits(2);
    nf.setMaximumFractionDigits(2);

    frameRateText.text(
        "frames/sec = " + nf.format((double) frameCount / ((double) (time - lastTime) / 1000.0)));
  }
  /** Update the statistic */
  public void setStats(int mi, double me, int ma, double sd) {
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(2);
    nf.setMinimumFractionDigits(2);
    nf.setGroupingUsed(false);

    min.setText(Integer.toString(mi));
    max.setText(Integer.toString(ma));

    String s = nf.format(me);
    mean.setText(s);
    s = nf.format(sd);
    stdDev.setText(s);
  }