Skip to content

Commit

Permalink
Creating Data Structures
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel301 committed Oct 24, 2019
1 parent 56c551b commit 13aa076
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 1 deletion.
15 changes: 15 additions & 0 deletions Week 6 - Facility Location/facility/Clock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import time
import datetime
# Class to help to monitor the runtimes of the algorithm
class Clock():
def __init__ (self):
self.start = None

def isTimeOver(self,end,duration):
return end-self.start >= duration

def setStart(self,start):
self.start = start

def getStart(self):
return self.start
41 changes: 41 additions & 0 deletions Week 6 - Facility Location/facility/Forest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from Tree import Tree

class Forest:
Trees = {}
TotalCost = 0
TreesCount = 0
TotalNodes = 0

def addTree(self,tree):
if tree.getRoot().index in self.Trees.keys():
self.removeTree(tree)

self.Trees[tree.getRoot().index] = tree
self.TotalCost = self.TotalCost + tree.getCost()
self.TreesCount = self.TreesCount + 1
self.TotalNodes = self.TotalNodes + len(tree.getNodes())

def removeTree(self,tree):
if tree.getRoot().index in self.Trees.keys():
self.TotalCost = self.TreesCount - self.Trees[tree.getRoot().index].getCost()
self.TotalNodes = self.TotalNodes - len(self.Trees[tree.getRoot().index].getNodes())
self.Trees.pop(tree.getRoot().index,None)
self.TreesCount = self.TreesCount - 1

def buildForestFromArray(self,assignments,facilities,customers):
size = len(assignments)
for index in range(0,size):
if(assignments[index] not in self.Trees.keys()):
self.Trees[assignments[index]] = Tree(facilities[assignments[index]])

self.Trees[assignments[index]].addNode(customers[index])


def getAssignmentsArray(self):
assignments = [-1]*self.TotalNodes
for tree in self.Trees:
for node in tree.getNodes():
assignments[node.index] = tree.getRoot().index

return assignments

37 changes: 37 additions & 0 deletions Week 6 - Facility Location/facility/Tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from Util import Util

class Tree:
Root = None
Nodes = {}
Cost = 0

def __init__(self,treeRoot):
self.setRoot(treeRoot)

def setRoot(self,newRoot):
if self.Root is not None:
self.Cost = self.Cost - self.Root.setup_cost
self.Root = newRoot
self.Cost = self.Cost + self.Root.setup_cost

def addNode(self,node):
self.Nodes[node.index] = node
self.Cost = self.Cost + Util.length(self.Root.location,node.location)

def removeNode(self,node):
if node.index in self.Nodes.keys():
self.Cost = self.Cost - Util.length(self.Root.location,node.location)
self.Nodes.pop(node.index, None)

def clearNodes(self):
self.Nodes.clear()
self.cost = self.Cost = self.Cost + self.Root.setup_cost

def getRoot(self):
return self.Root

def getCost(self):
return self.Cost

def getNodes(self):
return self.Nodes
2 changes: 2 additions & 0 deletions Week 6 - Facility Location/facility/Util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import math
import time
import datetime

class Util:

Expand Down
9 changes: 8 additions & 1 deletion Week 6 - Facility Location/facility/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from MIP import MIP
from ParametersConfiguration import ParametersConfiguration
from EnumSettings import Strategy,ImprovementType,SolvingParadigm
import time
import datetime

Point = namedtuple("Point", ['x', 'y'])
Facility = namedtuple("Facility", ['index', 'setup_cost', 'capacity', 'location'])
Expand Down Expand Up @@ -39,7 +41,8 @@ def getGreedyInitialSolution(facilities,customers):
return obj,solution

def solve_it(input_data):
# Modify this code to run your optimization algorithm
start = time.time()
print("Start DateTime: {}".format(datetime.datetime.now()))

# parse the input
lines = input_data.split('\n')
Expand Down Expand Up @@ -80,6 +83,10 @@ def solve_it(input_data):
output_data = '%.2f' % obj + ' ' + str(0) + '\n'
output_data += ' '.join(map(str,assignments))

end = time.time()
hours, minutes, seconds = Util.getIntervalDuration(start,end)
print("End DateTime: {}".format(datetime.datetime.now()))
print("EXECUTION TIME: {:0>2}:{:0>2}:{:05.2f}s".format(int(hours),int(minutes),seconds))
return output_data


Expand Down

0 comments on commit 13aa076

Please sign in to comment.