E题(无人机)

第一问

5部雷达的地理位置坐标分别为:
雷达1(80,0,0),雷达2(30,60,0),雷达3(55,110,0),雷达4(105,110,0),雷达5(130,60,0)(单位:统一为km)。

至少三部雷达探测到

无人机的飞行速度控制120km/h~180km/h
飞行高度控制在2000m~2500m
最大加速度不超过10
无人机间距需控制在100 m以上
同一时刻一架无人机只能干扰一部雷达

贪心算法伪代码:

  1. Search 所有能够由ABC三架无人机按顺序遍历20个点的路线组合:L1,L2,L3...
  2. 删除含有相邻点距离大于最大飞行距离的路线
  3. 可行解

{
    利用可行的决策,求出可行解的一个解元素;
   }
由所有解元素组合成问题的一个可行解;

##  无人机距离矩阵
import math
import pandas as pd 
import numpy as np
import matplotlib.pyplot as plt

df=pd.read_csv('qqq.csv')
df1=pd.read_csv('c.csv')
f1=[]
f2=[]
f3=[]

n=0

def point(x1,y1,z1,x2,y2,z2,z):
    x=0
    y=0
    k=(z-z1)/(z2-z1)
    x=round(x1+k*(x2-x1),4)
    y=round(y1+k*(y2-y1),4)
    return x,y

def distance(x1,y1,x2,y2):
    k=math.sqrt((y2-y1)**2+(x2-x1)**2)    
    return k

n=0
while n<=19:
    m=0
#     print('----------')
#     print(n+1)
#     print('----------')
    while m<=4:
        x,y=point(df1.x[m],df1.y[m],df1.z[m],df.x[n],df.y[n],df.z[n],2.5)
#         print(x,y)
        f1.append(x)
        f2.append(y)
        m+=1
    n+=1

i=0
k=0

while k<100:
    i=0
    while i<100:
        x = round(distance(f1[k+4],f2[k+4],f1[i+4],f2[i+4]),4)
#         print(x)
        f3.append(x)
        i+=5
    k+=5

a=np.array(f3).reshape(20,20)
df4=pd.DataFrame(a)
df4
## 理想无人机轨迹图
import math
import pandas as pd 
import numpy as np
import matplotlib.pyplot as plt

df=pd.read_csv('qqq.csv')
df1=pd.read_csv('c.csv')
f1=[]
f2=[]
f3=[]
f4=[]

n=0

def point(x1,y1,z1,x2,y2,z2,z):
    x=0
    y=0
    k=(z-z1)/(z2-z1)
    x=round(x1+k*(x2-x1),4)
    y=round(y1+k*(y2-y1),4)
    return x,y

while n<=19:
    m=0
    while m<=4:
        x,y=point(df1.x[m],df1.y[m],df1.z[m],df.x[n],df.y[n],df.z[n],2)
        f1.append(x)
        f2.append(y)
        m+=1
    n+=1

n=0
while n<=19:
    m=0
    while m<=4:
        x,y=point(df1.x[m],df1.y[m],df1.z[m],df.x[n],df.y[n],df.z[n],2.5)
        f3.append(x)
        f4.append(y)
        m+=1
    n+=1

fig=plt.figure(221)
plt.xlabel('x',fontsize=12,labelpad=5)
plt.xlabel('y',fontsize=12,labelpad=5)
plt.scatter(f1,f2,c='red',s=5,alpha=1,label='z=2')
plt.scatter(f3,f4,c='blue',s=5,alpha=1,label='z=2.5')
plt.legend(loc='best',fontsize=12)
plt.grid(True)
plt.show()


##  基于分层的无人机蚁群算法
import numpy as np
import pandas as pd 
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import proj3d
from mpl_toolkits.mplot3d import Axes3D

 
height3d = pd.read_csv('qqq.csv')
 
fig = figure()
ax = Axes3D(fig)
X = np.arange(21)
Y = np.arange(21)
X, Y = np.meshgrid(X, Y)
Z = -20*np.exp(-0.2*np.sqrt(np.sqrt(((X-10)**2+(Y-10)**2)/2)))+20+np.e-np.exp((np.cos(2*np.pi*X)+np.sin(2*np.pi*Y))/2)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='cool')
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z')
ax.set_title('3D map')
 
 
point0 = [0,9,Z[0][9]] 
point1 = [20,7,Z[20][7]]
 
ax.plot([point0[0]],[point0[1]],[point0[2]],'r',marker = u'o',markersize = 15)
ax.plot([point1[0]],[point1[1]],[point1[2]],'r',marker = u'o',markersize = 15)
 
x0,y0,_ = proj3d.proj_transform(point0[0],point0[1],point0[2], ax.get_proj())
x1,y1,_ = proj3d.proj_transform(point1[0],point1[1],point1[2], ax.get_proj())
 
label = pylab.annotate(
  "start", 
  xy = (x0, y0), xytext = (-20, 20),
  textcoords = 'offset points', ha = 'right', va = 'bottom',
  bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 1),
  arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'),fontsize=15)
label2 = pylab.annotate(
  "end", 
  xy = (x1, y1), xytext = (-20, 20),
  textcoords = 'offset points', ha = 'right', va = 'bottom',
  bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 1),
  arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'),fontsize=15)
