diff --git a/.pylintrc b/.pylintrc index 9f5e841..c4c3a0c 100644 --- a/.pylintrc +++ b/.pylintrc @@ -9,7 +9,7 @@ # Add files or directories to the blacklist. They should be base names, not # paths. -ignore=CVS,test,tests +ignore=CVS,test,tests,scripts # Pickle collected data for later comparisons. persistent=yes diff --git a/grortir/main/logging/logging_configuration.py b/grortir/main/logging/logging_configuration.py index 0d212b0..c2e9a6a 100644 --- a/grortir/main/logging/logging_configuration.py +++ b/grortir/main/logging/logging_configuration.py @@ -37,6 +37,12 @@ def _get_dict_config(cls): 'formatter': 'standard', 'class': 'logging.handlers.RotatingFileHandler', 'filename': 'grortir.log' + }, + 'file_result': { + 'level': 'INFO', + 'formatter': 'standard', + 'class': 'logging.handlers.RotatingFileHandler', + 'filename': 'grortir-results.log' } }, 'loggers': { @@ -44,6 +50,11 @@ def _get_dict_config(cls): 'handlers': ['default', 'file'], 'level': 'DEBUG', 'propagate': True + }, + 'results_logger': { + 'handlers': ['file_result'], + 'level': 'DEBUG', + 'propagate': True } } } diff --git a/grortir/main/pso/pso_algorithm.py b/grortir/main/pso/pso_algorithm.py index b6ff427..4b3fa82 100644 --- a/grortir/main/pso/pso_algorithm.py +++ b/grortir/main/pso/pso_algorithm.py @@ -40,8 +40,15 @@ def run(self): def _post_processing(self): final_status = OptimizationStatus.success + statuses = "" + costs = "" for stage in self.grouping_strategy.ordered_stages: + statuses += str(stage.optimization_status) + ", " + costs += str(stage.get_cost()) + ", " if stage.optimization_status != OptimizationStatus.success: + LOG.debug("Failed stage: " + str(stage)) final_status = OptimizationStatus.failed self.process.optimization_status = final_status LOG.info('Final status of process optimization: ' + str(final_status)) + LOG.info('Statuses of stages: ' + statuses[:-3]) + LOG.info('Costs of stages: ' + costs[:-3]) diff --git a/grortir/main/pso/swarm.py b/grortir/main/pso/swarm.py index 4872fb6..60dd2b5 100644 --- a/grortir/main/pso/swarm.py +++ b/grortir/main/pso/swarm.py @@ -1,12 +1,9 @@ """Represents swarm.""" -import logging import numpy as np from grortir.main.pso.particle import Particle -LOG = logging.getLogger(__name__) - class Swarm(object): """Class which represent swarm.""" @@ -19,11 +16,9 @@ def __init__(self, process, stages, number_of_particles): range(number_of_particles)] self.best_particle_quality = np.inf self.best_particle = self.particles[0] - LOG.debug('Swarm created.') def initialize(self): """Initialize all particles in swarm.""" - LOG.debug('Initialize swarm.') for particle in self.particles: particle.initialize() @@ -39,12 +34,8 @@ def do_single_iteration(self): def _update_best_particle(self): for particle in self.particles: if particle.best_quality < self.best_particle_quality: - LOG.debug('Update best particle.') self.best_particle = particle self.best_particle_quality = particle.best_quality - LOG.debug('Current best quality is: ' + str( - self.best_particle_quality)) - LOG.debug('The best particle is: ' + str(self.best_particle.number)) def _update_velocieties(self): for particle in self.particles: @@ -69,7 +60,6 @@ def _post_process_run_stages(self, best_control_params): stage.final_cost = stage.get_cost() stage.final_quality = stage.get_quality( stage.input_vector, best_control_params[stage]) - LOG.debug('Final stage status: ' + str(stage)) def _post_process_not_run_stages(self): for stage in self.stages: @@ -79,5 +69,3 @@ def _post_process_not_run_stages(self): stage.final_output = None stage.final_cost = None stage.final_quality = None - LOG.debug('Optimization for this stage was not triggered.') - LOG.debug('Final stage status: ' + str(stage)) diff --git a/grortir/scripts/trees_simulations.py b/grortir/scripts/trees_simulations.py new file mode 100644 index 0000000..e85c6c4 --- /dev/null +++ b/grortir/scripts/trees_simulations.py @@ -0,0 +1,287 @@ +import logging + +from grortir.main.logging.logging_configuration import LoggingConfiguration +from grortir.main.model.core.optimization_status import OptimizationStatus +from grortir.main.model.processes.calls_process import CallsProcess +from grortir.main.model.stages.cumulated_calls_stage import CumulatedCallsStage +from grortir.main.optimizers.grouping_strategy import GroupingStrategy +from grortir.main.pso.calls_optimization_strategy import \ + CallsOptimizationStrategy +from grortir.main.pso.credit_calls_optimization_strategy import \ + CreditCallsOptimizationStrategy +from grortir.main.pso.pso_algorithm import PsoAlgorithm + +LOG = logging.getLogger(__name__) + + +def create_input_vector_for_cumulated_stages(dimensions): + length_of_input_vector = (dimensions + 1) + return (0,) * length_of_input_vector + + +def create_stages(how_many_stages, max_calls, input_vector, expected_quality): + return [CumulatedCallsStage(i, max_calls, input_vector, expected_quality) + for i in + range(how_many_stages)] + + +def create_edges_balanced(process, stages): + for i in range(7): + process.add_edge(stages[i], stages[2 * i + 1]) + process.add_edge(stages[i], stages[2 * i + 2]) + + +def create_edges_unbalanced(process, stages): + for i in range(3): + process.add_edge(stages[i], stages[2 * i + 1]) + process.add_edge(stages[i], stages[2 * i + 2]) + process.add_edge(stages[5], stages[7]) + process.add_edge(stages[5], stages[8]) + process.add_edge(stages[6], stages[9]) + process.add_edge(stages[6], stages[10]) + process.add_edge(stages[9], stages[11]) + process.add_edge(stages[9], stages[12]) + process.add_edge(stages[10], stages[13]) + process.add_edge(stages[10], stages[14]) + + +class PSO_algorithm_BFS_balanced_SEQ: + @staticmethod + def create(how_many_stages, max_calls, + input_vector, expected_quality, + how_many_particles): + bfs_stages = create_stages(how_many_stages, max_calls, input_vector, + expected_quality) + bfs_process = CallsProcess() + create_edges_balanced(bfs_process, bfs_stages) + bfs_ordered_stages = bfs_stages + bfs_grouping_strategy = GroupingStrategy(bfs_ordered_stages) + for stage in bfs_ordered_stages: + bfs_grouping_strategy.define_group([stage]) + bfs_optimization_strategy = CallsOptimizationStrategy() + return PsoAlgorithm(bfs_process, bfs_grouping_strategy, + bfs_optimization_strategy, how_many_particles) + + +class PSO_algorithm_BFS_balanced_SIM: + @staticmethod + def create(how_many_stages, max_calls, + input_vector, expected_quality, + how_many_particles): + bfs_stages = create_stages(how_many_stages, max_calls, input_vector, + expected_quality) + bfs_process = CallsProcess() + create_edges_balanced(bfs_process, bfs_stages) + bfs_ordered_stages = bfs_stages + bfs_grouping_strategy = GroupingStrategy(bfs_ordered_stages) + bfs_grouping_strategy.define_group(bfs_ordered_stages) + bfs_optimization_strategy = CallsOptimizationStrategy() + return PsoAlgorithm(bfs_process, bfs_grouping_strategy, + bfs_optimization_strategy, how_many_particles) + + +class PSO_algorithm_DFS_balanced_SEQ: + @staticmethod + def create(how_many_stages, max_calls, + input_vector, expected_quality, + how_many_particles): + dfs_stages = create_stages(how_many_stages, max_calls, input_vector, + expected_quality) + dfs_process = CallsProcess() + create_edges_balanced(dfs_process, dfs_stages) + dfs_ordered_stages = get_dfs_balanced_ordered_stages(dfs_stages) + dfs_grouping_strategy = GroupingStrategy(dfs_ordered_stages) + for stage in dfs_ordered_stages: + dfs_grouping_strategy.define_group([stage]) + dfs_optimization_strategy = CallsOptimizationStrategy() + return PsoAlgorithm(dfs_process, dfs_grouping_strategy, + dfs_optimization_strategy, how_many_particles) + + +class PSO_algorithm_DFS_CREDIT_balanced_SEQ: + @staticmethod + def create(how_many_stages, max_calls, + input_vector, + expected_quality, + how_many_particles): + dfs_stages = create_stages(how_many_stages, max_calls, input_vector, + expected_quality) + dfs_process = CallsProcess() + create_edges_balanced(dfs_process, dfs_stages) + dfs_ordered_stages = get_dfs_balanced_ordered_stages(dfs_stages) + dfs_grouping_strategy = GroupingStrategy(dfs_ordered_stages) + for stage in dfs_ordered_stages: + dfs_grouping_strategy.define_group([stage]) + dfs_optimization_strategy = CreditCallsOptimizationStrategy() + return PsoAlgorithm(dfs_process, dfs_grouping_strategy, + dfs_optimization_strategy, how_many_particles) + + +class PSO_algorithm_DFS_unbalanced_SEQ: + @staticmethod + def create(how_many_stages, max_calls, + input_vector, expected_quality, + how_many_particles): + dfs_stages = create_stages(how_many_stages, max_calls, input_vector, + expected_quality) + dfs_process = CallsProcess() + create_edges_unbalanced(dfs_process, dfs_stages) + dfs_ordered_stages = get_dfs_unbalanced_ordered_stages(dfs_stages) + dfs_grouping_strategy = GroupingStrategy(dfs_ordered_stages) + for stage in dfs_ordered_stages: + dfs_grouping_strategy.define_group([stage]) + dfs_optimization_strategy = CallsOptimizationStrategy() + return PsoAlgorithm(dfs_process, dfs_grouping_strategy, + dfs_optimization_strategy, how_many_particles) + + +class PSO_algorithm_DFS_CREDIT_unbalanced_SEQ: + @staticmethod + def create(how_many_stages, max_calls, + input_vector, + expected_quality, + how_many_particles): + dfs_stages = create_stages(how_many_stages, max_calls, input_vector, + expected_quality) + dfs_process = CallsProcess() + create_edges_unbalanced(dfs_process, dfs_stages) + dfs_ordered_stages = get_dfs_unbalanced_ordered_stages(dfs_stages) + dfs_grouping_strategy = GroupingStrategy(dfs_ordered_stages) + for stage in dfs_ordered_stages: + dfs_grouping_strategy.define_group([stage]) + dfs_optimization_strategy = CreditCallsOptimizationStrategy() + return PsoAlgorithm(dfs_process, dfs_grouping_strategy, + dfs_optimization_strategy, how_many_particles) + + +class PSO_algorithm_DFS_balanced_SIM: + @staticmethod + def create(how_many_stages, max_calls, + input_vector, expected_quality, + how_many_particles): + dfs_stages = create_stages(how_many_stages, max_calls, input_vector, + expected_quality) + dfs_process = CallsProcess() + create_edges_balanced(dfs_process, dfs_stages) + dfs_ordered_stages_balanced = get_dfs_balanced_ordered_stages( + dfs_stages) + dfs_grouping_strategy = GroupingStrategy(dfs_ordered_stages_balanced) + dfs_grouping_strategy.define_group(dfs_ordered_stages_balanced) + dfs_optimization_strategy = CallsOptimizationStrategy() + return PsoAlgorithm(dfs_process, dfs_grouping_strategy, + dfs_optimization_strategy, how_many_particles) + + +class PSO_algorithm_DFS_unbalanced_SIM: + @staticmethod + def create(how_many_stages, max_calls, + input_vector, expected_quality, + how_many_particles): + dfs_stages = create_stages(how_many_stages, max_calls, input_vector, + expected_quality) + dfs_process = CallsProcess() + create_edges_unbalanced(dfs_process, dfs_stages) + dfs_ordered_stages_unbalanced = get_dfs_unbalanced_ordered_stages( + dfs_stages) + dfs_grouping_strategy = GroupingStrategy(dfs_ordered_stages_unbalanced) + dfs_grouping_strategy.define_group(dfs_ordered_stages_unbalanced) + dfs_optimization_strategy = CallsOptimizationStrategy() + return PsoAlgorithm(dfs_process, dfs_grouping_strategy, + dfs_optimization_strategy, how_many_particles) + + +def get_dfs_balanced_ordered_stages(stages): + return [ + stages[0], + stages[1], + stages[3], + stages[7], + stages[8], + stages[4], + stages[9], + stages[10], + stages[2], + stages[5], + stages[11], + stages[12], + stages[6], + stages[13], + stages[14] + ] + + +def get_dfs_unbalanced_ordered_stages(stages): + return [ + stages[0], + stages[1], + stages[3], + stages[4], + stages[2], + stages[5], + stages[7], + stages[8], + stages[6], + stages[9], + stages[11], + stages[12], + stages[10], + stages[13], + stages[14] + ] + + +def run_all(dimensions, max_calls, expected_quality, how_many_tries, + how_many_particles, algorithmsToRun): + how_many_stages = 15 + + success_count = {} + for algorithm in algorithmsToRun: + success_count[algorithm.__name__] = 0 + + for algorithm in algorithmsToRun: + for i in range(how_many_tries): + input_vector = create_input_vector_for_cumulated_stages(dimensions) + pso = algorithm.create( + how_many_stages, max_calls, input_vector, expected_quality, + how_many_particles) + LOG.info("Started " + algorithm.__name__) + pso.run() + if pso.process.optimization_status == OptimizationStatus.success: + success_count[algorithm.__name__] += 1 + LOG.info(algorithm.__name__ + " SUCCESS!") + + LOG.info("Iteracja:" + str(i)) + + LOG_RESULTS.info("***SUMMARY INFO: ***") + LOG_RESULTS.info( + "Max calls, expected_quality, dim, how_many_particles, how_many_tries, X^2") + LOG_RESULTS.info( + str([max_calls, expected_quality, dimensions, how_many_particles, + how_many_tries])) + + for algorithm in algorithmsToRun: + LOG_RESULTS.info( + algorithm.__name__ + ": " + str(success_count[algorithm.__name__])) + + +LoggingConfiguration.init() + +LOG_RESULTS = logging.getLogger('results_logger') + + +## Running parameters: +MAX_CALLS = 1000 +EXPECTED_QUALITY = 0.001 +HOW_MANY_PARTICLES = 40 +HOW_MANY_TRIES = 100 +DIMENSIONS = [8] +ALGORITHMS_TO_RUN = [ + # PSO_algorithm_DFS_balanced_SEQ, + PSO_algorithm_DFS_CREDIT_balanced_SEQ, + # PSO_algorithm_DFS_unbalanced_SEQ, + PSO_algorithm_DFS_CREDIT_unbalanced_SEQ +] + +for dimensions in DIMENSIONS: + run_all(dimensions, MAX_CALLS, EXPECTED_QUALITY, HOW_MANY_TRIES, + HOW_MANY_PARTICLES, ALGORITHMS_TO_RUN) diff --git a/grortir/scripts/unbalanced-naming.jpg b/grortir/scripts/unbalanced-naming.jpg new file mode 100644 index 0000000..be9f067 Binary files /dev/null and b/grortir/scripts/unbalanced-naming.jpg differ