Coder Social home page Coder Social logo

adamamer20 / streetcrime Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 640.35 MB

streetcrime: a python ABM package for urban crime

Home Page: https://adamamer20.github.io/streetcrime/api

License: GNU Lesser General Public License v2.1

Python 100.00%
agent-based-modeling crime-prediction social-sciences crime-pattern-theory milan-crime urban-crime

streetcrime's Introduction

streetcrime: a python ABM package for urban crime

Urban crime, with its complex local interactions and spatial dynamics, is an excellent candidate for Agent-Based Modeling (ABM). The streetcrime package is designed as a robust, user-friendly ABM tool for urban crime analysis. It facilitates the testing of various crime theories, compares them with real crime data, and evaluates crime reduction strategies. Future versions will include Machine Learning (ML) features for parameter tuning. The package integrates osmnx for urban data and mesa (along with mesa-frames) for the modeling framework.

streetcrime was developed for my BSc thesis. You can find the document here.

Installation

Prerequisites

Before installing streetcrime, ensure that mesa-frames is installed. As it's not yet available on PyPi or conda-forge, follow the installation instructions here.

Installation Steps

  1. Clone the GitHub Repository

    git clone https://github.com/adamamer20/streetcrime.git
    cd streetcrime
  2. Installation

    a. Installing in a Conda Environment

    conda activate myenv
    pip install -e .

    b. Installing in a Python Virtual Environment

    source myenv/bin/activate  # On Windows, use `myenv\Scripts\activate`
    pip install -e .

Usage

Note: streetcrime is in early development stages; expect changes and potential breaking updates. Feedback and issue reports are welcomed.

  • You can find the API Documentation here

  • You can find a simple usage script here

Key Components

City

The City class represents the geographical space for agent interactions, created using a CRS and an OpenStreetMap query. It features an advanced road network and pre-calculated shortest paths for efficiency. Buildings are categorized into homes, workplaces, and potential activities. They are also categorized as being open or closed during night-time. You can find the categorization here.

*Note: Data for large cities may take longer to download; it's recommended to start with smaller areas for initial tests.

from streetcrime.space.city import City
from streetcrime.model import StreetCrime
from streetcrime.agents.criminal import Pickpocket, Robber
from streetcrime.agents.police_agent import PoliceAgent
from streetcrime.agents.worker import Worker

milan = City(crs="EPSG:32632", city_name="Milan, Italy")
milan.load_data()

Agents

Here is a diagram which shows the currently supported agents:

New agents should be created from existing classes. For example, a House Thief could inherit from the Criminal class.

Base Classes
  • Mover: This is the foundational class for all moving agents within the model. The Mover class is equipped with the ability to navigate the city using its road network. Key attributes include a unique identifier, a geometric point representing the agent's location, and a status indicating their current activity. The class also handles the selection of random activities for agents and manages their movement to these locations. Upon reaching a destination, a countdown begins, determining when the agent can move again. Future enhancements will include diverse rules for activity selection.

  • InformedMover: This class represents an advanced version of Mover, planned for future implementation. InformedMover agents will have access to a subset of global model information, such as recent crimes or heavily trafficked areas, based on a defined threshold. This feature allows for more sophisticated decision-making and movement patterns.

  • Resident: Building on the InformedMover, the Resident class has a designated home location and a specified rest period, during which the agent must stay at home. This class simulates the daily life patterns of city inhabitants.

Deployable Classes
  • PoliceAgent: A subclass of InformedMover, the PoliceAgent functions as a guardian within the city. Their primary role is crime prevention; when in close proximity to a Criminal agent, they can inhibit criminal activities, thus playing a crucial role in the model's representation of law enforcement and public safety dynamics.

  • Worker: Deriving from the Resident class, Workers are typical inhabitants of the city with defined routines, including work locations and schedules. They represent potential targets for criminal agents, possessing a variable attribute of 'crime_attractiveness' which influences their likelihood of being victimized. This class illustrates the daily patterns of city residents and their interactions with other agent types.

  • Criminal (Pickpocket and Robber): These agents, branching from the Resident class, are central to the simulation of criminal activities within the city. Criminals actively seek opportunities to commit crimes, selecting their targets based on perceived opportunities and victim attributes. The Pickpocket and Robber subclasses differ in their operational tactics and the criteria for successful criminal acts. For instance, Pickpockets thrive in crowded settings, while Robbers may face additional challenges, such as potential resistance from Workers.

Model

After initliazing a city of choice and having loaded data, running involves initializing the agents and executing the simulation.

model = StreetCrime(milan)
model.create_agents(
    p_agents={Worker: 0.85, PoliceAgent: 0.05, Pickpocket: 0.05, Robber: 0.05}, 
    n_agents=1000
)

#Run the model for 3 days considering time steps of 10 minutes
model.run_model(days=3, len_step=10)

Users can also save the simulation state for analysis and resume from saved states. Graphs of crimes and agents can be generated.

#Save the state of the simulation
model.save_state()

#Plot the model at current state
model.plot()

What's Next?

If you encounter any challanges in using the package, or if you have suggestions for feature enhancements, please feel free to open an issue on this GitHub repository.

  • Enabling information access for InformedMover.
  • Incorporating public transport routes data.
  • Integrating diverse criminological theories (eg. ).
  • Enhancing visualization capabilities, including live or post-simulation video generation.
  • Strengthening integration with mesa and mesa-frames.
  • Introducing various criminal types (eg. House Thiefs).
  • Implementing ML for parameter optimization.

streetcrime's People

Contributors

adamamer20 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

streetcrime's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.