forked from henryzhangsta/elite-trade-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspark.py
executable file
·188 lines (146 loc) · 7.77 KB
/
spark.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from pyspark import SparkContext, SparkConf, StorageLevel
from CSVLoader import CSVLoader
import re, os, uuid, datetime, json, argparse, math
SystemsBlacklist = [
'LHS 3447'
]
def Dedupe(a, b):
return a
def DistanceBetweenSystems(a, b):
return math.sqrt((a['SystemX'] - b['SystemX']) ** 2 + (a['SystemY'] - b['SystemY']) ** 2 + (a['SystemZ'] - b['SystemZ']) ** 2)
def Main(maxJumpDistance, currentSystemName, searchradius):
conf = SparkConf().setMaster('local[8]')
sc = SparkContext(conf=conf)
systems = CSVLoader('data/headers/System.hcsv').loadMap(sc, 'data/csv/System.csv')
stationCommodities = CSVLoader('data/headers/SC.hcsv').loadMap(sc, 'data/csv/SC.csv')
commodities = CSVLoader('data/headers/Commod.hcsv').loadMap(sc, 'data/csv/Commod.csv')
def MapSystems(a):
del a['SystemSize']
del a['SystemId']
a['SystemX'] = float(a['SystemX'])
a['SystemY'] = float(a['SystemY'])
a['SystemZ'] = float(a['SystemZ'])
return a
systems = systems.map(MapSystems).filter(lambda x: x['SystemName'] not in SystemsBlacklist)
if currentSystemName:
currentSystem = systems.filter(lambda x: x['SystemName'] == currentSystemName).collect()[0]
systems = systems.filter(lambda x: DistanceBetweenSystems(currentSystem, x) < searchradius)
systemsWithStations = sc.broadcast(stationCommodities.map(lambda x: x['SCStationSystem']).distinct().collect())
systems = systems.filter(lambda x: x['SystemName'] in systemsWithStations.value)
def CalcSystemDistance(pair):
return {
'Distance': DistanceBetweenSystems(pair[0], pair[1]),
'Systems': pair
}
maxJumpDistance = sc.broadcast(maxJumpDistance)
systemPairs = systems.cartesian(systems).map(CalcSystemDistance).filter(lambda x: x['Distance'] < maxJumpDistance.value)
def TokenizeSystem(systemPair):
a = systemPair['Systems'][0]['SystemName']
b = systemPair['Systems'][1]['SystemName']
token = min(a, b) + '__' + max(a, b)
return (token, systemPair)
systemPairs = systemPairs.map(TokenizeSystem).reduceByKey(Dedupe).map(lambda x: x[1])
inrangeSystems = sc.broadcast(systems.map(lambda x: x['SystemName']).distinct().collect())
stationCommodities = stationCommodities.filter(lambda a: a['SCStationSystem'] in inrangeSystems.value)
def StationCommodityMap(a):
return (a['SCStationSystem'], {
'Station': a['SCStationName'],
'Commodity': a['SCStationCommod'],
'BuyPrice': int(a['SCStationPrice']),
'SellPrice': int(a['SCStationSell']),
'Stock': int(float(a['SCStationStock']))
})
def StationMap(a):
results = {}
for stationcommod in a[1]:
if stationcommod['Station'] in results:
results[stationcommod['Station']].append(stationcommod)
else:
results[stationcommod['Station']] = [stationcommod]
return (a[0], results)
stationCommoditiesTable = {system[0]: system[1] for system in stationCommodities.map(StationCommodityMap).groupByKey().map(StationMap).collect()}
stationCommoditiesTable = sc.broadcast(stationCommoditiesTable)
def MapSystemPairToSystems(pair):
pair['Systems'] = ({
'Name': pair['Systems'][0]['SystemName'],
'Stations': stationCommoditiesTable.value[pair['Systems'][0]['SystemName']]
},
{
'Name': pair['Systems'][1]['SystemName'],
'Stations': stationCommoditiesTable.value[pair['Systems'][1]['SystemName']]
})
return pair
systemPairs = systemPairs.map(MapSystemPairToSystems)
def GetStationPairs(syspair):
origin_sys = syspair[0]['Stations']
dest_sys = syspair[1]['Stations']
station_pairs = []
for origin_station, origin_commods in origin_sys.iteritems():
for dest_station, dest_commods in dest_sys.iteritems():
station_pairs.append(((origin_station, origin_commods), (dest_station, dest_commods)))
return station_pairs
def BestCommodityTrade(sellingCommodities, buyingCommodities):
a_max_profit = 0
a_max_price = 0
a_max_profit_commodity = None
for k, v in sellingCommodities.iteritems():
profit = buyingCommodities[k]['SellPrice'] - v['BuyPrice']
if profit > a_max_profit or (profit == a_max_profit and v['BuyPrice'] < a_max_price):
a_max_profit = profit
a_max_price = v['BuyPrice']
a_max_profit_commodity = k
return {
'Profit': a_max_profit,
'BuyPrice': a_max_price,
'Commodity': a_max_profit_commodity
} if a_max_profit_commodity != None else None
def BestSystemRoute(pair):
station_pairs = GetStationPairs(pair['Systems'])
origin_sys = pair['Systems'][0]['Name']
dest_sys = pair['Systems'][1]['Name']
trade_pairs = []
for stations in station_pairs:
station_a_commodities = {a['Commodity']: a for a in stations[0][1]}
station_b_commodities = {a['Commodity']: a for a in stations[1][1]}
common_commodities = set(station_a_commodities.keys()).intersection(station_b_commodities.keys())
station_a_sold = {k: station_a_commodities[k] for k in common_commodities if station_a_commodities[k]['BuyPrice'] > 0 and station_a_commodities[k]['Stock'] > 0}
station_b_sold = {k: station_b_commodities[k] for k in common_commodities if station_b_commodities[k]['BuyPrice'] > 0 and station_b_commodities[k]['Stock'] > 0}
station_a_trade = BestCommodityTrade(station_a_sold, station_b_commodities)
station_b_trade = BestCommodityTrade(station_b_sold, station_a_commodities)
if station_a_trade != None and station_b_trade != None:
trade_pairs.append(({
'System': origin_sys,
'Station': stations[0][0],
'Trade': station_a_trade
}, {
'System': dest_sys,
'Station': stations[1][0],
'Trade': station_b_trade
}))
if len(trade_pairs) > 0:
max_profit = 0
best_trade = None
for trade_pair in trade_pairs:
total_profit = trade_pair[0]['Trade']['Profit'] + trade_pair[1]['Trade']['Profit']
if total_profit > max_profit:
max_profit = total_profit
best_trade = trade_pair
return {
'Distance': pair['Distance'],
'Trade': best_trade,
'Profit': max_profit
}
else:
return None
routes = systemPairs.map(BestSystemRoute).filter(lambda a: a != None).map(lambda x: (x['Profit'], x)).sortByKey(False).map(lambda x: x[1]).collect()
open('output/routes.json', 'w').write(json.dumps(routes))
routes = routes[:50]
for route in routes:
print route
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Determine best trade routes between locations')
parser.add_argument('maxjumpdistance', type=float, help='Maximum single jump distance', nargs='?', default=30)
parser.add_argument('--currentsystem', type=str, help='Current system for filtering systems based on range', default=None)
parser.add_argument('--searchradius', type=float, help='Search radius around current system in light years', default=100)
args = parser.parse_args()
Main(args.maxjumpdistance, args.currentsystem, args.searchradius)