Example #1
0
def main():
    config = get_config()

    evo = Evolution(
        "state.json",
        config["boiler_pin"],
        config["steam_pin"],
        config["heater_pin"],
        config["pump_pin"],
        config["update_interval"],
    )

    server = websockets.serve(evo.websocket_handler, "127.0.0.1",
                              config["websocket_port"])

    try:
        loop = asyncio.get_event_loop()
        loop.run_until_complete(asyncio.wait([server, evo.heater_handler()]))
    except KeyboardInterrupt:
        print("Exiting on KeyboardInterrupt.")
    except Exception as ex:
        print(f"Exiting on unhandled exeption: '{ex}'.")
    finally:
        GPIO.cleanup()
        save_state(evo.state)
Example #2
0
    def test_compute_potential_positions_for_opponent_type_1(self):

        evolution: Evolution = Evolution()

        # Test 1

        column: [str] = ['.', '.', 'X', '.', '.', 'X', '.', '.', 'X', '.', '.', 'X', '.', 'X']

        result: int = evolution.compute_potential_positions_for_opponent_type_1(column)

        expected_result = [1, 4, 7, 10, 12]

        self.assertEqual(result, expected_result)

        # Test 2

        column : [str] = ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'X']

        result: int = evolution.compute_potential_positions_for_opponent_type_1(column)
        
        expected_result: [int] = [12]

        self.assertEqual(result, expected_result)

        # Test 3

        column : [str] = ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'X', 'X', 'X', 'X']

        result: int = evolution.compute_potential_positions_for_opponent_type_1(column)
        
        expected_result: [int] = [9]

        self.assertEqual(result, expected_result)
Example #3
0
def run():
    parser = argparse.ArgumentParser(allow_abbrev=False)
    parser.add_argument('--repository', action='store', type=str, required=True)
    args = parser.parse_args()
    path_to_repo = args.repository
    topic = path_to_repo.split("/")
    path_to_results = f'topic_analysis_results/{topic[1]}'
    Evolution().whole_evolution_with_try_except_tracking(path_to_repo, topic[1])
    ResultExtraction().current_state(path_to_results)
Example #4
0
    def test_select_column(self):
        
        np.random.seed(2)

        random_distribution: [float] = np.random.rand(5)

        position, random_distribution = Evolution().select_column(random_distribution)
    
        self.assertEqual(position, 2)

        clean_up()
Example #5
0
    def test_execute2(self):

        set_up_two_sample_users()

        result: str = Evolution().execute('user_002')   

        
        # Assuming that the hightest session stored for
        # the sample user should be 'session_042'.
        
        self.assertEqual(result, 'session_003')

        clean_up()
Example #6
0
    def test_execute(self):

        set_up_training_data()

        result: str = Evolution().execute('user_001')

        
        # Assuming that the hightest session stored for
        # the sample user should be 'session_042'.
        
        self.assertEqual(result, 'session_004')

        clean_up()
