-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBruteForce.py
115 lines (100 loc) · 3.86 KB
/
BruteForce.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 11 14:15:38 2021
@author: 200461
"""
from Route import Route
from Permutation import Permutation
import numpy as np
from Settings import Settings
class BruteForce(object):
def __init__(self, mgr):
self.routeList = list()
self.clusters = None
self.permObj = Permutation()
self.processor = None
self.mgr = mgr
self.mgr.routeList = self.routeList
def createBruteRoutes(self):
"""
Creates all possible routes for (hard coded) 3 vehicles and 7 jobs where all jobs needs to be completed,
vehicles with no job assigned are welcome
TODO: Enhance this method to work for n jobs and m vehicles
"""
self.clusters = self.__createClusters()
for cluster in self.clusters:
v1 = cluster[0]
v2 = cluster[1]
v3 = cluster[2]
v1Perms = self.permObj.getPermutation(v1, [])
for v1Route in v1Perms:
v2Perms = self.permObj.getPermutation(v2, v1Route)
for v2Route in v2Perms:
v3Perms = self.permObj.getPermutation(
v3, (v1Route + v2Route))
for v3Route in v3Perms:
route = Route()
route.routes[1] = v1Route
route.routes[2] = v2Route
route.routes[3] = v3Route
self.routeList.append(route)
def findMinRoute(self):
for route in self.routeList:
self.__calc_route_cost(route)
minroute = self.__find_min_route()
return minroute
def __calc_route_cost(self, route):
job_objects = self.mgr.job_objects
vehicle_objects = self.mgr.vehicle_objects
matrix = self.mgr.matrix
for vehicle in vehicle_objects.values():
vehicle_location = vehicle.location
vehicle_id = vehicle.vehicle_id
vehicle_jobs = route.routes[vehicle_id]
vehicle_stock = vehicle.stock
for job_id in vehicle_jobs:
job = job_objects[job_id]
target_location = job.location_index
service = job.service
delivery = job.delivery
cost = matrix[vehicle_location, target_location]
if Settings.STOCK_CONSTRAINT and vehicle_stock < delivery:
cost = float('inf')
route.addCost(cost)
break
vehicle_stock -= delivery
route.addCost(cost)
vehicle_location = target_location
def __find_min_route(self):
mincost = -1
minroute = None
for route in self.routeList:
if mincost < 0 or route.cost < mincost:
mincost = route.cost
minroute = route
return minroute
def __createClusters(self):
"""
Creates the reference data with all possible number of jobs
could be assigned to each vehicle
I.E: [4, 1, 2] ==> 4,1 and 2 jobs will be assigned to Vehicle-1, Vehicle-2 and Vehicle-3 respectively
TODO: Enhance this method to work for n jobs and m vehicles
"""
tmp = list()
for i in reversed(range(8)):
for j in range(0, 8-i):
k = 7 - (i+j)
tmp.append([i, j, k])
return tmp
# [7, 0, 0] [3, 2, 2] [1, 3, 3]
# [6, 0, 1] [3, 3, 1] [1, 4, 2]
# [6, 1, 0] [3, 4, 0] [1, 5, 1]
# [5, 0, 2] [2, 0, 5] [1, 6, 0]
# [5, 1, 1] [2, 1, 4] [0, 0, 7]
# [5, 2, 0] [2, 2, 3] [0, 1, 6]
# [4, 0, 3] [2, 3, 2] [0, 2, 5]
# [4, 1, 2] [2, 4, 1] [0, 3, 4]
# [4, 2, 1] [2, 5, 0] [0, 4, 3]
# [4, 3, 0] [1, 0, 6] [0, 5, 2]
# [3, 0, 4] [1, 1, 5] [0, 6, 1]
# [3, 1, 3] [1, 2, 4] [0, 7, 0]