def greed(G, iterations, cost_schedule, land, ws, greed_i): colormap = [] score_array = [] # vraag de kleur op van een specifieke node color = nx.get_node_attributes(G, 'color') for node in G.nodes(): # voeg de kleur toe in de array colormap.append(color[node]) # bereken de score tScore = scoreCounter(G, cost_schedule) loopA = iterations for i in range(loopA): G = greedy(G, colormap, cost_schedule, tScore) tScore, Temp = scoreCounter(G, cost_schedule) score_array.append(tScore) print("score ", tScore) excel_writer_greedy(score_array, land, greed_i, ws) return G, tScore
def hillclimber(G, iterations, cost_schedule, land, hill_i): print(hill_i) best_G = copy.deepcopy(G) alle_scores = [] for iter in range(iterations): # EXPONENTIAL T = iterations * (0.995 ** (iter * 20)) # store the old state old_G = copy.deepcopy(G) old_S, colormap = scoreCounter(old_G, cost_schedule) # randomly swap a node's color G = randomSwap(G) # get the new score new_S, colormap = scoreCounter(G, cost_schedule) # keep the new state if its an improvement if new_S < old_S: alle_scores.append(new_S) if new_S < beste_scores[0]: beste_scores.append(new_S) beste_scores.sort() best_G = copy.deepcopy(G) # print(new_S) old_S = new_S old_G = G elif sAnneal(T, old_S, new_S) == 1: alle_scores.append(new_S) # print("ANEEALING DONEEEE") old_S = new_S old_G = G # go back to the previous state since it's worse else: G = old_G # excel_writer(alle_scores, land, ws, hill_i, iterations) # print(beste_scores) return best_G, old_S
def main(): # maak een random indeling land = input("welk land wil je plotten? (RUSSIA/UKRAINE/USA) ") nodescsv = land + "/nodes.csv" edgescsv = land + "/edges.csv" G = dataLoader(nodescsv, edgescsv) G = rand(G) cost_table = int(input("welke kosten tabel? 1 t/m 4 ")) - 1 # bereken de score total_costs, colormap = scoreCounter(G, cost_table) destination = land + "/hiScore.xls" algo = input('welke algoritme? (random/greedy/hillclimber) ') if algo == 'random': # show de random indeling dataWriter(G, destination, cost_table, total_costs, algo) title = land + " Score " + str(total_costs) nx.draw_networkx(G, with_labels=True, node_color=colormap) plt.title(title) plt.show() if algo == 'greedy': greed_nr = int(input("how many times do you want to run greedy? ")) iter = int(input("how many iterations? ")) wb = xlwt.Workbook() ws = wb.add_sheet("Scores", cell_overwrite_ok=True) for greed_i in range(greed_nr): G, score = greed(G, iter, cost_table, land, ws, greed_i) total_costs, colormap = scoreCounter(G, cost_table) dataWriter(G, destination, cost_table, total_costs, algo) G = rand(G) destin = land + "/greedy_scores.xls" wb.save(destin) if algo == 'hillclimber': hill_nr = int( input("how many times do you want to run the hillclimber? ")) iter = int(input("how many iterations? ")) wb = xlwt.Workbook() ws = wb.add_sheet("Scores", cell_overwrite_ok=True) for hill_i in range(hill_nr): G, score = hillclimber(G, iter, cost_table, land, ws, hill_i) total_costs, colormap = scoreCounter(G, cost_table) dataWriter(G, destination, cost_table, total_costs, algo) G = rand(G) destin = land + "/hill_climber_scores.xls" wb.save(destin)
def hillOld(G, iterations, cost_schedule): colormap = [] # vraag de kleur op van een specifieke node color = nx.get_node_attributes(G, 'color') for node in G.nodes(): # voeg de kleur toe in de array colormap.append(color[node]) # bereken de score tScore = scoreCounter(G, cost_schedule) print(tScore, " score") loopA = iterations for i in range(loopA): G = hillclimber(G, colormap, cost_schedule, tScore) tScore = scoreCounter(G, cost_schedule) print(tScore, " scoreF1") return G, tScore
def main(): # maak een random indeling land = "UKRAINE" nodescsv = land + "/nodes.csv" edgescsv = land + "/edges.csv" G = dataLoader(nodescsv, edgescsv) G = rand(G) cost_table = 2 # bereken de score total_costs, colormap = scoreCounter(G, cost_table) destination = land + "/hiScore.xls" algo = "greedy" if algo == 'random': # show de random indeling dataWriter(G, destination, cost_table, total_costs, algo) nx.draw_networkx(G, with_labels=True, node_color=colormap) plt.show() if algo == 'greedy': iter = 5 # # draai hillclimber x aantal keer G, score = greed(G, iter, cost_table) total_costs, colormap = scoreCounter(G, cost_table) dataWriter(G, destination, cost_table, total_costs, algo) nx.draw_networkx(G, with_labels=True, node_color=colormap) plt.show() if algo == 'hillclimber': iter = int(input("how many iterations? ")) G, score = hillclimber(G, iter, cost_table) total_costs, colormap = scoreCounter(G, cost_table) dataWriter(G, destination, cost_table, total_costs, algo) nx.draw_networkx(G, with_labels=True, node_color=colormap) plt.show()
from scorecalculator import scoreCounter G = nx.Graph() land = input("welk land wil je plotten? (RUSSIA/UKRAINE/USA) ") destination = land + "/hiScore.xls" edgescsv = land + "/edges.csv" book = xlrd.open_workbook(destination) sh = book.sheet_by_index(0) n = 0 for rows in sh.col(0): G.add_node(str(rows.value), color=sh.col(1)[n].value) n += 1 G.remove_node(str(sh.col(0)[0].value)) cost_table = int(sh.col(0)[0].value - 1) # lijnen tussen staten with open(edgescsv, 'r') as csvfile: plots = csv.reader(csvfile, delimiter=',') for row in plots: G.add_edge(str(row[0]), str(row[1])) # lees de oude score uit file total_costs, colormap = scoreCounter(G, cost_table) title = land + " Score " + str(total_costs) nx.draw_networkx(G, with_labels=True, node_color=colormap) plt.title(title) plt.show()