Major overhaul of "randomness" throughout.

- Nobody does `import random` anymore.

- Random numbers are gotten from `ctx.random`, which is an object
of type `Randomness` with all the convenience methods that used to
be obnoxious functions in the `formulas` module.

- Every place that was using `random.random()` to implement the
equivalent of Python3 `random.choices(seq, weights)` has been updated
to use `ctx.random.weighted_choice(seq, weights)`.

This has a functional effect, since the details of random number
generation have changed. The *statistical* effect should be small.
I do observe that Copycat is having trouble inventing the "mrrjjjj"
solution right now (even in 1000 test runs), so maybe something is
slightly broken.
This commit is contained in:
Arthur O'Dwyer
2017-04-17 22:18:37 -07:00
parent 8fdb9d06e6
commit 3732ae8475
11 changed files with 179 additions and 214 deletions

View File

@ -1,25 +1,6 @@
import math
import random
from conceptMapping import ConceptMapping
def selectListPosition(probabilities):
total = sum(probabilities)
#logging.info('total: %s' % total)
r = random.random()
stopPosition = total * r
#logging.info('stopPosition: %s' % stopPosition)
total = 0
i = 0
for probability in probabilities:
total += probability
if total > stopPosition:
return i
i += 1
return 0
def weightedAverage(values):
total = 0.0
totalWeights = 0.0
@ -31,27 +12,6 @@ def weightedAverage(values):
return total / totalWeights
def coinFlip(chance=0.5):
return random.random() < chance
def blur(value):
root = math.sqrt(value)
if coinFlip():
return value + root
return value - root
def chooseRelevantDescriptionByActivation(workspaceObject):
descriptions = workspaceObject.relevantDescriptions()
if not descriptions:
return None
activations = [description.descriptor.activation
for description in descriptions]
i = selectListPosition(activations)
return descriptions[i]
def __relevantCategory(objekt, slipnode):
return objekt.rightBond and objekt.rightBond.category == slipnode