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.targetDescriptor.buffer = 100.0
mappings = correspondence.distinguishingConceptMappings()
urgency = sum([mapping.strength() for mapping in mappings])
urgency = sum(mapping.strength() for mapping in mappings)
numberOfMappings = len(mappings)
if urgency:
urgency /= numberOfMappings
@ -287,37 +287,33 @@ class CodeRack(object):
def chooseCodeletToRun(self):
if not self.codelets:
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: ")
workspace.target.log("Target: ")
#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: ")
#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:
urgencySum += codelet.urgency ** scale
if not chosen and urgencySum > threshold:
if urgencySum > threshold:
chosen = codelet
break
if not chosen:
chosen = self.codelets[0]
self.removeCodelet(chosen)
logging.info('chosen codelet\n\t%s, urgency = %s',
chosen.name, chosen.urgency)

View File

@ -88,7 +88,7 @@ class CoderackPressures(object):
#logging.debug('coderackPressures.calculatePressures()')
scale = (100.0 - Temperature + 10.0) / 15.0
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
)
totalValue = sum(values) or 1.0

View File

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

View File

@ -32,7 +32,9 @@ class Description(WorkspaceStructure):
def localSupport(self):
from workspace import workspace
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):
continue
for description in other.descriptions:

View File

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

View File

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

View File

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

View File

@ -56,7 +56,7 @@ class WorkspaceString(object):
def updateRelativeImportance(self):
"""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:
for o in self.objects:
o.relativeImportance = 0.0
@ -71,7 +71,7 @@ class WorkspaceString(object):
if not len(self.objects):
self.intraStringUnhappiness = 0.0
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)
def equivalentGroup(self, sought):