Further Pythonicity; and remove a bunch of logging from the inner loop.

This commit is contained in:
Arthur O'Dwyer
2017-04-16 01:19:36 -07:00
parent 77bfaaf5a7
commit 8e10814802
8 changed files with 49 additions and 58 deletions

View File

@ -159,7 +159,7 @@ class CodeRack(object):
mapping.targetDescriptionType.buffer = 100.0 mapping.targetDescriptionType.buffer = 100.0
mapping.targetDescriptor.buffer = 100.0 mapping.targetDescriptor.buffer = 100.0
mappings = correspondence.distinguishingConceptMappings() mappings = correspondence.distinguishingConceptMappings()
urgency = sum([mapping.strength() for mapping in mappings]) urgency = sum(mapping.strength() for mapping in mappings)
numberOfMappings = len(mappings) numberOfMappings = len(mappings)
if urgency: if urgency:
urgency /= numberOfMappings urgency /= numberOfMappings
@ -287,37 +287,33 @@ class CodeRack(object):
def chooseCodeletToRun(self): def chooseCodeletToRun(self):
if not self.codelets: if not self.codelets:
return None return None
temp = formulas.Temperature
scale = (100.0 - temp + 10.0) / 15.0
urgsum = 0.0
for codelet in self.codelets:
urg = codelet.urgency ** scale
urgsum += urg
r = random.random()
threshold = r * urgsum
chosen = None
urgencySum = 0.0
logging.info('temperature: %f', formulas.Temperature)
logging.info('actualTemperature: %f', formulas.actualTemperature)
logging.info('Slipnet:')
for node in slipnet.slipnodes:
logging.info("\tnode %s, activation: %d, buffer: %d, depth: %s",
node.get_name(), node.activation, node.buffer,
node.conceptualDepth)
logging.info('Coderack:')
for codelet in self.codelets:
logging.info('\t%s, %d', codelet.name, codelet.urgency)
from workspace import workspace
workspace.initial.log("Initial: ") #logging.info('temperature: %f', formulas.Temperature)
workspace.target.log("Target: ") #logging.info('actualTemperature: %f', formulas.actualTemperature)
#logging.info('Slipnet:')
#for node in slipnet.slipnodes:
# logging.info("\tnode %s, activation: %d, buffer: %d, depth: %s",
# node.get_name(), node.activation, node.buffer,
# node.conceptualDepth)
#logging.info('Coderack:')
#for codelet in self.codelets:
# logging.info('\t%s, %d', codelet.name, codelet.urgency)
#from workspace import workspace
#workspace.initial.log("Initial: ")
#workspace.target.log("Target: ")
scale = (100.0 - formulas.Temperature + 10.0) / 15.0
urgsum = sum(codelet.urgency ** scale for codelet in self.codelets)
threshold = random.random() * urgsum
chosen = self.codelets[0]
urgencySum = 0.0
for codelet in self.codelets: for codelet in self.codelets:
urgencySum += codelet.urgency ** scale urgencySum += codelet.urgency ** scale
if not chosen and urgencySum > threshold: if urgencySum > threshold:
chosen = codelet chosen = codelet
break break
if not chosen:
chosen = self.codelets[0]
self.removeCodelet(chosen) self.removeCodelet(chosen)
logging.info('chosen codelet\n\t%s, urgency = %s', logging.info('chosen codelet\n\t%s, urgency = %s',
chosen.name, chosen.urgency) chosen.name, chosen.urgency)

View File

@ -88,7 +88,7 @@ class CoderackPressures(object):
#logging.debug('coderackPressures.calculatePressures()') #logging.debug('coderackPressures.calculatePressures()')
scale = (100.0 - Temperature + 10.0) / 15.0 scale = (100.0 - Temperature + 10.0) / 15.0
values = map( values = map(
lambda pressure: sum([c.urgency ** scale for c in pressure.codelets]), lambda pressure: sum(c.urgency ** scale for c in pressure.codelets),
self.pressures self.pressures
) )
totalValue = sum(values) or 1.0 totalValue = sum(values) or 1.0

View File

