def setUp(self): """ Sets up some configurations. """ Test.maxDiff = None # For travis-ci output. self.start_time = time() self.chromosome_size = 100 self.default_brkga_params = BrkgaParams() self.default_brkga_params.population_size = 10 self.default_brkga_params.elite_percentage = 0.3 self.default_brkga_params.mutants_percentage = 0.1 self.default_brkga_params.num_elite_parents = 1 self.default_brkga_params.total_parents = 2 self.default_brkga_params.bias_type = BiasFunctionType.LOGINVERSE self.default_brkga_params.num_independent_populations = 3 self.default_brkga_params.pr_number_pairs = 0 self.default_brkga_params.pr_minimum_distance = 0.0 self.default_brkga_params.pr_type = PathRelinkingType.DIRECT self.default_brkga_params.pr_selection = PathRelinkingSelection.BESTSOLUTION self.default_brkga_params.alpha_block_size = 1.0 self.default_brkga_params.pr_percentage = 1.0 self.instance = Instance(self.chromosome_size) self.sum_decoder = SumDecode(self.instance) self.rank_decoder = RankDecode(self.instance) self.default_param_values = { "decoder": self.sum_decoder, "sense": Sense.MAXIMIZE, "seed": 98747382473209, "chromosome_size": self.chromosome_size, "params": self.default_brkga_params, "evolutionary_mechanism_on": True, "chrmosome_type": BaseChromosome }
import dill as pickle import math from brkga_mp_ipr.algorithm import BrkgaMpIpr from brkga_mp_ipr.enums import * from brkga_mp_ipr.types import BaseChromosome, BrkgaParams from brkga_mp_ipr.types_io import load_configuration from tests.instance import Instance from tests.decoders import SumDecode, RankDecode from tests.paths_constants import * ############################################################################### chromosome_size = 100 default_brkga_params = BrkgaParams() default_brkga_params.population_size = 10 default_brkga_params.elite_percentage = 0.3 default_brkga_params.mutants_percentage = 0.1 default_brkga_params.num_elite_parents = 1 default_brkga_params.total_parents = 2 default_brkga_params.bias_type = BiasFunctionType.LOGINVERSE default_brkga_params.num_independent_populations = 3 default_brkga_params.pr_number_pairs = 0 default_brkga_params.pr_minimum_distance = 0.0 default_brkga_params.pr_type = PathRelinkingType.DIRECT default_brkga_params.pr_selection = PathRelinkingSelection.BESTSOLUTION default_brkga_params.alpha_block_size = 1.0 default_brkga_params.pr_percentage = 1.0 instance = Instance(chromosome_size)
def load_configuration(configuration_file: str) -> \ (BrkgaParams, ExternalControlParams): """ Loads the parameters from `configuration_file` returning them as a tuple. Args: configuration_file (str): plain text file containing the configuration. Returns: A tuple containing a `BrkgaParams` and a `ExternalControlParams` object. Raises: IsADirectoryError: If `configuration_file` is a folder. FileNotFoundError: If `configuration_file` does not exist. LoadError: In cases of missing data or bad-formatted data. """ brkga_params = BrkgaParams() control_params = ExternalControlParams() param_names_types = { name: type(value) for name, value in chain( vars(brkga_params).items(), vars(control_params).items()) } param_given = {name: False for name in param_names_types.keys()} with open(configuration_file) as hd: lines = hd.readlines() if not lines: raise LoadError(f"Cannot read {configuration_file}") for (line_number, line) in enumerate(lines): line = line.strip() if len(line) == 0 or line[0] == '#': continue param_name = None value = None try: param_name, value = [x.strip().lower() for x in line.split()] except ValueError: raise LoadError(f"Line {line_number}: " f"missing parameter or value") if param_name in vars(brkga_params): data = brkga_params elif param_name in vars(control_params): data = control_params else: raise LoadError(f"Line {line_number}: " f"parameter '{param_name}' unknown") try: setattr(data, param_name, param_names_types[param_name](value)) param_given[param_name] = True except ValueError: raise LoadError(f"Line {line_number}: " f"invalid value for '{param_name}': {value}") # end for missing_params = [] for name, value in param_given.items(): if not value: missing_params.append(name) if missing_params: missing_params = ", ".join(missing_params) raise LoadError(f"Missing parameters: {missing_params}") return (brkga_params, control_params)