def start_tt_server(): print('Press 1 + <Enter> to run as a OCL GA Server for remote clients.') print('Press 2 + <Enter> to run Taiwan Travel OCL GA L-O-C-A-L-L-Y.') best = None city_info = None statistics = None def callback_from_client(info): nonlocal statistics, best, city_info if 'best' in info: cities = info['best'].dna best = list(range(len(cities))) city_info = [(float(c['x']), float(c['y'])) for c in cities] if 'statistics' in info: statistics = info['statistics'] pass tt_info = get_taiwan_travel_info() while True: user_input = get_input() if user_input == '1': ui = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'ui') start_ocl_ga_server(tt_info, 12345, { 'message' : callback_from_client }, ui) break elif user_input == '2': start_ocl_ga_local(tt_info) break elif user_input == 'exit': break if best and city_info: utils.plot_tsp_result(city_info, best) if statistics: utils.plot_ga_result(statistics)
def run(num_chromosomes, generations): random.seed() type1 = ['q12', 'q23', 'q34', 'q41'] type2 = ['q1', 'q2', 'q3', 'q4'] sample = SimpleChromosome([ SimpleGene('q12', type1, 'unit-1'), SimpleGene('q12', type1, 'unit-2'), SimpleGene('q1', type2, 'unit-3'), SimpleGene('q1', type2, 'unit-4'), SimpleGene('q1', type2, 'unit-5'), SimpleGene('q1', type2, 'unit-6'), SimpleGene('q1', type2, 'unit-7') ]) self_path = os.path.dirname(os.path.abspath(__file__)) f = open(os.path.join(self_path, 'power.cl'), 'r') fstr = ''.join(f.readlines()) f.close() import threading evt = threading.Event() evt.clear() def state_changed(state): if 'stopped' == state: evt.set() ga_cl = OpenCLGA( { 'sample_chromosome': sample, 'termination': { 'type': 'count', 'count': generations }, 'population': num_chromosomes, 'fitness_kernel_str': fstr, 'fitness_func': 'power_station_fitness', 'opt_for_max': 'max', 'debug': True, 'generation_callback': show_generation_info }, action_callbacks={'state': state_changed}) ga_cl.prepare() prob_mutate = 0.05 prob_cross = 0.8 ga_cl.run(prob_mutate, prob_cross) evt.wait() utils.plot_ga_result(ga_cl.get_statistics()) print('run took', ga_cl.elapsed_time, 'seconds') best_chromosome, best_fitness, best_info = ga_cl.get_the_best() print('Best Fitness: %f' % (best_fitness)) print('1 ~ 7 units are maintained at: ' + ', '.join(str(g.dna) for g in best_info.genes))
def run(num_chromosomes, generations): value_ranges = [10, 20, 50, 150, 250, 300, 250, 150, 50, 20, 10] sample = SimpleChromosome( [SimpleGene(0, list(range(v))) for v in value_ranges]) algebra_path = os.path.dirname(os.path.abspath(__file__)) ocl_kernels = os.path.realpath( os.path.join(algebra_path, '..', '..', 'kernel')) algebra_kernels = os.path.join(algebra_path, 'kernel') f = open(os.path.join(algebra_kernels, 'expansion.cl'), 'r') fstr = ''.join(f.readlines()) f.close() import threading evt = threading.Event() evt.clear() def state_changed(state): if 'stopped' == state: evt.set() ga_cl = OpenCLGA( { 'sample_chromosome': sample, 'termination': { 'type': 'count', 'count': generations }, 'population': num_chromosomes, 'fitness_kernel_str': fstr, 'fitness_func': 'expansion_fitness', 'extra_include_path': [ocl_kernels], 'opt_for_max': 'min', 'generation_callback': show_generation_info }, action_callbacks={'state': state_changed}) ga_cl.prepare() prob_mutate = 0.1 prob_cross = 0.8 ga_cl.run(prob_mutate, prob_cross) evt.wait() utils.plot_ga_result(ga_cl.get_statistics()) print('run took', ga_cl.elapsed_time, 'seconds') best_chromosome, best_fitness, best_info = ga_cl.get_the_best() print('Best Fitness: %f' % (best_fitness)) print('Expansion of (x + y)^10 is: ' + ' '.join(str(g) for g in best_chromosome))
def start_ocl_ga_local(info): info['saved_filename'] = info['saved_filename'] % (0, 0) info['generation_callback'] = show_generation_info prob_mutation = info['prob_mutation'] prob_crossover = info['prob_crossover'] ga_target = OpenCLGA(info) ga_target.prepare() try: print('Press run + <Enter> to run') print('Press pause + <Enter> to pause') print('Press restore + <Enter> to restore') print('Press save + <Enter> to save') print('Press stop + <Enter> to stop') print('Press get_st + <Enter> to get statistics information') print('Press get_best + <Enter> to get best information') print('Press ctrl + c to exit') while True: user_input = get_input() if user_input == 'pause': ga_target.pause() elif user_input == 'run': ga_target.run(prob_mutation, prob_crossover) elif user_input == 'stop': ga_target.stop() elif user_input == 'get_st': st = ga_target.get_statistics() utils.plot_ga_result(st) elif user_input == 'get_best': best_chromosome, best_fitness, best_info = ga_target.get_the_best( ) cities, city_info, city_infoX, city_infoY = read_all_cities( 'TW319_368Addresses-no-far-islands.json') utils.plot_tsp_result(city_info, best_chromosome) elif user_input == 'exit': break elif user_input == 'save': ga_target.save() elif user_input == 'restore': ga_target.restore() except KeyboardInterrupt: traceback.print_exc()
def start_tt_server(): print('Press 1 + <Enter> to run as a OCL GA Server for remote clients.') print('Press 2 + <Enter> to run Taiwan Travel OCL GA L-O-C-A-L-L-Y.') best = None city_info = None statistics = None def callback_from_client(info): nonlocal statistics, best, city_info if 'best' in info: cities, city_info, city_infoX, city_infoY = read_all_cities( 'TW319_368Addresses-no-far-islands.json') best = info['best'] if 'statistics' in info: statistics = info['statistics'] pass tt_info = get_taiwan_travel_info() while True: user_input = get_input() if user_input == '1': ui = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'ui') start_ocl_ga_server(tt_info, 12345, {'message': callback_from_client}, ui) break elif user_input == '2': start_ocl_ga_local(tt_info) break elif user_input == 'exit': break if best and city_info: utils.plot_tsp_result(city_info, best) if statistics: utils.plot_ga_result(statistics)
def run(num_chromosomes, generations): num_cities = 20 random.seed() city_ids = list(range(0, num_cities)) city_info = { city_id: (random.random() * 100, random.random() * 100) for city_id in city_ids } sample = ShufflerChromosome([SimpleGene(v, city_ids) for v in city_ids]) tsp_path = os.path.dirname(os.path.abspath(__file__)) tsp_kernels = os.path.join(tsp_path, 'kernel') f = open(os.path.join(tsp_kernels, 'simple_tsp.cl'), 'r') fstr = ''.join(f.readlines()) f.close() pointX = [str(city_info[v][0]) for v in city_info] pointY = [str(city_info[v][1]) for v in city_info] import threading evt = threading.Event() evt.clear() def state_changed(state): if 'stopped' == state: evt.set() tsp_ga_cl = OpenCLGA( { 'sample_chromosome': sample, 'termination': { 'type': 'count', 'count': generations }, 'population': num_chromosomes, 'fitness_kernel_str': fstr, 'fitness_func': 'simple_tsp_fitness', 'fitness_args': [{ 't': 'float', 'v': pointX, 'n': 'x' }, { 't': 'float', 'v': pointY, 'n': 'y' }], 'opt_for_max': 'min', 'debug': True, 'generation_callback': show_generation_info }, action_callbacks={'state': state_changed}) tsp_ga_cl.prepare() prob_mutate = 0.1 prob_cross = 0.8 tsp_ga_cl.run(prob_mutate, prob_cross) evt.wait() utils.plot_ga_result(tsp_ga_cl.get_statistics()) print('run took', tsp_ga_cl.elapsed_time, 'seconds') best_chromosome, best_fitness, best_info = tsp_ga_cl.get_the_best() print('Best Fitness: %f' % (best_fitness)) print('Shortest Path: ' + ' => '.join(str(g) for g in best_chromosome)) utils.plot_tsp_result(city_info, best_chromosome)