Esempio n. 1
0
def show_edge_weights(self, raw_input: str):  # show0 weights1 on/off2
    list = raw_input.split(' ')
    if len(list) == 3:
        if list[2] == 'on':
            s.show_weight_labels = True
        else:
            s.show_weight_labels = False
    else:
        s.cust_print(self,
                     ('Improper command caught in add node: ' + raw_input))
Esempio n. 2
0
def swap_colors(self, raw_input: str):  # swap0 colors1 colorone2 colortwo3
    list = raw_input.split(' ')
    if len(list) == 4:
        for g in s.graph.nodes:
            if s.graph.nodes[g]['color'] == str(list[2]):
                s.graph.nodes[g]['color'] = str(list[3])
        s.successfulCommand = True
    else:
        s.cust_print(self,
                     ('Improper command caught in swap colors: ' + raw_input))
Esempio n. 3
0
def choose_import(self, raw_input: str):
    if '.json' in raw_input:
        dm.import_json(self, raw_input)
    elif '.gexf' in raw_input:
        dm.import_gexf(self, raw_input)
    elif '.png' in raw_input:
        dm.import_png(self, raw_input)
    else:
        s.cust_print(self, (
            'What is the file format from command? I accept .png, .gexf and .json: '
            + str(raw_input)))
Esempio n. 4
0
def set_node_pos(self, raw_input: str):
    list = raw_input.split(' ')
    if len(list) == 5:
        try:
            s.graph.nodes[list[2]]['pos'] = str(list[3]) + ';' + str(list[4])
            s.successfulCommand = True
        except Exception as e:
            s.cust_print(self,
                         ('Something went wrong while setting node position'))
    else:
        s.cust_print(self,
                     ('Improper command caught in set node pos: ' + raw_input))
Esempio n. 5
0
def define_draw_style(self, raw_input):
    list = raw_input.split(' ')
    if len(list) == 2:
        if list[1] == 'none':
            s.cust_print(self, (
                'Selected none drawstyle. Make sure to set up all node positions!'
            ))
        s.draw_style = list[1]
        s.successfulCommand = True
    else:
        s.cust_print(self,
                     ('Improper command caught in drawstyle: ' + raw_input))
Esempio n. 6
0
def save_gexf(self, raw_input):
    list = raw_input.split(' ')
    if len(list) == 3:
        s.filename = list[2]
        s.successfulCommand = True
        now = s.datetime.datetime.now()
        file_name = s.filename + now.strftime("-%m-%d-%Y--%H-%M-%S") + '.gexf'
        file_plus_path = str(s.getcwd()) + '\\' + file_name
        s.nx.write_gexf(s.graph, file_plus_path)
        s.cust_print(self, (str(file_plus_path) + ' was saved.'))
    else:
        s.cust_print(self,
                     ('Improper command caught in save gexf: ' + raw_input))
Esempio n. 7
0
def fullscreen(self, raw_input: str):
    #    remove edge x1 x2
    list = raw_input.split(' ')
    if len(list) == 2:
        if 'on' == list[1]:
            s.fullscreen = True
        elif 'off' == list[1]:
            s.fullscreen = False
        else:
            s.cust_print(
                self, ('Improper command caught in fullscreen: ' + raw_input))
    else:
        s.cust_print(self,
                     ('Improper command caught in fullscreen: ' + raw_input))
Esempio n. 8
0
def change_node_color(self,
                      raw_input):  # change0 node1 color2 [nodename]3 [color]4
    list = raw_input.split(' ')
    if len(list) == 5:
        try:
            s.graph.nodes[list[3]]['color'] = list[4]
            s.successfulCommand = True
        except Exception as e:
            s.cust_print(self, (
                'Something went wrong while assigning node color, please make sure such node or color exists'
            ))
    else:
        s.cust_print(
            self,
            ('Improper command caught in change node color: ' + raw_input))
Esempio n. 9
0
def save_json(self, raw_input):
    list = raw_input.split(' ')
    if len(list) == 3:
        s.filename = list[2]
        s.successfulCommand = True
        now = s.datetime.datetime.now()
        file_name = s.filename + now.strftime("-%m-%d-%Y--%H-%M-%S") + '.json'
        file_plus_path = str(s.getcwd()) + '\\' + file_name
        f = open(file_plus_path, 'a')
        f.write(s.json.dumps(s.json_graph.node_link_data(s.graph)))
        f.close()
        s.cust_print(self, (str(file_plus_path) + ' was saved.'))
    else:
        s.cust_print(self,
                     ('Improper command caught in save json: ' + raw_input))
