Skip to content

Commit

Permalink
More bugfixes and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
the-code-magician committed Sep 3, 2015
1 parent 27cfae7 commit 1ea69b5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
4 changes: 2 additions & 2 deletions cameo/strain_design/heuristic/generators.py
Expand Up @@ -122,7 +122,7 @@ def multiple_chromosome_set_generator(random, args):
return candidate


def linear_representation(random, args):
def linear_set_generator(random, args):
"""
Generates a list continuous values of the size of a representation.
This function requires that a bounder is defined on the EvolutionaryAlgorithm.
Expand Down Expand Up @@ -157,5 +157,5 @@ def linear_representation(random, args):
size = max_size

indices = random.sample(range(len(representation)), size)
values = [random.uniform(bounder.lower_bound, bounder.upper_bound) for _ in indices]
values = random.uniform(next(bounder.lower_bound), next(bounder.upper_bound), len(indices))
return [(i, v) for i, v in zip(indices, values)]
3 changes: 3 additions & 0 deletions cameo/util.py
Expand Up @@ -161,6 +161,9 @@ def __getstate__(self):
def __setstate__(self, d):
self._random = d['_random']

def uniform(self, low=0.0, high=1.0, size=None):
return self._random.uniform(low, high, size)


class Singleton(object):
"""
Expand Down
21 changes: 18 additions & 3 deletions tests/test_strain_design_heuristics.py
Expand Up @@ -13,12 +13,14 @@
# limitations under the License.

from __future__ import absolute_import, print_function
from collections import namedtuple
from math import sqrt

import os
import unittest
import inspyred
import pickle
from inspyred.ec import Bounder
from ordered_set import OrderedSet

from pandas.util.testing import assert_frame_equal
Expand All @@ -37,7 +39,7 @@
from cameo.strain_design.heuristic.archivers import SolutionTuple, BestSolutionArchiver
from cameo.strain_design.heuristic.decoders import ReactionKnockoutDecoder, KnockoutDecoder, GeneKnockoutDecoder
from cameo.strain_design.heuristic.generators import set_generator, unique_set_generator, \
multiple_chromosome_set_generator
multiple_chromosome_set_generator, linear_set_generator
from cameo.strain_design.heuristic.objective_functions import biomass_product_coupled_yield, product_yield, \
number_of_knockouts
from cobra.manipulation.delete import find_gene_knockout_reactions
Expand Down Expand Up @@ -140,6 +142,7 @@ def test_solution_comparison_minimization(self):
self.assertEqual(sol1.__cmp__(sol2), -1)
self.assertEqual(sol1.__cmp__(sol1), 0)
self.assertEqual(sol1.__cmp__(sol3), -1)
self.assertEqual(sol3.__cmp__(sol1), 1)

self.assertTrue(sol1 < sol2)
self.assertTrue(sol1 == sol1)
Expand Down Expand Up @@ -359,6 +362,8 @@ def test_gene_knockout_decoder(self):


class TestGenerators(unittest.TestCase):
mockup_evolutionary_algorithm = namedtuple("EA", ["bounder"])

def setUp(self):
self.model = TEST_MODEL
self.args = {}
Expand Down Expand Up @@ -394,7 +399,7 @@ def test_multiple_chromossome_set_generator(self):
self.assertEqual(len(candidate['test_key_1']), 3)
self.assertEqual(len(candidate['test_key_2']), 5)

def test_fixed_size_generator(self):
def test_fixed_size_set_generator(self):
self.args.setdefault('variable_candidate_size', False)

self.args['candidate_size'] = 10
Expand All @@ -411,7 +416,7 @@ def test_fixed_size_generator(self):
candidate = unique_set_generator(self.random, self.args)
self.assertEqual(len(candidate), 20)

def test_variable_size_generator(self):
def test_variable_size_set_generator(self):
self.args.setdefault('variable_candidate_size', True)

self.args['candidate_size'] = 10
Expand All @@ -428,6 +433,16 @@ def test_variable_size_generator(self):
candidate = unique_set_generator(self.random, self.args)
self.assertLessEqual(len(candidate), 20)

def test_fixed_size_linear_set_generator(self):
ec = self.mockup_evolutionary_algorithm(Bounder(-10, 10))
self.args.setdefault('variable_candidate_size', True)
self.args['candidate_size'] = 10
self.args['_ec'] = ec
for _ in range(10000):
candidate = linear_set_generator(self.random, self.args)
self.assertTrue(all(isinstance(c[0], int) and isinstance(c[1], float) for c in candidate))
self.assertLessEqual(len(candidate), 10)


class TestHeuristicOptimization(unittest.TestCase):
def setUp(self):
Expand Down

0 comments on commit 1ea69b5

Please sign in to comment.