Reputation: 806
I am having some troubles to plot objects created using hatch. I would like to plot objects created by engineersA and engineersB, separately. After JenB’s answer, the code that I am using for this purpose is
sk one-of turtles with [breed = engineersA or breed = engineersB]
[ if empty? my-list
[ set size random-float 1
]
hatch-objects random 10
[ hide-turtle
ask myself
[ set my-list fput self my-list
]
]
]
What I tried to do is
ask engineersA [plotxy ticks objects]
same for engineersB.
Unfortunately, because of objects as a breed, it does not work.
I hope you can help me.
Upvotes: 0
Views: 91
Reputation: 1473
There are problems with context in your hatch-objects code, particularly with what values "self" and "myself" take on. The code as written will never put any objects on the my-lists. Explaining it is difficult so I wrote code (that runs) with a verbose? variable in setup. If you set verbose? to true, the print commands will print out what's going on in each step of the code. ( if you set it to false, the print commands won't print anything.)
At the end of the code are the commands I used to get a plot of the counts of objects created by each breed of Engineers. That is documented in the code right there, and it works correctly.
I decided to store text values of "A" or "B" in each object to show what breed created that object. It was a problem to store the breed in that variable and test for it, so I just used text, which worked.
I made the turtles large so that they were easy to click on an inspect.
This should be enough of an example for you to figure out how to compute and store the other things you want to do.
globals [
verbose? ;; set this true to print out debugging info
count-type-a ;; count of objects created by engineersA
count-type-b ;; count of objects created by engineersB
]
breed [engineersA engineerA]
engineersA-own [ my-list]
breed [engineersB engineerB]
engineersB-own [my-list]
breed [objects object]
objects-own[
creator-breed ;; set to either "A" or "B"
]
to setup
clear-all
set verbose? true ;; turns on and off debugging print statements
create-engineersA 3 [
setxy random-xcor random-ycor
set label who set size 3
set my-list []
]
create-engineersB 3 [
setxy random-xcor random-ycor
set label who set size 3
set my-list []
]
reset-ticks
end
to go
let breed-picked "none"
ask one-of turtles with [breed = engineersA or breed = engineersB]
[
;;set breed-picked breed ( produces a result that looks like a string but isn't)
if-else ( breed = engineersA) [
if verbose? [ print " this is breed engineersa "]
set breed-picked "A" ]
[ set breed-picked "B" ]
if verbose? [ print ( word "we are looking at turtle " who " now, which is breed " breed-picked) ]
if my-list = []
[
if verbose? [print "my-list is empty"]
; set size random-float 1
]
hatch-objects 3
[
set size 3 set color yellow set shape "circle"
set creator-breed breed-picked
if verbose? [
print ( word " in hatch-objects, myself = " myself)
print ( word " in hatch-objects, self = " self)
]
let object-tag self ;; so this will persist into ask myself
hide-turtle
ask myself
[
if verbose? [
print ( word " in ask myself, self = " self )
print ( word " in ask myself, object-tag = " object-tag)
print ( word " who of object-tag " [who] of object-tag )
]
set my-list fput object-tag my-list
if verbose? [ print ( word "hatched an object, my-list is now " my-list)]
]
]
]
if verbose? [ ask objects [ print creator-breed]]
set count-type-a count objects with[ creator-breed = "A" ]
set count-type-b count objects with[ creator-breed = "B" ]
print ( word "count of objects created by engineersA is now " count-type-a)
print ( word "count of objects created by engineersB is now " count-type-b)
;; The following code works. I'm sure there are better ways to do it.
;; The interface has a plot called "plot 1"
;; Within that plot object, two pens were created, called "type-a" and "type-b"
;; and the pens were given different colors.
;; The check box for "show legend" was checked.
;; Everything else was left at default values. The plot commands were left blank.
set-current-plot "plot 1"
set-current-plot-pen "type-a"
plot count-type-a
set-current-plot-pen "type-b"
plot count-type-b
end
Upvotes: 3