How to set 2 turtles to the same location?
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
add a comment |
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
add a comment |
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
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
netlogo
asked Nov 20 at 23:55
Sam
184
184
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 21 at 7:41
answered Nov 21 at 1:02
JenB
8,19411036
8,19411036
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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