Answered step by step
Verified Expert Solution
Link Copied!

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 xml.dom.minidom import getDOMImplementation
from enum import Enum
import numpy as np
class MotorType(Enum):
PULSE =1
SINE =2
class Motor:
def __init__(self, control_waveform, control_amp, control_freq):
if control_waveform <=0.5:
self.motor_type = MotorType.PULSE
else:
self.motor_type = MotorType.SINE
self.amp = control_amp
self.freq = control_freq
self.phase =0
def get_output(self):
self.phase =(self.phase + self.freq)%(np.pi *2)
if self.motor_type == MotorType.PULSE:
if self.phase < np.pi:
output =1
else:
output =-1
if self.motor_type == MotorType.SINE:
output = np.sin(self.phase)
return output
class Creature:
def __init__(self, gene_count):
self.spec = genome.Genome.get_gene_spec()
self.dna = genome.Genome.get_random_genome(len(self.spec), gene_count)
self.flat_links = None
self.exp_links = None
self.motors = None
self.start_position = None
self.last_position = None
def get_flat_links(self):
if self.flat_links == None:
gdicts = genome.Genome.get_genome_dicts(self.dna, self.spec)
self.flat_links = genome.Genome.genome_to_links(gdicts)
return self.flat_links
def get_expanded_links(self):
self.get_flat_links()
if self.exp_links is not None:
return self.exp_links
exp_links =[self.flat_links[0]]
genome.Genome.expandLinks(self.flat_links[0],
self.flat_links[0].name,
self.flat_links,
exp_links)
self.exp_links = exp_links
return self.exp_links
def to_xml(self):
self.get_expanded_links()
domimpl = getDOMImplementation()
adom = domimpl.createDocument(None, \"start\", None)
robot_tag = adom.createElement(\"robot\")
for link in self.exp_links:
robot_tag.appendChild(link.to_link_element(adom))
first = True
for link in self.exp_links:
if first:# skip the root node!
first = False
continue
robot_tag.appendChild(link.to_joint_element(adom))
robot_tag.setAttribute(\"name\", \"pepe\") # choose a name!
return \'\'+ robot_tag.toprettyxml()
def get_motors(self):
self.get_expanded_links()
if self.motors == None:
motors =[]
for i in range(1, len(self.exp_links)):
l = self.exp_links[i]
m = Motor(l.control_waveform, l.control_amp, l.control_freq)
motors.append(m)
self.motors = motors
return self.motors
def update_position(self, pos):
if self.start_position == None:
self.start_position = pos
else:
self.last_position = pos
def get_distance_travelled(self):
if self.start_position is None or self.last_position is None:
return 0
p1= np.asarray(self.start_position)
p2= np.asarray(self.last_position)
dist = np.linalg.norm(p1-p2)
return dist
def update_dna(self, dna):
self.dna = dna
self.flat_links = None
self.exp_links = None
self.motors = None
self.start_position = None
self.last_position = None

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Structures and Algorithms in Python

Authors: Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser

1st edition

1118290275, 1-118-54958-2, 978-1118290279

More Books

Students also viewed these Programming questions

Question

What are the characteristics of a virtual channel?

Answered: 1 week ago

Question

Define computer security.

Answered: 1 week ago