Esempio n. 10
0
def remove_node(self, raw_input: str):
    #    remove node x1
    list = raw_input.split(' ')
    if len(list) == 3:
        try:
            s.graph.remove_node(list[2])
            s.successfulCommand = True
        except Exception as e:
            s.cust_print(
                self,
                ('something went wrong when removing node, make sure it exists'
                 ))
    else:
        s.cust_print(self,
                     ('Improper command caught in remove node: ' + raw_input))
Esempio n. 11
0
def set_edge_weight(self, raw_input: str):  # set0 edge1 weight2 u3 v4 weight5
    list = raw_input.split(
        ' '
    )  # edge = ('n0', 'n1')  [round(float(loc_arr[0]), 4), round(float(loc_arr[1]), 4)]
    if len(list) == 6:
        try:
            s.graph[str(list[3])][str(list[4])]['weight'] = round(
                float(list[5]), 4)
            s.successfulCommand = True
        except Exception as e:
            s.cust_print(self,
                         ('Something went wrong while setting edge weight'))
    else:
        s.cust_print(self,
                     ('Improper command caught in set weight: ' + raw_input))
Esempio n. 12
0
def get_dist_between_two_points(self, x1, y1, x2, y2):
    pos1x = float(str(x1))
    pos1y = float(str(y1))
    pos2x = float(str(x2))
    pos2y = float(str(y2))
    x = pos1x - pos2x
    y = pos1y - pos2y
    dist = round(s.math.sqrt(x * x + y * y), 4)
    metrics = float(float(s.prop_m) * float(dist) / float(s.prop_d))
    if s.distance_out_loud:
        s.cust_print(self,
                     ('Distance between 2 points: ' + str(dist) + '; or ' +
                      str(metrics) + ' ' + str(s.prop_m_suffix)))
        s.distance_out_loud = False
    return dist
Esempio n. 13
0
def add_node(self, raw_input: str):
    list = raw_input.split(' ')
    if len(list) == 4:
        s.graph.add_node(list[2],
                         weight=list[3],
                         color=s.default_node_color,
                         name=list[2],
                         pos='None')
        s.successfulCommand = True
    elif len(list) == 3:
        s.graph.add_node(list[2],
                         color=s.default_node_color,
                         name=list[2],
                         pos='None')
        s.successfulCommand = True
    else:
        s.cust_print(self,
                     ('Improper command caught in add node: ' + raw_input))
Esempio n. 14
0
def get_node_dist(self, raw_input: str):
    list = raw_input.split(' ')  # get0 node1 dist3 u4 v5
    if len(list) == 5:
        try:
            pos1 = s.graph.nodes[str(list[3])]['pos']
            pos1x = float(str(pos1).split(';')[0])
            pos1y = float(str(pos1).split(';')[1])
            pos2 = s.graph.nodes[str(list[4])]['pos']
            pos2x = float(str(pos2).split(';')[0])
            pos2y = float(str(pos2).split(';')[1])
            s.distance_out_loud = True
            get_dist_between_two_points(self, pos1x, pos1y, pos2x, pos2y)
        except Exception as e:
            s.cust_print(self,
                         ('Something went wrong while getting node positions'))
    else:
        s.cust_print(
            self, ('Improper command caught in get node dist: ' + raw_input))
