Esempio n. 1
0
  /**
   * <br>
   * In the first round,</br> <br>
   * In the paper the server is supposed to ask sensors for uncoded data.</br> <br>
   * We sent the maximum number of bits: 63 bits = "111111"</br> <br>
   * Sensor will encode in IEEE double format</br> <br>
   * Calculates the i (number of requested bits), as given in the paper</br>
   */
  @Override
  public String sendRequest(int sensor_id) {

    if (is_first_round[sensor_id] || sensor_id == 0 || round_number < M) {

      requested_bits[sensor_id] = maximum_number_of_bits_askable;

      return "111111";
    }

    double delta = code_book.getD();

    int i =
        (int)
            Math.ceil(
                (0.5
                        * (Math.log((sigma[sensor_id] * sigma[sensor_id]) / (delta * delta * P_e))
                            / Math.log(2.0)))
                    + 1);
    i = Math.min(i, maximum_number_of_bits_askable); // request not more that 63 bits
    i = Math.max(0, i); // request no less than 0 bits

    requested_bits[sensor_id] = i;

    String bit_string =
        tools.pad0ToFront(
            Integer.toBinaryString(i),
            bits_needed_to_represent_maximum_askable); // pad it to make length 6

    return tools.reverse(bit_string);
  }
Esempio n. 2
0
  @Override
  public double[] getReadingReceivedByServer() {

    return tools.makeCopy(current_sensor_readings);
  }
Esempio n. 3
0
  @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
  }