kingrif
kingrif

Reputation: 11

I get the following error when I run this code netlogo

my model of netlogo

globals [ mejor-recorrido
          coste-mejor-recorrido ]
breed [ nodos nodo ]
breed [ hormigas hormiga ]
links-own [ coste
            feromona ]       
hormigas-own [ recorrido
               coste-recorrido ]
to setup
  __clear-all-and-reset-ticks
  set-default-shape nodos "circle"
  ask patches [set pcolor white]
  crea-nodos
  crea-aristas
  crea-hormigas
  set mejor-recorrido camino-aleatorio
  set coste-mejor-recorrido longitud-recorrido mejor-recorrido
end

to crea-nodos
  ask n-of num-nodos patches
  [  
    sprout-nodos 1
    [
      set color blue + 2
      set size 2
      set label-color black
      set label (word "Ciudad-" who)
    ]
  ]
end

to crea-aristas
  ask nodos
  [ 
    create-links-with other nodos
    [
     ; hide-link
      set color red
      set coste link-length 
      set feromona random-float 0.1
    ]
  ]
  ask links [
    if(feromona < 0.05) [die] ]
  let max-coste max [coste] of links
  ask links
  [
    set coste coste / max-coste
  ]
end

to crea-hormigas
  create-hormigas num-hormigas [
    hide-turtle
    set recorrido []
    set coste-recorrido 0
  ]
end

to reset
  ask hormigas [die]
  ask links [
    hide-link
    set feromona random-float 0.1
  ]
  crea-hormigas
  set mejor-recorrido camino-aleatorio
  set coste-mejor-recorrido longitud-recorrido mejor-recorrido  
  clear-all-plots
end

to go
  no-display
  ask hormigas [
    set recorrido generar-recorrido
    set coste-recorrido longitud-recorrido recorrido 
    if coste-recorrido < coste-mejor-recorrido [
      set mejor-recorrido recorrido
      set coste-mejor-recorrido coste-recorrido
    ]
  ]

  actualiza-feromona
  tick
  display
end

to-report camino-aleatorio
  let resp [self] of nodos
  report lput (first resp) resp
end

to-report generar-recorrido
  let origen one-of nodos
  let nuevo-recorrido (list origen)
  let resto-nodos [self] of nodos with [self != origen]
  let nodo-actual origen

  while [not empty? resto-nodos] [
    if (self = origen) [
        ask hormigas [die]
      ]
    let siguiente-nodo elige-siguiente-nodo nodo-actual resto-nodos
    set nuevo-recorrido lput siguiente-nodo nuevo-recorrido
    set resto-nodos remove siguiente-nodo resto-nodos
    set nodo-actual siguiente-nodo
  ]
  set nuevo-recorrido lput origen nuevo-recorrido

  report nuevo-recorrido
end

to-report elige-siguiente-nodo [nodo-actual resto-nodos]
  let probabilidades calcula-probabilidades nodo-actual resto-nodos
  let rand-num random-float 1
  report last first filter [first ? >= rand-num] probabilidades
end

to-report calcula-probabilidades [nodo-actual resto-nodos]
  let pt map [([feromona] of ? ^ alpha) * ((1 / [coste] of ?) ^ beta)]
             (map [arista nodo-actual ?] resto-nodos)
  let denominador sum pt
  set pt map [? / denominador] pt
  let probabilidades sort-by [first ?1 < first ?2]
                             (map [(list ?1 ?2)] pt resto-nodos)
  let probabilidad-normalizada []
  let ac 0
  foreach probabilidades [
    set ac (ac + first ?)
    set probabilidad-normalizada lput (list ac last ?) probabilidad-normalizada
  ]
  report probabilidad-normalizada  
end

to actualiza-feromona
  ;; Evapora la feromona del grafo
  ask links [
    set feromona (feromona * (1 - rho))
  ]
  ask hormigas [
    let inc-feromona (100 / coste-recorrido)
    foreach aristas-recorrido recorrido [
      ask ? [ set feromona (feromona + inc-feromona) ]    
    ]
  ]
end

to-report aristas-recorrido [nodos-recorrido]
  report map [arista (item ? nodos-recorrido)
                     (item (? + 1) nodos-recorrido)] (n-values num-nodos [?])
end

to-report arista [n1 n2]
  report (link [who] of n1 [who] of n2)
end

to-report longitud-recorrido [nodos-recorrido]
  report reduce [?1 + ?2] map [[coste] of ?] (aristas-recorrido nodos-recorrido)
end

Upvotes: 1

Views: 142

Answers (1)

Mars
Mars

Reputation: 8854

I added

print aristas-recorrido nodos-recorrido

as the first line after to-report longitud-recorrido [nodos-recorrido]. This shows that aristas-recorrido nodos-recorrido returns a list in which some of the elements are nobody. That's what's processed by one of the lines that is generating the error you mentioned.

I believe that the problem is that in the definition of aristas-recorrido,

to-report aristas-recorrido [nodos-recorrido]
  report map [arista (item ? nodos-recorrido)
                     (item (? + 1) nodos-recorrido)]
             (n-values num-nodos [?])
end

arista attempts to report the link between subsequent nodo turtles in the list nodos-recorrido, but some of the pairs of nodos are not linked.

It looks to me like the procedure crea-aristas links every nodo with every other nodo, but then removes links with feromona < 0.05. After that, not all nodos are linked. Since nodos-recorrido above is just a randomly-ordered list of nodos (is this right?), some of the pairs of nodos are not linked, and thus arista returns nobody rather than a link for some pairs. Then this leads to the error in longitud-recorrido.

(I didn't investigate the other line that's generating the error, but I think you'll have enough information to track down that error now.)

Upvotes: 2

Related Questions