Esempio n. 15
0
def add_b_edge(self, raw_input: str):
    list = raw_input.split(' ')
    if '.DiGraph' in str(type(s.graph)):
        if len(list) == 7:
            # first, add nodes with corresponding parameters
            add_node('add node ' + list[3])
            add_node('add node ' + list[4])
            s.graph.add_edge(list[3], list[4], weight=list[5])
            s.graph.add_edge(list[4], list[3], weight=list[6])
            s.successfulCommand = True
        elif len(list) == 6:
            # first, add nodes with corresponding parameters
            add_node('add node ' + list[3])
            add_node('add node ' + list[4])
            s.graph.add_edge(list[3], list[4], weight=list[5])
            s.graph.add_edge(list[4], list[3], weight=list[5])
            s.successfulCommand = True
        elif len(list) == 5:
            # first, add nodes with corresponding parameters
            add_node('add node ' + list[3])
            add_node('add node ' + list[4])
            s.graph.add_edge(list[3], list[4], weight=0)
            s.graph.add_edge(list[4], list[3], weight=0)
        else:
            s.cust_print(
                self, ('Improper command caught in add bidirectional edge: ' +
                       raw_input))
    if '.Graph' in str(type(s.graph)):
        if len(list) == 6:
            # first, add nodes with corresponding parameters
            add_node('add node ' + list[3])
            add_node('add node ' + list[4])
            s.graph.add_edge(list[3], list[4], weight=list[5])
            s.successfulCommand = True
        elif len(list) == 5:
            # first, add nodes with corresponding parameters
            add_node('add node ' + list[3])
            add_node('add node ' + list[4])
            s.graph.add_edge(list[3], list[4], weight=0)
            s.successfulCommand = True
        else:
            s.cust_print(
                self, ('Improper command caught in add bidirectional edge: ' +
                       raw_input))
Esempio n. 16
0
def set_weight_proportion(
        self, raw_input: str):  # set0 weight1 proportion2 u3 v4 xxxKm5
    list = raw_input.split(' ')  # edge = ('n0', 'n1')
    if len(list) == 6:
        try:
            # w = s.graph[str(list[3])][str(list[4])]['weight']
            pos1 = s.graph.nodes[str(list[3])]['pos']
            pos1x = float(str(pos1).split(';')[0])
            pos1y = float(str(pos1).split(';')[1])
            pos2 = s.graph.nodes[str(list[4])]['pos']
            pos2x = float(str(pos2).split(';')[0])
            pos2y = float(str(pos2).split(';')[1])
            dist = get_dist_between_two_points(self, pos1x, pos1y, pos2x,
                                               pos2y)
            s.prop_d = float(dist)
            # s.prop_m = ''.join(re.findall(r"[-+]?\d*\.\d+|\d+", str(list[5])))
            if str(list[5])[-2].isdigit():
                s.prop_m = str(list[5][:-1])
                s.prop_m_suffix = str(list[5][-1])
            else:
                s.prop_m = str(list[5][:-2])
                s.prop_m_suffix = str(list[5][-2]) + str(list[5][-1])
            s.cust_print(self,
                         ('new distance proportion: ' + str(s.prop_d) + ' = ' +
                          str(s.prop_m) + ' ' + str(s.prop_m_suffix)))
        except Exception as e:
            s.cust_print(
                self, ('Something went wrong while setting weight proportion'))
    else:
        s.cust_print(
            self,
            ('Improper command caught in set weight proportion: ' + raw_input))
Esempio n. 17
0
def generate_custom_pos(self):
    emptyDict = {}
    # s.cust_print(self, 'custom pos generation started')
    for n in s.graph.nodes:
        try:
            if s.graph.nodes[n][
                    'pos'] == 'None' or s.graph.nodes[n]['pos'] is None:
                s.cust_print(
                    self, (str(n) + ' _node has no pos, set it up properly'))
                s.cust_print(
                    self,
                    (str(n) + ' has invalid position, using default layout'))
                s.draw_style = 'default'
                emptyDict = s.nx.planar_layout(s.graph)
                return emptyDict
            else:
                loc_arr = str(s.graph.nodes[n]['pos']).split(';')
                emptyDict[n] = [
                    round(float(loc_arr[0]), 4),
                    round(float(loc_arr[1]), 4)
                ]
        except Exception as e:
            s.cust_print(
                self,
                (str(n) +
                 ' has invalid position, using default layout(exception)'))
            s.draw_style = 'default'
            emptyDict = s.nx.planar_layout(s.graph)
            return emptyDict
    # s.cust_print(self, 'result of custom generation:')
    # s.cust_print(self, emptyDict)
    return emptyDict
Esempio n. 18
0
def dum_dum_shmoys_cycled(self, wh_max_number, init_radius, f_node, cycles):
    global local_results
    for i in range(int(cycles)):
        dum_dum_shmoys_cycled_small(self, wh_max_number, init_radius, f_node)
    local_best_rad = 99999999999999999999999999999999999
    for result in local_results:
        if local_best_rad > float(result['rad']):
            local_best_rad = float(result['rad'])
    for nd in s.graph.nodes:
        s.graph.nodes[nd]['color'] = s.default_node_color
    for el in local_results:
        if float(el['rad']) <= float(local_best_rad):
            s.cust_print(self,
                         ('cycled shmoys: With radius of ' + str(el['rad'])))
            s.cust_print(
                self,
                ('cycled shmoys: These nodes will work: ' + str(el['wh'])))
            for e in el['wh']:
                try:
                    s.graph.nodes[e]['color'] = s.default_wh_color
                except Exception as e:
                    s.cust_print(self, (
                        'cycled shmoys: something went wrong while changing wh color'
                    ))
            break
    dm.draw_graph(self)
    local_results.clear()
