def generate_population(self, Function, dimension=2): super().generate_population() population = [] neighbor = Solution(lower_bound=Function.left, upper_bound=Function.right) neighbor.fill_vector_with_gaussian(self.best_solution, self.sigma) population.append(neighbor) return population
def generate_random_solution(self, lower_bound, upper_bound, dimension=2): """Generates random solution according to bounds Args: lower_bound (float) upper_bound (float) dimension (int, optional) Defaults to 2. Returns: Solution: generated Solution with inicialized random vector """ random_solution = Solution(dimension, lower_bound, upper_bound) random_solution.fill_vector_with_random() return random_solution
def crossover(self, iteration_individual, mutation_vector): trial_solution = Solution(self.D) # trial_vector dimension = len(trial_solution.vector) random_position_j = np.random.randint(0, dimension) for j in range(dimension): if np.random.uniform( ) < self.crossover_range or j == random_position_j: trial_solution.vector[j] = mutation_vector[j] else: trial_solution.vector[j] = iteration_individual.vector[j] return trial_solution
def crossover(self, parent_A, parent_B): length = len(parent_A.vector) random_position_in_range = random.randint(0, length - 1) part_A = parent_A.vector[0:random_position_in_range, :] rest = [] for city in parent_B.vector: is_there = np.any(part_A == city) if not is_there: rest.append(list(city)) crossover_vector = np.concatenate((part_A, np.array(rest)), axis=0) offspring_AB = Solution() offspring_AB.vector = crossover_vector return offspring_AB
def solving(config): # read problems from file file_name = config["solving"]["file_name"] problems_file_path = PathResolver.get_problem_file_absolute_path(file_name) problems = ProblemsReader.read(problems_file_path, config["boundary"]) # solve problem for index in config["solving"]["problems"]: problem = problems[index - 1] start_time = time.time() solution = ProblemSolver.solve(problem) # Heuristic solving if config["solving"]["use_heuristic"]: processing_time = config["heuristic"]["processing_time"] max_tabu_list_size = config["heuristic"]["max_tabu_list_size"] heuristic_problem_solver = HeuristicProblemSolver( start_time, processing_time, max_tabu_list_size) solution = heuristic_problem_solver.solve(solution.problem) print("Processing time[{}]: {}s".format( index, round(time.time() - start_time, 5))) # save solution solution_file_name = Solution.get_file_name_for_solution( problem, config) solution_file_path = PathResolver.get_solution_file_absolute_path( solution_file_name) SolutionWriter.write(solution_file_path, solution)
def solve(self, problem: Problem): self.best = copy.deepcopy(problem) self.best_candidate = problem while time.time( ) - self.start_time < self.processing_time and self.empty_iterations < 100: self.__iteration() self.iterations = self.iterations + 1 return Solution(self.best, CostCounter.count(self.best))
def solve_with_permutations(problem: Problem): tasks_permutations = list(itertools.permutations(range(len(problem.tasks)))) tasks_costs = [] for permutation in tasks_permutations: problem = ProblemSolver.order_tasks(problem, permutation) tasks_costs.append(CostCounter.count(problem)) min_cost_index = tasks_costs.index(min(tasks_costs)) optimal_problem = ProblemSolver.order_tasks(problem, tasks_permutations[min_cost_index]) optimal_cost = tasks_costs[min_cost_index] return Solution(optimal_problem, optimal_cost)
def generate_population(self, Function, dimension=2): """Generates population for HillClimbAlgorithm with np.random.normal Args: Function (class Function): specific Function (Sphere || Ackley..) dimension (int, optional): Defaults to 2. Returns: [Solution[]]: generated population as specific generation """ super().generate_population() population = [] for _ in range(self.size_of_population): neighbor = Solution(lower_bound=Function.left, upper_bound=Function.right) neighbor.fill_vector_with_gaussian(self.best_solution, self.sigma) population.append(neighbor) return population
def generate_individual(self, cities): """Generating single individual according to input Shuffle cities If fixed_first_index then always starts from one point and this will be not moved when alg moves forward Returns: []: [description] """ individual = Solution() if self.fixed_first_index: first = list(self.cities)[0] without_first = list(self.cities)[1:] shuffled = random.sample(without_first, len(without_first)) result = [first] + shuffled individual.vector = np.array(result) else: individual.vector = np.array( random.sample(list(self.cities), len(self.cities))) return individual
def __init__(self, graph=None, size_of_population=1000, max_generation=20, D=2): self.size_of_population = size_of_population self.max_generation = max_generation self.graph = graph self.index_of_generation = 0 self.best_solution = Solution() self.D = D self.max_OFE = None self.current_OFE = 0
def read(file_path: str): with open(file_path, 'r') as file: data = file.readline().split(" ") data = list(map(lambda element: int(element), data)) problem = SolutionReader.get_problem(file_path) cost = data[0] starting_point = data[1] tasks_order = data[2::] problem.starting_point = starting_point problem = SolutionReader.order_tasks(problem, tasks_order) return Solution(problem, cost)
def select_best_solution(self, population): """Runs over whole population and returns one with best fitness Args: population (Solution[]): whole population len(population)=self.size_of_population Returns: Solution: Solution with best fitness value """ best_in_population = Solution() for solution in population: if solution.fitness_value < best_in_population.fitness_value: best_in_population = solution return best_in_population
def calculate_new_position(self, solution): """Calculates new positon of particle According to textbooks when value is out of bounderies then is generated new random position Args: solution ([type]): [description] """ new_position = solution.vector + solution.velocity_vector # solution.vector = np.clip(new_position, solution.lower_bound, solution.upper_bound) #!old one # #! new one for index, param in enumerate(new_position): if not Solution.is_in_bounderies(param, solution.lower_bound, solution.upper_bound): new_position[index] = np.random.uniform( low=solution.lower_bound, high=solution.upper_bound) solution.vector = new_position
def solve_smart(problem: Problem): tasks = problem.tasks # Sort decreasing considering (L-E)/T - cost per time unit tasks = sorted( tasks, key=lambda item: (item.too_late_penalty - item.too_early_penalty) / item.processing_time, reverse=True ) # Split into two parts L and R considering CDD as split point time_sum = 0 left_part = [] right_part = [] for task in tasks: time_sum += task.processing_time if time_sum < problem.common_due_date: left_part.append(task) else: right_part.append(task) # Sort left side by E/T - increasing cost per time unit for too early delay left_part = sorted(left_part, key=lambda item: item.too_early_penalty / item.processing_time) # Sort right side by L/T - decreasing cost per time unit for too late delay right_part = sorted(right_part, key=lambda item: item.too_late_penalty / item.processing_time, reverse=True) # Connect L and R problem.tasks = left_part + right_part # Move starting point from 0 to CDD searching for minimum cost costs = [] for index in range(problem.common_due_date): problem.starting_point = index costs.append(CostCounter.count(problem)) min_cost_index = costs.index(min(costs)) problem.starting_point = min_cost_index return Solution(problem, costs[min_cost_index])
def solve_random(problem: Problem): shuffle(problem.tasks) problem.starting_point = random.randrange(0, 10) cost = CostCounter.count(problem) return Solution(problem, cost)
def solve_simple(problem: Problem): cost = CostCounter.count(problem) return Solution(problem, cost)