Ally
Ally

Reputation: 65

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!

Upvotes: 1

Views: 58

Answers (1)

JenB
JenB

Reputation: 17678

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.

Upvotes: 3

Related Questions