Skip to content

Commit

Permalink
Fixed nasty bug in trade calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Smith committed Feb 1, 2015
1 parent 44f9df0 commit 63b4859
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
TradeDangerous, Copyright (C) Oliver "kfsone" Smith, July 2014
==============================================================================

v6.8.4 Feb 01 2015
. (kfsone) Fixed tradecalculator not selecting most profitable trade sometimes
. (kfsone) Set 'BRUTE_FIT' in the environment to force use of the brute-force
trade calculator (for diagnostic purposes). e.g.
BRUTE_FIT=1 trade.py run ...

v6.8.3 Jan 31 2015
. (kfsone) Code and data cleanup, added stations
. (kfsone) Added misc/eddn.py API for developers wanting to access EDDN,
Expand Down
32 changes: 19 additions & 13 deletions tradecalc.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

import locale
import math
import os
import time

locale.setlocale(locale.LC_ALL, '')
Expand Down Expand Up @@ -332,6 +333,8 @@ def __init__(self, tdb, tdenv, fit=None):
self.tdb = tdb
self.tdenv = tdenv
self.defaultFit = fit or self.fastFit
if "BRUTE_FIT" in os.environ:
self.defaultFit = self.bruteForceFit

db = self.tdb.getDB()

Expand Down Expand Up @@ -471,11 +474,12 @@ def _fitCombos(offset, cr, cap):
# seemed significantly slower than this approach.
for item in items[offset:]:
itemCostCr = item.costCr
stock = item.stock
maxQty = min(maxUnits, cap, cr // itemCostCr)

# Adjust for age for "M"/"H" items with low units.
if item.stock < maxQty and item.stock > 0: # -1 = unknown
maxQty = min(maxQty, item.stock)
if stock < maxQty and stock >= 0: # -1 = unknown
maxQty = min(maxQty, stock)

if maxQty <= 0:
offset += 1
Expand Down Expand Up @@ -505,21 +509,23 @@ def _fitCombos(offset, cr, cap):
offset += 1

bestLoad = emptyLoad
for result in _fitCombos(0, credits, capacity):
for newResult in _fitCombos(0, credits, capacity):
if bestLoad:
# Compare our new candidate against the previous best.
resGain, bestGain = result.gainCr, bestLoad.gainCr
if bestGain < resGain:
bestGain, newGain = bestLoad.gainCr, newResult.gainCr
# Ignore a result that doesn't match the previous best gain
if newGain < bestGain:
continue
if bestGain == resGain:
resUnits, bestUnits = result.units, bestLoad.units
# Prefer the option which requires least units
if bestUnits < resUnits:
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 bestUnits == resUnits:
if bestLoad.costCr >= result.costCr:
if newUnits == bestUnits:
# They use the same units, choose the least expensive
if newResult.costCr > bestLoad.costCr:
continue
bestLoad = result
bestLoad = newResult

return bestLoad

Expand Down

0 comments on commit 63b4859

Please sign in to comment.