Skip to content

Commit

Permalink
Merge pull request #32 from qbahn/feature/implementMaxCalls
Browse files Browse the repository at this point in the history
Add max_calls param
  • Loading branch information
wojtekPi committed Apr 11, 2016
2 parents 3758950 + bc82d16 commit 8c5af0b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 7 deletions.
5 changes: 5 additions & 0 deletions grortir/main/model/core/abstract_stage.py
Expand Up @@ -30,3 +30,8 @@ def get_cost():
def get_quality():
"""Return quality of stage."""
raise NotImplementedError

@staticmethod
def could_be_optimized():
"""Return answer if it is still possible to optimize that stage."""
raise NotImplementedError
Expand Up @@ -10,11 +10,13 @@ class CallsProcessFactory:
Attributes:
structure_type (str): represent type of structure
how_many_nodes (int): how many nodes should be in generated process
max_calls (int): max_calls for stages
process (CallsProcess): process which will be incrementally build
"""

def __init__(self, structure_type, how_many_nodes):
def __init__(self, structure_type, how_many_nodes, max_calls):
"""Constructor."""
self.max_calls = max_calls
self._stages_to_add = []
self.structure_type = structure_type
self.how_many_nodes = how_many_nodes
Expand All @@ -30,7 +32,7 @@ def _update_structure(self):
def _create_stages(self):
"""Create stages which will be injected to process."""
for i in range(self.how_many_nodes):
self._stages_to_add += [CallsStage(str(i))]
self._stages_to_add += [CallsStage(str(i), self.max_calls)]

def construct_process(self):
"""Construct process."""
Expand Down
8 changes: 7 additions & 1 deletion grortir/main/model/stages/calls_stage.py
Expand Up @@ -8,12 +8,14 @@ class CallsStage(AbstractStage):
Cost is calculated by number of calls of cost function.
Attributes:
cost (float): Actual cost of stage.
max_calls (int): Maximum possible calls of cost quality function
name (str): Name of stage
"""

def __init__(self, name, input_vector=()):
def __init__(self, name, max_calls, input_vector=()):
"""Constructor."""
super().__init__(input_vector)
self.max_calls = max_calls
self.name = name
self.control_params = [0] * len(self.input_vector)
self.cost = 0
Expand Down Expand Up @@ -55,3 +57,7 @@ def get_cost(self):
cost (float): cost
"""
return self.cost

def could_be_optimized(self):
"""Return answer if it is still possible to optimize that stage."""
return self.get_cost() < self.max_calls
6 changes: 6 additions & 0 deletions grortir/test/model/core/test_abstract_stage.py
Expand Up @@ -39,3 +39,9 @@ def test_get_quality(self):
tested_object = AbstractStage()
with self.assertRaises(NotImplementedError):
tested_object.get_quality()

def test_could_be_optimized(self):
"""Check could_be_optimized method."""
tested_object = AbstractStage()
with self.assertRaises(NotImplementedError):
tested_object.could_be_optimized()
Expand Up @@ -6,18 +6,20 @@
from grortir.main.model.processes.factories.calls_process_factory import \
CallsProcessFactory

MAX_CALLS = 1000


class TestCallsProcessFactory(TestCase):
"""Class to test CallsProcessFactory."""

def test_construct_process_linear(self):
"""Test linear process construction."""
tested_object = CallsProcessFactory("linear", 7)
tested_object = CallsProcessFactory("linear", 7, MAX_CALLS)
result = tested_object.construct_process()
self.assertIsInstance(result, CallsProcess)

def test_construct_process_not_ex(self):
"""Test case when structure not implemented."""
tested_object = CallsProcessFactory("not_existed", 7)
tested_object = CallsProcessFactory("not_existed", 7, MAX_CALLS)
with self.assertRaises(NotImplementedError):
tested_object.construct_process()
22 changes: 20 additions & 2 deletions grortir/test/model/stages/test_calls_stage.py
Expand Up @@ -5,6 +5,8 @@

from grortir.main.model.stages.calls_stage import CallsStage

MAX_CALLS = 100


class TestCallsStage(TestCase):
"""Test class for CallsStage."""
Expand All @@ -29,15 +31,31 @@ def test_get_cost(self):
def test_calculate_quality_ex(self):
"""Test case when control are wrong."""
input_vector = (2, 3, 4, 5, 6)
tested_object = CallsStage('name', input_vector)
tested_object = CallsStage('name', MAX_CALLS, input_vector)
tested_object.control_params = [2, 2]
with self.assertRaises(AssertionError):
tested_object.calculate_quality()

def test_calculate_quality_ok(self):
"""Test case when control params and input are okay."""
input_vector = (2, 3, 4, 5, 6, 1)
tested_object = CallsStage('name', input_vector)
tested_object = CallsStage('name', MAX_CALLS, input_vector)
tested_object.control_params = [1, 1, 1, 1, 1, 1.5]
result = tested_object.calculate_quality()
self.assertEqual(result, 55.25)

def test_could_be_optimized_pos(self):
"""Positive case for test could_be_optimized method."""
tested_object = Mock()
tested_object.get_cost.return_value = MAX_CALLS - 1
tested_object.max_calls = MAX_CALLS
result = CallsStage.could_be_optimized(tested_object)
self.assertTrue(result)

def test_could_be_optimized_neg(self):
"""Negative case for test could_be_optimized method."""
tested_object = Mock()
tested_object.get_cost.return_value = MAX_CALLS + 1
tested_object.max_calls = MAX_CALLS
result = CallsStage.could_be_optimized(tested_object)
self.assertFalse(result)

0 comments on commit 8c5af0b

Please sign in to comment.