Skip to content

Commit

Permalink
Including Euclidean Distance Initial Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel301 committed Oct 28, 2019
1 parent 7d42a4e commit 71de125
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 22 deletions.
8 changes: 7 additions & 1 deletion Week 6 - Facility Location/facility/EnumSettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ class ImprovementType(Enum):
class SolvingParadigm(Enum):
MIP = "MIP"
Heuristic = "Heuristic"
Hybrid = "Hybrid"
Hybrid = "Hybrid"

# Enum to choose the initial solution function
class InitialSolutionFunction(Enum):
Radius = "Radius"
Euclidean = "Euclidean"
Manhatan = "Manhatan"
2 changes: 1 addition & 1 deletion Week 6 - Facility Location/facility/MIP.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def createModel(self):

print("Creating Objective Function...")
#Objective Function
self.model.setObjective(quicksum(self.varFacilityAssignment[facility.index]*facility.setup_cost for facility in self.facilities) + quicksum(Preprocessing.length(facility.location,customer.location)*self.varCustomerAssignment[facility.index,customer.index] for facility in self.facilities for customer in self.customers),"minimize")
self.model.setObjective(quicksum(self.varFacilityAssignment[facility.index]*facility.setup_cost for facility in self.facilities) + quicksum(Preprocessing.getEuclideanDistance(facility.location,customer.location)*self.varCustomerAssignment[facility.index,customer.index] for facility in self.facilities for customer in self.customers),"minimize")
self.model.data = self.varFacilityAssignment, self.varCustomerAssignment

def optimize(self,timeLimit):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from Util import Util
from EnumSettings import Strategy,ImprovementType,SolvingParadigm
from EnumSettings import Strategy,ImprovementType,SolvingParadigm,InitialSolutionFunction

class ParametersConfiguration:
INITIAL_FACILITIES_BY_SUBPROBLEM = 5 #Maximum desired number of facilities 'in' the first cluster
INITIAL_FACILITIES_BY_SUBPROBLEM = 10 #Maximum desired number of facilities 'in' the first cluster

def __init__(self,facilityCount,instanceSize):
self.instanceSize = instanceSize
Expand Down Expand Up @@ -55,7 +55,7 @@ def __DefaultSetup(self,instanceSize):
self.params["strategy"] = Strategy.Default
self.params["paradigm"] = SolvingParadigm.MIP
self.params["quantile_intervals"] = self.__getQuantilesIntervals()

self.params["initialSolutionFunction"] = InitialSolutionFunction.Euclidean

def __AlphaSetup(self,instanceSize):
self.__DefaultSetup(instanceSize)
Expand Down
9 changes: 7 additions & 2 deletions Week 6 - Facility Location/facility/Preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Preprocessing:

#Calculate the Euclidean distance between two points
@staticmethod
def length(point1, point2):
def getEuclideanDistance(point1, point2):
return math.sqrt((point1.x - point2.x)**2 + (point1.y - point2.y)**2)

#Return quantiles for distances between facilities
Expand All @@ -18,7 +18,7 @@ def getDistanceQuantiles(facilities,intervals):
for f2 in facilities:
if f1.index == f2.index:
continue
distances.append(Preprocessing.length(f1.location,f2.location))
distances.append(Preprocessing.getEuclideanDistance(f1.location,f2.location))
f1.distance_quantiles.extend(np.quantile(a=distances,q=intervals, interpolation='midpoint').tolist())

#Return quantiles for distances between facilities
Expand All @@ -35,5 +35,10 @@ def getFacilityClusters(facilities,numberClusters):

return clusters

#Calculate the Manhatan distance between two points
@staticmethod
def getManhatanDistance(p1,p2):
return math.fabs(p1.x-p2.x) + math.fabs(p1.y-p2.y)



4 changes: 2 additions & 2 deletions Week 6 - Facility Location/facility/Tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ def setRoot(self,newRoot):

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

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

def clearNodes(self):
Expand Down
4 changes: 0 additions & 4 deletions Week 6 - Facility Location/facility/Util.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,3 @@ def filterbyThreshold(values,threshold,iteration):
result = [i for i in range(0,size) if Util.truncate(values[i]/iteration,3) >= Util.truncate(threshold,3)]
return result

#Return an array with indexes greater or equal than a threshold
@staticmethod
def getManhatanDistance(p1,p2):
return math.fabs(p1.x-p2.x) + math.fabs(p1.y-p2.y)
22 changes: 13 additions & 9 deletions Week 6 - Facility Location/facility/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from Util import Util
from MIP import MIP
from ParametersConfiguration import ParametersConfiguration
from EnumSettings import Strategy,ImprovementType,SolvingParadigm
from EnumSettings import Strategy,ImprovementType,SolvingParadigm,InitialSolutionFunction
from Preprocessing import Preprocessing
from Forest import Forest
from LNS import LNS
Expand Down Expand Up @@ -41,11 +41,10 @@ def getTrivialInitialSolution(facilities,customers):
# calculate the cost of the solution
obj = sum([f.setup_cost*used[f.index] for f in facilities])
for customer in customers:
obj += Preprocessing.length(customer.location, facilities[solution[customer.index]].location)
obj += Preprocessing.getEuclideanDistance(customer.location, facilities[solution[customer.index]].location)
return obj,solution

def getGreedyInitialSolution(facilities,customers,clusters):

def getRadiusDistanceInitialSolution(facilities,customers,clusters):
customersToBeAssigned = {}
customersAssigned = []
assigments = []
Expand Down Expand Up @@ -84,8 +83,7 @@ def getGreedyInitialSolution(facilities,customers,clusters):

return assigments

def getManhatanDistanceInitialSolution(facilities,customers):

def getNearestNeighbourInitialSolution(facilities,customers,distanceType):
customersToBeAssigned = {}
assigments = []
customersAssigned = []
Expand All @@ -98,7 +96,11 @@ def getManhatanDistanceInitialSolution(facilities,customers):
minDistanceIndex = -1
currMinDistance = float("inf")
for facility in facilities:
currDistance = Util.getManhatanDistance(facility.location,customers[customerIndex].location)
if(distanceType == InitialSolutionFunction.Manhatan):
currDistance = Preprocessing.getManhatanDistance(facility.location,customers[customerIndex].location)
else:
currDistance = Preprocessing.getEuclideanDistance(facility.location,customers[customerIndex].location)

if currDistance < currMinDistance and remainingCapacity[facility.index] >= customers[customerIndex].demand:
currMinDistance = currDistance
minDistanceIndex = facility.index
Expand Down Expand Up @@ -175,8 +177,10 @@ def solve_it(input_data):

Preprocessing.getDistanceQuantiles(facilities,params["quantile_intervals"])
clusterAreas = getClusters(facilities,params["quantile_intervals"])
#initialSolution = getGreedyInitialSolution(facilities,customers,clusterAreas.get(0))
initialSolution = getManhatanDistanceInitialSolution(facilities,customers)
if(params["initialSolutionFunction"] == InitialSolutionFunction.Radius):
initialSolution = getRadiusDistanceInitialSolution(facilities,customers,clusterAreas.get(0))
else:
initialSolution = getNearestNeighbourInitialSolution(facilities,customers,params["initialSolutionFunction"])

search = LNS(Util.formatSolutionFromMIP(initialSolution),facilities,customers,params["improvementType"],clusterAreas,params["quantile_intervals"],params["mipTimeLimit"])
obj,assignments = search.optimize()
Expand Down

0 comments on commit 71de125

Please sign in to comment.