2017-02-14 16 views
0

nicht finden Das folgende Verfahren soll einen "Patron" aus einer Anzahl von Schildkröten auswählen und ihre Farbe in weiß ändern. Die Auswahl basiert auf dem Reichtum der Schildkröten.netlogo Fehler kann Element x in Liste der Länge x

Ausführen dieses Codes, insbesondere die Zeile item i ranking-in-radius Ich bekomme eine Fehlermeldung etwa wie folgt: "cannot find element 43 in list...(shows list of turtles' wealth)... which is only of length 43." Ich verstehe, ich bekomme diesen Fehler, weil Listen bei 0 beginnen, damit eine Liste von 43 Elementen von 0 bis 42 geht, aber ich verstehe nicht, warum der Code, den ich geschrieben habe, nicht korrekt ist. Mein i startet um 0, und die Prozedur wird beendet, wenn i nicht < ist, als die Anzahl der abgefragten Schildkröten (dh sollte bei 42 aufhören). Kann jemand eine Lösung vorschlagen? Vielen Dank!

to choose-patron 
    let i 0 
    let patrons-in-radius count turtles in-radius radius with [wealth >= 80] 
    let ranking-in-radius sort-by > [wealth] of turtles in-radius radius with [wealth >= 80] 
    let total-wealth-in-radius sum [wealth] of turtles in-radius radius with [wealth >= 80] 
    while [i < (patrons-in-radius)][ 
    ask turtles in-radius radius with [wealth >= 80] [ 
     if [wealth] of self = item i ranking-in-radius [ 
     if random 100 < ((wealth/total-wealth-in-radius) * 100) [ 
      set color white 
      if any? turtles in-radius radius with [color = white] [stop] 
     ] 
     set i (i + 1) 
    ] 
    ] 
] 
end 

Antwort

0

Es sieht aus wie Ihre set i (i + 1) ist im falschen Block. Gerade jetzt i erhöht jedes Mal, wenn Ihre if [wealth]... läuft, aber Sie sollten nur i erhöhen am Ende der while [i < (patrons-in-radius)] Block.

Versuchen Sie den Code als Ersatz für Ihre while Aussage:

while [ i < (patrons-in-radius) ] [ 
    ask turtles in-radius radius with [ wealth >= 80 ] [ 
     if [ wealth ] of self = item i ranking-in-radius [ 
     if random 100 < ((wealth/total-wealth-in-radius) * 100) [ 
      set color white 
      if any? turtles in-radius radius with [color = white] [ 
      stop 
      ] 
     ] 
     ] 
    ] 
    set i i + 1 
    ] 
Verwandte Themen