-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data.py
63 lines (52 loc) · 2.02 KB
/
Data.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
import numpy as np
def get_distance(p1 , p2 ): # manhattan distance , modify as per use
return abs( p1[0] - p2[0] ) + abs( p1[1] - p2[1] )
class DataCVRP :
points = []
depot = []
num_vehicles = 0
vehicle_cap = []
demands = []
def __init__(self , _num_vehicles , _depot , locations , _vehicle_cap , _demands ):
self.points = locations # [ [x , y] , [x , y] .. ]
self.depot = _depot # [ [ x , y ] ]
self.demands = _demands
self.vehicle_cap = _vehicle_cap
self.num_vehicles = _num_vehicles
def merge(self , a , b ):
return a + b
def generate_distance_matrix(self , locations , F ):
n = len(locations)
res = np.zeros( shape = (n , n) )
for i in range( n ):
for j in range( n ):
if( i != j ): res[i][j] = F( locations[i] , locations[j] )
return res
def create_data_model(self , F = get_distance ):
locations = self.merge( self.depot , self.points )
distance_matrix = self.generate_distance_matrix( locations , F )
data = {}
data['distance_matrix'] = distance_matrix
data['demands'] = self.merge( [0] , self.demands)
data['vehicle_capacities'] = self.vehicle_cap
data['num_vehicles'] = self.num_vehicles
data['depot'] = 0 # at idx 0 in locations
return data
class DataBinPacking :
weights = []
values = []
bin_capacities = []
def __init__( self , _weights , _values , _bin_capacities ):
self.weights = _weights
self.values = _values
self.bin_capacities = _bin_capacities
def create_data_model(self):
data = {}
data['num_items'] = len( self.weights )
data['all_items'] = range( data['num_items'] )
data['weights'] = self.weights
data['values'] = self.values
data['bin_capacities'] = self.bin_capacities
data['num_bins'] = len( data['bin_capacities'] )
data['all_bins'] = range( data['num_bins'] )
return data