How to set 2 turtles to the same location?












1














I'm trying to make a simulation exactly the Dining Philosophers in the net logo library but with a different method. I'm trying to create a situation where there are 20 philosophers in a circle with one "fork" in front of each philosopher. The philosophers either eat, think, or get hungry. They can only eat by obtaining 2 forks and after they're done eating, they put the forks down and think until they get hungry. I'm trying to ask the 2 forks within the range of the hungry philosophers to move to the respective philosophers, but I'm not sure how to do it.
Here is my code so far:



   breed [philosophers philosopher]
breed [forks fork]
philosophers-own [thinking eating hungry]
globals [x y]
;eating = green
;thinking = blue
;hungry = red


to setup
ca
cro num-philosophers [set breed philosophers
fd 10 set shape "person-1"
set color blue
ask philosophers [
set hungry hungry = false
set thinking thinking = true
set eating eating = false]
set size 3]

cro num-philosophers [set breed forks fd 8
set heading heading + 180 / num-philosophers
fd -1
lt 180
set shape "fork"
set color grey
set size 2.5
]
reset-timer
end

to go
move
end

to move

every .1 [
ask philosophers with [who mod 2 = 0] [set color red
set hungry hungry = true
set thinking thinking = false
set eating eating = false]

ask philosophers with [hungry = true] [
;this following line with in-radius was my attempt to move the forks but it doesn't work
ask [forks in-radius 4] of philosophers with [hungry = true] [setxy x y]
ask fork 21 [setxy x y]
set y [ycor] of one-of philosophers with [hungry = true]
set x [xcor] of one-of philosophers with [hungry = true]
]]
end


any advice on how to solve this is appreciated! Thank you!










