Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge develop updates to stop_order #20

Merged
merged 5 commits into from
Mar 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fasttrips/Assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def read_configuration(input_network_dir, input_demand_dir):
'stochastic_pathset_size' :1000,
'capacity_constraint' :False,
'trace_person_ids' :'None',
'prepend_route_id_to_trip_id' :False,
'prepend_route_id_to_trip_id' :'False',
'number_of_processes' :0,
'bump_buffer' :5,
'bump_one_at_a_time' :True,
Expand Down
59 changes: 58 additions & 1 deletion fasttrips/FastTrips.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"""
import os
from operator import attrgetter
import pandas
import transitfeed

from .Assignment import Assignment
Expand All @@ -36,6 +37,9 @@ class FastTrips:
#: Debug log filename. Detailed output goes here, including trace information.
DEBUG_LOG = "ft_debug%s.log"

#: Pathset debug filename. Writes pathset here.
PATHSET_LOG = "ft_pathset%s.txt"

def __init__(self, input_network_dir, input_demand_dir, output_dir,
is_child_process=False, logname_append="", appendLog=False):
"""
Expand Down Expand Up @@ -93,6 +97,15 @@ def __init__(self, input_network_dir, input_demand_dir, output_dir,
os.path.join(self.output_dir, FastTrips.DEBUG_LOG % logname_append),
logToConsole=False if is_child_process else True, append=appendLog)

# clear pathset files if we're starting out -- reset them to just a header
# there will be one for the parent process, and one each for workers
pathset_filename = os.path.join(self.output_dir, FastTrips.PATHSET_LOG % logname_append)
if not appendLog:
FastTripsLogger.info("Writing %s" % pathset_filename)
pathset_file = open(pathset_filename, 'w')
pathset_file.write("iteration passenger_id_num trip_list_id_num path_cost path_probability path_board_stops path_trips path_alight_stops\n")
pathset_file.close()

# Read the configuration
Assignment.read_configuration(self.input_network_dir, self.input_demand_dir)

Expand Down Expand Up @@ -143,6 +156,50 @@ def read_input_files(self):
else:
self.passengers = None

def combine_pathset_files(self):
"""
Since the pathset files are output by worker, let's combine them into a single file.
"""
procnum = 1
pathset_init = False
pathsets_df = None
# if we don't have one, no worries

while True:
logname_append = "_worker%02d" % procnum
pathset_filename = os.path.join(self.output_dir, FastTrips.PATHSET_LOG % logname_append)

if not os.path.exists(pathset_filename):
break

# read the pathset
pathset_df = pandas.read_table(pathset_filename, sep="[ ]+")
FastTripsLogger.info("Read %d lines from %s" % (len(pathset_df), pathset_filename))
# remove it
os.remove(pathset_filename)

# append to ours
if not pathset_init:
pathsets_df = pathset_df
pathset_init = True
else:
pathsets_df = pandas.concat([pathsets_df, pathset_df], axis=0)

# see if we have more
procnum += 1

if pathset_init:
# sort it by iteration, trip_id_num
pathsets_df.sort(columns=['iteration','trip_list_id_num'], inplace=True)
# write it
pathset_filename = os.path.join(self.output_dir, FastTrips.PATHSET_LOG % "")
pathsets_df.to_csv(pathset_filename, sep=" ", index=False)
FastTripsLogger.info("Wrote %d lines to %s" % (len(pathsets_df), pathset_filename))


def run_assignment(self, output_dir):
# Do it!
Assignment.assign_paths(output_dir, self);
Assignment.assign_paths(output_dir, self)

self.combine_pathset_files()

29 changes: 14 additions & 15 deletions src/fasttrips.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,29 +120,28 @@ _fasttrips_find_path(PyObject *self, PyObject *args)

// package for returning. We'll separate ints and doubles.
npy_intp dims_int[2];
dims_int[0] = path.stops_.size();
dims_int[0] = path.size();
dims_int[1] = 6; // stop_id, deparr_mode_, trip_id_, stop_succpred_, seq_, seq_succpred_
PyArrayObject *ret_int = (PyArrayObject *)PyArray_SimpleNew(2, dims_int, NPY_INT32);

npy_intp dims_double[2];
dims_double[0] = path.stops_.size();
dims_double[0] = path.size();
dims_double[1] = 5; // label_, deparr_time_, link_time_, cost_, arrdep_time_
PyArrayObject *ret_double = (PyArrayObject *)PyArray_SimpleNew(2, dims_double, NPY_DOUBLE);

for (int ind = 0; ind < dims_int[0]; ++ind) {
int stop_id = path.stops_[ind];
*(npy_int32*)PyArray_GETPTR2(ret_int, ind, 0) = stop_id;
*(npy_int32*)PyArray_GETPTR2(ret_int, ind, 1) = path.states_[stop_id].deparr_mode_;
*(npy_int32*)PyArray_GETPTR2(ret_int, ind, 2) = path.states_[stop_id].trip_id_;
*(npy_int32*)PyArray_GETPTR2(ret_int, ind, 3) = path.states_[stop_id].stop_succpred_;
*(npy_int32*)PyArray_GETPTR2(ret_int, ind, 4) = path.states_[stop_id].seq_;
*(npy_int32*)PyArray_GETPTR2(ret_int, ind, 5) = path.states_[stop_id].seq_succpred_;

*(npy_double*)PyArray_GETPTR2(ret_double, ind, 0) = path.states_[stop_id].label_;
*(npy_double*)PyArray_GETPTR2(ret_double, ind, 1) = path.states_[stop_id].deparr_time_;
*(npy_double*)PyArray_GETPTR2(ret_double, ind, 2) = path.states_[stop_id].link_time_;
*(npy_double*)PyArray_GETPTR2(ret_double, ind, 3) = path.states_[stop_id].cost_;
*(npy_double*)PyArray_GETPTR2(ret_double, ind, 4) = path.states_[stop_id].arrdep_time_;
*(npy_int32*)PyArray_GETPTR2(ret_int, ind, 0) = path[ind].first;
*(npy_int32*)PyArray_GETPTR2(ret_int, ind, 1) = path[ind].second.deparr_mode_;
*(npy_int32*)PyArray_GETPTR2(ret_int, ind, 2) = path[ind].second.trip_id_;
*(npy_int32*)PyArray_GETPTR2(ret_int, ind, 3) = path[ind].second.stop_succpred_;
*(npy_int32*)PyArray_GETPTR2(ret_int, ind, 4) = path[ind].second.seq_;
*(npy_int32*)PyArray_GETPTR2(ret_int, ind, 5) = path[ind].second.seq_succpred_;

*(npy_double*)PyArray_GETPTR2(ret_double, ind, 0) = path[ind].second.label_;
*(npy_double*)PyArray_GETPTR2(ret_double, ind, 1) = path[ind].second.deparr_time_;
*(npy_double*)PyArray_GETPTR2(ret_double, ind, 2) = path[ind].second.link_time_;
*(npy_double*)PyArray_GETPTR2(ret_double, ind, 3) = path[ind].second.cost_;
*(npy_double*)PyArray_GETPTR2(ret_double, ind, 4) = path[ind].second.arrdep_time_;
}

PyObject *returnobj = Py_BuildValue("(OOd)",ret_int,ret_double,path_info.cost_);
Expand Down
Loading