plot3d.py
2.92 KB
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
import numpy as np
import matplotlib.pyplot as plt
#val = input("1:WoodYield/SoilLoss \n 2:WoodYield/RiskPercentile \n 3:SoilLoss/RiskPercentile \n 4:All three
f=open("paretoWSR.csv","r")
#lines=sorted(list(map(str, f.readlines())))
f2 =open("nonParetoWSR.csv","r")
#lines2 = sorted(list(map(str, f2.readlines())))
lines=f.readlines()
lines2 = f2.readlines()
paretoPoints = []
dominatedPoints = []
x = []
y = []
z = []
lst = []
scatterx = []
scattery = []
for i in lines:
result = i.split(',')
x.append(int(result[0]))
y.append(int(result[1]))
paretoPoints.append([int(result[0]),int(result[1]),int(result[2])])
f.close()
for i in lines2:
result = i.split(',')
x.append(int(result[0]))
y.append(int(result[1]))
z.append(int(result[2]))
if([int(result[0]),int(result[1]),int(result[2])] not in paretoPoints):
dominatedPoints.append([int(result[0]),int(result[1]),int(result[2])])
f2.close()
#print(dominatedPoints)
def simple_cull(inputPoints, dominates):
paretoPoints = set()
candidateRowNr = 0
dominatedPoints = set()
while True:
candidateRow = inputPoints[candidateRowNr]
inputPoints.remove(candidateRow)
rowNr = 0
nonDominated = True
while len(inputPoints) != 0 and rowNr < len(inputPoints):
row = inputPoints[rowNr]
if dominates(candidateRow, row):
# If it is worse on all features remove the row from the array
inputPoints.remove(row)
dominatedPoints.add(tuple(row))
elif dominates(row, candidateRow):
nonDominated = False
dominatedPoints.add(tuple(candidateRow))
rowNr += 1
else:
rowNr += 1
if nonDominated:
# add the non-dominated point to the Pareto frontier
paretoPoints.add(tuple(candidateRow))
if len(inputPoints) == 0:
break
return paretoPoints, dominatedPoints
def dominates(row, candidateRow):
return sum([row[x] >= candidateRow[x] for x in range(len(row))]) == len(row)
import random
inputPoints = [[random.randint(70,100) for i in range(3)] for j in range(500)]
#paretoPoints, dominatedPoints = simple_cull(inputPoints, dominates)
#print()
#print(paretoPoints)
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
dp = np.array(dominatedPoints)
pp = np.array(paretoPoints)
#print(dp)
ax.scatter(dp[:,0],dp[:,1],dp[:,2])
ax.scatter(pp[:,0],pp[:,1],pp[:,2],color='red')
import matplotlib.tri as mtri
triang = mtri.Triangulation(pp[:,0],pp[:,1])
ax.plot_trisurf(triang,pp[:,2],color='red')
ax.set_xlabel("Wood Yield",linespacing=5)
ax.set_ylabel("Soil Loss",linespacing=5)
ax.set_zlabel("Fire Risk Protection",linespacing=5)
ax.xaxis.labelpad=7
ax.yaxis.labelpad=7
ax.zaxis.labelpad=7
#plt.xlabel("Wood Yield")
#plt.ylabel("Soil Loss")
#plt.clabel("Fire Risk Protection")
plt.show()