Skip to content

Commit

Permalink
Merged kfsone/tradedangerous into master
Browse files Browse the repository at this point in the history
  • Loading branch information
maddavo committed Feb 6, 2015
2 parents 354071b + d756b9e commit 2e0544d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 42 deletions.
6 changes: 5 additions & 1 deletion cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@
‹ |
\bSATION\b |
,\w |
\bINGLY\b
\bINGLY\b |
\bAU\sL[DO0]\b
)''', flags=re.X)


Expand Down Expand Up @@ -477,11 +479,13 @@ def ignoreOrWarn(error):
raise error
elif not quiet:
def ignoreOrWarn(error):
nonlocal warnings
error.category = "WARNING"
print(error)
warnings += 1
else:
def ignoreOrWarn(error):
nonlocal warnings
warnings += 1

DEBUG0, DEBUG1 = tdenv.DEBUG0, tdenv.DEBUG1
Expand Down
2 changes: 2 additions & 0 deletions data/Station.csv
Original file line number Diff line number Diff line change
Expand Up @@ -5143,6 +5143,8 @@ unq:[email protected]_id,unq:name,ls_from_star,blackmarket,max_pad_size
'NANOAI','Glushko Vision',0,'?','?'
'NANOMAM','Gresley Dock',398,'Y','L'
'NAPOYO','Rosse Camp',0,'?','?'
'NARAKA','Hawking Depot',51,'N','L'
'NARAKA','Novitski Oasis',50,'Y','L'
'NARAKWATES','Dubyago City',0,'?','?'
'NARASIMHA','Mendel Survey',2480,'Y','M'
'NARASIMHA','Zudov Survey',340779,'?','?'
Expand Down
86 changes: 45 additions & 41 deletions tradecalc.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ class TradeLoad(namedtuple('TradeLoad', [
def __bool__(self):
return self.units > 0

def __lt__(self, rhs):
if self.gainCr < rhs.gainCr:
return True
if rhs.gainCr < self.gainCr:
return False
if self.units < rhs.units:
return True
if rhs.units < self.units:
return False
return self.costCr < rhs.costCr


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


Expand Down Expand Up @@ -460,9 +472,8 @@ def _fitCombos(offset, cr, cap):
# for itemNo in range(offset, len(items)):
# item = items[itemNo]
# seemed significantly slower than this approach.
for (iNo, item) in enumerate(items):
if iNo < offset:
continue
for iNo in range(offset, len(items)):
item = items[iNo]
itemCostCr = item.costCr
stock = item.stock
maxQty = min(maxUnits, cap, cr // itemCostCr)
Expand All @@ -472,47 +483,40 @@ def _fitCombos(offset, cr, cap):
if maxQty <= 0:
continue

bestGainCr = -1
itemGainCr = item.gainCr
for qty in range(maxQty, int(maxQty * 0.5), -1):
loadItems = [(item, qty)]
loadCostCr = qty * itemCostCr
loadGainCr = qty * itemGainCr
if loadGainCr >= bestGainCr:
yield TradeLoad(loadItems, loadCostCr, loadGainCr, qty)
crLeft, capLeft = cr - loadCostCr, cap - qty
if crLeft > 0 and capLeft > 0:
# Solve for the remaining credits and capacity with what
# is left in items after the item we just checked.
for subLoad in _fitCombos(iNo + 1, crLeft, capLeft):
slGain = loadGainCr + subLoad.gainCr
if slGain >= bestGainCr:
yield TradeLoad(
subLoad.items + loadItems,
slGain,
subLoad.costCr + loadCostCr,
subLoad.units + maxQty
)
bestGainCr = subLoad.gainCr
if maxQty >= cap:
yield TradeLoad(
[(item, cap)],
itemGainCr * cap, itemCostCr * cap,
cap
)
return

bestGainCr = -1
loadItems = [(item, maxQty)]
loadCostCr = maxQty * itemCostCr
loadGainCr = maxQty * itemGainCr
crLeft, capLeft = cr - loadCostCr, cap - maxQty
if crLeft > 0 and capLeft > 0:
# Solve for the remaining credits and capacity with what
# is left in items after the item we just checked.
for subLoad in _fitCombos(iNo + 1, crLeft, capLeft):
slGain = loadGainCr + subLoad.gainCr
if slGain >= bestGainCr:
yield TradeLoad(
subLoad.items + loadItems,
slGain,
subLoad.costCr + loadCostCr,
subLoad.units + maxQty,
)
bestGainCr = subLoad.gainCr
if bestGainCr < 0 and loadGainCr >= bestGainCr:
yield TradeLoad(loadItems, loadGainCr, loadCostCr, maxQty)

bestLoad = emptyLoad
for newResult in _fitCombos(0, credits, capacity):
if bestLoad:
bestGain, newGain = bestLoad.gainCr, newResult.gainCr
# Ignore a result that doesn't match the previous best gain
if newGain < bestGain:
continue
if newGain == bestGain:
bestUnits, newUnits = bestLoad.units, newResult.units
# Ignore entries that require more units for the same
# amount of gain as the previous best.
if newUnits > bestUnits:
continue
if newUnits == bestUnits:
# They use the same units, choose the least expensive
if newResult.costCr > bestLoad.costCr:
continue
bestLoad = newResult
for newLoad in _fitCombos(0, credits, capacity):
if bestLoad < newLoad:
bestLoad = newLoad

return bestLoad

Expand Down

0 comments on commit 2e0544d

Please sign in to comment.