def sim_an(map, costs, signals, iterations=200000, begin_temp=5, end_temp=0.01, heatalgorithm=linear_temperature): #plot = [] #map_list = [] freq = analyse.analyse_signal_frequentie(map) old_costs = analyse.get_cost(freq, costs) weight = analyse.get_weight(costs) #lowest_cost = old_costs #lowest_map = copy.deepcopy(map) for x in range(iterations): #map_list.append(map) temperature = heatalgorithm(begin_temp, end_temp, iterations, x) #plot.append(old_costs) swapped_state = swap_state(map, signals, weight) new_freq = analyse.analyse_signal_frequentie(map) new_costs = analyse.get_cost(new_freq, costs) improvement = old_costs - new_costs try: chance = math.exp(improvement / temperature) except OverflowError: chance = 0 if random.random() < chance: old_costs = new_costs else: revert_changes(swapped_state) #if old_costs < lowest_cost: # lowest_cost = old_costs # lowest_map = copy.deepcopy(map) #draw.line_plot(plot) return map
def random_walker(map, costs, signals, iterations=10000): plot = [] freq = analyse.analyse_signal_frequentie(map) old_costs = analyse.get_cost(freq, costs) for x in range(iterations): plot.append(old_costs) swapped_state = swap_state(map, signals) new_freq = analyse.analyse_signal_frequentie(map) old_costs = analyse.get_cost(new_freq, costs) #draw.line_plot(plot, y_min=2000, y_max=2600) return plot
def hill_climber(map, costs, signals, iterations=10000): #plot = [] freq = analyse.analyse_signal_frequentie(map) old_costs = analyse.get_cost(freq, costs) for x in range(iterations): swapped_state = swap_state(map, signals) new_freq = analyse.analyse_signal_frequentie(map) new_costs = analyse.get_cost(new_freq, costs) #plot.append(old_costs) if new_costs <= old_costs: old_costs = new_costs else: revert_changes(swapped_state) #draw.line_plot(plot) return map
signals = ['zA', 'zB', 'zC', 'zD', 'zE', 'zF', 'zG'] cost1 = {'zA': 12, 'zB': 26, 'zC': 27, 'zD': 30, 'zE': 37, 'zF': 39, 'zG': 41} cost2 = {'zA': 19, 'zB': 20, 'zC': 21, 'zD': 23, 'zE': 36, 'zF': 37, 'zG': 38} cost3 = {'zA': 16, 'zB': 17, 'zC': 31, 'zD': 33, 'zE': 36, 'zF': 56, 'zG': 57} cost4 = {'zA': 3, 'zB': 34, 'zC': 36, 'zD': 39, 'zE': 41, 'zF': 43, 'zG': 58} costlist = [cost1, cost2, cost3, cost4] begintemp = {12: 8, 19: 4, 16: 8, 3: 14} citer = 1 endtemp = [0.001, 0.01, 0.1, 0.25, 0.5, 0.75, 1, 1.5, 2] with open("endtempstats.txt", 'a') as text: for c in costlist: for e in endtemp: m = 0 print('end: ' + str(e)) text.write(str(citer) + ',') text.write(str(begintemp[c['zA']]) + ',') text.write(str(e) + ',') for y in range(20): map = readMap.read_complete_map('Russiadfs.txt') map = sim_an_search.sim_an(map, c, signals, 200000, begintemp[c['zA']], e) freq = analyse.analyse_signal_frequentie(map) costs = analyse.get_cost(freq, c) text.write(str(costs) + ', ') m += costs print('mean: ' + str(m / 20)) text.write('\n') citer += 1
def animate(i): ax = plt.gca() nodes = input_list[i] for nshape, seg in enumerate(my_map.states): for state in nodes: if state_names[nshape] == state.name: color = color_dict[state.signal] poly = Polygon(seg, facecolor=color) result = ax.add_patch(poly) break freq = analyse.signal_frequentie(nodes) spacing = {} for signal in color_dict: if signal not in freq: freq[signal] = 0 if freq[signal] > 9: spacing[signal] = ' €' else: spacing[signal] = ' €' map_costs = analyse.get_cost(freq, signal_costs) red_patch = mpatches.Patch(color='red', label='zA' + ' {' + str(freq['zA']) + '}' + spacing['zA'] + str(signal_costs['zA'])) blue_patch = mpatches.Patch(color='blue', label='zB' + ' {' + str(freq['zB']) + '}' + spacing['zB'] + str(signal_costs['zB'])) yellow_patch = mpatches.Patch(color='yellow', label='zC' + ' {' + str(freq['zC']) + '}' + spacing['zC'] + str(signal_costs['zC'])) green_patch = mpatches.Patch(color='green', label='zD' + ' {' + str(freq['zD']) + '}' + spacing['zD'] + str(signal_costs['zD'])) pink_patch = mpatches.Patch(color='pink', label='zE' + ' {' + str(freq['zE']) + '}' + spacing['zE'] + str(signal_costs['zE'])) cyan_patch = mpatches.Patch(color='cyan', label='zF' + ' {' + str(freq['zF']) + '}' + spacing['zF'] + str(signal_costs['zF'])) orange_patch = mpatches.Patch(color='orange', label='zG' + ' {' + str(freq['zG']) + '}' + spacing['zG'] + str(signal_costs['zG'])) plt.legend(handles=[ red_patch, blue_patch, yellow_patch, green_patch, pink_patch, cyan_patch, orange_patch ], loc=4, prop={'family': 'monospace'}) for txt in text_list: txt.set_visible(False) text = plt.text(0, 0, 'costs = ' + str(map_costs) + ' iterations = ' + str(i), horizontalalignment='left', verticalalignment='bottom', fontsize=12, transform=ax.transAxes, backgroundcolor="white", fontproperties=font1) text_list.append(text) return result,
import analyse import readMap import hill_solve for x in range(6, 13): signal_costs = analyse.get_cost_scheme(int(x)) signals = list(signal_costs.keys()) costs_list = [] for y in range(20): map = readMap.read_complete_map('Russiadfs.txt') hill_solve.hill_climber(map, signal_costs, signals, iterations=200000) freq = analyse.analyse_signal_frequentie(map) costs = analyse.get_cost(freq, signal_costs) costs_list.append(costs) print(costs) print('number = '+ str(x)) with open('costs_stats_hill.txt', 'a') as text: text.write(str(x)) text.write(',') for cost in costs_list: text.write(str(cost)) text.write(',') text.write('\n')
def draw_america(input, signal_costs): font1 = FontProperties() font1.set_family('monospace') font1.set_size(14) map = Basemap( resolution='i', # c, l, i, h, f or None projection='mill', llcrnrlon=-125.5, llcrnrlat=24, urcrnrlon=-66, urcrnrlat=50) map.drawmapboundary(fill_color='#46bcec') map.drawcountries(linewidth=1) map.fillcontinents(color='white') map.readshapefile('st99_d00', name='states', drawbounds=True) state_names = [] for shape_dict in map.states_info: state_names.append(shape_dict['NAME']) ax = plt.gca() # get current axes instance color_dict = { 'zA': 'red', 'zB': 'blue', 'zC': 'yellow', 'zD': 'green', 'zE': 'pink', 'zF': 'cyan', 'zG': 'orange' } for nshape, seg in enumerate(map.states): for state in input: if state_names[nshape] == state.name: color = color_dict[state.signal] poly = Polygon(seg, facecolor=color) ax.add_patch(poly) break freq = analyse.analyse_signal_frequentie(input) spacing = {} for signal in color_dict: if signal not in freq: freq[signal] = 0 if freq[signal] > 9: spacing[signal] = ' €' else: spacing[signal] = ' €' map_costs = analyse.get_cost(freq, signal_costs) print(map_costs) red_patch = mpatches.Patch(color='red', label='zA' + ' {' + str(freq['zA']) + '}' + spacing['zA'] + str(signal_costs['zA'])) blue_patch = mpatches.Patch(color='blue', label='zB' + ' {' + str(freq['zB']) + '}' + spacing['zB'] + str(signal_costs['zB'])) yellow_patch = mpatches.Patch(color='yellow', label='zC' + ' {' + str(freq['zC']) + '}' + spacing['zC'] + str(signal_costs['zC'])) green_patch = mpatches.Patch(color='green', label='zD' + ' {' + str(freq['zD']) + '}' + spacing['zD'] + str(signal_costs['zD'])) pink_patch = mpatches.Patch(color='pink', label='zE' + ' {' + str(freq['zE']) + '}' + spacing['zE'] + str(signal_costs['zE'])) cyan_patch = mpatches.Patch(color='cyan', label='zF' + ' {' + str(freq['zF']) + '}' + spacing['zF'] + str(signal_costs['zF'])) orange_patch = mpatches.Patch(color='orange', label='zG' + ' {' + str(freq['zG']) + '}' + spacing['zG'] + str(signal_costs['zG'])) plt.legend(handles=[ red_patch, blue_patch, yellow_patch, green_patch, pink_patch, cyan_patch, orange_patch ], loc=4, prop={'family': 'monospace'}) plt.text(0, 0, 'costs = ' + str(map_costs), horizontalalignment='left', verticalalignment='bottom', fontsize=12, transform=ax.transAxes, backgroundcolor="white", fontproperties=font1) plt.show()
signals = ['zA', 'zB', 'zC', 'zD', 'zE', 'zF', 'zG'] cost1 = {'zA':12, 'zB':26, 'zC':27, 'zD':30, 'zE':37, 'zF':39, 'zG':41} cost2 = {'zA':19, 'zB':20, 'zC':21, 'zD':23, 'zE':36, 'zF':37, 'zG':38} cost3 = {'zA':16, 'zB':17, 'zC':31, 'zD':33, 'zE':36, 'zF':56, 'zG':57} cost4 = {'zA':3, 'zB':34, 'zC':36, 'zD':39, 'zE':41, 'zF':43, 'zG':58} costlist = [cost1, cost2, cost3, cost4] begintemp = [9, 5, 9, 15] maplist = [readMap.read_complete_map('Ukrainedfs.txt'), readMap.read_complete_map('UnitedStatesdfs.txt'), readMap.read_complete_map('Russiadfs.txt')] with open('temptation.txt', 'a') as text: for m in range(3): for c in range(4): lowest_cost = 2000 for y in range(20): map = maplist[m] sim_an_search.sim_an(map, costlist[c], signals, begin_temp=begintemp[c], end_temp=0.5) freq = analyse.analyse_signal_frequentie(map) costs = analyse.get_cost(freq, costlist[c]) if costs < lowest_cost: lowest_cost = costs print(m, c + 1, y, lowest_cost) text.write(str(m) + str(c + 1) + ': ' + str(lowest_cost)) text.write('\n')
worst_costs = 0 #for l in range(loops): #map = readMap.readStates("UnitedStates.txt") #print(analyse.analyse_adjacent_states(map)) signals = ['zA', 'zB', 'zC', 'zD', 'zE', 'zF', 'zG'] costs = {'zA': 19, 'zB': 20, 'zC': 21, 'zD': 23, 'zE': 36, 'zF': 37, 'zG': 38} algos = { sim_an_search.sigmoidal_temperature: [], sim_an_search.sigmoidal_temperature_2: [], sim_an_search.sigmoidal_temperature_3: [], sim_an_search.double_sigmoidal_temperature: [], sim_an_search.sinus_double_sigmoidal_temperature: [], sim_an_search.sinus_temperature: [], sim_an_search.linear_temperature: [], sim_an_search.sinus_linear_temperature: [] } with open("sinsigmoid.txt", 'w') as text: text.write('sinus_sigmoidal_temperature') text.write(',') for x in range(loops): map = readMap.read_complete_map("UnitedStatesdfs.txt") sim_an_search.stats_climber(map, costs, signals, 200000, 5, 0.01, sim_an_search.sinus_sigmoidal_temperature) freq = analyse.signal_frequentie(map) hill_costs = analyse.get_cost(freq, costs) text.write(str(hill_costs)) text.write(',') print(str(x)) text.write('\n')