Esempio n. 19
0
def dum_dum_floyd_alg(self):
    distance_matrix = s.nx.floyd_warshall_numpy(s.graph, s.graph.nodes)
    ind = 0
    sums = ({})
    for nd in s.graph.nodes:
        sums[nd] = str(distance_matrix[ind])
        ind += 1
    newlist = []
    for i in sums.values():
        newlist.append(str(i).strip(" "))
    two_d_array = []

    for i in newlist:
        x = i.strip().split(" ")
        one_d_arr = []
        for xi in x:
            xi = re.findall(r"[-+]?\d*\.\d+|\d+", str(xi))
            if len(xi) > 0:
                one_d_arr.append(float(xi[0]))
        two_d_array.append(one_d_arr)
    overall_sums = ({})
    index2 = 0
    for nd in s.graph.nodes:
        overall_sums[str(nd)] = max(two_d_array[index2])
        index2 += 1
    s.cust_print(self, ('and the winner is:'))
    minimal_sum = min(overall_sums.values())
    min_sum_name = ''
    for name, summ in overall_sums.items():
        if summ == minimal_sum:
            min_sum_name = name
    s.cust_print(
        self, (min_sum_name + ' with a longest dist of ' + str(minimal_sum)))
    for nd in s.graph.nodes:
        s.graph.nodes[nd]['color'] = s.default_node_color
    s.graph.nodes[min_sum_name]['color'] = s.default_wh_color
    dm.draw_graph(self)
    s.successfulCommand = True
Esempio n. 20
0
def draw_graph(self):
    self.figure.clf()
    colors = [
        s.graph.nodes[a].get('color', s.default_node_color)
        for a in s.graph.nodes
    ]
    if s.draw_style == 'default' or s.draw_style == 'planar':
        pos = s.nx.planar_layout(s.graph)
    elif s.draw_style == 'shell':
        pos = s.nx.shell_layout(s.graph)
    elif s.draw_style == 'spring':
        pos = s.nx.spring_layout(s.graph)
    elif s.draw_style == 'spectral':
        pos = s.nx.spectral_layout(s.graph)
    elif s.draw_style == 'none':
        pos = generate_custom_pos(self)
    elif s.draw_style == 'random':
        pos = s.nx.random_layout(s.graph)
    elif s.draw_style == 'circular':
        pos = s.nx.circular_layout(s.graph)
    else:
        s.cust_print(
            self, ('Hard switch to planar. What is this draw style? ' +
                   s.draw_style + '; Please define a valid one (see help).'))
        pos = s.nx.planar_layout(s.graph)

    if s.background_is_image:
        implot = s.plt.imshow(s.background_img)

    s.nx.draw(s.graph, pos, node_color=colors)
    labels = s.nx.get_edge_attributes(s.graph, 'weight')
    nd_labels = s.nx.get_node_attributes(s.graph, 'name')
    if s.show_weight_labels:
        s.nx.draw_networkx_edge_labels(s.graph, pos, edge_labels=labels)
    s.nx.draw_networkx_labels(s.graph, pos, labels=nd_labels)

    self.canvas.draw_idle()
