-
Notifications
You must be signed in to change notification settings - Fork 0
/
Optimize.py
32 lines (27 loc) · 1.22 KB
/
Optimize.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
import numpy as np
from DataPoints import *
import PointCollection
from Segmentize import get_segments
def generateWeightageMatrix(points, segments):
m = np.zeros((len(segments), datatypes + 1))
for point in points:
for i in range(0, len(segments)):
m[i][point.type] += point.time_penalty(segments[i])
for i in range(0, len(segments)):
for j in range(0, datatypes):
m[i][j] = subclassList[j].weightage(m[i][j])
m[i][datatypes] = 1
return m
def findBestTheta(points, segs):
""" Uses vectorized gradient descent to find the best value of the over determined matrix"""
gradients = np.transpose(np.matrix([segment.grad for segment in segs]))
alpha = 0.001
theta = np.zeros((datatypes + 1, 1))
m = generateWeightageMatrix(points, segs)
delta = (1 / len(segs)) * np.matmul(np.transpose(m), (np.matmul(m, theta) - gradients))
while np.sum(np.matmul(delta, np.transpose(delta))) > 0.001:
theta = theta - alpha * delta
delta = (1 / len(segs)) * np.matmul(np.transpose(m), (np.matmul(m, theta) - gradients))
diff = (numpy.matmul(m, theta) - gradients)
print("ERROR: " + str(numpy.sum(numpy.matmul(diff.transpose(), diff))))
return theta