NetLogo: break out of nested foreach loop





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm trying to break out of a nested foreach loop using 2 lists of sorted turtles.
But instead of just leaving the inner loop, netlogo breaks out of the whole procedure.
I have a code like the one below (this one is made up, but has exactly the same structure):



to do-something    
let xturtles sort-by [ [a b] -> [birthday] of a > [birthday] of b ] turtles
;; construct an ordered set
foreach xturtles [ the-xturtle ->
ask the-xturtle [
let xage birthday
let yturtles sort-by [ [a b] -> [birthday] of a > [birthday] of b ] turtles with [birthday < xage]
;; construct a second ordered set
foreach yturtles [ the-yturtle ->
let breakout-condition? false
ask the-yturtle [
if (hidden? ) [
set breakout-condition? true
]
]
if breakout-condition? [ stop ]
]
]
]
end


However, when the stop condition is reached, netlogo breaks out of the whole procedure, instead of continuing with the outer loop (the xturtles loop)?



Is that the expected behavior? If so, what is a good practice in this case?



Thanks!
Felix










share|improve this question





























    0















    I'm trying to break out of a nested foreach loop using 2 lists of sorted turtles.
    But instead of just leaving the inner loop, netlogo breaks out of the whole procedure.
    I have a code like the one below (this one is made up, but has exactly the same structure):



    to do-something    
    let xturtles sort-by [ [a b] -> [birthday] of a > [birthday] of b ] turtles
    ;; construct an ordered set
    foreach xturtles [ the-xturtle ->
    ask the-xturtle [
    let xage birthday
    let yturtles sort-by [ [a b] -> [birthday] of a > [birthday] of b ] turtles with [birthday < xage]
    ;; construct a second ordered set
    foreach yturtles [ the-yturtle ->
    let breakout-condition? false
    ask the-yturtle [
    if (hidden? ) [
    set breakout-condition? true
    ]
    ]
    if breakout-condition? [ stop ]
    ]
    ]
    ]
    end


    However, when the stop condition is reached, netlogo breaks out of the whole procedure, instead of continuing with the outer loop (the xturtles loop)?



    Is that the expected behavior? If so, what is a good practice in this case?



    Thanks!
    Felix










    share|improve this question

























      0












      0








      0








      I'm trying to break out of a nested foreach loop using 2 lists of sorted turtles.
      But instead of just leaving the inner loop, netlogo breaks out of the whole procedure.
      I have a code like the one below (this one is made up, but has exactly the same structure):



      to do-something    
      let xturtles sort-by [ [a b] -> [birthday] of a > [birthday] of b ] turtles
      ;; construct an ordered set
      foreach xturtles [ the-xturtle ->
      ask the-xturtle [
      let xage birthday
      let yturtles sort-by [ [a b] -> [birthday] of a > [birthday] of b ] turtles with [birthday < xage]
      ;; construct a second ordered set
      foreach yturtles [ the-yturtle ->
      let breakout-condition? false
      ask the-yturtle [
      if (hidden? ) [
      set breakout-condition? true
      ]
      ]
      if breakout-condition? [ stop ]
      ]
      ]
      ]
      end


      However, when the stop condition is reached, netlogo breaks out of the whole procedure, instead of continuing with the outer loop (the xturtles loop)?



      Is that the expected behavior? If so, what is a good practice in this case?



      Thanks!
      Felix










      share|improve this question














      I'm trying to break out of a nested foreach loop using 2 lists of sorted turtles.
      But instead of just leaving the inner loop, netlogo breaks out of the whole procedure.
      I have a code like the one below (this one is made up, but has exactly the same structure):



      to do-something    
      let xturtles sort-by [ [a b] -> [birthday] of a > [birthday] of b ] turtles
      ;; construct an ordered set
      foreach xturtles [ the-xturtle ->
      ask the-xturtle [
      let xage birthday
      let yturtles sort-by [ [a b] -> [birthday] of a > [birthday] of b ] turtles with [birthday < xage]
      ;; construct a second ordered set
      foreach yturtles [ the-yturtle ->
      let breakout-condition? false
      ask the-yturtle [
      if (hidden? ) [
      set breakout-condition? true
      ]
      ]
      if breakout-condition? [ stop ]
      ]
      ]
      ]
      end


      However, when the stop condition is reached, netlogo breaks out of the whole procedure, instead of continuing with the outer loop (the xturtles loop)?



      Is that the expected behavior? If so, what is a good practice in this case?



      Thanks!
      Felix







      foreach netlogo






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 12:45









      Felix HelixFelix Helix

      355




      355
























          1 Answer
          1






          active

          oldest

          votes


















          1














          It looks even nesting the stop within an extra ask procedure in the same procedure doesn't help. However, if you need a quick fix I think you can replace the second foreach loop with a standalone procedure that contains the stop as a workaround. For example, this procedure follows a similar format to yours and the same problem comes up- as soon as stop is called the broader foreach is exited.



          to nest-foreach-example
          ca
          crt 1
          let xs [ 1 2 3 4 ]
          let ys [ 1 2 3 4 ]
          foreach xs [
          x ->
          foreach ys [
          y ->
          ask turtles [
          if y > x [
          stop
          ]
          ]
          print word x y
          ]
          ]
          end


          This prints out 11.



          However, if you make a custom procedure to take the place of your "y" foreach loop, it works (with or without the ask turtles):



          to nest-foreach-example-turtles
          ca
          crt 1
          let xs [ 1 2 3 4 ]
          let ys [ 1 2 3 4 ]
          foreach xs [
          x ->
          for-y x ys
          ]

          end

          to for-y [ x_ ys ]
          foreach ys [
          y ->
          ask turtles [
          if y > x_ [
          stop
          ]
          ]
          print word x_ y
          ]
          end


          Outputs:



          11
          21
          22
          31
          32
          33
          41
          42
          43
          44





          share|improve this answer
























          • Thanks Luke! Yes, that's an option. Maybe I can come up with a general reporter to be used whenever this kind of computation is used. Another option I found is to use a stop flag and inside the inner loop to check if it's body should be executed. But this adds an extra variable and the loop is running anyway.

            – Felix Helix
            Nov 19 '18 at 10:23













          • @FelixHelix - You bet! You may be able to figure this out, but I'm not sure how to go more general than the for-y example already is, as it accepts the current item being iterated and the new list you want to pass to it. It should be "nestable" as is, too. You're right about the stop flag, but honestly if you don't have a very large model I bet you're probably okay to let the loop run. If you have a larger one, something like the for-y will save some computation time.

            – Luke C
            Nov 20 '18 at 21:36












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53338208%2fnetlogo-break-out-of-nested-foreach-loop%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          It looks even nesting the stop within an extra ask procedure in the same procedure doesn't help. However, if you need a quick fix I think you can replace the second foreach loop with a standalone procedure that contains the stop as a workaround. For example, this procedure follows a similar format to yours and the same problem comes up- as soon as stop is called the broader foreach is exited.



          to nest-foreach-example
          ca
          crt 1
          let xs [ 1 2 3 4 ]
          let ys [ 1 2 3 4 ]
          foreach xs [
          x ->
          foreach ys [
          y ->
          ask turtles [
          if y > x [
          stop
          ]
          ]
          print word x y
          ]
          ]
          end


          This prints out 11.



          However, if you make a custom procedure to take the place of your "y" foreach loop, it works (with or without the ask turtles):



          to nest-foreach-example-turtles
          ca
          crt 1
          let xs [ 1 2 3 4 ]
          let ys [ 1 2 3 4 ]
          foreach xs [
          x ->
          for-y x ys
          ]

          end

          to for-y [ x_ ys ]
          foreach ys [
          y ->
          ask turtles [
          if y > x_ [
          stop
          ]
          ]
          print word x_ y
          ]
          end


          Outputs:



          11
          21
          22
          31
          32
          33
          41
          42
          43
          44





          share|improve this answer
























          • Thanks Luke! Yes, that's an option. Maybe I can come up with a general reporter to be used whenever this kind of computation is used. Another option I found is to use a stop flag and inside the inner loop to check if it's body should be executed. But this adds an extra variable and the loop is running anyway.

            – Felix Helix
            Nov 19 '18 at 10:23













          • @FelixHelix - You bet! You may be able to figure this out, but I'm not sure how to go more general than the for-y example already is, as it accepts the current item being iterated and the new list you want to pass to it. It should be "nestable" as is, too. You're right about the stop flag, but honestly if you don't have a very large model I bet you're probably okay to let the loop run. If you have a larger one, something like the for-y will save some computation time.

            – Luke C
            Nov 20 '18 at 21:36
















          1














          It looks even nesting the stop within an extra ask procedure in the same procedure doesn't help. However, if you need a quick fix I think you can replace the second foreach loop with a standalone procedure that contains the stop as a workaround. For example, this procedure follows a similar format to yours and the same problem comes up- as soon as stop is called the broader foreach is exited.



          to nest-foreach-example
          ca
          crt 1
          let xs [ 1 2 3 4 ]
          let ys [ 1 2 3 4 ]
          foreach xs [
          x ->
          foreach ys [
          y ->
          ask turtles [
          if y > x [
          stop
          ]
          ]
          print word x y
          ]
          ]
          end


          This prints out 11.



          However, if you make a custom procedure to take the place of your "y" foreach loop, it works (with or without the ask turtles):



          to nest-foreach-example-turtles
          ca
          crt 1
          let xs [ 1 2 3 4 ]
          let ys [ 1 2 3 4 ]
          foreach xs [
          x ->
          for-y x ys
          ]

          end

          to for-y [ x_ ys ]
          foreach ys [
          y ->
          ask turtles [
          if y > x_ [
          stop
          ]
          ]
          print word x_ y
          ]
          end


          Outputs:



          11
          21
          22
          31
          32
          33
          41
          42
          43
          44





          share|improve this answer
























          • Thanks Luke! Yes, that's an option. Maybe I can come up with a general reporter to be used whenever this kind of computation is used. Another option I found is to use a stop flag and inside the inner loop to check if it's body should be executed. But this adds an extra variable and the loop is running anyway.

            – Felix Helix
            Nov 19 '18 at 10:23













          • @FelixHelix - You bet! You may be able to figure this out, but I'm not sure how to go more general than the for-y example already is, as it accepts the current item being iterated and the new list you want to pass to it. It should be "nestable" as is, too. You're right about the stop flag, but honestly if you don't have a very large model I bet you're probably okay to let the loop run. If you have a larger one, something like the for-y will save some computation time.

            – Luke C
            Nov 20 '18 at 21:36














          1












          1








          1







          It looks even nesting the stop within an extra ask procedure in the same procedure doesn't help. However, if you need a quick fix I think you can replace the second foreach loop with a standalone procedure that contains the stop as a workaround. For example, this procedure follows a similar format to yours and the same problem comes up- as soon as stop is called the broader foreach is exited.



          to nest-foreach-example
          ca
          crt 1
          let xs [ 1 2 3 4 ]
          let ys [ 1 2 3 4 ]
          foreach xs [
          x ->
          foreach ys [
          y ->
          ask turtles [
          if y > x [
          stop
          ]
          ]
          print word x y
          ]
          ]
          end


          This prints out 11.



          However, if you make a custom procedure to take the place of your "y" foreach loop, it works (with or without the ask turtles):



          to nest-foreach-example-turtles
          ca
          crt 1
          let xs [ 1 2 3 4 ]
          let ys [ 1 2 3 4 ]
          foreach xs [
          x ->
          for-y x ys
          ]

          end

          to for-y [ x_ ys ]
          foreach ys [
          y ->
          ask turtles [
          if y > x_ [
          stop
          ]
          ]
          print word x_ y
          ]
          end


          Outputs:



          11
          21
          22
          31
          32
          33
          41
          42
          43
          44





          share|improve this answer













          It looks even nesting the stop within an extra ask procedure in the same procedure doesn't help. However, if you need a quick fix I think you can replace the second foreach loop with a standalone procedure that contains the stop as a workaround. For example, this procedure follows a similar format to yours and the same problem comes up- as soon as stop is called the broader foreach is exited.



          to nest-foreach-example
          ca
          crt 1
          let xs [ 1 2 3 4 ]
          let ys [ 1 2 3 4 ]
          foreach xs [
          x ->
          foreach ys [
          y ->
          ask turtles [
          if y > x [
          stop
          ]
          ]
          print word x y
          ]
          ]
          end


          This prints out 11.



          However, if you make a custom procedure to take the place of your "y" foreach loop, it works (with or without the ask turtles):



          to nest-foreach-example-turtles
          ca
          crt 1
          let xs [ 1 2 3 4 ]
          let ys [ 1 2 3 4 ]
          foreach xs [
          x ->
          for-y x ys
          ]

          end

          to for-y [ x_ ys ]
          foreach ys [
          y ->
          ask turtles [
          if y > x_ [
          stop
          ]
          ]
          print word x_ y
          ]
          end


          Outputs:



          11
          21
          22
          31
          32
          33
          41
          42
          43
          44






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 18:50









          Luke CLuke C

          7,0371815




          7,0371815













          • Thanks Luke! Yes, that's an option. Maybe I can come up with a general reporter to be used whenever this kind of computation is used. Another option I found is to use a stop flag and inside the inner loop to check if it's body should be executed. But this adds an extra variable and the loop is running anyway.

            – Felix Helix
            Nov 19 '18 at 10:23













          • @FelixHelix - You bet! You may be able to figure this out, but I'm not sure how to go more general than the for-y example already is, as it accepts the current item being iterated and the new list you want to pass to it. It should be "nestable" as is, too. You're right about the stop flag, but honestly if you don't have a very large model I bet you're probably okay to let the loop run. If you have a larger one, something like the for-y will save some computation time.

            – Luke C
            Nov 20 '18 at 21:36



















          • Thanks Luke! Yes, that's an option. Maybe I can come up with a general reporter to be used whenever this kind of computation is used. Another option I found is to use a stop flag and inside the inner loop to check if it's body should be executed. But this adds an extra variable and the loop is running anyway.

            – Felix Helix
            Nov 19 '18 at 10:23













          • @FelixHelix - You bet! You may be able to figure this out, but I'm not sure how to go more general than the for-y example already is, as it accepts the current item being iterated and the new list you want to pass to it. It should be "nestable" as is, too. You're right about the stop flag, but honestly if you don't have a very large model I bet you're probably okay to let the loop run. If you have a larger one, something like the for-y will save some computation time.

            – Luke C
            Nov 20 '18 at 21:36

















          Thanks Luke! Yes, that's an option. Maybe I can come up with a general reporter to be used whenever this kind of computation is used. Another option I found is to use a stop flag and inside the inner loop to check if it's body should be executed. But this adds an extra variable and the loop is running anyway.

          – Felix Helix
          Nov 19 '18 at 10:23







          Thanks Luke! Yes, that's an option. Maybe I can come up with a general reporter to be used whenever this kind of computation is used. Another option I found is to use a stop flag and inside the inner loop to check if it's body should be executed. But this adds an extra variable and the loop is running anyway.

          – Felix Helix
          Nov 19 '18 at 10:23















          @FelixHelix - You bet! You may be able to figure this out, but I'm not sure how to go more general than the for-y example already is, as it accepts the current item being iterated and the new list you want to pass to it. It should be "nestable" as is, too. You're right about the stop flag, but honestly if you don't have a very large model I bet you're probably okay to let the loop run. If you have a larger one, something like the for-y will save some computation time.

          – Luke C
          Nov 20 '18 at 21:36





          @FelixHelix - You bet! You may be able to figure this out, but I'm not sure how to go more general than the for-y example already is, as it accepts the current item being iterated and the new list you want to pass to it. It should be "nestable" as is, too. You're right about the stop flag, but honestly if you don't have a very large model I bet you're probably okay to let the loop run. If you have a larger one, something like the for-y will save some computation time.

          – Luke C
          Nov 20 '18 at 21:36




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53338208%2fnetlogo-break-out-of-nested-foreach-loop%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python