コード例 #1
0
ファイル: NNDSCServer.java プロジェクト: puaykai/U154030
  @Override
  public void initialize(int number_of_sensors) {

    this.number_of_sensors = number_of_sensors;
    this.is_first_round = new boolean[this.number_of_sensors];
    Arrays.fill(is_first_round, true);

    current_sensor_readings = new double[this.number_of_sensors];
    obtained_sensor_reading = new boolean[this.number_of_sensors];
    past_sensor_readings = new double[this.number_of_sensors][this.M];

    // **********************Neural Network Initialization

    array_of_neural_nets = new BasicNetwork[this.number_of_sensors];

    input_datas = new OrderedDataSet[this.number_of_sensors];

    for (int i = 0; i < this.number_of_sensors; i++) {

      array_of_neural_nets[i] = new BasicNetwork();

      array_of_neural_nets[i].addLayer(
          new BasicLayer(
              new ActivationSigmoid(),
              true,
              this.M
                  + i)); // input layer corresponds to number of features (amount of past readings
                         // to remember + amount of sensors before it)

      array_of_neural_nets[i].addLayer(
          new BasicLayer(new ActivationSigmoid(), true, number_of_nodes_in_middle_layer));

      array_of_neural_nets[i].addLayer(
          new BasicLayer(
              new ActivationSigmoid(),
              true,
              1)); // the output layer, i.e. prediction of the sensor reading

      array_of_neural_nets[i].getStructure().finalizeStructure();

      array_of_neural_nets[i].reset();

      input_datas[i] = new OrderedDataSet(amount_of_neural_net_input_to_keep);
    }
    // **********************Neural Network Initialization

    predicted_sensor_readings = new double[this.number_of_sensors][1];
    sigma = new double[this.number_of_sensors];
    requested_bits = new int[number_of_sensors];

    diff = new double[number_of_sensors];
  }
コード例 #2
0
ファイル: NNDSCServer.java プロジェクト: puaykai/U154030
  @Override
  public boolean receiveData(String data, int id) {

    if (id != 0
        && round_number >= K
        && data.length() != requested_bits[id] + 1
        && data.length() != 0) {
      return false;
    }

    if (!is_first_round[id] && id != 0 && !obtained_sensor_reading[0]) {

      return false;
    }

    // first_reading / first sensor everybody is un-coded, and is assumed to be correct
    if (round_number < M || is_first_round[id] || id == 0) {

      is_first_round[id] = false;

      current_sensor_readings[id] = tools.binaryToDouble(data);

      obtained_sensor_reading[id] = true;

    } else {

      current_sensor_readings[id] = code_book.getDecodedValue(current_sensor_readings[0], data);
      obtained_sensor_reading[id] = true;
    }

    // calculate Y, prediction values, only after M readings

    if (round_number >= (M - 1)) {

      double temp = current_sensor_readings[id];
      updatePastReadings(temp, id);

      this.updateInputData(id);
      this.trainNeuralNets(id);
      this.calculatePrediction(id);
      this.updateVarianceOfPrediction(id);

    } else {

      double temp_value = current_sensor_readings[id];

      past_sensor_readings[id][M - round_number - 1] = temp_value;
    }

    diff[id] = current_sensor_readings[id] - predicted_sensor_readings[id][0];
    boolean all_received = true;
    for (int i = 0; i < number_of_sensors; i++) {

      if (!obtained_sensor_reading[i]) {

        all_received = false;
        break;
      }
    }
    if (all_received) {
      Arrays.fill(obtained_sensor_reading, false);

      round_number++;

      double average = 0.0;
      for (int i = 0; i < number_of_sensors; i++) {

        average += Math.abs(diff[i]);
      }
      // System.out.println("Total prediction error: "+average);

    }

    return true; // received the data
  }