Esempio n. 21
0
def on_click(self, event):
    if event.dblclick:
        pass
    if event.button:
        if event.button == 3:
            round(float(event.xdata), 4)
            add_posed_node(
                self, 'add pnode ' + s.generic_node_name +
                str(s.generic_node_name_counter) + ' ' +
                str(round(float(event.xdata), 4)) + ' ' +
                str(round(float(event.ydata), 4)))
            s.generic_node_name_counter += 1
            draw_graph(self)
        if event.button == 2:
            try:
                for n in s.graph.nodes:
                    npos = s.graph.nodes[n]['pos']
                    nx = float(str(npos).split(';')[0])
                    ny = float(str(npos).split(';')[1])
                    if get_dist_between_two_points(
                            self, round(float(event.xdata), 4),
                            round(float(event.ydata), 4), nx, ny) <= 15:
                        s.mouse_clicked_nodes.append(n)
                        break
                if len(s.mouse_clicked_nodes) >= 2:
                    n1x = str(s.graph.nodes[str(
                        s.mouse_clicked_nodes[0])]['pos']).split(';')[0]
                    n1y = str(s.graph.nodes[str(
                        s.mouse_clicked_nodes[0])]['pos']).split(';')[1]
                    n2x = str(s.graph.nodes[str(
                        s.mouse_clicked_nodes[1])]['pos']).split(';')[0]
                    n2y = str(s.graph.nodes[str(
                        s.mouse_clicked_nodes[1])]['pos']).split(';')[1]
                    dist = get_dist_between_two_points(self, n1x, n1y, n2x,
                                                       n2y)
                    weight = float(
                        (float(s.prop_m) * float(dist)) / float(s.prop_d))
                    add_edge(
                        self,
                        'add edge ' + str(s.mouse_clicked_nodes[0]) + ' ' +
                        str(s.mouse_clicked_nodes[1]) + ' ' + str(weight))
                    s.mouse_clicked_nodes.clear()
                    draw_graph(self)
            except Exception as e:
                s.cust_print(
                    self,
                    ('something went wrong while creating edge with mouse'))
                s.cust_print(self, (e))
        if event.button == 1:
            if event.xdata:
                s.cust_print(
                    self, ('mouse pos: ' + str(round(float(event.xdata), 4)) +
                           ' ' + str(round(float(event.xdata), 4))))
Esempio n. 22
0
def remove_edge(self, raw_input: str):  # remove0 edge1 u2 v3
    #    remove node x1
    list = raw_input.split(' ')
    if len(list) == 4:
        try:
            s.graph.remove_edge(str(list[2]), str(list[3]))
            s.successfulCommand = True
        except Exception as e:
            s.cust_print(self, (e))
            s.cust_print(self, ('Failed to remove edge'))
    else:
        s.cust_print(self,
                     ('Improper command caught in remove edge: ' + raw_input))
Esempio n. 23
0
def import_png(self, raw_input):
    list = raw_input.split(' ')
    if len(list) == 2:
        try:
            s.background_img = s.plt.imread(raw_input.split(' ')[1])
            s.successfulCommand = True
            s.background_is_image = True
        except Exception as e:
            s.cust_print(self, ('something went wrong while importing file ' +
                                str(raw_input.split(' ')[1])))
            s.cust_print(self, (e))
    else:
        s.cust_print(self,
                     ('Improper command caught in import png: ' + raw_input))
Esempio n. 24
0
def convert(self, raw_input: str):  # convert0 dist/metr1 500_2
    list = raw_input.split(' ')
    # (prop_m * edge dist) / prop_d
    if len(list) == 3:
        if 'dist' in raw_input:
            metrics = float(float(s.prop_m) * float(list[2]) / float(s.prop_d))
            s.cust_print(self, ('it corresponds to ' + str(metrics) + ' ' +
                                str(s.prop_m_suffix)))
        else:
            dist = float(float(s.prop_d) * float(list[2]) / float(s.prop_m))
            s.cust_print(self, ('it corresponds to ' + str(dist)))
    else:
        s.cust_print(
            self,
            ('Improper command caught in converting values: ' + raw_input))
Esempio n. 25
0
def import_gexf(self, raw_input):
    list = raw_input.split(' ')
    if len(list) == 2:
        filename = list[1]
        try:
            s.graph = s.nx.read_gexf(filename)
            s.generic_node_name_counter = find_next_generica_node_name_counter(
            )
            s.successfulCommand = True
        except Exception as e:
            s.cust_print(
                self,
                ('something went wrong while importing file ' + str(filename)))
            s.cust_print(self, (e))
    else:
        s.cust_print(self,
                     ('Improper command caught in import gexf: ' + raw_input))
Esempio n. 26
0
def import_json(self, raw_input):
    list = raw_input.split(' ')
    if len(list) == 2:
        filename = list[1]
        try:
            f = open(filename)
            s.graph = s.json_graph.node_link_graph(s.json.load(f))
            f.close()
            s.generic_node_name_counter = find_next_generica_node_name_counter(
            )
            s.successfulCommand = True
        except Exception as e:
            s.cust_print(
                self,
                ('something went wrong while importing file ' + str(filename)))
            s.cust_print(self, e)
    else:
        s.cust_print(self,
                     ('Improper command caught in import json: ' + raw_input))
