Skip to content

Commit

Permalink
Fixed #4193
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo committed Jun 8, 2018
1 parent aec8e2a commit 2dcc139
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions tools/createVehTypeDistribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,32 @@ def __init__(self, params, isNumeric=True):
self._params = params
self._limits = (0, None)
self._isNumeric = isNumeric
self._maxSampleAttempts = 10

def setMaxSamplingAttempts(self, n):
if n is not None:
self._maxSampleAttempts = n

def setLimits(self, limits):
self._limits = limits

def sampleValue(self):
value = self._sampleValue()
if self._isNumeric:
value = None
nrSampleAttempts = 0
# Sample until value falls into limits
while nrSampleAttempts < self._maxSampleAttempts \
and (value is None or (self._limits[1] is not None and value > self._limits[1]) \
or (self._limits[0] is not None and value < self._limits[0])):
value = self._sampleValue()
nrSampleAttempts += 1
# Eventually apply fallback cutting value to limits
if self._limits[0] is not None and value < self._limits[0]:
value = self._limits[0]
elif self._limits[1] is not None and value > self._limits[1]:
value = self._limits[1]

else:
value = self._sampleValue()
return value

def sampleValueString(self, decimalPlaces):
Expand Down Expand Up @@ -114,13 +128,16 @@ def get_options(args=None):
"-s", "--size", type=int, default=100, dest="vehicleCount", help="number of vTypes in the distribution")
argParser.add_argument(
"-d", "--decimal-places", type=int, default=3, dest="decimalPlaces", help="number of decimal places for numeric attribute values")
argParser.add_argument(
"--resampling", type=int, default=100, dest="nrSamplingAttempts", help="number of attempts to resample a value until it lies in the specified bounds")
argParser.add_argument("--seed", type=int, help="random seed", default=42)

options = argParser.parse_args()
return options


def readConfigFile(filePath):
def readConfigFile(options):
filePath = options.configFile
result = {}

distSyntaxes = {'normal': 'normal\(\s*(-?[0-9]+(\.[0-9]+)?)\s*,\s*([0-9]+(\.[0-9]+)?)\s*\)',
Expand Down Expand Up @@ -181,6 +198,7 @@ def readConfigFile(filePath):
lowerLimit = float(items[0][0])
upperLimit = float(items[0][2])
value.setLimits((lowerLimit, upperLimit))
value.setMaxSamplingAttempts(options.nrSamplingAttempts)
res = {"value":value, "isParameter":isParameter}
result[attName] = res
return result
Expand All @@ -189,7 +207,7 @@ def readConfigFile(filePath):
def main(options):
if options.seed:
random.seed(options.seed)
vTypeParameters = readConfigFile(options.configFile)
vTypeParameters = readConfigFile(options)
useExistingFile = False
if os.path.exists(options.outputFile):
try:
Expand Down

0 comments on commit 2dcc139

Please sign in to comment.