netlogopy 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- netlogopy-0.1.0/Examples/complexe/27-external-model.py +106 -0
- netlogopy-0.1.0/Examples/complexe/Fire/Fire.py +44 -0
- netlogopy-0.1.0/Examples/complexe/Wolf Sheep/Wolf.py +44 -0
- netlogopy-0.1.0/Examples/complexe/test/Fire + Wolf Sheep/Fire + Wolf Sheep.py +65 -0
- netlogopy-0.1.0/Examples/complexe/test/Fire + Wolf Sheep.py +65 -0
- netlogopy-0.1.0/Examples/complexe/test/run_both_models.py +37 -0
- netlogopy-0.1.0/Examples/complexe/test/run_fire_model.py +23 -0
- netlogopy-0.1.0/Examples/complexe/test/run_wolf_sheep_model.py +23 -0
- netlogopy-0.1.0/Examples/simple/1-pyturtle.py +15 -0
- netlogopy-0.1.0/Examples/simple/10-set_shape.py +21 -0
- netlogopy-0.1.0/Examples/simple/11-getxyturtle.py +24 -0
- netlogopy-0.1.0/Examples/simple/12-setxy.py +21 -0
- netlogopy-0.1.0/Examples/simple/13-distancebetween.py +20 -0
- netlogopy-0.1.0/Examples/simple/14-rt_lt.py +27 -0
- netlogopy-0.1.0/Examples/simple/15-bk.py +26 -0
- netlogopy-0.1.0/Examples/simple/16-set_heading.py +25 -0
- netlogopy-0.1.0/Examples/simple/17-set_color.py +26 -0
- netlogopy-0.1.0/Examples/simple/18-set_size.py +24 -0
- netlogopy-0.1.0/Examples/simple/19-showturtle.py +26 -0
- netlogopy-0.1.0/Examples/simple/2-set_background.py +19 -0
- netlogopy-0.1.0/Examples/simple/20-pen_draw.py +33 -0
- netlogopy-0.1.0/Examples/simple/21-count_turtles.py +35 -0
- netlogopy-0.1.0/Examples/simple/22-ticks.py +24 -0
- netlogopy-0.1.0/Examples/simple/23-slider.py +44 -0
- netlogopy-0.1.0/Examples/simple/24-switch.py +44 -0
- netlogopy-0.1.0/Examples/simple/25-monitor.py +36 -0
- netlogopy-0.1.0/Examples/simple/26-plot.py +67 -0
- netlogopy-0.1.0/Examples/simple/3-street.py +20 -0
- netlogopy-0.1.0/Examples/simple/4-fd.py +18 -0
- netlogopy-0.1.0/Examples/simple/5-netlogoshow.py +21 -0
- netlogopy-0.1.0/Examples/simple/6-distanceto.py +21 -0
- netlogopy-0.1.0/Examples/simple/7-face_to.py +20 -0
- netlogopy-0.1.0/Examples/simple/8-move_to.py +21 -0
- netlogopy-0.1.0/Examples/simple/9-hideturtle.py +21 -0
- netlogopy-0.1.0/LICENSE +21 -0
- netlogopy-0.1.0/MANIFEST.in +5 -0
- netlogopy-0.1.0/PKG-INFO +516 -0
- netlogopy-0.1.0/README.md +466 -0
- netlogopy-0.1.0/environment.yml +11 -0
- netlogopy-0.1.0/netlogopy/__init__.py +89 -0
- netlogopy-0.1.0/netlogopy/base.nls +334 -0
- netlogopy-0.1.0/netlogopy/netlogopy.nlogo +3854 -0
- netlogopy-0.1.0/netlogopy/netlogopy.py +644 -0
- netlogopy-0.1.0/netlogopy.egg-info/PKG-INFO +516 -0
- netlogopy-0.1.0/netlogopy.egg-info/SOURCES.txt +49 -0
- netlogopy-0.1.0/netlogopy.egg-info/dependency_links.txt +1 -0
- netlogopy-0.1.0/netlogopy.egg-info/requires.txt +1 -0
- netlogopy-0.1.0/netlogopy.egg-info/top_level.txt +4 -0
- netlogopy-0.1.0/pyproject.toml +37 -0
- netlogopy-0.1.0/setup.cfg +4 -0
- netlogopy-0.1.0/setup.py +53 -0
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Example 27 : Integrating an Existing NetLogo Model with Python
|
|
3
|
+
==============================================================
|
|
4
|
+
This example shows the correct way to control an *existing* NetLogo model
|
|
5
|
+
(one that was NOT created by netlogopy) from Python.
|
|
6
|
+
|
|
7
|
+
KEY DIFFERENCE vs the built-in model:
|
|
8
|
+
• Use run_netlogo_model() instead of run_netlogo()
|
|
9
|
+
→ does NOT call setup automatically; lets you configure widgets first
|
|
10
|
+
• Use resize_world_direct() instead of resize_world()
|
|
11
|
+
→ uses a native NetLogo command; does not require base.nls
|
|
12
|
+
|
|
13
|
+
Workflow:
|
|
14
|
+
1. run_netlogo_model() ← open the .nlogo file
|
|
15
|
+
2. set_slider() / set_switch() ← configure parameters (before setup)
|
|
16
|
+
3. run_command(n, "setup") ← run the model's own setup
|
|
17
|
+
4. add pyturtle agents ← mix Python agents into the model
|
|
18
|
+
5. loop: run_command(n, "go") + move pyturtles + read monitors
|
|
19
|
+
|
|
20
|
+
Widget functions used:
|
|
21
|
+
get_slider / set_slider → Slider widgets (global variables)
|
|
22
|
+
get_switch / set_switch → Switch widgets (boolean globals)
|
|
23
|
+
get_chooser / set_chooser → Chooser widgets
|
|
24
|
+
get_monitor / get_monitor → any NetLogo reporter expression
|
|
25
|
+
set_current_plot / plot_add → Plot widgets
|
|
26
|
+
|
|
27
|
+
Note: This example targets the classic "Wolf Sheep Predation" model that
|
|
28
|
+
ships with NetLogo. Adjust path_model to point to any .nlogo file you want.
|
|
29
|
+
"""
|
|
30
|
+
import time, os, sys
|
|
31
|
+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
|
|
32
|
+
from netlogopy.netlogopy import *
|
|
33
|
+
|
|
34
|
+
if __name__ == "__main__":
|
|
35
|
+
netlogo_home = "C:/Program Files/NetLogo 6.2.2"
|
|
36
|
+
|
|
37
|
+
# Path to an existing NetLogo model
|
|
38
|
+
# (e.g. the Wolf Sheep model bundled with NetLogo)
|
|
39
|
+
path_model = (
|
|
40
|
+
"C:/Program Files/NetLogo 6.2.2/models/Sample Models/"
|
|
41
|
+
"Biology/Wolf Sheep Predation.nlogo"
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
# ── Step 1 : open the model WITHOUT auto-setup ────────────────────────────
|
|
45
|
+
n = run_netlogo_model(netlogo_home, path_model)
|
|
46
|
+
print("Model opened.")
|
|
47
|
+
|
|
48
|
+
# ── Step 2 : configure Slider / Switch widgets before setup ───────────────
|
|
49
|
+
# These global variables must exist in the .nlogo model.
|
|
50
|
+
set_slider(n, "initial-number-sheep", 100)
|
|
51
|
+
set_slider(n, "initial-number-wolves", 50)
|
|
52
|
+
set_slider(n, "grass-regrowth-time", 30)
|
|
53
|
+
set_switch(n, "grass?", True)
|
|
54
|
+
|
|
55
|
+
print("Sliders configured:")
|
|
56
|
+
print(f" sheep = {get_slider(n, 'initial-number-sheep'):.0f}")
|
|
57
|
+
print(f" wolves = {get_slider(n, 'initial-number-wolves'):.0f}")
|
|
58
|
+
print(f" grass-time = {get_slider(n, 'grass-regrowth-time'):.0f}")
|
|
59
|
+
print(f" grass? = {get_switch(n, 'grass?')}")
|
|
60
|
+
|
|
61
|
+
# ── Step 3 : resize world (direct command — no base.nls needed) ──────────
|
|
62
|
+
resize_world_direct(n, -25, 25, -25, 25)
|
|
63
|
+
|
|
64
|
+
# ── Step 4 : run the model's own setup ───────────────────────────────────
|
|
65
|
+
run_command(n, "setup")
|
|
66
|
+
reset_ticks(n)
|
|
67
|
+
print("Setup complete.")
|
|
68
|
+
|
|
69
|
+
# ── Step 5 : add Python-controlled agents into the running model ──────────
|
|
70
|
+
observer = pyturtle(n, x=0, y=0, shape="circle", size_shape=3,
|
|
71
|
+
color=105, name="observer")
|
|
72
|
+
|
|
73
|
+
# ── Step 6 : main loop ────────────────────────────────────────────────────
|
|
74
|
+
for i in range(40):
|
|
75
|
+
time.sleep(0.2)
|
|
76
|
+
|
|
77
|
+
# Advance the model one step
|
|
78
|
+
run_command(n, "go")
|
|
79
|
+
do_tick(n)
|
|
80
|
+
|
|
81
|
+
# Read monitor values (any valid NetLogo reporter)
|
|
82
|
+
sheep = get_monitor(n, "count sheep")
|
|
83
|
+
wolves = get_monitor(n, "count wolves")
|
|
84
|
+
grass = get_monitor(n, "count patches with [pcolor = green]")
|
|
85
|
+
tick = get_ticks(n)
|
|
86
|
+
|
|
87
|
+
print(f"[tick {tick:3d}] sheep={sheep} wolves={wolves} grass={grass}")
|
|
88
|
+
|
|
89
|
+
# Move the Python observer in circles
|
|
90
|
+
observer.rt(9)
|
|
91
|
+
observer.fd(1)
|
|
92
|
+
observer.set_label(f"t={tick}")
|
|
93
|
+
|
|
94
|
+
# Dynamically adjust grass regrowth speed based on sheep population
|
|
95
|
+
if isinstance(sheep, str):
|
|
96
|
+
sheep_count = int(float(sheep)) if sheep else 0
|
|
97
|
+
else:
|
|
98
|
+
sheep_count = int(sheep)
|
|
99
|
+
|
|
100
|
+
if sheep_count < 30:
|
|
101
|
+
set_slider(n, "grass-regrowth-time", 10) # speed up grass
|
|
102
|
+
elif sheep_count > 150:
|
|
103
|
+
set_slider(n, "grass-regrowth-time", 50) # slow down grass
|
|
104
|
+
|
|
105
|
+
print("Done.")
|
|
106
|
+
n.close_model()
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import time,os,random
|
|
2
|
+
import sys
|
|
3
|
+
import os
|
|
4
|
+
|
|
5
|
+
# Get the parent directory path
|
|
6
|
+
parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../"))
|
|
7
|
+
|
|
8
|
+
# Add the parent directory to the system path
|
|
9
|
+
sys.path.insert(0, parent_dir)
|
|
10
|
+
from netlogopy.netlogopy import *
|
|
11
|
+
|
|
12
|
+
os.system('cls')
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
if __name__ == "__main__" :
|
|
17
|
+
dest = os.path.dirname(__file__)
|
|
18
|
+
|
|
19
|
+
netlogo_home="C:/Program Files/NetLogo 6.2.2"
|
|
20
|
+
path_model = os.path.dirname(__file__)+"\\Fire.nlogo"
|
|
21
|
+
# n=run_netlogo(netlogo_home)
|
|
22
|
+
n=run_netlogo(netlogo_home,path_model)
|
|
23
|
+
|
|
24
|
+
# n=run_netlogo(netlogo_home)
|
|
25
|
+
|
|
26
|
+
# Resize world
|
|
27
|
+
resize_world(n,0,70,0,55)
|
|
28
|
+
# Change path fo image
|
|
29
|
+
# path_image= os.path.dirname(__file__)+"/netlogopy.png"
|
|
30
|
+
|
|
31
|
+
path_image = "D:/cphs2024/netlogopy.png"
|
|
32
|
+
run_command(n,"setup")
|
|
33
|
+
|
|
34
|
+
car01=pyturtle(n,x=20,y=20,shape="car",size_shape=4,color=15,name="car01",labelcolor=15)
|
|
35
|
+
car02=pyturtle(n,x=5,y=5,shape="car",size_shape=4,color=55,name="car02",labelcolor=55)
|
|
36
|
+
street( n,fromm=car01,too=car02,label="street",labelcolor=35,color=35,shape="default",thickness=0.5)
|
|
37
|
+
for i in range(0,100):
|
|
38
|
+
run_command(n,"go")
|
|
39
|
+
time.sleep(0.1)
|
|
40
|
+
print("*********** ",i," ********")
|
|
41
|
+
car01.fd(1)
|
|
42
|
+
if i%20== 0 :
|
|
43
|
+
car01.setxy(10,10)
|
|
44
|
+
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import time,os,random
|
|
2
|
+
import sys
|
|
3
|
+
import os
|
|
4
|
+
|
|
5
|
+
# Get the parent directory path
|
|
6
|
+
parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../"))
|
|
7
|
+
|
|
8
|
+
# Add the parent directory to the system path
|
|
9
|
+
sys.path.insert(0, parent_dir)
|
|
10
|
+
from netlogopy.netlogopy import *
|
|
11
|
+
|
|
12
|
+
os.system('cls')
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
if __name__ == "__main__" :
|
|
17
|
+
dest = os.path.dirname(__file__)
|
|
18
|
+
|
|
19
|
+
netlogo_home="C:/Program Files/NetLogo 6.2.2"
|
|
20
|
+
path_model = os.path.dirname(__file__)+"\\Wolf Sheep Predation.nlogo"
|
|
21
|
+
# n=run_netlogo(netlogo_home)
|
|
22
|
+
n=run_netlogo(netlogo_home,path_model)
|
|
23
|
+
|
|
24
|
+
# n=run_netlogo(netlogo_home)
|
|
25
|
+
|
|
26
|
+
# Resize world
|
|
27
|
+
resize_world(n,0,70,0,55)
|
|
28
|
+
# Change path fo image
|
|
29
|
+
# path_image= os.path.dirname(__file__)+"/netlogopy.png"
|
|
30
|
+
|
|
31
|
+
path_image = "D:/cphs2024/netlogopy.png"
|
|
32
|
+
run_command(n,"setup")
|
|
33
|
+
|
|
34
|
+
car01=pyturtle(n,x=20,y=20,shape="car",size_shape=4,color=15,name="car01",labelcolor=15)
|
|
35
|
+
car02=pyturtle(n,x=5,y=5,shape="car",size_shape=4,color=55,name="car02",labelcolor=55)
|
|
36
|
+
street( n,fromm=car01,too=car02,label="street",labelcolor=35,color=35,shape="default",thickness=0.5)
|
|
37
|
+
for i in range(0,100):
|
|
38
|
+
run_command(n,"go")
|
|
39
|
+
time.sleep(0.1)
|
|
40
|
+
print("*********** ",i," ********")
|
|
41
|
+
car01.fd(1)
|
|
42
|
+
if i%20== 0 :
|
|
43
|
+
car01.setxy(10,10)
|
|
44
|
+
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import time
|
|
2
|
+
import os
|
|
3
|
+
import multiprocessing
|
|
4
|
+
import nl4py
|
|
5
|
+
|
|
6
|
+
def run_model_in_process(netlogo_home, model_path, lock, output_file_path):
|
|
7
|
+
try:
|
|
8
|
+
# Initialize NetLogo within the process
|
|
9
|
+
nl4py.initialize(netlogo_home)
|
|
10
|
+
netlogo_app = nl4py.netlogo_app()
|
|
11
|
+
netlogo_app.open_model(model_path)
|
|
12
|
+
netlogo_app.command("setup")
|
|
13
|
+
for i in range(10):
|
|
14
|
+
# Simulate some model action
|
|
15
|
+
netlogo_app.command("go")
|
|
16
|
+
# Prepare the data to write
|
|
17
|
+
data_to_write = f"{model_path} iteration {i}\n"
|
|
18
|
+
# Acquire the lock before writing to the file
|
|
19
|
+
with lock:
|
|
20
|
+
with open(output_file_path, 'a') as f:
|
|
21
|
+
f.write(data_to_write)
|
|
22
|
+
# Print to the console
|
|
23
|
+
print(f"{model_path} {i}")
|
|
24
|
+
time.sleep(1)
|
|
25
|
+
netlogo_app.command("quit")
|
|
26
|
+
time.sleep(5)
|
|
27
|
+
except Exception as e:
|
|
28
|
+
# Capture any exceptions and write them to the output file
|
|
29
|
+
error_message = f"Error in process for model {model_path}: {str(e)}\n"
|
|
30
|
+
with lock:
|
|
31
|
+
with open(output_file_path, 'a') as f:
|
|
32
|
+
f.write(error_message)
|
|
33
|
+
print(error_message)
|
|
34
|
+
|
|
35
|
+
if __name__ == "__main__":
|
|
36
|
+
netlogo_home = "C:/Program Files/NetLogo 6.2.2"
|
|
37
|
+
|
|
38
|
+
# Ensure that model paths are correct
|
|
39
|
+
current_dir = os.path.dirname(__file__)
|
|
40
|
+
path_model1 = os.path.join(current_dir, "Fire.nlogo")
|
|
41
|
+
path_model2 = os.path.join(current_dir, "Wolf Sheep Predation.nlogo")
|
|
42
|
+
|
|
43
|
+
# Print model paths to verify
|
|
44
|
+
print(f"Model 1 path: {path_model1}")
|
|
45
|
+
print(f"Model 2 path: {path_model2}")
|
|
46
|
+
|
|
47
|
+
# Define the output file path
|
|
48
|
+
output_file_path = os.path.join(current_dir, "output.txt")
|
|
49
|
+
|
|
50
|
+
# Create a lock for synchronized file access
|
|
51
|
+
lock = multiprocessing.Lock()
|
|
52
|
+
|
|
53
|
+
# Create processes, passing the lock and output file path
|
|
54
|
+
p1 = multiprocessing.Process(target=run_model_in_process, args=(netlogo_home, path_model1, lock, output_file_path))
|
|
55
|
+
p2 = multiprocessing.Process(target=run_model_in_process, args=(netlogo_home, path_model2, lock, output_file_path))
|
|
56
|
+
|
|
57
|
+
# Start the processes
|
|
58
|
+
p1.start()
|
|
59
|
+
p2.start()
|
|
60
|
+
|
|
61
|
+
# Wait for the processes to finish
|
|
62
|
+
p1.join()
|
|
63
|
+
p2.join()
|
|
64
|
+
|
|
65
|
+
print("Both models have finished running.")
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import time
|
|
2
|
+
import os
|
|
3
|
+
import multiprocessing
|
|
4
|
+
import nl4py
|
|
5
|
+
|
|
6
|
+
def run_model_in_process(netlogo_home, model_path, lock, output_file_path):
|
|
7
|
+
try:
|
|
8
|
+
# Initialize NetLogo within the process
|
|
9
|
+
nl4py.initialize(netlogo_home)
|
|
10
|
+
netlogo_app = nl4py.netlogo_app()
|
|
11
|
+
netlogo_app.open_model(model_path)
|
|
12
|
+
netlogo_app.command("setup")
|
|
13
|
+
for i in range(10):
|
|
14
|
+
# Simulate some model action
|
|
15
|
+
netlogo_app.command("go")
|
|
16
|
+
# Prepare the data to write
|
|
17
|
+
data_to_write = f"{model_path} iteration {i}\n"
|
|
18
|
+
# Acquire the lock before writing to the file
|
|
19
|
+
with lock:
|
|
20
|
+
with open(output_file_path, 'a') as f:
|
|
21
|
+
f.write(data_to_write)
|
|
22
|
+
# Print to the console
|
|
23
|
+
print(f"{model_path} {i}")
|
|
24
|
+
time.sleep(1)
|
|
25
|
+
netlogo_app.command("quit")
|
|
26
|
+
time.sleep(5)
|
|
27
|
+
except Exception as e:
|
|
28
|
+
# Capture any exceptions and write them to the output file
|
|
29
|
+
error_message = f"Error in process for model {model_path}: {str(e)}\n"
|
|
30
|
+
with lock:
|
|
31
|
+
with open(output_file_path, 'a') as f:
|
|
32
|
+
f.write(error_message)
|
|
33
|
+
print(error_message)
|
|
34
|
+
|
|
35
|
+
if __name__ == "__main__":
|
|
36
|
+
netlogo_home = "C:/Program Files/NetLogo 6.2.2"
|
|
37
|
+
|
|
38
|
+
# Ensure that model paths are correct
|
|
39
|
+
current_dir = os.path.dirname(__file__)
|
|
40
|
+
path_model1 = os.path.join(current_dir, "Fire.nlogo")
|
|
41
|
+
path_model2 = os.path.join(current_dir, "Wolf Sheep Predation.nlogo")
|
|
42
|
+
|
|
43
|
+
# Print model paths to verify
|
|
44
|
+
print(f"Model 1 path: {path_model1}")
|
|
45
|
+
print(f"Model 2 path: {path_model2}")
|
|
46
|
+
|
|
47
|
+
# Define the output file path
|
|
48
|
+
output_file_path = os.path.join(current_dir, "output.txt")
|
|
49
|
+
|
|
50
|
+
# Create a lock for synchronized file access
|
|
51
|
+
lock = multiprocessing.Lock()
|
|
52
|
+
|
|
53
|
+
# Create processes, passing the lock and output file path
|
|
54
|
+
p1 = multiprocessing.Process(target=run_model_in_process, args=(netlogo_home, path_model1, lock, output_file_path))
|
|
55
|
+
p2 = multiprocessing.Process(target=run_model_in_process, args=(netlogo_home, path_model2, lock, output_file_path))
|
|
56
|
+
|
|
57
|
+
# Start the processes
|
|
58
|
+
p1.start()
|
|
59
|
+
p2.start()
|
|
60
|
+
|
|
61
|
+
# Wait for the processes to finish
|
|
62
|
+
p1.join()
|
|
63
|
+
p2.join()
|
|
64
|
+
|
|
65
|
+
print("Both models have finished running.")
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import subprocess
|
|
2
|
+
import sys
|
|
3
|
+
import os
|
|
4
|
+
|
|
5
|
+
python_executable = sys.executable
|
|
6
|
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
7
|
+
fire_model_script = os.path.join(current_dir, 'run_fire_model.py')
|
|
8
|
+
wolf_sheep_script = os.path.join(current_dir, 'run_wolf_sheep_model.py')
|
|
9
|
+
|
|
10
|
+
print(f"Running {fire_model_script}")
|
|
11
|
+
print(f"Running {wolf_sheep_script}")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
p2 = subprocess.Popen(
|
|
15
|
+
[python_executable, wolf_sheep_script],
|
|
16
|
+
stdout=subprocess.PIPE,
|
|
17
|
+
stderr=subprocess.PIPE,
|
|
18
|
+
text=True
|
|
19
|
+
)
|
|
20
|
+
p1 = subprocess.Popen(
|
|
21
|
+
[python_executable, fire_model_script],
|
|
22
|
+
stdout=subprocess.PIPE,
|
|
23
|
+
stderr=subprocess.PIPE,
|
|
24
|
+
text=True
|
|
25
|
+
)
|
|
26
|
+
stdout2, stderr2 = p2.communicate()
|
|
27
|
+
stdout1, stderr1 = p1.communicate()
|
|
28
|
+
|
|
29
|
+
print("Output from run_fire_model.py:")
|
|
30
|
+
print(stdout1)
|
|
31
|
+
print("Errors from run_fire_model.py:")
|
|
32
|
+
print(stderr1)
|
|
33
|
+
|
|
34
|
+
print("Output from run_wolf_sheep_model.py:")
|
|
35
|
+
print(stdout2)
|
|
36
|
+
print("Errors from run_wolf_sheep_model.py:")
|
|
37
|
+
print(stderr2)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import nl4py
|
|
2
|
+
import os
|
|
3
|
+
import time
|
|
4
|
+
|
|
5
|
+
netlogo_home = "C:/Program Files/NetLogo 6.2.2"
|
|
6
|
+
model_path = os.path.join(os.path.dirname(__file__), "Fire.nlogo")
|
|
7
|
+
output_file_path = os.path.join(os.path.dirname(__file__), "output_fire.txt")
|
|
8
|
+
|
|
9
|
+
# Initialize NetLogo
|
|
10
|
+
nl4py.initialize(netlogo_home)
|
|
11
|
+
netlogo_app = nl4py.netlogo_app()
|
|
12
|
+
netlogo_app.open_model(model_path)
|
|
13
|
+
netlogo_app.command("setup")
|
|
14
|
+
|
|
15
|
+
for i in range(10):
|
|
16
|
+
netlogo_app.command("go")
|
|
17
|
+
data_to_write = f"{model_path} iteration {i}\n"
|
|
18
|
+
with open(output_file_path, 'a') as f:
|
|
19
|
+
f.write(data_to_write)
|
|
20
|
+
print(f"{model_path} {i}")
|
|
21
|
+
time.sleep(1)
|
|
22
|
+
|
|
23
|
+
netlogo_app.command("quit")
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import nl4py
|
|
2
|
+
import os
|
|
3
|
+
import time
|
|
4
|
+
|
|
5
|
+
netlogo_home = "C:/Program Files/NetLogo 6.2.2"
|
|
6
|
+
model_path = os.path.join(os.path.dirname(__file__), "Wolf Sheep Predation.nlogo")
|
|
7
|
+
output_file_path = os.path.join(os.path.dirname(__file__), "output_wolf_sheep.txt")
|
|
8
|
+
|
|
9
|
+
# Initialize NetLogo
|
|
10
|
+
nl4py.initialize(netlogo_home)
|
|
11
|
+
netlogo_app = nl4py.netlogo_app()
|
|
12
|
+
netlogo_app.open_model(model_path)
|
|
13
|
+
netlogo_app.command("setup")
|
|
14
|
+
|
|
15
|
+
for i in range(10):
|
|
16
|
+
netlogo_app.command("go")
|
|
17
|
+
data_to_write = f"{model_path} iteration {i}\n"
|
|
18
|
+
with open(output_file_path, 'a') as f:
|
|
19
|
+
f.write(data_to_write)
|
|
20
|
+
print(f"{model_path} {i}")
|
|
21
|
+
time.sleep(1)
|
|
22
|
+
|
|
23
|
+
netlogo_app.command("quit")
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import time
|
|
2
|
+
from netlogopy.netlogopy import *
|
|
3
|
+
|
|
4
|
+
if __name__ == "__main__" :
|
|
5
|
+
# Change netlogo_home
|
|
6
|
+
netlogo_home="C:/Program Files/NetLogo 6.2.2"
|
|
7
|
+
n=run_netlogo(netlogo_home)
|
|
8
|
+
# Resize world
|
|
9
|
+
resize_world(n,0,60,0,60)
|
|
10
|
+
car01=pyturtle(n,x=20,y=20,shape="car",size_shape=9,color=15,name="car01",labelcolor=15)
|
|
11
|
+
|
|
12
|
+
for i in range(0,10):
|
|
13
|
+
time.sleep(1)
|
|
14
|
+
print("*********** ",i," ********")
|
|
15
|
+
n.close_model()
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import time
|
|
2
|
+
from netlogopy.netlogopy import *
|
|
3
|
+
|
|
4
|
+
if __name__ == "__main__" :
|
|
5
|
+
# Change netlogo_home
|
|
6
|
+
netlogo_home="C:/Program Files/NetLogo 6.2.2"
|
|
7
|
+
n=run_netlogo(netlogo_home)
|
|
8
|
+
# Resize world
|
|
9
|
+
resize_world(n,0,60,0,60)
|
|
10
|
+
# Change path fo image
|
|
11
|
+
car01=pyturtle(n,x=20,y=20,shape="car",size_shape=4,color=15,name="car01",labelcolor=15)
|
|
12
|
+
car02=pyturtle(n,x=5,y=5,shape="car",size_shape=4,color=55,name="car02",labelcolor=55)
|
|
13
|
+
street( n,fromm=car01,too=car02,label="street",labelcolor=35,color=35,shape="aa",thickness=0.5)
|
|
14
|
+
for i in range(0,100):
|
|
15
|
+
time.sleep(1)
|
|
16
|
+
print("*********** ",i," ********")
|
|
17
|
+
car01.fd(1)
|
|
18
|
+
if i==5:
|
|
19
|
+
car01.set_shape('default')
|
|
20
|
+
|
|
21
|
+
n.close_model()
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import time
|
|
2
|
+
from netlogopy.netlogopy import *
|
|
3
|
+
|
|
4
|
+
if __name__ == "__main__" :
|
|
5
|
+
# Change netlogo_home
|
|
6
|
+
netlogo_home="C:/Program Files/NetLogo 6.2.2"
|
|
7
|
+
n=run_netlogo(netlogo_home)
|
|
8
|
+
# Resize world
|
|
9
|
+
resize_world(n,0,60,0,60)
|
|
10
|
+
# Change path fo image
|
|
11
|
+
car01=pyturtle(n,x=20,y=20,shape="car",size_shape=4,color=15,name="car01",labelcolor=15)
|
|
12
|
+
car02=pyturtle(n,x=5,y=5,shape="car",size_shape=4,color=55,name="car02",labelcolor=55)
|
|
13
|
+
street( n,fromm=car01,too=car02,label="street",labelcolor=35,color=35,shape="aa",thickness=0.5)
|
|
14
|
+
for i in range(0,10):
|
|
15
|
+
time.sleep(1)
|
|
16
|
+
print("*********** ",i," ********")
|
|
17
|
+
car01.fd(1)
|
|
18
|
+
xy=getxyturtle(n,car01)
|
|
19
|
+
x=xy[0]
|
|
20
|
+
y=xy[1]
|
|
21
|
+
print("xy : ",xy)
|
|
22
|
+
print("x : ",x)
|
|
23
|
+
print("y : ",y)
|
|
24
|
+
n.close_model()
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import time
|
|
2
|
+
from netlogopy.netlogopy import *
|
|
3
|
+
|
|
4
|
+
if __name__ == "__main__" :
|
|
5
|
+
# Change netlogo_home
|
|
6
|
+
netlogo_home="C:/Program Files/NetLogo 6.2.2"
|
|
7
|
+
n=run_netlogo(netlogo_home)
|
|
8
|
+
# Resize world
|
|
9
|
+
resize_world(n,0,60,0,60)
|
|
10
|
+
# Change path fo image
|
|
11
|
+
car01=pyturtle(n,x=20,y=20,shape="car",size_shape=4,color=15,name="car01",labelcolor=15)
|
|
12
|
+
car02=pyturtle(n,x=5,y=5,shape="car",size_shape=4,color=55,name="car02",labelcolor=55)
|
|
13
|
+
street( n,fromm=car01,too=car02,label="street",labelcolor=35,color=35,shape="aa",thickness=0.5)
|
|
14
|
+
for i in range(0,100):
|
|
15
|
+
time.sleep(1)
|
|
16
|
+
print("*********** ",i," ********")
|
|
17
|
+
car01.fd(1)
|
|
18
|
+
if i==5:
|
|
19
|
+
car01.setxy(10,10)
|
|
20
|
+
|
|
21
|
+
n.close_model()
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import time
|
|
2
|
+
from netlogopy.netlogopy import *
|
|
3
|
+
|
|
4
|
+
if __name__ == "__main__" :
|
|
5
|
+
# Change netlogo_home
|
|
6
|
+
netlogo_home="C:/Program Files/NetLogo 6.2.2"
|
|
7
|
+
n=run_netlogo(netlogo_home)
|
|
8
|
+
# Resize world
|
|
9
|
+
resize_world(n,0,60,0,60)
|
|
10
|
+
# Change path fo image
|
|
11
|
+
car01=pyturtle(n,x=20,y=20,shape="car",size_shape=4,color=15,name="car01",labelcolor=15)
|
|
12
|
+
car02=pyturtle(n,x=5,y=5,shape="car",size_shape=4,color=55,name="car02",labelcolor=55)
|
|
13
|
+
street( n,fromm=car01,too=car02,label="street",labelcolor=35,color=35,shape="aa",thickness=0.5)
|
|
14
|
+
for i in range(0,10):
|
|
15
|
+
time.sleep(1)
|
|
16
|
+
print("*********** ",i," ********")
|
|
17
|
+
car01.fd(1)
|
|
18
|
+
word=distancebetween(n,car01,car02)
|
|
19
|
+
print(word)
|
|
20
|
+
n.close_model()
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Example 14 : rt / lt
|
|
3
|
+
Turn a turtle right and left.
|
|
4
|
+
"""
|
|
5
|
+
import time, os, sys
|
|
6
|
+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
|
|
7
|
+
from netlogopy.netlogopy import *
|
|
8
|
+
|
|
9
|
+
if __name__ == "__main__":
|
|
10
|
+
netlogo_home = "C:/Program Files/NetLogo 6.2.2"
|
|
11
|
+
n = run_netlogo(netlogo_home)
|
|
12
|
+
resize_world(n, 0, 60, 0, 60)
|
|
13
|
+
|
|
14
|
+
car01 = pyturtle(n, x=30, y=30, shape="car", size_shape=4, color=15, name="car01", labelcolor=15)
|
|
15
|
+
|
|
16
|
+
for i in range(36):
|
|
17
|
+
time.sleep(0.2)
|
|
18
|
+
car01.rt(10) # turn 10 degrees right → full circle in 36 steps
|
|
19
|
+
car01.fd(1)
|
|
20
|
+
|
|
21
|
+
# Now go left
|
|
22
|
+
for i in range(36):
|
|
23
|
+
time.sleep(0.2)
|
|
24
|
+
car01.lt(10) # turn 10 degrees left
|
|
25
|
+
car01.fd(1)
|
|
26
|
+
|
|
27
|
+
n.close_model()
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Example 15 : bk (backward)
|
|
3
|
+
Move a turtle backward.
|
|
4
|
+
"""
|
|
5
|
+
import time, os, sys
|
|
6
|
+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
|
|
7
|
+
from netlogopy.netlogopy import *
|
|
8
|
+
|
|
9
|
+
if __name__ == "__main__":
|
|
10
|
+
netlogo_home = "C:/Program Files/NetLogo 6.2.2"
|
|
11
|
+
n = run_netlogo(netlogo_home)
|
|
12
|
+
resize_world(n, 0, 60, 0, 60)
|
|
13
|
+
|
|
14
|
+
car01 = pyturtle(n, x=30, y=30, shape="car", size_shape=4, color=55, name="car01", labelcolor=55)
|
|
15
|
+
|
|
16
|
+
for i in range(10):
|
|
17
|
+
time.sleep(0.5)
|
|
18
|
+
print(f"Step {i}: moving forward")
|
|
19
|
+
car01.fd(2)
|
|
20
|
+
|
|
21
|
+
for i in range(10):
|
|
22
|
+
time.sleep(0.5)
|
|
23
|
+
print(f"Step {i}: moving backward")
|
|
24
|
+
car01.bk(2)
|
|
25
|
+
|
|
26
|
+
n.close_model()
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Example 16 : set_heading / get_heading
|
|
3
|
+
Set and read the heading (direction) of a turtle.
|
|
4
|
+
NetLogo heading: 0=North, 90=East, 180=South, 270=West.
|
|
5
|
+
"""
|
|
6
|
+
import time, os, sys
|
|
7
|
+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
|
|
8
|
+
from netlogopy.netlogopy import *
|
|
9
|
+
|
|
10
|
+
if __name__ == "__main__":
|
|
11
|
+
netlogo_home = "C:/Program Files/NetLogo 6.2.2"
|
|
12
|
+
n = run_netlogo(netlogo_home)
|
|
13
|
+
resize_world(n, 0, 60, 0, 60)
|
|
14
|
+
|
|
15
|
+
car01 = pyturtle(n, x=30, y=30, shape="car", size_shape=4, color=15, name="car01", labelcolor=15)
|
|
16
|
+
|
|
17
|
+
headings = [0, 90, 180, 270] # North, East, South, West
|
|
18
|
+
for h in headings:
|
|
19
|
+
time.sleep(1)
|
|
20
|
+
car01.set_heading(h)
|
|
21
|
+
current = car01.get_heading()
|
|
22
|
+
print(f"Heading set to {h} → NetLogo reports: {current}")
|
|
23
|
+
car01.fd(5)
|
|
24
|
+
|
|
25
|
+
n.close_model()
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Example 17 : set_color / get_color
|
|
3
|
+
Change and read the color of a turtle.
|
|
4
|
+
See NetLogo color chart: https://ccl.northwestern.edu/netlogo/docs/images/colors.jpg
|
|
5
|
+
"""
|
|
6
|
+
import time, os, sys
|
|
7
|
+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
|
|
8
|
+
from netlogopy.netlogopy import *
|
|
9
|
+
|
|
10
|
+
if __name__ == "__main__":
|
|
11
|
+
netlogo_home = "C:/Program Files/NetLogo 6.2.2"
|
|
12
|
+
n = run_netlogo(netlogo_home)
|
|
13
|
+
resize_world(n, 0, 60, 0, 60)
|
|
14
|
+
|
|
15
|
+
car01 = pyturtle(n, x=20, y=20, shape="car", size_shape=4, color=15, name="car01", labelcolor=15)
|
|
16
|
+
|
|
17
|
+
# NetLogo color codes: 15=red, 55=green, 95=blue, 35=orange, 125=violet
|
|
18
|
+
colors = [15, 55, 95, 35, 125]
|
|
19
|
+
for c in colors:
|
|
20
|
+
time.sleep(1)
|
|
21
|
+
car01.set_color(c)
|
|
22
|
+
current = car01.get_color()
|
|
23
|
+
print(f"Color set to {c} → NetLogo reports: {current}")
|
|
24
|
+
car01.fd(3)
|
|
25
|
+
|
|
26
|
+
n.close_model()
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Example 18 : set_size / get_size
|
|
3
|
+
Change and read the size of a turtle.
|
|
4
|
+
"""
|
|
5
|
+
import time, os, sys
|
|
6
|
+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
|
|
7
|
+
from netlogopy.netlogopy import *
|
|
8
|
+
|
|
9
|
+
if __name__ == "__main__":
|
|
10
|
+
netlogo_home = "C:/Program Files/NetLogo 6.2.2"
|
|
11
|
+
n = run_netlogo(netlogo_home)
|
|
12
|
+
resize_world(n, 0, 60, 0, 60)
|
|
13
|
+
|
|
14
|
+
car01 = pyturtle(n, x=30, y=10, shape="car", size_shape=2, color=15, name="car01", labelcolor=15)
|
|
15
|
+
|
|
16
|
+
for i in range(8):
|
|
17
|
+
time.sleep(0.8)
|
|
18
|
+
new_size = 2 + i # grows from 2 to 9
|
|
19
|
+
car01.set_size(new_size)
|
|
20
|
+
current = car01.get_size()
|
|
21
|
+
print(f"Size set to {new_size} → NetLogo reports: {current}")
|
|
22
|
+
car01.fd(2)
|
|
23
|
+
|
|
24
|
+
n.close_model()
|