/// <summary> /// within my subzone ONLY /// the source and sender node are the same. /// </summary> /// <param name="senderNode"></param> /// <returns></returns> public List <RoutingMetric> SelectForwardersSetWithinMySubZone(Sensor senderNode) { // the routing zone. List <Point> FourCorners = senderNode.MyRoutingZone; Parallelogram Zone = new Computations.Parallelogram(); if (FourCorners != null) { Zone.P1 = FourCorners[0]; Zone.P2 = FourCorners[1]; Zone.P3 = FourCorners[2]; Zone.P4 = FourCorners[3]; } Geomtric Geo = new Computations.Geomtric(); List <RoutingMetric> Forwarders = new List <RoutingMetric>(); if (PublicParamerters.SinkNode == null) { return(null); } else if (PublicParamerters.SinkNode.ID != senderNode.ID) // for all node but sink node { List <Sensor> N = senderNode.NeighboreNodes; if (N != null) { if (N.Count > 0) { // sum: double EnergySum = 0; // energy double DirectionSum = 0; // direction double PreDistanceSum = 0; // prependiculare distance double TransDistanceSum = 0; // transmission distance foreach (Sensor potentialForwarder in N) { if (potentialForwarder.ResidualEnergy > 0) { // if next hop is wthin the zone of the source node then: Point point = potentialForwarder.CenterLocation; if (Geo.PointTestParallelogram(Zone, point)) { double Dij = Operations.DistanceBetweenTwoSensors(senderNode, potentialForwarder); // i and j double Djb = Operations.DistanceBetweenTwoSensors(potentialForwarder, PublicParamerters.SinkNode); // j and b double Dib = Operations.DistanceBetweenTwoSensors(senderNode, PublicParamerters.SinkNode); // i and b RoutingMetric metRic = new RoutingMetric(); metRic.PID = PublicParamerters.NumberofGeneratedPackets; metRic.SenderNode = senderNode; metRic.SourceNode = senderNode; metRic.PotentialForwarder = potentialForwarder; metRic.DistanceFromSenderToForwarder = Dij; metRic.DistanceFromSenderToSink = Dib; metRic.DistanceFromFrowarderToSink = Djb; metRic.r = senderNode.ComunicationRangeRadius; metRic.ZoneWidthControl = Settings.Default.ZoneWidth; // sum's: EnergySum += Math.Exp(Math.Pow(metRic.NormalizedEnergy, Settings.Default.EnergyDistCnt)); TransDistanceSum += (1 / (Math.Exp(Math.Pow(metRic.NormalizedTransDistance, Settings.Default.TransDistanceDistCnt)))); DirectionSum += (1 / (Math.Exp(Math.Pow(metRic.NormalizedDirection, Settings.Default.DirectionDistCnt)))); PreDistanceSum += (1 / (Math.Exp(Math.Pow(metRic.NormalizePerpendicularDistance, Settings.Default.PrepDistanceDistCnt)))); Forwarders.Add(metRic); // keep for each node. } else { // if the sink is a direct neigbore with me if (PublicParamerters.SinkNode.ID == potentialForwarder.ID) { double Dij = Operations.DistanceBetweenTwoSensors(senderNode, potentialForwarder); // i and j double Djb = Operations.DistanceBetweenTwoSensors(potentialForwarder, PublicParamerters.SinkNode); // j and b double Dib = Operations.DistanceBetweenTwoSensors(senderNode, PublicParamerters.SinkNode); // i and b RoutingMetric metRic = new RoutingMetric(); metRic.PID = PublicParamerters.NumberofGeneratedPackets; metRic.SenderNode = senderNode; metRic.SourceNode = senderNode; metRic.PotentialForwarder = potentialForwarder; metRic.DistanceFromSenderToForwarder = Dij; metRic.DistanceFromSenderToSink = Dib; metRic.DistanceFromFrowarderToSink = Djb; metRic.r = senderNode.ComunicationRangeRadius; metRic.ZoneWidthControl = Settings.Default.ZoneWidth; // sum's: EnergySum += Math.Exp(Math.Pow(metRic.NormalizedEnergy, Settings.Default.EnergyDistCnt)); TransDistanceSum += (1 / (Math.Exp(Math.Pow(metRic.NormalizedTransDistance, Settings.Default.TransDistanceDistCnt)))); DirectionSum += (1 / (Math.Exp(Math.Pow(metRic.NormalizedDirection, Settings.Default.DirectionDistCnt)))); PreDistanceSum += (1 / (Math.Exp(Math.Pow(metRic.NormalizePerpendicularDistance, Settings.Default.PrepDistanceDistCnt)))); Forwarders.Add(metRic); // keep for each node. } } } } double LinkQEstiSum = 0;// the sum of link quality estimations, for all forwarders. int k = 0; foreach (RoutingMetric forwarder in Forwarders) { // propablity distrubution: forwarder.EnPr = (Math.Exp(Math.Pow(forwarder.NormalizedEnergy, Settings.Default.EnergyDistCnt))) / EnergySum; forwarder.TdPr = (1 / (Math.Exp(Math.Pow(forwarder.NormalizedTransDistance, Settings.Default.TransDistanceDistCnt)))) / TransDistanceSum; forwarder.DirPr = (1 / (Math.Exp(Math.Pow(forwarder.NormalizedDirection, Settings.Default.DirectionDistCnt)))) / DirectionSum; // propablity for lemda. forwarder.PerdPr = (1 / (Math.Exp(Math.Pow(forwarder.NormalizePerpendicularDistance, Settings.Default.PrepDistanceDistCnt)))) / PreDistanceSum; LinkQEstiSum += forwarder.LinkEstimation; k++; } foreach (RoutingMetric Potentialforwarder in Forwarders) { Potentialforwarder.LinkEstimationNormalized = Potentialforwarder.LinkEstimation / LinkQEstiSum; } // end lik hoode /* * * // * List<RoutingMetric> selected = new List<Forwarding.RoutingMetric>(); * foreach (RoutingMetric or in Forwarders) * { * if(or.LinkEstimationNormalized>(1/(Convert.ToDouble(Forwarders.Count)))) * { * selected.Add(or); * } * } * * if(selected.Count>=1) * { * Forwarders.Clear(); * Forwarders = selected; * } * * LinkQEstiSum = 0; * foreach (RoutingMetric forwarder in Forwarders) * { * LinkQEstiSum += forwarder.LinkEstimation; * } * * foreach (RoutingMetric Potentialforwarder in Forwarders) * { * Potentialforwarder.LinkEstimationNormalized = Potentialforwarder.LinkEstimation / LinkQEstiSum; * * } */ } } } Forwarders.Sort(new LinkPrioritySorter());// sort according to Priority. return(Forwarders); }
/// <summary> /// PowerThetaij should be the maximal. /// PowerHij should be smaller than PowerThetaij /// PowerNij should be smaller than (PowerThetaij+PowerHij)/2 /// PowerRj= the main of all. /// </summary> /// <summary> /// i current node. /// when i has the data packet it make the dicient which is the nesx step based on these two things. /// </summary> /// <param name="SourceNode"></param> /// <param name="SinkNode"></param> /// <param name="forwardNode"></param> /// <returns></returns> public List <RoutingRandomVariable> ComputeDistributions(Sensor SourceNode, Sensor SinkNode, Sensor forwardNode, double EnergyDistCnt, double TransDistanceDistCnt, double DirectionDistCnt, double PrepDistanceDistCnt) { List <Point> Zone = SourceNode.MyRoutingZone; Parallelogram rect = new Computations.Parallelogram(); if (Zone != null) { rect.P1 = Zone[0]; rect.P2 = Zone[1]; rect.P3 = Zone[2]; rect.P4 = Zone[3]; } Geomtric Geo = new Computations.Geomtric(); List <RoutingRandomVariable> distributions = new List <RoutingRandomVariable>(); if (SinkNode == null) { return(null); } else if (SinkNode.ID != forwardNode.ID) // for all node but sink node { List <Sensor> N = forwardNode.NeighboreNodes; if (N != null) { if (N.Count > 0) { // sum: double EnergySum = 0; // energy double DirectionSum = 0; // direction double PreDistanceSum = 0; // prependiculare distance double TransDistanceSum = 0; // transmission distance foreach (Sensor nextHop in N) { if (nextHop.ResidualEnergy > 0) { // if next hop is wthin the zone of the source node then: Point point = nextHop.CenterLocation; if (Geo.PointTestParallelogram(rect, point)) { double Dij = Operations.DistanceBetweenTwoSensors(forwardNode, nextHop); // i and j double Djb = Operations.DistanceBetweenTwoSensors(nextHop, SinkNode); // j and b double Dib = Operations.DistanceBetweenTwoSensors(forwardNode, SinkNode); // i and b RoutingRandomVariable variable = new RoutingRandomVariable(); variable.PID = PublicParamerters.PackeTSequenceID; variable.ForwardNode = forwardNode; variable.SourceNode = SourceNode; variable.NextHopNode = nextHop; variable.Dij = Dij; variable.Dib = Dib; variable.Djb = Djb; variable.r = forwardNode.ComunicationRangeRadius; variable.ZoneWidthControl = Settings.Default.ZoneWidthCnt; // sum's: EnergySum += Math.Exp(Math.Pow(variable.NormalizedEnergy, EnergyDistCnt)); TransDistanceSum += (1 / (Math.Exp(Math.Pow(variable.NormalizedTransDistance, TransDistanceDistCnt)))); DirectionSum += (1 / (Math.Exp(Math.Pow(variable.NormalizedDirection, DirectionDistCnt)))); PreDistanceSum += (1 / (Math.Exp(Math.Pow(variable.NormalizePerpendicularDistance, PrepDistanceDistCnt)))); distributions.Add(variable); // keep for each node. } } } double RoutingvariablSum = 0; int k = 0; foreach (RoutingRandomVariable dist in distributions) { // propablity distrubution: dist.EnergyProb = (Math.Exp(Math.Pow(dist.NormalizedEnergy, EnergyDistCnt))) / EnergySum; dist.TransDistanceProb = (1 / (Math.Exp(Math.Pow(dist.NormalizedTransDistance, TransDistanceDistCnt)))) / TransDistanceSum; dist.DirectionProb = (1 / (Math.Exp(Math.Pow(dist.NormalizedDirection, DirectionDistCnt)))) / DirectionSum; // propablity for lemda. dist.PerpendicularDistanceProb = (1 / (Math.Exp(Math.Pow(dist.NormalizePerpendicularDistance, PrepDistanceDistCnt)))) / PreDistanceSum; RoutingvariablSum += dist.RoutingVariable; k++; } distributions.Sort(new RelativityCompar());// sort //- likehool probablity. double LikehoodcumlativeMin = 0; double LikeacumlativeMax = 0; int f = 0; foreach (RoutingRandomVariable randvar in distributions) { randvar.RoutingVariableProb = randvar.RoutingVariable / RoutingvariablSum; // set ranges: if (f == 0) { LikehoodcumlativeMin = 0; LikeacumlativeMax = randvar.RoutingVariableProb * PublicParamerters.MultiplyBy; randvar.RoutingProbRange[0] = LikehoodcumlativeMin; // min randvar.RoutingProbRange[1] = LikeacumlativeMax; // max } else { // get: LikehoodcumlativeMin += (distributions[f - 1].RoutingVariableProb * PublicParamerters.MultiplyBy); LikeacumlativeMax += (distributions[f].RoutingVariableProb * PublicParamerters.MultiplyBy); randvar.RoutingProbRange[0] = LikehoodcumlativeMin; // min randvar.RoutingProbRange[1] = LikeacumlativeMax; // max } f++; } // end lik hoode } } } return(distributions); }