Skip to content

Commit

Permalink
Revert "Some minor python perf tuning"
Browse files Browse the repository at this point in the history
This reverts commit f831f56.
  • Loading branch information
maddavo committed Apr 26, 2015
1 parent 5117425 commit fdc90d1
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 138 deletions.
8 changes: 4 additions & 4 deletions commands/run_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,7 @@ def routeFailedRestrictions(
places = list(
set(
chain.from_iterable(
(place,) if isinstance(place, Station) else place.stations
[place] if isinstance(place, Station) else place.stations
for place in restrictTo
)
)
Expand Down Expand Up @@ -1064,9 +1064,9 @@ def run(results, cmdenv, tdb):
startCr = cmdenv.credits - cmdenv.insurance
routes = [
Route(
stations=(src,),
hops=(),
jumps=(),
stations=[src],
hops=[],
jumps=[],
startCr=startCr,
gainCr=0,
score=0
Expand Down
143 changes: 68 additions & 75 deletions tradecalc.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ class NoHopsError(TradeException):
######################################################################
# Stuff that passes for classes (but isn't)

class TradeLoad(namedtuple('TradeLoad', (
class TradeLoad(namedtuple('TradeLoad', [
'items', 'gainCr', 'costCr', 'units'
))):
])):
"""
Describes the manifest of items to be exchanged in a
trade.
Expand Down Expand Up @@ -120,7 +120,7 @@ def gpt(self):
return self.gainCr / self.units if self.units else 0


emptyLoad = TradeLoad((), 0, 0, 0)
emptyLoad = TradeLoad([], 0, 0, 0)


######################################################################
Expand Down Expand Up @@ -169,20 +169,21 @@ def lastSystem(self):

@property
def gpt(self):
if self.hops:
return sum(hop.gpt for hop in self.hops) / len(self.hops)
hops = self.hops
if hops:
return sum(hop.gpt for hop in hops) / len(hops)
return 0

def plus(self, dst, hop, jumps, score):
"""
Returns a new route describing the sum of this route plus a new hop.
"""
return Route(
self.route + (dst,),
self.hops + (hop,),
self.route + [dst],
self.hops + [hop],
self.startCr,
self.gainCr + hop[1],
self.jumps + (jumps,),
self.jumps + [jumps],
self.score + score,
)

Expand Down Expand Up @@ -210,15 +211,17 @@ def detail(self, tdenv):
gainCr = 0
route = self.route

hops = self.hops
hops = [
hop for hop in self.hops[0:len(self.hops)]
]

# TODO: Write as a comprehension, just can't wrap my head
# around it this morning.
def genSubValues():
def genSubValues(key):
for hop in hops:
for (tr, qty) in hop[0]:
yield tr.name()
longestNameLen = max(genSubValues())
yield key(tr)
longestNameLen = max(genSubValues(key=lambda tr: len(tr.name())))

text = self.str()
if detail >= 1:
Expand Down Expand Up @@ -336,7 +339,7 @@ def goalDistance(station):
purchases=purchases
)
if jumpsFmt and self.jumps[i]:
jumps = ' -> '.join(jump.name() for jump in self.jumps[i])
jumps = ' -> '.join([jump.name() for jump in self.jumps[i]])
text += jumpsFmt.format(
jumps=jumps,
gain=hopGainCr,
Expand Down Expand Up @@ -373,12 +376,12 @@ def summary(self):
"""

credits, hops, jumps = self.startCr, self.hops, self.jumps
ttlGainCr = sum(hop[1] for hop in hops)
numJumps = sum(
ttlGainCr = sum([hop[1] for hop in hops])
numJumps = sum([
len(hopJumps)-1
for hopJumps in jumps
if hopJumps # don't include in-system hops
)
])
return (
"Start CR: {start:10n}\n"
"Hops : {hops:10n}\n"
Expand Down Expand Up @@ -436,7 +439,7 @@ def __init__(self, tdb, tdenv=None, fit=None, items=None):

avoidItemIDs = set(
item.ID
for item in (tdenv.avoidItems or ())
for item in (tdenv.avoidItems or [])
)

if tdenv.maxAge:
Expand Down Expand Up @@ -530,7 +533,7 @@ def _fitCombos(offset, cr, cap, level=1):

for qty in range(1, maxQty + 1):
loadGain, loadCost = itemGain * qty, itemCost * qty
load = TradeLoad(((item, qty),), loadGain, loadCost, qty)
load = TradeLoad([(item, qty)], loadGain, loadCost, qty)
subLoad = _fitCombos(
offset, cr - loadCost, cap - qty, level+1
)
Expand Down Expand Up @@ -641,9 +644,9 @@ def _fitCombos(offset, cr, cap):
if not bestItem:
return emptyLoad

bestLoad = ((bestItem, bestQty),)
bestLoad = [(bestItem, bestQty)]
if bestSub:
bestLoad = bestLoad + bestSub.items
bestLoad.extend(bestSub.items)
bestQty += bestSub.units
return TradeLoad(bestLoad, bestGainCr, bestGainCr, bestQty)

Expand Down Expand Up @@ -709,7 +712,7 @@ def getBestHops(self, routes, restrictTo=None):

tdb = self.tdb
tdenv = self.tdenv
avoidPlaces = getattr(tdenv, 'avoidPlaces', None) or ()
avoidPlaces = getattr(tdenv, 'avoidPlaces', []) or []
assert not restrictTo or isinstance(restrictTo, set)
maxJumpsPer = tdenv.maxJumpsPer
maxLyPer = tdenv.maxLyPer
Expand Down Expand Up @@ -747,7 +750,7 @@ def getBestHops(self, routes, restrictTo=None):
# Are we doing direct routes?
if tdenv.direct:
if goalSystem and not restrictTo:
restrictTo = (goalSystem,)
restrictTo = [ goalSystem ]
restrictStations = set(goalSystem.stations)
if avoidPlaces:
restrictStations = set(
Expand All @@ -762,7 +765,7 @@ def station_iterator(srcStation):
stnSys = stn.system
yield Destination(
stnSys, stn,
(srcSys, stnSys),
[srcSys, stnSys],
srcDist(stnSys)
)
else:
Expand Down Expand Up @@ -822,63 +825,53 @@ def station_iterator(srcStation):
elif loopInt:
uniquePath = route.route[-loopInt:-1]

stations = (
dest for dest in station_iterator(srcStation)
if dest.station != srcStation
)
if reqBlackMarket:
stations = (d for d in stations if d.station.blackMarket == 'Y')
if uniquePath:
stations = (d for d in stations if d.station not in uniquePath)
if restrictStations:
stations = (
d for d in stations
if d.station in restrictStations
)
if maxAge:
def age_check():
for d in stations:
age = d.station.dataAge
if age and age <= maxAge:
yield d
stations = iter(age_check())
if goalSystem:
def goal_check():
unique = bool(tdenv.unique)
for d in stations:
if unique and d.system is srcSystem:
continue
if d.system is not goalSystem:
# Ignore jumps longer than remaining distance
# to the goal system.
if d.distLy >= srcGoalDist:
continue
yield d
stations = iter(goal_check())

if tdenv.debug >= 1:
def annotate():
for dest in stations:
tdenv.DEBUG1(
"destSys {}, destStn {}, jumps {}, distLy {}",
dest.system.dbname,
dest.station.dbname,
"->".join(jump.str() for jump in dest.via),
dest.distLy
)
yield dest
stations = iter(annotate())

for dest in stations:
for dest in station_iterator(srcStation):
dstStation = dest.station
if dstStation is srcStation:
continue

if uniquePath and dstStation in uniquePath:
continue

if reqBlackMarket and dstStation.blackMarket != 'Y':
continue

connections += 1

if maxAge:
stnDataAge = dstStation.dataAge
if stnDataAge is None or stnDataAge > maxAge:
continue

multiplier = 1.0
if restrictStations:
if dstStation not in restrictStations:
continue
if goalSystem:
# Bias in favor of getting closer
dstSys = dstStation.system
if dstSys is srcSystem:
if tdenv.unique:
continue
elif dstSys is not goalSystem:
# Ignore jumps longer than remaining distance to goal.
if dest.distLy >= srcGoalDist:
continue

if tdenv.debug >= 1:
tdenv.DEBUG1(
"destSys {}, destStn {}, jumps {}, distLy {}",
dstStation.system.dbname,
dstStation.dbname,
"->".join([jump.str() for jump in dest.via]),
dest.distLy
)

items = self.getTrades(srcStation, dstStation, srcSelling)
if not items:
continue
trade = fitFunction(items, startCr, capacity, maxUnits)

multiplier = 1.0
# Calculate total K-lightseconds supercruise time.
# This will amortize for the start/end stations
if goalSystem and dstSys is not goalSystem:
Expand Down Expand Up @@ -927,9 +920,9 @@ def annotate():
if bestLy <= dest.distLy:
continue

bestToDest[dstID] = (
bestToDest[dstID] = [
dstStation, route, trade, dest.via, dest.distLy, score
)
]

prog.clear()

Expand Down
Loading

0 comments on commit fdc90d1

Please sign in to comment.