Skip to content

Instantly share code, notes, and snippets.

@zenfiric
Created September 11, 2017 12:59
Show Gist options
  • Save zenfiric/042530eefffe094303b27271c3625fe6 to your computer and use it in GitHub Desktop.
Save zenfiric/042530eefffe094303b27271c3625fe6 to your computer and use it in GitHub Desktop.
@trollius.coroutine
def generate_population(self, n):
"""
Generates population of `n` valid robots robots.
:param n: Number of robots
:return: Future with a list of valid robot trees and corresponding
bounding boxes.
"""
logger.debug("Generating population of size %d..." % n)
trees = []
bboxes = []
# for _ in xrange(n):
while n - len(trees) > 0:
gen = yield From(self.generate_valid_robot())
if not gen:
raise Return(None)
tree, robot, bbox = gen
hinge_count = self._count_hinges(robot.body.root)
if hinge_count < 5:
logger.error("Robot has {} hinges, trees {}".format(
hinge_count, len(trees)))
continue
trees.append(tree)
bboxes.append(bbox)
raise Return(trees, bboxes)
def _count_hinges(self, proto_bot):
hinge_count = 0
if proto_bot.type == "ActiveHinge":
hinge_count += 1
if len(proto_bot.child) > 0:
for proto_child in proto_bot.child:
hinge_count += self._count_hinges(proto_child.part)
return hinge_count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment