Giter Site home page Giter Site logo

jonpappalord / ditras Goto Github PK

View Code? Open in Web Editor NEW
33.0 3.0 11.0 1.81 MB

DITRAS (DIary-based TRAjectory Simulator), a mathematical model to simulate human mobility

License: Apache License 2.0

Python 69.98% Jupyter Notebook 30.02%
humanmobility humanmobilitymodeling python mobility-diary mobility-trajectory mobility mobilityanalysis mobilitydata simulation trajectory-simulator

ditras's Introduction

This version is no longer being maintained. For an updated version of DITRAS, please visit https://github.com/scikit-mobility/scikit-mobility

DITRAS

DITRAS (DIary-based TRAjectory Simulator) is a framework to simulate the spatio-temporal patterns of human mobility in a realistic way. DITRAS operates in two steps: the generation of a mobility diary and the translation of the mobility diary into a mobility trajectory. The mobility diary is constructed by a Markov model which captures the tendency of individuals to follow or break their routine. The mobility trajectory is produced by a model based on the concept of preferential exploration and preferential return. DITRAS can reproduce the statistical properties of real human mobility trajectories in an accurate way. For technical details on DITRAS please read the scientific article at this link: http://arxiv.org/abs/1607.05952

Citation

If you use this code, please cite the following scientific article:

Required Python packages

  • random
  • cPickle
  • math
  • scipy
  • collections
  • numpy
  • sys
  • os
  • time
  • csv

How to run DITRAS as standalone

To run the code of DITRAS, download all the folder in this repository and write on the terminal a command in the following format:
python DITRAS.py n_agents time_slots spatial_tessellation od_matrix diary_generator_1hour.pkl filename, where:

  • n_agents is an integer indicating the number of agents to simulate (e.g., 10000)
  • time_slots is the length of the time period in hours (e.g., 720, that is 1 month)
  • spatial_tessellation is the name of a file where the weighted spatial tessellation is stored. The format of the spatial_tessellation file must be the following: latitude,longitude,relevance. Latitude and longitude are float numbers indicating the geographic position of a location and relevance is an integer indicating the importance of the location (e.g., the number of residents in the locations or the number of calls made in that location during a given period). We provide you an example of spatial tessellation in the Italian regione of Trentino, stored in the file location2info_trentino. This spatial tessellation is released under a Open Data Commons Open Database License (ODbL) and it is obtained from the publicly available dataset described in this paper http://www.nature.com/articles/sdata201555#data-records.
  • od_matrix is the name of a file where the origin-destination matrix based on the spatial_tessellation is stored. The origin-destination matrix is used by DITRAS to decide the movements of the synthetic agents. If the file specified does not exist in the current folder, DITRAS will automatically compute the origin-destination matrix and store it in a file named od_matrix.
  • diary_generator_1hour.pkl is the diary generator we computed for you, stored as a pickle file. If you have real trajectory data, you can compute your own diary generator using the Python script diary_generator_builder.py.

For example type the following command on the terminal to start your DITRAS simulation:

python DITRAS.py 10000 720 location2info_trentino od_matrix.pkl diary_generator_1hour.pkl trajs_10000_720.csv 

DITRAS will simulate the mobility of 10,000 agents for 720 hours (1 months), using the location2info_trentino weighted spatial tessellation and the diary generator diary_generator_1hour.pkl. The produced synthetic trajectories will be stored in a file named trajs_10000_720.csv. DITRAS will also compute the origin destination matrix od_matrix.pkl the first time you run the command.

How to run DITRAS as library

First, read the spatial tessellation from the corresponding file and load it in a dictionary, using the function load_spatial_tessellation. In this repository we provide an example of spatial tessellation regarding the Italian region of Trentino, it is stored in the file location2info_trentino. Second, initialize a DITRAS object with the parameters you prefer. Finally, start the execution of DITRAS by using the method start_simulation.

spatial_tessellation = load_spatial_tessellation('location2info_trentino')

ditras = DITRAS(n_agents=10000, length=720, diary_generator=MD(filename='diary_generator_1hour.pkl'),
                 trajectory_generator=dEPR(rho=0.6, gamma=0.21), filename='trajs_10000_720.csv')

ditras.start_simulation(spatial_tessellation, od_matrix_file='od_matrix.pkl')

