def local_search_2(solution): best_cost = calc_cost(solution) best_sol = solution for idx1 in range(num_jobs): for idx2 in range(idx1 + 1, num_jobs): sol = swap(idx1, idx2, solution) sol_cost = calc_cost(sol) if sol_cost < best_cost: best_cost = sol_cost best_sol = sol return best_sol, best_cost
def local_search_2(solution): best_cost = calc_cost(solution) best_sol = solution for idx1 in range(num_jobs): for idx2 in range(idx1+1, num_jobs): sol = swap(idx1, idx2, solution) sol_cost = calc_cost(sol) if sol_cost < best_cost: best_cost = sol_cost best_sol = sol return best_sol, best_cost
def local_search(solution): best_cost = calc_cost(solution) best_sol = solution for job_idx in range(num_jobs): sub_sol = np.delete(solution,job_idx) for idx in range(num_jobs): if job_idx != idx: sol = np.insert(sub_sol, idx, solution[job_idx]) sol_cost = calc_cost(sol) if sol_cost < best_cost: best_cost = sol_cost best_sol = sol return best_sol
def local_search(solution): best_cost = calc_cost(solution) best_sol = solution for job_idx in range(num_jobs): sub_sol = np.delete(solution, job_idx) for idx in range(num_jobs): if job_idx != idx: sol = np.insert(sub_sol, idx, solution[job_idx]) sol_cost = calc_cost(sol) if sol_cost < best_cost: best_cost = sol_cost best_sol = sol return best_sol
def ils(num_iter): initial_sol = best_insertion() solution = local_search(initial_sol) best_cost = calc_cost(solution) for i in range(num_iter): sol = perturbate(solution) sol = local_search(sol) sol_cost = calc_cost(sol) if sol_cost < best_cost: solution = sol best_cost = sol_cost return solution, best_cost
def best_insertion(): solution = np.array([], dtype=int) unallocated = list(range(num_jobs)) for job_idx in range(num_jobs): best_cost = sys.maxsize for job in unallocated: for pos in range(job_idx+1): sol = np.insert(solution, pos, job) cost = calc_cost(sol) if cost < best_cost: best_cost = cost best_insertion = [job,pos] job, pos = best_insertion solution = np.insert(solution, pos, job) unallocated.remove(job) return solution, best_cost
def best_insertion(): solution = np.array([], dtype=int) unallocated = list(range(num_jobs)) for job_idx in range(num_jobs): best_cost = sys.maxsize for job in unallocated: for pos in range(job_idx + 1): sol = np.insert(solution, pos, job) cost = calc_cost(sol) if cost < best_cost: best_cost = cost best_insertion = [job, pos] job, pos = best_insertion solution = np.insert(solution, pos, job) unallocated.remove(job) return solution, best_cost
def genetic(num_iter, num_sampl, mut_prob, local_prob): population = build_initial_pop(num_sampl, num_jobs) costs = calc_costs(population) new_population = np.zeros((num_sampl, num_jobs), dtype=int) new_costs = np.zeros(num_sampl, dtype=int) for i in range(num_iter): for j in range(num_sampl): parent1, parent2 = select_couple(population, costs) son = crossover(parent1, parent2) if random() < mut_prob: son = mutate(son) if random() < local_prob: son = local_search(son) new_costs[j] = calc_cost(son) new_population[j] = son population, costs = update_population(population, costs, new_population, new_costs) best_idx = np.argmin(costs) return population[best_idx], costs[best_idx]
def calc_costs(samples): return [calc_cost(sample) for sample in samples]
def calc_costs(samples): return [ calc_cost(sample) for sample in samples ]
def accept_criteria(sol, best_cost): return min(calc_cost(sol), best_cost)