/** 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.");
  }
Пример #2
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;
    }
  }
Пример #3
0
 protected void plotScatterDiagram() {
   // plot sample as one dimensional scatter plot and Gaussian
   double xmax = 5.;
   double xmin = -5.;
   DatanGraphics.openWorkstation(getClass().getName(), "E3Min_1.ps");
   DatanGraphics.setFormat(0., 0.);
   DatanGraphics.setWindowInComputingCoordinates(xmin, xmax, 0., .5);
   DatanGraphics.setViewportInWorldCoordinates(-.15, .9, .16, .86);
   DatanGraphics.setWindowInWorldCoordinates(-.414, 1., 0., 1.);
   DatanGraphics.setBigClippingWindow();
   DatanGraphics.chooseColor(2);
   DatanGraphics.drawFrame();
   DatanGraphics.drawScaleX("y");
   DatanGraphics.drawScaleY("f(y)");
   DatanGraphics.drawBoundary();
   double xpl[] = new double[2];
   double ypl[] = new double[2];
   // plot scatter diagram
   DatanGraphics.chooseColor(1);
   for (int i = 0; i < y.length; i++) {
     xpl[0] = y[i];
     xpl[1] = y[i];
     ypl[0] = 0.;
     ypl[0] = .1;
     DatanGraphics.drawPolyline(xpl, ypl);
   }
   // draw Gaussian corresponding to solution
   int npl = 100;
   xpl = new double[npl];
   ypl = new double[npl];
   double fact = 1. / (Math.sqrt(2. * Math.PI) * x.getElement(1));
   double dpl = (xmax - xmin) / (double) (npl - 1);
   for (int i = 0; i < npl; i++) {
     xpl[i] = xmin + (double) i * dpl;
     ypl[i] = fact * Math.exp(-.5 * Math.pow((xpl[i] - x.getElement(0)) / x.getElement(1), 2.));
   }
   DatanGraphics.chooseColor(5);
   DatanGraphics.drawPolyline(xpl, ypl);
   // draw caption
   String sn = "N = " + nny;
   numForm.setMaximumFractionDigits(3);
   numForm.setMinimumFractionDigits(3);
   String sx1 = ", x_1# = " + numForm.format(x.getElement(0));
   String sx2 = ", x_2# = " + numForm.format(x.getElement(1));
   String sdx1 = ", &D@x_1# = " + numForm.format(Math.sqrt(cx.getElement(0, 0)));
   String sdx2 = ", &D@x_2# = " + numForm.format(Math.sqrt(cx.getElement(1, 1)));
   caption = sn + sx1 + sx2 + sdx1 + sdx2;
   DatanGraphics.setBigClippingWindow();
   DatanGraphics.chooseColor(2);
   DatanGraphics.drawCaption(1., caption);
   DatanGraphics.closeWorkstation();
 }
Пример #4
0
 public E3Min() {
   numForm = NumberFormat.getNumberInstance(Locale.US);
   numForm.setMaximumFractionDigits(12);
   numForm.setMinimumFractionDigits(12);
   String s =
       "Example demonstrating the use of class MinAsy by fitting a Gaussian to small sample"
           + " and determining the asymmetric errors of parameters by MinAsy";
   df = new DatanFrame(getClass().getName(), s);
   AuxJInputGroup ig = new AuxJInputGroup("Enter number N of events (>= 2, <= 10000)", "");
   JLabel errorLabel = new JLabel();
   ni[0] = new AuxJNumberInput("N", "number of events", errorLabel);
   ig.add(ni[0]);
   ni[0].setProperties("N", true);
   ni[0].setMinimum(2);
   ni[0].setMaximum(10000);
   ni[0].setNumberInTextField(10);
   df.add(ig);
   df.add(errorLabel);
   JButton goButton = new JButton("Go");
   GoButtonListener gl = new GoButtonListener();
   goButton.addActionListener(gl);
   df.add(goButton);
   df.repaint();
 }