Run the ditras_example.ipynb notebook in this repository to try different executions of DITRAS.

Output of DITRAS

DITRAS produces a file with the following format:

agent_id,location_id,time_slot

where:

  • agent_id is an identifier of the agent
  • location_id is the identifier of the location visited by the agent, as specified in the spatial tessellation file
  • time_slot is the time slot of the visit, i.e., the hour when the agent visited the location

ditras's People

Contributors

jonpappalord avatar josemarfdc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ditras's Issues

missing diary_generator_builder.py

The documentation mentions we can use a diary_generator_builder.py to create our own diary generator, however such a file is not included.

Would it be possible for you to git push that file to the repo, please ๐Ÿ™?
Thank you so much

ValueError: could not convert string to float

I did clone of the project. When I execute the "python DITRAS.py 10000 720 location2info_trentino od_matrix.pkl diary_generator_1hour.pkl trajs_10000_720.csv " command, I receive the error message:

ValueError: could not convert string to float

Traceback Full:

Traceback (most recent call last):
  File "DITRAS.py", line 467, in <module>
    class DITRAS(object):
  File "DITRAS.py", line 474, in DITRAS
    def __init__(self, n_agents=10000, length=168, diary_generator=MD(),
  File "DITRAS.py", line 396, in __init__
    self.markov_model = pickle.load(open(filename, 'rb'))
ValueError: could not convert string to float

OS: Windows 10
Python 3.7.5

ValueError: could not convert string to float: 'MONOTXT'

Training.py

import numpy
import tensorflow as tf
from tqdm import tqdm
import os
from pandas import np

from Data import trainDataSet, testDataSet
from MultiLayerModel import y, y_, x

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=y + 1e-50, labels=y_))

-GradientDescentOptimizer is used to minimize loss
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

-Define accuracy
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

-Train
epochs = 50000
train_acc = numpy.zeros(epochs // 10)
test_acc = numpy.zeros(epochs // 10)

for i in tqdm(range(epochs), ascii=True):
if i % 10 == 0: # Record summary data, and the accuracy
- Check accuracy on train set
A = accuracy.eval(feed_dict={x: trainDataSet.drop(['font', 'fontVariant'], axis=1), y_: trainDataSet['font']})
train_acc[i // 10] = A
- And now the validation set
A = accuracy.eval(feed_dict={x: testDataSet.drop(['font', 'fontVariant'], axis=1), y_: testDataSet['font']})

    test_acc[i // 10] = A

train_step.run(feed_dict={x: trainDataSet.drop(['font', 'fontVariant'], axis=1), y_: trainDataSet[['font']]})

Multilayermodel.py
import math

import tensorflow as tf

  • Create a Tensorflow session
    session = tf.InteractiveSession()

  • These will be inputs for the model

  • Input pixels of images, flattened

  • 400 = 20*20 which is the size
    x = tf.placeholder("float", [None, 410])

  • Known labels
    y_ = tf.placeholder("float", [None, 2])

  • Hidden layer 1 with 256 neurons
    num_hidden1 = 256

  • Variables

  • W1 is for weights

  • b1 is for bias
    W1 = tf.Variable(tf.truncated_normal([410, num_hidden1], stddev=1. / math.sqrt(410)))
    b1 = tf.Variable(tf.constant(0.1, shape=[num_hidden1]))

  • Compute the activation function of the weighted sum -> produce 128 intermediate value

  • Nonlinear transform functions - activation function: sigmoid
    h1 = tf.sigmoid(tf.matmul(x, W1) + b1)

  • Hidden Layer 2 with 64 neurons
    num_hidden2 = 64
    W2 = tf.Variable(tf.truncated_normal([num_hidden1, num_hidden2], stddev=2. / math.sqrt(num_hidden1)))
    b2 = tf.Variable(tf.constant(0.2, shape=[num_hidden2]))
    h2 = tf.sigmoid(tf.matmul(h1, W2) + b2)

  • Output Layer

  • Logistic regression again
    W3 = tf.Variable(tf.truncated_normal([num_hidden2, 2], stddev=1. / math.sqrt(2)))
    b3 = tf.Variable(tf.constant(0.1, shape=[2]))

  • Just initialize
    session.run(tf.global_variables_initializer())

  • Define model
    y = tf.nn.softmax(tf.matmul(h2, W3) + b3)

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.