removed unnecessary utils
This commit is contained in:
@ -1,4 +1,4 @@
|
|||||||
#import utils
|
import random
|
||||||
|
|
||||||
from coderack import coderack
|
from coderack import coderack
|
||||||
from workspaceObject import WorkspaceObject
|
from workspaceObject import WorkspaceObject
|
||||||
@ -31,7 +31,7 @@ def __getScoutSource(slipnode, relevanceMethod, typeName):
|
|||||||
logging.info('initial : relevance = %d, unhappiness=%d' % (initialRelevance, int(initialUnhappiness)))
|
logging.info('initial : relevance = %d, unhappiness=%d' % (initialRelevance, int(initialUnhappiness)))
|
||||||
logging.info('target : relevance = %d, unhappiness=%d' % (targetRelevance, int(targetUnhappiness)))
|
logging.info('target : relevance = %d, unhappiness=%d' % (targetRelevance, int(targetUnhappiness)))
|
||||||
string = workspace.initial
|
string = workspace.initial
|
||||||
if utils.random() * (initialRelevance + initialUnhappiness + targetRelevance + targetUnhappiness) > (initialRelevance + initialUnhappiness):
|
if random.random() * (initialRelevance + initialUnhappiness + targetRelevance + targetUnhappiness) > (initialRelevance + initialUnhappiness):
|
||||||
string = workspace.target
|
string = workspace.target
|
||||||
logging.info('target string selected: %s for %s' % (workspace.target, typeName))
|
logging.info('target string selected: %s for %s' % (workspace.target, typeName))
|
||||||
else:
|
else:
|
||||||
@ -62,7 +62,7 @@ def __structureVsStructure(structure1, weight1, structure2, weight2):
|
|||||||
structure2.updateStrength()
|
structure2.updateStrength()
|
||||||
weightedStrength1 = temperatureAdjustedValue(structure1.totalStrength * weight1)
|
weightedStrength1 = temperatureAdjustedValue(structure1.totalStrength * weight1)
|
||||||
weightedStrength2 = temperatureAdjustedValue(structure2.totalStrength * weight2)
|
weightedStrength2 = temperatureAdjustedValue(structure2.totalStrength * weight2)
|
||||||
rhs = (weightedStrength1 + weightedStrength2) * utils.random()
|
rhs = (weightedStrength1 + weightedStrength2) * random.random()
|
||||||
logging.info('%d > %d' % (weightedStrength1, rhs))
|
logging.info('%d > %d' % (weightedStrength1, rhs))
|
||||||
return weightedStrength1 > rhs
|
return weightedStrength1 > rhs
|
||||||
|
|
||||||
@ -108,7 +108,7 @@ def breaker():
|
|||||||
isinstance(s, Bond) or
|
isinstance(s, Bond) or
|
||||||
isinstance(s, Correspondence)]
|
isinstance(s, Correspondence)]
|
||||||
assert len(structures)
|
assert len(structures)
|
||||||
structure = utils.choice(structures)
|
structure = random.choice(structures)
|
||||||
__showWhichStringObjectIsFrom(structure)
|
__showWhichStringObjectIsFrom(structure)
|
||||||
breakObjects = [structure]
|
breakObjects = [structure]
|
||||||
if isinstance(structure, Bond):
|
if isinstance(structure, Bond):
|
||||||
@ -251,14 +251,14 @@ def rule_strength_tester(codelet):
|
|||||||
rule = codelet.arguments[0]
|
rule = codelet.arguments[0]
|
||||||
rule.updateStrength()
|
rule.updateStrength()
|
||||||
probability = temperatureAdjustedProbability(rule.totalStrength / 100.0)
|
probability = temperatureAdjustedProbability(rule.totalStrength / 100.0)
|
||||||
assert utils.random() <= probability
|
assert random.random() <= probability
|
||||||
coderack.newCodelet('rule-builder', codelet, rule.totalStrength, rule)
|
coderack.newCodelet('rule-builder', codelet, rule.totalStrength, rule)
|
||||||
|
|
||||||
|
|
||||||
def replacement_finder():
|
def replacement_finder():
|
||||||
# choose random letter in initial string
|
# choose random letter in initial string
|
||||||
letters = [o for o in workspace.initial.objects if isinstance(o, Letter)]
|
letters = [o for o in workspace.initial.objects if isinstance(o, Letter)]
|
||||||
letterOfInitialString = utils.choice(letters)
|
letterOfInitialString = random.choice(letters)
|
||||||
logging.info('selected letter in initial string = %s' % letterOfInitialString)
|
logging.info('selected letter in initial string = %s' % letterOfInitialString)
|
||||||
if letterOfInitialString.replacement:
|
if letterOfInitialString.replacement:
|
||||||
logging.info("Replacement already found for %s, so fizzling" % letterOfInitialString)
|
logging.info("Replacement already found for %s, so fizzling" % letterOfInitialString)
|
||||||
@ -406,7 +406,7 @@ def top_down_group_scout__category(codelet):
|
|||||||
if category == slipnet.sameness and isinstance(source, Letter):
|
if category == slipnet.sameness and isinstance(source, Letter):
|
||||||
group = Group(source.string, slipnet.samenessGroup, None, slipnet.letterCategory, [source], [])
|
group = Group(source.string, slipnet.samenessGroup, None, slipnet.letterCategory, [source], [])
|
||||||
probability = group.singleLetterGroupProbability()
|
probability = group.singleLetterGroupProbability()
|
||||||
assert utils.random() >= probability
|
assert random.random() >= probability
|
||||||
coderack.proposeSingleLetterGroup(source, codelet)
|
coderack.proposeSingleLetterGroup(source, codelet)
|
||||||
return
|
return
|
||||||
direction = firstBond.directionCategory
|
direction = firstBond.directionCategory
|
||||||
@ -528,7 +528,7 @@ def top_down_group_scout__direction(codelet):
|
|||||||
#noinspection PyStringFormat
|
#noinspection PyStringFormat
|
||||||
def group_scout__whole_string(codelet):
|
def group_scout__whole_string(codelet):
|
||||||
string = workspace.initial
|
string = workspace.initial
|
||||||
if utils.random() > 0.5:
|
if random.random() > 0.5:
|
||||||
string = workspace.target
|
string = workspace.target
|
||||||
logging.info('target string selected: %s' % workspace.target)
|
logging.info('target string selected: %s' % workspace.target)
|
||||||
else:
|
else:
|
||||||
@ -553,7 +553,7 @@ def group_scout__whole_string(codelet):
|
|||||||
objects += [leftmost]
|
objects += [leftmost]
|
||||||
assert leftmost.rightmost
|
assert leftmost.rightmost
|
||||||
# choose a random bond from list
|
# choose a random bond from list
|
||||||
chosenBond = utils.choice(bonds)
|
chosenBond = random.choice(bonds)
|
||||||
category = chosenBond.category
|
category = chosenBond.category
|
||||||
directionCategory = chosenBond.directionCategory
|
directionCategory = chosenBond.directionCategory
|
||||||
bondFacet = chosenBond.facet
|
bondFacet = chosenBond.facet
|
||||||
@ -570,7 +570,7 @@ def group_strength_tester(codelet):
|
|||||||
group.updateStrength()
|
group.updateStrength()
|
||||||
strength = group.totalStrength
|
strength = group.totalStrength
|
||||||
probability = temperatureAdjustedProbability(strength / 100.0)
|
probability = temperatureAdjustedProbability(strength / 100.0)
|
||||||
assert utils.random() <= probability
|
assert random.random() <= probability
|
||||||
# it is strong enough - post builder & activate nodes
|
# it is strong enough - post builder & activate nodes
|
||||||
group.groupCategory.getRelatedNode(slipnet.bondCategory).buffer = 100.0
|
group.groupCategory.getRelatedNode(slipnet.bondCategory).buffer = 100.0
|
||||||
if group.directionCategory:
|
if group.directionCategory:
|
||||||
@ -670,7 +670,7 @@ def __getCutOff(density):
|
|||||||
distribution = [1.0, 1.0, 2.0, 5.0, 150.0, 5.0, 2.0, 1.0, 1.0, 1.0]
|
distribution = [1.0, 1.0, 2.0, 5.0, 150.0, 5.0, 2.0, 1.0, 1.0, 1.0]
|
||||||
else:
|
else:
|
||||||
distribution = [1.0, 1.0, 1.0, 2.0, 5.0, 150.0, 5.0, 2.0, 1.0, 1.0]
|
distribution = [1.0, 1.0, 1.0, 2.0, 5.0, 150.0, 5.0, 2.0, 1.0, 1.0]
|
||||||
stop = sum(distribution) * utils.random()
|
stop = sum(distribution) * random.random()
|
||||||
total = 0.0
|
total = 0.0
|
||||||
for i in range(0, len(distribution)):
|
for i in range(0, len(distribution)):
|
||||||
total += distribution[i]
|
total += distribution[i]
|
||||||
@ -766,7 +766,7 @@ def correspondence_strength_tester(codelet):
|
|||||||
correspondence.updateStrength()
|
correspondence.updateStrength()
|
||||||
strength = correspondence.totalStrength
|
strength = correspondence.totalStrength
|
||||||
probability = temperatureAdjustedProbability(strength / 100.0)
|
probability = temperatureAdjustedProbability(strength / 100.0)
|
||||||
assert utils.random() <= probability
|
assert random.random() <= probability
|
||||||
# activate some concepts
|
# activate some concepts
|
||||||
for mapping in correspondence.conceptMappings:
|
for mapping in correspondence.conceptMappings:
|
||||||
mapping.initialDescriptionType.buffer = 100.0
|
mapping.initialDescriptionType.buffer = 100.0
|
||||||
|
|||||||
@ -2,8 +2,8 @@ import re
|
|||||||
import inspect
|
import inspect
|
||||||
import math
|
import math
|
||||||
import logging
|
import logging
|
||||||
|
import random
|
||||||
|
|
||||||
import utils
|
|
||||||
import formulas
|
import formulas
|
||||||
import workspaceFormulas
|
import workspaceFormulas
|
||||||
from slipnet import slipnet
|
from slipnet import slipnet
|
||||||
@ -70,7 +70,7 @@ class CodeRack(object):
|
|||||||
howMany = workspaceFormulas.howManyToPost(codeletName)
|
howMany = workspaceFormulas.howManyToPost(codeletName)
|
||||||
#print '%s:%d' % (codeletName,howMany)
|
#print '%s:%d' % (codeletName,howMany)
|
||||||
for unused in range(0, howMany):
|
for unused in range(0, howMany):
|
||||||
if utils.random() < probability:
|
if random.random() < probability:
|
||||||
urgency = self.getUrgencyBin(node.activation * node.conceptualDepth / 100.0)
|
urgency = self.getUrgencyBin(node.activation * node.conceptualDepth / 100.0)
|
||||||
codelet = Codelet(codeletName, urgency, self.codeletsRun)
|
codelet = Codelet(codeletName, urgency, self.codeletsRun)
|
||||||
codelet.arguments += [node]
|
codelet.arguments += [node]
|
||||||
@ -105,7 +105,7 @@ class CodeRack(object):
|
|||||||
if formulas.Temperature < 25.0 and 'translator' in codeletName:
|
if formulas.Temperature < 25.0 and 'translator' in codeletName:
|
||||||
urgency = 5
|
urgency = 5
|
||||||
for unused in range(0, howMany):
|
for unused in range(0, howMany):
|
||||||
if utils.random() < probability:
|
if random.random() < probability:
|
||||||
codelet = Codelet(codeletName, urgency, self.codeletsRun)
|
codelet = Codelet(codeletName, urgency, self.codeletsRun)
|
||||||
self.post(codelet)
|
self.post(codelet)
|
||||||
|
|
||||||
@ -200,7 +200,7 @@ class CodeRack(object):
|
|||||||
for codelet in self.codelets:
|
for codelet in self.codelets:
|
||||||
urgency = (coderack.codeletsRun - codelet.timeStamp) * (7.5 - codelet.urgency)
|
urgency = (coderack.codeletsRun - codelet.timeStamp) * (7.5 - codelet.urgency)
|
||||||
urgencies += [urgency]
|
urgencies += [urgency]
|
||||||
threshold = utils.random() * sum(urgencies)
|
threshold = random.random() * sum(urgencies)
|
||||||
sumOfUrgencies = 0.0
|
sumOfUrgencies = 0.0
|
||||||
for i in range(0, len(self.codelets)):
|
for i in range(0, len(self.codelets)):
|
||||||
sumOfUrgencies += urgencies[i]
|
sumOfUrgencies += urgencies[i]
|
||||||
@ -275,12 +275,12 @@ class CodeRack(object):
|
|||||||
return None
|
return None
|
||||||
temp = formulas.Temperature
|
temp = formulas.Temperature
|
||||||
scale = (100.0 - temp + 10.0) / 15.0
|
scale = (100.0 - temp + 10.0) / 15.0
|
||||||
# threshold = sum( [ c.urgency ** scale for c in self.codelets ] ) * utils.random()
|
# threshold = sum( [ c.urgency ** scale for c in self.codelets ] ) * random.random()
|
||||||
urgsum = 0.0
|
urgsum = 0.0
|
||||||
for codelet in self.codelets:
|
for codelet in self.codelets:
|
||||||
urg = codelet.urgency ** scale
|
urg = codelet.urgency ** scale
|
||||||
urgsum += urg
|
urgsum += urg
|
||||||
r = utils.random()
|
r = random.random()
|
||||||
threshold = r * urgsum
|
threshold = r * urgsum
|
||||||
chosen = None
|
chosen = None
|
||||||
urgencySum = 0.0
|
urgencySum = 0.0
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
import math # , random
|
import math
|
||||||
import logging
|
import logging
|
||||||
|
import random
|
||||||
import utils
|
|
||||||
|
|
||||||
from temperature import temperature
|
from temperature import temperature
|
||||||
|
|
||||||
@ -11,7 +10,7 @@ actualTemperature = Temperature = 100.0
|
|||||||
def selectListPosition(probabilities):
|
def selectListPosition(probabilities):
|
||||||
total = sum(probabilities)
|
total = sum(probabilities)
|
||||||
#logging.info('total: %s' % total)
|
#logging.info('total: %s' % total)
|
||||||
r = utils.random()
|
r = random.random()
|
||||||
stopPosition = total * r
|
stopPosition = total * r
|
||||||
#logging.info('stopPosition: %s' % stopPosition)
|
#logging.info('stopPosition: %s' % stopPosition)
|
||||||
total = 0
|
total = 0
|
||||||
@ -57,7 +56,7 @@ def temperatureAdjustedProbability(value):
|
|||||||
|
|
||||||
|
|
||||||
def coinFlip(chance=0.5):
|
def coinFlip(chance=0.5):
|
||||||
return utils.random() < chance
|
return random.random() < chance
|
||||||
|
|
||||||
|
|
||||||
def blur(value):
|
def blur(value):
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
import utils
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from workspace import workspace
|
from workspace import workspace
|
||||||
@ -63,7 +62,7 @@ class Group(WorkspaceObject):
|
|||||||
|
|
||||||
#check whether or not to add length description category
|
#check whether or not to add length description category
|
||||||
probability = self.lengthDescriptionProbability()
|
probability = self.lengthDescriptionProbability()
|
||||||
if utils.random() < probability:
|
if random.random() < probability:
|
||||||
length = len(self.objectList)
|
length = len(self.objectList)
|
||||||
if length < 6:
|
if length < 6:
|
||||||
self.addDescription(slipnet.length, slipnet.numbers[length - 1])
|
self.addDescription(slipnet.length, slipnet.numbers[length - 1])
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
import math
|
import math
|
||||||
import utils
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
|
||||||
@ -155,7 +154,7 @@ class Slipnode(object):
|
|||||||
def jump(self):
|
def jump(self):
|
||||||
value = (self.activation / 100.0) ** 3
|
value = (self.activation / 100.0) ** 3
|
||||||
#logging.info('jumping for %s at activation %s' % (self.name,self.activation))
|
#logging.info('jumping for %s at activation %s' % (self.name,self.activation))
|
||||||
if self.activation > jump_threshold() and utils.random() < value and not self.clamped:
|
if self.activation > jump_threshold() and random.random() < value and not self.clamped:
|
||||||
self.activate_fully()
|
self.activate_fully()
|
||||||
|
|
||||||
def get_name(self):
|
def get_name(self):
|
||||||
|
|||||||
142
copycat/utils.py
142
copycat/utils.py
@ -1,142 +0,0 @@
|
|||||||
def any(things):
|
|
||||||
"""Return True if any of the things are True.
|
|
||||||
|
|
||||||
things should be iterable.
|
|
||||||
|
|
||||||
If the things are empty, then we can't say any are True
|
|
||||||
>>> any([])
|
|
||||||
False
|
|
||||||
|
|
||||||
If all the things are False, then we can't say any are True
|
|
||||||
>>> any([False,False,False])
|
|
||||||
False
|
|
||||||
|
|
||||||
If all the things are equivalent to False, then we can't say any are True
|
|
||||||
>>> any([0,[],''])
|
|
||||||
False
|
|
||||||
|
|
||||||
The type of the true thing should not matter
|
|
||||||
>>> any([1,[],''])
|
|
||||||
True
|
|
||||||
>>> any([0,(2,),''])
|
|
||||||
True
|
|
||||||
>>> any([0,[],'foo'])
|
|
||||||
True
|
|
||||||
>>> any([0,[],True,''])
|
|
||||||
True
|
|
||||||
|
|
||||||
It should not matter where the True thing is
|
|
||||||
>>> any((True,False,False,False,False,))
|
|
||||||
True
|
|
||||||
>>> any((False,False,True,False,False,))
|
|
||||||
True
|
|
||||||
>>> any((False,False,False,False,True,))
|
|
||||||
True
|
|
||||||
|
|
||||||
The size of the sequence should not matter
|
|
||||||
>>> True == any((True,)) == any((True,True,)) == any((True,True,True,True,))
|
|
||||||
True
|
|
||||||
|
|
||||||
Any string is True
|
|
||||||
>>> any('foo')
|
|
||||||
True
|
|
||||||
|
|
||||||
Except an empty string
|
|
||||||
>>> any('')
|
|
||||||
False
|
|
||||||
|
|
||||||
The function cannot be applied to ints
|
|
||||||
>>> any(7)
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
TypeError: iteration over non-sequence
|
|
||||||
"""
|
|
||||||
for thing in things:
|
|
||||||
if thing:
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def all(things):
|
|
||||||
"""Return True if all of the things are True.
|
|
||||||
|
|
||||||
things should be iterable.
|
|
||||||
|
|
||||||
If the things are empty, then we can't say all are True
|
|
||||||
>>> all([])
|
|
||||||
False
|
|
||||||
|
|
||||||
If all the things are False, then we can't say all are True
|
|
||||||
>>> all([False,False,False])
|
|
||||||
False
|
|
||||||
|
|
||||||
If all the things are equivalent to False, then we can't say all are True
|
|
||||||
>>> all([0,[],''])
|
|
||||||
False
|
|
||||||
|
|
||||||
The type of the false thing should not matter
|
|
||||||
>>> all([0,True,True,])
|
|
||||||
False
|
|
||||||
>>> all([True,(),True,])
|
|
||||||
False
|
|
||||||
>>> all([True,True,'',])
|
|
||||||
False
|
|
||||||
|
|
||||||
Position of the false thing should not matter
|
|
||||||
>>> all((False,True,True,))
|
|
||||||
False
|
|
||||||
>>> all((True,False,True,))
|
|
||||||
False
|
|
||||||
>>> all((True,True,False,))
|
|
||||||
False
|
|
||||||
|
|
||||||
any string is True
|
|
||||||
>>> all('foo')
|
|
||||||
True
|
|
||||||
|
|
||||||
Except an empty string
|
|
||||||
>>> all('')
|
|
||||||
False
|
|
||||||
|
|
||||||
The function cannot be applied to ints
|
|
||||||
>>> all(7)
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
TypeError: iteration over non-sequence
|
|
||||||
"""
|
|
||||||
for thing in things:
|
|
||||||
if not thing:
|
|
||||||
return False
|
|
||||||
return len(things) > 0
|
|
||||||
|
|
||||||
import logging
|
|
||||||
|
|
||||||
seed = 999.0
|
|
||||||
count = 0
|
|
||||||
testably_random = True
|
|
||||||
|
|
||||||
|
|
||||||
def random():
|
|
||||||
global testably_random
|
|
||||||
if testably_random:
|
|
||||||
from random import random
|
|
||||||
return random()
|
|
||||||
global seed
|
|
||||||
global count
|
|
||||||
seed += 1.0
|
|
||||||
count += 1
|
|
||||||
if seed > 1999:
|
|
||||||
seed = 0.0
|
|
||||||
logging.info("count: %d" % count)
|
|
||||||
#if seed == 998:
|
|
||||||
# sys.exit(1)
|
|
||||||
return seed / 2000.0
|
|
||||||
|
|
||||||
|
|
||||||
def choice(aList):
|
|
||||||
i = int(random() * len(aList))
|
|
||||||
return aList[i]
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
import doctest
|
|
||||||
doctest.testmod()
|
|
||||||
Reference in New Issue
Block a user