-
Notifications
You must be signed in to change notification settings - Fork 2
/
nodeResults.py
executable file
·146 lines (108 loc) · 4.46 KB
/
nodeResults.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#testModel
from shared import *
import numpy,time,scipy.optimize,latinHypercube,travellingPlane,dubinPath,plot
SAMPLE_CUBE = latinHypercube.sampleSpace([MAX_LENGTH,MAX_LENGTH,MAX_LENGTH],100)
numberI = 4
SAMPLE_PLAN = [list(SAMPLE_CUBE[i//numberI])+[0.1*int((i+1)-numberI*(i//numberI))] for i in range(len(SAMPLE_CUBE)*numberI)]
# print(SAMPLE_PLAN)
def returnNodes(alpha,xLength,yLength,zLength,totalEnergy):
"""using the calculated models this function takes the:
Research volume as defined by xLength,yLength and zLength
The totalEnergy that the UAV batteries contain
And returns the number of nodes in the optimal latin hypercube"""
#define the sample plan
[xLengths,yLengths,zLengths,alphas] = changeArray(SAMPLE_PLAN)
#define all alphas that are close
alphaIndexs = [alphas.index(item) for item in alphas if round(item,1)==round(alpha,1)]
diffList = []
for index in alphaIndexs:
xDiff = abs(xLength - xLengths[index])
yDiff = abs(yLength - yLengths[index])
zDiff = abs(zLength - zLengths[index])
diffList.append(sum([xDiff,yDiff,zDiff]))
minDiff = min(diffList)
index = alphaIndexs[diffList.index(minDiff)]
print("------------")
print("Desired:\t{:0.0f}\t{:0.0f}\t{:0.0f}".format(xLength,yLength,zLength))
xLength,yLength,zLength = xLengths[index],yLengths[index],zLengths[index]
print("Actual:\t\t{:0.0f}\t{:0.0f}\t{:0.0f}".format(xLength,yLength,zLength))
data = computeResults(alpha,xLength,yLength,zLength)
# energys = [calculateEnergy(data["distance"][i]-data["glideDistance"][i],data["glideHeight"][i],alpha) for i in range(len(data["numberNode"]))]
# plot.line(data["numberNode"],{"Cost":data["cost"],"Energy":energys},show=True)
numberNode = 0
energyDiffs = []
for i in range(len(data["numberNode"])):
distance = data["distance"][i]-data["glideDistance"][i]
height = data["glideHeight"][i]
energy = calculateEnergy(distance,height,alpha)
energyDiff = totalEnergy - energy
if (energyDiff >= 0):
energyDiffs.append(energyDiff)
else:
energyDiffs.append(numpy.infty)
minEnergyDiff = min(energyDiffs)
#LP = (9:35R + 0:90)LR
if (minEnergyDiff > 100):
return None
index = energyDiffs.index(minEnergyDiff)
return data["numberNode"][index]
def computeResults(alpha,xLength,yLength,zLength):
"function to fully investigate the resulting routes of a latin hypercube"
filename = "results/route_path_lengths_{:0.1f}_{:0.0f}_{:0.0f}_{:0.0f}.csv".format(alpha,xLength,yLength,zLength)
try:
data = loadData(filename)
except FileNotFoundError:
data = {"numberNode":[],"alpha":[],"xLength":[],"yLength":[],"zLength":[],"glideDistance":[],"glideHeight":[],"distance":[],"height":[],"cost":[]}
for numberNode in EVEN_RESULTS:
nodes = latinHypercube.sampleSpace([xLength,yLength,zLength],numberNode)
# print(len(nodes))
route,cost = travellingPlane.progressiveRoute(nodes,alpha)
# print(len(route))
orderedNodes = travellingPlane.orderNodes(nodes,route)
routeData = travellingPlane.routeData(orderedNodes,alpha)
data["numberNode"].append(numberNode)
data["alpha"].append(alpha)
data["xLength"].append(xLength)
data["yLength"].append(yLength)
data["zLength"].append(zLength)
for name in ["glideDistance","glideHeight","distance","height","cost"]:
data[name].append(routeData[name])
saveData(filename,data)
for key,value in data.items():
data[key] = numpy.array(value)
return data
def computeAllModelResults(newSample=False):
"function to compute all length width height results possible"
filename = "sample_plan.csv"
if newSample:
[xLengths,yLengths,zLengths,alphas] = changeArray(SAMPLE_PLAN)
data = {
"alpha":alphas,
"xLength":xLengths,
"yLength":yLengths,
"zLength":zLengths,
}
saveData(filename,data)
while not newSample:
data = loadData(filename)
alpha = data["alpha"].pop(0)
xLength = data["xLength"].pop(0)
yLength = data["yLength"].pop(0)
zLength = data["zLength"].pop(0)
try:
saveData(filename,data)
try:
print(alpha,xLength,yLength,zLength)
computeResults(alpha,xLength,yLength,zLength)
except:
data["alpha"].insert(0,alpha)
data["xLength"].insert(0,xLength)
data["yLength"].insert(0,yLength)
data["zLength"].insert(0,zLength)
saveData(filename,data)
except PermissionError:
time.sleep(20)
if (len(data["alpha"]) < 1):
break
if (__name__ == "__main__"):
computeAllModelResults(False)