def run_experiment(experiment_name, network, alpha, edit_type, n, mcls, temps, cooling_schedules, reps=10): d_ns = [] d_costs = [] d_temps = [] d_alphas = [] d_ls = [] d_cs = [] d_init_temps = [] print("Run " + experiment_name + " for " + network.name + " with " + str(reps) + " reps.") for start_temp in temps: for cooling_schedule in cooling_schedules: for mcl in mcls: for _ in range(reps): ns, costs, temps, _ = SA(network).run( start_temp, alpha, edit_type, n, mcl, cooling_schedule=cooling_schedule) d_ns += ns d_costs += costs d_temps += temps d_alphas += [alpha] * len(ns) d_ls += [mcl] * len(ns) d_cs += [cooling_schedule] * len(ns) d_init_temps += [start_temp] * len(ns) print("L: " + str(mcl) + "\tT0: " + str(start_temp) + "\tcooling_schedule: " + cooling_schedule + "\tinitial cost: " + str(costs[0]) + "\tfinal cost: " + str(costs[-1])) dt = { "n": d_ns, "cost": d_costs, "temp": d_temps, "alpha": d_alphas, "l": d_ls, "cooling_schedule": d_cs, "init_temp": d_init_temps } df = pd.DataFrame().from_dict(dt) save_pickle(df, "data/" + network.name + "_" + experiment_name)
def eggCarton(M, N, K): board = [[False] * M] * N # build and empty N*M carton for k in range(K): board[0][k] = True # start at fill K eggs in first row state = Node(board, K) # build first state Tmax, dT = 99, 1 # Tmax max temperature; dT: reduce temperature size Ftarget = math.min(M, N) while (True): solution = SA(state, Tmax, Ftarget, dT) Ftarget = math.max(Ftarget, solution)
def type_to_class(type_name): """ Converts automaton type name to automata object :param type_name: automaton type name :return: automaton object """ return { "INFA": SA(), "LFA": LFA(), "INT": ST(), "GBA": BA(), }[type_name]
# import tensorflow as tf import numpy as np from sa import SA from common import * model = SA(shape=[48, 48, 1], no_classes=7) model.build_model() data = np.zeros((7, 100, 48, 48, 1)) val = np.ones((20, 48, 48, 1)) labels_val = one_hot(np.ones((20)), 7) model.train(data, val, labels_val, period_save_model=5)
# -*- coding: utf-8 -*- """ experiment on different anneal rate(gamma). For more detail, please reference to Readme.md """ import numpy as np from sa import SA data = np.loadtxt('location.txt', delimiter=',', usecols=[1, 2]) seed = None sa = SA(data, T=200, anneal_rate=0.95, final_T=0.001, inner_iters=120, random_init=True) gammas = [0.1, 0.3, 0.5, 0.8, 0.9, 0.95, 0.98, 0.99] n = 5 with open('./experiment/2.3_gamma.txt', 'w') as f: for gamma in gammas: t = 0 dis = 0 for i in range(n): sa.reset() sa.gamma = gamma log = sa.train(mode='reverse') t += log['time'] dis += log['dis'] s = 'gamma={}\ttime={:.3f}\tdistance={}'.format(gamma, t / n, dis / n) print(s, file=f, flush=True) print(s)
result = {"code": 200, "score": str(score)} except Exception as e: print(e) result = {"code": 500, "message": "后端发生异常,请检查"} return jsonify({"result": result}) @app.route('/') def hhhh(): print('23232') return render_template('index.html') class ARGS(): def __init__(self) -> None: self.no_cuda = False self.local_rank = -1 self.max_seq_length = 256 self.model_type = "bert" # self.model_name_or_path="/home/mhxia/whou/workspace/pretrained_models/roberta-large #roberta-large/", self.output_dir = "./output/checkpoint-best/" self.seed = 1 if __name__ == '__main__': args = ARGS() # print(args) sa = SA(args) server = pywsgi.WSGIServer(('0.0.0.0', 10001), app) server.serve_forever()
default='reverse', help= 'the method of get new solution. It must be one of [reverse,cross](default:reverse)' ) parser.add_argument('--anneal_mode', type=int, default=0, help='anneal mode. It must be one of [0,1,2](default:0)') ars = parser.parse_args() np.random.seed(ars.seed) # set random seed for reproducing experiment results data = np.loadtxt(ars.file_path, delimiter=',', usecols=[1, 2]) # load data # init algorithm sa = SA(data, T=ars.init_T, anneal_rate=ars.anneal_rate, final_T=ars.final_T, inner_iters=ars.inner_iters, random_init=ars.random_init) # run algorithm log = sa.train(mode=ars.mode, anneal_mode=ars.anneal_mode) # log information t = log['time'] dis = log['dis'] path = log['path'] dis_lst = deepcopy(sa.dis_lst) print('time:{:.3f}\tbest distance:{}'.format(t, dis)) print('Now you can use test.exe to verify solution.') # save best result. So you can excute test.exe to verify the solution np.savetxt('city.txt', path + 1, fmt='%d', newline=',') # plot distance over time fig = plt.figure()
def multiroute2(point_file, line_file, cluster_num): f = open(point_file) # read point file content = f.read() lines = content.splitlines() point_remain = [] # ID start_id = -1 max_num = 0 point_id = 0 position = {} weight = {} for line in lines: line = line.split(" ") point_remain.append(int(line[0])) position[point_id] = [float(line[1]), float(line[2])] if int(line[3]) != 0: # get the start point ID start_id = point_id max_num = int(line[3]) if int(line[4]) != 0: weight[point_id] = int(line[4]) point_id += 1 f.close() f = open(line_file) # read line file content = f.read() lines = content.splitlines() line_cost = [] # cost of each line # get_the_route = {} # display the route for line in lines: line = line.split(" ") line_cost.append(float(line[2])) # key1 = (int(line[0]), int(line[1])) # small-big # key2 = (int(line[1]), int(line[0])) # big-small # temp = line[3].split("->") # value1 = [] # value2 = [] # for i in range(len(temp)): # value1.append(int(temp[i])) # value2.append(int(temp[len(temp) - 1 - i])) # # get_the_route[key1] = value1 # get_the_route[key2] = value2 f.close() matrix = [[float(0.0) for i in range(len(point_remain))] for j in range(len(point_remain))] # initialize map num = 0 for i in range(len(matrix)): for j in range(i + 1, len(matrix)): matrix[i][j] = line_cost[num] matrix[j][i] = line_cost[num] num += 1 multiclustering = MULTICLUSTERING(position, weight, start_id, matrix) ID = multiclustering.partition(max_num, cluster_num) # partition 2 print(ID) for i in range(len(ID)): ID[i].append(start_id) sa = [0 for i in range(len(ID))] for i in range(len(ID)): sa[i] = SA(ID[i], matrix) get_value = [[] for i in range(len(ID))] for i in range(len(ID)): for j in range(2): get_value[i].append( sa[i].min_path_2(start_id)) # save the iteration result dis = [float("inf") for i in range(len(ID))] path = [[] for i in range(len(ID))] for i in range(len(ID)): for j in range(len(get_value[i])): if dis[i] > get_value[i][j][0]: dis[i] = get_value[i][j][0] path[i] = [] for k in range(len(ID[i])): path[i].append(point_remain[get_value[i][j][1] [k]]) # get the shortest print(path) result = [[] for i in range(len(ID))] for i in range(len(ID)): result[i] = path[i] for i in range(len(ID)): result[i].append(point_remain[start_id]) result_route = ["" for i in range(len(ID))] for i in range(len(ID)): for j in range(len(result[i])): # display the route result_route[i] += str(result[i][j]) if j != len(result[i]) - 1: result_route[i] += "->" # save like id->id print(result_route) print(dis) return result
def multimission(point_file, line_file): f = open(point_file) # read point file content = f.read() lines = content.splitlines() point_remain = [] # ID start_id = -1 max_num = 0 point_id = 0 position = {} weight = {} for line in lines: line = line.split(" ") point_remain.append(int(line[0])) position[point_id] = [float(line[1]), float(line[2])] if int(line[3]) != 0: # get the start point ID start_id = point_id max_num = int(line[3]) if int(line[4]) != 0: weight[point_id] = int(line[4]) point_id += 1 f.close() f = open(line_file) # read line file content = f.read() lines = content.splitlines() line_cost = [] # cost of each line get_the_route = {} # display the route for line in lines: line = line.split(" ") line_cost.append(float(line[2])) key1 = (int(line[0]), int(line[1])) # small-big key2 = (int(line[1]), int(line[0])) # big-small temp = line[3].split("->") value1 = [] value2 = [] for i in range(len(temp)): value1.append(int(temp[i])) value2.append(int(temp[len(temp) - 1 - i])) get_the_route[key1] = value1 get_the_route[key2] = value2 f.close() matrix = [[float(0.0) for i in range(len(point_remain))] for j in range(len(point_remain))] # initialize map num = 0 for i in range(len(matrix)): for j in range(i + 1, len(matrix)): matrix[i][j] = line_cost[num] matrix[j][i] = line_cost[num] num += 1 clustering = CLUSTERING(position, weight, start_id, matrix) ID1, ID2 = clustering.partition(max_num) # partition 2 ID1.append(start_id) ID2.append(start_id) sa1 = SA(ID1, matrix) get_value1 = [] for i in range(20): # 20 iteration get_value1.append(sa1.min_path_2(start_id)) dis1 = float("inf") path1 = list(range(len(ID1))) for i in range(len(get_value1)): if dis1 > get_value1[i][0]: dis1 = get_value1[i][0] # get the smallest for j in range(len(ID1)): path1[j] = point_remain[get_value1[i][1][ j]] # path contains the nodes in the new graph result1 = [] # result contains the nodes in the raw graph for i in range(len(path1) - 1): key = (path1[i], path1[i + 1]) for j in range(len(get_the_route[key]) - 1): result1.append(get_the_route[key][j]) # a-b for i in range(len(get_the_route[(path1[len(path1) - 1], path1[0])])): # b-a result1.append(get_the_route[(path1[len(path1) - 1], path1[0])][i]) result_route1 = "" for i in range(len(result1)): # display the route result_route1 += str(result1[i]) if i != len(result1) - 1: result_route1 += "->" print(result_route1) print(dis1) sa2 = SA(ID2, matrix) get_value2 = [] for i in range(20): # 20 iteration get_value2.append(sa2.min_path_2(start_id)) dis2 = float("inf") path2 = list(range(len(ID2))) for i in range(len(get_value2)): if dis2 > get_value2[i][0]: dis2 = get_value2[i][0] # get the smallest for j in range(len(ID2)): path2[j] = point_remain[get_value2[i][1][ j]] # path contains the nodes in the new graph result2 = [] # result contains the nodes in the raw graph for i in range(len(path2) - 1): key = (path2[i], path2[i + 1]) for j in range(len(get_the_route[key]) - 1): result2.append(get_the_route[key][j]) # a-b for i in range(len(get_the_route[(path2[len(path2) - 1], path2[0])])): # b-a result2.append(get_the_route[(path2[len(path2) - 1], path2[0])][i]) result_route2 = "" for i in range(len(result2)): # display the route result_route2 += str(result2[i]) if i != len(result2) - 1: result_route2 += "->" print(result_route2) print(dis2) return result_route1, result_route2
def sa(point_file, line_file): f = open(point_file) # read point file content = f.read() lines = content.splitlines() point_remain = [] # ID start_id = -1 point_id = 0 for line in lines: line = line.split(" ") point_remain.append(int(line[0])) if int(line[3]) != 0: # get the start point ID start_id = point_id point_id += 1 f.close() f = open(line_file) # read line file content = f.read() lines = content.splitlines() line_cost = [] # cost of each line get_the_route = {} # display the route for line in lines: line = line.split(" ") line_cost.append(float(line[2])) key1 = (int(line[0]), int(line[1])) # small-big key2 = (int(line[1]), int(line[0])) # big-small temp = line[3].split("->") value1 = [] value2 = [] for i in range(len(temp)): value1.append(int(temp[i])) value2.append(int(temp[len(temp) - 1 - i])) get_the_route[key1] = value1 get_the_route[key2] = value2 f.close() matrix = [[float(0.0) for i in range(len(point_remain))] for j in range(len(point_remain))] # initialize map num = 0 for i in range(len(matrix)): for j in range(i + 1, len(matrix)): matrix[i][j] = line_cost[num] matrix[j][i] = line_cost[num] num += 1 sa = SA(point_remain, matrix) get_value = [] for i in range(20): # 20 iteration get_value.append(sa.min_path(start_id)) dis = float("inf") path = list(range(len(point_remain))) for i in range(len(get_value)): if dis > get_value[i][0]: dis = get_value[i][0] # get the smallest for j in range(len(point_remain)): path[j] = point_remain[get_value[i][1][ j]] # path contains the nodes in the new graph result = [] # result contains the nodes in the raw graph for i in range(len(path) - 1): key = (path[i], path[i + 1]) for j in range(len(get_the_route[key]) - 1): result.append(get_the_route[key][j]) # a-b for i in range(len(get_the_route[(path[len(path) - 1], path[0])])): # b-a result.append(get_the_route[(path[len(path) - 1], path[0])][i]) result_route = "" for i in range(len(result)): # display the route result_route += str(result[i]) if i != len(result) - 1: result_route += "->" print(result_route) print(dis) return result_route
# from settings import Settings from ga_settings import GA_settings from ga import GA from ts_settings import TS_settings from ts import TS from sa_settings import SA_settings from sa import SA ga_settings = GA_settings() ga = GA(ga_settings) ga.main() ts_settings = TS_settings() ts = TS(ts_settings) ts.main() sa_settings = SA_settings() sa = SA(sa_settings) sa.main()
# -*- coding: utf-8 -*- """ experiment on different anneal mode. For more detail, please reference to Readme.md """ import numpy as np from sa import SA from copy import deepcopy data = np.loadtxt('location.txt', delimiter=',', usecols=[1, 2]) seed = None sa = SA(data, T=200, anneal_rate=0.95, final_T=0.001, inner_iters=120, random_init=True) modes = [0, 1, 2] n = 5 dis_lst = None with open('./experiment/2.2_annealMode.txt', 'w') as f: for mode in modes: t = 0 dis = 0 for i in range(n): sa.reset() log = sa.train(mode='reverse', anneal_mode=mode) t += log['time'] dis += log['dis'] dis_lst = deepcopy(sa.dis_lst) sa.plot_dis(title=f'Mode {mode}') s = 'anneal mode={}\ttime={:.3f}\tdistance={}'.format(