#include <msp/core/maputils.h>
+#include <msp/time/utils.h>
#include "catalogue.h"
#include "layout.h"
#include "route.h"
TrainRoutePlanner::TrainRoutePlanner(Layout &layout):
goal(0),
+ timeout(10*Time::sec),
result(PENDING),
thread(0)
{
for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
{
TrainRoutingInfo info(*i->second);
- if(info.destination)
+ if(!info.waypoints.empty())
routed_trains.push_back(info);
}
}
TrainRoutePlanner::~TrainRoutePlanner()
{
- delete thread;
+ if(thread)
+ {
+ thread->join();
+ delete thread;
+ }
+}
+
+void TrainRoutePlanner::set_timeout(const Time::TimeDelta &t)
+{
+ timeout = t;
}
TrainRoutePlanner::Result TrainRoutePlanner::plan()
{
if(result==PENDING && goal)
{
+ if(thread)
+ {
+ thread->join();
+ delete thread;
+ thread = 0;
+ }
finalize_plan();
- delete thread;
- thread = 0;
}
return result;
void TrainRoutePlanner::create_plan()
{
+ Time::TimeStamp timeout_stamp = Time::now()+timeout;
+ unsigned count = 0;
while(!queue.empty())
{
const RoutingStep &step = get_step();
}
add_steps(step);
+
+ if(++count>=1000)
+ {
+ if(Time::now()>timeout_stamp)
+ break;
+ count = 0;
+ }
}
result = FAILED;
TrainRoutePlanner::TrainRoutingInfo::TrainRoutingInfo(Train &t):
train(&t),
speed(train->get_maximum_speed()),
+ first_noncritical(train->get_last_critical_block().next().block()),
router(train->get_ai_of_type<TrainRouter>()),
- destination(0),
+ waypoints(router ? router->get_n_waypoints() : 0),
has_duration(false)
{
- if(router)
+ if(!waypoints.empty())
{
- destination = router->get_destination();
- waypoints.resize(router->get_n_waypoints());
- metrics.resize(waypoints.size()+1);
- metrics[0] = &router->get_metric(-1);
+ metrics.resize(waypoints.size());
for(unsigned i=0; i<waypoints.size(); ++i)
{
waypoints[i] = &router->get_waypoint(i);
- metrics[i+1] = &router->get_metric(i);
+ metrics[i] = &router->get_metric(i);
}
has_duration = router->get_trip_duration();
}
TrainRoutePlanner::TrainRoutingState::TrainRoutingState(TrainRoutingInfo &inf):
info(&inf),
+ critical(true),
occupied_tracks(0),
state(MOVING),
delay(info->router->get_departure_delay()),
duration(info->router->get_trip_duration()),
- waypoint(info->waypoints.empty() ? -1 : 0),
+ waypoint(0),
blocked_by(-1)
{
const Vehicle *veh = &info->train->get_vehicle(0);
info(other.info),
track(other.track),
path(other.path),
+ critical(other.critical),
occupied_tracks(other.occupied_tracks),
offset(other.offset),
back_offset(other.back_offset),
{
TrackIter next_track = track.next(path);
- if(waypoint<0 && info->destination->has_track(*track) && !info->destination->has_track(*next_track))
+ const TrackChain *wp_chain = info->waypoints[waypoint];
+ if(wp_chain->has_track(*track) && !wp_chain->has_track(*next_track))
{
- state = ARRIVED;
- return true;
- }
- else if(waypoint>=0 && info->waypoints[waypoint]->has_track(*track) && !info->waypoints[waypoint]->has_track(*next_track))
- {
- ++waypoint;
- if(waypoint>=static_cast<int>(info->waypoints.size()))
- waypoint = -1;
+ if(waypoint+1<info->waypoints.size())
+ ++waypoint;
+ else
+ {
+ state = ARRIVED;
+ return true;
+ }
}
+ if(info->first_noncritical->has_track(*track))
+ critical = false;
+
return false;
}
offset += distance;
back_offset += distance;
- OccupiedTrack *last_occ = occupied_tracks;
- for(unsigned n=occupied_tracks->n_tracks; n>1; --n)
- last_occ = last_occ->next;
+ unsigned count_to_free = 0;
+ unsigned last_sensor_addr = 0;
+ float distance_after_sensor = 0;
+ OccupiedTrack *occ = occupied_tracks;
+ for(unsigned n=occupied_tracks->n_tracks; n>0; --n)
+ {
+ if(unsigned saddr = occ->track->get_sensor_address())
+ {
+ if(saddr!=last_sensor_addr)
+ {
+ count_to_free = 0;
+ distance_after_sensor = 0;
+ }
+ last_sensor_addr = saddr;
+ }
+
+ ++count_to_free;
+ distance_after_sensor += occ->path_length;
+
+ occ = occ->next;
+ }
- // XXX What if there's multiple tracks to remove?
- if(back_offset>last_occ->path_length)
+ if(count_to_free && back_offset>distance_after_sensor)
{
- back_offset -= last_occ->path_length;
+ back_offset -= distance_after_sensor;
if(occupied_tracks->refcount>1)
{
--occupied_tracks->refcount;
occupied_tracks = new OccupiedTrack(*occupied_tracks);
}
- --occupied_tracks->n_tracks;
+ occupied_tracks->n_tracks -= count_to_free;
}
distance_traveled += distance;
void TrainRoutePlanner::TrainRoutingState::update_estimate()
{
TrackIter iter = track.reverse(path);
- float distance = info->metrics[waypoint+1]->get_distance_from(*iter.track(), iter.entry());
- distance += track->get_type().get_path_length(path)-offset;
- remaining_estimate = distance;
+ remaining_estimate = info->metrics[waypoint]->get_distance_from(*iter.track(), iter.entry());
+ if(remaining_estimate>=0)
+ remaining_estimate += track->get_type().get_path_length(path)-offset;
+}
+
+bool TrainRoutePlanner::TrainRoutingState::is_viable() const
+{
+ if(remaining_estimate<0)
+ return false;
+ if(critical && state==BLOCKED)
+ return false;
+ return true;
}
TrainRoutePlanner::RoutingStep::RoutingStep(const RoutingStep *p):
time(p->time),
+ penalty(p->penalty),
cost_estimate(p->cost_estimate),
trains(p->trains),
prev(p)
return;
}
- TrackIter next_track = train.track.next(train.path);
train.advance_track(0);
- const TrackType::Endpoint &next_entry_ep = next_track.endpoint();
- for(unsigned i=0; next_entry_ep.paths>>i; ++i)
- if(next_entry_ep.has_path(i))
- {
- train.path = i;
- train.update_estimate();
- next.update_estimate();
- if(next.is_viable())
- new_steps.push_back(next);
- }
+ const TrackType::Endpoint &entry_ep = train.track.endpoint();
+ if(train.critical)
+ {
+ train.path = train.track->get_type().coerce_path(train.track.entry(), train.track->get_active_path());
+ train.update_estimate();
+ next.update_estimate();
+ if(next.is_viable())
+ new_steps.push_back(next);
+ }
+ else
+ {
+ for(unsigned i=0; entry_ep.paths>>i; ++i)
+ if(entry_ep.has_path(i))
+ {
+ train.path = i;
+ train.update_estimate();
+ next.update_estimate();
+ if(next.is_viable())
+ new_steps.push_back(next);
+ }
+ }
- if(next_entry_ep.paths!=next_track->get_type().get_paths())
+ new_steps.sort();
+ for(list<RoutingStep>::iterator i=new_steps.begin(); ++i!=new_steps.end(); )
+ {
+ i->penalty += 5*Time::sec;
+ i->update_estimate();
+ }
+
+ if(entry_ep.paths!=train.track->get_type().get_paths() && !train.critical)
{
RoutingStep wait(this);
wait.advance(dt);
wait.trains[train_index].state = WAITING;
+ wait.penalty += 15*Time::sec;
+ wait.update_estimate();
if(wait.is_viable())
new_steps.push_back(wait);
}
{
i->blocked_by = get_occupant(*next_track);
if(i->blocked_by>=0)
+ {
+ if(i->info->first_noncritical->has_track(*next_track))
+ i->critical = false;
i->state = BLOCKED;
+ }
else if(i->state==BLOCKED)
i->state = MOVING;
}
void TrainRoutePlanner::RoutingStep::update_estimate()
{
- cost_estimate = Time::zero;
+ cost_estimate = penalty;
for(vector<TrainRoutingState>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
if(i->remaining_estimate>=0)
cost_estimate += i->wait_time+((i->distance_traveled+i->remaining_estimate)/i->info->speed)*Time::sec;
bool TrainRoutePlanner::RoutingStep::is_viable() const
{
for(vector<TrainRoutingState>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
- if(i->remaining_estimate<0)
+ if(!i->is_viable())
return false;
for(vector<TrainRoutingState>::const_iterator i=trains.begin(); i!=trains.end(); ++i)