def test_calc_fitness(mocked_init, mocked_raise_on): planner = GAPlanner(None, None, None) planner._logger = ru.Logger('dummy') planner._campaign = [] planner._population = [[1, 3, 5, 6, -1, 2, 7, -1, 4, 8, 9], [1, 5, 6, -1, 3, 2, 7, -1, 4, 8, 9]] planner._est_txs = [[10, 10, 10], [10, 10, 10], [10, 10, 10], [10, 10, 10], [10, 10, 10], [10, 10, 10], [10, 10, 10], [10, 10, 10], [10, 10, 10], [10, 10, 10]] planner._abs_fitness_term = 30 planner._population_size = 2 planner._fitness = [] for i in range(9): workflow = { 'description': 'W%s' % str(i + 1), 'id': i + 1, 'num_oper': 10, 'requirements': None } planner._campaign.append(workflow) planner._resources = [{ 'id': 1, 'performance': 1 }, { 'id': 2, 'performance': 1 }, { 'id': 3, 'performance': 1 }] planner._calc_fitness() assert planner._fitness[1] == 1 assert planner._fitness[0] == (1 / math.sqrt(200))
def test_selection(mocked_init, mocked_raise_on): planner = GAPlanner(None, None, None) planner._logger = ru.Logger('dummy') planner._campaign = {'campaign': []} planner._population = [[1, 3, 5, 6, -1, 2, 7, -1, 4, 8, 9], [1, 5, 6, -1, 3, 2, 7, -1, 4, 8, 9]] planner._population_size = 2 planner._fitness = [1, 0.5] parents = planner._selection() assert (parents[0] == [1, 3, 5, 6, -1, 2, 7, -1, 4, 8, 9] or parents[0] == [1, 5, 6, -1, 3, 2, 7, -1, 4, 8, 9])
def test_get_makespan(mocked_init, mocked_raise_on): planner = GAPlanner(None, None, None) planner._logger = ru.Logger('dummy') planner._campaign = [ 'W1', 'W2', 'W3', 'W4', 'W5', 'W6', 'W7', 'W8', 'W9', 'W10' ] planner._resources = [{ 'id': 1, 'performance': 1 }, { 'id': 2, 'performance': 1 }, { 'id': 3, 'performance': 1 }] planner._num_oper = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10] planner._est_txs = [[10, 10, 10], [10, 10, 10], [10, 10, 10], [10, 10, 10], [10, 10, 10], [10, 10, 10], [10, 10, 10], [10, 10, 10], [10, 10, 10], [10, 10, 10]] makespan = planner._get_makespan([1, 3, 5, 6, -1, 2, 7, 0, -1, 4, 8, 9]) assert makespan == 40
def test_get_plan(mocked_init, mocked_raise_on): actual_plan = [({ 'description': 'W1', 'id': 0, 'num_oper': 10 }, { 'id': 2, 'performance': 1 }, 0, 10), ({ 'description': 'W2', 'id': 1, 'num_oper': 10 }, { 'id': 1, 'performance': 1 }, 0, 10), ({ 'description': 'W3', 'id': 2, 'num_oper': 10 }, { 'id': 2, 'performance': 1 }, 10, 20), ({ 'description': 'W4', 'id': 3, 'num_oper': 10 }, { 'id': 1, 'performance': 1 }, 10, 20), ({ 'description': 'W5', 'id': 4, 'num_oper': 10 }, { 'id': 3, 'performance': 1 }, 0, 10), ({ 'description': 'W6', 'id': 5, 'num_oper': 10 }, { 'id': 1, 'performance': 1 }, 20, 30), ({ 'description': 'W7', 'id': 6, 'num_oper': 10 }, { 'id': 1, 'performance': 1 }, 30, 40), ({ 'description': 'W8', 'id': 7, 'num_oper': 10 }, { 'id': 2, 'performance': 1 }, 20, 30), ({ 'description': 'W9', 'id': 8, 'num_oper': 10 }, { 'id': 3, 'performance': 1 }, 10, 20), ({ 'description': 'W10', 'id': 9, 'num_oper': 10 }, { 'id': 3, 'performance': 1 }, 20, 30)] planner = GAPlanner(None, None, None) planner._logger = ru.Logger('dummy') planner._campaign = [{ 'description': 'W1', 'id': 0, 'num_oper': 10 }, { 'description': 'W2', 'id': 1, 'num_oper': 10 }, { 'description': 'W3', 'id': 2, 'num_oper': 10 }, { 'description': 'W4', 'id': 3, 'num_oper': 10 }, { 'description': 'W5', 'id': 4, 'num_oper': 10 }, { 'description': 'W6', 'id': 5, 'num_oper': 10 }, { 'description': 'W7', 'id': 6, 'num_oper': 10 }, { 'description': 'W8', 'id': 7, 'num_oper': 10 }, { 'description': 'W9', 'id': 8, 'num_oper': 10 }, { 'description': 'W10', 'id': 9, 'num_oper': 10 }] planner._resources = [{ 'id': 1, 'performance': 1 }, { 'id': 2, 'performance': 1 }, { 'id': 3, 'performance': 1 }] planner._num_oper = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10] planner._est_txs = [[10, 10, 10], [10, 10, 10], [10, 10, 10], [10, 10, 10], [10, 10, 10], [10, 10, 10], [10, 10, 10], [10, 10, 10], [10, 10, 10], [10, 10, 10]] planner._get_plan([1, 3, 5, 6, -1, 2, 7, 0, -1, 4, 8, 9]) assert planner._plan == actual_plan actual_plan = [({ 'description': None, 'id': 1, 'num_oper': 75000 }, { 'id': 1, 'performance': 1 }, 0, 75000.0), ({ 'description': None, 'id': 2, 'num_oper': 75000 }, { 'id': 3, 'performance': 1 }, 0, 75000.0), ({ 'description': None, 'id': 3, 'num_oper': 75000 }, { 'id': 4, 'performance': 1 }, 0, 75000.0), ({ 'description': None, 'id': 4, 'num_oper': 75000 }, { 'id': 2, 'performance': 1 }, 0, 75000.0)] planner = GAPlanner(None, None, None) planner._logger = ru.Logger('dummy') planner._campaign = [{ 'description': None, 'id': 1, 'num_oper': 75000 }, { 'description': None, 'id': 2, 'num_oper': 75000 }, { 'description': None, 'id': 3, 'num_oper': 75000 }, { 'description': None, 'id': 4, 'num_oper': 75000 }] planner._resources = [{ 'id': 1, 'performance': 1 }, { 'id': 2, 'performance': 1 }, { 'id': 3, 'performance': 1 }, { 'id': 4, 'performance': 1 }] planner._num_oper = [75000, 75000, 75000, 75000] planner._est_txs = [[75000, 75000, 75000, 75000], [75000, 75000, 75000, 75000], [75000, 75000, 75000, 75000], [75000, 75000, 75000, 75000]] planner._get_plan([1, -1, 4, -1, 2, -1, 3]) assert planner._plan == actual_plan