public static org.nd4j.linalg.api.rng.distribution.Distribution createDistribution(
     Distribution dist) {
   if (dist == null) return null;
   if (dist instanceof NormalDistribution) {
     NormalDistribution nd = (NormalDistribution) dist;
     return Nd4j.getDistributions().createNormal(nd.getMean(), nd.getStd());
   }
   if (dist instanceof GaussianDistribution) {
     GaussianDistribution nd = (GaussianDistribution) dist;
     return Nd4j.getDistributions().createNormal(nd.getMean(), nd.getStd());
   }
   if (dist instanceof UniformDistribution) {
     UniformDistribution ud = (UniformDistribution) dist;
     return Nd4j.getDistributions().createUniform(ud.getLower(), ud.getUpper());
   }
   if (dist instanceof BinomialDistribution) {
     BinomialDistribution bd = (BinomialDistribution) dist;
     return Nd4j.getDistributions()
         .createBinomial(bd.getNumberOfTrials(), bd.getProbabilityOfSuccess());
   }
   throw new RuntimeException("unknown distribution type: " + dist.getClass());
 }
Example #2
0
  /** Handles data arriving at Channel port */
  protected synchronized void dataArriveAtChannelPort(Object data_) {
    long s;
    if (gen == null) {
      s = SEED_RNG;
      // Uncomment the following line to get different results in every run
      // s = (long)(Math.abs(java.lang.System.currentTimeMillis()*Math.random()*100)) ;
      gen = new GaussianDistribution(NOISE_MEAN, NOISE_VAR, s);
    }

    // get most up-to-date location of the node (i.e., the receiver
    // of the message) from SensorMobilityModel
    SensorPositionReportContract.Message msg = new SensorPositionReportContract.Message();
    msg = (SensorPositionReportContract.Message) mobilityPort.sendReceive(msg);
    double Xc = msg.getX();
    double Yc = msg.getY();
    double Zc = msg.getZ();

    // extract the location of the sender from the message being received
    double Xs, Ys, Zs; // position of the sender
    SensorNodeChannelContract.Message msg2 = (SensorNodeChannelContract.Message) data_;
    Xs = msg2.getX();
    Ys = msg2.getY();
    Zs = msg2.getZ();

    // extract also the power with which the packet was sent
    double Pt_received; // Pt of the received packet
    Pt_received = msg2.getPt();

    long target_nid = msg2.getNid();

    // make up a SensorRadioPropagationContract to ask the
    // propagation model to reply with the received signal strength
    SensorRadioPropagationQueryContract.Message msg3 =
        (SensorRadioPropagationQueryContract.Message)
            propagationPort.sendReceive(
                new SensorRadioPropagationQueryContract.Message(
                    Pt_received, Xs, Ys, Zs, Xc, Yc, Zc));
    double Pr = msg3.getPr();

    if (Pr < RxThresh) {
      System.out.println(
          "SensorPhy nid=" + nid + " Packet was discarded because Pr = " + Pr + " < " + RxThresh);
    } else {
      double af = Pr; // attenuation factor
      int size = ((TargetPacket) msg2.getPkt()).size;
      TargetPacket sensorPkt = new TargetPacket(size, ((TargetPacket) msg2.getPkt()).data);
      lastNoisePower = 0.0;
      double rd;
      for (int k = 0; k < sensorPkt.size; k++) {
        sensorPkt.data[k] = sensorPkt.data[k] * af; // attenuate the signal
        rd = gen.nextDouble();
        double noise = noiseStrength * rd;
        sensorPkt.data[k] = sensorPkt.data[k] + noise;
        lastNoisePower = lastNoisePower + (noise * noise);
      } // end for

      lastNoisePower = lastNoisePower / ((double) size);

      // Forward the data packet up to the sensor agent
      toAgentPort.doSending(
          new SensorAgentPhyContract.Message(
              lastNoisePower, new TargetPacket(size, sensorPkt.data), target_nid));
    } // end else
  } // end dataArriveAtChannelPort