def update_position(e):
  x2, y2, _ = proj3d.proj_transform(point0[0],point0[1],point0[2],ax.get_proj())
  label.xy = x2,y2
  label.update_positions(fig.canvas.renderer)
 
  x1,y1,_ = proj3d.proj_transform(point1[0],point1[1],point1[2],ax.get_proj())
  label2.xy = x1,y1
  label2.update_positions(fig.canvas.renderer)
  fig.canvas.draw()
 
fig.canvas.mpl_connect('button_release_event', update_position)



def getdistmat(coordinates):
  num = coordinates.shape[0]
  distmat = np.zeros((52,52))
  for i in range(num):
    for j in range(i,num):
      distmat[i][j] = distmat[j][i]=np.linalg.norm(coordinates[i]-coordinates[j])
  return distmat
distmat = getdistmat(coordinates)
numant = 40 #蚂蚁个数
numcity = coordinates.shape[0] #无人机个数
alpha = 1  #信息素重要程度因子
beta = 5  #启发函数重要程度因子
rho = 0.1  
Q = 1
iter = 0
itermax = 250
etatable = 1.0/(distmat+np.diag([1e10]*numcity)) #启发函数矩阵,表示蚂蚁从无人机航迹i转移到无人机航迹j的期望程度
pheromonetable = np.ones((numcity,numcity))
pathtable = np.zeros((numant,numcity)).astype(int) 
distmat = getdistmat(coordinates) 
lengthaver = np.zeros(itermax) 
lengthbest = np.zeros(itermax) 
pathbest = np.zeros((itermax,numcity)) 
while iter < itermax:
  if numant <= numcity:
    pathtable[:,0] = np.random.permutation(range(0,numcity))[:numant]
  else: 
    pathtable[:numcity,0] = np.random.permutation(range(0,numcity))[:]
    pathtable[numcity:,0] = np.random.permutation(range(0,numcity))[:numant-numcity]
  length = np.zeros(numant) 
  for i in range(numant):
    visiting = pathtable[i,0] 
    #visited = set() 
    #visited.add(visiting) 
    unvisited = set(range(numcity))
    unvisited.remove(visiting) 
    for j in range(1,numcity):
      listunvisited = list(unvisited)
      probtrans = np.zeros(len(listunvisited))
      for k in range(len(listunvisited)):
        probtrans[k] = np.power(pheromonetable[visiting][listunvisited[k]],alpha)\
            *np.power(etatable[visiting][listunvisited[k]],alpha)
      cumsumprobtrans = (probtrans/sum(probtrans)).cumsum()
      cumsumprobtrans -= np.random.rand()
      k = listunvisited[find(cumsumprobtrans>0)[0]] 
      pathtable[i,j] = k
      unvisited.remove(k)
      #visited.add(k)
      length[i] += distmat[visiting][k]
      visiting = k
    length[i] += distmat[visiting][pathtable[i,0]]

  lengthaver[iter] = length.mean()
  if iter == 0:
    lengthbest[iter] = length.min()
    pathbest[iter] = pathtable[length.argmin()].copy()   
  else:
    if length.min() > lengthbest[iter-1]:
      lengthbest[iter] = lengthbest[iter-1]
      pathbest[iter] = pathbest[iter-1].copy()
    else:
      lengthbest[iter] = length.min()
      pathbest[iter] = pathtable[length.argmin()].copy()  

  changepheromonetable = np.zeros((numcity,numcity))
  for i in range(numant):
    for j in range(numcity-1):
      changepheromonetable[pathtable[i,j]][pathtable[i,j+1]] += Q/distmat[pathtable[i,j]][pathtable[i,j+1]]
    changepheromonetable[pathtable[i,j+1]][pathtable[i,0]] += Q/distmat[pathtable[i,j+1]][pathtable[i,0]]
  pheromonetable = (1-rho)*pheromonetable + changepheromonetable
  iter += 1
  if (iter-1)%20==0: 
    print iter-1 
fig,axes = plt.subplots(nrows=2,ncols=1,figsize=(12,10))
axes[0].plot(lengthaver,'k',marker = u'')
axes[0].set_title('Average Length')
axes[0].set_xlabel(u'iteration')
axes[1].plot(lengthbest,'k',marker = u'')
axes[1].set_title('Best Length')
axes[1].set_xlabel(u'iteration')
fig.savefig('Average_Best.png',dpi=500,bbox_inches='tight')
plt.close()
bestpath = pathbest[-1]
plt.plot(coordinates[:,0],coordinates[:,1],'r.',marker=u'$\cdot$')
plt.xlim([-100,2000])
plt.ylim([-100,1500])
for i in range(numcity-1):#
  m,n = bestpath[i],bestpath[i+1]
  print m,n
  plt.plot([coordinates[m][0],coordinates[n][0]],[coordinates[m][1],coordinates[n][1]],'k')
plt.plot([coordinates[bestpath[0]][0],coordinates[n][0]],[coordinates[bestpath[0]][1],coordinates[n][1]],'b')
ax=plt.gca()
ax.set_title("Best Path")
ax.set_xlabel('X axis')
ax.set_ylabel('Y_axis')
plt.savefig('Best Path.png',dpi=500,bbox_inches='tight')
plt.close()
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,825评论 4 377
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,887评论 2 308
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,425评论 0 255
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,801评论 0 224
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,252评论 3 299
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,089评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,216评论 2 322
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 31,005评论 0 215
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,747评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,883评论 2 255
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,354评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,694评论 3 265
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,406评论 3 246
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,222评论 0 9
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,996评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,242评论 2 287
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 36,017评论 2 281

推荐阅读更多精彩内容