share|improve this question



























    1














    I'm trying to make a simulation exactly the Dining Philosophers in the net logo library but with a different method. I'm trying to create a situation where there are 20 philosophers in a circle with one "fork" in front of each philosopher. The philosophers either eat, think, or get hungry. They can only eat by obtaining 2 forks and after they're done eating, they put the forks down and think until they get hungry. I'm trying to ask the 2 forks within the range of the hungry philosophers to move to the respective philosophers, but I'm not sure how to do it.
    Here is my code so far:



       breed [philosophers philosopher]
    breed [forks fork]
    philosophers-own [thinking eating hungry]
    globals [x y]
    ;eating = green
    ;thinking = blue
    ;hungry = red


    to setup
    ca
    cro num-philosophers [set breed philosophers
    fd 10 set shape "person-1"
    set color blue
    ask philosophers [
    set hungry hungry = false
    set thinking thinking = true
    set eating eating = false]
    set size 3]

    cro num-philosophers [set breed forks fd 8
    set heading heading + 180 / num-philosophers
    fd -1
    lt 180
    set shape "fork"
    set color grey
    set size 2.5
    ]
    reset-timer
    end

    to go
    move
    end

    to move

    every .1 [
    ask philosophers with [who mod 2 = 0] [set color red
    set hungry hungry = true
    set thinking thinking = false
    set eating eating = false]

    ask philosophers with [hungry = true] [
    ;this following line with in-radius was my attempt to move the forks but it doesn't work
    ask [forks in-radius 4] of philosophers with [hungry = true] [setxy x y]
    ask fork 21 [setxy x y]
    set y [ycor] of one-of philosophers with [hungry = true]
    set x [xcor] of one-of philosophers with [hungry = true]
    ]]
    end


    any advice on how to solve this is appreciated! Thank you!










    share|improve this question

























      1












      1








      1







      I'm trying to make a simulation exactly the Dining Philosophers in the net logo library but with a different method. I'm trying to create a situation where there are 20 philosophers in a circle with one "fork" in front of each philosopher. The philosophers either eat, think, or get hungry. They can only eat by obtaining 2 forks and after they're done eating, they put the forks down and think until they get hungry. I'm trying to ask the 2 forks within the range of the hungry philosophers to move to the respective philosophers, but I'm not sure how to do it.
      Here is my code so far:



         breed [philosophers philosopher]
      breed [forks fork]
      philosophers-own [thinking eating hungry]
      globals [x y]
      ;eating = green
      ;thinking = blue
      ;hungry = red


      to setup
      ca
      cro num-philosophers [set breed philosophers
      fd 10 set shape "person-1"
      set color blue
      ask philosophers [
      set hungry hungry = false
      set thinking thinking = true
      set eating eating = false]
      set size 3]

      cro num-philosophers [set breed forks fd 8
      set heading heading + 180 / num-philosophers
      fd -1
      lt 180
      set shape "fork"
      set color grey
      set size 2.5
      ]
      reset-timer
      end

      to go
      move
      end

      to move

      every .1 [
      ask philosophers with [who mod 2 = 0] [set color red
      set hungry hungry = true
      set thinking thinking = false
      set eating eating = false]

      ask philosophers with [hungry = true] [
      ;this following line with in-radius was my attempt to move the forks but it doesn't work
      ask [forks in-radius 4] of philosophers with [hungry = true] [setxy x y]
      ask fork 21 [setxy x y]
      set y [ycor] of one-of philosophers with [hungry = true]
      set x [xcor] of one-of philosophers with [hungry = true]
      ]]
      end


      any advice on how to solve this is appreciated! Thank you!










      share|improve this question













      I'm trying to make a simulation exactly the Dining Philosophers in the net logo library but with a different method. I'm trying to create a situation where there are 20 philosophers in a circle with one "fork" in front of each philosopher. The philosophers either eat, think, or get hungry. They can only eat by obtaining 2 forks and after they're done eating, they put the forks down and think until they get hungry. I'm trying to ask the 2 forks within the range of the hungry philosophers to move to the respective philosophers, but I'm not sure how to do it.
      Here is my code so far:



         breed [philosophers philosopher]
      breed [forks fork]
      philosophers-own [thinking eating hungry]
      globals [x y]
      ;eating = green
      ;thinking = blue
      ;hungry = red


      to setup
      ca
      cro num-philosophers [set breed philosophers
      fd 10 set shape "person-1"
      set color blue
      ask philosophers [
      set hungry hungry = false
      set thinking thinking = true
      set eating eating = false]
      set size 3]

      cro num-philosophers [set breed forks fd 8
      set heading heading + 180 / num-philosophers
      fd -1
      lt 180
      set shape "fork"
      set color grey
      set size 2.5
      ]
      reset-timer
      end

      to go
      move
      end

      to move

      every .1 [
      ask philosophers with [who mod 2 = 0] [set color red
      set hungry hungry = true
      set thinking thinking = false
      set eating eating = false]

      ask philosophers with [hungry = true] [
      ;this following line with in-radius was my attempt to move the forks but it doesn't work
      ask [forks in-radius 4] of philosophers with [hungry = true] [setxy x y]
      ask fork 21 [setxy x y]
      set y [ycor] of one-of philosophers with [hungry = true]
      set x [xcor] of one-of philosophers with [hungry = true]
      ]]
      end


      any advice on how to solve this is appreciated! Thank you!







      netlogo






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 at 23:55









      Sam

      184




      184
























          1 Answer
          1






          active

          oldest

          votes


















          3














          First problem is your lines like set hungry hungry = false. In NetLogo, you assign a variable value without an equals sign. Assuming that you want to set the variable named 'hungry' to false, your code should be set hungry false. Also, by convention, NetLogo boolean variable names use a question mark at the end (to remind you they are boolean) so it would be better to have set hungry? false and change the philosophers-own statement accordingly.



          This will be causing part of your error because the value of hungry is being tested as true or false, but you didn't assign true or false. So the if statement will always be false.



          Second, since you are essentially doing the moving from the perspective of the forks, it is probably best to ask forks for the movement, rather than ask philosophers. Perhaps something like:



          ask forks
          [ let targets (philosophers in-radius 4) with [hungry?]
          if any? targets
          move-to target with-min [distance myself]
          ]


          This code is not tested. The basic approach is to check with the fork whether there are any hungry philosophers within a distance of 4. If there are, the fork moves to the location of the closest hungry philosopher. Look up move-to in the NetLogo dictionary. Even if this isn't the answer you want, it's probably the primitive you are looking for. You don't need to be getting the xcor and ycor from one turtle and passing them to the other turtle, you can simply move to the turtle (or face the turtle and then move forward a little).



          Finally, I recommend you build your code more gradually. For example, you could turn the philosopher red if it's within 4 distance of a fork. Then you can worry about moving.



          On a separate issue, it is extremely unlikely that you actually want to use every. This is only when you want to have real time (such as a number of seconds) for each time step. Instead, you should be thinking with tick to increment the clock. Your model will run much faster because it will be limited by how much processing is required instead of tracking against time in the real world.






          share|improve this answer























            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%2f53403373%2fhow-to-set-2-turtles-to-the-same-location%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









            3














            First problem is your lines like set hungry hungry = false. In NetLogo, you assign a variable value without an equals sign. Assuming that you want to set the variable named 'hungry' to false, your code should be set hungry false. Also, by convention, NetLogo boolean variable names use a question mark at the end (to remind you they are boolean) so it would be better to have set hungry? false and change the philosophers-own statement accordingly.



            This will be causing part of your error because the value of hungry is being tested as true or false, but you didn't assign true or false. So the if statement will always be false.



            Second, since you are essentially doing the moving from the perspective of the forks, it is probably best to ask forks for the movement, rather than ask philosophers. Perhaps something like:



            ask forks
            [ let targets (philosophers in-radius 4) with [hungry?]
            if any? targets
            move-to target with-min [distance myself]
            ]


            This code is not tested. The basic approach is to check with the fork whether there are any hungry philosophers within a distance of 4. If there are, the fork moves to the location of the closest hungry philosopher. Look up move-to in the NetLogo dictionary. Even if this isn't the answer you want, it's probably the primitive you are looking for. You don't need to be getting the xcor and ycor from one turtle and passing them to the other turtle, you can simply move to the turtle (or face the turtle and then move forward a little).



            Finally, I recommend you build your code more gradually. For example, you could turn the philosopher red if it's within 4 distance of a fork. Then you can worry about moving.



            On a separate issue, it is extremely unlikely that you actually want to use every. This is only when you want to have real time (such as a number of seconds) for each time step. Instead, you should be thinking with tick to increment the clock. Your model will run much faster because it will be limited by how much processing is required instead of tracking against time in the real world.






            share|improve this answer




























              3














              First problem is your lines like set hungry hungry = false. In NetLogo, you assign a variable value without an equals sign. Assuming that you want to set the variable named 'hungry' to false, your code should be set hungry false. Also, by convention, NetLogo boolean variable names use a question mark at the end (to remind you they are boolean) so it would be better to have set hungry? false and change the philosophers-own statement accordingly.



              This will be causing part of your error because the value of hungry is being tested as true or false, but you didn't assign true or false. So the if statement will always be false.



              Second, since you are essentially doing the moving from the perspective of the forks, it is probably best to ask forks for the movement, rather than ask philosophers. Perhaps something like:



              ask forks
              [ let targets (philosophers in-radius 4) with [hungry?]
              if any? targets
              move-to target with-min [distance myself]
              ]


              This code is not tested. The basic approach is to check with the fork whether there are any hungry philosophers within a distance of 4. If there are, the fork moves to the location of the closest hungry philosopher. Look up move-to in the NetLogo dictionary. Even if this isn't the answer you want, it's probably the primitive you are looking for. You don't need to be getting the xcor and ycor from one turtle and passing them to the other turtle, you can simply move to the turtle (or face the turtle and then move forward a little).



              Finally, I recommend you build your code more gradually. For example, you could turn the philosopher red if it's within 4 distance of a fork. Then you can worry about moving.



              On a separate issue, it is extremely unlikely that you actually want to use every. This is only when you want to have real time (such as a number of seconds) for each time step. Instead, you should be thinking with tick to increment the clock. Your model will run much faster because it will be limited by how much processing is required instead of tracking against time in the real world.






              share|improve this answer


























                3












                3








                3






                First problem is your lines like set hungry hungry = false. In NetLogo, you assign a variable value without an equals sign. Assuming that you want to set the variable named 'hungry' to false, your code should be set hungry false. Also, by convention, NetLogo boolean variable names use a question mark at the end (to remind you they are boolean) so it would be better to have set hungry? false and change the philosophers-own statement accordingly.



                This will be causing part of your error because the value of hungry is being tested as true or false, but you didn't assign true or false. So the if statement will always be false.



                Second, since you are essentially doing the moving from the perspective of the forks, it is probably best to ask forks for the movement, rather than ask philosophers. Perhaps something like:



                ask forks
                [ let targets (philosophers in-radius 4) with [hungry?]
                if any? targets
                move-to target with-min [distance myself]
                ]


                This code is not tested. The basic approach is to check with the fork whether there are any hungry philosophers within a distance of 4. If there are, the fork moves to the location of the closest hungry philosopher. Look up move-to in the NetLogo dictionary. Even if this isn't the answer you want, it's probably the primitive you are looking for. You don't need to be getting the xcor and ycor from one turtle and passing them to the other turtle, you can simply move to the turtle (or face the turtle and then move forward a little).



                Finally, I recommend you build your code more gradually. For example, you could turn the philosopher red if it's within 4 distance of a fork. Then you can worry about moving.



                On a separate issue, it is extremely unlikely that you actually want to use every. This is only when you want to have real time (such as a number of seconds) for each time step. Instead, you should be thinking with tick to increment the clock. Your model will run much faster because it will be limited by how much processing is required instead of tracking against time in the real world.






                share|improve this answer














                First problem is your lines like set hungry hungry = false. In NetLogo, you assign a variable value without an equals sign. Assuming that you want to set the variable named 'hungry' to false, your code should be set hungry false. Also, by convention, NetLogo boolean variable names use a question mark at the end (to remind you they are boolean) so it would be better to have set hungry? false and change the philosophers-own statement accordingly.



                This will be causing part of your error because the value of hungry is being tested as true or false, but you didn't assign true or false. So the if statement will always be false.



                Second, since you are essentially doing the moving from the perspective of the forks, it is probably best to ask forks for the movement, rather than ask philosophers. Perhaps something like:



                ask forks
                [ let targets (philosophers in-radius 4) with [hungry?]
                if any? targets
                move-to target with-min [distance myself]
                ]


                This code is not tested. The basic approach is to check with the fork whether there are any hungry philosophers within a distance of 4. If there are, the fork moves to the location of the closest hungry philosopher. Look up move-to in the NetLogo dictionary. Even if this isn't the answer you want, it's probably the primitive you are looking for. You don't need to be getting the xcor and ycor from one turtle and passing them to the other turtle, you can simply move to the turtle (or face the turtle and then move forward a little).



                Finally, I recommend you build your code more gradually. For example, you could turn the philosopher red if it's within 4 distance of a fork. Then you can worry about moving.



                On a separate issue, it is extremely unlikely that you actually want to use every. This is only when you want to have real time (such as a number of seconds) for each time step. Instead, you should be thinking with tick to increment the clock. Your model will run much faster because it will be limited by how much processing is required instead of tracking against time in the real world.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 21 at 7:41

























                answered Nov 21 at 1:02









                JenB

                8,19411036




                8,19411036






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f53403373%2fhow-to-set-2-turtles-to-the-same-location%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

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    TypeError: fit_transform() missing 1 required positional argument: 'X'