Commit 49536792 authored by David A.. Werner's avatar David A.. Werner
Browse files

bug fixes. implement load. auto-infer muscle strength

parent 88063fae
import numpy
import numpy as np
import StringIO
import functools
class ManducaDeformity(Exception):
class ManducaDeformityError(Exception):
def __init__(self, message):
self.message = message
def __str__(self):
......@@ -9,6 +10,7 @@ class ManducaDeformity(Exception):
class Manduca(object):
def __init__(self, legs, muscles, time_step):
self._legs = self._muscles = None
self.legs = np.copy(legs)
self.muscles = np.copy(muscles)
self._time_step = time_step
......@@ -26,7 +28,7 @@ class Manduca(object):
if new_legs.shape != self._legs.shape:
msg = 'Ill-defined leg shape. Expected {} but got {}'
e = msg.format(self._legs.shape, new_legs.shape)
raise(ManducaDeformity(e))
raise(ManducaDeformityError(e))
self._legs = new_legs
self._fitness = None
......@@ -41,7 +43,7 @@ class Manduca(object):
if new_muscles.shape != self._muscles.shape:
msg = 'Ill-defined muscles shape. Expected {} but got {}'
e = msg.format(self._muscles.shape, new_muscles.shape)
raise(ManducaDeformity(e))
raise(ManducaDeformityError(e))
self._muscles = new_muscles
self._fitness = None
......@@ -68,7 +70,7 @@ class Manduca(object):
if leg_ts != muscle_ts:
msg = 'Arbitrary number of time-steps. {} in legs, {} in muscles'
e = msg.format(leg_ts, muscle_ts)
raise(ManducaDeformity(e))
raise(ManducaDeformityError(e))
return leg_ts
@property
......@@ -80,16 +82,17 @@ class Manduca(object):
@property
def initial_position(self):
return numpy.arange(self.num_legs)*500
return np.arange(self.num_legs)*500
def check_consistancy(self):
if self.num_muscles != self.num_legs - 1:
msg = 'Ill-defined manduca has {} legs and {} muscles'
e = msg.format(self.num_legs, self.num_muscles))
raise(ManducaDeformity(e))
e = msg.format(self.num_legs, self.num_muscles)
raise(ManducaDeformityError(e))
# Make sure the number of time steps is consistant
self.num_time_steps
def __repr__(self):
output = StringIO.StringIO()
output.write("\n legs | muscles")
......@@ -122,17 +125,26 @@ class Manduca(object):
if compressed:
save_fn = np.savez_compressed
else:
save_fn = np.savez
save_fn = np.savez
save_fn(file_name, legs=self.legs, muscles=self.muscles,
time_step=self.time_step,fitness=self.fitness)
time_step=self.time_step,fitness=self._fitness)
@classmethod
def load(cls, file_name):
def load(cls, file_name, *other_params, **kwargs):
try:
results = np.load(file_name)
except IOError, ValueError as e:
print('Error loading {} from file {}'.format(cls.__name__, file_name)
legs = results['legs']
muscles = results['muscles']
time_step = results['time_step']
args =[]
for param in other_params:
args.append(results[param])
new_manduca = cls(legs, muscles, time_step, *args, **kwargs)
if 'fitness' in results:
new_manduca._fitness = np.asscalar(results['fitness'])
return new_manduca
except (IOError, ValueError) as e:
print('Error loading {} from file {}'.format(cls.__name__, file_name))
raise(e)
def clone(self):
......@@ -140,14 +152,17 @@ class Manduca(object):
class SimpleManduca(Manduca):
"""A Manduca that has legs and muscles that are either ON/OFF"""
def __init__(self, legs, muscles, time_step, muscle_strength=100):
super().__init__(legs, muscles, time_step)
def __init__(self, legs, muscles, time_step, muscle_strength=None):
super(SimpleManduca, self).__init__(legs, muscles, time_step)
assert ((self.legs==1)|(self.legs==0)).all(), "Legs must be 0 or 1"
if muscle_strength is None:
assert self.muscles.max() != 0, "Cannot detemine muscle_strength when all values are 0"
muscle_strength = self.muscles.max()
assert ((self.muscles==0)|(self.muscles==muscle_strength)).all(), \
"Muscles must be 0 or {}".format(muscle_strength)
@staticmethod
def random_individual(num_legs, time_segments, time_step, muscle_strength, rng=numpy.random):
def random_individual(num_legs, time_segments, time_step, muscle_strength=100, rng=np.random):
legs = rng.choice([0,1],size=(time_segments, num_legs))
muscles = rng.choice([0,muscle_strength],size=(time_segments, num_legs-1))
return SimpleManduca(legs, muscles, time_step, muscle_strength=muscle_strength)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment