/** 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.");
  }
  /** A class to render numbers in scientific format. */
  class ScientificRenderer extends JLabel implements TableCellRenderer {
    NumberFormat format = NumberFormat.getInstance();

    public ScientificRenderer(int sigfigs) {
      sigfigs = Math.min(sigfigs, 6);
      if (format instanceof DecimalFormat) {
        String pattern = "0.0"; // $NON-NLS-1$
        for (int i = 0; i < sigfigs - 1; i++) {
          pattern += "0"; // $NON-NLS-1$
        }
        pattern += "E0"; // $NON-NLS-1$
        ((DecimalFormat) format).applyPattern(pattern);
      }
    }

    public Component getTableCellRendererComponent(
        JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
      setFont(
          getDefaultRenderer(String.class)
              .getTableCellRendererComponent(DatasetStatisticsTable.this, "a", false, false, 0, 0)
              .getFont()); //$NON-NLS-1$
      setText(format.format(value));
      setHorizontalAlignment(SwingConstants.TRAILING);
      return this;
    }
  }
示例#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;
    }
  }
示例#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);
  }
/**
 * This is a JTextField that accepts only numbers.
 *
 * @author Douglas Brown
 * @version 1.0
 */
public class NumberField extends JTextField {

  // instance fields
  protected NumberFormat format = NumberFormat.getInstance();
  protected double prevValue;
  protected Double maxValue;
  protected Double minValue;

  /**
   * Constructs a NumberField.
   *
   * @param columns the number of character columns
   */
  public NumberField(int columns) {
    super(columns);
  }

  /**
   * Gets the value from the text field.
   *
   * @return the value
   */
  public double getValue() {
    if (getText().equals(format.format(prevValue))) return prevValue;
    double retValue;
    try {
      retValue = format.parse(getText()).doubleValue();
      if (minValue != null && retValue < minValue.doubleValue()) {
        setValue(minValue.doubleValue());
        return minValue.doubleValue();
      }
      if (maxValue != null && retValue > maxValue.doubleValue()) {
        setValue(maxValue.doubleValue());
        return maxValue.doubleValue();
      }
    } catch (ParseException e) {
      Toolkit.getDefaultToolkit().beep();
      setValue(prevValue);
      return prevValue;
    }
    return retValue;
  }

  /**
   * Formats the specified value and enters it in the text field.
   *
   * @param value the value to be entered
   */
  public void setValue(double value) {
    if (!isVisible()) return;
    if (minValue != null) value = Math.max(value, minValue.doubleValue());
    if (maxValue != null) value = Math.min(value, maxValue.doubleValue());
    setFormatFor(value);
    setText(format.format(value));
    prevValue = value;
  }

  /**
   * Sets the resolution for this number field.
   *
   * @param delta the change in value that must be resolvable
   */
  public void setResolution(double delta) {
    /** implemented in subclasses */
  }

  /**
   * Sets a minimum value for this field.
   *
   * @param min the minimum allowed value
   */
  public void setMinValue(double min) {
    minValue = new Double(min);
  }

  /**
   * Sets a maximum value for this field.
   *
   * @param max the maximum allowed value
   */
  public void setMaxValue(double max) {
    maxValue = new Double(max);
  }

  /**
   * Gets the format for this field.
   *
   * @return the format
   */
  public NumberFormat getFormat() {
    return format;
  }

  /**
   * Sets the format for a specified value. Subclasses may override this to modify the format before
   * displaying the value.
   *
   * @param value the value to be displayed
   */
  public void setFormatFor(double value) {
    /** implemented in subclasses */
  }
}
  public MainCitiesCriteriaPanel() {
    super(new BorderLayout());

    CountryController countryc = Application.getCountryController();
    CitiesController citiesc = Application.getCitiesController();
    countryName = Application.getCountryName();

    label = new JLabel();
    labelPanel = new JPanel();
    labelPanel.add(label);

    label.setText("Criteria to select main cities for " + countryName.replaceAll("_", " "));

    listModel = new DefaultListModel();

    Iterator<HashMap<String, String>> iter = citiesc.getToponymTypesIterator();
    while (iter.hasNext()) {
      String topTypeName = iter.next().get("code");
      listModel.addElement(topTypeName);
    }
    list = new JList(listModel);
    list.addListSelectionListener(this);
    listPanel = new JScrollPane(list);

    NumberFormat nCitiesFormat = NumberFormat.getInstance();
    nCitiesFormat.setMaximumFractionDigits(0);
    nCitiesFormat.setMaximumIntegerDigits(4);

    NumberFormat distFormat = NumberFormat.getInstance();
    distFormat.setMaximumFractionDigits(0);
    distFormat.setMaximumIntegerDigits(3);

    nCitiesField = new JFormattedTextField(nCitiesFormat);
    distField = new JFormattedTextField(distFormat);

    nCitiesField.setMaximumSize(new Dimension(50, 1));
    distField.setMaximumSize(new Dimension(50, 1));

    rb1 = new JRadioButton("Filter by type", true);
    rb2 = new JRadioButton("Filter by number", false);
    rb3 = new JRadioButton("Filter by distance to the borders (km)", false);

    ButtonGroup bgroup = new ButtonGroup();

    bgroup.add(rb1);
    bgroup.add(rb2);
    bgroup.add(rb3);

    JPanel radioPanel = new JPanel();

    radioPanel.setLayout(new BoxLayout(radioPanel, BoxLayout.Y_AXIS));
    radioPanel.add(rb1);
    radioPanel.add(listPanel);
    radioPanel.add(rb2);
    radioPanel.add(nCitiesField);
    radioPanel.add(rb3);
    radioPanel.add(distField);

    submit = new JButton();
    back = new JButton();

    submit.setText("OK");
    back.setText("GO BACK");
    submit.addActionListener(this);
    back.addActionListener(this);

    buttonPanel = new JPanel();
    buttonPanel.add(back);
    buttonPanel.add(submit);

    add(labelPanel, BorderLayout.NORTH);
    add(radioPanel, BorderLayout.CENTER);
    add(buttonPanel, BorderLayout.SOUTH);
  }