-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeatureExtractor.py
149 lines (130 loc) · 5.41 KB
/
featureExtractor.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
147
import numpy as np
from System import System
from Force import Load
import itertools
import math
import json
from graphics import *
class FeatureExtractorUtil:
def __init__(self):
pass
def extractFeatures(self, structure):
if structure == {}:
return [0,0,0,0]
else:
features = [self.totalLengthFeature(structure),
self.averageAngle(structure),
self.pointDistribution(structure),
self.averageDisplacement(structure)]
return features
def extractTargets(self, structure):
return self.maxDisplacement(structure)
def totalLengthFeature(self, structure):
totalLength = 0
for key in structure:
nodeA, nodeB = structure[key]
totalLength += np.sqrt(pow(nodeB[0]-nodeA[0], 2) + pow(nodeB[1]-nodeA[1], 2))
return totalLength
def averageAngle(self, structure):
totalAngle = 0
numAngles = 0
#print(structure)
#construct a graph from the structure
vertices = {}
for key in structure:
if not tuple(structure[key][0]) in vertices:
vertices[tuple(structure[key][0])] = []
if not tuple(structure[key][1]) in vertices:
vertices[tuple(structure[key][1])] = []
for key in structure:
#changed the edges from a list to tuples
vertices[tuple(structure[key][0])].append((tuple(structure[key][0]), tuple(structure[key][1])))
vertices[tuple(structure[key][1])].append((tuple(structure[key][0]), tuple(structure[key][1])))
for vertex in vertices:
#print("vertex {}".format(vertex))
edges = vertices[vertex]
#print("edges {}".format(edges))
angleEnds = list(itertools.combinations(edges, 2))
#print("angle ends {}".format(angleEnds))
for pair in angleEnds:
if pair[0][0] == vertex:
vecA = np.asarray(pair[0][1]) - np.asarray(vertex)
else:
vecA = np.asarray(pair[0][0]) - np.asarray(vertex)
if pair[1][0] == vertex:
vecB = np.asarray(pair[1][1]) - np.asarray(vertex)
else:
vecB = np.asarray(pair[1][0]) - np.asarray(vertex)
magA = np.sqrt((vecA[0])**2+(vecA[1])**2)
magB = np.sqrt((vecB[0])**2+(vecB[1])**2)
totalAngle += np.arccos(np.dot(vecA,vecB)/(magA*magB))
numAngles += 1
totalAngle = totalAngle / 2
if numAngles == 0:
return 0
else:
averageAngle = totalAngle / numAngles
averageAngle = math.degrees(averageAngle)
#print("averageAngle {}".format(averageAngle))
return averageAngle
def pointDistribution(self, structure):
minX = 0
points = []
for key in structure:
points.append(structure[key][0])
points.append(structure[key][1])
maxX = max([pos[0] for pos in points])
center = (maxX-minX)/2
difference = abs(sum(pos[0] > center for pos in points) - sum(pos[0] < center for pos in points))
return difference
def computeSolution(self, structure):
#print (structure)
nodes = []
for key in structure:
nodeA, nodeB = structure[key]
if nodeA not in nodes:
nodes.append(nodeA)
if nodeB not in nodes:
nodes.append(nodeB)
connections = [(nodes.index(structure[key][0]), nodes.index(structure[key][1])) for key in structure]
nodeLoad = 10000
loads = [Load(-1 * nodeLoad, 'y', idx) for idx in range(len(nodes))]
#print("Nodes {}".format(nodes))
maxRight = (0, 0)
maxIdx = 0
for idx in range(len(nodes)):
if nodes[idx][0] > maxRight[0]:
maxRight = nodes[idx]
maxIdx = idx
if nodes[idx][0] == maxRight[0]:
if nodes[idx][1] < maxRight[1]:
maxRight = nodes[idx]
maxIdx = idx
maxLeft = (float("inf"), float("inf"))
minIdx = 0
for idx in range(len(nodes)):
if nodes[idx][0] < maxLeft[0]:
maxLeft = nodes[idx]
minIdx = idx
if nodes[idx][0] == maxLeft[0]:
if nodes[idx][1] < maxLeft[1]:
maxLeft = nodes[idx]
minIdx = idx
#not a good way to fix this. think of a better way
if minIdx == maxIdx:
maxIdx+=1
fixedNodes = [minIdx, maxIdx]
#print("fixedNodes {}".format(fixedNodes))
#print(fixedNodes)
#print("structure {}".format(structure))
system = System(modulus=30e6, area=10, inertia=100, nodes=nodes, fixedNodes=fixedNodes, connectivity=connections,
loads=loads)
return system.computeDisplacements()
def averageDisplacement(self, structure):
solution = self.computeSolution(structure)
return sum(solution)/len(solution)
def maxDisplacement(self, structure):
return max(self.computeSolution(structure))
beam = {"beam0": [[0, 0], [4, 0]], "beam1": [[0, 0], [4, 4]], "beam2": [[4, 2], [2, 4]]}
f = FeatureExtractorUtil()
f.computeSolution({'beam0': ((0, 0), (4, 4)), 'beam1': ((4, 4), (0, 4)), 'beam2': ((0, 4), (0, 0))})