Commit db723898 authored by dagal's avatar dagal

better trip generation

parent 626551d4
......@@ -354,12 +354,22 @@ def load_stops(path, nStops=20000):
return stops
def is_better_stop(candidate_stop, next_stops):
same_stops = [s for s in next_stops if s[2] == candidate_stop[2]]
if len(same_stops) == 0:
# stop not in the next ones, should keep
return True
else:
# stop will arrive sooner this way, should keep
return candidate_stop[0] < same_stops[0][0]
def main(argv):
#n_traj = 0
#n_traj = 1
n_traj = 1000
#n_traj = 10000000
n_traj = 10000
#n_traj = 1000000
#change_probs = [0.50, 0.90, 0.95, 0.98, 1.0]
#change_probs = [0.98, 0.98, 0.99, 1.0]
switch_weights = [10, 10, 5, 1]
......@@ -401,163 +411,99 @@ def main(argv):
max_waiting_time = 30*60
err = 0
skip_bad = 0
n_switch = [n_traj/x for x in switch_weights]
for switch,n in enumerate(n_switch):
i = 0
while i < n:
current_day = getRandomDay()
origin = network.stops[random.choice(tripsByStop[current_day.val() % 7].keys())]
(t, trip_id) = random.choice(tripsByStop[current_day.val() % 7][origin.id])
current_trip = network.trips[trip_id]
current_line = current_trip.get_line()
next_stops = [s for s in current_trip.stops if s[1] > t and s[2] != origin]
if len(next_stops) == 0:
skip_bad += 1
continue
(t,_,next) = random.choice(next_stops)
trajectory = [origin.id, next]
lines = [current_line, current_line]
times = 2*[tripsByDay[current_day.val()][(current_line, current_trip.start_time)]]
changes.update([switch])
lengths.update([len(trajectory)])
unused_stops.difference_update(trajectory)
sys.stdout.write(",".join(map(lambda (l,s,t): "%s:%s:%s" % (l,str(stops_dict[s]),str(t)), zip(lines, trajectory, times))))
sys.stdout.write("\n")
i += 1
break
while i < 0:
n_switch = [x*n_traj/sum(switch_weights) for x in switch_weights]
n_switch[0] += n_traj - sum(n_switch)
while i < n_traj:
current_day = getRandomDay()
origin = network.stops[random.choice(tripsByStop[current_day.val() % 7].keys())]
(t, trip_id) = random.choice(tripsByStop[current_day.val() % 7][origin.id])
current_trip = network.trips[trip_id]
print "Current trip: " + str(current_trip.id)
next_stops = [s for s in reversed(current_trip.stops) if s[1] > t and s[2] != origin]
print next_stops
current_line = current_trip.get_line()
current_time = TTime(current_day, 0) + t
next_stops = [s for s in current_trip.stops if s[1] > t and s[2] != origin.id]
if len(next_stops) == 0:
skip_bad += 1
continue
complete_trajectory = [origin.id]
trajectory = [origin.id]
current_line = current_trip.get_line()
cur_changes = 0
current_time = TTime(current_day, 0) + t
times = [tripsByDay[current_day.val()][(current_line, current_trip.start_time)]]
prob_next = 0.0
lines = [current_line]
(t,_,next) = next_stops.pop()
complete_trajectory = [origin.id, next]
print complete_trajectory
print trajectory
print lines
print times
try:
while random.random() > prob_next:
prob_next += 0.01
should_change = len(next_stops) == 0 or random.random() > change_probs[cur_changes]
if should_change:
if cur_changes+1 == len(change_probs):
break
prev = next
prev_t = t
connections = [s for s in network.stops[next].connections if s not in complete_trajectory]
current_time = TTime(current_day, 0) + t
trips = [current_trip]
times = [tripsByDay[current_day.val()][(current_line, current_trip.start_time)]]
switch_stops = [s for s in next_stops if len(network.stops[s[2]].connections) > 0 and s[2] not in complete_trajectory]
switch_goal = 0
cur_switch = 0
for switch,n in reversed([x for x in enumerate(n_switch)]):
if n > 0:
switch_goal = switch
break
while cur_switch < switch_goal and len(switch_stops) > 0:
ta,tz,next = random.choice(switch_stops)
try:
next_time = TTime(current_time.day,0) + ta
next_day = next_time.day if next in tripsByStop[next_time.day.val() % 7] else current_time.day
while len(connections) > 0:
try:
next = random.choice(connections)
next_day = current_day if next in tripsByStop[current_day.val() % 7] else current_time.day
possible_trips = [(ttrip[0], network.trips[ttrip[1]]) for ttrip in \
tripsByStop[next_day.val() % 7][next] \
if t <= ttrip[0] <= t + max_waiting_time \
and network.trips[ttrip[1]].route != current_trip.route\
and len([s for s in network.trips[ttrip[1]].stops if s[1] > ttrip[0]]) > 1]
if len(possible_trips) > 0:
current_day = next_day
break
else:
connections.remove(next)
except (IndexError,KeyError):
connections.remove(next)
if len(connections) == 0:
next = prev
t = prev_t
prob_next = 1
else:
if valid_stops:
if current_line not in valid_stops[stops_dict[prev]]:
print("DAMN")
print((stops_dict[prev], current_line))
skip_bad += 1
break
(t, current_trip) = random.choice(possible_trips)
next_stops = [s for s in reversed(current_trip.stops) if s[1] > t]
lines.append(current_line)
trajectory.append(prev)
times.append(times[-1])
current_line = current_trip.get_line()
current_time = TTime(current_day, 0) + t
cur_changes += 1
if current_line not in valid_stops[stops_dict[next]]:
print("DAMN")
print((stops_dict[next], current_line))
skip_bad += 1
break
lines.append(current_line)
trajectory.append(next)
times.append(tripsByDay[current_day.val()][(current_line, current_trip.start_time)])
complete_trajectory.append(next)
else:
(t,_,next) = next_stops.pop()
possible_trips = [(ttrip[0], network.trips[ttrip[1]]) for c in network.stops[next].connections \
for ttrip in (tripsByStop[next_day.val() % 7][c] if c in tripsByStop[next_day.val() % 7] else []) \
if ta <= ttrip[0] <= ta + max_waiting_time and c not in complete_trajectory]
possible_trips = [(t,trip) for (t,trip) in possible_trips \
if trip.route not in [tr.route for tr in trips] \
and len([s for s in trip.stops if s[1] > t and s[2] not in complete_trajectory and is_better_stop(s,next_stops)]) > 1]
if len(possible_trips) > 0:
complete_trajectory += [s[2] for s in next_stops if s[0] <= tz]
trajectory.append(next)
trips.append(current_trip)
lines.append(current_line)
times.append(tripsByDay[current_day.val()][(current_line, current_trip.start_time)])
current_day = next_day
(t, current_trip) = random.choice(possible_trips)
current_line = current_trip.get_line()
current_time = TTime(current_day, 0) + t
next_stops = [s for s in current_trip.stops if s[1] >= t and s[2] not in complete_trajectory]
(_,t,next) = next_stops.pop(0)
complete_trajectory.append(next)
trajectory.append(next)
trips.append(current_trip)
lines.append(current_line)
times.append(tripsByDay[current_day.val()][(current_line, current_trip.start_time)])
switch_stops = [s for s in next_stops if len(network.stops[s[2]].connections) > 0 and s[2] not in complete_trajectory]
cur_switch += 1
else:
switch_stops.remove((ta,tz,next))
except (IndexError,KeyError):
err += 1
switch_stops.remove((ta,tz,next))
if len(next_stops) > 0:
(t,_,next) = next_stops.pop()
complete_trajectory.append(next)
except IndexError:
err += 1
if cur_switch < switch_goal and n_switch[cur_switch] <= 0:
skip_bad += 1
continue
if (trajectory[-1] != next):
trajectory.append(next)
times.append(tripsByDay[current_day.val()][(current_line, current_trip.start_time)])
lines.append(current_line)
if len(trajectory) % 2 == 1 or any([]):
# Just discard this piece of shit
err += 1
continue
# complete last stage
(t,_,next) = random.choice(next_stops)
trajectory.append(next)
trips.append(current_trip)
lines.append(current_line)
times.append(tripsByDay[current_day.val()][(current_line, current_trip.start_time)])
n_switch[cur_switch] -= 1
changes.update([cur_switch])
lengths.update([len(trajectory)])
unused_stops.difference_update(trajectory)
trajectory[:] = map(lambda (l,s,t): "%s:%s:%s" % (l,str(stops_dict[s]),str(t)), zip(lines, trajectory, times))
# loops = min(int(random.expovariate(0.5)) + 1, n_traj-i)
loops = 1
for _ in xrange(loops):
changes.update([cur_changes])
lengths.update([len(trajectory)])
sys.stdout.write(",".join(trajectory))
sys.stdout.write("\n")
i += 1
sys.stdout.write(",".join(map(lambda (l,s,t): "%s:%s:%s" % (l,str(stops_dict[s]),str(t)), zip(lines, trajectory, times))))
sys.stdout.write("\n")
i+=1
if unused_stops:
sys.stderr.write("\nWARNING: " + str(len(unused_stops)) + " Unused stops\n")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment