netlogopy : Usage netlogo by python
Project description
Project: Netlogopy
Netlogopy is a Python library designed to interface with NetLogo, allowing users to control NetLogo models through Python scripts. This integration enables users to leverage Python's capabilities while utilizing the powerful modeling features of NetLogo.
Developed by
- Nourddine Bouaziz: GitHub Profile
Installation Instructions
Step 1: Install NetLogo
Download and install NetLogo from the official website:
Step 2: Install Netlogopy
To install Netlogopy, run the following commands in your terminal:
conda install openjdk -y
pip install netlogopy
pip install netlogopy --upgrade
Step 3: Set Up Visual Studio Code (VS Code)
To ensure smooth operation within VS Code, set the default terminal to Command Line Interface (CLI). Refer to these links for guidance:
Example Test Script
Here is a basic script to test Netlogopy functionality. This example demonstrates how to set up a NetLogo environment, create a turtle, and animate it.
import time
from netlogopy.netlogopy import *
if __name__ == "__main__" :
# Specify the NetLogo installation path
netlogo_home = "C:/Program Files/NetLogo 6.2.2"
# Run the NetLogo instance
n = run_netlogo(netlogo_home)
# Resize the NetLogo world
resize_world(n, 0, 60, 0, 60)
# Create a turtle (car) at specified coordinates
car01 = pyturtle(n, x=20, y=20, shape="car", size_shape=4, color=15, name="car01", labelcolor=15)
# Animate the turtle for 10 seconds
for i in range(0, 10):
time.sleep(1)
print("*********** ", i, " ********")
# Close the NetLogo model
n.close_model()
Color Palette
The available colors for turtles in NetLogo can be found in the following image:
Usage Examples
1. Creating a Turtle (pyturtle)
import time
from netlogopy.netlogopy import *
if __name__ == "__main__" :
netlogo_home = "C:/Program Files/NetLogo 6.2.2"
n = run_netlogo(netlogo_home)
resize_world(n, 0, 60, 0, 60)
car01 = pyturtle(n, x=20, y=20, shape="car", size_shape=4, color=15, name="car01", labelcolor=15)
for i in range(0, 10):
time.sleep(1)
print("*********** ", i, " ********")
n.close_model()
2. Setting a Background Image
import time
from netlogopy.netlogopy import *
if __name__ == "__main__" :
netlogo_home = "C:/Program Files/NetLogo 6.2.2"
n = run_netlogo(netlogo_home)
resize_world(n, 0, 60, 0, 60)
path_image = "path/to/image/netlogopy.png" # Change to your image path
set_background(n, path_image)
car01 = pyturtle(n, x=20, y=20, shape="car", size_shape=4, color=15, name="car01", labelcolor=15)
for i in range(0, 10):
time.sleep(1)
print("*********** ", i, " ********")
n.close_model()
3. Drawing a Street Between Two Turtles
import time
from netlogopy.netlogopy import *
if __name__ == "__main__" :
netlogo_home = "C:/Program Files/NetLogo 6.2.2"
n = run_netlogo(netlogo_home)
resize_world(n, 0, 60, 0, 60)
car01 = pyturtle(n, x=20, y=20, shape="car", size_shape=4, color=15, name="car01", labelcolor=15)
car02 = pyturtle(n, x=5, y=5, shape="car", size_shape=4, color=55, name="car02", labelcolor=55)
street(n, fromm=car01, too=car02, label="street", labelcolor=35, color=35, shape="aa", thickness=0.5)
for i in range(0, 10):
time.sleep(1)
print("*********** ", i, " ********")
n.close_model()
4. Moving a Turtle Forward
import time
from netlogopy.netlogopy import *
if __name__ == "__main__" :
netlogo_home = "C:/Program Files/NetLogo 6.2.2"
n = run_netlogo(netlogo_home)
resize_world(n, 0, 60, 0, 60)
car01 = pyturtle(n, x=20, y=20, shape="car", size_shape=4, color=15, name="car01", labelcolor=15)
for i in range(0, 10):
time.sleep(1)
print("*********** ", i, " ********")
car01.fd(1) # Move the turtle forward by 1 unit
n.close_model()
5. Displaying Distance to Another Turtle
import time
from netlogopy.netlogopy import *
if __name__ == "__main__" :
netlogo_home = "C:/Program Files/NetLogo 6.2.2"
n = run_netlogo(netlogo_home)
resize_world(n, 0, 60, 0, 60)
car01 = pyturtle(n, x=20, y=20, shape="car", size_shape=4, color=15, name="car01", labelcolor=15)
car02 = pyturtle(n, x=5, y=5, shape="car", size_shape=4, color=55, name="car02", labelcolor=55)
for i in range(0, 10):
time.sleep(1)
print("*********** ", i, " ********")
distance = car01.distanceto(car02)
print(f"Distance to car02: {distance}")
n.close_model()
Additional Functions
The library provides a variety of functions to manipulate turtle behavior and interactions, including:
- fd(distance): Move the turtle forward by the specified distance.
- face_to(turtle): Make the turtle face another turtle.
- move_to(turtle): Move the turtle to the location of another turtle.
- hideturtle(): Hide the turtle from view.
- set_shape(shape_name): Change the shape of the turtle.
- setxy(x, y): Set the turtle's position to the specified coordinates.
- distancebetween(turtle1, turtle2): Calculate the distance between two turtles.
Conclusion
This documentation provides a comprehensive overview of the Netlogopy library, including installation steps, usage examples, and descriptions of key functions. By utilizing this library, you can seamlessly integrate Python scripting with NetLogo's modeling capabilities, enabling advanced simulations and analyses.
For further inquiries or contributions, feel free to contact the developer.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file netlogopy-0.0.16-py3-none-any.whl
.
File metadata
- Download URL: netlogopy-0.0.16-py3-none-any.whl
- Upload date:
- Size: 62.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a6f5066a21ac475fc943d297f75c6b89fc077b7731e118ab2a4b17f00df35d99 |
|
MD5 | cf2f22fc64239baa0220a351d2bae954 |
|
BLAKE2b-256 | 5b4b5227833d207a391c1f58a7b4f78d6c510a99fa6deea6a206b3a676293807 |