def transform(problem_folder): i = 1 #l1 = [4,6,8,10,12,14,16] #l1 = [0,5,10,15,20,25,30,35,40,45] #l1 = [0,5,10,15,20,25,30] #l1 = [12,18,24,30] #l1 = [14,16,18,20,22,24,26,28,30] #l1 = [40,45,50,55,60,65,70,75,80] l1 = [20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50] for x in l1: for y in range(10): #name = 'Release%5CInstances%5C9x21_left2_right2-{0}-{1}'.format(x,y) name = 'Instance-20-10-{0}-{1}'.format(x,y) path = 'problems/original/{0}/Instances/{1}'.format(problem_folder, name) print(name, path) name2 ='Instance-{0}-{1}'.format(x,y) problem = lp_generator.Problem(50,0,0) problem.read_instance(path) path = 'problems/surynek/grid20_smt/{1}'.format(problem_folder,name2) problem.write_to_surynek3(path)
def run_test(solver_type, problem_folder, call_extra, only_first_sol, base_path): opt_makespans = [] if solver_type == 2: with open('problems/original/{0}/opt_makespan'.format(problem_folder), 'r') as in_file: opt_makespans = [int(line.strip()) for line in in_file] with open('results/results_{0}_{1}_last.csv'.format(problem_folder, base_path.split('/')[-1]), 'w') as results: results.write('sep=;\n') ''' results.write('Instance; First_solved;solved;Models; Optimum; Calls;Threads;MIN-Timestep;Min-SUMTIME; ;' '1stTime(Total); 1stTime(Solving);1stTime(Unsat); 1stCPU Time;1stOptimization;1stSOL-COST; 1st Makespan; ;' 'OPT - Optimization; OPT- Time(Total); OPT-Time(Solving); OPT-Time(Unsat); OPT-CPU Time; OPT-SOL-COST; Last-Makespan ;OPT-Makespan; OPT-Solved;' 'Moved On Goal; Theoric Makespan; 1stRunTime ;Total RunTime; Ground Time; Percent Grounding; Called Extra; Atoms; Bodies; Rules; Total\n') ''' results.write('Instance; First_solved;solved;;' '1stOPT;1stSOL-COST;1stMakespan;1stTheoricMakespan;1stRunTime;1stGroundTime;1stGroundPerc;1stAtoms;1stBodies;1stRules;1stTotal;;' 'OPT;SOL-COST;Makespan;TheoricMakespan;RunTime;GroundTime;GroundPerc;Atoms;Bodies;Rules;Total\n') print(base_path.split('/')[-1]) i = 1 #l1 = [4,5,6,7,8,9,10,11,12,13,14,15,16,17,18] #l1 = [0,5,10,15,20,25,30] #l1 = [12,18,24,30] #l1 = [14,16,18,20,22,24,26,28,30] #l1 = [40,45,50,55,60,65,70,75,80] l1 = [20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50] #l1 = [42,44,46,48,50] #l1 = [1,60,65,70,75,80,85,90,95,100,100] #l1 = [42,44,46,48,50] l1 = [50] for x in l1: for y in range(10): name = 'Instance-20-10-{0}-{1}'.format(x,y) #name = 'brc202d-{0}-{1}'.format(x,y) path = 'problems/original/{0}/Instances/{1}'.format(problem_folder, name) i+=1 if y != 5: continue #print(name) ''' if x < 38: continue if x == 38 and (y <= 6 or y == 8): continue if x == 40 and (y == 0 or y == 2 or y == 3 or y == 4 or y == 8): continue ''' problem = lp_generator.Problem(50) print('reading instance') problem.read_instance(path) print('generating solution') problem.gen_solution() path = 'problems/asp/{0}/{1}'.format(problem_folder,name) problem.write_to_lp(path) print('Solving with clingo...') start_time = time.time() num = problem.max_time ground_time = 0 ground_time0 = 0 runtime = 0 runtime0 = 0 print(num) first_solved = False solved = False solv0 = None solv = None curr = 300 while True: solv0 = asp_solver.IncrementalSolver('{0}.lp'.format(path), num, problem.num_agents, problem.min_sum, problem.total_cost, solver_type, only_first_sol) clingo.clingo_main(solv0, ['{0}.lp'.format(path), '{0}.lp'.format(base_path) , '--outf=3', '--opt-strat=usc,disjoint', '--time-limit={0}'.format(curr), '-t', '4', '-c','bound={0}'.format(num)]) #print(solv0.ground_time) ground_time += solv0.ground_time elapsed = time.time() - start_time curr = int(300 - elapsed) print(curr) if curr < 0: break if solv0.sol_cost > 0: runtime0 = time.time() - start_time ms = int(solv0.theoric_makespan) first_solved = True ground_time0 = ground_time print(ms) if ms == num: solv = solv0 solved = True runtime = runtime0 break break solv = asp_solver.IncrementalSolver('{0}.lp'.format(path), ms, problem.num_agents, problem.min_sum, problem.total_cost, solver_type, only_first_sol) clingo.clingo_main(solv, ['{0}.lp'.format(path), '{0}.lp'.format(base_path) , '--outf=3', '--opt-strat=usc,disjoint', '--time-limit={0}'.format(curr), '-t', '4', '-c','bound={0}'.format(ms)]) ground_time += solv.ground_time if solv.stats is not None: runtime = time.time() - start_time summary = solv.stats['summary'] solved = True break num += 1 print(num) #sol = solv.resp row = [name] #print(solv.sol_cost) if first_solved: #print(json.dumps(solv.stats, sort_keys=True, indent=4, separators=(',', ': '))) row.append(1) row.append(0) row.append('\t') row.append(format_float(solv0.stats['summary']['costs'][0])) row.append(format_float(solv0.sol_cost)) row.append('-1') row.append(int(solv0.theoric_makespan)) row.append(format_float(runtime0)) row.append(format_float(ground_time0)) row.append(format_float(ground_time0/runtime0*100)) row.append(format_float(solv0.stats['problem']['lp']['atoms'])) row.append(format_float(solv0.stats['problem']['lp']['bodies'])) row.append(format_float(solv0.stats['problem']['lp']['rules'])) row.append(format_float(solv0.stats['problem']['lp']['atoms'] + solv0.stats['problem']['lp']['bodies'] + solv0.stats['problem']['lp']['rules'])) row.append('\t') if solved: row[2] = 1 row.append(format_float(solv.stats['summary']['costs'][0])) row.append(format_float(solv.sol_cost)) row.append('-1') row.append(int(solv.theoric_makespan)) row.append(format_float(runtime)) row.append(format_float(ground_time)) row.append(format_float(ground_time/runtime*100)) row.append(format_float(solv.stats['problem']['lp']['atoms'])) row.append(format_float(solv.stats['problem']['lp']['bodies'])) row.append(format_float(solv.stats['problem']['lp']['rules'])) row.append(format_float(solv.stats['problem']['lp']['atoms'] + solv.stats['problem']['lp']['bodies'] + solv.stats['problem']['lp']['rules'])) else: row.append(0) row.append(0) #print(elapsed_time) #print(solv.resp) #print(check_makespan(solv.resp)) results.write(';'.join(map(str, row)) + '\n') results.flush()
def run_problem(instance_path, base_path, results_path, penalty_type, results_mode): write_type = 'a' if results_mode: write_type = 'w' with open(results_path, write_type) as results: if results_mode: results.write('sep=;\n') results.write( 'Instance;cost_relax;makespan_relax;First_solved;solved;;' '1stOPT;1stSOL-COST;1stMakespan;1stTheoricMakespan;1stRunTime;1stGroundTime;1stGroundPerc;1stAtoms;1stBodies;1stRules;1stTotal;;' 'OPT;SOL-COST;Makespan;TheoricMakespan;RunTime;GroundTime;GroundPerc;Atoms;Bodies;Rules;Total\n' ) results.flush() problem = lp_generator.Problem(50) print('reading instance') problem.read_instance(instance_path) print('generating solution') problem.gen_solution() buffer_path = 'buffer' problem.write_to_lp(buffer_path) print('Solving with clingo...') start_time = time.time() ms_max = problem.max_time ground_time = 0 ground_time0 = 0 runtime = 0 runtime0 = 0 makespan_relax = ms_max + 1 ms0 = ms_max cost_relax = problem.total_cost first_solved = False solved = False solv0 = None solv = None curr = 300 elapsed_real = 0 try: while True: solv0 = asp_solver.IncrementalSolver(ms_max, problem.num_agents, problem.min_sum, problem.total_cost, ms0, penalty_type, False) clingo.clingo_main(solv0, [ buffer_path, base_path, '--outf=3', '--opt-strat=usc,disjoint', '--time-limit={0}'.format(curr), '-t', '4', '-c', 'bound={0}'.format(ms_max) ]) ground_time += solv0.ground_time elapsed_real += (solv0.solve_time + solv0.ground_time) if solv0.error: break curr = int(300 - elapsed_real) if curr < 0: break if solv0.sol_cost > 0: runtime0 = elapsed_real first_ms = ms_max + 1 ms_opt = int(solv0.theoric_makespan) first_solved = True ground_time0 = ground_time if ms_opt == ms_max: solv = solv0 solved = True runtime = runtime0 final_ms = first_ms break solv = asp_solver.IncrementalSolver( ms_opt, problem.num_agents, problem.min_sum, problem.total_cost, ms0, penalty_type, True) clingo.clingo_main(solv, [ buffer_path, base_path, '--outf=3', '--opt-strat=usc,disjoint', '--time-limit={0}'.format(curr), '-t', '4', '-c', 'bound={0}'.format(ms_opt) ]) ground_time += solv.ground_time if solv.stats is not None: runtime = elapsed_real + (solv.solve_time + solv.ground_time) final_ms = check_makespan(solv.resp) summary = solv.stats['summary'] solved = True break ms_max += 1 except: print(traceback.format_exc()) row = [instance_path] row.append(cost_relax) row.append(makespan_relax) if first_solved: row.append(1) row.append(0) row.append('\t') row.append(format_float(solv0.stats['summary']['costs'][0])) row.append(format_float(solv0.sol_cost)) row.append(first_ms) row.append(int(solv0.theoric_makespan) + 1) row.append(format_float(runtime0)) row.append(format_float(ground_time0)) row.append(format_float(ground_time0 / runtime0 * 100)) row.append(format_float(solv0.stats['problem']['lp']['atoms'])) row.append(format_float(solv0.stats['problem']['lp']['bodies'])) row.append(format_float(solv0.stats['problem']['lp']['rules'])) row.append( format_float(solv0.stats['problem']['lp']['atoms'] + solv0.stats['problem']['lp']['bodies'] + solv0.stats['problem']['lp']['rules'])) row.append('\t') if solved: row[4] = 1 row.append(format_float(solv.stats['summary']['costs'][0])) row.append(format_float(solv.sol_cost)) row.append(final_ms) row.append(int(solv.theoric_makespan) + 1) row.append(format_float(runtime)) row.append(format_float(ground_time)) row.append(format_float(ground_time / runtime * 100)) row.append(format_float(solv.stats['problem']['lp']['atoms'])) row.append(format_float(solv.stats['problem']['lp']['bodies'])) row.append(format_float(solv.stats['problem']['lp']['rules'])) row.append( format_float(solv.stats['problem']['lp']['atoms'] + solv.stats['problem']['lp']['bodies'] + solv.stats['problem']['lp']['rules'])) else: row.append(0) row.append(0) results.write(';'.join(map(str, row)) + '\n') results.flush()
def run(problem_path, lp_path, base_path, landmarks, overflow): num_landmarks = int(landmarks) num_overflow = int(overflow) problem = lp_generator.Problem(50, num_landmarks, num_overflow) print('reading instance') problem.read_instance(problem_path) print('generating solution') problem.gen_solution() problem.write_to_lp(lp_path) print('Solving with clingo...') return start_time = time.time() num_makespan = problem.max_time + num_overflow ground_time = 0 runtime = 0 first_solved = False solved = False curr_time = 300 print(num_makespan) solv0 = asp_solver.IncrementalSolver('{0}.lp'.format(lp_path), num_makespan, problem.num_agents, problem.min_sum, problem.total_cost, 4, True) clingo.clingo_main(solv0, [ '{0}.lp'.format(lp_path), '{0}'.format(base_path), '--time-limit={0}'.format(curr_time), '-t', '4', '-c', 'bound={0}'.format(num_makespan), '-c', 'lbound={0}'.format(num_landmarks), '--opt-strategy=usc,disjoint', '--text' ]) print(solv0.resp) print('found solution: ') print('\t total_cost: {0}'.format(solv0.sol_cost)) #print('\t makespan: {0}'.format(check_makespan(solv.resp))) return while curr_time >= 0: print('solving with makespan: {0}'.format(num_makespan + 1)) solv0 = asp_solver.IncrementalSolver('{0}.lp'.format(lp_path), num_makespan, problem.num_agents, problem.min_sum, problem.total_cost, 4, True) clingo.clingo_main(solv0, [ '{0}.lp'.format(lp_path), '{0}'.format(base_path), '--outf=3', '--opt-strat=usc,disjoint', '--time-limit={0}'.format(curr_time), '-t', '4', '-c', 'bound={0}'.format(num_makespan) ]) ground_time += solv0.ground_time elapsed = time.time() - start_time curr_time = int(300 - elapsed) if curr_time < 0: break if solv0.sol_cost > 0: runtime0 = time.time() - start_time ms = int(solv0.theoric_makespan) first_solved = True ground_time0 = ground_time if ms == num_makespan: solv = solv0 solved = True runtime = runtime0 break print('solving with makespan: {0}'.format(ms + 1)) solv = asp_solver.IncrementalSolver('buffer.lp'.format(lp_path), ms, problem.num_agents, problem.min_sum, problem.total_cost, 4, True) clingo.clingo_main(solv, [ '{0}.lp'.format(lp_path), '{0}'.format(base_path), '--outf=3', '--opt-strat=usc,disjoint', '--time-limit={0}'.format(curr_time), '-t', '4', '-c', 'bound={0}'.format(ms) ]) ground_time += solv.ground_time if solv.stats is not None: runtime = time.time() - start_time ground_time += solv.ground_time solved = True break num_makespan += 1 print('----------') if solved: print('found solution: ') print('\t total_cost: {0}'.format(solv.sol_cost)) #print('\t makespan: {0}'.format(check_makespan(solv.resp))) print('\t runtime: {0} seconds'.format(runtime)) print('\t ground_time: {0} seconds'.format(ground_time)) else: print('No solution found')
def run_problem(problem_folder, base_path, results_path, instance_name, window_size): with open(results_path, 'a') as results: path = '../problems/original/{0}/Instances/{1}'.format(problem_folder, instance_name) #path = 'Instance-8-10-4-0' #path = 'brc202d-70-0' #path = 'ost003d-70-0' problem = lp_generator.Problem(window_size) print('reading instance') problem.read_instance(path) print('generating solution') problem.gen_solution() window_id = 0 #path = 'problems/asp/{0}/{1}-{2}'.format(problem_folder,instance_name, window_id) path = 'buffer' print('Solving with clingo...') start_time = time.time() max_bound = problem.max_time print(max_bound) ground_time = 0 curr_time = 600 solved = False current_makespan = 0 current_costs = [] current_penalty = [] total_cost = 0 for ag in range(problem.num_agents): current_costs.append(0) current_penalty.append(0) problem.write_to_lp_window(path, problem.agents_pos, current_penalty) while True: current_makespan += window_size solv = asp_solver.IncrementalSolver('{0}.lp'.format(path), problem.num_agents, window_size) clingo.clingo_main(solv, ['{0}.lp'.format(path), '{0}.lp'.format(base_path), '--opt-strat=usc,disjoint','--outf=3','--time-limit={0}'.format(curr_time), '-t', '4', '-c', 'window_bound={0}'.format(window_size)]) window_id+=1 ground_time += solv.ground_time elapsed = time.time() - start_time curr_time = int(600.0 - elapsed) print(elapsed) if curr_time < 0: break if solv.sol_cost < 0: break solved_agents = problem.check_solved(solv.final_pos) all_solved = True total_cost = 0 for ag in range(problem.num_agents): if solved_agents[ag]: current_penalty[ag] += window_size - (solv.current_costs[ag] - solv.reset_penalty[ag]) else: all_solved = False current_costs[ag] += solv.current_costs[ag] total_cost += current_costs[ag] #print(current_costs) if all_solved: solved = True break #x = input() #path = 'problems/asp/{0}/{1}-{2}'.format(problem_folder,instance_name, window_id) path = 'buffer' problem.write_to_lp_window(path, solv.final_pos, current_penalty) row = [instance_name] if solved: row.append(1) else: row.append(0) row.append(ground_time) row.append(elapsed) row.append(total_cost) row.append(window_id) row.append(window_size) results.write(';'.join(map(str, row)) + '\n') results.flush()
def run_problem(problem_folder, base_path, results_path, instance_name): with open(results_path, 'a') as results: path = 'problems/original/{0}/Instances/{1}'.format( problem_folder, instance_name) problem = lp_generator.Problem(50, 0, 0) print('reading instance') problem.read_instance(path) print('generating solution') problem.gen_solution() path = 'problems/asp/{0}/{1}'.format(problem_folder, instance_name) problem.write_to_lp(path) print('Solving with clingo...') start_time = time.time() num = problem.max_time ground_time = 0 ground_time0 = 0 runtime = 0 runtime0 = 0 print(num) first_solved = False solved = False solv0 = None solv = None curr = 300 while True: solv0 = asp_solver.IncrementalSolver('{0}.lp'.format(path), num, problem.num_agents, problem.min_sum, problem.total_cost, 4, False) clingo.clingo_main(solv0, [ '{0}.lp'.format(path), '{0}.lp'.format(base_path), '--outf=3', '--opt-strat=usc,disjoint', '--time-limit={0}'.format(curr), '-t', '4', '-c', 'bound={0}'.format(num) ]) #print(solv0.ground_time) ground_time += solv0.ground_time elapsed = time.time() - start_time curr = int(300 - elapsed) print(curr) if curr < 0: break if solv0.sol_cost > 0: runtime0 = time.time() - start_time ms = int(solv0.theoric_makespan) first_solved = True ground_time0 = ground_time print(ms) if ms == num: solv = solv0 solved = True runtime = runtime0 break solv = asp_solver.IncrementalSolver('{0}.lp'.format(path), ms, problem.num_agents, problem.min_sum, problem.total_cost, 4, False) clingo.clingo_main(solv, [ '{0}.lp'.format(path), '{0}.lp'.format(base_path), '--outf=3', '--opt-strat=usc,disjoint', '--time-limit={0}'.format(curr), '-t', '4', '-c', 'bound={0}'.format(ms) ]) ground_time += solv.ground_time if solv.stats is not None: runtime = time.time() - start_time summary = solv.stats['summary'] solved = True break num += 1 print(num) #sol = solv.resp row = [instance_name] #print(solv.sol_cost) if first_solved: #print(json.dumps(solv.stats, sort_keys=True, indent=4, separators=(',', ': '))) row.append(1) row.append(0) row.append('\t') row.append(format_float(solv0.stats['summary']['costs'][0])) row.append(format_float(solv0.sol_cost)) row.append('-1') row.append(int(solv0.theoric_makespan)) row.append(format_float(runtime0)) row.append(format_float(ground_time0)) row.append(format_float(ground_time0 / runtime0 * 100)) row.append(format_float(solv0.stats['problem']['lp']['atoms'])) row.append(format_float(solv0.stats['problem']['lp']['bodies'])) row.append(format_float(solv0.stats['problem']['lp']['rules'])) row.append( format_float(solv0.stats['problem']['lp']['atoms'] + solv0.stats['problem']['lp']['bodies'] + solv0.stats['problem']['lp']['rules'])) row.append('\t') if solved: row[2] = 1 row.append(format_float(solv.stats['summary']['costs'][0])) row.append(format_float(solv.sol_cost)) row.append('-1') row.append(int(solv.theoric_makespan)) row.append(format_float(runtime)) row.append(format_float(ground_time)) row.append(format_float(ground_time / runtime * 100)) row.append(format_float(solv.stats['problem']['lp']['atoms'])) row.append(format_float(solv.stats['problem']['lp']['bodies'])) row.append(format_float(solv.stats['problem']['lp']['rules'])) row.append( format_float(solv.stats['problem']['lp']['atoms'] + solv.stats['problem']['lp']['bodies'] + solv.stats['problem']['lp']['rules'])) else: row.append(0) row.append(0) #print(elapsed) #print(solv.resp) #print(check_makespan(solv.resp)) results.write(';'.join(map(str, row)) + '\n') results.flush()