public BiPolarNeuralData randomBiPolar(int size) {
   BiPolarNeuralData result = new BiPolarNeuralData(size);
   for (int i = 0; i < size; i++) {
     if (Math.random() > 0.5) result.setData(i, -1);
     else result.setData(i, 1);
   }
   return result;
 }
  /** Train the neural network. */
  public void train() {
    BiPolarNeuralData pattern = new BiPolarNeuralData(this.grid.length);

    for (int i = 0; i < this.grid.length; i++) {
      pattern.setData(i, grid[i]);
    }

    this.hopfield.addPattern(pattern);
  }
  public BiPolarNeuralData stringToBipolar(String str) {
    BiPolarNeuralData result = new BiPolarNeuralData(str.length() * BITS_PER_CHAR);
    int currentIndex = 0;
    for (int i = 0; i < str.length(); i++) {
      char ch = Character.toUpperCase(str.charAt(i));
      int idx = ch - FIRST_CHAR;

      int place = 1;
      for (int j = 0; j < BITS_PER_CHAR; j++) {
        boolean value = (idx & place) > 0;
        result.setData(currentIndex++, value);
        place *= 2;
      }
    }
    return result;
  }
  public String bipolalToString(BiPolarNeuralData data) {
    StringBuilder result = new StringBuilder();

    int j, a, p;

    for (int i = 0; i < (data.size() / BITS_PER_CHAR); i++) {
      a = 0;
      p = 1;
      for (j = 0; j < BITS_PER_CHAR; j++) {
        if (data.getBoolean(i * BITS_PER_CHAR + j)) a += p;

        p *= 2;
      }
      result.append((char) (a + FIRST_CHAR));
    }

    return result.toString();
  }