From 2281870cf2864972b7acf2fa6157f961f8ec7a48 Mon Sep 17 00:00:00 2001 From: James Tauber Date: Sat, 1 Dec 2012 02:10:33 -0500 Subject: [PATCH] removed unnecessary utils --- copycat/codeletMethods.py | 24 +++---- copycat/coderack.py | 12 ++-- copycat/formulas.py | 9 ++- copycat/group.py | 3 +- copycat/slipnode.py | 3 +- copycat/utils.py | 142 -------------------------------------- 6 files changed, 24 insertions(+), 169 deletions(-) delete mode 100644 copycat/utils.py diff --git a/copycat/codeletMethods.py b/copycat/codeletMethods.py index f618add..0e210ac 100644 --- a/copycat/codeletMethods.py +++ b/copycat/codeletMethods.py @@ -1,4 +1,4 @@ -#import utils +import random from coderack import coderack 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('target : relevance = %d, unhappiness=%d' % (targetRelevance, int(targetUnhappiness))) string = workspace.initial - if utils.random() * (initialRelevance + initialUnhappiness + targetRelevance + targetUnhappiness) > (initialRelevance + initialUnhappiness): + if random.random() * (initialRelevance + initialUnhappiness + targetRelevance + targetUnhappiness) > (initialRelevance + initialUnhappiness): string = workspace.target logging.info('target string selected: %s for %s' % (workspace.target, typeName)) else: @@ -62,7 +62,7 @@ def __structureVsStructure(structure1, weight1, structure2, weight2): structure2.updateStrength() weightedStrength1 = temperatureAdjustedValue(structure1.totalStrength * weight1) weightedStrength2 = temperatureAdjustedValue(structure2.totalStrength * weight2) - rhs = (weightedStrength1 + weightedStrength2) * utils.random() + rhs = (weightedStrength1 + weightedStrength2) * random.random() logging.info('%d > %d' % (weightedStrength1, rhs)) return weightedStrength1 > rhs @@ -108,7 +108,7 @@ def breaker(): isinstance(s, Bond) or isinstance(s, Correspondence)] assert len(structures) - structure = utils.choice(structures) + structure = random.choice(structures) __showWhichStringObjectIsFrom(structure) breakObjects = [structure] if isinstance(structure, Bond): @@ -251,14 +251,14 @@ def rule_strength_tester(codelet): rule = codelet.arguments[0] rule.updateStrength() probability = temperatureAdjustedProbability(rule.totalStrength / 100.0) - assert utils.random() <= probability + assert random.random() <= probability coderack.newCodelet('rule-builder', codelet, rule.totalStrength, rule) def replacement_finder(): # choose random letter in initial string 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) if letterOfInitialString.replacement: 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): group = Group(source.string, slipnet.samenessGroup, None, slipnet.letterCategory, [source], []) probability = group.singleLetterGroupProbability() - assert utils.random() >= probability + assert random.random() >= probability coderack.proposeSingleLetterGroup(source, codelet) return direction = firstBond.directionCategory @@ -528,7 +528,7 @@ def top_down_group_scout__direction(codelet): #noinspection PyStringFormat def group_scout__whole_string(codelet): string = workspace.initial - if utils.random() > 0.5: + if random.random() > 0.5: string = workspace.target logging.info('target string selected: %s' % workspace.target) else: @@ -553,7 +553,7 @@ def group_scout__whole_string(codelet): objects += [leftmost] assert leftmost.rightmost # choose a random bond from list - chosenBond = utils.choice(bonds) + chosenBond = random.choice(bonds) category = chosenBond.category directionCategory = chosenBond.directionCategory bondFacet = chosenBond.facet @@ -570,7 +570,7 @@ def group_strength_tester(codelet): group.updateStrength() strength = group.totalStrength probability = temperatureAdjustedProbability(strength / 100.0) - assert utils.random() <= probability + assert random.random() <= probability # it is strong enough - post builder & activate nodes group.groupCategory.getRelatedNode(slipnet.bondCategory).buffer = 100.0 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] else: 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 for i in range(0, len(distribution)): total += distribution[i] @@ -766,7 +766,7 @@ def correspondence_strength_tester(codelet): correspondence.updateStrength() strength = correspondence.totalStrength probability = temperatureAdjustedProbability(strength / 100.0) - assert utils.random() <= probability + assert random.random() <= probability # activate some concepts for mapping in correspondence.conceptMappings: mapping.initialDescriptionType.buffer = 100.0 diff --git a/copycat/coderack.py b/copycat/coderack.py index c0b7fcf..e25eef6 100644 --- a/copycat/coderack.py +++ b/copycat/coderack.py @@ -2,8 +2,8 @@ import re import inspect import math import logging +import random -import utils import formulas import workspaceFormulas from slipnet import slipnet @@ -70,7 +70,7 @@ class CodeRack(object): howMany = workspaceFormulas.howManyToPost(codeletName) #print '%s:%d' % (codeletName,howMany) for unused in range(0, howMany): - if utils.random() < probability: + if random.random() < probability: urgency = self.getUrgencyBin(node.activation * node.conceptualDepth / 100.0) codelet = Codelet(codeletName, urgency, self.codeletsRun) codelet.arguments += [node] @@ -105,7 +105,7 @@ class CodeRack(object): if formulas.Temperature < 25.0 and 'translator' in codeletName: urgency = 5 for unused in range(0, howMany): - if utils.random() < probability: + if random.random() < probability: codelet = Codelet(codeletName, urgency, self.codeletsRun) self.post(codelet) @@ -200,7 +200,7 @@ class CodeRack(object): for codelet in self.codelets: urgency = (coderack.codeletsRun - codelet.timeStamp) * (7.5 - codelet.urgency) urgencies += [urgency] - threshold = utils.random() * sum(urgencies) + threshold = random.random() * sum(urgencies) sumOfUrgencies = 0.0 for i in range(0, len(self.codelets)): sumOfUrgencies += urgencies[i] @@ -275,12 +275,12 @@ class CodeRack(object): return None temp = formulas.Temperature 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 for codelet in self.codelets: urg = codelet.urgency ** scale urgsum += urg - r = utils.random() + r = random.random() threshold = r * urgsum chosen = None urgencySum = 0.0 diff --git a/copycat/formulas.py b/copycat/formulas.py index f05f382..b3fb011 100644 --- a/copycat/formulas.py +++ b/copycat/formulas.py @@ -1,7 +1,6 @@ -import math # , random +import math import logging - -import utils +import random from temperature import temperature @@ -11,7 +10,7 @@ actualTemperature = Temperature = 100.0 def selectListPosition(probabilities): total = sum(probabilities) #logging.info('total: %s' % total) - r = utils.random() + r = random.random() stopPosition = total * r #logging.info('stopPosition: %s' % stopPosition) total = 0 @@ -57,7 +56,7 @@ def temperatureAdjustedProbability(value): def coinFlip(chance=0.5): - return utils.random() < chance + return random.random() < chance def blur(value): diff --git a/copycat/group.py b/copycat/group.py index 5d39997..e4a2ba7 100644 --- a/copycat/group.py +++ b/copycat/group.py @@ -1,4 +1,3 @@ -import utils import logging from workspace import workspace @@ -63,7 +62,7 @@ class Group(WorkspaceObject): #check whether or not to add length description category probability = self.lengthDescriptionProbability() - if utils.random() < probability: + if random.random() < probability: length = len(self.objectList) if length < 6: self.addDescription(slipnet.length, slipnet.numbers[length - 1]) diff --git a/copycat/slipnode.py b/copycat/slipnode.py index b3d200c..9d69243 100644 --- a/copycat/slipnode.py +++ b/copycat/slipnode.py @@ -1,5 +1,4 @@ import math -import utils import logging @@ -155,7 +154,7 @@ class Slipnode(object): def jump(self): value = (self.activation / 100.0) ** 3 #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() def get_name(self): diff --git a/copycat/utils.py b/copycat/utils.py deleted file mode 100644 index a7e6439..0000000 --- a/copycat/utils.py +++ /dev/null @@ -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()