def initialize_state(): sumoProcessCmd = run_sumo(SUMO) traci.init(PORT) select_valid_edges() genetic.initialize() state.initialized = True traci.close() sumoProcessCmd.wait()
#! /usr/bin/env python def fitness_function(bin_string): from expression import eval_bin_string val = eval_bin_string(bin_string) if val == required_int: ret = float("inf") else: ret = 1 / abs(float(val - required_int)) return ret if __name__ == '__main__': import genetic from expression import eval_bin_string, conv_to_expression global required_int required_int = int(raw_input('Input integer: ')) genetic.initialize(50, 4, 100, fitness_function, 0.7, 0.001) while True: fittest = genetic.get_fittest(5) print "Most fittest:", for v in fittest: print eval_bin_string(v), print if eval_bin_string(fittest[0]) == required_int: break genetic.next_generation() print "Found expression: %s" % conv_to_expression(genetic.get_fittest(1)[0])
#! /usr/bin/env python def fitness_function(bin_string): from expression import eval_bin_string val = eval_bin_string(bin_string) if val == required_int: ret = float("inf") else: ret = 1 / abs(float(val - required_int)) return ret if __name__ == '__main__': import genetic from expression import eval_bin_string, conv_to_expression global required_int required_int = int(raw_input('Input integer: ')) genetic.initialize(50, 4, 100, fitness_function, 0.7, 0.001) while True: fittest = genetic.get_fittest(5) print "Most fittest:", for v in fittest: print eval_bin_string(v), print if eval_bin_string(fittest[0]) == required_int: break genetic.next_generation() print "Found expression: %s" % conv_to_expression( genetic.get_fittest(1)[0])
def test_initialize(): """Test population initalization""" population = genetic.initialize() assert(isinstance(population, list)) assert(len(population) == genetic.individuals)
import genetic import fitness import mock import random import pytest """Pytest TDD Test definition file""" __author__ = "Ernesto Serrano" __license__ = "GPLv3" __email__ = "*****@*****.**" population = genetic.initialize() def test_individual(): """Test individual generation""" config = genetic.individual() assert (isinstance(config, list)) assert (len(config) == genetic.genes) def test_initialize(): """Test population initalization""" assert (isinstance(population, list)) assert (len(population) == genetic.individuals) @mock.patch('fitness.calculate_fitness', return_value=random.randint(0, 999)) def test_selection_and_reproduction(function): """Test selection and reproduction""" local_population = [(i[0], i) for i in population]