Programming දන්න අය

Dragon Kolla

Well-known member
  • Oct 26, 2015
    9,330
    1,597
    113
    මකර ලන්තෙ
    aluthin seen ekata enter une bois
    google collab eken thamai meka run krnne

    mkkd me prashne ?

    1704824586998.jpeg
     

    Solo Rider

    Well-known member
  • Sep 4, 2020
    35,924
    1
    157,340
    113
    98
    හෝමාගම
    සම්පූර්න කොඩ් එක දාපන්
    මොකද්ද මෙ කරන්න හදන්නෙ, read_nodes method එකට param විදියට nodes_file.txt පාස් කරන්නද හදන්නෙ

    nodes_file.txt මෙක ඉම්පොර්ට් කරන්න ඕන ඇති
    chatgpt දාපන් error එකත් එක්ක
     

    Dragon Kolla

    Well-known member
  • Oct 26, 2015
    9,330
    1,597
    113
    මකර ලන්තෙ
    සම්පූර්න කොඩ් එක දාපන්
    මොකද්ද මෙ කරන්න හදන්නෙ, read_nodes method එකට param විදියට nodes_file.txt පාස් කරන්නද හදන්නෙ

    nodes_file.txt මෙක ඉම්පොර්ට් කරන්න ඕන ඇති
    chatgpt දාපන් error එකත් එක්ක
    menna meka run krnna yanne



    Python:
    import heapq
    
    def read_nodes(file_path):
        with open(file_path, 'r') as file:
            return [tuple(map(float, line.split())) for line in file]
    
    def read_edges(file_path):
        with open(file_path, 'r') as file:
            return [tuple(map(float, line.split())) for line in file]
    
    def read_points_of_interest(file_path):
        with open(file_path, 'r') as file:
            return [line.split() for line in file]
    
    def build_graph(nodes, edges):
        graph = {int(node_id): {} for node_id, _, _ in nodes}
        for edge_id, start_node, end_node, l2_distance in edges:
            graph[int(start_node)][int(end_node)] = l2_distance
            graph[int(end_node)][int(start_node)] = l2_distance  # Assuming undirected edges
        return graph
    
    def build_traffic_data(points_of_interest, categories):
        traffic_data = {int(node_id): {} for _, node_id, _ in points_of_interest}
        for poi_category, longitude, latitude in points_of_interest:
            for node_id, _, _ in points_of_interest:
                weight = 1.0  # Default weight
                if poi_category in categories:
                    weight = categories[poi_category]
                traffic_data[int(node_id)][int(node_id)] = weight
        return traffic_data
    
    def modified_dijkstra(graph, source, destination, traffic_data):
        distances = {node: float('infinity') for node in graph}
        distances[source] = 0
        priority_queue = [(0, source)]
    
        while priority_queue:
            current_distance, current_node = heapq.heappop(priority_queue)
    
            if current_distance > distances[current_node]:
                continue
    
            for neighbor, weight in graph[current_node].items():
                dynamic_weight = weight * (1 + traffic_data[current_node].get(neighbor, 0))
                distance = current_distance + dynamic_weight
    
                if distance < distances[neighbor]:
                    distances[neighbor] = distance
                    heapq.heappush(priority_queue, (distance, neighbor))
    
        return distances[destination]
    
    # Example usage:
    source_node = 1
    destination_node = 3
    
    optimized_distance = modified_dijkstra(graph, source_node, destination_node, traffic_data)
    print(f"Optimized Distance from Node {source_node} to Node {destination_node}: {optimized_distance}")



    Replace the file paths in nodes_file, edges_file, and poi_file with the actual paths to your text files containing nodes, edges, and points of interest data, respectively. Also, make sure that your text files are formatted correctly.

    Note: You'll need to implement the actual Modified Dynamic Weight Dijkstra's Algorithm in the modified_dijkstra function, as the provided code contains a placeholder comment for that part.
     

    Max_r

    Well-known member
  • May 10, 2007
    12,864
    1
    15,577
    113
    ado meka chatgpt damma uttarayak deno
    harida danna itin... (mn melo magulak danne na neh)

    t looks like there's a minor issue with the indentation of your functions, and also, there's a logical error in the build_traffic_data function. The for node_id, _, _ in points_of_interest loop is not using the correct variables. Here's the corrected code:
     
    • Like
    Reactions: Dragon Kolla

    Dragon Kolla

    Well-known member
  • Oct 26, 2015
    9,330
    1,597
    113
    මකර ලන්තෙ
    ado meka chatgpt damma uttarayak deno
    harida danna itin... (mn melo magulak danne na neh)

    t looks like there's a minor issue with the indentation of your functions, and also, there's a logical error in the build_traffic_data function. The for node_id, _, _ in points_of_interest loop is not using the correct variables. Here's the corrected code:
    thanks.

    bt error eka kohed tynne kiyl hoyagnna bane bn
     

    gamerlk

    Well-known member
  • Dec 23, 2023
    4,799
    6,565
    113
    File path එක දෙපැත්තට " " දාපන් 🤦‍♂️

    එකපාරටම program ගහන්න කලින් චුට්ටක් ඔය ලන්වේජ් එක ස්ටඩි කොරා නම් මොකෝ?
     

    TNHM

    Well-known member
  • Jan 3, 2017
    5,340
    15,529
    113
    Invalid variable name ekak. Dot thiyenna ba variable wala. Ube function eke argument eke Dot ekak thiyenawa.
    node_file.txt => invalid
    node_file_text => valid
     
    Last edited:
    • Love
    Reactions: Dragon Kolla

    TNHM

    Well-known member
  • Jan 3, 2017
    5,340
    15,529
    113
    ela. meka tama aula

    https://users.cs.utah.edu/~lifeifei/research/tpq/CA
    mcn me list eke tyna points of interest wala nam tika ganna kramayak tynwa

    airport', 'forest', 'lake'

    me wage
    Python:
    from urllib.request import urlopen
    link = "https://users.cs.utah.edu/~lifeifei/research/tpq/CA"
    f = urlopen(link)
    list = []
    for l in f:
        t = l.decode().split(' ')[0];
        if t not in list:
            list.append(t)
    print(list)

    Loop karana gaman split කරලා list එකට add කරන්න first index එක