Example #7
0
    def test_select_row(self):
       
        evolution: Evolution = Evolution()

        # Test 1

        row : [str] = ['.', '.', '.', '.', '.', 'X', '.', '.', 'X', '.', '.', 'X', '.', 'X']

        result: int = evolution.select_row(row, 'F')
        
        expected_result: int = 0

        self.assertEqual(result, expected_result)

        # Test 2

        row : [str] = ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'X']

        result: int = evolution.select_row(row, 'C')
        
        expected_result: int = 12

        self.assertEqual(result, expected_result)

        # Test 3

        row : [str] = ['X', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.']

        result: int = evolution.select_row(row, 'C')
        
        expected_result: int = 404

        self.assertEqual(result, expected_result)

        # Test 4

        row : [str] = ['.', 'X', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.']

        result: int = evolution.select_row(row, 'C')
        
        expected_result: int = 0

        self.assertEqual(result, expected_result)
Example #8
0
    def test_compute_potential_positions_for_opponent_type_3(self):

        evolution: Evolution = Evolution()

        # Test 1

        column: [str] = ['.', '.', 'X', '.', '.', 'X', '.', '.', 'X', '.', '.', 'X', '.', 'X']

        result: int = evolution.compute_potential_positions_for_opponent_type_3(column)

        expected_result = []

        self.assertEqual(result, expected_result)

        # Test 2

        column: [str] = ['.', '.', '.', '.', '.', 'X', '.', '.', 'X', '.', '.', 'X', '.', 'X']

        result: int = evolution.compute_potential_positions_for_opponent_type_3(column)

        expected_result = [0]

        self.assertEqual(result, expected_result)

        # Test 3

        column: [str] = ['.', '.', '.', '.', '.', '.', '.', '.', 'X', '.', '.', 'X', '.', 'X']

        result: int = evolution.compute_potential_positions_for_opponent_type_3(column)

        expected_result = [0, 1, 2, 3]

        self.assertEqual(result, expected_result)

        # Test 4

        column: [str] = ['.', 'X', '.', 'X', '.', 'X', 'X', '.', '.', '.', '.', '.', '.', '.']

        result: int = evolution.compute_potential_positions_for_opponent_type_3(column)

        expected_result = [7]

        self.assertEqual(result, expected_result)
Example #9
0
def evolution_cloud_function(request):

    import os
    import sys
    import json

    sys.path.insert(
        0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

    from models.user import User
    from evolution.evolution import Evolution
    from models.level import Level

    from normalization.normalization import Normalization

    request_json = request.get_json(silent=True)
    request_args = request.args

    # Declares a variable to store a user key.
    user_key: str

    if request_json and 'user_key' in request_json:

        user_key = request_json['user_key']

    elif request_args and 'user_key' in request_args:

        user_key = request_args['user_key']

    else:

        user_key = 'User not found.'

    # Executes the actual evolution process by creating
    # an Evolution objects and calling the execute function
    # the retrieved user key.
    session_key: str = Evolution().execute(user_key)

    # Retruns a simple success message after the evolution
    # process has been terminated.
    return f'Session {session_key} has been created.'
Example #10
0
    def initialize_levels(self):

        database: Database = Database()
        evolution: Evolution = Evolution()

        level_01: Level = Level(
            'level_01', '01',
            TextFileReader().read_text_file('level_01.txt'))
        level_02: Level = Level(
            'level_02', '02',
            TextFileReader().read_text_file('level_02.txt'))
        level_03: Level = Level(
            'level_03', '03',
            TextFileReader().read_text_file('level_03.txt'))
        level_04: Level = Level(
            'level_04', '04',
            TextFileReader().read_text_file('level_04.txt'))
        level_05: Level = Level(
            'level_05', '05',
            TextFileReader().read_text_file('level_05.txt'))
        level_06: Level = Level(
            'level_06', '06',
            TextFileReader().read_text_file('level_06.txt'))
        level_07: Level = Level(
            'level_07', '07',
            TextFileReader().read_text_file('level_07.txt'))
        level_08: Level = Level(
            'level_08', '08',
            TextFileReader().read_text_file('level_08.txt'))
        tutorial: Level = Level(
            'tutorial', 'tutorial',
            TextFileReader().read_text_file('tutorial.txt'))

        level_01 = Helper.clean_level(level_01)
        level_02 = Helper.clean_level(level_02)
        level_03 = Helper.clean_level(level_03)
        level_04 = Helper.clean_level(level_04)
        level_05 = Helper.clean_level(level_05)
        level_06 = Helper.clean_level(level_06)
        level_07 = Helper.clean_level(level_07)
        level_08 = Helper.clean_level(level_08)

        database.store_level_prototype(level_01)
        database.store_level_prototype(level_02)
        database.store_level_prototype(level_03)
        database.store_level_prototype(level_04)
        database.store_level_prototype(level_05)
        database.store_level_prototype(level_06)
        database.store_level_prototype(level_07)
        database.store_tutorial(tutorial)

        level_01 = evolution.evolve('random_distribution', level_01)
        level_02 = evolution.evolve('random_distribution', level_02)
        level_03 = evolution.evolve('random_distribution', level_03)
        level_04 = evolution.evolve('random_distribution', level_04)
        level_05 = evolution.evolve('random_distribution', level_05)
        level_06 = evolution.evolve('random_distribution', level_06)
        level_07 = evolution.evolve('random_distribution', level_07)
        level_08 = evolution.evolve('random_distribution', level_08)

        database.store_initial_level(level_01)
        database.store_initial_level(level_02)
        database.store_initial_level(level_03)
        database.store_initial_level(level_04)
        database.store_initial_level(level_05)
        database.store_initial_level(level_06)
        database.store_initial_level(level_07)
        database.store_initial_level(level_08)
from evolution.evolution import Evolution
import time

if __name__ == '__main__':
    start_time = time.time()
    Evolution().whole_evolution_multiple_repositories('Python_api')
    Evolution().whole_evolution_multiple_repositories('Python_application')
    Evolution().whole_evolution_multiple_repositories(
        'Python_artificial-intelligence')
    Evolution().whole_evolution_multiple_repositories('Python_database')
    Evolution().whole_evolution_multiple_repositories('Python_django')
    Evolution().whole_evolution_multiple_repositories('Python_flask')
    Evolution().whole_evolution_multiple_repositories('Python_library')
    Evolution().whole_evolution_multiple_repositories(
        'Python_machine_learning')

    end_time = time.time()
    print(end_time - start_time)
                       mutations.note_to_prolongation_mutation,
                       mutations.prolongation_to_note_mutation,
                       mutations.long_to_short_ratio_mutation)
reproduction_function = one_point_crossover
fitness_function = statistical_fitness_function
selection_function = roulette_selection
population_generation_function = generate_music_vectors

POPULATION_SIZE = 10
PROBS = utils.PROBS
SONG_SIZE = 64
population_kwargs = {
    'song_size': SONG_SIZE,
    'population_size': POPULATION_SIZE,
    'probabilities': PROBS
}

evolution_model = Evolution(mutations_functions,
                            reproduction_function,
                            fitness_function,
                            selection_function,
                            population_generation_function,
                            tonality=tonality.F_MAJOR)

evolution_model.initialize_population(population_kwargs)
evolution_model.start(n_epochs=10000)
songs = evolution_model.get_population(n_best=10)
songs = np.array(songs)

np.save('generated_songs.npy', songs)