@ -108,8 +108,8 @@ class Correspondence(WorkspaceStructure):
if isinstance(self.objectFromTarget, Letter): if isinstance(self.objectFromTarget, Letter):
if self.objectFromTarget.spansString(): if self.objectFromTarget.spansString():
return 100.0 return 100.0
total = sum([c.totalStrength for c in workspace.correspondences() total = sum(c.totalStrength for c in workspace.correspondences()
if self.supporting(c)]) if self.supporting(c))
total = min(total, 100.0) total = min(total, 100.0)
return total return total
@ -122,7 +122,7 @@ class Correspondence(WorkspaceStructure):
if numberOfConceptMappings < 1: if numberOfConceptMappings < 1:
self.internalStrength = 0.0 self.internalStrength = 0.0
return return
totalStrength = sum([m.strength() for m in distinguishingMappings]) totalStrength = sum(m.strength() for m in distinguishingMappings)
averageStrength = totalStrength / numberOfConceptMappings averageStrength = totalStrength / numberOfConceptMappings
if numberOfConceptMappings == 1.0: if numberOfConceptMappings == 1.0:
numberOfConceptMappingsFactor = 0.8 numberOfConceptMappingsFactor = 0.8

View File

@ -32,7 +32,9 @@ class Description(WorkspaceStructure):
def localSupport(self): def localSupport(self):
from workspace import workspace from workspace import workspace
described_like_self = 0 described_like_self = 0
for other in workspace.otherObjects(self.object): for other in workspace.objects:
if self.object == other:
continue
if self.object.isWithin(other) or other.isWithin(self.object): if self.object.isWithin(other) or other.isWithin(self.object):
continue continue
for description in other.descriptions: for description in other.descriptions:

View File

@ -106,11 +106,11 @@ class Rule(WorkspaceStructure):
if self.relation == slipnet.predecessor: if self.relation == slipnet.predecessor:
if 'a' in string: if 'a' in string:
return None return None
return ''.join([chr(ord(c) - 1) for c in string]) return ''.join(chr(ord(c) - 1) for c in string)
elif self.relation == slipnet.successor: elif self.relation == slipnet.successor:
if 'z' in string: if 'z' in string:
return None return None
return ''.join([chr(ord(c) + 1) for c in string]) return ''.join(chr(ord(c) + 1) for c in string)
else: else:
return self.relation.name.lower() return self.relation.name.lower()

View File

