def trainabGA(model_name,dataset_name,iter_num): # init T,R,SP T = getSGGRes(model_name,dataset_name,"train",tasktype) R = getGT(model_name,dataset_name,"train",tasktype) SP,N = getPfromR(R,dataset_name) print("T info: ",len(T),len(T[0]),T[0][0]) print("R info: ",len(R),len(R[0]),R[0][0]) device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # start iteration low = [0.0,0.0] high = [1e6,1e6] for i in range(iter_num): #ga = GA(func=fitness, n_dim=2, size_pop=4, max_iter=10, \ # lb=[0.0, 0.0], ub=[1.0, 1.0], precision=1e-7) #ga = GA(func=fitness, n_dim=2, size_pop=6, max_iter=50, \ # lb=low, ub=high, precision=1e-7) #ga = GA(func=fitness, n_dim=2, size_pop=50, max_iter=50, \ # lb=low, ub=high, precision=1e-7) ga = GA(func=fitnessNDCG, n_dim=2, size_pop=100, max_iter=100, \ lb=low, ub=high, precision=1e-7) ga.register(operator_name='selection', operator=selection_tournament, tourn_size=3).\ register(operator_name='ranking', operator=ranking.ranking). \ register(operator_name='crossover', operator=crossover.crossover_2point). \ register(operator_name='mutation', operator=mutation.mutation) ga.to(device=device) #GPU start_time = time.time() best_x, best_y = ga.run()# best_x={alpha,beta},best_y=Q-R print("run time: ",time.time() - start_time) alpha, beta = best_x print('iteration ',i,': best alpha and beta: ', alpha,beta) print('best_y:', best_y) print("sum_ndcg: ",-best_y-0.5*(alpha**2+beta**2)) if best_y==0: print('best {alpha,beta}:',best_x,'best_y',best_y) end_cnt = fitnessNDCG(best_x) print("detect if zeros: ",end_cnt) if end_cnt==0: break else: #R = ReSet(alpha,beta) R = addsmallNDCG(alpha,beta,R) SP,_ = getPfromR(R,dataset_name) cnt_now=fitness(best_x) print(i," iter :", cnt_now) return best_x
def config_ga(): lb = [1] * (station_count + truck_count - 1) ub = [21] * (station_count + truck_count - 1) iteration = int(cfg.read_config("iteration_time")) ga = GA(func=fitness_fun, size_pop=100, n_dim=station_count + truck_count - 1, max_iter=iteration, prob_mut=0.01, lb=lb, ub=ub, precision=[1] * (station_count + truck_count - 1)) ga.Chrom = generate_new_pop() if cfg.read_config("crossover_type") is not None: ga.register(operator_name='crossover', operator=customized_crossover). \ register(operator_name='mutation', operator=customized_mutation) # TODO: 排序的策略配置(暂时不考虑) return ga
def config_ga(): constraint_eq = [ lambda x: int(cfg.read_config('bike_total_count')) - sum(x) ] lb = [0] * station_count ub = hub.hub iteration = int(cfg.read_config("iteration_time")) ga = GA(func=fitness_fun, size_pop=300, n_dim=station_count, max_iter=iteration, prob_mut=0.01, lb=lb, ub=ub, constraint_eq=constraint_eq, precision=[1] * 20) if cfg.read_config("crossover_type") is not None: ga.register(operator_name='crossover', operator=crossover.crossover_1point) # TODO: 变异和排序的策略配置(暂时不考虑) return ga
# %% step2: import package and build ga, as usual. import numpy as np from sko.GA import GA, GA_TSP demo_func = lambda x: x[0]**2 + (x[1] - 0.05)**2 + (x[2] - 0.5)**2 ga = GA(func=demo_func, n_dim=3, size_pop=100, max_iter=500, lb=[-1, -10, -5], ub=[2, 10, 2], precision=[1e-7, 1e-7, 1]) # %% step3: register your own operator ga.register(operator_name='selection', operator=selection_tournament, tourn_size=3) # %% Or import the operators scikit-opt already defined. from sko.operators import ranking, selection, crossover, mutation ga.register(operator_name='ranking', operator=ranking.ranking). \ register(operator_name='crossover', operator=crossover.crossover_2point). \ register(operator_name='mutation', operator=mutation.mutation) # %% Run ga best_x, best_y = ga.run() print('best_x:', best_x, '\n', 'best_y:', best_y) # %% For advanced users class MyGA(GA): def selection(self, tourn_size=3):