user3369125
user3369125

Reputation: 165

Optimal Path on a given graph

I am working on a project in netLogo in which i have a random network in which each and every link is assigned a bandwidth. The algorithm chooses a random Source and Destination by itself and after which it has to choose an optimal path between these two.

My question is how to choose the optimal path by exploring all the possible paths.

hereby attaching the sourcecode of my model:

    breed[nodes node]
    breed[ants ant ]

    globals [nodename nodenumbersource nodenumberdestination relaynode dead-network num        ]
    nodes-own[visited ]
    links-own[visit bandwidth]



    ants-own 
    [
    distance-gone
    distance-to-go
    target-node
    current-node
    ]




     to cr11
     ask  nodes with [label = "Source"]
     [
     set num  count (link-neighbors)
     ]
     create-ants num ;num-ants 
     [

     let n one-of nodes with [label = "Source"]

     setxy ([xcor] of n)  ([ycor]of n)
     set current-node n
     set color white set size 0.95
     set distance-gone 0
     set distance-to-go 0
     set target-node one-of nodes with [ label = "Relay Node" ] ;nobody
     ]
     end

     to face-targets
     ask ants ;with [ target-node]; = node 4 ]    ;nobody ]
     [

     let d 0

     face (one-of nodes with [ label = "Relay Node" ]);target-node
      ask current-node [
        set d distance  (one-of nodes with [ label = "Relay Node" ]);target-node)

      ] 
      set distance-to-go d

      ]
      end

      to move-forward
      face-targets 

      ask ants [
       while [  distance-gone < (distance-to-go  )]
      [


       fd  1
       set distance-gone (distance-gone + 1)
       ]
      ]
        ask ants [
           if distance-gone < distance-to-go

           [
            set current-node target-node 
            setxy ([xcor] of current-node) ([ycor] of current-node)
            set distance-gone 0
            set distance-to-go 0

            ]
           ]
            end





                ;This is used to design the Network
               to setup
                setup1
                 setup-spatially-clustered-network
                 ask links [set color white
                 set visit false
                 set bandwidth (5 + random 10) ;min bw 5 max 15
                ] 


                end



             to setup1

            __clear-all-and-reset-ticks
            set dead-network 0
            create-nodes number-of-nodes
               [

                 setxy (random-xcor   * 0.95) (random-ycor * 0.95)
                 set shape "circle"
                 set color green
                 set visited false
                 set label who
               ]


             end

             ;Links are created for the  nodes
             to setup-spatially-clustered-network
             let num-links (6 * number-of-nodes) / 2 

             while [count links < num-links ]
             [
               ask one-of turtles
              [
               let choice (min-one-of (other turtles with [not link-neighbor? myself])
              [distance myself])
           if choice != nobody [ create-link-with choice

         ]
        ]
       ]

       repeat 10
       [ 
         layout-spring turtles links 0.3 (world-width / (sqrt number-of-nodes)) 1
       ]  
        end

       ;This is to Generate Message for nodes
       to test1
       ask one-of nodes
       [
        set color red 
         set label "Source"
         set nodenumbersource who


        ]
          ask one-of nodes with [color = green]
         [
          set color red
          set label "Destination"
          set nodenumberdestination who


          ]

        cr11


        end



        to test3




        ask turtles with [label = "Source"]
      [
      set label "ants moving"

       ask my-links 
      [
         set color green
       ]


           ask link-neighbors
        [
          set color blue

         ]




            ask min-one-of turtles with [color = blue and my-links] [distance turtle   nodenumberdestination ] 
           [
           ask max-one-of my-links [bandwidth ]
           [


             set color red 
            ]
           set color white
           set relaynode who
           set label "Relay Node"
            ]



            ; face-targets 
            move-forward
          end

     to test4


      ask turtle nodenumberdestination 
       [
       while [color != white]
       [


        ask turtle relaynode
        [
        set label "" 

         ask my-links
         [
          set color green
          ]
         ask link-neighbors
        [


      set color yellow
        ]
        ]

       ask turtles with [color = yellow] 

       [
        set color violet
        ]

         ask turtles with [color = violet] with [visited = false]
         [
         set color magenta
         ]
         ask min-one-of turtles with [color = magenta] [distance turtle nodenumberdestination] 
         [  
          set color white
         set relaynode who
         set label "Relay Node" 
         set visited true
        ]
         move-forward

          ]
       ]








      end

      to test6
      ask nodes ; turtles
      [
       set color green
       set visited false
       set label ""
       ]
      ask links
       [
       set color white
       set visit false
        ]
      end


      to test5

      test1
      test3
      test4


     end

Upvotes: 3

Views: 488

Answers (1)

Bryan Head
Bryan Head

Reputation: 12580

You can use the NW-Extension for that! Just download it and stick it in NetLogo's extensions folder. Then, in your model, you can start using by adding

extensions [ nw ]

to the top of your code. Then you can use one of the weighted-path-to primitives to get the turtles or links along the shortest path between two nodes.

Note that the nw extension is under active development, though it is well tested. You also must be using the latest version of NetLogo.

If you want to implement it yourself, Dijkstra's algorithm is the classic solution if you have nonnegative weights in your graph.

Upvotes: 3

Related Questions