@ -42,14 +42,15 @@ class Workspace(object):
self.target = WorkspaceString(self.targetString) self.target = WorkspaceString(self.targetString)
def assessUnhappiness(self): def assessUnhappiness(self):
self.intraStringUnhappiness = __adjustUnhappiness([ self.intraStringUnhappiness = __adjustUnhappiness(
o.relativeImportance * o.intraStringUnhappiness o.relativeImportance * o.intraStringUnhappiness
for o in self.objects]) for o in self.objects)
self.interStringUnhappiness = __adjustUnhappiness([ self.interStringUnhappiness = __adjustUnhappiness(
o.relativeImportance * o.interStringUnhappiness o.relativeImportance * o.interStringUnhappiness
for o in self.objects]) for o in self.objects)
self.totalUnhappiness = __adjustUnhappiness([ self.totalUnhappiness = __adjustUnhappiness(
o.relativeImportance * o.totalUnhappiness for o in self.objects]) o.relativeImportance * o.totalUnhappiness
for o in self.objects)
def assessTemperature(self): def assessTemperature(self):
self.calculateIntraStringUnhappiness() self.calculateIntraStringUnhappiness()
@ -57,24 +58,21 @@ class Workspace(object):
self.calculateTotalUnhappiness() self.calculateTotalUnhappiness()
def calculateIntraStringUnhappiness(self): def calculateIntraStringUnhappiness(self):
values = [o.relativeImportance * o.intraStringUnhappiness value = sum(o.relativeImportance * o.intraStringUnhappiness
for o in self.objects] for o in self.objects) / 2.0
value = sum(values) / 2.0
self.intraStringUnhappiness = min(value, 100.0) self.intraStringUnhappiness = min(value, 100.0)
def calculateInterStringUnhappiness(self): def calculateInterStringUnhappiness(self):
values = [o.relativeImportance * o.interStringUnhappiness value = sum(o.relativeImportance * o.interStringUnhappiness
for o in self.objects] for o in self.objects) / 2.0
value = sum(values) / 2.0
self.interStringUnhappiness = min(value, 100.0) self.interStringUnhappiness = min(value, 100.0)
def calculateTotalUnhappiness(self): def calculateTotalUnhappiness(self):
for o in self.objects: for o in self.objects:
logging.info("%s, totalUnhappiness: %d, relativeImportance: %d", logging.info("%s, totalUnhappiness: %d, relativeImportance: %d",
o, o.totalUnhappiness, o.relativeImportance * 1000) o, o.totalUnhappiness, o.relativeImportance * 1000)
values = [o.relativeImportance * o.totalUnhappiness value = sum(o.relativeImportance * o.totalUnhappiness
for o in self.objects] for o in self.objects) / 2.0
value = sum(values) / 2.0
self.totalUnhappiness = min(value, 100.0) self.totalUnhappiness = min(value, 100.0)
def updateEverything(self): def updateEverything(self):
@ -87,9 +85,6 @@ class Workspace(object):
self.initial.updateIntraStringUnhappiness() self.initial.updateIntraStringUnhappiness()
self.target.updateIntraStringUnhappiness() self.target.updateIntraStringUnhappiness()
def otherObjects(self, anObject):
return [o for o in self.objects if o != anObject]
def numberOfUnrelatedObjects(self): def numberOfUnrelatedObjects(self):
"""A list of all objects in the workspace with >= 1 open bond slots""" """A list of all objects in the workspace with >= 1 open bond slots"""
objects = [o for o in self.objects objects = [o for o in self.objects

View File

@ -185,22 +185,20 @@ class WorkspaceObject(WorkspaceStructure):
def getDescriptor(self, descriptionType): def getDescriptor(self, descriptionType):
"""The description attached to this object of the description type.""" """The description attached to this object of the description type."""
descriptor = None
logging.info("\nIn %s, trying for type: %s", logging.info("\nIn %s, trying for type: %s",
self, descriptionType.get_name()) self, descriptionType.get_name())
for description in self.descriptions: for description in self.descriptions:
logging.info("Trying description: %s", description) logging.info("Trying description: %s", description)
if description.descriptionType == descriptionType: if description.descriptionType == descriptionType:
return description.descriptor return description.descriptor
return descriptor return None
def getDescriptionType(self, sought_description): def getDescriptionType(self, sought_description):
"""The description_type attached to this object of that description""" """The description_type attached to this object of that description"""
for description in self.descriptions: for description in self.descriptions:
if description.descriptor == sought_description: if description.descriptor == sought_description:
return description.descriptionType return description.descriptionType
description = None return None
return description
def getCommonGroups(self, other): def getCommonGroups(self, other):
return [o for o in self.string.objects return [o for o in self.string.objects

View File

@ -56,7 +56,7 @@ class WorkspaceString(object):
def updateRelativeImportance(self): def updateRelativeImportance(self):
"""Update the normalised importance of all objects in the string""" """Update the normalised importance of all objects in the string"""
total = sum([o.rawImportance for o in self.objects]) total = sum(o.rawImportance for o in self.objects)
if not total: if not total:
for o in self.objects: for o in self.objects:
o.relativeImportance = 0.0 o.relativeImportance = 0.0
@ -71,7 +71,7 @@ class WorkspaceString(object):
if not len(self.objects): if not len(self.objects):
self.intraStringUnhappiness = 0.0 self.intraStringUnhappiness = 0.0
return return
total = sum([o.intraStringUnhappiness for o in self.objects]) total = sum(o.intraStringUnhappiness for o in self.objects)
self.intraStringUnhappiness = total / len(self.objects) self.intraStringUnhappiness = total / len(self.objects)
def equivalentGroup(self, sought): def equivalentGroup(self, sought):