Beispiel #1
0
    def __init__(self,
                 filename,
                 population_size=100,
                 mutation_probability=0.1,
                 pool_probability=0.2,
                 timer_limit=20000):
        # Matrices
        self.weights = get_weights_matrix(filename, static=False)
        #self.weights = self.weights[:50, :50, :50]

        # constants
        self.N_CITIES = np.shape(self.weights)[1]
        self.TIMER_LIMIT = timer_limit
        self.MUTATION_PROBABILITY = mutation_probability
        self.POOL_PROBABILITY = pool_probability
        self.POP_SIZE = population_size
        self.SENSIBILITY = 0.1

        # population
        self.population = init_population(self.POP_SIZE, self.N_CITIES)
        self.population_cost = None  # it is a numpy array.
        self.compute_population_cost()

        # gene_pool
        self.K = 6  # cardinality of the gene pool
        self.gene_pool = []
        self.init_gene_pool()

        # solutions:
        self.best_cost = float('Inf')
        self.best_solution = None
    def __init__(self,
                 filename,
                 card_max=None,
                 is_static=True,
                 is_fengyun=False):
        if is_fengyun:
            self.weights, self.N_CITIES = get_fengyun_weights(filename)
        else:
            self.weights, self.N_CITIES = get_weights_matrix(
                filename, card_max=card_max, is_static=is_static)

        self.is_static = is_static
        self.best_solution = None
        self.best_cost = float('Inf')
    def __init__(self,
                 filename,
                 n_ants=10,
                 alpha=0.1,
                 beta=6,
                 rho=0.1,
                 q0=0.98,
                 max_iteration=100):
        # Matrices
        self.weights = get_weights_matrix(filename, static=True)
        #self.weights = self.weights[:20, :20]

        # Constants
        self.BETA = beta
        self.N_CITIES = np.shape(self.weights)[1]
        self.N_ANTS = n_ants
        self.MAX_ITERATION = max_iteration
        self.TAO_0 = 0
        self.RHO = rho
        self.ALPHA = alpha
        self.Q0 = q0

        # Solutions
        self.solutions = []
        self.new_solutions = None

        # global solution
        self.global_best_solution = None
        self.global_best_cost = float('Inf')

        # indexes
        self.iteration = 0
        # self.start_idx = 0

        # pheromone
        self.pheromone_matrix = None
        self.init_pheromone_matrix()
Beispiel #4
0
    def __init__(self,
                 filename,
                 weights=None,
                 population=None,
                 is_static=True,
                 population_size=100,
                 mutation_probability=0.05,
                 timer_limit=20000):
        self.is_static = is_static
        # Matrices
        if weights is None:
            self.weights = get_weights_matrix(filename, static=self.is_static)
        else:
            self.weights = weights
            #self.weights = self.weights[:25, :25]

        # population
        if population is None:
            self.population = None
            self.POP_SIZE = population_size
        else:
            self.population = population
            self.POP_SIZE = len(self.population)
        self.population_cost = None
        self.compute_population_cost()

        # constants
        self.N_CITIES = np.shape(self.weights)[1]
        self.TIMER_LIMIT = timer_limit
        self.MUTATION_PROBABILITY = mutation_probability  # mutation probability

        # solutions:
        self.best_cost = float('Inf')
        self.best_solution = None
        self.nn_solution = []
        self.nn_cost = float('Inf')
