def inserting(wizards, constraints): constraint_map = utils.get_constraint_map(constraints) violations = utils.check_violations(wizards, constraint_map) while violations > 0: starting_violations = violations for i in range(len(wizards)): best_cur_violations = violations best_j = i cur_wizard = wizards[i] wizards.remove(cur_wizard) wizards = [cur_wizard] + wizards for j in range(len(wizards) - 1): temp_violations = utils.check_violations( wizards, constraint_map) if temp_violations <= best_cur_violations: best_cur_violations = temp_violations best_j = j wizards[j], wizards[j + 1] = wizards[j + 1], wizards[j] wizards.pop() wizards.insert(best_j, cur_wizard) violations = best_cur_violations if starting_violations == violations: random.shuffle(wizards) print("Stuck at " + str(violations) + " violations") print(wizards) violations = utils.check_violations(wizards, constraint_map) return wizards
def swap(wizards, constraints): constraint_map = utils.get_constraint_map(constraints) violations = utils.check_violations(wizards, constraint_map) temp_list = wizards[:] while violations > 0: besti = 0 bestj = 0 for i in range(len(wizards) - 1): for j in range(1, len(wizards)): temp_list[i], temp_list[j] = temp_list[j], temp_list[i] temp_violations = utils.check_violations( temp_list, constraint_map) if temp_violations < violations: besti = i bestj = j violations = temp_violations temp_list[i], temp_list[j] = temp_list[j], temp_list[i] if besti == bestj: random.shuffle(wizards) temp_list = wizards[:] print(violations) print(wizards) violations = utils.check_violations(wizards, constraint_map) else: x = violations wizards[besti], wizards[bestj] = wizards[bestj], wizards[besti] temp_list[besti], temp_list[bestj] = temp_list[bestj], temp_list[ besti] return temp_list
def solve(wizards, constraints): constraint_map = utils.get_constraint_map(constraints) violations = utils.check_violations(wizards, constraint_map) sorted_wizards = utils.sort_wizards(wizards, constraint_map) while violations > 0: starting_violations = violations for wizard in sorted_wizards: violations, wizards = place_in_best_location( violations, wizard, wizards, constraint_map) if starting_violations == violations: random.shuffle(wizards) print("Stuck at " + str(violations) + " violations") print(wizards) violations = utils.check_violations(wizards, constraint_map) return wizards
def helper(wizards, sorted_wizards): if tuple(wizards) in seen: return [] seen.add(tuple(wizards)) violations = utils.check_violations(wizards, constraint_map) print(violations) print(wizards) if violations == 0: return wizards for wizard in sorted_wizards: locations = get_best_locations(violations, wizard, wizards, constraint_map) if len(locations) == len(wizards): return locations for ndx in locations: wizard_dimension = wizards[:] wizard_dimension.remove(wizard) wizard_dimension.insert(ndx, wizard) sorted_dimension = sorted_wizards[:] sorted_dimension.remove(wizard) r = helper(wizard_dimension, sorted_dimension) if len(r) != 0: return r return []
def place_in_best_location(violations, wizard, wizards, constraint_map): best_cur_violations = violations best_j = wizards.index(wizard) wizards.remove(wizard) wizards = [wizard] + wizards for j in range(len(wizards) - 1): temp_violations = utils.check_violations(wizards, constraint_map) if temp_violations < best_cur_violations: best_cur_violations = temp_violations best_j = j wizards[j], wizards[j + 1] = wizards[j + 1], wizards[j] wizards.pop() wizards.insert(best_j, wizard) return best_cur_violations, wizards
def get_best_locations(violations, wizard, wizards, constraint_map): wizards = wizards[:] best_cur_violations = violations starting_index = wizards.index(wizard) best_ndxs = [] wizards.remove(wizard) wizards = [wizard] + wizards for ndx in range(len(wizards) - 1): if ndx != starting_index: temp_violations = utils.check_violations(wizards, constraint_map) if temp_violations == 0: return wizards if temp_violations < best_cur_violations: best_cur_violations = temp_violations best_ndxs = [ndx] elif temp_violations == best_cur_violations: best_ndxs.append(ndx) wizards[ndx], wizards[ndx + 1] = wizards[ndx + 1], wizards[ndx] return best_ndxs