Esempio n. 27
0
def add_edge(self, raw_input: str):
    list = raw_input.split(' ')
    if len(list) == 5:
        if list[2] in s.graph.nodes and list[3] in s.graph.nodes:
            s.graph.add_edge(list[2], list[3], weight=round(float(list[4]),
                                                            4))  # list[4]
            s.successfulCommand = True
        else:
            s.cust_print(self, (
                'abort adding edge, one of nodes is missing. (hint: command print nodes)'
            ))
    elif len(list) == 4:
        if list[2] in s.graph.nodes and list[3] in s.graph.nodes:
            s.graph.add_edge(list[2], list[3], weight=0)
            s.successfulCommand = True
        else:
            s.cust_print(self, (
                'abort adding edge, one of nodes is missing. (hint: command print nodes)'
            ))
    else:
        s.cust_print(self,
                     ('Improper command caught in add edge: ' + raw_input))
Esempio n. 28
0
def dum_dum_shmoys_cycled_small(self, wh_max_number, init_radius, f_node):
    global last_working_result
    global last_working_radius
    global local_results

    warehouse_current_number = 0
    warehouse_max_number = wh_max_number
    all_nodes = list(s.graph.nodes)
    all_warehouses = []
    first_node = None
    amount_of_cycles = int(warehouse_max_number)
    if f_node is not None and f_node != 'None':
        if '(' not in f_node or ')' not in f_node:
            s.cust_print(self, (
                'cycled shmoys: nodes should be passed inside brackets without spaces, separated by \',\''
            ))
            return
        raw_given_nodes = str(f_node).split('(')[1].split(')')[0].split(',')
        given_nodes = [n for n in raw_given_nodes if n in s.graph.nodes]
        given_nodes = list(set(given_nodes))
        if int(wh_max_number) < len(given_nodes):
            s.cust_print(self, (
                'cycled shmoys: there are more given nodes than max number of warehouses, aborting'
            ))
            return
        amount_of_cycles = amount_of_cycles - len(given_nodes)
        for n in given_nodes:
            all_warehouses.append(n)
            warehouse_current_number += 1
            nodes_in_radius.append(first_node)
            recursive_search(n, 0, init_radius)
            for n in nodes_in_radius:
                try:
                    all_nodes.remove(n)
                except ValueError:
                    pass  # do nothing!
            nodes_in_radius.clear()
            if len(all_nodes) <= 0:
                break

    for k in range(amount_of_cycles):
        first_node = random.choice(all_nodes)
        all_warehouses.append(first_node)
        warehouse_current_number += 1
        nodes_in_radius.append(first_node)
        recursive_search(first_node, 0, init_radius)
        for n in nodes_in_radius:
            try:
                all_nodes.remove(n)
            except ValueError:
                pass
        nodes_in_radius.clear()
        if len(all_nodes) <= 0:
            break

    if len(all_nodes) > 0:
        new_radius = int(init_radius) + int(int(init_radius)) / 4
        if new_radius < last_working_radius:
            dum_dum_shmoys_cycled_small(self, wh_max_number, new_radius,
                                        f_node)
        else:
            temp_dict = {
                'rad': last_working_radius,
                'wh': list(last_working_result)
            }
            local_results.append(temp_dict)
            last_working_radius = 99999999999999999999999999999999999
            last_working_result.clear()
            s.successfulCommand = True
    else:
        last_working_result = all_warehouses
        last_working_radius = int(init_radius)
        new_radius = int(init_radius) - int(int(init_radius) / 32)
        dum_dum_shmoys_cycled_small(self, wh_max_number, new_radius, f_node)
