Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Creature.py import genome from xml . dom.minidom import getDOMImplementation from enum import Enum import numpy as np class MotorType ( Enum ) : PULSE =
Creature.pyÂ
import genome
from xmldom.minidom import getDOMImplementation
from enum import Enum
import numpy as np
class MotorTypeEnum:
PULSE
SINE
class Motor:
def initself controlwaveform, controlamp, controlfreq:
if controlwaveform :
self.motortype MotorType.PULSE
else:
self.motortype MotorType.SINE
self.amp controlamp
self.freq controlfreq
self.phase
def getoutputself:
self.phase selfphase self.freqnppi
if self.motortype MotorType.PULSE:
if self.phase nppi:
output
else:
output
if self.motortype MotorType.SINE:
output npsinselfphase
return output
class Creature:
def initself genecount:
self.spec genome.Genome.getgenespec
self.dna genome.Genome.getrandomgenomelenselfspec genecount
self.flatlinks None
self.explinks None
self.motors None
self.startposition None
self.lastposition None
def getflatlinksself:
if self.flatlinks None:
gdicts genome.Genome.getgenomedictsselfdna, self.spec
self.flatlinks genome.Genome.genometolinksgdicts
return self.flatlinks
def getexpandedlinksself:
self.getflatlinks
if self.explinks is not None:
return self.explinks
explinks selfflatlinks
genome.Genome.expandLinksselfflatlinks
self.flatlinksname,
self.flatlinks,
explinks
self.explinks explinks
return self.explinks
def toxmlself:
self.getexpandedlinks
domimpl getDOMImplementation
adom domimpl.createDocumentNone \"start\", None
robottag adom.createElementrobot
for link in self.explinks:
robottag.appendChildlinktolinkelementadom
first True
for link in self.explinks:
if first:# skip the root node!
first False
continue
robottag.appendChildlinktojointelementadom
robottag.setAttributename \"pepe\" # choose a name!
return robottag.toprettyxml
def getmotorsself:
self.getexpandedlinks
if self.motors None:
motors
for i in range lenselfexplinks:
l self.explinksi
m Motorlcontrolwaveform, lcontrolamp, lcontrolfreq
motors.appendm
self.motors motors
return self.motors
def updatepositionself pos:
if self.startposition None:
self.startposition pos
else:
self.lastposition pos
def getdistancetravelledself:
if self.startposition is None or self.lastposition is None:
return
p npasarrayselfstartposition
p npasarrayselflastposition
dist nplinalg.normpp
return dist
def updatednaself dna:
self.dna dna
self.flatlinks None
self.explinks None
self.motors None
self.startposition None
self.lastposition None
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started