forked from gabriel301/DiscreteOptimizationCoursera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.py
66 lines (53 loc) · 2.04 KB
/
Util.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
import math
import time
import datetime
class Util:
#Format an assignment solved by MIP strategy to output correctly
@staticmethod
def formatSolutionFromMIP(assignments):
solutionDict = {}
for (facility,customer) in assignments:
solutionDict[customer] = facility
size = len(solutionDict)
solution = [solutionDict[i] for i in range(0,size)]
return solution
@staticmethod
#Get a Dictionary with the MIP Solution
def getDictSolutionFromMIP(assignments):
solutionDict = {}
for (facility,customer) in assignments:
solutionDict[customer] = facility
return solutionDict
# Return the time from hour, second and secods to seconds
@staticmethod
def getTimeInSeconds(hours,minutes,seconds):
return (((hours*3600)+(minutes*60)+seconds))
# Return the time interval between start and end (both in seconds) in hour, minute and second
@staticmethod
def getIntervalDuration(start,end):
hours, rem = divmod(end-start, 3600)
minutes, seconds = divmod(rem, 60)
return int(hours),int(minutes),seconds
#Return whether a point is inside a circle
@staticmethod
def isInsideCircle(center,radius,point):
EPS = 1.e-6
dx = point.x - center.x
dy = point.y - center.y
diameter = radius*radius
euc = (dx * dx) + (dy*dy)
return euc-diameter <= EPS
#Truncates/pads a float f to n decimal places without rounding
@staticmethod
def truncate(f, n):
s = '{}'.format(f)
if 'e' in s or 'E' in s:
return float('{0:.{1}f}'.format(f, n))
i, p, d = s.partition('.')
return float('.'.join([i, (d+'0'*n)[:n]]))
#Return an array with indexes greater or equal than a threshold
@staticmethod
def filterbyThreshold(values,threshold,iteration):
size = len(values)
result = [i for i in range(0,size) if Util.truncate(values[i]/iteration,3) >= Util.truncate(threshold,3)]
return result