def test_testCase16(): simulator = Simulator() initial_game = simulator.initialize_game( Position(3989, 3259), 3, [{"id": 0, "position_x": 302, "position_y": 6109}, {"id": 1, "position_x": 3671, "position_y": 981}, {"id": 2, "position_x": 6863, "position_y": 809}] , 11, [{"id": 0, "position_x": 208, "position_y": 156}, {"id": 1, "position_x": 10129, "position_y": 711}, {"id": 2, "position_x": 13229, "position_y": 413}, {"id": 3, "position_x": 203, "position_y": 3627}, {"id": 4, "position_x": 7310, "position_y": 3912}, {"id": 5, "position_x": 9814, "position_y": 3223}, {"id": 6, "position_x": 13556, "position_y": 3668}, {"id": 7, "position_x": 3923, "position_y": 6251}, {"id": 8, "position_x": 6720, "position_y": 6574}, {"id": 9, "position_x": 10387, "position_y": 6136}, {"id": 10, "position_x": 13093, "position_y": 6253}] ) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score > 0
def test_testCase1(): simulator = Simulator() initial_game = simulator.initialize_game(Position(0, 0), 1, [{"id": 0, "position_x": 8250, "position_y": 4500}], 1, [{"id": 0, "position_x": 8250, "position_y": 8999}]) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score == 10
def test_initialize_game(): simulator = Simulator() inital_game = simulator.initialize_game( Position(0, 0), 3, [{"id": 0, "position_x": 500, "position_y": 600}, {"id": 1, "position_x": 700, "position_y": 300}, {"id": 2, "position_x": 100, "position_y": 900}], 1, [{"id": 0, "position_x": 2000, "position_y": 1000}] ) assert len(inital_game.humans) == 3 assert len(inital_game.zombies) == 1 inital_game = simulator.initialize_game( Position(0, 0), 4, [], 2, [] ) assert len(inital_game.humans) == 4 assert len(inital_game.zombies) == 2
def test_run(): np.random.seed(123) torch.manual_seed(2) cfg_file = open('cfg.log', "w", 1) acc_file = open('acc.log', "w", 1) cr_file = open('cr.log', "w", 1) # setup target network # so that we only do this once sim = Simulator() cgen = C_Generator(explore=True) num_cfg = 100 # number of cfgs to be explored selected_ranges = [10*i for i in range(1,10)]+[100*i for i in range(1,8)]+[782] print('Num batches:',num_cfg,sim.num_batches) TF = Transformer('Tiled') # the pareto front can be restarted, need to try for bi in range(num_cfg): # DDPG-based generator C_param = cgen.get() # apply the compression param chosen by the generator map50s,crs = sim.get_multi_point(selected_ranges, TF=TF, C_param=np.copy(C_param)) # optimize generator cgen.optimize((map50s[-1],crs[-1]),False) # write logs cfg_file.write(' '.join([str(n) for n in C_param])+'\n') acc_file.write(' '.join([str(n) for n in map50s])+'\n') cr_file.write(' '.join([str(n) for n in crs])+'\n')
def test_testCase21(): simulator = Simulator() initial_game = simulator.initialize_game( Position(7992, 8304), 15, [{"id": 0, "position_x": 757, "position_y": 3545}, {"id": 1, "position_x": 510, "position_y": 8170}, {"id": 2, "position_x": 1119, "position_y": 733}, {"id": 3, "position_x": 1416, "position_y": 7409}, {"id": 4, "position_x": 1110, "position_y": 8488}, {"id": 5, "position_x": 2118, "position_y": 1983}, {"id": 6, "position_x": 3167, "position_y": 480}, {"id": 7, "position_x": 6576, "position_y": 664}, {"id": 8, "position_x": 8704, "position_y": 1276}, {"id": 9, "position_x": 13340, "position_y": 5663}, {"id": 10, "position_x": 13808, "position_y": 4731}, {"id": 11, "position_x": 15355, "position_y": 3528}, {"id": 12, "position_x": 15495, "position_y": 5035}, {"id": 13, "position_x": 15182, "position_y": 6184}, {"id": 14, "position_x": 15564, "position_y": 7640}] , 7, [{"id": 0, "position_x": 3996, "position_y": 4152}, {"id": 1, "position_x": 3996, "position_y": 4844}, {"id": 2, "position_x": 3996, "position_y": 7612}, {"id": 3, "position_x": 5328, "position_y": 1384}, {"id": 4, "position_x": 7992, "position_y": 3460}, {"id": 5, "position_x": 11322, "position_y": 5536}, {"id": 6, "position_x": 11322, "position_y": 8304}] ) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score > 0
def speed_test(EXP_NAME): np.random.seed(123) torch.manual_seed(2) sim = Simulator(train=False) TF = Transformer(name=EXP_NAME) datarange = [66,70] eval_file = open(EXP_NAME+'_spdtest.log', "w", 1) if EXP_NAME in ['Tiled','TiledLegacy']: if EXP_NAME == 'Tiled': selected_ranges = [32,42,51,58,72,197] else: selected_ranges = [50, 58, 69, 85, 108,170] with open(EXP_NAME+'_MOBO_pf.log','r') as f: for lidx,line in enumerate(f.readlines()): if lidx not in selected_ranges:continue print(EXP_NAME,lidx) tmp = line.strip().split(' ') acc,cr = float(tmp[0]),float(tmp[1]) C_param = np.array([float(n) for n in tmp[2:]]) acc1,cr1 = sim.get_one_point(datarange, TF=TF, C_param=C_param) else: if EXP_NAME == 'JPEG': rate_ranges = [7,11,15,21,47,100] elif EXP_NAME == 'JPEG2000': rate_ranges = range(6) elif EXP_NAME == 'WebP': rate_ranges = [0,5,37,100] for r in rate_ranges: print(EXP_NAME,r) acc,cr = sim.get_one_point(datarange, TF=TF, C_param=r) m,s = TF.get_compression_time() eval_file.write(f"{m:.3f} {s:.3f}\n")
def pareto_front_approx(comp_name,EXP_NAME): cfg_file = open(comp_name+'_'+EXP_NAME+'_cfg.log', "w", 1) acc_file = open(comp_name+'_'+EXP_NAME+'_acc.log', "w", 1) cr_file = open(comp_name+'_'+EXP_NAME+'_cr.log', "w", 1) # test wigh 500 iter start = time.perf_counter() # setup target network # so that we only do this once sim = Simulator(train=True) cgen = C_Generator(name=EXP_NAME,explore=True) num_cfg = 500 # number of cfgs to be explored datarange = [0,100] print(EXP_NAME,'num configs:',num_cfg, 'total batches:', sim.num_batches) TF = Transformer(comp_name) # the pareto front can be restarted, need to try for bi in range(num_cfg): print(bi) # DDPG-based generator C_param = cgen.get() # apply the compression param chosen by the generator map50,cr = sim.get_one_point(datarange=datarange, TF=TF, C_param=np.copy(C_param)) # optimize generator cgen.optimize((map50,cr),False) # write logs cfg_file.write(' '.join([str(n) for n in C_param])+'\n') acc_file.write(str(float(map50))+'\n') cr_file.write(str(cr)+'\n') # test wigh 500 iter end = time.perf_counter() with open(EXP_NAME+'_time.log','w',1) as f: f.write(str(end-start)+'s')
def test_testCase20(): simulator = Simulator() initial_game = simulator.initialize_game( Position(0, 4500), 2, [{"id": 0, "position_x": 7000, "position_y": 3500}, {"id": 1, "position_x": 0, "position_y": 500}, {"id": 2, "position_x": 7000, "position_y": 5500}, {"id": 3, "position_x": 3500, "position_y": 1000}, {"id": 4, "position_x": 9250, "position_y": 8000}, {"id": 5, "position_x": 13000, "position_y": 4500}] , 2, [{"id": 0, "position_x": 3600, "position_y": 3500}, {"id": 1, "position_x": 3700, "position_y": 4500}, {"id": 2, "position_x": 3400, "position_y": 6500}, {"id": 3, "position_x": 9000, "position_y": 3500}, {"id": 4, "position_x": 8990, "position_y": 4500}, {"id": 5, "position_x": 9000, "position_y": 5500}, {"id": 6, "position_x": 11000, "position_y": 4000}, {"id": 7, "position_x": 9100, "position_y": 10}] ) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score > 0
def test_testCase11(): simulator = Simulator() initial_game = simulator.initialize_game( Position(9000, 684), 3, [{"id": 0, "position_x": 15999, "position_y": 4500}, {"id": 1, "position_x": 8000, "position_y": 7999}, {"id": 2, "position_x": 0, "position_y": 4500}] , 9, [{"id": 0, "position_x": 0, "position_y": 3033}, {"id": 1, "position_x": 1500, "position_y": 6251}, {"id": 2, "position_x": 3000, "position_y": 2502}, {"id": 3, "position_x": 4500, "position_y": 6556}, {"id": 4, "position_x": 6000, "position_y": 3905}, {"id": 5, "position_x": 7500, "position_y": 5472}, {"id": 6, "position_x": 10500, "position_y": 2192}, {"id": 7, "position_x": 12000, "position_y": 6568}, {"id": 8, "position_x": 13500, "position_y": 7448}] ) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score > 0
def test_testCase9(): simulator = Simulator() initial_game = simulator.initialize_game( Position(8000, 4500), 4, [ {"id": 0, "position_x": 4000, "position_y": 2250}, {"id": 1, "position_x": 4000, "position_y": 6750}, {"id": 2, "position_x": 12000, "position_y": 2250}, {"id": 3, "position_x": 12000, "position_y": 6750} ], 12, [ {"id": 0, "position_x": 4000, "position_y": 3375}, {"id": 1, "position_x": 12000, "position_y": 3375}, {"id": 2, "position_x": 4000, "position_y": 4500}, {"id": 3, "position_x": 12000, "position_y": 4500}, {"id": 4, "position_x": 4000, "position_y": 5625}, {"id": 5, "position_x": 12000, "position_y": 5625}, {"id": 6, "position_x": 6000, "position_y": 2250}, {"id": 7, "position_x": 8000, "position_y": 2250}, {"id": 8, "position_x": 10000, "position_y": 2250}, {"id": 9, "position_x": 6000, "position_y": 6750}, {"id": 10, "position_x": 8000, "position_y": 6750}, {"id": 11, "position_x": 10000, "position_y": 6750} ] ) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score > 0
def test_testCase7(): simulator = Simulator() initial_game = simulator.initialize_game( Position(0, 4000), 2, [ {"id": 0, "position_x": 0, "position_y": 1000}, {"id": 1, "position_x": 0, "position_y": 8000} ], 16, [ {"id": 0, "position_x": 5000, "position_y": 1000}, {"id": 1, "position_x": 5000, "position_y": 8000}, {"id": 2, "position_x": 7000, "position_y": 1000}, {"id": 3, "position_x": 7000, "position_y": 8000}, {"id": 4, "position_x": 9000, "position_y": 1000}, {"id": 5, "position_x": 9000, "position_y": 8000}, {"id": 6, "position_x": 11000, "position_y": 1000}, {"id": 7, "position_x": 11000, "position_y": 8000}, {"id": 8, "position_x": 13000, "position_y": 1000}, {"id": 9, "position_x": 13000, "position_y": 8000}, {"id": 10, "position_x": 14000, "position_y": 1000}, {"id": 11, "position_x": 14000, "position_y": 8000}, {"id": 12, "position_x": 14500, "position_y": 1000}, {"id": 13, "position_x": 14500, "position_y": 8000}, {"id": 14, "position_x": 15000, "position_y": 1000}, {"id": 15, "position_x": 15000, "position_y": 8000} ] ) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score > 0
def __init__(self): super().__init__(n_var=6, n_obj=2, n_constr=0, xl=np.array([-.5]*6), xu=np.array([.5]*6)) self.sim = Simulator(train=True) self.TF = Transformer(comp_name) self.datarange = [0,100] self.cfg_file = open(comp_name+'_NSGA2_cfg.log', "w", 1) self.acc_file = open(comp_name+'_NSGA2_acc.log', "w", 1) self.cr_file = open(comp_name+'_NSGA2_cr.log', "w", 1) self.iter = 0
def objective(x): sim = Simulator(train=True) TF = Transformer(comp_name) datarange = [0,100] print('Iter:',d['iter'],x) acc,cr = sim.get_one_point(datarange=datarange, TF=TF, C_param=x) d['cfg_file'].write(' '.join([str(n) for n in x])+'\n') d['acc_file'].write(str(float(acc))+'\n') d['cr_file'].write(str(cr)+'\n') d['iter'] += 1 return np.array([float(acc),cr])
def test_testCase10(): simulator = Simulator() initial_game = simulator.initialize_game( Position(8000, 0), 3, [ {"id": 0, "position_x": 0, "position_y": 4500}, {"id": 1, "position_x": 15999, "position_y": 4500}, {"id": 2, "position_x": 8000, "position_y": 7999} ] , 23, [ {"id": 0, "position_x": 2000, "position_y": 1200}, {"id": 1, "position_x": 3000, "position_y": 1800}, {"id": 2, "position_x": 4000, "position_y": 2400}, {"id": 3, "position_x": 5000, "position_y": 3000}, {"id": 4, "position_x": 6000, "position_y": 3600}, {"id": 5, "position_x": 9000, "position_y": 5400}, {"id": 6, "position_x": 10000, "position_y": 6000}, {"id": 7, "position_x": 11000, "position_y": 6600}, {"id": 8, "position_x": 12000, "position_y": 7200}, {"id": 9, "position_x": 13000, "position_y": 7800}, {"id": 10, "position_x": 14000, "position_y": 8400}, {"id": 11, "position_x": 14000, "position_y": 600}, {"id": 12, "position_x": 13000, "position_y": 1200}, {"id": 13, "position_x": 12000, "position_y": 1800}, {"id": 14, "position_x": 11000, "position_y": 2400}, {"id": 15, "position_x": 10000, "position_y": 3000}, {"id": 16, "position_x": 9000, "position_y": 3600}, {"id": 17, "position_x": 6000, "position_y": 5400}, {"id": 18, "position_x": 5000, "position_y": 6000}, {"id": 19, "position_x": 4000, "position_y": 6600}, {"id": 20, "position_x": 3000, "position_y": 7200}, {"id": 21, "position_x": 2000, "position_y": 7800}, {"id": 22, "position_x": 1000, "position_y": 8400} ] ) # pr = cProfile.Profile() # pr.enable() final_score, move = initial_game.get_best_move() # pr.disable() # s = io.StringIO() # sortby = 'tottime' # ps = pstats.Stats(pr, stream=s).sort_stats(sortby) # ps.print_stats() # print(s.getvalue()) pytest.total_score += final_score assert final_score > 0
def test_testCase4(): simulator = Simulator() initial_game = simulator.initialize_game( Position(8000, 2000), 1, [{"id": 0, "position_x": 8000, "position_y": 4500}], 2, [{"id": 0, "position_x": 2000, "position_y": 6500}, {"id": 1, "position_x": 14000, "position_y": 6500}] ) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score > 0
def test_testCase3(): simulator = Simulator() initial_game = simulator.initialize_game( Position(10999, 0), 1, [{"id": 0, "position_x": 8000, "position_y": 5500}, {"id": 1, "position_x": 4000, "position_y": 5500}], 1, [{"id": 0, "position_x": 1250, "position_y": 5500}, {"id": 1, "position_x": 15999, "position_y": 5500}] ) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score > 0
def test_testCase2(): simulator = Simulator() initial_game = simulator.initialize_game( Position(5000, 0), 1, [{"id": 0, "position_x": 950, "position_y": 6000}, {"id": 1, "position_x": 8000, "position_y": 6100}], 1, [{"id": 0, "position_x": 3100, "position_y": 7000}, {"id": 1, "position_x": 11500, "position_y": 7100}] ) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score > 0
def test_play_round_move(): simulator = Simulator() inital_game = simulator.initialize_game( Position(0, 0), 1, [{"id": 0, "position_x": 8250, "position_y": 4500}], 1, [{"id": 0, "position_x": 8250, "position_y": 8999}] ) ash_target_position = Position(8250, 8999) updated_game, round_score = simulator.play_round(inital_game.ash, inital_game.humans, inital_game.zombies, ash_target_position) assert updated_game.zombies[0].position.is_equal(Position(8250, 8599)) assert updated_game.ash.position.is_equal(Position(675, 737))
def test_testCase5(): simulator = Simulator() initial_game = simulator.initialize_game( Position(7500, 2000), 2, [{"id": 0, "position_x": 9000, "position_y": 1200}, {"id": 1, "position_x": 4000, "position_y": 6000}], 3, [{"id": 0, "position_x": 2000, "position_y": 1500}, {"id": 1, "position_x": 13900, "position_y": 6500}, {"id": 2, "position_x": 7000, "position_y": 7500}] ) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score > 0
def test_testCase17(): simulator = Simulator() initial_game = simulator.initialize_game( Position(3989, 3259), 4, [{"id": 0, "position_x": 3647, "position_y": 384}, {"id": 1, "position_x": 60, "position_y": 3262}, {"id": 2, "position_x": 2391, "position_y": 1601}, {"id": 3, "position_x": 2363, "position_y": 3422}] , 30, [{"id": 0, "position_x": 6485, "position_y": 499}, {"id": 1, "position_x": 7822, "position_y": 446}, {"id": 2, "position_x": 9202, "position_y": 826}, {"id": 3, "position_x": 11060, "position_y": 253}, {"id": 4, "position_x": 12568, "position_y": 808}, {"id": 5, "position_x": 14148, "position_y": 650}, {"id": 6, "position_x": 6571, "position_y": 1893}, {"id": 7, "position_x": 8484, "position_y": 2013}, {"id": 8, "position_x": 9669, "position_y": 1968}, {"id": 9, "position_x": 7570, "position_y": 3338}, {"id": 10, "position_x": 9780, "position_y": 3611}, {"id": 11, "position_x": 8360, "position_y": 4767}, {"id": 12, "position_x": 9804, "position_y": 4154}, {"id": 13, "position_x": 10935, "position_y": 4977}, {"id": 14, "position_x": 12310, "position_y": 4614}, {"id": 15, "position_x": 13891, "position_y": 4302}, {"id": 16, "position_x": 913, "position_y": 5636}, {"id": 17, "position_x": 2410, "position_y": 5912}, {"id": 18, "position_x": 3952, "position_y": 6143}, {"id": 19, "position_x": 4615, "position_y": 5995}, {"id": 20, "position_x": 6568, "position_y": 6085}, {"id": 21, "position_x": 8204, "position_y": 5579}, {"id": 22, "position_x": 9049, "position_y": 5470}, {"id": 23, "position_x": 30, "position_y": 6798}, {"id": 24, "position_x": 1798, "position_y": 6682}, {"id": 25, "position_x": 3247, "position_y": 7664}, {"id": 26, "position_x": 5005, "position_y": 7319}, {"id": 27, "position_x": 6415, "position_y": 7094}, {"id": 28, "position_x": 8159, "position_y": 7447}, {"id": 29, "position_x": 9550, "position_y": 6847}] ) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score > 0
def generate_image_samples(EXP_NAME): sim = Simulator(train=True) TF = Transformer(name=EXP_NAME,snapshot=True) datarange = [0,1]#sim.num_batches] selected_lines = [92,152] # replace pf file later with open(EXP_NAME+'_MOBO_pf.log','r') as f: for lcnt,line in enumerate(f.readlines()): if lcnt not in selected_lines: continue tmp = line.strip().split(' ') acc,cr = float(tmp[0]),float(tmp[1]) C_param = np.array([float(n) for n in tmp[2:]]) acc1,cr1 = sim.get_one_point(datarange, TF=TF, C_param=C_param) print(acc1,cr1,C_param) break m,s = TF.get_compression_time() print(m,s)
def test_testCase14(): simulator = Simulator() initial_game = simulator.initialize_game( Position(8020, 3500), 3, [{"id": 0, "position_x": 11000, "position_y": 1000}, {"id": 1, "position_x": 11000, "position_y": 6000}, {"id": 2, "position_x": 4000, "position_y": 3500}] , 5, [{"id": 0, "position_x": 15000, "position_y": 1000}, {"id": 1, "position_x": 15000, "position_y": 6000}, {"id": 2, "position_x": 120, "position_y": 3500}, {"id": 3, "position_x": 0, "position_y": 4000}, {"id": 4, "position_x": 120, "position_y": 3000}] ) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score > 0
def test_play_round_kill_zombies(): simulator = Simulator() inital_game = simulator.initialize_game( Position(0, 0), 3, [{"id": 0, "position_x": 8250, "position_y": 4500}, {"id": 1, "position_x": 8000, "position_y": 7000}, {"id": 2, "position_x": 2500, "position_y": 1000}], 2, [{"id": 0, "position_x": 100, "position_y": 100}, {"id": 0, "position_x": 200, "position_y": 200}] ) ash_target_position = Position(8250, 8999) updated_game, round_score = simulator.play_round(inital_game.ash, inital_game.humans, inital_game.zombies, ash_target_position) assert inital_game.zombies_count == 2 assert updated_game.zombies_count == 0 assert len(updated_game.zombies) == 0 assert round_score == 270
def evaluation(EXP_NAME): np.random.seed(123) torch.manual_seed(2) sim = Simulator(train=False) TF = Transformer(name=EXP_NAME) datarange = [0,sim.num_batches] eval_file = open(EXP_NAME+'_eval.log', "w", 1) if EXP_NAME in ['Tiled', 'TiledLegacy','ROI']: with open(EXP_NAME+'_MOBO_pf.log','r') as f: for line in f.readlines()[::-1]: tmp = line.strip().split(' ') acc,cr = float(tmp[0]),float(tmp[1]) C_param = np.array([float(n) for n in tmp[2:]]) acc1,cr1 = sim.get_one_point(datarange, TF=TF, C_param=C_param) eval_file.write(f"{acc1:.3f} {cr1:.3f} {acc:.3f} {cr:.3f}\n") elif EXP_NAME == 'RAW': acc,cr = sim.get_one_point(datarange, TF=None, C_param=None) eval_file.write(f"{acc:.3f} {cr:.3f}\n") elif EXP_NAME == 'Scale': for i in range(1,33): acc,cr = sim.get_one_point(datarange, TF=TF, C_param=i) eval_file.write(f"{acc:.3f} {cr:.3f}\n") else: for i in range(101): print(EXP_NAME,i) acc,cr = sim.get_one_point(datarange, TF=TF, C_param=i) eval_file.write(f"{acc:.3f} {cr:.3f}\n")
def test_testCase18(): simulator = Simulator() initial_game = simulator.initialize_game( Position(3989, 3259), 8, [{"id": 0, "position_x": 647, "position_y": 384}, {"id": 1, "position_x": 60, "position_y": 1262}, {"id": 2, "position_x": 1391, "position_y": 1601}, {"id": 3, "position_x": 1363, "position_y": 422}, {"id": 4, "position_x": 15470, "position_y": 384}, {"id": 5, "position_x": 15060, "position_y": 1262}, {"id": 6, "position_x": 11391, "position_y": 1601}, {"id": 7, "position_x": 11363, "position_y": 422}] , 18, [{"id": 0, "position_x": 7900, "position_y": 1579}, {"id": 1, "position_x": 8500, "position_y": 2470}, {"id": 2, "position_x": 7500, "position_y": 3798}, {"id": 3, "position_x": 6500, "position_y": 4682}, {"id": 4, "position_x": 9000, "position_y": 5664}, {"id": 5, "position_x": 7500, "position_y": 6319}, {"id": 6, "position_x": 8500, "position_y": 7094}, {"id": 7, "position_x": 7800, "position_y": 8447}, {"id": 8, "position_x": 8100, "position_y": 8847}, {"id": 9, "position_x": 0, "position_y": 7000}, {"id": 10, "position_x": 1000, "position_y": 7900}, {"id": 11, "position_x": 3000, "position_y": 8500}, {"id": 12, "position_x": 5000, "position_y": 7500}, {"id": 13, "position_x": 7000, "position_y": 6500}, {"id": 14, "position_x": 9000, "position_y": 7000}, {"id": 15, "position_x": 11000, "position_y": 7500}, {"id": 16, "position_x": 13000, "position_y": 8500}, {"id": 17, "position_x": 15000, "position_y": 7800}] ) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score > 0
def test_testCase15(): simulator = Simulator() initial_game = simulator.initialize_game( Position(3900, 5000), 4, [{"id": 0, "position_x": 3000, "position_y": 3000}, {"id": 1, "position_x": 3000, "position_y": 5000}, {"id": 2, "position_x": 3000, "position_y": 7000}, {"id": 3, "position_x": 12000, "position_y": 3500}] , 6, [{"id": 0, "position_x": 10000, "position_y": 1000}, {"id": 1, "position_x": 10000, "position_y": 6000}, {"id": 2, "position_x": 15500, "position_y": 2000}, {"id": 3, "position_x": 15500, "position_y": 3600}, {"id": 4, "position_x": 15500, "position_y": 5000}, {"id": 5, "position_x": 0, "position_y": 1200}] ) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score > 0
def test_testCase13(): simulator = Simulator() initial_game = simulator.initialize_game( Position(4920, 6810), 10, [{"id": 0, "position_x": 50, "position_y": 4810}, {"id": 1, "position_x": 14820, "position_y": 3870}, {"id": 2, "position_x": 10869, "position_y": 8250}, {"id": 3, "position_x": 9695, "position_y": 7220}, {"id": 4, "position_x": 10160, "position_y": 5600}, {"id": 5, "position_x": 12988, "position_y": 5820}, {"id": 6, "position_x": 14892, "position_y": 5180}, {"id": 7, "position_x": 881, "position_y": 1210}, {"id": 8, "position_x": 7258, "position_y": 2130}, {"id": 9, "position_x": 13029, "position_y": 6990}] , 14, [{"id": 0, "position_x": 11048, "position_y": 720}, {"id": 1, "position_x": 2155, "position_y": 1650}, {"id": 2, "position_x": 9618, "position_y": 2820}, {"id": 3, "position_x": 12157, "position_y": 3770}, {"id": 4, "position_x": 2250, "position_y": 5180}, {"id": 5, "position_x": 8617, "position_y": 4890}, {"id": 6, "position_x": 7028, "position_y": 960}, {"id": 7, "position_x": 1518, "position_y": 280}, {"id": 8, "position_x": 7996, "position_y": 4080}, {"id": 9, "position_x": 13029, "position_y": 150}, {"id": 10, "position_x": 3119, "position_y": 4600}, {"id": 11, "position_x": 3339, "position_y": 4150}, {"id": 12, "position_x": 894, "position_y": 7340}, {"id": 13, "position_x": 7550, "position_y": 7550}] ) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score > 0
def test_testCase12(): simulator = Simulator() initial_game = simulator.initialize_game( Position(8000, 4000), 2, [{"id": 0, "position_x": 0, "position_y": 4000}, {"id": 1, "position_x": 15000, "position_y": 4000}] , 9, [{"id": 0, "position_x": 4333, "position_y": 1800}, {"id": 1, "position_x": 4333, "position_y": 3600}, {"id": 2, "position_x": 4333, "position_y": 5400}, {"id": 3, "position_x": 4333, "position_y": 7200}, {"id": 4, "position_x": 10666, "position_y": 1800}, {"id": 5, "position_x": 10666, "position_y": 3600}, {"id": 6, "position_x": 10666, "position_y": 5400}, {"id": 7, "position_x": 10666, "position_y": 7200}, {"id": 8, "position_x": 0, "position_y": 7200}] ) final_score, move = initial_game.get_best_move() pytest.total_score += final_score assert final_score > 0
class MyProblem(Problem): def __init__(self): super().__init__(n_var=6, n_obj=2, n_constr=0, xl=np.array([-.5]*6), xu=np.array([.5]*6)) self.sim = Simulator(train=True) self.TF = Transformer(comp_name) self.datarange = [0,100] self.cfg_file = open(comp_name+'_NSGA2_cfg.log', "w", 1) self.acc_file = open(comp_name+'_NSGA2_acc.log', "w", 1) self.cr_file = open(comp_name+'_NSGA2_cr.log', "w", 1) self.iter = 0 def _evaluate(self, x, out, *args, **kwargs): points = [] for row in range(x.shape[0]): acc,cr = self.sim.get_one_point(datarange=self.datarange, TF=self.TF, C_param=x[row,:]) points += [[float(acc),cr]] self.cfg_file.write(' '.join([str(n) for n in x[row,:]])+'\n') self.acc_file.write(str(float(acc))+'\n') self.cr_file.write(str(cr)+'\n') print('Iter:',self.iter) self.iter += 1 out["F"] = np.array(points)
def dual_train(net): np.random.seed(123) criterion = nn.MSELoss(reduction='sum') optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9) log_file = open('training.log', "w", 1) log_file.write('Training...\n') # setup target network # so that we only do this once sim = Simulator(10) cgen = C_Generator() num_cfg = 1#sim.point_per_sim//batch_size print('Num batches:',num_cfg,sim.point_per_sim) for epoch in range(10): running_loss = 0.0 TF = Transformer('Tiled') # the pareto front can be restarted, need to try for bi in range(num_cfg): inputs,labels = [],[] # DDPG-based generator C_param = cgen.get() # batch result of mAP and compression ratio batch_acc, batch_cr = [],[] for k in range(batch_size): di = bi*batch_size + k # data index # start counting the compressed size TF.reset() # apply the compression param chosen by the generator fetch_start = time.perf_counter() # the function to get results from cloud model sim_result = sim.get_one_point(index=di, TF=TF, C_param=np.copy(C_param)) fetch_end = time.perf_counter() # get the compression ratio cr = TF.get_compression_ratio() batch_acc += [sim_result] batch_cr += [cr] print_str = str(di)+str(C_param)+'\t'+str(sim_result)+'\t'+str(cr)+'\t'+str(fetch_end-fetch_start) print(print_str) log_file.write(print_str+'\n') inputs.append(C_param) labels.append(sim_result) # accuracy of IoU=0.5 # optimize generator cgen.optimize((np.mean(batch_acc),np.mean(batch_cr)),False) log_file.write(print_str+'\n') # transform to tensor inputs = torch.FloatTensor(inputs)#.cuda() labels = torch.FloatTensor(labels)#.cuda() # zero gradient optimizer.zero_grad() # forward + backward + optimize outputs = net(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() # print statistics running_loss += loss.item() val_loss = abs(torch.mean(labels.cpu()-outputs.cpu())) print_str = '{:d}, {:d}, loss {:.6f}, val loss {:.6f}'.format(epoch + 1, bi + 1, loss.item(), val_loss) print(print_str) log_file.write(print_str + '\n') print_str = str(cgen.paretoFront.data.keys()) print(print_str) cgen.optimize(None,True) torch.save(net.state_dict(), PATH)