# 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)
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() ax = fig.add_subplot(211) ax.plot(dis_lst) ax.set_xlabel('num') ax.set_ylabel('distance')
# -*- coding: utf-8 -*- """ experiment on different state transition mode. 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) modes = ['cross', 'reverse'] n = 10 with open('./experiment/2.5_mode.txt', 'w') as f: for mode in modes: t = 0 dis = 0 for i in range(n): sa.reset() log = sa.train(mode=mode) t += log['time'] dis += log['dis'] s = 'mode={}\ttime={:.3f}\tdistance={}'.format(mode, t / n, dis / n) print(s, file=f, flush=True) print(s)
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( mode, t / n, dis / n) print(s, file=f, flush=True) print(s)