Esempio n. 29
0
def dum_dum_shmoys(self, wh_max_number, init_radius, f_node):
    # from now on f_node will be a list of nodes or None. list ex.: (1,2,3)
    global last_working_result
    global last_working_radius
    warehouse_current_number = 0
    warehouse_max_number = wh_max_number
    all_nodes = list(s.graph.nodes)
    all_warehouses = []
    first_node = None
    amount_of_cycles = int(warehouse_max_number)
    if f_node is not None and f_node != 'None':
        if '(' not in f_node or ')' not in f_node:
            s.cust_print(self, (
                'dum_dum_shmoys: nodes should be passed inside brackets without spaces, separated by \',\''
            ))
            return
        raw_given_nodes = str(f_node).split('(')[1].split(')')[0].split(',')
        given_nodes = [n for n in raw_given_nodes if n in s.graph.nodes]
        given_nodes = list(set(given_nodes))
        if int(wh_max_number) < len(given_nodes):
            s.cust_print(self, (
                'dum_dum_shmoys: there are more given nodes than max number of warehouses, aborting'
            ))
            return
        amount_of_cycles = amount_of_cycles - len(given_nodes)
        for n in given_nodes:
            all_warehouses.append(n)
            warehouse_current_number += 1
            nodes_in_radius.append(first_node)
            recursive_search(n, 0, init_radius)
            for n in nodes_in_radius:
                try:
                    all_nodes.remove(n)
                except ValueError:
                    pass  # do nothing!
            nodes_in_radius.clear()
            if len(all_nodes) <= 0:
                break

    for k in range(amount_of_cycles):
        first_node = random.choice(all_nodes)
        all_warehouses.append(first_node)
        warehouse_current_number += 1
        nodes_in_radius.append(first_node)
        recursive_search(first_node, 0, init_radius)
        for n in nodes_in_radius:
            try:
                all_nodes.remove(n)
            except ValueError:
                pass
        nodes_in_radius.clear()
        if len(all_nodes) <= 0:
            break

    if len(all_nodes) > 0:
        s.cust_print(self, (
            'dum_dum_shmoys: there are still nodes that are not covered by warehouses, incrementing radius ('
            + str(init_radius) + ') by quarter and starting again'))
        new_radius = int(init_radius) + int(int(init_radius)) / 4
        if new_radius < last_working_radius:
            dum_dum_shmoys(self, wh_max_number, new_radius, f_node)
        else:
            s.cust_print(self, (
                'dum_dum_shmoys: extended function execution failed, last successful radius = '
                + str(last_working_radius)))
            s.cust_print(
                self, ('dum_dum_shmoys: last successful placed warehouses: ' +
                       str(last_working_result)))
            for nd in s.graph.nodes:
                s.graph.nodes[nd]['color'] = s.default_node_color
            for nd in last_working_result:
                s.graph.nodes[nd]['color'] = s.default_wh_color
            last_working_radius = 99999999999999999999999999999999999
            last_working_result.clear()
            s.successfulCommand = True
    else:
        s.cust_print(
            self,
            ('dum_dum_shmoys: function execution completed with radius = ' +
             str(init_radius)))
        s.cust_print(
            self,
            ('dum_dum_shmoys: placed warehouses: ' + str(all_warehouses)))
        last_working_result = all_warehouses
        last_working_radius = int(init_radius)
        new_radius = int(init_radius) - int(int(init_radius) / 32)
        s.cust_print(
            self, ('dum_dum_shmoys: starting recursion with lesser radius = ' +
                   str(new_radius)))
        dum_dum_shmoys(self, wh_max_number, new_radius, f_node)
Esempio n. 30
0
def print_list(self, raw_input):
    if 'edges' in raw_input:
        s.cust_print(self, (s.graph.edges))
        for e in s.graph.edges:
            s.cust_print(self,
                         (str(e) + ' : ' + str(s.graph.edges[e]['weight'])))
        #
        # labels = s.nx.get_edge_attributes(s.graph, 'weight')
        # s.cust_print(self, labels)
        # colors = [s.graph.nodes[a].get('weight', 0) for a in s.graph.nodes]
        # s.cust_print(self, colors)
    elif 'nodes' in raw_input:
        s.cust_print(self, (s.graph.nodes))
    elif 'style' in raw_input:
        s.cust_print(self, ('current drawstyle is: ' + str(s.draw_style)))
    elif 'one' in raw_input:
        try:
            s.cust_print(self, (s.graph.nodes[str(raw_input).split(' ')[-1]]))
        except Exception as e:
            s.cust_print(
                self, ('Failed to print info about node: ' + str(raw_input)))
    else:
        s.cust_print(self, (s.graph.nodes.keys()))
        s.cust_print(self, (s.graph.nodes.values()))
        s.cust_print(self,
                     ('What is this option?(caught in print): ' + raw_input))