diff --git a/cache.py b/cache.py index fafe12d0..52049b64 100644 --- a/cache.py +++ b/cache.py @@ -218,7 +218,9 @@ ‹ | \bSATION\b | ,\w | - \bINGLY\b + \bINGLY\b | + \bAU\sL[DO0]\b + )''', flags=re.X) @@ -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 diff --git a/data/Station.csv b/data/Station.csv index 4ddfc6d1..d681e0fc 100644 --- a/data/Station.csv +++ b/data/Station.csv @@ -5143,6 +5143,8 @@ unq:name@System.system_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,'?','?' diff --git a/tradecalc.py b/tradecalc.py index 87031fa9..d9a14726 100644 --- a/tradecalc.py +++ b/tradecalc.py @@ -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) @@ -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) @@ -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