forked from gabriel301/DiscreteOptimizationCoursera
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56c551b
commit 13aa076
Showing
5 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import math | ||
import time | ||
import datetime | ||
|
||
class Util: | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters