Example #1
0
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
Example #2
0
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
Example #3
0
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
Example #4
0
def dfs(map, signals):
    amount_dic = analyse.analyse_signal_frequentie(map)
    state = find_next(map)
    if state == 0:
        return True
    possible_signal = possible_signals(signals, state)
    possible_signal = reorder_possible_signals(possible_signal, amount_dic)
    for signal in possible_signal:
        state.signal = signal
        checker = dfs(map, signals)
        if checker:
            return True
    state.signal = 0
    return False
Example #5
0
def draw_america_2(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)

    plt.show()
Example #6
0
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
Example #7
0
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()