diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index 79a4080..318191a 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -2,30 +2,28 @@ name: build-and-test
on:
push:
+ pull_request:
workflow_dispatch:
+env:
+ LIBERICA_URL: https://download.bell-sw.com/java/17.0.5+8/bellsoft-jdk17.0.5+8-linux-amd64-full.tar.gz
+
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- - uses: actions/checkout@v2
+ - uses: actions/checkout@v3
- - uses: olafurpg/setup-scala@v10
+ - uses: olafurpg/setup-scala@v13
with:
- java-version: adopt@1.8
+ java-version: liberica@17=tgz+${{ env.LIBERICA_URL }}
- - uses: actions/setup-python@v2
+ - uses: actions/setup-python@v4
with:
- python-version: '3.9'
+ python-version: '3.x'
- - name: Install Python Libraries
- run: |
- pip3 install numpy
- pip3 install sklearn
- sudo apt-get install python2
- curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py
- python2 get-pip.py
- pip2 install numpy
+ - name: Install Python 3 libraries
+ run: pip3 install numpy scikit-learn
- run: sbt -v update compile
- run: sbt -v test
diff --git a/.gitignore b/.gitignore
index 9585033..4e2cd66 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
+.bsp/
.bundledFiles
target/
extensions/
diff --git a/USING.md b/USING.md
index 1884df5..a383150 100644
--- a/USING.md
+++ b/USING.md
@@ -35,9 +35,9 @@ observer> py:run "print(min(patch_xs))"
See the documentation for each of the particular primitives for details on, for instance, how to multi-line statements and how object type conversions work.
See the demo models included in the `demo` folder for some examples of using libraries such as `numpy` and `tensorflow`.
-See the documentation on `py:set` to learn how to have the extension serialize entire agents and agentsets into Python dictionaries.
+See the documentation on `py:set` to learn how to have the extension serialize entire agents and agentsets into Python dictionaries.
-There is also a separate interactive Python console that can be found under Python > Interactive Python Console.
+There is also a separate interactive Python console that can be found under Python > Interactive Python Console.
This console is connected to the same Python session as all the Python NetLogo primitives, so you can define a variable in your model and access it in the interactive Python console window.
### Error handling
diff --git a/build.sbt b/build.sbt
index 4cd51ec..88e2de0 100644
--- a/build.sbt
+++ b/build.sbt
@@ -2,17 +2,17 @@ import org.nlogo.build.{ NetLogoExtension, ExtensionDocumentationPlugin }
enablePlugins(NetLogoExtension, ExtensionDocumentationPlugin)
-version := "0.5.1"
+name := "py"
+version := "1.0.1"
isSnapshot := true
-scalaVersion := "2.12.12"
-scalaSource in Test := baseDirectory.value / "src" / "test"
-scalaSource in Compile := baseDirectory.value / "src" / "main"
-scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature", "-Xfatal-warnings", "-Xlint")
+scalaVersion := "3.7.0"
+Test / scalaSource := baseDirectory.value / "src" / "test"
+Compile / scalaSource := baseDirectory.value / "src" / "main"
+scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature", "-Xfatal-warnings", "-release", "11")
-netLogoVersion := "6.2.2"
+netLogoVersion := "7.0.0-2486d1e" // This extension gets its NL version from language-library; any update to its NL version has to be mirrored in that package and published in a new version --Jason B. (5/5/25)
netLogoClassManager := "org.nlogo.extensions.py.PythonExtension"
-netLogoExtName := "py"
netLogoPackageExtras += (baseDirectory.value / "src" / "pyext.py", None)
Compile / packageBin / artifactPath := {
@@ -21,7 +21,10 @@ Compile / packageBin / artifactPath := {
newPath.toFile
}
-resolvers += "netlogo-language-library" at "https://dl.cloudsmith.io/public/netlogo/language-library/maven"
+resolvers ++= Seq(
+ "netlogo-language-library" at "https://dl.cloudsmith.io/public/netlogo/language-library/maven"
+)
+
libraryDependencies ++= Seq(
- "org.nlogo.languagelibrary" %% "language-library" % "1.1.0"
+ "org.nlogo.languagelibrary" %% "language-library" % "3.3.3-cfbf09b"
)
diff --git a/demos/Flocking Clusters.nlogo b/demos/Flocking Clusters.nlogo
deleted file mode 100644
index 5cc83bc..0000000
--- a/demos/Flocking Clusters.nlogo
+++ /dev/null
@@ -1,689 +0,0 @@
-extensions [ py ]
-
-turtles-own [
- flockmates ;; agentset of nearby turtles
- nearest-neighbor ;; closest one of our flockmates
-]
-
-to setup
- clear-all
- py:setup py:python
- (py:run
- "import numpy as np"
- "import sklearn.cluster as cl"
- )
- create-turtles population
- [ set color yellow - 2 + random 7 ;; random shades look nice
- set size 1.5 ;; easier to see
- setxy random-xcor random-ycor
- set flockmates no-turtles ]
- reset-ticks
-end
-
-to go
- ask turtles [ flock ]
- ;; the following line is used to make the turtles
- ;; animate more smoothly.
- repeat 5 [ ask turtles [ fd 0.2 ] display ]
- ;; for greater efficiency, at the expense of smooth
- ;; animation, substitute the following line instead:
- ;; ask turtles [ fd 1 ]
-
- py:set "coords" map [ t -> [ (list xcor ycor dx dy) ] of t ] sort turtles
- py:set "eps" vision
-
- let clusters py:runresult "cl.dbscan(np.array(coords), eps)"
- (foreach (sort turtles) (item 1 clusters) [ [t c] ->
- ask t [
- ifelse c = -1 [
- set color grey
- ][
- set color item ((c mod 13) + 1) base-colors
- ]
- ]
- ])
- tick
-end
-
-to flock ;; turtle procedure
- find-flockmates
- if any? flockmates
- [ find-nearest-neighbor
- ifelse distance nearest-neighbor < minimum-separation
- [ separate ]
- [ align
- cohere ] ]
-end
-
-to find-flockmates ;; turtle procedure
- set flockmates other turtles in-radius vision
-end
-
-to find-nearest-neighbor ;; turtle procedure
- set nearest-neighbor min-one-of flockmates [distance myself]
-end
-
-;;; SEPARATE
-
-to separate ;; turtle procedure
- turn-away ([heading] of nearest-neighbor) max-separate-turn
-end
-
-;;; ALIGN
-
-to align ;; turtle procedure
- turn-towards average-flockmate-heading max-align-turn
-end
-
-to-report average-flockmate-heading ;; turtle procedure
- ;; We can't just average the heading variables here.
- ;; For example, the average of 1 and 359 should be 0,
- ;; not 180. So we have to use trigonometry.
- let x-component sum [dx] of flockmates
- let y-component sum [dy] of flockmates
- ifelse x-component = 0 and y-component = 0
- [ report heading ]
- [ report atan x-component y-component ]
-end
-
-;;; COHERE
-
-to cohere ;; turtle procedure
- turn-towards average-heading-towards-flockmates max-cohere-turn
-end
-
-to-report average-heading-towards-flockmates ;; turtle procedure
- ;; "towards myself" gives us the heading from the other turtle
- ;; to me, but we want the heading from me to the other turtle,
- ;; so we add 180
- let x-component mean [sin (towards myself + 180)] of flockmates
- let y-component mean [cos (towards myself + 180)] of flockmates
- ifelse x-component = 0 and y-component = 0
- [ report heading ]
- [ report atan x-component y-component ]
-end
-
-;;; HELPER PROCEDURES
-
-to turn-towards [new-heading max-turn] ;; turtle procedure
- turn-at-most (subtract-headings new-heading heading) max-turn
-end
-
-to turn-away [new-heading max-turn] ;; turtle procedure
- turn-at-most (subtract-headings heading new-heading) max-turn
-end
-
-;; turn right by "turn" degrees (or left if "turn" is negative),
-;; but never turn more than "max-turn" degrees
-to turn-at-most [turn max-turn] ;; turtle procedure
- ifelse abs turn > max-turn
- [ ifelse turn > 0
- [ rt max-turn ]
- [ lt max-turn ] ]
- [ rt turn ]
-end
-
-
-; Copyright 1998 Uri Wilensky.
-; See Info tab for full copyright and license.
-@#$#@#$#@
-GRAPHICS-WINDOW
-250
-10
-755
-516
--1
--1
-7.0
-1
-10
-1
-1
-1
-0
-1
-1
-1
--35
-35
--35
-35
-1
-1
-1
-ticks
-30.0
-
-BUTTON
-39
-93
-116
-126
-NIL
-setup
-NIL
-1
-T
-OBSERVER
-NIL
-NIL
-NIL
-NIL
-1
-
-BUTTON
-122
-93
-203
-126
-NIL
-go
-T
-1
-T
-OBSERVER
-NIL
-NIL
-NIL
-NIL
-0
-
-SLIDER
-9
-51
-232
-84
-population
-population
-1.0
-1000.0
-300.0
-1.0
-1
-NIL
-HORIZONTAL
-
-SLIDER
-4
-217
-237
-250
-max-align-turn
-max-align-turn
-0.0
-20.0
-5.0
-0.25
-1
-degrees
-HORIZONTAL
-
-SLIDER
-4
-251
-237
-284
-max-cohere-turn
-max-cohere-turn
-0.0
-20.0
-3.0
-0.25
-1
-degrees
-HORIZONTAL
-
-SLIDER
-4
-285
-237
-318
-max-separate-turn
-max-separate-turn
-0.0
-20.0
-1.5
-0.25
-1
-degrees
-HORIZONTAL
-
-SLIDER
-9
-135
-232
-168
-vision
-vision
-0.0
-10.0
-3.0
-0.5
-1
-patches
-HORIZONTAL
-
-SLIDER
-9
-169
-232
-202
-minimum-separation
-minimum-separation
-0.0
-5.0
-1.0
-0.25
-1
-patches
-HORIZONTAL
-
-@#$#@#$#@
-## WHAT IS IT?
-
-This model is an attempt to mimic the flocking of birds. (The resulting motion also resembles schools of fish.) The flocks that appear in this model are not created or led in any way by special leader birds. Rather, each bird is following exactly the same set of rules, from which flocks emerge.
-
-## HOW IT WORKS
-
-The birds follow three rules: "alignment", "separation", and "cohesion".
-
-"Alignment" means that a bird tends to turn so that it is moving in the same direction that nearby birds are moving.
-
-"Separation" means that a bird will turn to avoid another bird which gets too close.
-
-"Cohesion" means that a bird will move towards other nearby birds (unless another bird is too close).
-
-When two birds are too close, the "separation" rule overrides the other two, which are deactivated until the minimum separation is achieved.
-
-The three rules affect only the bird's heading. Each bird always moves forward at the same constant speed.
-
-## HOW TO USE IT
-
-First, determine the number of birds you want in the simulation and set the POPULATION slider to that value. Press SETUP to create the birds, and press GO to have them start flying around.
-
-The default settings for the sliders will produce reasonably good flocking behavior. However, you can play with them to get variations:
-
-Three TURN-ANGLE sliders control the maximum angle a bird can turn as a result of each rule.
-
-VISION is the distance that each bird can see 360 degrees around it.
-
-## THINGS TO NOTICE
-
-Central to the model is the observation that flocks form without a leader.
-
-There are no random numbers used in this model, except to position the birds initially. The fluid, lifelike behavior of the birds is produced entirely by deterministic rules.
-
-Also, notice that each flock is dynamic. A flock, once together, is not guaranteed to keep all of its members. Why do you think this is?
-
-After running the model for a while, all of the birds have approximately the same heading. Why?
-
-Sometimes a bird breaks away from its flock. How does this happen? You may need to slow down the model or run it step by step in order to observe this phenomenon.
-
-## THINGS TO TRY
-
-Play with the sliders to see if you can get tighter flocks, looser flocks, fewer flocks, more flocks, more or less splitting and joining of flocks, more or less rearranging of birds within flocks, etc.
-
-You can turn off a rule entirely by setting that rule's angle slider to zero. Is one rule by itself enough to produce at least some flocking? What about two rules? What's missing from the resulting behavior when you leave out each rule?
-
-Will running the model for a long time produce a static flock? Or will the birds never settle down to an unchanging formation? Remember, there are no random numbers used in this model.
-
-## EXTENDING THE MODEL
-
-Currently the birds can "see" all around them. What happens if birds can only see in front of them? The `in-cone` primitive can be used for this.
-
-Is there some way to get V-shaped flocks, like migrating geese?
-
-What happens if you put walls around the edges of the world that the birds can't fly into?
-
-Can you get the birds to fly around obstacles in the middle of the world?
-
-What would happen if you gave the birds different velocities? For example, you could make birds that are not near other birds fly faster to catch up to the flock. Or, you could simulate the diminished air resistance that birds experience when flying together by making them fly faster when in a group.
-
-Are there other interesting ways you can make the birds different from each other? There could be random variation in the population, or you could have distinct "species" of bird.
-
-## NETLOGO FEATURES
-
-Notice the need for the `subtract-headings` primitive and special procedure for averaging groups of headings. Just subtracting the numbers, or averaging the numbers, doesn't give you the results you'd expect, because of the discontinuity where headings wrap back to 0 once they reach 360.
-
-## RELATED MODELS
-
-* Moths
-* Flocking Vee Formation
-* Flocking - Alternative Visualizations
-
-## CREDITS AND REFERENCES
-
-This model is inspired by the Boids simulation invented by Craig Reynolds. The algorithm we use here is roughly similar to the original Boids algorithm, but it is not the same. The exact details of the algorithm tend not to matter very much -- as long as you have alignment, separation, and cohesion, you will usually get flocking behavior resembling that produced by Reynolds' original model. Information on Boids is available at http://www.red3d.com/cwr/boids/.
-
-## HOW TO CITE
-
-If you mention this model or the NetLogo software in a publication, we ask that you include the citations below.
-
-For the model itself:
-
-* Wilensky, U. (1998). NetLogo Flocking model. http://ccl.northwestern.edu/netlogo/models/Flocking. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.
-
-Please cite the NetLogo software as:
-
-* Wilensky, U. (1999). NetLogo. http://ccl.northwestern.edu/netlogo/. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.
-
-## COPYRIGHT AND LICENSE
-
-Copyright 1998 Uri Wilensky.
-
-
-
-This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit https://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
-
-Commercial licenses are also available. To inquire about commercial licenses, please contact Uri Wilensky at uri@northwestern.edu.
-
-This model was created as part of the project: CONNECTED MATHEMATICS: MAKING SENSE OF COMPLEX PHENOMENA THROUGH BUILDING OBJECT-BASED PARALLEL MODELS (OBPML). The project gratefully acknowledges the support of the National Science Foundation (Applications of Advanced Technologies Program) -- grant numbers RED #9552950 and REC #9632612.
-
-This model was converted to NetLogo as part of the projects: PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN CLASSROOMS and/or INTEGRATED SIMULATION AND MODELING ENVIRONMENT. The project gratefully acknowledges the support of the National Science Foundation (REPP & ROLE programs) -- grant numbers REC #9814682 and REC-0126227. Converted from StarLogoT to NetLogo, 2002.
-
-
-@#$#@#$#@
-default
-true
-0
-Polygon -7500403 true true 150 5 40 250 150 205 260 250
-
-airplane
-true
-0
-Polygon -7500403 true true 150 0 135 15 120 60 120 105 15 165 15 195 120 180 135 240 105 270 120 285 150 270 180 285 210 270 165 240 180 180 285 195 285 165 180 105 180 60 165 15
-
-arrow
-true
-0
-Polygon -7500403 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150
-
-box
-false
-0
-Polygon -7500403 true true 150 285 285 225 285 75 150 135
-Polygon -7500403 true true 150 135 15 75 150 15 285 75
-Polygon -7500403 true true 15 75 15 225 150 285 150 135
-Line -16777216 false 150 285 150 135
-Line -16777216 false 150 135 15 75
-Line -16777216 false 150 135 285 75
-
-bug
-true
-0
-Circle -7500403 true true 96 182 108
-Circle -7500403 true true 110 127 80
-Circle -7500403 true true 110 75 80
-Line -7500403 true 150 100 80 30
-Line -7500403 true 150 100 220 30
-
-butterfly
-true
-0
-Polygon -7500403 true true 150 165 209 199 225 225 225 255 195 270 165 255 150 240
-Polygon -7500403 true true 150 165 89 198 75 225 75 255 105 270 135 255 150 240
-Polygon -7500403 true true 139 148 100 105 55 90 25 90 10 105 10 135 25 180 40 195 85 194 139 163
-Polygon -7500403 true true 162 150 200 105 245 90 275 90 290 105 290 135 275 180 260 195 215 195 162 165
-Polygon -16777216 true false 150 255 135 225 120 150 135 120 150 105 165 120 180 150 165 225
-Circle -16777216 true false 135 90 30
-Line -16777216 false 150 105 195 60
-Line -16777216 false 150 105 105 60
-
-car
-false
-0
-Polygon -7500403 true true 300 180 279 164 261 144 240 135 226 132 213 106 203 84 185 63 159 50 135 50 75 60 0 150 0 165 0 225 300 225 300 180
-Circle -16777216 true false 180 180 90
-Circle -16777216 true false 30 180 90
-Polygon -16777216 true false 162 80 132 78 134 135 209 135 194 105 189 96 180 89
-Circle -7500403 true true 47 195 58
-Circle -7500403 true true 195 195 58
-
-circle
-false
-0
-Circle -7500403 true true 0 0 300
-
-circle 2
-false
-0
-Circle -7500403 true true 0 0 300
-Circle -16777216 true false 30 30 240
-
-cow
-false
-0
-Polygon -7500403 true true 200 193 197 249 179 249 177 196 166 187 140 189 93 191 78 179 72 211 49 209 48 181 37 149 25 120 25 89 45 72 103 84 179 75 198 76 252 64 272 81 293 103 285 121 255 121 242 118 224 167
-Polygon -7500403 true true 73 210 86 251 62 249 48 208
-Polygon -7500403 true true 25 114 16 195 9 204 23 213 25 200 39 123
-
-cylinder
-false
-0
-Circle -7500403 true true 0 0 300
-
-dot
-false
-0
-Circle -7500403 true true 90 90 120
-
-face happy
-false
-0
-Circle -7500403 true true 8 8 285
-Circle -16777216 true false 60 75 60
-Circle -16777216 true false 180 75 60
-Polygon -16777216 true false 150 255 90 239 62 213 47 191 67 179 90 203 109 218 150 225 192 218 210 203 227 181 251 194 236 217 212 240
-
-face neutral
-false
-0
-Circle -7500403 true true 8 7 285
-Circle -16777216 true false 60 75 60
-Circle -16777216 true false 180 75 60
-Rectangle -16777216 true false 60 195 240 225
-
-face sad
-false
-0
-Circle -7500403 true true 8 8 285
-Circle -16777216 true false 60 75 60
-Circle -16777216 true false 180 75 60
-Polygon -16777216 true false 150 168 90 184 62 210 47 232 67 244 90 220 109 205 150 198 192 205 210 220 227 242 251 229 236 206 212 183
-
-fish
-false
-0
-Polygon -1 true false 44 131 21 87 15 86 0 120 15 150 0 180 13 214 20 212 45 166
-Polygon -1 true false 135 195 119 235 95 218 76 210 46 204 60 165
-Polygon -1 true false 75 45 83 77 71 103 86 114 166 78 135 60
-Polygon -7500403 true true 30 136 151 77 226 81 280 119 292 146 292 160 287 170 270 195 195 210 151 212 30 166
-Circle -16777216 true false 215 106 30
-
-flag
-false
-0
-Rectangle -7500403 true true 60 15 75 300
-Polygon -7500403 true true 90 150 270 90 90 30
-Line -7500403 true 75 135 90 135
-Line -7500403 true 75 45 90 45
-
-flower
-false
-0
-Polygon -10899396 true false 135 120 165 165 180 210 180 240 150 300 165 300 195 240 195 195 165 135
-Circle -7500403 true true 85 132 38
-Circle -7500403 true true 130 147 38
-Circle -7500403 true true 192 85 38
-Circle -7500403 true true 85 40 38
-Circle -7500403 true true 177 40 38
-Circle -7500403 true true 177 132 38
-Circle -7500403 true true 70 85 38
-Circle -7500403 true true 130 25 38
-Circle -7500403 true true 96 51 108
-Circle -16777216 true false 113 68 74
-Polygon -10899396 true false 189 233 219 188 249 173 279 188 234 218
-Polygon -10899396 true false 180 255 150 210 105 210 75 240 135 240
-
-house
-false
-0
-Rectangle -7500403 true true 45 120 255 285
-Rectangle -16777216 true false 120 210 180 285
-Polygon -7500403 true true 15 120 150 15 285 120
-Line -16777216 false 30 120 270 120
-
-leaf
-false
-0
-Polygon -7500403 true true 150 210 135 195 120 210 60 210 30 195 60 180 60 165 15 135 30 120 15 105 40 104 45 90 60 90 90 105 105 120 120 120 105 60 120 60 135 30 150 15 165 30 180 60 195 60 180 120 195 120 210 105 240 90 255 90 263 104 285 105 270 120 285 135 240 165 240 180 270 195 240 210 180 210 165 195
-Polygon -7500403 true true 135 195 135 240 120 255 105 255 105 285 135 285 165 240 165 195
-
-line
-true
-0
-Line -7500403 true 150 0 150 300
-
-line half
-true
-0
-Line -7500403 true 150 0 150 150
-
-pentagon
-false
-0
-Polygon -7500403 true true 150 15 15 120 60 285 240 285 285 120
-
-person
-false
-0
-Circle -7500403 true true 110 5 80
-Polygon -7500403 true true 105 90 120 195 90 285 105 300 135 300 150 225 165 300 195 300 210 285 180 195 195 90
-Rectangle -7500403 true true 127 79 172 94
-Polygon -7500403 true true 195 90 240 150 225 180 165 105
-Polygon -7500403 true true 105 90 60 150 75 180 135 105
-
-plant
-false
-0
-Rectangle -7500403 true true 135 90 165 300
-Polygon -7500403 true true 135 255 90 210 45 195 75 255 135 285
-Polygon -7500403 true true 165 255 210 210 255 195 225 255 165 285
-Polygon -7500403 true true 135 180 90 135 45 120 75 180 135 210
-Polygon -7500403 true true 165 180 165 210 225 180 255 120 210 135
-Polygon -7500403 true true 135 105 90 60 45 45 75 105 135 135
-Polygon -7500403 true true 165 105 165 135 225 105 255 45 210 60
-Polygon -7500403 true true 135 90 120 45 150 15 180 45 165 90
-
-square
-false
-0
-Rectangle -7500403 true true 30 30 270 270
-
-square 2
-false
-0
-Rectangle -7500403 true true 30 30 270 270
-Rectangle -16777216 true false 60 60 240 240
-
-star
-false
-0
-Polygon -7500403 true true 151 1 185 108 298 108 207 175 242 282 151 216 59 282 94 175 3 108 116 108
-
-target
-false
-0
-Circle -7500403 true true 0 0 300
-Circle -16777216 true false 30 30 240
-Circle -7500403 true true 60 60 180
-Circle -16777216 true false 90 90 120
-Circle -7500403 true true 120 120 60
-
-tree
-false
-0
-Circle -7500403 true true 118 3 94
-Rectangle -6459832 true false 120 195 180 300
-Circle -7500403 true true 65 21 108
-Circle -7500403 true true 116 41 127
-Circle -7500403 true true 45 90 120
-Circle -7500403 true true 104 74 152
-
-triangle
-false
-0
-Polygon -7500403 true true 150 30 15 255 285 255
-
-triangle 2
-false
-0
-Polygon -7500403 true true 150 30 15 255 285 255
-Polygon -16777216 true false 151 99 225 223 75 224
-
-truck
-false
-0
-Rectangle -7500403 true true 4 45 195 187
-Polygon -7500403 true true 296 193 296 150 259 134 244 104 208 104 207 194
-Rectangle -1 true false 195 60 195 105
-Polygon -16777216 true false 238 112 252 141 219 141 218 112
-Circle -16777216 true false 234 174 42
-Rectangle -7500403 true true 181 185 214 194
-Circle -16777216 true false 144 174 42
-Circle -16777216 true false 24 174 42
-Circle -7500403 false true 24 174 42
-Circle -7500403 false true 144 174 42
-Circle -7500403 false true 234 174 42
-
-turtle
-true
-0
-Polygon -10899396 true false 215 204 240 233 246 254 228 266 215 252 193 210
-Polygon -10899396 true false 195 90 225 75 245 75 260 89 269 108 261 124 240 105 225 105 210 105
-Polygon -10899396 true false 105 90 75 75 55 75 40 89 31 108 39 124 60 105 75 105 90 105
-Polygon -10899396 true false 132 85 134 64 107 51 108 17 150 2 192 18 192 52 169 65 172 87
-Polygon -10899396 true false 85 204 60 233 54 254 72 266 85 252 107 210
-Polygon -7500403 true true 119 75 179 75 209 101 224 135 220 225 175 261 128 261 81 224 74 135 88 99
-
-wheel
-false
-0
-Circle -7500403 true true 3 3 294
-Circle -16777216 true false 30 30 240
-Line -7500403 true 150 285 150 15
-Line -7500403 true 15 150 285 150
-Circle -7500403 true true 120 120 60
-Line -7500403 true 216 40 79 269
-Line -7500403 true 40 84 269 221
-Line -7500403 true 40 216 269 79
-Line -7500403 true 84 40 221 269
-
-x
-false
-0
-Polygon -7500403 true true 270 75 225 30 30 225 75 270
-Polygon -7500403 true true 30 75 75 30 270 225 225 270
-@#$#@#$#@
-NetLogo 6.2.2
-@#$#@#$#@
-set population 200
-setup
-repeat 200 [ go ]
-@#$#@#$#@
-@#$#@#$#@
-@#$#@#$#@
-@#$#@#$#@
-default
-0.0
--0.2 0 0.0 1.0
-0.0 1 1.0 0.0
-0.2 0 0.0 1.0
-link direction
-true
-0
-Line -7500403 true 150 150 90 180
-Line -7500403 true 150 150 210 180
-@#$#@#$#@
-0
-@#$#@#$#@
diff --git a/demos/Flocking Clusters.nlogox b/demos/Flocking Clusters.nlogox
new file mode 100644
index 0000000..4333b24
--- /dev/null
+++ b/demos/Flocking Clusters.nlogox
@@ -0,0 +1,957 @@
+
+
+ [ (list xcor ycor dx dy) ] of t ] sort turtles
+ py:set "eps" vision
+
+ let clusters py:runresult "cl.dbscan(np.array(coords), eps)"
+ (foreach (sort turtles) (item 1 clusters) [ [t c] ->
+ ask t [
+ ifelse c = -1 [
+ set color grey
+ ][
+ set color item ((c mod 13) + 1) base-colors
+ ]
+ ]
+ ])
+ tick
+end
+
+to flock ;; turtle procedure
+ find-flockmates
+ if any? flockmates
+ [ find-nearest-neighbor
+ ifelse distance nearest-neighbor < minimum-separation
+ [ separate ]
+ [ align
+ cohere ] ]
+end
+
+to find-flockmates ;; turtle procedure
+ set flockmates other turtles in-radius vision
+end
+
+to find-nearest-neighbor ;; turtle procedure
+ set nearest-neighbor min-one-of flockmates [distance myself]
+end
+
+;;; SEPARATE
+
+to separate ;; turtle procedure
+ turn-away ([heading] of nearest-neighbor) max-separate-turn
+end
+
+;;; ALIGN
+
+to align ;; turtle procedure
+ turn-towards average-flockmate-heading max-align-turn
+end
+
+to-report average-flockmate-heading ;; turtle procedure
+ ;; We can't just average the heading variables here.
+ ;; For example, the average of 1 and 359 should be 0,
+ ;; not 180. So we have to use trigonometry.
+ let x-component sum [dx] of flockmates
+ let y-component sum [dy] of flockmates
+ ifelse x-component = 0 and y-component = 0
+ [ report heading ]
+ [ report atan x-component y-component ]
+end
+
+;;; COHERE
+
+to cohere ;; turtle procedure
+ turn-towards average-heading-towards-flockmates max-cohere-turn
+end
+
+to-report average-heading-towards-flockmates ;; turtle procedure
+ ;; "towards myself" gives us the heading from the other turtle
+ ;; to me, but we want the heading from me to the other turtle,
+ ;; so we add 180
+ let x-component mean [sin (towards myself + 180)] of flockmates
+ let y-component mean [cos (towards myself + 180)] of flockmates
+ ifelse x-component = 0 and y-component = 0
+ [ report heading ]
+ [ report atan x-component y-component ]
+end
+
+;;; HELPER PROCEDURES
+
+to turn-towards [new-heading max-turn] ;; turtle procedure
+ turn-at-most (subtract-headings new-heading heading) max-turn
+end
+
+to turn-away [new-heading max-turn] ;; turtle procedure
+ turn-at-most (subtract-headings heading new-heading) max-turn
+end
+
+;; turn right by "turn" degrees (or left if "turn" is negative),
+;; but never turn more than "max-turn" degrees
+to turn-at-most [turn max-turn] ;; turtle procedure
+ ifelse abs turn > max-turn
+ [ ifelse turn > 0
+ [ rt max-turn ]
+ [ lt max-turn ] ]
+ [ rt turn ]
+end
+
+
+; Copyright 1998 Uri Wilensky.
+; See Info tab for full copyright and license.]]>
+
+
+
+
+
+
+
+
+
+
+
+ ]]>
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ set population 200
+setup
+repeat 200 [ go ]
+
diff --git a/demos/Traffic Basic - Reinforcement.nlogo b/demos/Traffic Basic - Reinforcement.nlogo
deleted file mode 100644
index 002594c..0000000
--- a/demos/Traffic Basic - Reinforcement.nlogo
+++ /dev/null
@@ -1,934 +0,0 @@
-extensions [ py ]
-
-globals [
- sample-car
- speed-limit
- speed-min
- inputs
- loss
-]
-
-turtles-own [
- speed
- reward
- state
- action ; 0 = decelerate, 1 = stay same, 2 = accelerate
- next-state
-]
-
-to setup-tf
- py:setup py:python
- (py:run
- "import numpy as np"
- "from keras.models import Sequential"
- "from keras.layers.core import Dense"
- "from keras import optimizers"
- )
- setup
-end
-
-to setup
- clear-all
-
- set inputs (list
- [-> speed]
- [-> distance next-car]
- [-> [speed] of next-car]
- )
-
- if fp-exp? [
- set inputs lput [-> exp-rate ] inputs
- ]
- if fp-ticks? [
- set inputs lput [-> ticks] inputs
- ]
-
- py:set "state_dims" length inputs
- py:set "hl_size" 36
- py:set "num_actions" 3
- py:set "memory_size" memory-size
- py:set "batch_size" batch-size
- py:set "lr" learning-rate
-
- (py:run
- "model = Sequential()"
- "model.add(Dense(hl_size, input_shape=(state_dims,), activation='relu'))"
- "model.add(Dense(hl_size, activation='relu'))"
- "model.add(Dense(hl_size, activation='relu'))"
- "model.add(Dense(num_actions))"
- "optimizer = optimizers.adam(lr=lr)"
- "model.compile(optimizer, 'mse')"
- "model.summary()"
- "memory = []")
-
- ask patches [ setup-road ]
- set speed-limit 1
- set speed-min 0
- setup-cars
- reset-ticks
-end
-
-to setup-road ;; patch procedure
- if pycor < 2 and pycor > -2 [ set pcolor white ]
-end
-
-to setup-cars
- if number-of-cars > world-width [
- user-message (word
- "There are too many cars for the amount of road. "
- "Please decrease the NUMBER-OF-CARS slider to below "
- (world-width + 1) " and press the SETUP button again. "
- "The setup has stopped.")
- stop
- ]
- set-default-shape turtles "car"
- create-turtles number-of-cars [
- set color blue
- set xcor random-xcor
- set heading 90
- ;; set initial speed to be in range 0.1 to 1.0
- set speed 0.1 + random-float 0.9
- separate-cars
- ]
- set sample-car one-of turtles
- ask sample-car [ set color red ]
-end
-
-; this procedure is needed so when we click "Setup" we
-; don't end up with any two cars on the same patch
-to separate-cars ;; turtle procedure
- if any? other turtles-here [
- fd 1
- separate-cars
- ]
-end
-
-to go
- py:set "discount" discount
- ;; if there is a car right ahead of you, match its speed then slow down
- select-actions
- ask turtles [
- if action = 0 [ decelerate set color red ]
- if action = 1 [ set color yellow ]
- if action = 2 [ accelerate set color green]
- ;; don't slow down below speed minimum or speed up beyond speed limit
- if distance next-car < 1 + speed [ slow-down-car next-car ]
- if speed < speed-min [ set speed speed-min ]
- if speed > speed-limit [ set speed speed-limit ]
- fd speed
- set reward (log (speed + 1e-8) 2)
- ;set reward speed
- ]
- if train? [
- remember
- train
- ]
- tick
-end
-
-to select-actions
- ask turtles [ set state map runresult inputs ]
- let turtle-list sort turtles
- py:set "states" map [ t -> [ state ] of t ] turtle-list
- let actions py:runresult "np.argmax(model.predict(np.array(states)), axis = 1)"
-
- (foreach turtle-list actions [ [t a] ->
- ask t [
- ifelse random-float 1 < exp-rate [
- set action random 3
- ] [
- set action a
- ]
- ]
- ])
-end
-
-to-report exp-rate
- report exploration-rate / (1 + exploration-decay-rate * ticks)
-end
-
-to remember
- ask turtles [ set next-state map runresult inputs ]
- let data [ (list state action reward next-state) ] of turtles
- py:set "new_exp" data
- (py:run
- "memory.extend(new_exp)"
- "if len(memory) > memory_size:"
- " memory = memory[-memory_size:]")
-end
-
-to train
- (py:run
- "sample_ix = np.random.randint(len(memory), size = batch_size)"
- "inputs = np.array([memory[i][0] for i in sample_ix])"
- "actions = np.array([memory[i][1] for i in sample_ix])"
- "rewards = np.array([memory[i][2] for i in sample_ix])"
- "next_states = np.array([memory[i][3] for i in sample_ix])"
- "targets = model.predict(inputs)"
- "next_state_rewards = model.predict(next_states)"
- "next_state_qs = np.max(next_state_rewards, axis = 1)"
- "targets[np.arange(targets.shape[0]), actions] = rewards + discount * next_state_qs"
- "model.train_on_batch(inputs, targets)"
- )
-
-end
-
-to-report next-car
- let here min-one-of turtles-here with [ xcor > [ xcor ] of myself ] [ distance myself ]
- if here != nobody [ report here ]
- let i 1
- while [ not any? turtles-on patch-ahead i ] [
- set i i + 1
- ]
- report min-one-of turtles-on patch-ahead i [ distance myself ]
-end
-
-
-to slow-down-car [ car-ahead ] ;; turtle procedure
- ;; slow down so you are driving more slowly than the car ahead of you
- set speed [ speed ] of car-ahead - stop-penalty
-end
-
-to accelerate
- set speed speed + acceleration
-end
-
-to decelerate
- set speed speed - deceleration
-end
-
-; Copyright 1997 Uri Wilensky.
-; See Info tab for full copyright and license.
-@#$#@#$#@
-GRAPHICS-WINDOW
-15
-480
-703
-609
--1
--1
-13.33333333333334
-1
-10
-1
-1
-1
-0
-1
-0
-1
--25
-25
--4
-4
-1
-1
-1
-ticks
-30.0
-
-BUTTON
-110
-190
-182
-231
-NIL
-setup
-NIL
-1
-T
-OBSERVER
-NIL
-NIL
-NIL
-NIL
-1
-
-BUTTON
-193
-191
-264
-231
-NIL
-go
-T
-1
-T
-OBSERVER
-NIL
-NIL
-NIL
-NIL
-0
-
-SLIDER
-15
-15
-265
-48
-number-of-cars
-number-of-cars
-1
-41
-20.0
-1
-1
-NIL
-HORIZONTAL
-
-SLIDER
-15
-270
-265
-303
-deceleration
-deceleration
-0
-.0099
-0.0045
-.0001
-1
-NIL
-HORIZONTAL
-
-SLIDER
-15
-235
-265
-268
-acceleration
-acceleration
-0
-.0099
-0.0044
-.0001
-1
-NIL
-HORIZONTAL
-
-PLOT
-285
-15
-705
-212
-Car speeds
-time
-speed
-0.0
-300.0
-0.0
-1.0
-true
-false
-"" ""
-PENS
-"sample car" 1.0 0 -2674135 true "" "plot [speed] of sample-car"
-"min speed" 1.0 0 -13345367 true "" "plot min [speed] of turtles"
-"max speed" 1.0 0 -10899396 true "" "plot max [speed] of turtles"
-
-BUTTON
-15
-190
-97
-230
-NIL
-setup-tf
-NIL
-1
-T
-OBSERVER
-NIL
-NIL
-NIL
-NIL
-1
-
-SLIDER
-15
-410
-265
-443
-discount
-discount
-0
-1
-0.9
-0.01
-1
-NIL
-HORIZONTAL
-
-SLIDER
-15
-340
-265
-373
-exploration-rate
-exploration-rate
-0
-1
-1.0
-0.01
-1
-NIL
-HORIZONTAL
-
-PLOT
-285
-210
-705
-405
-selected-actions
-NIL
-NIL
-0.0
-300.0
-0.0
-20.0
-true
-false
-"set-plot-y-range 0 number-of-cars" ""
-PENS
-"default" 1.0 0 -2674135 true "" "plot count turtles with [ action = 0 ]"
-"pen-1" 1.0 0 -1184463 true "" "plot count turtles with [ action = 1 ]"
-"pen-2" 1.0 0 -10899396 true "" "plot count turtles with [ action = 2 ]"
-
-SLIDER
-15
-305
-265
-338
-stop-penalty
-stop-penalty
-0
-.099
-0.027
-0.001
-1
-NIL
-HORIZONTAL
-
-SWITCH
-15
-445
-117
-478
-train?
-train?
-0
-1
--1000
-
-SLIDER
-15
-50
-265
-83
-memory-size
-memory-size
-0
-1000000
-10000.0
-1000
-1
-NIL
-HORIZONTAL
-
-SLIDER
-15
-85
-265
-118
-batch-size
-batch-size
-0
-1024
-128.0
-32
-1
-NIL
-HORIZONTAL
-
-SLIDER
-15
-120
-265
-153
-learning-rate
-learning-rate
-0
-0.01
-0.001
-0.0001
-1
-NIL
-HORIZONTAL
-
-SLIDER
-15
-375
-265
-408
-exploration-decay-rate
-exploration-decay-rate
-0
-0.1
-0.01
-0.001
-1
-NIL
-HORIZONTAL
-
-MONITOR
-285
-405
-352
-450
-NIL
-exp-rate
-4
-1
-11
-
-SWITCH
-15
-155
-140
-188
-fp-exp?
-fp-exp?
-0
-1
--1000
-
-SWITCH
-145
-155
-265
-188
-fp-ticks?
-fp-ticks?
-1
-1
--1000
-
-@#$#@#$#@
-## WHAT IS IT?
-
-This model models the movement of cars on a highway. Each car follows a simple set of rules: it slows down (decelerates) if it sees a car close ahead, and speeds up (accelerates) if it doesn't see a car ahead. The model demonstrates how traffic jams can form even without any accidents, broken bridges, or overturned trucks. No "centralized cause" is needed for a traffic jam to form.
-
-## HOW TO USE IT
-
-Click on the SETUP button to set up the cars.
-
-Set the NUMBER-OF-CARS slider to change the number of cars on the road.
-
-Click on GO to start the cars moving. Note that they wrap around the world as they move, so the road is like a continuous loop.
-
-The ACCELERATION slider controls the rate at which cars accelerate (speed up) when there are no cars ahead.
-
-When a car sees another car right in front, it matches that car's speed and then slows down a bit more. How much slower it goes than the car in front of it is controlled by the DECELERATION slider.
-
-## THINGS TO NOTICE
-
-Traffic jams can start from small "seeds." These cars start with random positions and random speeds. If some cars are clustered together, they will move slowly, causing cars behind them to slow down, and a traffic jam forms.
-
-Even though all of the cars are moving forward, the traffic jams tend to move backwards. This behavior is common in wave phenomena: the behavior of the group is often very different from the behavior of the individuals that make up the group.
-
-The plot shows three values as the model runs:
-
-* the fastest speed of any car (this doesn't exceed the speed limit!)
-
-* the slowest speed of any car
-
-* the speed of a single car (turtle 0), painted red so it can be watched.
-
-Notice not only the maximum and minimum, but also the variability -- the "jerkiness" of one vehicle.
-
-Notice that the default settings have cars decelerating much faster than they accelerate. This is typical of traffic flow models.
-
-Even though both ACCELERATION and DECELERATION are very small, the cars can achieve high speeds as these values are added or subtracted at each tick.
-
-## THINGS TO TRY
-
-In this model there are three sliders that can affect the tendency to create traffic jams: the initial NUMBER-OF-CARS, ACCELERATION, and DECELERATION.
-
-Look for patterns in how these settings affect the traffic flow. Which variable has the greatest effect? Do the patterns make sense? Do they seem to be consistent with your driving experiences?
-
-Set DECELERATION to zero. What happens to the flow? Gradually increase DECELERATION while the model runs. At what point does the flow "break down"?
-
-## EXTENDING THE MODEL
-
-Try other rules for speeding up and slowing down. Is the rule presented here realistic? Are there other rules that are more accurate or represent better driving strategies?
-
-In reality, different vehicles may follow different rules. Try giving different rules or ACCELERATION/DECELERATION values to some of the cars. Can one bad driver mess things up?
-
-The asymmetry between acceleration and deceleration is a simplified representation of different driving habits and response times. Can you explicitly encode these into the model?
-
-What could you change to minimize the chances of traffic jams forming?
-
-What could you change to make traffic jams move forward rather than backward?
-
-Make a model of two-lane traffic.
-
-## NETLOGO FEATURES
-
-The plot shows both global values and the value for a single car, which helps one watch overall patterns and individual behavior at the same time.
-
-The `watch` command is used to make it easier to focus on the red car.
-
-The `speed-limit` and `speed-min` variables are set to constant values. Since they are the same for every car, these variables could have been defined as globals rather than turtle variables. We have specified them as turtle variables since modifications or extensions to this model might well have every car with its own speed-limit values.
-
-## RELATED MODELS
-
-- "Traffic Basic Utility": a version of "Traffic Basic" including a utility function for the cars.
-
-- "Traffic Basic Adaptive": a version of "Traffic Basic" where cars adapt their acceleration to try and maintain a smooth flow of traffic.
-
-- "Traffic Basic Adaptive Individuals": a version of "Traffic Basic Adaptive" where each car adapts individually, instead of all cars adapting in unison.
-
-- "Traffic 2 Lanes": a more sophisticated two-lane version of the "Traffic Basic" model.
-
-- "Traffic Intersection": a model of cars traveling through a single intersection.
-
-- "Traffic Grid": a model of traffic moving in a city grid, with stoplights at the intersections.
-
-- "Traffic Grid Goal": a version of "Traffic Grid" where the cars have goals, namely to drive to and from work.
-
-- "Gridlock HubNet": a version of "Traffic Grid" where students control traffic lights in real-time.
-
-- "Gridlock Alternate HubNet": a version of "Gridlock HubNet" where students can enter NetLogo code to plot custom metrics.
-
-## HOW TO CITE
-
-If you mention this model or the NetLogo software in a publication, we ask that you include the citations below.
-
-For the model itself:
-
-* Wilensky, U. (1997). NetLogo Traffic Basic model. http://ccl.northwestern.edu/netlogo/models/TrafficBasic. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.
-
-Please cite the NetLogo software as:
-
-* Wilensky, U. (1999). NetLogo. http://ccl.northwestern.edu/netlogo/. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.
-
-## COPYRIGHT AND LICENSE
-
-Copyright 1997 Uri Wilensky.
-
-
-
-This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit https://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
-
-Commercial licenses are also available. To inquire about commercial licenses, please contact Uri Wilensky at uri@northwestern.edu.
-
-This model was created as part of the project: CONNECTED MATHEMATICS: MAKING SENSE OF COMPLEX PHENOMENA THROUGH BUILDING OBJECT-BASED PARALLEL MODELS (OBPML). The project gratefully acknowledges the support of the National Science Foundation (Applications of Advanced Technologies Program) -- grant numbers RED #9552950 and REC #9632612.
-
-This model was developed at the MIT Media Lab using CM StarLogo. See Resnick, M. (1994) "Turtles, Termites and Traffic Jams: Explorations in Massively Parallel Microworlds." Cambridge, MA: MIT Press. Adapted to StarLogoT, 1997, as part of the Connected Mathematics Project.
-
-This model was converted to NetLogo as part of the projects: PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN CLASSROOMS and/or INTEGRATED SIMULATION AND MODELING ENVIRONMENT. The project gratefully acknowledges the support of the National Science Foundation (REPP & ROLE programs) -- grant numbers REC #9814682 and REC-0126227. Converted from StarLogoT to NetLogo, 2001.
-
-
-@#$#@#$#@
-default
-true
-0
-Polygon -7500403 true true 150 5 40 250 150 205 260 250
-
-airplane
-true
-0
-Polygon -7500403 true true 150 0 135 15 120 60 120 105 15 165 15 195 120 180 135 240 105 270 120 285 150 270 180 285 210 270 165 240 180 180 285 195 285 165 180 105 180 60 165 15
-
-arrow
-true
-0
-Polygon -7500403 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150
-
-box
-false
-0
-Polygon -7500403 true true 150 285 285 225 285 75 150 135
-Polygon -7500403 true true 150 135 15 75 150 15 285 75
-Polygon -7500403 true true 15 75 15 225 150 285 150 135
-Line -16777216 false 150 285 150 135
-Line -16777216 false 150 135 15 75
-Line -16777216 false 150 135 285 75
-
-bug
-true
-0
-Circle -7500403 true true 96 182 108
-Circle -7500403 true true 110 127 80
-Circle -7500403 true true 110 75 80
-Line -7500403 true 150 100 80 30
-Line -7500403 true 150 100 220 30
-
-butterfly
-true
-0
-Polygon -7500403 true true 150 165 209 199 225 225 225 255 195 270 165 255 150 240
-Polygon -7500403 true true 150 165 89 198 75 225 75 255 105 270 135 255 150 240
-Polygon -7500403 true true 139 148 100 105 55 90 25 90 10 105 10 135 25 180 40 195 85 194 139 163
-Polygon -7500403 true true 162 150 200 105 245 90 275 90 290 105 290 135 275 180 260 195 215 195 162 165
-Polygon -16777216 true false 150 255 135 225 120 150 135 120 150 105 165 120 180 150 165 225
-Circle -16777216 true false 135 90 30
-Line -16777216 false 150 105 195 60
-Line -16777216 false 150 105 105 60
-
-car
-false
-0
-Polygon -7500403 true true 300 180 279 164 261 144 240 135 226 132 213 106 203 84 185 63 159 50 135 50 75 60 0 150 0 165 0 225 300 225 300 180
-Circle -16777216 true false 180 180 90
-Circle -16777216 true false 30 180 90
-Polygon -16777216 true false 162 80 132 78 134 135 209 135 194 105 189 96 180 89
-Circle -7500403 true true 47 195 58
-Circle -7500403 true true 195 195 58
-
-circle
-false
-0
-Circle -7500403 true true 0 0 300
-
-circle 2
-false
-0
-Circle -7500403 true true 0 0 300
-Circle -16777216 true false 30 30 240
-
-cow
-false
-0
-Polygon -7500403 true true 200 193 197 249 179 249 177 196 166 187 140 189 93 191 78 179 72 211 49 209 48 181 37 149 25 120 25 89 45 72 103 84 179 75 198 76 252 64 272 81 293 103 285 121 255 121 242 118 224 167
-Polygon -7500403 true true 73 210 86 251 62 249 48 208
-Polygon -7500403 true true 25 114 16 195 9 204 23 213 25 200 39 123
-
-cylinder
-false
-0
-Circle -7500403 true true 0 0 300
-
-dot
-false
-0
-Circle -7500403 true true 90 90 120
-
-face happy
-false
-0
-Circle -7500403 true true 8 8 285
-Circle -16777216 true false 60 75 60
-Circle -16777216 true false 180 75 60
-Polygon -16777216 true false 150 255 90 239 62 213 47 191 67 179 90 203 109 218 150 225 192 218 210 203 227 181 251 194 236 217 212 240
-
-face neutral
-false
-0
-Circle -7500403 true true 8 7 285
-Circle -16777216 true false 60 75 60
-Circle -16777216 true false 180 75 60
-Rectangle -16777216 true false 60 195 240 225
-
-face sad
-false
-0
-Circle -7500403 true true 8 8 285
-Circle -16777216 true false 60 75 60
-Circle -16777216 true false 180 75 60
-Polygon -16777216 true false 150 168 90 184 62 210 47 232 67 244 90 220 109 205 150 198 192 205 210 220 227 242 251 229 236 206 212 183
-
-fish
-false
-0
-Polygon -1 true false 44 131 21 87 15 86 0 120 15 150 0 180 13 214 20 212 45 166
-Polygon -1 true false 135 195 119 235 95 218 76 210 46 204 60 165
-Polygon -1 true false 75 45 83 77 71 103 86 114 166 78 135 60
-Polygon -7500403 true true 30 136 151 77 226 81 280 119 292 146 292 160 287 170 270 195 195 210 151 212 30 166
-Circle -16777216 true false 215 106 30
-
-flag
-false
-0
-Rectangle -7500403 true true 60 15 75 300
-Polygon -7500403 true true 90 150 270 90 90 30
-Line -7500403 true 75 135 90 135
-Line -7500403 true 75 45 90 45
-
-flower
-false
-0
-Polygon -10899396 true false 135 120 165 165 180 210 180 240 150 300 165 300 195 240 195 195 165 135
-Circle -7500403 true true 85 132 38
-Circle -7500403 true true 130 147 38
-Circle -7500403 true true 192 85 38
-Circle -7500403 true true 85 40 38
-Circle -7500403 true true 177 40 38
-Circle -7500403 true true 177 132 38
-Circle -7500403 true true 70 85 38
-Circle -7500403 true true 130 25 38
-Circle -7500403 true true 96 51 108
-Circle -16777216 true false 113 68 74
-Polygon -10899396 true false 189 233 219 188 249 173 279 188 234 218
-Polygon -10899396 true false 180 255 150 210 105 210 75 240 135 240
-
-house
-false
-0
-Rectangle -7500403 true true 45 120 255 285
-Rectangle -16777216 true false 120 210 180 285
-Polygon -7500403 true true 15 120 150 15 285 120
-Line -16777216 false 30 120 270 120
-
-leaf
-false
-0
-Polygon -7500403 true true 150 210 135 195 120 210 60 210 30 195 60 180 60 165 15 135 30 120 15 105 40 104 45 90 60 90 90 105 105 120 120 120 105 60 120 60 135 30 150 15 165 30 180 60 195 60 180 120 195 120 210 105 240 90 255 90 263 104 285 105 270 120 285 135 240 165 240 180 270 195 240 210 180 210 165 195
-Polygon -7500403 true true 135 195 135 240 120 255 105 255 105 285 135 285 165 240 165 195
-
-line
-true
-0
-Line -7500403 true 150 0 150 300
-
-line half
-true
-0
-Line -7500403 true 150 0 150 150
-
-pentagon
-false
-0
-Polygon -7500403 true true 150 15 15 120 60 285 240 285 285 120
-
-person
-false
-0
-Circle -7500403 true true 110 5 80
-Polygon -7500403 true true 105 90 120 195 90 285 105 300 135 300 150 225 165 300 195 300 210 285 180 195 195 90
-Rectangle -7500403 true true 127 79 172 94
-Polygon -7500403 true true 195 90 240 150 225 180 165 105
-Polygon -7500403 true true 105 90 60 150 75 180 135 105
-
-plant
-false
-0
-Rectangle -7500403 true true 135 90 165 300
-Polygon -7500403 true true 135 255 90 210 45 195 75 255 135 285
-Polygon -7500403 true true 165 255 210 210 255 195 225 255 165 285
-Polygon -7500403 true true 135 180 90 135 45 120 75 180 135 210
-Polygon -7500403 true true 165 180 165 210 225 180 255 120 210 135
-Polygon -7500403 true true 135 105 90 60 45 45 75 105 135 135
-Polygon -7500403 true true 165 105 165 135 225 105 255 45 210 60
-Polygon -7500403 true true 135 90 120 45 150 15 180 45 165 90
-
-square
-false
-0
-Rectangle -7500403 true true 30 30 270 270
-
-square 2
-false
-0
-Rectangle -7500403 true true 30 30 270 270
-Rectangle -16777216 true false 60 60 240 240
-
-star
-false
-0
-Polygon -7500403 true true 151 1 185 108 298 108 207 175 242 282 151 216 59 282 94 175 3 108 116 108
-
-target
-false
-0
-Circle -7500403 true true 0 0 300
-Circle -16777216 true false 30 30 240
-Circle -7500403 true true 60 60 180
-Circle -16777216 true false 90 90 120
-Circle -7500403 true true 120 120 60
-
-tree
-false
-0
-Circle -7500403 true true 118 3 94
-Rectangle -6459832 true false 120 195 180 300
-Circle -7500403 true true 65 21 108
-Circle -7500403 true true 116 41 127
-Circle -7500403 true true 45 90 120
-Circle -7500403 true true 104 74 152
-
-triangle
-false
-0
-Polygon -7500403 true true 150 30 15 255 285 255
-
-triangle 2
-false
-0
-Polygon -7500403 true true 150 30 15 255 285 255
-Polygon -16777216 true false 151 99 225 223 75 224
-
-truck
-false
-0
-Rectangle -7500403 true true 4 45 195 187
-Polygon -7500403 true true 296 193 296 150 259 134 244 104 208 104 207 194
-Rectangle -1 true false 195 60 195 105
-Polygon -16777216 true false 238 112 252 141 219 141 218 112
-Circle -16777216 true false 234 174 42
-Rectangle -7500403 true true 181 185 214 194
-Circle -16777216 true false 144 174 42
-Circle -16777216 true false 24 174 42
-Circle -7500403 false true 24 174 42
-Circle -7500403 false true 144 174 42
-Circle -7500403 false true 234 174 42
-
-turtle
-true
-0
-Polygon -10899396 true false 215 204 240 233 246 254 228 266 215 252 193 210
-Polygon -10899396 true false 195 90 225 75 245 75 260 89 269 108 261 124 240 105 225 105 210 105
-Polygon -10899396 true false 105 90 75 75 55 75 40 89 31 108 39 124 60 105 75 105 90 105
-Polygon -10899396 true false 132 85 134 64 107 51 108 17 150 2 192 18 192 52 169 65 172 87
-Polygon -10899396 true false 85 204 60 233 54 254 72 266 85 252 107 210
-Polygon -7500403 true true 119 75 179 75 209 101 224 135 220 225 175 261 128 261 81 224 74 135 88 99
-
-wheel
-false
-0
-Circle -7500403 true true 3 3 294
-Circle -16777216 true false 30 30 240
-Line -7500403 true 150 285 150 15
-Line -7500403 true 15 150 285 150
-Circle -7500403 true true 120 120 60
-Line -7500403 true 216 40 79 269
-Line -7500403 true 40 84 269 221
-Line -7500403 true 40 216 269 79
-Line -7500403 true 84 40 221 269
-
-x
-false
-0
-Polygon -7500403 true true 270 75 225 30 30 225 75 270
-Polygon -7500403 true true 30 75 75 30 270 225 225 270
-@#$#@#$#@
-NetLogo 6.2.2
-@#$#@#$#@
-setup
-repeat 180 [ go ]
-@#$#@#$#@
-@#$#@#$#@
-@#$#@#$#@
-@#$#@#$#@
-default
-0.0
--0.2 0 0.0 1.0
-0.0 1 1.0 0.0
-0.2 0 0.0 1.0
-link direction
-true
-0
-Line -7500403 true 150 150 90 180
-Line -7500403 true 150 150 210 180
-@#$#@#$#@
-1
-@#$#@#$#@
diff --git a/demos/Traffic Basic - Reinforcement.nlogox b/demos/Traffic Basic - Reinforcement.nlogox
new file mode 100644
index 0000000..1faf962
--- /dev/null
+++ b/demos/Traffic Basic - Reinforcement.nlogox
@@ -0,0 +1,1082 @@
+
+
+ speed]
+ [-> distance next-car]
+ [-> [speed] of next-car]
+ )
+
+ if fp-exp? [
+ set inputs lput [-> exp-rate ] inputs
+ ]
+ if fp-ticks? [
+ set inputs lput [-> ticks] inputs
+ ]
+
+ py:set "state_dims" length inputs
+ py:set "hl_size" 36
+ py:set "num_actions" 3
+ py:set "memory_size" memory-size
+ py:set "batch_size" batch-size
+ py:set "lr" learning-rate
+
+ (py:run
+ "model = Sequential()"
+ "model.add(Dense(hl_size, input_shape=(state_dims,), activation='relu'))"
+ "model.add(Dense(hl_size, activation='relu'))"
+ "model.add(Dense(hl_size, activation='relu'))"
+ "model.add(Dense(num_actions))"
+ "optimizer = optimizers.adam(lr=lr)"
+ "model.compile(optimizer, 'mse')"
+ "model.summary()"
+ "memory = []")
+
+ ask patches [ setup-road ]
+ set speed-limit 1
+ set speed-min 0
+ setup-cars
+ reset-ticks
+end
+
+to setup-road ;; patch procedure
+ if pycor < 2 and pycor > -2 [ set pcolor white ]
+end
+
+to setup-cars
+ if number-of-cars > world-width [
+ user-message (word
+ "There are too many cars for the amount of road. "
+ "Please decrease the NUMBER-OF-CARS slider to below "
+ (world-width + 1) " and press the SETUP button again. "
+ "The setup has stopped.")
+ stop
+ ]
+ set-default-shape turtles "car"
+ create-turtles number-of-cars [
+ set color blue
+ set xcor random-xcor
+ set heading 90
+ ;; set initial speed to be in range 0.1 to 1.0
+ set speed 0.1 + random-float 0.9
+ separate-cars
+ ]
+ set sample-car one-of turtles
+ ask sample-car [ set color red ]
+end
+
+; this procedure is needed so when we click "Setup" we
+; don't end up with any two cars on the same patch
+to separate-cars ;; turtle procedure
+ if any? other turtles-here [
+ fd 1
+ separate-cars
+ ]
+end
+
+to go
+ py:set "discount" discount
+ ;; if there is a car right ahead of you, match its speed then slow down
+ select-actions
+ ask turtles [
+ if action = 0 [ decelerate set color red ]
+ if action = 1 [ set color yellow ]
+ if action = 2 [ accelerate set color green]
+ ;; don't slow down below speed minimum or speed up beyond speed limit
+ if distance next-car < 1 + speed [ slow-down-car next-car ]
+ if speed < speed-min [ set speed speed-min ]
+ if speed > speed-limit [ set speed speed-limit ]
+ fd speed
+ set reward (log (speed + 1e-8) 2)
+ ;set reward speed
+ ]
+ if train? [
+ remember
+ train
+ ]
+ tick
+end
+
+to select-actions
+ ask turtles [ set state map runresult inputs ]
+ let turtle-list sort turtles
+ py:set "states" map [ t -> [ state ] of t ] turtle-list
+ let actions py:runresult "np.argmax(model.predict(np.array(states)), axis = 1)"
+
+ (foreach turtle-list actions [ [t a] ->
+ ask t [
+ ifelse random-float 1 < exp-rate [
+ set action random 3
+ ] [
+ set action a
+ ]
+ ]
+ ])
+end
+
+to-report exp-rate
+ report exploration-rate / (1 + exploration-decay-rate * ticks)
+end
+
+to remember
+ ask turtles [ set next-state map runresult inputs ]
+ let data [ (list state action reward next-state) ] of turtles
+ py:set "new_exp" data
+ (py:run
+ "memory.extend(new_exp)"
+ "if len(memory) > memory_size:"
+ " memory = memory[-memory_size:]")
+end
+
+to train
+ (py:run
+ "sample_ix = np.random.randint(len(memory), size = batch_size)"
+ "inputs = np.array([memory[i][0] for i in sample_ix])"
+ "actions = np.array([memory[i][1] for i in sample_ix])"
+ "rewards = np.array([memory[i][2] for i in sample_ix])"
+ "next_states = np.array([memory[i][3] for i in sample_ix])"
+ "targets = model.predict(inputs)"
+ "next_state_rewards = model.predict(next_states)"
+ "next_state_qs = np.max(next_state_rewards, axis = 1)"
+ "targets[np.arange(targets.shape[0]), actions] = rewards + discount * next_state_qs"
+ "model.train_on_batch(inputs, targets)"
+ )
+
+end
+
+to-report next-car
+ let here min-one-of turtles-here with [ xcor > [ xcor ] of myself ] [ distance myself ]
+ if here != nobody [ report here ]
+ let i 1
+ while [ not any? turtles-on patch-ahead i ] [
+ set i i + 1
+ ]
+ report min-one-of turtles-on patch-ahead i [ distance myself ]
+end
+
+
+to slow-down-car [ car-ahead ] ;; turtle procedure
+ ;; slow down so you are driving more slowly than the car ahead of you
+ set speed [ speed ] of car-ahead - stop-penalty
+end
+
+to accelerate
+ set speed speed + acceleration
+end
+
+to decelerate
+ set speed speed - deceleration
+end
+
+; Copyright 1997 Uri Wilensky.
+; See Info tab for full copyright and license.]]>
+
+
+
+
+
+
+
+
+
+
+
+
+ plot [speed] of sample-car
+
+
+
+ plot min [speed] of turtles
+
+
+
+ plot max [speed] of turtles
+
+
+
+
+
+
+ set-plot-y-range 0 number-of-cars
+
+
+
+ plot count turtles with [ action = 0 ]
+
+
+
+ plot count turtles with [ action = 1 ]
+
+
+
+ plot count turtles with [ action = 2 ]
+
+
+
+
+
+
+
+
+ exp-rate
+
+
+
+ ]]>
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ setup
+repeat 180 [ go ]
+
diff --git a/demos/Wolf Sheep Predation - Real-time 3D Plot.nlogo b/demos/Wolf Sheep Predation - Real-time 3D Plot.nlogo
deleted file mode 100644
index d413494..0000000
--- a/demos/Wolf Sheep Predation - Real-time 3D Plot.nlogo
+++ /dev/null
@@ -1,931 +0,0 @@
-extensions [ py ]
-
-globals [ max-sheep sheep-data wolf-data grass-data pyplot-dirty? ] ; don't let sheep population grow too large
-; Sheep and wolves are both breeds of turtle.
-breed [ sheep a-sheep ] ; sheep is its own plural, so we use "a-sheep" as the singular.
-breed [ wolves wolf ]
-turtles-own [ energy ] ; both wolves and sheep have energy
-patches-own [ countdown ]
-
-to setup
- clear-all
- ifelse netlogo-web? [set max-sheep 10000] [set max-sheep 30000]
-
- py:setup py:python
-
- (py:run
- "import numpy as np"
- "import matplotlib"
- "matplotlib.use('TkAgg')"
- "import matplotlib.pyplot as plt"
- "from mpl_toolkits.mplot3d import Axes3D"
- )
-
- ; Check model-version switch
- ; if we're not modeling grass, then the sheep don't need to eat to survive
- ; otherwise the grass's state of growth and growing logic need to be set up
- ifelse model-version = "sheep-wolves-grass" [
- ask patches [
- set pcolor one-of [ green brown ]
- ifelse pcolor = green
- [ set countdown grass-regrowth-time ]
- [ set countdown random grass-regrowth-time ] ; initialize grass regrowth clocks randomly for brown patches
- ]
- ]
- [
- ask patches [ set pcolor green ]
- ]
-
- create-sheep initial-number-sheep ; create the sheep, then initialize their variables
- [
- set shape "sheep"
- set color white
- set size 1.5 ; easier to see
- set label-color blue - 2
- set energy random (2 * sheep-gain-from-food)
- setxy random-xcor random-ycor
- ]
-
- create-wolves initial-number-wolves ; create the wolves, then initialize their variables
- [
- set shape "wolf"
- set color black
- set size 2 ; easier to see
- set energy random (2 * wolf-gain-from-food)
- setxy random-xcor random-ycor
- ]
- set sheep-data []
- set wolf-data []
- set grass-data []
- display-labels
- update-stats
- (py:run
- "fig = plt.figure()"
- "ax = fig.add_subplot(111, projection='3d')"
- "line = ax.plot([],[],[])[0]"
- "ax.set_xlabel('sheep')"
- "ax.set_ylabel('wolves')"
- "ax.set_zlabel('grass')"
- "plt.show(block=False)"
- )
- reset-ticks
-end
-
-to go
- ; stop the simulation of no wolves or sheep
- if not any? turtles [ stop ]
- ; stop the model if there are no wolves and the number of sheep gets very large
- if not any? wolves and count sheep > max-sheep [ user-message "The sheep have inherited the earth" stop ]
- ask sheep [
- move
- if model-version = "sheep-wolves-grass" [ ; in this version, sheep eat grass, grass grows and it costs sheep energy to move
- set energy energy - 1 ; deduct energy for sheep only if running sheep-wolf-grass model version
- eat-grass ; sheep eat grass only if running sheep-wolf-grass model version
- death ; sheep die from starvation only if running sheep-wolf-grass model version
- ]
- reproduce-sheep ; sheep reproduce at random rate governed by slider
- ]
- ask wolves [
- move
- set energy energy - 1 ; wolves lose energy as they move
- eat-sheep ; wolves eat a sheep on their patch
- death ; wolves die if our of energy
- reproduce-wolves ; wolves reproduce at random rate governed by slider
- ]
- if model-version = "sheep-wolves-grass" [ ask patches [ grow-grass ] ]
- ; set grass count patches with [pcolor = green]
- update-stats
- tick
- display-labels
-end
-
-to update-stats
- set sheep-data lput count sheep sheep-data
- set wolf-data lput count wolves wolf-data
- set grass-data lput count grass grass-data
- set pyplot-dirty? true
-end
-
-to update-pyplot
- every 0.25 [
- every 1 [
- if pyplot-dirty? [ redraw-pyplot ]
- ]
- py:run "plt.pause(0.01)"
- ]
-end
-
-to redraw-pyplot
- py:set "s" sheep-data
- py:set "w" wolf-data
- py:set "g" grass-data
- (py:run
- "line.set_data((s,w))"
- "line.set_3d_properties(g)"
- "ax.set_xlim([min(s), max(s)])"
- "ax.set_ylim([min(w), max(w)])"
- "ax.set_zlim([min(g), max(g)])"
- "plt.draw()"
- )
- set pyplot-dirty? false
-end
-
-to move ; turtle procedure
- rt random 50
- lt random 50
- fd 1
-end
-
-to eat-grass ; sheep procedure
- ; sheep eat grass, turn the patch brown
- if pcolor = green [
- set pcolor brown
- set energy energy + sheep-gain-from-food ; sheep gain energy by eating
- ]
-end
-
-to reproduce-sheep ; sheep procedure
- if random-float 100 < sheep-reproduce [ ; throw "dice" to see if you will reproduce
- set energy (energy / 2) ; divide energy between parent and offspring
- hatch 1 [ rt random-float 360 fd 1 ] ; hatch an offspring and move it forward 1 step
- ]
-end
-
-to reproduce-wolves ; wolf procedure
- if random-float 100 < wolf-reproduce [ ; throw "dice" to see if you will reproduce
- set energy (energy / 2) ; divide energy between parent and offspring
- hatch 1 [ rt random-float 360 fd 1 ] ; hatch an offspring and move it forward 1 step
- ]
-end
-
-to eat-sheep ; wolf procedure
- let prey one-of sheep-here ; grab a random sheep
- if prey != nobody [ ; did we get one? if so,
- ask prey [ die ] ; kill it, and...
- set energy energy + wolf-gain-from-food ; get energy from eating
- ]
-end
-
-to death ; turtle procedure (i.e. both wolf nd sheep procedure)
- ; when energy dips below zero, die
- if energy < 0 [ die ]
-end
-
-to grow-grass ; patch procedure
- ; countdown on brown patches: if reach 0, grow some grass
- if pcolor = brown [
- ifelse countdown <= 0
- [ set pcolor green
- set countdown grass-regrowth-time ]
- [ set countdown countdown - 1 ]
- ]
-end
-
-to-report grass
- ifelse model-version = "sheep-wolves-grass" [
- report patches with [pcolor = green]
- ]
- [ report 0 ]
-end
-
-
-to display-labels
- ask turtles [ set label "" ]
- if show-energy? [
- ask wolves [ set label round energy ]
- if model-version = "sheep-wolves-grass" [ ask sheep [ set label round energy ] ]
- ]
-end
-
-
-; Copyright 1997 Uri Wilensky.
-; See Info tab for full copyright and license.
-@#$#@#$#@
-GRAPHICS-WINDOW
-355
-10
-873
-529
--1
--1
-10.0
-1
-14
-1
-1
-1
-0
-1
-1
-1
--25
-25
--25
-25
-1
-1
-1
-ticks
-30.0
-
-SLIDER
-5
-60
-179
-93
-initial-number-sheep
-initial-number-sheep
-0
-250
-100.0
-1
-1
-NIL
-HORIZONTAL
-
-SLIDER
-5
-196
-179
-229
-sheep-gain-from-food
-sheep-gain-from-food
-0.0
-50.0
-4.0
-1.0
-1
-NIL
-HORIZONTAL
-
-SLIDER
-5
-231
-179
-264
-sheep-reproduce
-sheep-reproduce
-1.0
-20.0
-4.0
-1.0
-1
-%
-HORIZONTAL
-
-SLIDER
-185
-60
-350
-93
-initial-number-wolves
-initial-number-wolves
-0
-250
-50.0
-1
-1
-NIL
-HORIZONTAL
-
-SLIDER
-183
-195
-348
-228
-wolf-gain-from-food
-wolf-gain-from-food
-0.0
-100.0
-20.0
-1.0
-1
-NIL
-HORIZONTAL
-
-SLIDER
-183
-231
-348
-264
-wolf-reproduce
-wolf-reproduce
-0.0
-20.0
-6.0
-1.0
-1
-%
-HORIZONTAL
-
-SLIDER
-40
-100
-252
-133
-grass-regrowth-time
-grass-regrowth-time
-0
-100
-30.0
-1
-1
-NIL
-HORIZONTAL
-
-BUTTON
-40
-140
-109
-173
-setup
-setup
-NIL
-1
-T
-OBSERVER
-NIL
-NIL
-NIL
-NIL
-1
-
-BUTTON
-115
-140
-190
-173
-go
-go
-T
-1
-T
-OBSERVER
-NIL
-NIL
-NIL
-NIL
-0
-
-PLOT
-10
-360
-350
-530
-populations
-time
-pop.
-0.0
-100.0
-0.0
-100.0
-true
-true
-"" ""
-PENS
-"sheep" 1.0 0 -612749 true "" "plot count sheep"
-"wolves" 1.0 0 -16449023 true "" "plot count wolves"
-"grass / 4" 1.0 0 -10899396 true "" "if model-version = \"sheep-wolves-grass\" [ plot count grass / 4 ]"
-
-MONITOR
-41
-308
-111
-353
-sheep
-count sheep
-3
-1
-11
-
-MONITOR
-115
-308
-185
-353
-wolves
-count wolves
-3
-1
-11
-
-MONITOR
-191
-308
-256
-353
-grass
-count grass / 4
-0
-1
-11
-
-TEXTBOX
-20
-178
-160
-196
-Sheep settings
-11
-0.0
-0
-
-TEXTBOX
-198
-176
-311
-194
-Wolf settings
-11
-0.0
-0
-
-SWITCH
-105
-270
-241
-303
-show-energy?
-show-energy?
-1
-1
--1000
-
-CHOOSER
-5
-10
-350
-55
-model-version
-model-version
-"sheep-wolves" "sheep-wolves-grass"
-1
-
-BUTTON
-195
-140
-325
-173
-NIL
-update-pyplot
-T
-1
-T
-OBSERVER
-NIL
-NIL
-NIL
-NIL
-1
-
-@#$#@#$#@
-## WHAT IS IT?
-
-This model explores the stability of predator-prey ecosystems. Such a system is called unstable if it tends to result in extinction for one or more species involved. In contrast, a system is stable if it tends to maintain itself over time, despite fluctuations in population sizes.
-
-## HOW IT WORKS
-
-There are two main variations to this model.
-
-In the first variation, the "sheep-wolves" version, wolves and sheep wander randomly around the landscape, while the wolves look for sheep to prey on. Each step costs the wolves energy, and they must eat sheep in order to replenish their energy - when they run out of energy they die. To allow the population to continue, each wolf or sheep has a fixed probability of reproducing at each time step. In this variation, we model the grass as "infinite" so that sheep always have enough to eat, and we don't explicitly model the eating or growing of grass. As such, sheep don't either gain or lose energy by eating or moving. This variation produces interesting population dynamics, but is ultimately unstable. This variation of the model is particularly well-suited to interacting species in a rich nutrient environment, such as two strains of bacteria in a petri dish (Gause, 1934).
-
-The second variation, the "sheep-wolves-grass" version explictly models grass (green) in addition to wolves and sheep. The behavior of the wolves is identical to the first variation, however this time the sheep must eat grass in order to maintain their energy - when they run out of energy they die. Once grass is eaten it will only regrow after a fixed amount of time. This variation is more complex than the first, but it is generally stable. It is a closer match to the classic Lotka Volterra population oscillation models. The classic LV models though assume the populations can take on real values, but in small populations these models underestimate extinctions and agent-based models such as the ones here, provide more realistic results. (See Wilensky & Rand, 2015; chapter 4).
-
-The construction of this model is described in two papers by Wilensky & Reisman (1998; 2006) referenced below.
-
-## HOW TO USE IT
-
-1. Set the model-version chooser to "sheep-wolves-grass" to include grass eating and growth in the model, or to "sheep-wolves" to only include wolves (black) and sheep (white).
-2. Adjust the slider parameters (see below), or use the default settings.
-3. Press the SETUP button.
-4. Press the GO button to begin the simulation.
-5. Look at the monitors to see the current population sizes
-6. Look at the POPULATIONS plot to watch the populations fluctuate over time
-
-Parameters:
-MODEL-VERSION: Whether we model sheep wolves and grass or just sheep and wolves
-INITIAL-NUMBER-SHEEP: The initial size of sheep population
-INITIAL-NUMBER-WOLVES: The initial size of wolf population
-SHEEP-GAIN-FROM-FOOD: The amount of energy sheep get for every grass patch eaten (Note this is not used in the sheep-wolves model version)
-WOLF-GAIN-FROM-FOOD: The amount of energy wolves get for every sheep eaten
-SHEEP-REPRODUCE: The probability of a sheep reproducing at each time step
-WOLF-REPRODUCE: The probability of a wolf reproducing at each time step
-GRASS-REGROWTH-TIME: How long it takes for grass to regrow once it is eaten (Note this is not used in the sheep-wolves model version)
-SHOW-ENERGY?: Whether or not to show the energy of each animal as a number
-
-Notes:
-- one unit of energy is deducted for every step a wolf takes
-- when running the sheep-wolves-grass model version, one unit of energy is deducted for every step a sheep takes
-
-There are three monitors to show the populations of the wolves, sheep and grass and a populations plot to display the population values over time.
-
-If there are no wolves left and too many sheep, the model run stops.
-
-## THINGS TO NOTICE
-
-When running the sheep-wolves model variation, watch as the sheep and wolf populations fluctuate. Notice that increases and decreases in the sizes of each population are related. In what way are they related? What eventually happens?
-
-In the sheep-wolves-grass model variation, notice the green line added to the population plot representing fluctuations in the amount of grass. How do the sizes of the three populations appear to relate now? What is the explanation for this?
-
-Why do you suppose that some variations of the model might be stable while others are not?
-
-## THINGS TO TRY
-
-Try adjusting the parameters under various settings. How sensitive is the stability of the model to the particular parameters?
-
-Can you find any parameters that generate a stable ecosystem in the sheep-wolves model variation?
-
-Try running the sheep-wolves-grass model variation, but setting INITIAL-NUMBER-WOLVES to 0. This gives a stable ecosystem with only sheep and grass. Why might this be stable while the variation with only sheep and wolves is not?
-
-Notice that under stable settings, the populations tend to fluctuate at a predictable pace. Can you find any parameters that will speed this up or slow it down?
-
-## EXTENDING THE MODEL
-
-There are a number ways to alter the model so that it will be stable with only wolves and sheep (no grass). Some will require new elements to be coded in or existing behaviors to be changed. Can you develop such a version?
-
-Try changing the reproduction rules -- for example, what would happen if reproduction depended on energy rather than being determined by a fixed probability?
-
-Can you modify the model so the sheep will flock?
-
-Can you modify the model so that wolves actively chase sheep?
-
-## NETLOGO FEATURES
-
-Note the use of breeds to model two different kinds of "turtles": wolves and sheep. Note the use of patches to model grass.
-
-Note use of the ONE-OF agentset reporter to select a random sheep to be eaten by a wolf.
-
-## RELATED MODELS
-
-Look at Rabbits Grass Weeds for another model of interacting populations with different rules.
-
-## CREDITS AND REFERENCES
-
-Wilensky, U. & Reisman, K. (1998). Connected Science: Learning Biology through Constructing and Testing Computational Theories -- an Embodied Modeling Approach. International Journal of Complex Systems, M. 234, pp. 1 - 12. (The Wolf-Sheep-Predation model is a slightly extended version of the model described in the paper.)
-
-Wilensky, U. & Reisman, K. (2006). Thinking like a Wolf, a Sheep or a Firefly: Learning Biology through Constructing and Testing Computational Theories -- an Embodied Modeling Approach. Cognition & Instruction, 24(2), pp. 171-209. http://ccl.northwestern.edu/papers/wolfsheep.pdf .
-
-Wilensky, U., & Rand, W. (2015). An introduction to agent-based modeling: Modeling natural, social and engineered complex systems with NetLogo. Cambridge, MA: MIT Press.
-
-Lotka, A. J. (1925). Elements of physical biology. New York: Dover.
-
-Volterra, V. (1926, October 16). Fluctuations in the abundance of a species considered mathematically. Nature, 118, 558–560.
-
-Gause, G. F. (1934). The struggle for existence. Baltimore: Williams & Wilkins.
-
-## HOW TO CITE
-
-If you mention this model or the NetLogo software in a publication, we ask that you include the citations below.
-
-For the model itself:
-
-* Wilensky, U. (1997). NetLogo Wolf Sheep Predation model. http://ccl.northwestern.edu/netlogo/models/WolfSheepPredation. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.
-
-Please cite the NetLogo software as:
-
-* Wilensky, U. (1999). NetLogo. http://ccl.northwestern.edu/netlogo/. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.
-
-## COPYRIGHT AND LICENSE
-
-Copyright 1997 Uri Wilensky.
-
-
-
-This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit https://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
-
-Commercial licenses are also available. To inquire about commercial licenses, please contact Uri Wilensky at uri@northwestern.edu.
-
-This model was created as part of the project: CONNECTED MATHEMATICS: MAKING SENSE OF COMPLEX PHENOMENA THROUGH BUILDING OBJECT-BASED PARALLEL MODELS (OBPML). The project gratefully acknowledges the support of the National Science Foundation (Applications of Advanced Technologies Program) -- grant numbers RED #9552950 and REC #9632612.
-
-This model was converted to NetLogo as part of the projects: PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN CLASSROOMS and/or INTEGRATED SIMULATION AND MODELING ENVIRONMENT. The project gratefully acknowledges the support of the National Science Foundation (REPP & ROLE programs) -- grant numbers REC #9814682 and REC-0126227. Converted from StarLogoT to NetLogo, 2000.
-
-
-@#$#@#$#@
-default
-true
-0
-Polygon -7500403 true true 150 5 40 250 150 205 260 250
-
-airplane
-true
-0
-Polygon -7500403 true true 150 0 135 15 120 60 120 105 15 165 15 195 120 180 135 240 105 270 120 285 150 270 180 285 210 270 165 240 180 180 285 195 285 165 180 105 180 60 165 15
-
-arrow
-true
-0
-Polygon -7500403 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150
-
-box
-false
-0
-Polygon -7500403 true true 150 285 285 225 285 75 150 135
-Polygon -7500403 true true 150 135 15 75 150 15 285 75
-Polygon -7500403 true true 15 75 15 225 150 285 150 135
-Line -16777216 false 150 285 150 135
-Line -16777216 false 150 135 15 75
-Line -16777216 false 150 135 285 75
-
-bug
-true
-0
-Circle -7500403 true true 96 182 108
-Circle -7500403 true true 110 127 80
-Circle -7500403 true true 110 75 80
-Line -7500403 true 150 100 80 30
-Line -7500403 true 150 100 220 30
-
-butterfly
-true
-0
-Polygon -7500403 true true 150 165 209 199 225 225 225 255 195 270 165 255 150 240
-Polygon -7500403 true true 150 165 89 198 75 225 75 255 105 270 135 255 150 240
-Polygon -7500403 true true 139 148 100 105 55 90 25 90 10 105 10 135 25 180 40 195 85 194 139 163
-Polygon -7500403 true true 162 150 200 105 245 90 275 90 290 105 290 135 275 180 260 195 215 195 162 165
-Polygon -16777216 true false 150 255 135 225 120 150 135 120 150 105 165 120 180 150 165 225
-Circle -16777216 true false 135 90 30
-Line -16777216 false 150 105 195 60
-Line -16777216 false 150 105 105 60
-
-car
-false
-0
-Polygon -7500403 true true 300 180 279 164 261 144 240 135 226 132 213 106 203 84 185 63 159 50 135 50 75 60 0 150 0 165 0 225 300 225 300 180
-Circle -16777216 true false 180 180 90
-Circle -16777216 true false 30 180 90
-Polygon -16777216 true false 162 80 132 78 134 135 209 135 194 105 189 96 180 89
-Circle -7500403 true true 47 195 58
-Circle -7500403 true true 195 195 58
-
-circle
-false
-0
-Circle -7500403 true true 0 0 300
-
-circle 2
-false
-0
-Circle -7500403 true true 0 0 300
-Circle -16777216 true false 30 30 240
-
-cow
-false
-0
-Polygon -7500403 true true 200 193 197 249 179 249 177 196 166 187 140 189 93 191 78 179 72 211 49 209 48 181 37 149 25 120 25 89 45 72 103 84 179 75 198 76 252 64 272 81 293 103 285 121 255 121 242 118 224 167
-Polygon -7500403 true true 73 210 86 251 62 249 48 208
-Polygon -7500403 true true 25 114 16 195 9 204 23 213 25 200 39 123
-
-cylinder
-false
-0
-Circle -7500403 true true 0 0 300
-
-dot
-false
-0
-Circle -7500403 true true 90 90 120
-
-face happy
-false
-0
-Circle -7500403 true true 8 8 285
-Circle -16777216 true false 60 75 60
-Circle -16777216 true false 180 75 60
-Polygon -16777216 true false 150 255 90 239 62 213 47 191 67 179 90 203 109 218 150 225 192 218 210 203 227 181 251 194 236 217 212 240
-
-face neutral
-false
-0
-Circle -7500403 true true 8 7 285
-Circle -16777216 true false 60 75 60
-Circle -16777216 true false 180 75 60
-Rectangle -16777216 true false 60 195 240 225
-
-face sad
-false
-0
-Circle -7500403 true true 8 8 285
-Circle -16777216 true false 60 75 60
-Circle -16777216 true false 180 75 60
-Polygon -16777216 true false 150 168 90 184 62 210 47 232 67 244 90 220 109 205 150 198 192 205 210 220 227 242 251 229 236 206 212 183
-
-fish
-false
-0
-Polygon -1 true false 44 131 21 87 15 86 0 120 15 150 0 180 13 214 20 212 45 166
-Polygon -1 true false 135 195 119 235 95 218 76 210 46 204 60 165
-Polygon -1 true false 75 45 83 77 71 103 86 114 166 78 135 60
-Polygon -7500403 true true 30 136 151 77 226 81 280 119 292 146 292 160 287 170 270 195 195 210 151 212 30 166
-Circle -16777216 true false 215 106 30
-
-flag
-false
-0
-Rectangle -7500403 true true 60 15 75 300
-Polygon -7500403 true true 90 150 270 90 90 30
-Line -7500403 true 75 135 90 135
-Line -7500403 true 75 45 90 45
-
-flower
-false
-0
-Polygon -10899396 true false 135 120 165 165 180 210 180 240 150 300 165 300 195 240 195 195 165 135
-Circle -7500403 true true 85 132 38
-Circle -7500403 true true 130 147 38
-Circle -7500403 true true 192 85 38
-Circle -7500403 true true 85 40 38
-Circle -7500403 true true 177 40 38
-Circle -7500403 true true 177 132 38
-Circle -7500403 true true 70 85 38
-Circle -7500403 true true 130 25 38
-Circle -7500403 true true 96 51 108
-Circle -16777216 true false 113 68 74
-Polygon -10899396 true false 189 233 219 188 249 173 279 188 234 218
-Polygon -10899396 true false 180 255 150 210 105 210 75 240 135 240
-
-house
-false
-0
-Rectangle -7500403 true true 45 120 255 285
-Rectangle -16777216 true false 120 210 180 285
-Polygon -7500403 true true 15 120 150 15 285 120
-Line -16777216 false 30 120 270 120
-
-leaf
-false
-0
-Polygon -7500403 true true 150 210 135 195 120 210 60 210 30 195 60 180 60 165 15 135 30 120 15 105 40 104 45 90 60 90 90 105 105 120 120 120 105 60 120 60 135 30 150 15 165 30 180 60 195 60 180 120 195 120 210 105 240 90 255 90 263 104 285 105 270 120 285 135 240 165 240 180 270 195 240 210 180 210 165 195
-Polygon -7500403 true true 135 195 135 240 120 255 105 255 105 285 135 285 165 240 165 195
-
-line
-true
-0
-Line -7500403 true 150 0 150 300
-
-line half
-true
-0
-Line -7500403 true 150 0 150 150
-
-pentagon
-false
-0
-Polygon -7500403 true true 150 15 15 120 60 285 240 285 285 120
-
-person
-false
-0
-Circle -7500403 true true 110 5 80
-Polygon -7500403 true true 105 90 120 195 90 285 105 300 135 300 150 225 165 300 195 300 210 285 180 195 195 90
-Rectangle -7500403 true true 127 79 172 94
-Polygon -7500403 true true 195 90 240 150 225 180 165 105
-Polygon -7500403 true true 105 90 60 150 75 180 135 105
-
-plant
-false
-0
-Rectangle -7500403 true true 135 90 165 300
-Polygon -7500403 true true 135 255 90 210 45 195 75 255 135 285
-Polygon -7500403 true true 165 255 210 210 255 195 225 255 165 285
-Polygon -7500403 true true 135 180 90 135 45 120 75 180 135 210
-Polygon -7500403 true true 165 180 165 210 225 180 255 120 210 135
-Polygon -7500403 true true 135 105 90 60 45 45 75 105 135 135
-Polygon -7500403 true true 165 105 165 135 225 105 255 45 210 60
-Polygon -7500403 true true 135 90 120 45 150 15 180 45 165 90
-
-sheep
-false
-15
-Circle -1 true true 203 65 88
-Circle -1 true true 70 65 162
-Circle -1 true true 150 105 120
-Polygon -7500403 true false 218 120 240 165 255 165 278 120
-Circle -7500403 true false 214 72 67
-Rectangle -1 true true 164 223 179 298
-Polygon -1 true true 45 285 30 285 30 240 15 195 45 210
-Circle -1 true true 3 83 150
-Rectangle -1 true true 65 221 80 296
-Polygon -1 true true 195 285 210 285 210 240 240 210 195 210
-Polygon -7500403 true false 276 85 285 105 302 99 294 83
-Polygon -7500403 true false 219 85 210 105 193 99 201 83
-
-square
-false
-0
-Rectangle -7500403 true true 30 30 270 270
-
-square 2
-false
-0
-Rectangle -7500403 true true 30 30 270 270
-Rectangle -16777216 true false 60 60 240 240
-
-star
-false
-0
-Polygon -7500403 true true 151 1 185 108 298 108 207 175 242 282 151 216 59 282 94 175 3 108 116 108
-
-target
-false
-0
-Circle -7500403 true true 0 0 300
-Circle -16777216 true false 30 30 240
-Circle -7500403 true true 60 60 180
-Circle -16777216 true false 90 90 120
-Circle -7500403 true true 120 120 60
-
-tree
-false
-0
-Circle -7500403 true true 118 3 94
-Rectangle -6459832 true false 120 195 180 300
-Circle -7500403 true true 65 21 108
-Circle -7500403 true true 116 41 127
-Circle -7500403 true true 45 90 120
-Circle -7500403 true true 104 74 152
-
-triangle
-false
-0
-Polygon -7500403 true true 150 30 15 255 285 255
-
-triangle 2
-false
-0
-Polygon -7500403 true true 150 30 15 255 285 255
-Polygon -16777216 true false 151 99 225 223 75 224
-
-truck
-false
-0
-Rectangle -7500403 true true 4 45 195 187
-Polygon -7500403 true true 296 193 296 150 259 134 244 104 208 104 207 194
-Rectangle -1 true false 195 60 195 105
-Polygon -16777216 true false 238 112 252 141 219 141 218 112
-Circle -16777216 true false 234 174 42
-Rectangle -7500403 true true 181 185 214 194
-Circle -16777216 true false 144 174 42
-Circle -16777216 true false 24 174 42
-Circle -7500403 false true 24 174 42
-Circle -7500403 false true 144 174 42
-Circle -7500403 false true 234 174 42
-
-turtle
-true
-0
-Polygon -10899396 true false 215 204 240 233 246 254 228 266 215 252 193 210
-Polygon -10899396 true false 195 90 225 75 245 75 260 89 269 108 261 124 240 105 225 105 210 105
-Polygon -10899396 true false 105 90 75 75 55 75 40 89 31 108 39 124 60 105 75 105 90 105
-Polygon -10899396 true false 132 85 134 64 107 51 108 17 150 2 192 18 192 52 169 65 172 87
-Polygon -10899396 true false 85 204 60 233 54 254 72 266 85 252 107 210
-Polygon -7500403 true true 119 75 179 75 209 101 224 135 220 225 175 261 128 261 81 224 74 135 88 99
-
-wheel
-false
-0
-Circle -7500403 true true 3 3 294
-Circle -16777216 true false 30 30 240
-Line -7500403 true 150 285 150 15
-Line -7500403 true 15 150 285 150
-Circle -7500403 true true 120 120 60
-Line -7500403 true 216 40 79 269
-Line -7500403 true 40 84 269 221
-Line -7500403 true 40 216 269 79
-Line -7500403 true 84 40 221 269
-
-wolf
-false
-0
-Polygon -16777216 true false 253 133 245 131 245 133
-Polygon -7500403 true true 2 194 13 197 30 191 38 193 38 205 20 226 20 257 27 265 38 266 40 260 31 253 31 230 60 206 68 198 75 209 66 228 65 243 82 261 84 268 100 267 103 261 77 239 79 231 100 207 98 196 119 201 143 202 160 195 166 210 172 213 173 238 167 251 160 248 154 265 169 264 178 247 186 240 198 260 200 271 217 271 219 262 207 258 195 230 192 198 210 184 227 164 242 144 259 145 284 151 277 141 293 140 299 134 297 127 273 119 270 105
-Polygon -7500403 true true -1 195 14 180 36 166 40 153 53 140 82 131 134 133 159 126 188 115 227 108 236 102 238 98 268 86 269 92 281 87 269 103 269 113
-
-x
-false
-0
-Polygon -7500403 true true 270 75 225 30 30 225 75 270
-Polygon -7500403 true true 30 75 75 30 270 225 225 270
-@#$#@#$#@
-NetLogo 6.2.2
-@#$#@#$#@
-set model-version "sheep-wolves-grass"
-set show-energy? false
-setup
-repeat 75 [ go ]
-@#$#@#$#@
-@#$#@#$#@
-@#$#@#$#@
-@#$#@#$#@
-default
-0.0
--0.2 0 0.0 1.0
-0.0 1 1.0 0.0
-0.2 0 0.0 1.0
-link direction
-true
-0
-Line -7500403 true 150 150 90 180
-Line -7500403 true 150 150 210 180
-@#$#@#$#@
-1
-@#$#@#$#@
diff --git a/demos/Wolf Sheep Predation - Real-time 3D Plot.nlogox b/demos/Wolf Sheep Predation - Real-time 3D Plot.nlogox
new file mode 100644
index 0000000..2202b6f
--- /dev/null
+++ b/demos/Wolf Sheep Predation - Real-time 3D Plot.nlogox
@@ -0,0 +1,1202 @@
+
+
+ max-sheep [ user-message "The sheep have inherited the earth" stop ]
+ ask sheep [
+ move
+ if model-version = "sheep-wolves-grass" [ ; in this version, sheep eat grass, grass grows and it costs sheep energy to move
+ set energy energy - 1 ; deduct energy for sheep only if running sheep-wolf-grass model version
+ eat-grass ; sheep eat grass only if running sheep-wolf-grass model version
+ death ; sheep die from starvation only if running sheep-wolf-grass model version
+ ]
+ reproduce-sheep ; sheep reproduce at random rate governed by slider
+ ]
+ ask wolves [
+ move
+ set energy energy - 1 ; wolves lose energy as they move
+ eat-sheep ; wolves eat a sheep on their patch
+ death ; wolves die if our of energy
+ reproduce-wolves ; wolves reproduce at random rate governed by slider
+ ]
+ if model-version = "sheep-wolves-grass" [ ask patches [ grow-grass ] ]
+ ; set grass count patches with [pcolor = green]
+ update-stats
+ tick
+ display-labels
+end
+
+to update-stats
+ set sheep-data lput count sheep sheep-data
+ set wolf-data lput count wolves wolf-data
+ set grass-data lput count grass grass-data
+ set pyplot-dirty? true
+end
+
+to update-pyplot
+ every 0.25 [
+ every 1 [
+ if pyplot-dirty? [ redraw-pyplot ]
+ ]
+ py:run "plt.pause(0.01)"
+ ]
+end
+
+to redraw-pyplot
+ py:set "s" sheep-data
+ py:set "w" wolf-data
+ py:set "g" grass-data
+ (py:run
+ "line.set_data((s,w))"
+ "line.set_3d_properties(g)"
+ "ax.set_xlim([min(s), max(s)])"
+ "ax.set_ylim([min(w), max(w)])"
+ "ax.set_zlim([min(g), max(g)])"
+ "plt.draw()"
+ )
+ set pyplot-dirty? false
+end
+
+to move ; turtle procedure
+ rt random 50
+ lt random 50
+ fd 1
+end
+
+to eat-grass ; sheep procedure
+ ; sheep eat grass, turn the patch brown
+ if pcolor = green [
+ set pcolor brown
+ set energy energy + sheep-gain-from-food ; sheep gain energy by eating
+ ]
+end
+
+to reproduce-sheep ; sheep procedure
+ if random-float 100 < sheep-reproduce [ ; throw "dice" to see if you will reproduce
+ set energy (energy / 2) ; divide energy between parent and offspring
+ hatch 1 [ rt random-float 360 fd 1 ] ; hatch an offspring and move it forward 1 step
+ ]
+end
+
+to reproduce-wolves ; wolf procedure
+ if random-float 100 < wolf-reproduce [ ; throw "dice" to see if you will reproduce
+ set energy (energy / 2) ; divide energy between parent and offspring
+ hatch 1 [ rt random-float 360 fd 1 ] ; hatch an offspring and move it forward 1 step
+ ]
+end
+
+to eat-sheep ; wolf procedure
+ let prey one-of sheep-here ; grab a random sheep
+ if prey != nobody [ ; did we get one? if so,
+ ask prey [ die ] ; kill it, and...
+ set energy energy + wolf-gain-from-food ; get energy from eating
+ ]
+end
+
+to death ; turtle procedure (i.e. both wolf nd sheep procedure)
+ ; when energy dips below zero, die
+ if energy < 0 [ die ]
+end
+
+to grow-grass ; patch procedure
+ ; countdown on brown patches: if reach 0, grow some grass
+ if pcolor = brown [
+ ifelse countdown <= 0
+ [ set pcolor green
+ set countdown grass-regrowth-time ]
+ [ set countdown countdown - 1 ]
+ ]
+end
+
+to-report grass
+ ifelse model-version = "sheep-wolves-grass" [
+ report patches with [pcolor = green]
+ ]
+ [ report 0 ]
+end
+
+
+to display-labels
+ ask turtles [ set label "" ]
+ if show-energy? [
+ ask wolves [ set label round energy ]
+ if model-version = "sheep-wolves-grass" [ ask sheep [ set label round energy ] ]
+ ]
+end
+
+
+; Copyright 1997 Uri Wilensky.
+; See Info tab for full copyright and license.]]>
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ plot count sheep
+
+
+
+ plot count wolves
+
+
+
+ if model-version = "sheep-wolves-grass" [ plot count grass / 4 ]
+
+
+ count sheep
+ count wolves
+ count grass / 4
+ Sheep settings
+ Wolf settings
+
+
+
+
+
+
+
+ ]]>
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ set model-version "sheep-wolves-grass"
+set show-energy? false
+setup
+repeat 75 [ go ]
+
diff --git a/demos/Wolf Sheep Predation - Static 3D Plot.nlogo b/demos/Wolf Sheep Predation - Static 3D Plot.nlogo
deleted file mode 100644
index 0abb281..0000000
--- a/demos/Wolf Sheep Predation - Static 3D Plot.nlogo
+++ /dev/null
@@ -1,912 +0,0 @@
-extensions [ py ]
-
-globals [ max-sheep sheep-data wolf-data grass-data ] ; don't let sheep population grow too large
-; Sheep and wolves are both breeds of turtle.
-breed [ sheep a-sheep ] ; sheep is its own plural, so we use "a-sheep" as the singular.
-breed [ wolves wolf ]
-turtles-own [ energy ] ; both wolves and sheep have energy
-patches-own [ countdown ]
-
-to setup
- clear-all
- ifelse netlogo-web? [set max-sheep 10000] [set max-sheep 30000]
-
- py:setup py:python
-
- (py:run
- "import numpy as np"
- "import matplotlib"
- "matplotlib.use('TkAgg')"
- "import matplotlib.pyplot as plt"
- "from mpl_toolkits.mplot3d import Axes3D"
- )
-
- ; Check model-version switch
- ; if we're not modeling grass, then the sheep don't need to eat to survive
- ; otherwise the grass's state of growth and growing logic need to be set up
- ifelse model-version = "sheep-wolves-grass" [
- ask patches [
- set pcolor one-of [ green brown ]
- ifelse pcolor = green
- [ set countdown grass-regrowth-time ]
- [ set countdown random grass-regrowth-time ] ; initialize grass regrowth clocks randomly for brown patches
- ]
- ]
- [
- ask patches [ set pcolor green ]
- ]
-
- create-sheep initial-number-sheep ; create the sheep, then initialize their variables
- [
- set shape "sheep"
- set color white
- set size 1.5 ; easier to see
- set label-color blue - 2
- set energy random (2 * sheep-gain-from-food)
- setxy random-xcor random-ycor
- ]
-
- create-wolves initial-number-wolves ; create the wolves, then initialize their variables
- [
- set shape "wolf"
- set color black
- set size 2 ; easier to see
- set energy random (2 * wolf-gain-from-food)
- setxy random-xcor random-ycor
- ]
- set sheep-data []
- set wolf-data []
- set grass-data []
- display-labels
- update-stats
- reset-ticks
-end
-
-to go
- ; stop the simulation of no wolves or sheep
- if not any? turtles [ stop ]
- ; stop the model if there are no wolves and the number of sheep gets very large
- if not any? wolves and count sheep > max-sheep [ user-message "The sheep have inherited the earth" stop ]
- ask sheep [
- move
- if model-version = "sheep-wolves-grass" [ ; in this version, sheep eat grass, grass grows and it costs sheep energy to move
- set energy energy - 1 ; deduct energy for sheep only if running sheep-wolf-grass model version
- eat-grass ; sheep eat grass only if running sheep-wolf-grass model version
- death ; sheep die from starvation only if running sheep-wolf-grass model version
- ]
- reproduce-sheep ; sheep reproduce at random rate governed by slider
- ]
- ask wolves [
- move
- set energy energy - 1 ; wolves lose energy as they move
- eat-sheep ; wolves eat a sheep on their patch
- death ; wolves die if our of energy
- reproduce-wolves ; wolves reproduce at random rate governed by slider
- ]
- if model-version = "sheep-wolves-grass" [ ask patches [ grow-grass ] ]
- ; set grass count patches with [pcolor = green]
- update-stats
- tick
- display-labels
-end
-
-to update-stats
- set sheep-data lput count sheep sheep-data
- set wolf-data lput count wolves wolf-data
- set grass-data lput count grass grass-data
-end
-
-to show-pyplot
- py:set "s" sheep-data
- py:set "w" wolf-data
- py:set "g" grass-data
- (py:run
- "fig = plt.figure()"
- "ax = fig.add_subplot(111, projection='3d')"
- "ax.plot(s,w,g)"
- "ax.set_xlabel('sheep')"
- "ax.set_ylabel('wolves')"
- "ax.set_zlabel('grass')"
- "plt.show()"
- )
-end
-
-to move ; turtle procedure
- rt random 50
- lt random 50
- fd 1
-end
-
-to eat-grass ; sheep procedure
- ; sheep eat grass, turn the patch brown
- if pcolor = green [
- set pcolor brown
- set energy energy + sheep-gain-from-food ; sheep gain energy by eating
- ]
-end
-
-to reproduce-sheep ; sheep procedure
- if random-float 100 < sheep-reproduce [ ; throw "dice" to see if you will reproduce
- set energy (energy / 2) ; divide energy between parent and offspring
- hatch 1 [ rt random-float 360 fd 1 ] ; hatch an offspring and move it forward 1 step
- ]
-end
-
-to reproduce-wolves ; wolf procedure
- if random-float 100 < wolf-reproduce [ ; throw "dice" to see if you will reproduce
- set energy (energy / 2) ; divide energy between parent and offspring
- hatch 1 [ rt random-float 360 fd 1 ] ; hatch an offspring and move it forward 1 step
- ]
-end
-
-to eat-sheep ; wolf procedure
- let prey one-of sheep-here ; grab a random sheep
- if prey != nobody [ ; did we get one? if so,
- ask prey [ die ] ; kill it, and...
- set energy energy + wolf-gain-from-food ; get energy from eating
- ]
-end
-
-to death ; turtle procedure (i.e. both wolf nd sheep procedure)
- ; when energy dips below zero, die
- if energy < 0 [ die ]
-end
-
-to grow-grass ; patch procedure
- ; countdown on brown patches: if reach 0, grow some grass
- if pcolor = brown [
- ifelse countdown <= 0
- [ set pcolor green
- set countdown grass-regrowth-time ]
- [ set countdown countdown - 1 ]
- ]
-end
-
-to-report grass
- ifelse model-version = "sheep-wolves-grass" [
- report patches with [pcolor = green]
- ]
- [ report 0 ]
-end
-
-
-to display-labels
- ask turtles [ set label "" ]
- if show-energy? [
- ask wolves [ set label round energy ]
- if model-version = "sheep-wolves-grass" [ ask sheep [ set label round energy ] ]
- ]
-end
-
-
-; Copyright 1997 Uri Wilensky.
-; See Info tab for full copyright and license.
-@#$#@#$#@
-GRAPHICS-WINDOW
-355
-10
-873
-529
--1
--1
-10.0
-1
-14
-1
-1
-1
-0
-1
-1
-1
--25
-25
--25
-25
-1
-1
-1
-ticks
-30.0
-
-SLIDER
-5
-60
-179
-93
-initial-number-sheep
-initial-number-sheep
-0
-250
-100.0
-1
-1
-NIL
-HORIZONTAL
-
-SLIDER
-5
-196
-179
-229
-sheep-gain-from-food
-sheep-gain-from-food
-0.0
-50.0
-4.0
-1.0
-1
-NIL
-HORIZONTAL
-
-SLIDER
-5
-231
-179
-264
-sheep-reproduce
-sheep-reproduce
-1.0
-20.0
-4.0
-1.0
-1
-%
-HORIZONTAL
-
-SLIDER
-185
-60
-350
-93
-initial-number-wolves
-initial-number-wolves
-0
-250
-50.0
-1
-1
-NIL
-HORIZONTAL
-
-SLIDER
-183
-195
-348
-228
-wolf-gain-from-food
-wolf-gain-from-food
-0.0
-100.0
-20.0
-1.0
-1
-NIL
-HORIZONTAL
-
-SLIDER
-183
-231
-348
-264
-wolf-reproduce
-wolf-reproduce
-0.0
-20.0
-6.0
-1.0
-1
-%
-HORIZONTAL
-
-SLIDER
-40
-100
-252
-133
-grass-regrowth-time
-grass-regrowth-time
-0
-100
-30.0
-1
-1
-NIL
-HORIZONTAL
-
-BUTTON
-40
-140
-109
-173
-setup
-setup
-NIL
-1
-T
-OBSERVER
-NIL
-NIL
-NIL
-NIL
-1
-
-BUTTON
-115
-140
-190
-173
-go
-go
-T
-1
-T
-OBSERVER
-NIL
-NIL
-NIL
-NIL
-0
-
-PLOT
-10
-360
-350
-530
-populations
-time
-pop.
-0.0
-100.0
-0.0
-100.0
-true
-true
-"" ""
-PENS
-"sheep" 1.0 0 -612749 true "" "plot count sheep"
-"wolves" 1.0 0 -16449023 true "" "plot count wolves"
-"grass / 4" 1.0 0 -10899396 true "" "if model-version = \"sheep-wolves-grass\" [ plot count grass / 4 ]"
-
-MONITOR
-41
-308
-111
-353
-sheep
-count sheep
-3
-1
-11
-
-MONITOR
-115
-308
-185
-353
-wolves
-count wolves
-3
-1
-11
-
-MONITOR
-191
-308
-256
-353
-grass
-count grass / 4
-0
-1
-11
-
-TEXTBOX
-20
-178
-160
-196
-Sheep settings
-11
-0.0
-0
-
-TEXTBOX
-198
-176
-311
-194
-Wolf settings
-11
-0.0
-0
-
-SWITCH
-105
-270
-241
-303
-show-energy?
-show-energy?
-1
-1
--1000
-
-CHOOSER
-5
-10
-350
-55
-model-version
-model-version
-"sheep-wolves" "sheep-wolves-grass"
-1
-
-BUTTON
-195
-140
-325
-173
-NIL
-show-pyplot
-NIL
-1
-T
-OBSERVER
-NIL
-NIL
-NIL
-NIL
-1
-
-@#$#@#$#@
-## WHAT IS IT?
-
-This model explores the stability of predator-prey ecosystems. Such a system is called unstable if it tends to result in extinction for one or more species involved. In contrast, a system is stable if it tends to maintain itself over time, despite fluctuations in population sizes.
-
-## HOW IT WORKS
-
-There are two main variations to this model.
-
-In the first variation, the "sheep-wolves" version, wolves and sheep wander randomly around the landscape, while the wolves look for sheep to prey on. Each step costs the wolves energy, and they must eat sheep in order to replenish their energy - when they run out of energy they die. To allow the population to continue, each wolf or sheep has a fixed probability of reproducing at each time step. In this variation, we model the grass as "infinite" so that sheep always have enough to eat, and we don't explicitly model the eating or growing of grass. As such, sheep don't either gain or lose energy by eating or moving. This variation produces interesting population dynamics, but is ultimately unstable. This variation of the model is particularly well-suited to interacting species in a rich nutrient environment, such as two strains of bacteria in a petri dish (Gause, 1934).
-
-The second variation, the "sheep-wolves-grass" version explictly models grass (green) in addition to wolves and sheep. The behavior of the wolves is identical to the first variation, however this time the sheep must eat grass in order to maintain their energy - when they run out of energy they die. Once grass is eaten it will only regrow after a fixed amount of time. This variation is more complex than the first, but it is generally stable. It is a closer match to the classic Lotka Volterra population oscillation models. The classic LV models though assume the populations can take on real values, but in small populations these models underestimate extinctions and agent-based models such as the ones here, provide more realistic results. (See Wilensky & Rand, 2015; chapter 4).
-
-The construction of this model is described in two papers by Wilensky & Reisman (1998; 2006) referenced below.
-
-## HOW TO USE IT
-
-1. Set the model-version chooser to "sheep-wolves-grass" to include grass eating and growth in the model, or to "sheep-wolves" to only include wolves (black) and sheep (white).
-2. Adjust the slider parameters (see below), or use the default settings.
-3. Press the SETUP button.
-4. Press the GO button to begin the simulation.
-5. Look at the monitors to see the current population sizes
-6. Look at the POPULATIONS plot to watch the populations fluctuate over time
-
-Parameters:
-MODEL-VERSION: Whether we model sheep wolves and grass or just sheep and wolves
-INITIAL-NUMBER-SHEEP: The initial size of sheep population
-INITIAL-NUMBER-WOLVES: The initial size of wolf population
-SHEEP-GAIN-FROM-FOOD: The amount of energy sheep get for every grass patch eaten (Note this is not used in the sheep-wolves model version)
-WOLF-GAIN-FROM-FOOD: The amount of energy wolves get for every sheep eaten
-SHEEP-REPRODUCE: The probability of a sheep reproducing at each time step
-WOLF-REPRODUCE: The probability of a wolf reproducing at each time step
-GRASS-REGROWTH-TIME: How long it takes for grass to regrow once it is eaten (Note this is not used in the sheep-wolves model version)
-SHOW-ENERGY?: Whether or not to show the energy of each animal as a number
-
-Notes:
-- one unit of energy is deducted for every step a wolf takes
-- when running the sheep-wolves-grass model version, one unit of energy is deducted for every step a sheep takes
-
-There are three monitors to show the populations of the wolves, sheep and grass and a populations plot to display the population values over time.
-
-If there are no wolves left and too many sheep, the model run stops.
-
-## THINGS TO NOTICE
-
-When running the sheep-wolves model variation, watch as the sheep and wolf populations fluctuate. Notice that increases and decreases in the sizes of each population are related. In what way are they related? What eventually happens?
-
-In the sheep-wolves-grass model variation, notice the green line added to the population plot representing fluctuations in the amount of grass. How do the sizes of the three populations appear to relate now? What is the explanation for this?
-
-Why do you suppose that some variations of the model might be stable while others are not?
-
-## THINGS TO TRY
-
-Try adjusting the parameters under various settings. How sensitive is the stability of the model to the particular parameters?
-
-Can you find any parameters that generate a stable ecosystem in the sheep-wolves model variation?
-
-Try running the sheep-wolves-grass model variation, but setting INITIAL-NUMBER-WOLVES to 0. This gives a stable ecosystem with only sheep and grass. Why might this be stable while the variation with only sheep and wolves is not?
-
-Notice that under stable settings, the populations tend to fluctuate at a predictable pace. Can you find any parameters that will speed this up or slow it down?
-
-## EXTENDING THE MODEL
-
-There are a number ways to alter the model so that it will be stable with only wolves and sheep (no grass). Some will require new elements to be coded in or existing behaviors to be changed. Can you develop such a version?
-
-Try changing the reproduction rules -- for example, what would happen if reproduction depended on energy rather than being determined by a fixed probability?
-
-Can you modify the model so the sheep will flock?
-
-Can you modify the model so that wolves actively chase sheep?
-
-## NETLOGO FEATURES
-
-Note the use of breeds to model two different kinds of "turtles": wolves and sheep. Note the use of patches to model grass.
-
-Note use of the ONE-OF agentset reporter to select a random sheep to be eaten by a wolf.
-
-## RELATED MODELS
-
-Look at Rabbits Grass Weeds for another model of interacting populations with different rules.
-
-## CREDITS AND REFERENCES
-
-Wilensky, U. & Reisman, K. (1998). Connected Science: Learning Biology through Constructing and Testing Computational Theories -- an Embodied Modeling Approach. International Journal of Complex Systems, M. 234, pp. 1 - 12. (The Wolf-Sheep-Predation model is a slightly extended version of the model described in the paper.)
-
-Wilensky, U. & Reisman, K. (2006). Thinking like a Wolf, a Sheep or a Firefly: Learning Biology through Constructing and Testing Computational Theories -- an Embodied Modeling Approach. Cognition & Instruction, 24(2), pp. 171-209. http://ccl.northwestern.edu/papers/wolfsheep.pdf .
-
-Wilensky, U., & Rand, W. (2015). An introduction to agent-based modeling: Modeling natural, social and engineered complex systems with NetLogo. Cambridge, MA: MIT Press.
-
-Lotka, A. J. (1925). Elements of physical biology. New York: Dover.
-
-Volterra, V. (1926, October 16). Fluctuations in the abundance of a species considered mathematically. Nature, 118, 558–560.
-
-Gause, G. F. (1934). The struggle for existence. Baltimore: Williams & Wilkins.
-
-## HOW TO CITE
-
-If you mention this model or the NetLogo software in a publication, we ask that you include the citations below.
-
-For the model itself:
-
-* Wilensky, U. (1997). NetLogo Wolf Sheep Predation model. http://ccl.northwestern.edu/netlogo/models/WolfSheepPredation. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.
-
-Please cite the NetLogo software as:
-
-* Wilensky, U. (1999). NetLogo. http://ccl.northwestern.edu/netlogo/. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.
-
-## COPYRIGHT AND LICENSE
-
-Copyright 1997 Uri Wilensky.
-
-
-
-This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit https://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
-
-Commercial licenses are also available. To inquire about commercial licenses, please contact Uri Wilensky at uri@northwestern.edu.
-
-This model was created as part of the project: CONNECTED MATHEMATICS: MAKING SENSE OF COMPLEX PHENOMENA THROUGH BUILDING OBJECT-BASED PARALLEL MODELS (OBPML). The project gratefully acknowledges the support of the National Science Foundation (Applications of Advanced Technologies Program) -- grant numbers RED #9552950 and REC #9632612.
-
-This model was converted to NetLogo as part of the projects: PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN CLASSROOMS and/or INTEGRATED SIMULATION AND MODELING ENVIRONMENT. The project gratefully acknowledges the support of the National Science Foundation (REPP & ROLE programs) -- grant numbers REC #9814682 and REC-0126227. Converted from StarLogoT to NetLogo, 2000.
-
-
-@#$#@#$#@
-default
-true
-0
-Polygon -7500403 true true 150 5 40 250 150 205 260 250
-
-airplane
-true
-0
-Polygon -7500403 true true 150 0 135 15 120 60 120 105 15 165 15 195 120 180 135 240 105 270 120 285 150 270 180 285 210 270 165 240 180 180 285 195 285 165 180 105 180 60 165 15
-
-arrow
-true
-0
-Polygon -7500403 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150
-
-box
-false
-0
-Polygon -7500403 true true 150 285 285 225 285 75 150 135
-Polygon -7500403 true true 150 135 15 75 150 15 285 75
-Polygon -7500403 true true 15 75 15 225 150 285 150 135
-Line -16777216 false 150 285 150 135
-Line -16777216 false 150 135 15 75
-Line -16777216 false 150 135 285 75
-
-bug
-true
-0
-Circle -7500403 true true 96 182 108
-Circle -7500403 true true 110 127 80
-Circle -7500403 true true 110 75 80
-Line -7500403 true 150 100 80 30
-Line -7500403 true 150 100 220 30
-
-butterfly
-true
-0
-Polygon -7500403 true true 150 165 209 199 225 225 225 255 195 270 165 255 150 240
-Polygon -7500403 true true 150 165 89 198 75 225 75 255 105 270 135 255 150 240
-Polygon -7500403 true true 139 148 100 105 55 90 25 90 10 105 10 135 25 180 40 195 85 194 139 163
-Polygon -7500403 true true 162 150 200 105 245 90 275 90 290 105 290 135 275 180 260 195 215 195 162 165
-Polygon -16777216 true false 150 255 135 225 120 150 135 120 150 105 165 120 180 150 165 225
-Circle -16777216 true false 135 90 30
-Line -16777216 false 150 105 195 60
-Line -16777216 false 150 105 105 60
-
-car
-false
-0
-Polygon -7500403 true true 300 180 279 164 261 144 240 135 226 132 213 106 203 84 185 63 159 50 135 50 75 60 0 150 0 165 0 225 300 225 300 180
-Circle -16777216 true false 180 180 90
-Circle -16777216 true false 30 180 90
-Polygon -16777216 true false 162 80 132 78 134 135 209 135 194 105 189 96 180 89
-Circle -7500403 true true 47 195 58
-Circle -7500403 true true 195 195 58
-
-circle
-false
-0
-Circle -7500403 true true 0 0 300
-
-circle 2
-false
-0
-Circle -7500403 true true 0 0 300
-Circle -16777216 true false 30 30 240
-
-cow
-false
-0
-Polygon -7500403 true true 200 193 197 249 179 249 177 196 166 187 140 189 93 191 78 179 72 211 49 209 48 181 37 149 25 120 25 89 45 72 103 84 179 75 198 76 252 64 272 81 293 103 285 121 255 121 242 118 224 167
-Polygon -7500403 true true 73 210 86 251 62 249 48 208
-Polygon -7500403 true true 25 114 16 195 9 204 23 213 25 200 39 123
-
-cylinder
-false
-0
-Circle -7500403 true true 0 0 300
-
-dot
-false
-0
-Circle -7500403 true true 90 90 120
-
-face happy
-false
-0
-Circle -7500403 true true 8 8 285
-Circle -16777216 true false 60 75 60
-Circle -16777216 true false 180 75 60
-Polygon -16777216 true false 150 255 90 239 62 213 47 191 67 179 90 203 109 218 150 225 192 218 210 203 227 181 251 194 236 217 212 240
-
-face neutral
-false
-0
-Circle -7500403 true true 8 7 285
-Circle -16777216 true false 60 75 60
-Circle -16777216 true false 180 75 60
-Rectangle -16777216 true false 60 195 240 225
-
-face sad
-false
-0
-Circle -7500403 true true 8 8 285
-Circle -16777216 true false 60 75 60
-Circle -16777216 true false 180 75 60
-Polygon -16777216 true false 150 168 90 184 62 210 47 232 67 244 90 220 109 205 150 198 192 205 210 220 227 242 251 229 236 206 212 183
-
-fish
-false
-0
-Polygon -1 true false 44 131 21 87 15 86 0 120 15 150 0 180 13 214 20 212 45 166
-Polygon -1 true false 135 195 119 235 95 218 76 210 46 204 60 165
-Polygon -1 true false 75 45 83 77 71 103 86 114 166 78 135 60
-Polygon -7500403 true true 30 136 151 77 226 81 280 119 292 146 292 160 287 170 270 195 195 210 151 212 30 166
-Circle -16777216 true false 215 106 30
-
-flag
-false
-0
-Rectangle -7500403 true true 60 15 75 300
-Polygon -7500403 true true 90 150 270 90 90 30
-Line -7500403 true 75 135 90 135
-Line -7500403 true 75 45 90 45
-
-flower
-false
-0
-Polygon -10899396 true false 135 120 165 165 180 210 180 240 150 300 165 300 195 240 195 195 165 135
-Circle -7500403 true true 85 132 38
-Circle -7500403 true true 130 147 38
-Circle -7500403 true true 192 85 38
-Circle -7500403 true true 85 40 38
-Circle -7500403 true true 177 40 38
-Circle -7500403 true true 177 132 38
-Circle -7500403 true true 70 85 38
-Circle -7500403 true true 130 25 38
-Circle -7500403 true true 96 51 108
-Circle -16777216 true false 113 68 74
-Polygon -10899396 true false 189 233 219 188 249 173 279 188 234 218
-Polygon -10899396 true false 180 255 150 210 105 210 75 240 135 240
-
-house
-false
-0
-Rectangle -7500403 true true 45 120 255 285
-Rectangle -16777216 true false 120 210 180 285
-Polygon -7500403 true true 15 120 150 15 285 120
-Line -16777216 false 30 120 270 120
-
-leaf
-false
-0
-Polygon -7500403 true true 150 210 135 195 120 210 60 210 30 195 60 180 60 165 15 135 30 120 15 105 40 104 45 90 60 90 90 105 105 120 120 120 105 60 120 60 135 30 150 15 165 30 180 60 195 60 180 120 195 120 210 105 240 90 255 90 263 104 285 105 270 120 285 135 240 165 240 180 270 195 240 210 180 210 165 195
-Polygon -7500403 true true 135 195 135 240 120 255 105 255 105 285 135 285 165 240 165 195
-
-line
-true
-0
-Line -7500403 true 150 0 150 300
-
-line half
-true
-0
-Line -7500403 true 150 0 150 150
-
-pentagon
-false
-0
-Polygon -7500403 true true 150 15 15 120 60 285 240 285 285 120
-
-person
-false
-0
-Circle -7500403 true true 110 5 80
-Polygon -7500403 true true 105 90 120 195 90 285 105 300 135 300 150 225 165 300 195 300 210 285 180 195 195 90
-Rectangle -7500403 true true 127 79 172 94
-Polygon -7500403 true true 195 90 240 150 225 180 165 105
-Polygon -7500403 true true 105 90 60 150 75 180 135 105
-
-plant
-false
-0
-Rectangle -7500403 true true 135 90 165 300
-Polygon -7500403 true true 135 255 90 210 45 195 75 255 135 285
-Polygon -7500403 true true 165 255 210 210 255 195 225 255 165 285
-Polygon -7500403 true true 135 180 90 135 45 120 75 180 135 210
-Polygon -7500403 true true 165 180 165 210 225 180 255 120 210 135
-Polygon -7500403 true true 135 105 90 60 45 45 75 105 135 135
-Polygon -7500403 true true 165 105 165 135 225 105 255 45 210 60
-Polygon -7500403 true true 135 90 120 45 150 15 180 45 165 90
-
-sheep
-false
-15
-Circle -1 true true 203 65 88
-Circle -1 true true 70 65 162
-Circle -1 true true 150 105 120
-Polygon -7500403 true false 218 120 240 165 255 165 278 120
-Circle -7500403 true false 214 72 67
-Rectangle -1 true true 164 223 179 298
-Polygon -1 true true 45 285 30 285 30 240 15 195 45 210
-Circle -1 true true 3 83 150
-Rectangle -1 true true 65 221 80 296
-Polygon -1 true true 195 285 210 285 210 240 240 210 195 210
-Polygon -7500403 true false 276 85 285 105 302 99 294 83
-Polygon -7500403 true false 219 85 210 105 193 99 201 83
-
-square
-false
-0
-Rectangle -7500403 true true 30 30 270 270
-
-square 2
-false
-0
-Rectangle -7500403 true true 30 30 270 270
-Rectangle -16777216 true false 60 60 240 240
-
-star
-false
-0
-Polygon -7500403 true true 151 1 185 108 298 108 207 175 242 282 151 216 59 282 94 175 3 108 116 108
-
-target
-false
-0
-Circle -7500403 true true 0 0 300
-Circle -16777216 true false 30 30 240
-Circle -7500403 true true 60 60 180
-Circle -16777216 true false 90 90 120
-Circle -7500403 true true 120 120 60
-
-tree
-false
-0
-Circle -7500403 true true 118 3 94
-Rectangle -6459832 true false 120 195 180 300
-Circle -7500403 true true 65 21 108
-Circle -7500403 true true 116 41 127
-Circle -7500403 true true 45 90 120
-Circle -7500403 true true 104 74 152
-
-triangle
-false
-0
-Polygon -7500403 true true 150 30 15 255 285 255
-
-triangle 2
-false
-0
-Polygon -7500403 true true 150 30 15 255 285 255
-Polygon -16777216 true false 151 99 225 223 75 224
-
-truck
-false
-0
-Rectangle -7500403 true true 4 45 195 187
-Polygon -7500403 true true 296 193 296 150 259 134 244 104 208 104 207 194
-Rectangle -1 true false 195 60 195 105
-Polygon -16777216 true false 238 112 252 141 219 141 218 112
-Circle -16777216 true false 234 174 42
-Rectangle -7500403 true true 181 185 214 194
-Circle -16777216 true false 144 174 42
-Circle -16777216 true false 24 174 42
-Circle -7500403 false true 24 174 42
-Circle -7500403 false true 144 174 42
-Circle -7500403 false true 234 174 42
-
-turtle
-true
-0
-Polygon -10899396 true false 215 204 240 233 246 254 228 266 215 252 193 210
-Polygon -10899396 true false 195 90 225 75 245 75 260 89 269 108 261 124 240 105 225 105 210 105
-Polygon -10899396 true false 105 90 75 75 55 75 40 89 31 108 39 124 60 105 75 105 90 105
-Polygon -10899396 true false 132 85 134 64 107 51 108 17 150 2 192 18 192 52 169 65 172 87
-Polygon -10899396 true false 85 204 60 233 54 254 72 266 85 252 107 210
-Polygon -7500403 true true 119 75 179 75 209 101 224 135 220 225 175 261 128 261 81 224 74 135 88 99
-
-wheel
-false
-0
-Circle -7500403 true true 3 3 294
-Circle -16777216 true false 30 30 240
-Line -7500403 true 150 285 150 15
-Line -7500403 true 15 150 285 150
-Circle -7500403 true true 120 120 60
-Line -7500403 true 216 40 79 269
-Line -7500403 true 40 84 269 221
-Line -7500403 true 40 216 269 79
-Line -7500403 true 84 40 221 269
-
-wolf
-false
-0
-Polygon -16777216 true false 253 133 245 131 245 133
-Polygon -7500403 true true 2 194 13 197 30 191 38 193 38 205 20 226 20 257 27 265 38 266 40 260 31 253 31 230 60 206 68 198 75 209 66 228 65 243 82 261 84 268 100 267 103 261 77 239 79 231 100 207 98 196 119 201 143 202 160 195 166 210 172 213 173 238 167 251 160 248 154 265 169 264 178 247 186 240 198 260 200 271 217 271 219 262 207 258 195 230 192 198 210 184 227 164 242 144 259 145 284 151 277 141 293 140 299 134 297 127 273 119 270 105
-Polygon -7500403 true true -1 195 14 180 36 166 40 153 53 140 82 131 134 133 159 126 188 115 227 108 236 102 238 98 268 86 269 92 281 87 269 103 269 113
-
-x
-false
-0
-Polygon -7500403 true true 270 75 225 30 30 225 75 270
-Polygon -7500403 true true 30 75 75 30 270 225 225 270
-@#$#@#$#@
-NetLogo 6.2.2
-@#$#@#$#@
-set model-version "sheep-wolves-grass"
-set show-energy? false
-setup
-repeat 75 [ go ]
-@#$#@#$#@
-@#$#@#$#@
-@#$#@#$#@
-@#$#@#$#@
-default
-0.0
--0.2 0 0.0 1.0
-0.0 1 1.0 0.0
-0.2 0 0.0 1.0
-link direction
-true
-0
-Line -7500403 true 150 150 90 180
-Line -7500403 true 150 150 210 180
-@#$#@#$#@
-1
-@#$#@#$#@
diff --git a/demos/Wolf Sheep Predation - Static 3D Plot.nlogox b/demos/Wolf Sheep Predation - Static 3D Plot.nlogox
new file mode 100644
index 0000000..7d9a268
--- /dev/null
+++ b/demos/Wolf Sheep Predation - Static 3D Plot.nlogox
@@ -0,0 +1,1183 @@
+
+
+ max-sheep [ user-message "The sheep have inherited the earth" stop ]
+ ask sheep [
+ move
+ if model-version = "sheep-wolves-grass" [ ; in this version, sheep eat grass, grass grows and it costs sheep energy to move
+ set energy energy - 1 ; deduct energy for sheep only if running sheep-wolf-grass model version
+ eat-grass ; sheep eat grass only if running sheep-wolf-grass model version
+ death ; sheep die from starvation only if running sheep-wolf-grass model version
+ ]
+ reproduce-sheep ; sheep reproduce at random rate governed by slider
+ ]
+ ask wolves [
+ move
+ set energy energy - 1 ; wolves lose energy as they move
+ eat-sheep ; wolves eat a sheep on their patch
+ death ; wolves die if our of energy
+ reproduce-wolves ; wolves reproduce at random rate governed by slider
+ ]
+ if model-version = "sheep-wolves-grass" [ ask patches [ grow-grass ] ]
+ ; set grass count patches with [pcolor = green]
+ update-stats
+ tick
+ display-labels
+end
+
+to update-stats
+ set sheep-data lput count sheep sheep-data
+ set wolf-data lput count wolves wolf-data
+ set grass-data lput count grass grass-data
+end
+
+to show-pyplot
+ py:set "s" sheep-data
+ py:set "w" wolf-data
+ py:set "g" grass-data
+ (py:run
+ "fig = plt.figure()"
+ "ax = fig.add_subplot(111, projection='3d')"
+ "ax.plot(s,w,g)"
+ "ax.set_xlabel('sheep')"
+ "ax.set_ylabel('wolves')"
+ "ax.set_zlabel('grass')"
+ "plt.show()"
+ )
+end
+
+to move ; turtle procedure
+ rt random 50
+ lt random 50
+ fd 1
+end
+
+to eat-grass ; sheep procedure
+ ; sheep eat grass, turn the patch brown
+ if pcolor = green [
+ set pcolor brown
+ set energy energy + sheep-gain-from-food ; sheep gain energy by eating
+ ]
+end
+
+to reproduce-sheep ; sheep procedure
+ if random-float 100 < sheep-reproduce [ ; throw "dice" to see if you will reproduce
+ set energy (energy / 2) ; divide energy between parent and offspring
+ hatch 1 [ rt random-float 360 fd 1 ] ; hatch an offspring and move it forward 1 step
+ ]
+end
+
+to reproduce-wolves ; wolf procedure
+ if random-float 100 < wolf-reproduce [ ; throw "dice" to see if you will reproduce
+ set energy (energy / 2) ; divide energy between parent and offspring
+ hatch 1 [ rt random-float 360 fd 1 ] ; hatch an offspring and move it forward 1 step
+ ]
+end
+
+to eat-sheep ; wolf procedure
+ let prey one-of sheep-here ; grab a random sheep
+ if prey != nobody [ ; did we get one? if so,
+ ask prey [ die ] ; kill it, and...
+ set energy energy + wolf-gain-from-food ; get energy from eating
+ ]
+end
+
+to death ; turtle procedure (i.e. both wolf nd sheep procedure)
+ ; when energy dips below zero, die
+ if energy < 0 [ die ]
+end
+
+to grow-grass ; patch procedure
+ ; countdown on brown patches: if reach 0, grow some grass
+ if pcolor = brown [
+ ifelse countdown <= 0
+ [ set pcolor green
+ set countdown grass-regrowth-time ]
+ [ set countdown countdown - 1 ]
+ ]
+end
+
+to-report grass
+ ifelse model-version = "sheep-wolves-grass" [
+ report patches with [pcolor = green]
+ ]
+ [ report 0 ]
+end
+
+
+to display-labels
+ ask turtles [ set label "" ]
+ if show-energy? [
+ ask wolves [ set label round energy ]
+ if model-version = "sheep-wolves-grass" [ ask sheep [ set label round energy ] ]
+ ]
+end
+
+
+; Copyright 1997 Uri Wilensky.
+; See Info tab for full copyright and license.]]>
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ plot count sheep
+
+
+
+ plot count wolves
+
+
+
+ if model-version = "sheep-wolves-grass" [ plot count grass / 4 ]
+
+
+ count sheep
+ count wolves
+ count grass / 4
+ Sheep settings
+ Wolf settings
+
+
+
+
+
+
+
+ ]]>
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ set model-version "sheep-wolves-grass"
+set show-energy? false
+setup
+repeat 75 [ go ]
+
diff --git a/demos/digit-prediction/digits_prediction.nlogo b/demos/digit-prediction/digits_prediction.nlogo
deleted file mode 100644
index 1f13716..0000000
--- a/demos/digit-prediction/digits_prediction.nlogo
+++ /dev/null
@@ -1,629 +0,0 @@
-extensions [ py ]
-
-globals [ coords-x coords-y digit probabilities ]
-
-to setup-py
- clear-all
- py:setup py:python
- (py:run
- "import numpy as np"
- "from keras.models import load_model"
- )
- ;; load model
- py:run "model = load_model(\"model.h5\")"
-
- set coords-x (range min-pxcor (max-pxcor + 1))
- set coords-y (range max-pycor (min-pycor - 1) -1)
-
- set probabilities n-values 10 [ 0 ]
-end
-
-to draw
- if mouse-down?
- [
- ask patch mouse-xcor mouse-ycor
- [
- ifelse erase?
- [
- set pcolor black
- ]
- [
- set pcolor white
- ]
- predict
- ]
- ]
- display
-end
-
-to predict
- py:set "images" map [ y ->
- map [ x ->
- [ pcolor / 10 ] of patch x y
- ] coords-x
- ] coords-y
- py:run "array = np.array(images).reshape(1, 28, 28, 1)"
- py:run "y_pred = model.predict(array)[0]"
- set digit py:runresult "np.argmax(y_pred)"
-
- ;; return probabilities per digit (softmax layer)
- set probabilities py:runresult "y_pred"
-end
-
-to clear
- clear-patches
- predict
-end
-@#$#@#$#@
-GRAPHICS-WINDOW
-297
-37
-687
-428
--1
--1
-13.643
-1
-10
-1
-1
-1
-0
-0
-0
-1
-0
-27
-0
-27
-0
-0
-1
-ticks
-30.0
-
-BUTTON
-11
-30
-155
-63
-NIL
-setup-py
-NIL
-1
-T
-OBSERVER
-NIL
-NIL
-NIL
-NIL
-1
-
-BUTTON
-13
-71
-76
-104
-NIL
-draw
-T
-1
-T
-OBSERVER
-NIL
-NIL
-NIL
-NIL
-1
-
-BUTTON
-90
-71
-153
-104
-NIL
-clear
-NIL
-1
-T
-OBSERVER
-NIL
-NIL
-NIL
-NIL
-1
-
-SWITCH
-12
-118
-115
-151
-erase?
-erase?
-1
-1
--1000
-
-MONITOR
-168
-33
-284
-90
-Prediction digit
-digit
-2
-1
-14
-
-MONITOR
-21
-171
-129
-224
-Prob. of 0 [%]
-100 * item 0 probabilities
-5
-1
-13
-
-MONITOR
-138
-171
-246
-224
-Prob. of 1 [%]
-100 * item 1 probabilities
-5
-1
-13
-
-MONITOR
-20
-230
-128
-283
-Prob. of 2 [%]
-100 * item 2 probabilities
-5
-1
-13
-
-MONITOR
-138
-232
-246
-285
-Prob. of 3 [%]
-100 * item 3 probabilities
-5
-1
-13
-
-MONITOR
-19
-290
-127
-343
-Prob. of 4 [%]
-100 * item 4 probabilities
-5
-1
-13
-
-MONITOR
-138
-291
-246
-344
-Prob. of 5 [%]
-100 * item 5 probabilities
-5
-1
-13
-
-MONITOR
-19
-349
-127
-402
-Prob. of 6 [%]
-100 * item 6 probabilities
-5
-1
-13
-
-MONITOR
-139
-349
-247
-402
-Prob. of 7 [%]
-100 * item 7 probabilities
-5
-1
-13
-
-MONITOR
-19
-410
-127
-463
-Prob. of 8 [%]
-100 * item 8 probabilities
-5
-1
-13
-
-MONITOR
-139
-411
-247
-464
-Prob. of 9 [%]
-100 * item 9 probabilities
-5
-1
-13
-
-@#$#@#$#@
-## WHAT IS IT?
-
-The model predict digits written on the patch. The model was pretrained on MNIST dataset using convolutional neural network (CNN) based on [Kaggle implementation](https://www.kaggle.com/yassineghouzam/introduction-to-cnn-keras-0-997-top-6). It can recognised digits from 0 to 9 (multilabel classification).
-
-## HOW IT WORKS
-
-The input to neural network is array created from the colors of the patch. Next the neural network compute its weights and return the number from 0 to 9.
-
-## HOW TO USE IT
-
-1. Click _setup-py_ to import all necessary libraries and load model (it can take a while)
-2. Click _draw_ to start drawing on the board
-3. While the mouse is not pressed, on the monitor widget it should be displayed predicted digit
-4. On multiple monitors are showing probabilities from softmax neural network layer
-5. Click _clear_ button to set all patches to black (reset)
-6. Turn on _erase?_ to set pen to black color
-
-## THINGS TO NOTICE
-
-The model is not 100% accurate and sometimes show ridiculous result.
-
-Notice the need for the `Python` packages:
-- **keras** (also **tensorflow** as a backend)
-- **numpy**
-
-## EXTENDING THE MODEL
-
-Create new model in `Python` e.g for letters recognition.
-
-## NETLOGO FEATURES
-
-Usage of `map` in `map` to get pcolor of the patches (_predict_ function).
-
-## COPYRIGHT
-
-Copyright 2018 Robert Jankowski.
-@#$#@#$#@
-default
-true
-0
-Polygon -7500403 true true 150 5 40 250 150 205 260 250
-
-airplane
-true
-0
-Polygon -7500403 true true 150 0 135 15 120 60 120 105 15 165 15 195 120 180 135 240 105 270 120 285 150 270 180 285 210 270 165 240 180 180 285 195 285 165 180 105 180 60 165 15
-
-arrow
-true
-0
-Polygon -7500403 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150
-
-box
-false
-0
-Polygon -7500403 true true 150 285 285 225 285 75 150 135
-Polygon -7500403 true true 150 135 15 75 150 15 285 75
-Polygon -7500403 true true 15 75 15 225 150 285 150 135
-Line -16777216 false 150 285 150 135
-Line -16777216 false 150 135 15 75
-Line -16777216 false 150 135 285 75
-
-bug
-true
-0
-Circle -7500403 true true 96 182 108
-Circle -7500403 true true 110 127 80
-Circle -7500403 true true 110 75 80
-Line -7500403 true 150 100 80 30
-Line -7500403 true 150 100 220 30
-
-butterfly
-true
-0
-Polygon -7500403 true true 150 165 209 199 225 225 225 255 195 270 165 255 150 240
-Polygon -7500403 true true 150 165 89 198 75 225 75 255 105 270 135 255 150 240
-Polygon -7500403 true true 139 148 100 105 55 90 25 90 10 105 10 135 25 180 40 195 85 194 139 163
-Polygon -7500403 true true 162 150 200 105 245 90 275 90 290 105 290 135 275 180 260 195 215 195 162 165
-Polygon -16777216 true false 150 255 135 225 120 150 135 120 150 105 165 120 180 150 165 225
-Circle -16777216 true false 135 90 30
-Line -16777216 false 150 105 195 60
-Line -16777216 false 150 105 105 60
-
-car
-false
-0
-Polygon -7500403 true true 300 180 279 164 261 144 240 135 226 132 213 106 203 84 185 63 159 50 135 50 75 60 0 150 0 165 0 225 300 225 300 180
-Circle -16777216 true false 180 180 90
-Circle -16777216 true false 30 180 90
-Polygon -16777216 true false 162 80 132 78 134 135 209 135 194 105 189 96 180 89
-Circle -7500403 true true 47 195 58
-Circle -7500403 true true 195 195 58
-
-circle
-false
-0
-Circle -7500403 true true 0 0 300
-
-circle 2
-false
-0
-Circle -7500403 true true 0 0 300
-Circle -16777216 true false 30 30 240
-
-cow
-false
-0
-Polygon -7500403 true true 200 193 197 249 179 249 177 196 166 187 140 189 93 191 78 179 72 211 49 209 48 181 37 149 25 120 25 89 45 72 103 84 179 75 198 76 252 64 272 81 293 103 285 121 255 121 242 118 224 167
-Polygon -7500403 true true 73 210 86 251 62 249 48 208
-Polygon -7500403 true true 25 114 16 195 9 204 23 213 25 200 39 123
-
-cylinder
-false
-0
-Circle -7500403 true true 0 0 300
-
-dot
-false
-0
-Circle -7500403 true true 90 90 120
-
-face happy
-false
-0
-Circle -7500403 true true 8 8 285
-Circle -16777216 true false 60 75 60
-Circle -16777216 true false 180 75 60
-Polygon -16777216 true false 150 255 90 239 62 213 47 191 67 179 90 203 109 218 150 225 192 218 210 203 227 181 251 194 236 217 212 240
-
-face neutral
-false
-0
-Circle -7500403 true true 8 7 285
-Circle -16777216 true false 60 75 60
-Circle -16777216 true false 180 75 60
-Rectangle -16777216 true false 60 195 240 225
-
-face sad
-false
-0
-Circle -7500403 true true 8 8 285
-Circle -16777216 true false 60 75 60
-Circle -16777216 true false 180 75 60
-Polygon -16777216 true false 150 168 90 184 62 210 47 232 67 244 90 220 109 205 150 198 192 205 210 220 227 242 251 229 236 206 212 183
-
-fish
-false
-0
-Polygon -1 true false 44 131 21 87 15 86 0 120 15 150 0 180 13 214 20 212 45 166
-Polygon -1 true false 135 195 119 235 95 218 76 210 46 204 60 165
-Polygon -1 true false 75 45 83 77 71 103 86 114 166 78 135 60
-Polygon -7500403 true true 30 136 151 77 226 81 280 119 292 146 292 160 287 170 270 195 195 210 151 212 30 166
-Circle -16777216 true false 215 106 30
-
-flag
-false
-0
-Rectangle -7500403 true true 60 15 75 300
-Polygon -7500403 true true 90 150 270 90 90 30
-Line -7500403 true 75 135 90 135
-Line -7500403 true 75 45 90 45
-
-flower
-false
-0
-Polygon -10899396 true false 135 120 165 165 180 210 180 240 150 300 165 300 195 240 195 195 165 135
-Circle -7500403 true true 85 132 38
-Circle -7500403 true true 130 147 38
-Circle -7500403 true true 192 85 38
-Circle -7500403 true true 85 40 38
-Circle -7500403 true true 177 40 38
-Circle -7500403 true true 177 132 38
-Circle -7500403 true true 70 85 38
-Circle -7500403 true true 130 25 38
-Circle -7500403 true true 96 51 108
-Circle -16777216 true false 113 68 74
-Polygon -10899396 true false 189 233 219 188 249 173 279 188 234 218
-Polygon -10899396 true false 180 255 150 210 105 210 75 240 135 240
-
-house
-false
-0
-Rectangle -7500403 true true 45 120 255 285
-Rectangle -16777216 true false 120 210 180 285
-Polygon -7500403 true true 15 120 150 15 285 120
-Line -16777216 false 30 120 270 120
-
-leaf
-false
-0
-Polygon -7500403 true true 150 210 135 195 120 210 60 210 30 195 60 180 60 165 15 135 30 120 15 105 40 104 45 90 60 90 90 105 105 120 120 120 105 60 120 60 135 30 150 15 165 30 180 60 195 60 180 120 195 120 210 105 240 90 255 90 263 104 285 105 270 120 285 135 240 165 240 180 270 195 240 210 180 210 165 195
-Polygon -7500403 true true 135 195 135 240 120 255 105 255 105 285 135 285 165 240 165 195
-
-line
-true
-0
-Line -7500403 true 150 0 150 300
-
-line half
-true
-0
-Line -7500403 true 150 0 150 150
-
-pentagon
-false
-0
-Polygon -7500403 true true 150 15 15 120 60 285 240 285 285 120
-
-person
-false
-0
-Circle -7500403 true true 110 5 80
-Polygon -7500403 true true 105 90 120 195 90 285 105 300 135 300 150 225 165 300 195 300 210 285 180 195 195 90
-Rectangle -7500403 true true 127 79 172 94
-Polygon -7500403 true true 195 90 240 150 225 180 165 105
-Polygon -7500403 true true 105 90 60 150 75 180 135 105
-
-plant
-false
-0
-Rectangle -7500403 true true 135 90 165 300
-Polygon -7500403 true true 135 255 90 210 45 195 75 255 135 285
-Polygon -7500403 true true 165 255 210 210 255 195 225 255 165 285
-Polygon -7500403 true true 135 180 90 135 45 120 75 180 135 210
-Polygon -7500403 true true 165 180 165 210 225 180 255 120 210 135
-Polygon -7500403 true true 135 105 90 60 45 45 75 105 135 135
-Polygon -7500403 true true 165 105 165 135 225 105 255 45 210 60
-Polygon -7500403 true true 135 90 120 45 150 15 180 45 165 90
-
-sheep
-false
-15
-Circle -1 true true 203 65 88
-Circle -1 true true 70 65 162
-Circle -1 true true 150 105 120
-Polygon -7500403 true false 218 120 240 165 255 165 278 120
-Circle -7500403 true false 214 72 67
-Rectangle -1 true true 164 223 179 298
-Polygon -1 true true 45 285 30 285 30 240 15 195 45 210
-Circle -1 true true 3 83 150
-Rectangle -1 true true 65 221 80 296
-Polygon -1 true true 195 285 210 285 210 240 240 210 195 210
-Polygon -7500403 true false 276 85 285 105 302 99 294 83
-Polygon -7500403 true false 219 85 210 105 193 99 201 83
-
-square
-false
-0
-Rectangle -7500403 true true 30 30 270 270
-
-square 2
-false
-0
-Rectangle -7500403 true true 30 30 270 270
-Rectangle -16777216 true false 60 60 240 240
-
-star
-false
-0
-Polygon -7500403 true true 151 1 185 108 298 108 207 175 242 282 151 216 59 282 94 175 3 108 116 108
-
-target
-false
-0
-Circle -7500403 true true 0 0 300
-Circle -16777216 true false 30 30 240
-Circle -7500403 true true 60 60 180
-Circle -16777216 true false 90 90 120
-Circle -7500403 true true 120 120 60
-
-tree
-false
-0
-Circle -7500403 true true 118 3 94
-Rectangle -6459832 true false 120 195 180 300
-Circle -7500403 true true 65 21 108
-Circle -7500403 true true 116 41 127
-Circle -7500403 true true 45 90 120
-Circle -7500403 true true 104 74 152
-
-triangle
-false
-0
-Polygon -7500403 true true 150 30 15 255 285 255
-
-triangle 2
-false
-0
-Polygon -7500403 true true 150 30 15 255 285 255
-Polygon -16777216 true false 151 99 225 223 75 224
-
-truck
-false
-0
-Rectangle -7500403 true true 4 45 195 187
-Polygon -7500403 true true 296 193 296 150 259 134 244 104 208 104 207 194
-Rectangle -1 true false 195 60 195 105
-Polygon -16777216 true false 238 112 252 141 219 141 218 112
-Circle -16777216 true false 234 174 42
-Rectangle -7500403 true true 181 185 214 194
-Circle -16777216 true false 144 174 42
-Circle -16777216 true false 24 174 42
-Circle -7500403 false true 24 174 42
-Circle -7500403 false true 144 174 42
-Circle -7500403 false true 234 174 42
-
-turtle
-true
-0
-Polygon -10899396 true false 215 204 240 233 246 254 228 266 215 252 193 210
-Polygon -10899396 true false 195 90 225 75 245 75 260 89 269 108 261 124 240 105 225 105 210 105
-Polygon -10899396 true false 105 90 75 75 55 75 40 89 31 108 39 124 60 105 75 105 90 105
-Polygon -10899396 true false 132 85 134 64 107 51 108 17 150 2 192 18 192 52 169 65 172 87
-Polygon -10899396 true false 85 204 60 233 54 254 72 266 85 252 107 210
-Polygon -7500403 true true 119 75 179 75 209 101 224 135 220 225 175 261 128 261 81 224 74 135 88 99
-
-wheel
-false
-0
-Circle -7500403 true true 3 3 294
-Circle -16777216 true false 30 30 240
-Line -7500403 true 150 285 150 15
-Line -7500403 true 15 150 285 150
-Circle -7500403 true true 120 120 60
-Line -7500403 true 216 40 79 269
-Line -7500403 true 40 84 269 221
-Line -7500403 true 40 216 269 79
-Line -7500403 true 84 40 221 269
-
-wolf
-false
-0
-Polygon -16777216 true false 253 133 245 131 245 133
-Polygon -7500403 true true 2 194 13 197 30 191 38 193 38 205 20 226 20 257 27 265 38 266 40 260 31 253 31 230 60 206 68 198 75 209 66 228 65 243 82 261 84 268 100 267 103 261 77 239 79 231 100 207 98 196 119 201 143 202 160 195 166 210 172 213 173 238 167 251 160 248 154 265 169 264 178 247 186 240 198 260 200 271 217 271 219 262 207 258 195 230 192 198 210 184 227 164 242 144 259 145 284 151 277 141 293 140 299 134 297 127 273 119 270 105
-Polygon -7500403 true true -1 195 14 180 36 166 40 153 53 140 82 131 134 133 159 126 188 115 227 108 236 102 238 98 268 86 269 92 281 87 269 103 269 113
-
-x
-false
-0
-Polygon -7500403 true true 270 75 225 30 30 225 75 270
-Polygon -7500403 true true 30 75 75 30 270 225 225 270
-@#$#@#$#@
-NetLogo 6.2.2
-@#$#@#$#@
-@#$#@#$#@
-@#$#@#$#@
-@#$#@#$#@
-@#$#@#$#@
-default
-0.0
--0.2 0 0.0 1.0
-0.0 1 1.0 0.0
-0.2 0 0.0 1.0
-link direction
-true
-0
-Line -7500403 true 150 150 90 180
-Line -7500403 true 150 150 210 180
-@#$#@#$#@
-0
-@#$#@#$#@
diff --git a/demos/digit-prediction/digits_prediction.nlogox b/demos/digit-prediction/digits_prediction.nlogox
new file mode 100644
index 0000000..2215d07
--- /dev/null
+++ b/demos/digit-prediction/digits_prediction.nlogox
@@ -0,0 +1,947 @@
+
+
+
+ map [ x ->
+ [ pcolor / 10 ] of patch x y
+ ] coords-x
+ ] coords-y
+ py:run "array = np.array(images).reshape(1, 28, 28, 1)"
+ py:run "y_pred = model.predict(array)[0]"
+ set digit py:runresult "np.argmax(y_pred)"
+
+ ;; return probabilities per digit (softmax layer)
+ set probabilities py:runresult "y_pred"
+end
+
+to clear
+ clear-patches
+ predict
+end]]>
+
+
+
+
+
+
+ digit
+ 100 * item 0 probabilities
+ 100 * item 1 probabilities
+ 100 * item 2 probabilities
+ 100 * item 3 probabilities
+ 100 * item 4 probabilities
+ 100 * item 5 probabilities
+ 100 * item 6 probabilities
+ 100 * item 7 probabilities
+ 100 * item 8 probabilities
+ 100 * item 9 probabilities
+
+ ## WHAT IS IT?
+
+The model predict digits written on the patch. The model was pretrained on MNIST dataset using convolutional neural network (CNN) based on [Kaggle implementation](https://www.kaggle.com/yassineghouzam/introduction-to-cnn-keras-0-997-top-6). It can recognised digits from 0 to 9 (multilabel classification).
+
+## HOW IT WORKS
+
+The input to neural network is array created from the colors of the patch. Next the neural network compute its weights and return the number from 0 to 9.
+
+## HOW TO USE IT
+
+1. Click _setup-py_ to import all necessary libraries and load model (it can take a while)
+2. Click _draw_ to start drawing on the board
+3. While the mouse is not pressed, on the monitor widget it should be displayed predicted digit
+4. On multiple monitors are showing probabilities from softmax neural network layer
+5. Click _clear_ button to set all patches to black (reset)
+6. Turn on _erase?_ to set pen to black color
+
+## THINGS TO NOTICE
+
+The model is not 100% accurate and sometimes show ridiculous result.
+
+Notice the need for the `Python` packages:
+- **keras** (also **tensorflow** as a backend)
+- **numpy**
+
+## EXTENDING THE MODEL
+
+Create new model in `Python` e.g for letters recognition.
+
+## NETLOGO FEATURES
+
+Usage of `map` in `map` to get pcolor of the patches (_predict_ function).
+
+## COPYRIGHT
+
+Copyright 2018 Robert Jankowski.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ setup repeat 75 [ go ]
+
diff --git a/project/build.properties b/project/build.properties
index 0837f7a..563a014 100644
--- a/project/build.properties
+++ b/project/build.properties
@@ -1 +1 @@
-sbt.version=1.3.13
+sbt.version=1.7.2
diff --git a/project/plugins.sbt b/project/plugins.sbt
index 4aa7d5d..fedf988 100644
--- a/project/plugins.sbt
+++ b/project/plugins.sbt
@@ -3,5 +3,5 @@ resolvers ++= Seq(
, "netlogo-extension-documentation" at "https://dl.cloudsmith.io/public/netlogo/netlogo-extension-documentation/maven/"
)
-addSbtPlugin("org.nlogo" % "netlogo-extension-plugin" % "5.2.2")
+addSbtPlugin("org.nlogo" % "netlogo-extension-plugin" % "7.0.2")
addSbtPlugin("org.nlogo" % "netlogo-extension-documentation" % "0.8.3")
diff --git a/src/main/PythonExtension.scala b/src/main/PythonExtension.scala
index 118642d..58e4089 100644
--- a/src/main/PythonExtension.scala
+++ b/src/main/PythonExtension.scala
@@ -1,32 +1,39 @@
package org.nlogo.extensions.py
+import com.fasterxml.jackson.core.json.JsonReadFeature
+import com.fasterxml.jackson.databind.json.JsonMapper
+import com.fasterxml.jackson.databind.ObjectMapper
+
import java.awt.GraphicsEnvironment
import java.io.{ BufferedReader, Closeable, File, IOException, InputStreamReader }
import java.lang.ProcessBuilder.Redirect
import java.nio.file.Paths
-import com.fasterxml.jackson.core.JsonParser
-import org.json4s.jackson.JsonMethods.mapper
+import org.json4s.jackson.{ JsonMethods, Json4sScalaModule }
import org.nlogo.languagelibrary.Subprocess.path
-import org.nlogo.languagelibrary.{ ShellWindow, Subprocess }
-import org.nlogo.languagelibrary.config.{ Config, FileProperty, Menu, Platform }
+import org.nlogo.languagelibrary.Subprocess
+import org.nlogo.languagelibrary.config.{ Config, ConfigProperty, FileProperty, Menu }
import org.nlogo.api
import org.nlogo.api._
import org.nlogo.core.{ LogoList, Syntax }
+import org.nlogo.workspace.AbstractWorkspace
+
+import scala.collection.immutable.ArraySeq
object PythonExtension {
val codeName = "py"
val longName = "Python"
- val extLangBin = if (Platform.isWindows) { "python" } else { "python3" }
+ val extLangBin = if (System.getProperty("os.name").toLowerCase.startsWith("win")) { "python" } else { "python3" }
private var _pythonProcess: Option[Subprocess] = None
- var shellWindow: Option[ShellWindow] = None
var menu: Option[Menu] = None
val config: Config = Config.createForPropertyFile(classOf[PythonExtension], PythonExtension.codeName)
+ var isHeadless = true
+
def pythonProcess: Subprocess = {
_pythonProcess.getOrElse(throw new ExtensionException(
"Python process has not been started. Please run PY:SETUP before any other python extension primitive."))
@@ -42,42 +49,44 @@ object PythonExtension {
_pythonProcess = None
}
- def isHeadless: Boolean = GraphicsEnvironment.isHeadless || System.getProperty("org.nlogo.preferHeadless") == "true"
-
}
class PythonExtension extends api.DefaultClassManager {
-
override def load(manager: api.PrimitiveManager): Unit = {
manager.addPrimitive("setup", SetupPython)
manager.addPrimitive("run", Run)
manager.addPrimitive("runresult", RunResult)
manager.addPrimitive("set", Set)
manager.addPrimitive("python2",
- FindPython(PythonSubprocess.python2 _)
+ FindPython(() => PythonSubprocess.python2)
)
manager.addPrimitive("python3",
- FindPython(PythonSubprocess.python3 _)
+ FindPython(() => PythonSubprocess.python3)
)
manager.addPrimitive("python",
- FindPython(PythonSubprocess.anyPython _)
+ FindPython(() => PythonSubprocess.anyPython)
)
manager.addPrimitive("__path", Path)
}
override def runOnce(em: ExtensionManager): Unit = {
super.runOnce(em)
- mapper.configure(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS, true)
- val py2Message = s"It is recommended to use Python 3 if possible and enter its path above. If you must use Python 2, enter the path to its executable folder below."
- val py2Property = new FileProperty("python2", "python2", PythonExtension.config.get("python2").getOrElse(""), py2Message)
- PythonExtension.menu = Menu.create(PythonExtension.longName, PythonExtension.extLangBin, PythonExtension.config, Seq(py2Property))
+ PythonExtension.isHeadless = !em.workspaceContext.workspaceGUI
+
+ if (!PythonExtension.isHeadless) {
+ val py2Message = s"It is recommended to use Python 3 if possible and enter its path above. If you must use Python 2, enter the path to its executable folder below."
+ val py2Property = new FileProperty("python2", "python2", PythonExtension.config.get("python2").getOrElse(""), py2Message)
+ PythonExtension.menu = Menu.create(em, PythonExtension.longName, PythonExtension.extLangBin, PythonExtension.config, Seq(py2Property))
+ }
}
override def unload(em: ExtensionManager): Unit = {
super.unload(em)
PythonExtension.killPython()
- PythonExtension.menu.foreach(_.unload())
+
+ if (!PythonExtension.isHeadless)
+ PythonExtension.menu.foreach(_.unload())
}
}
@@ -93,7 +102,7 @@ object Using {
object PythonSubprocess {
def python2: Option[File] = {
val maybePy2File = PythonExtension.config.get("python2").map( (dir) => {
- val bin = if (Platform.isWindows) { "python.exe" } else { "python2" }
+ val bin = if (System.getProperty("os.name").toLowerCase.startsWith("win")) { "python.exe" } else { "python2" }
val path = Paths.get(dir, bin)
new File(path.toString)
})
@@ -111,8 +120,8 @@ object PythonSubprocess {
def anyPython: Option[File] = python3 orElse python2
- def pythons: Stream[PythonBinary] =
- path.toStream
+ def pythons: LazyList[PythonBinary] =
+ path.to(LazyList)
.flatMap(_.listFiles((_, name) => name.toLowerCase.matches(raw"python[\d\.]*(?:\.exe)??")))
.flatMap(PythonBinary.fromFile)
}
@@ -124,13 +133,21 @@ object SetupPython extends api.Command {
override def perform(args: Array[Argument], context: Context): Unit = {
val pyExtensionDirectory = Config.getExtensionRuntimeDirectory(classOf[PythonExtension], PythonExtension.codeName)
- val pythonCmd = args.map(_.getString)
+ val pythonCmd = ArraySeq.unsafeWrapArray(args.map(_.getString))
val maybePyFile = new File(pyExtensionDirectory, "pyext.py")
val pyFile = if (maybePyFile.exists) { maybePyFile } else { (new File("pyext.py")).getCanonicalFile }
val pyScript: String = pyFile.toString
try {
- PythonExtension.pythonProcess = Subprocess.start(context.workspace, pythonCmd, Seq(pyScript), PythonExtension.codeName, PythonExtension.longName)
- PythonExtension.menu.foreach(_.setup(PythonExtension.pythonProcess.evalStringified))
+ val mapper = new JsonMethods {
+ override def mapper: ObjectMapper =
+ JsonMapper.builder.addModule(new Json4sScalaModule).enable(JsonReadFeature.ALLOW_NON_NUMERIC_NUMBERS).build()
+ }
+ PythonExtension.pythonProcess = Subprocess.start(context.workspace, pythonCmd, Seq(pyScript),
+ PythonExtension.codeName, PythonExtension.longName,
+ customMapper = Option(mapper))
+
+ if (!PythonExtension.isHeadless)
+ PythonExtension.menu.foreach(_.setup(PythonExtension.pythonProcess.evalStringified))
} catch {
case e: Exception =>
// Different errors can manifest in different operating systems. Thus, rather than dispatching in the specific
diff --git a/src/test/Tests.scala b/src/test/Tests.scala
index 8ed8527..7a162fd 100644
--- a/src/test/Tests.scala
+++ b/src/test/Tests.scala
@@ -3,6 +3,7 @@
package org.nlogo.extensions.py
import java.io.File
+
import org.nlogo.headless.TestLanguage
object Tests {
@@ -10,6 +11,4 @@ object Tests {
val testFiles = testFileNames.map( (f) => (new File(f)).getCanonicalFile )
}
-class Tests extends TestLanguage(Tests.testFiles) {
- System.setProperty("org.nlogo.preferHeadless", "true")
-}
+class Tests extends TestLanguage(Tests.testFiles)
diff --git a/tests.txt b/tests.txt
index a5c3f77..4965571 100644
--- a/tests.txt
+++ b/tests.txt
@@ -6,75 +6,75 @@
# extensions [ py ]
# O> py:setup "foobarbaz" => ERROR Extension exception: Couldn't find Python executable: foobarbaz
-open-connection-python2
- extensions [ py ]
- O> py:setup py:python2
- O> py:run "print('python hi1!')"
- O> py:run "print('python hi2!')"
-
-python2-gets-python-2
- extensions [ py ]
- O> py:setup py:python2
- O> py:run "import sys"
- py:runresult "sys.version_info[0]" => 2
-
-basic-python-types-convert-python2
- extensions [ py ]
- O> py:setup py:python2
- py:runresult "1" => 1
- py:runresult "'hi'" => "hi"
- py:runresult "True" => true
- py:runresult "False" => false
- py:runresult "[1, 'hi', [2, 'bye']]" => [1 "hi" [2 "bye"]]
-
-basic-netlogo-types-convert-python2
- extensions [ py ]
- O> py:setup py:python2
- O> py:set "num" 1
- O> py:set "string" "hi"
- O> py:set "lst" [1 "hi" [2 "bye"]]
- O> py:set "b" true
- py:runresult "num" => 1
- py:runresult "string" => "hi"
- py:runresult "lst" => [1 "hi" [2 "bye"]]
- py:runresult "b" => true
-
-error-handling-python2
- extensions [ py ]
- O> py:setup py:python2
- O> py:run "raise Exception('hi')" => ERROR Extension exception: hi
- py:runresult "1 / 0" => ERROR Extension exception: integer division or modulo by zero
-
-dicts-convert-to-lists-python2
- extensions [ py ]
- O> py:setup py:python2
- py:runresult "{'a': 1}" => [["a" 1]]
-
-sets-convert-to-lists-python2
- extensions [ py ]
- O> py:setup py:python2
- py:runresult "{1, 1}" => [1]
-
-np-array-convert-to-lists-python2
- extensions [ py ]
- O> py:setup py:python2
- O> py:run "import numpy as np"
- py:runresult "np.array([1,2,3])" => [1 2 3]
-
-supports-utf8-python2
- extensions [ py ]
- O> py:setup py:python2
- py:runresult "'😃'" => "😃"
-
-errors-on-nan-python2
- extensions [ py ]
- O> py:setup py:python2
- py:runresult "float('nan')" => ERROR Extension exception: Python reported a non-numeric value from a mathematical operation.
-
-errors-on-infinity-python2
- extensions [ py ]
- O> py:setup py:python2
- py:runresult "float('inf')" => ERROR Extension exception: Python reported a number too large for NetLogo.
+# open-connection-python2
+# extensions [ py ]
+# O> py:setup py:python2
+# O> py:run "print('python hi1!')"
+# O> py:run "print('python hi2!')"
+#
+# python2-gets-python-2
+# extensions [ py ]
+# O> py:setup py:python2
+# O> py:run "import sys"
+# py:runresult "sys.version_info[0]" => 2
+#
+# basic-python-types-convert-python2
+# extensions [ py ]
+# O> py:setup py:python2
+# py:runresult "1" => 1
+# py:runresult "'hi'" => "hi"
+# py:runresult "True" => true
+# py:runresult "False" => false
+# py:runresult "[1, 'hi', [2, 'bye']]" => [1 "hi" [2 "bye"]]
+#
+# basic-netlogo-types-convert-python2
+# extensions [ py ]
+# O> py:setup py:python2
+# O> py:set "num" 1
+# O> py:set "string" "hi"
+# O> py:set "lst" [1 "hi" [2 "bye"]]
+# O> py:set "b" true
+# py:runresult "num" => 1
+# py:runresult "string" => "hi"
+# py:runresult "lst" => [1 "hi" [2 "bye"]]
+# py:runresult "b" => true
+#
+# error-handling-python2
+# extensions [ py ]
+# O> py:setup py:python2
+# O> py:run "raise Exception('hi')" => ERROR Extension exception: hi
+# py:runresult "1 / 0" => ERROR Extension exception: integer division or modulo by zero
+#
+# dicts-convert-to-lists-python2
+# extensions [ py ]
+# O> py:setup py:python2
+# py:runresult "{'a': 1}" => [["a" 1]]
+#
+# sets-convert-to-lists-python2
+# extensions [ py ]
+# O> py:setup py:python2
+# py:runresult "{1, 1}" => [1]
+#
+# np-array-convert-to-lists-python2
+# extensions [ py ]
+# O> py:setup py:python2
+# O> py:run "import numpy as np"
+# py:runresult "np.array([1,2,3])" => [1 2 3]
+#
+# supports-utf8-python2
+# extensions [ py ]
+# O> py:setup py:python2
+# py:runresult "'😃'" => "😃"
+#
+# errors-on-nan-python2
+# extensions [ py ]
+# O> py:setup py:python2
+# py:runresult "float('nan')" => ERROR Extension exception: Python reported a non-numeric value from a mathematical operation.
+#
+# errors-on-infinity-python2
+# extensions [ py ]
+# O> py:setup py:python2
+# py:runresult "float('inf')" => ERROR Extension exception: Python reported a number too large for NetLogo.
open-connection-python3
extensions [ py ]