def test1(self): p = Problem('input2019/a_example.txt') s = Solution(p) s.add_horizontal(1) s.add_vertical(1, 2) s.add_horizontal(3) self.assertFalse(s.check_correctness())
def solve_antonio_sampling(p: Problem, input): s = Solution(p) myset = set(input) tail = myset.pop() head = myset.pop() s.add_any(head[0]) s.add_any(tail[0]) while (len(myset) > 0): tmp_max_tail = -1 selected_item_tail = None tmp_max_head = -1 selected_item_head = None testset = random.sample(myset, min(iteration, len(myset))) for val in testset: tmp_tail = computescore(tail, val) tmp_head = computescore(head, val) if (tmp_tail >= tmp_max_head): tmp_max_tail = tmp_tail selected_item_tail = val if (tmp_head >= tmp_max_head): tmp_max_head = tmp_head selected_item_head = val if tmp_max_tail > tmp_max_head: tail = selected_item_tail s.add_any(selected_item_tail[0]) myset.remove(selected_item_tail) else: head = selected_item_head s.add_any_head(selected_item_head[0]) myset.remove(selected_item_head) return s
def solve_combinatorial2(problem: Problem): s = Solution(problem) s = solve_combinatorial2_one_pass(problem, s, effective_x=problem.X / 2) random.shuffle(problem.requests) s.improvements_history.append("Second pass") s = solve_combinatorial2_one_pass(problem, s, effective_x=problem.X) return s
def b_test_solve(p): g = [[] for i in range(N)] used = [False for i in range(N)] sys.setrecursionlimit(N * 10) tags_ids = dict() solution = Solution(p) def dfs(v): used[v] = True solution.add_horizontal(v) for u in g[v]: if used[u] == False: # pass dfs(u) for pic_id in tqdm.tqdm(range(p.num_pics)): for tag in p.tags[pic_id]: if tag not in tags_ids: tags_ids[tag] = [] tags_ids[tag].append(pic_id) for tag, tag_id_list in tags_ids.items(): assert (len(tag_id_list) <= 2) if len(tag_id_list) == 2: g[tag_id_list[0]].append(tag_id_list[1]) g[tag_id_list[1]].append(tag_id_list[0]) v = -1 for i in range(N): if used[i] == False: v = i while (True): used[v] = True found = False solution.add_horizontal(v) for u in g[v]: if used[u] == False: v = u found = True break if found == False: break return solution
def solve_tobi(p: Problem): s = Solution(p) current = None remaining_pics = [] for i in range(p.num_pics): if (p.orientation[i] != 'H'): continue # for now only horizontal # init first if (current == None): current = p.tags[i] s.add_horizontal(i) remaining_pics = set(range(i + 1, p.num_pics)) best_candidate = -1 best_score = -1 for j in remaining_pics: candidate_id = j if (p.orientation[candidate_id] != 'H'): continue # for now only horizontal candidate = p.tags[candidate_id] score_candidate = score(current, candidate) if (score_candidate > best_score): best_candidate = candidate_id best_score = score_candidate if best_candidate != -1: s.add_horizontal(best_candidate) remaining_pics = remaining_pics - set([best_candidate]) return s
def solve_antonio_sequential(p: Problem, input): s = Solution(p) myset = set(input) current = myset.pop() s.add_any(current[0]) while (len(myset) > 0): tmp_max = -1 selected_item = None testset = random.sample(myset, min(iteration, len(myset))) for i, val in enumerate(myset): if (i > 50): break tmp = computescore(current, val) if (tmp >= tmp_max): tmp_max = tmp selected_item = val current = selected_item s.add_any(selected_item[0]) myset.remove(selected_item) return s
INPUT_DIR = "input2019/" OUTPUT_DIR = "output/" FILENAMES = ["a_example.txt", "b_lovely_landscapes.txt", "c_memorable_moments.txt", "d_pet_pictures.txt", "e_shiny_selfies.txt"] def final_solution(p): #return solve_random(p) s = solve_antonio(p, p.result_images) assert s.check_correctness() return s if __name__ == "__main__": for filename in FILENAMES: p = Problem(INPUT_DIR + filename) s = Solution(p) if filename == "b_lovely_landscapes.txt": s = b_test_sol.b_test_solve(p) else: s = final_solution(p) s.write(OUTPUT_DIR + filename + ".out") print(filename, s.calculate_score()) os.system("zip result.zip *.py") # s.write('solutions/kittens.out')
def test5(self): p = Problem('input2019/a_example.txt') s = Solution(p) s.add_vertical(1, 2) self.assertTrue(s.check_correctness())
def test4(self): p = Problem('input2019/a_example.txt') s = Solution(p) s.add_horizontal(0) s.add_horizontal(3) self.assertTrue(s.check_correctness())
def solve_random(p): random.shuffle(p.result_images) s = Solution(p) for idx, tags in p.result_images: s.add_any(idx) return s
def solve_combinatorial(problem: Problem): local_requests = problem.requests[:1000] cache_range = range(10) solver = pywraplp.Solver('SolveIntegerProblem', pywraplp.Solver.BOP_INTEGER_PROGRAMMING,) # solver.set_time_limit(10000) p_vars = [[solver.IntVar(0.0, 1, 'p_%s_%s' % (v, c)) for c in cache_range] for v in range(problem.V)] f_vars = [] print("P initialized") objective = solver.Objective() for v, e, number in tqdm(local_requests): req_vars = [] for c in cache_range: var = solver.IntVar(0.0, 1, 'f_%s_%s_%s' % (e, v, c)) objective.SetCoefficient(var, number * problem.endpoints_caches[e][c]) req_vars.append(var) f_vars.append(req_vars) objective.SetMinimization() print("F initialized") # Capacity for c in cache_range: constraint = solver.Constraint(-solver.infinity(), problem.X) for v in range(problem.V): size = problem.video_sizes[v] constraint.SetCoefficient(p_vars[v][c], size) print("Capacity done") # Presence for (v, e, number), req_vars in zip(local_requests, f_vars): for c, var in zip(cache_range, req_vars): constraint = solver.Constraint(0, solver.infinity()) constraint.SetCoefficient(p_vars[v][c], 1) constraint.SetCoefficient(var, -1) print("Presence done") # From-correctness for req_vars in f_vars: constraint = solver.Constraint(1, 1) for var in req_vars: constraint.SetCoefficient(var, 1) print("From-c done") """Solve the problem and print the solution.""" result_status = solver.Solve() # The problem has an optimal solution. # assert result_status == pywraplp.Solver.OPTIMAL # The solution looks legit (when using solvers other than # GLOP_LINEAR_PROGRAMMING, verifying the solution is highly recommended!). assert solver.VerifySolution(1e-7, True) print('Number of variables =', solver.NumVariables()) print('Number of constraints =', solver.NumConstraints()) # The objective value of the solution. print('Optimal objective value = %d' % solver.Objective().Value()) print() s = Solution(problem) for v in range(problem.V): for c in cache_range: # print(v, c, p_vars[v][c].solution_value()) if p_vars[v][c].solution_value() > 0: s.cache_servers[c].append(v) return s