Beispiel #5
0
    def __init__(self,
                 filename,
                 card_max=None,
                 n_ants=5,
                 alpha=1,
                 beta=6,
                 rho=0.1,
                 q0=0.9,
                 max_generations=1000,
                 type="AS",
                 is_fengyun=False):
        """Constructor of the class Dynamic Ant.
        Parameters:
            filename : string that contains the path of the weights file.
            card_max : (optional - default None) integer that represent the maximum number of cities to use.
            n_ants : (optional - default 5) integer. The number of the ants. .
            alpha : (optional - default 1) pheromone trail influence. The greater the more important is the pheromone
                    trail.
            beta : (optional - dafault 6) heuristic information influence. The greater the more important is the
                   heuristic.
            rho : (optional - default 0.1) pheromone evaporation rate.
            q0 : (optional - default 0.9) probability threshold of choosing the next city with the probabilistic rule.
            max_generations : (optional - default 1000) maximum number of the generations.
            type : (optional - default AS) Specify which type of approach keeping during pheromone deposit.
            is_fengyun : (optional - default False) If set to true change the ACO behaviour in order to manage its huge dataset.
        """
        # Weights Matrix
        if is_fengyun:
            self.weights, self.N_CITIES = get_fengyun_weights(filename)
        else:
            self.weights, self.N_CITIES = get_weights_matrix(filename,
                                                             card_max=card_max,
                                                             is_static=False)

        # Constants
        self.BETA = beta
        self.N_ANTS = n_ants
        self.MAX_GENERATIONS = max_generations
        self.RHO = rho  # pheromone evaporation speed.
        self.ALPHA = alpha
        self.Q0 = q0  # to be global constant
        self.TYPE = type
        self.is_fengyun = is_fengyun

        # Precompute the heuristic information based on the weights.
        if not self.is_fengyun:
            self.heuristic_info = np.power(
                np.divide(1, self.weights, where=self.weights != 0), self.BETA)

        # Time iterator for the dynamic application.
        self.t = 0  # current time
        self.t0 = 0  # initial time
        self.T = np.shape(self.weights)[0] - 1  # final time (index)

        # Solutions
        self.solutions = []
        self.new_solutions = None  # new solutions.
        self.new_costs = None  # costs of the new solutions.
        self.nn_solution = []
        self.nn_cost = float('Inf')

        # global solution
        self.global_best_solution = None
        self.global_best_cost = float('Inf')

        # indexes
        self.iteration = 0

        # pheromone
        self.TAO_0 = None  # minimum pheromone level.
        self.pheromone_matrix = None
        self.init_pheromone_matrix()

        # list of length k of sets of available cities
        self.available = None

        # aio local search
        self.IO_MAX_ITER = 10
        self.p_bi = 0.5
        self.p_gi = 0.5

        # statistic variables
        self.best_performance = np.zeros(self.MAX_GENERATIONS)
        self.avg_performance = np.zeros(self.MAX_GENERATIONS)
        self.worst_performance = np.zeros(self.MAX_GENERATIONS)
        self.change_global_min = np.zeros(self.MAX_GENERATIONS)
        self.global_p_bi = np.zeros(self.MAX_GENERATIONS)
        self.global_p_gi = np.zeros(self.MAX_GENERATIONS)
        self.global_n_bi_inversions = np.zeros(self.MAX_GENERATIONS)
        self.global_n_gi_inversions = np.zeros(self.MAX_GENERATIONS)
        self.relative_diversity = np.zeros(self.MAX_GENERATIONS)
        self.avg_time_per_solution = np.zeros(self.MAX_GENERATIONS)
        self.ls_number_of_improvements = np.zeros(
            (self.N_ANTS, self.MAX_GENERATIONS))
        self.ls_rate_of_improvements = np.zeros(
            (self.N_ANTS, self.MAX_GENERATIONS))
Beispiel #6
0
    def __init__(self,
                 filename,
                 card_max=None,
                 n_ants=5,
                 alpha=1,
                 beta=6,
                 q0=0.9,
                 pop_size=100,
                 max_generation=1000):
        """Constructor of the class Dynamic Ant for the Population Based Ant Colony Optimization.
        Parameters:
            filename : string that contains the path of the weights file.
            card_max : (optional - default None) integer that represent the maximum number of cities to use.
            n_ants : (optional - default 5) integer. The number of the ants. .
            alpha : (optional - default 1) pheromone trail influence. The greater the more important is the pheromone
                    trail.
            beta : (optional - dafault 1) heuristic information influence. The greater the more important is the
                   heuristic.
            q0 : (optional - default 0.1) probability threshold of choosing the next city with the probabilistic rule.
            max_generation : (optional - default 1000) maximum number of the generations.
        """
        # Weights Matrix
        self.weights = get_weights_matrix(filename)
        if card_max is not None and card_max < np.shape(
                self.weights)[1] and card_max > 0:
            self.N_CITIES = card_max
            self.weights = self.weights[:card_max, :card_max, :card_max]
        else:
            self.N_CITIES = np.shape(self.weights)[1]

        # Constants
        self.BETA = beta
        self.N_ANTS = n_ants
        self.MAX_GENERATION = max_generation
        self.TAO_0 = 0  # minimum pheromone level. initialized later.
        self.ALPHA = alpha
        self.Q0 = q0  # to be global constant
        self.MAX_POP = pop_size

        # Precompute the heuristic information based on the weights.
        self.heuristic_info = np.power(
            np.divide(1, self.weights, where=self.weights != 0), self.BETA)

        # Time iterator for the dynamic application.
        self.t = 0  # current time
        self.t0 = 0  # initial time
        self.T = np.shape(self.weights)[0] - 1  # final time (index)

        # Solutions
        self.solutions = []
        self.new_solutions = None
        self.nn_solution = []
        self.nn_cost = float('Inf')

        # global solution
        self.global_best_solution = None
        self.global_best_cost = float('Inf')

        # indexes
        self.iteration = 0

        # pheromone
        self.TAO_0 = None  # minimum pheromone level.
        self.TAO_MAX = None  # maximum pheromone level.
        self.TAO_DELTA = None  # delta step for the pheromone.
        self.pheromone_matrix = None
        self.init_pheromone_matrix()

        # list of length k of sets of available cities
        self.available = None

        # population: managed with the age mechanism.
        self.population = []
        self.pop_iter = 0
        self.pop_iter_age = 0

        self.overflow = 0