def run(self): """starts algorithm""" # sort nets by priority self.board.nets.sort(key=lambda net: net.priority_num, reverse=True) # for every net i = 0 while i < len(self.board.nets): net = self.board.nets[i] # add wire coordinates to grid solution = self.a_star_search(net) if solution: net.route = solution self.board.add_net(net) i += 1 # if no solution restart with unsolved net first else: self.board.reset_grid() self.board.nets.remove(net) self.board.nets.insert(0, net) i = 0 print("Restarting...") # total cost self.board.cost = helpers.calc_cost(self.board)
def rewire(self, nets, best_cost): """rewires nets""" # all permutations of nets permutations = list(itertools.permutations(nets)) best_permutation = [] solution_found = False while permutations: # current permutation nets = permutations.pop() # remove nets from grid for net in nets: self.board.remove_net(net) # replace nets for net in nets: # find solution route = self.a_star_search(net) if route: solution_found = True net.route = route self.board.add_net(net) else: solution_found = False print("skip permutation") break # no solution for this permutation if not solution_found: continue # check if current permutation is improvement rewire_cost = helpers.calc_cost(self.board) if rewire_cost < best_cost: print(f"improvement found: from {best_cost} to {rewire_cost}") best_permutation = [net.route for net in nets] self.board.cost = best_cost = rewire_cost # remove nets from grid for net in nets: self.board.remove_net(net) # return best permutation return best_permutation
def __init__(self, board, i, filename="./data/output/output.csv"): self.board = board self.grouped_nets = [] # read output file if first iteration if i == 0: self.board.read_output(filename) else: for net in self.board.nets: net.intersections = [] net.num_of_intersections = 0 # prior solution's cost self.board.cost = helpers.calc_cost(self.board) # create groups for hill climber self.create_groups()
def run(self): """starts algorithm""" for nets in self.grouped_nets: # save original route best_routes = [net.route for net in nets] # new best route if improvement found improvement = self.rewire(nets, self.board.cost) if improvement: best_routes = improvement for net in nets: # search for route matching to net for route in best_routes: if (route[0], route[-1]) == (net.connect[0].loc, net.connect[-1].loc): net.route = route self.board.add_net(net) self.board.cost = helpers.calc_cost(self.board)
def run(self): """starts algorithm""" # allowed deviation from manhattan current_deviation = DEVIATION no_solution = True # continue until solution found while no_solution: # determine routes for net in self.board.nets: # increase allowed deviation current_deviation += DEVIATION_INCREASE # starting data current_loc, goal = net.connect[0].loc, net.connect[1].loc start_distance = helpers.manhattan(self.board, current_loc, goal) net_length = 0 # coordinates of wire current_route = [current_loc] n_resets = 0 # continue until goal or limit is reached while current_loc != goal and n_resets < MAX_RESETS: # make move current_loc = self.move(MOVES.copy(), current_loc, current_route, goal, net_length, start_distance, current_deviation) # reset if unsuccesful if current_loc: current_route.append(current_loc) net_length += 1 else: n_resets += 1 current_loc = net.connect[0].loc current_route = [current_loc] net_length = 0 # start over if max resets reached if n_resets >= MAX_RESETS: self.board.reset_grid() print(f"got stuck at net: {net}, restarting...") break # store wire coordinates in net net.route = current_route # set net length net.length = net_length # add net to grid for x, y, z in current_route: self.board.grid[x][y][z].append(net.net_id) # check if solution found if n_resets != MAX_RESETS: self.board.cost = helpers.calc_cost(self.board) no_solution = False