#-------------------------------------------------------------------------------
# Name: module1
# Purpose:
#
# Author: Administrator
#
# Created: 05-10-2014
# Copyright: (c) Administrator 2014
# Licence: <your licence>
#-------------------------------------------------------------------------------
import time
import numpy as np
import matplotlib.pyplot as plt
import math
D=5
S=6
S1=7
S2=8
U=9
N=0
WL=-5
INF=1000
class DVHop:
def __init__(self):
## self.R=20
self.BorderLen=100
self.NodeCount=100
self.R=20
self.DeployArr=np.arange(self.NodeCount)
self.GAMtx=np.zeros((2,2))
self.GBMtx=np.zeros(2)
self.distMtxRef=np.zeros((self.NodeCount,self.NodeCount))
self.distMtx=np.zeros((self.NodeCount,self.NodeCount))
self.NodeCoordinateReference=np.zeros((2,self.NodeCount))
self.NodeCoordinate=np.zeros((2,self.NodeCount))
for idx in range(10):
for inner in range(10):
self.NodeCoordinateReference[0,idx*10+inner]=idx*10+np.random.random()*10
self.NodeCoordinateReference[1,idx*10+inner]=inner*10+np.random.random()*10
for idx in range(self.NodeCount):
for inner in range(self.NodeCount):
self.distMtxRef[idx,inner]=math.sqrt(math.pow(self.NodeCoordinateReference[0,idx]-self.NodeCoordinateReference[0,inner],2)+math.pow(self.NodeCoordinateReference[1,idx]-self.NodeCoordinateReference[1,inner],2))
self.distMtxRef[inner,idx]=self.distMtxRef[idx,inner]
def initScenario(self):
self.hopMtxRef=np.ones((self.NodeCount,self.NodeCount))*(1000)
self.hopMtx=np.ones((self.NodeCount,self.NodeCount))*(1000)
## self.NodeCoordinate=np.zeros((2,self.NodeCount))
## for idx in range(2):
## for inner in range(self.NodeCount):
## self.NodeCoordinate[idx,inner]=np.floor(np.random.random())
## self.NodeCoordinateReference=np.random.random((2,self.NodeCount))*self.BorderLen
## self.NodeCoordinate=np.zeros((2,self.NodeCount))
def generateMalicious(self):
self.MalCoordinate=np.zeros((2,2))
self.MalCoordinate[0,0]=np.random.random()*10+20
self.MalCoordinate[0,1]=np.random.random()*10+70
for idx in range(2):
self.MalCoordinate[1,idx]=np.random.random()*100
def deployMalicious(self):
self.DectaionList=np.zeros((2,self.NodeCount+1))
for idx in range(self.NodeCount):
for inner in range(2):
if math.sqrt(math.pow(self.NodeCoordinateReference[0,idx]-self.MalCoordinate[0,inner],2)+math.pow(self.NodeCoordinateReference[1,idx]-self.MalCoordinate[1,inner],2))<self.R:
self.DectaionList[inner,0]+=1
self.DectaionList[inner,self.DectaionList[inner,0]]=idx;
for idx in range(int(self.DectaionList[0,0])):
for inner in range(int(self.DectaionList[1,0])):
self.hopMtxRef[self.DectaionList[0,idx+1],self.DectaionList[1,inner+1]]=1
self.hopMtxRef[self.DectaionList[1,inner+1],self.DectaionList[0,idx+1]]=1
def labelAnchor(self):
for idx in range(self.AchorCount):
if math.pow(self.MalCoordinate[0,0]-self.NodeCoordinate[0,idx],2)+math.pow(self.MalCoordinate[1,0]-self.NodeCoordinate[1,idx],2)<math.pow(self.R,2) and math.pow(self.MalCoordinate[0,1]-self.NodeCoordinate[0,idx],2)+math.pow(self.MalCoordinate[1,1]-self.NodeCoordinate[1,idx],2)<math.pow(self.R,2):
self.LabelArr[idx]=D
else:
for inner in range(self.AchorCount):
if self.hopMtx[idx,inner]==1:
if self.distMtx[idx,inner]>=self.R:
self.LabelArr[idx]=S
self.LabelMtx[idx,inner]=WL
if self.distMtx[idx,inner]<self.R:
if math.pow(self.MalCoordinate[0,0]-self.NodeCoordinate[0,idx],2)+math.pow(self.MalCoordinate[1,0]-self.NodeCoordinate[1,idx],2)<math.pow(self.R,2) and math.pow(self.MalCoordinate[0,1]-self.NodeCoordinate[0,inner],2)+math.pow(self.MalCoordinate[1,1]-self.NodeCoordinate[1,inner],2)<math.pow(self.R,2):
self.LabelArr[idx]=S
self.LabelMtx[idx,inner]=WL
elif math.pow(self.MalCoordinate[0,0]-self.NodeCoordinate[0,inner],2)+math.pow(self.MalCoordinate[1,0]-self.NodeCoordinate[1,inner],2)<math.pow(self.R,2) and math.pow(self.MalCoordinate[0,1]-self.NodeCoordinate[0,idx],2)+math.pow(self.MalCoordinate[1,1]-self.NodeCoordinate[1,idx],2)<math.pow(self.R,2):
self.LabelArr[idx]=S
self.LabelMtx[idx,inner]=WL
for idx in range(self.AchorCount):
minID=self.NodeCount
minIdx=-1
if self.LabelArr[idx]==S and self.DeployArr[idx]<minID:
minID=self.DeployArr[idx]
minIdx=idx
for idx in range(self.AchorCount):
if self.LabelArr[idx]==S:
if all(self.LabelMtx[idx]==self.LabelMtx[minIdx]):
self.LabelArr[idx]=S1
else:
self.LabelArr[idx]=S2
def labelUnknown(self):
for idx in range(self.AchorCount,self.NodeCount):
if math.pow(self.MalCoordinate[0,0]-self.NodeCoordinate[0,idx],2)+math.pow(self.MalCoordinate[1,0]-self.NodeCoordinate[1,idx],2)<math.pow(self.R,2) and math.pow(self.MalCoordinate[0,1]-self.NodeCoordinate[0,idx],2)+math.pow(self.MalCoordinate[1,1]-self.NodeCoordinate[1,idx],2)<math.pow(self.R,2):
self.LabelArr[idx]=D
else:
for inner in range(self.NodeCount):
if self.hopMtx[idx,inner]==1:
if self.distMtx[idx,inner]<self.R:
if math.pow(self.MalCoordinate[0,0]-self.NodeCoordinate[0,idx],2)+math.pow(self.MalCoordinate[1,0]-self.NodeCoordinate[1,idx],2)<math.pow(self.R,2) and math.pow(self.MalCoordinate[0,1]-self.NodeCoordinate[0,inner],2)+math.pow(self.MalCoordinate[1,1]-self.NodeCoordinate[1,inner],2)<math.pow(self.R,2):
self.LabelArr[idx]=S
if inner<self.AchorCount:
if self.LabelArr[inner]==S1:
self.LabelArr[idx]=S2
elif self.LabelArr[inner]==S2:
self.LabelArr[idx]=S1
## self.LabelMtx[idx,inner]=WL
elif math.pow(self.MalCoordinate[0,0]-self.NodeCoordinate[0,inner],2)+math.pow(self.MalCoordinate[1,0]-self.NodeCoordinate[1,inner],2)<math.pow(self.R,2) and math.pow(self.MalCoordinate[0,1]-self.NodeCoordinate[0,idx],2)+math.pow(self.MalCoordinate[1,1]-self.NodeCoordinate[1,idx],2)<math.pow(self.R,2):
self.LabelArr[idx]=S
if inner<self.AchorCount:
if self.LabelArr[inner]==S1:
self.LabelArr[idx]=S2
elif self.LabelArr[inner]==S2:
self.LabelArr[idx]=S1
## self.LabelMtx[idx,inner]=WL
if self.LabelArr[idx]!=S1 and self.LabelArr[idx]!=S2:
tmpArr=np.arange(self.AchorCount)
tmpIdx=0
for inner in range(self.AchorCount):
if self.hopMtx[idx,inner]==1:
tmpArr[tmpIdx]=inner
tmpIdx+=1
if tmpIdx>1:
for idxI in range(tmpIdx-1):
for idxJ in range(idxI+1,tmpIdx):
if math.pow(self.NodeC