Commit fabc021b authored by mugurelionut's avatar mugurelionut
Browse files

hopefully fixed all potential deadlocks causing timeouts 3rd attempt

parent 2c8b52b7
......@@ -231,6 +231,7 @@ void RepopulateVisitingOrders() {
}
int covered_by[MAX_NUM_THREADS][TMAX][MAXNODES], is_covered[MAX_NUM_THREADS][TMAX][MAXNODES], is_covered_idx[MAX_NUM_THREADS], can_reach_idx[MAX_NUM_THREADS], can_reach[MAX_NUM_THREADS][TMAX][MAXNODES][4], can_reach_with_t1[MAX_NUM_THREADS][TMAX][MAXNODES][4];
int tmax_at_poz_node[MAX_NUM_THREADS][NMAX];
void CheckAgent(int aid) {
auto& agent_aid = agent[aid];
......@@ -345,8 +346,18 @@ void ReadAgentsData(bool replay_mode = false) {
tmp_agent.inside_poz = tmp_agent.status != READY_TO_DEPART && tmp_agent.status != DONE_REMOVED;
auto& agent_aid = agent[aid];
if (has_moving_to_data) fscanf(fin, "%d %d", &tmp_agent.moving_to_node, &tmp_agent.moving_to_o);
else if (TINIT == 0) tmp_agent.moving_to_node = tmp_agent.moving_to_o = -1;
if (has_moving_to_data) {
int row, col;
fscanf(fin, "%d %d %d", &row, &col, &tmp_agent.moving_to_o);
if (row < 0 && col < 0) {
tmp_agent.moving_to_node = -1;
assert(tmp_agent.moving_to_o < 0);
} else {
tmp_agent.moving_to_node = cell_to_node[row][col];
assert(0 <= tmp_agent.moving_to_node && tmp_agent.moving_to_node < nnodes);
assert(0 <= tmp_agent.moving_to_o && tmp_agent.moving_to_o <= 3);
}
} else if (TINIT == 0) tmp_agent.moving_to_node = tmp_agent.moving_to_o = -1;
else {
tmp_agent.moving_to_node = agent_aid.moving_to_node;
tmp_agent.moving_to_o = agent_aid.moving_to_o;
......@@ -415,7 +426,7 @@ void ReadAgentsData(bool replay_mode = false) {
if (ipath_aid.tmax >= TINIT && ipath_aid.p[ipath_aid.tmax].node == agent_aid.target_node && agent_aid.status != DONE_REMOVED)
++num_planned;
DBG(2, "\nTINIT=%d aid=%d: poz=(%d %d %d):%d inside_poz=%d target=(%d %d):%d moving_to:(%d %d) cturns=%d speed=%.6lf poz_frac=%.6lf malf=%d nr_malf=%d status=%d fresh_malf=%d\n", TINIT, agent_aid.aid, agent_aid.poz_row, agent_aid.poz_col, agent_aid.poz_o, agent_aid.poz_node, agent_aid.inside_poz, agent_aid.target_row, agent_aid.target_col, agent_aid.target_node, agent_aid.moving_to_node, agent_aid.moving_to_o, agent_aid.cturns, agent_aid.speed, agent_aid.poz_frac, agent_aid.malfunc, agent_aid.nr_malfunc, agent_aid.status, agent_aid.fresh_malfunc);
DBG(2, "\nTINIT=%d aid=%d: poz=(%d %d %d):%d inside_poz=%d target=(%d %d):%d moving_to_node=%d:(%d %d) moving_to_o=%d cturns=%d speed=%.6lf poz_frac=%.6lf malf=%d nr_malf=%d status=%d fresh_malf=%d\n", TINIT, agent_aid.aid, agent_aid.poz_row, agent_aid.poz_col, agent_aid.poz_o, agent_aid.poz_node, agent_aid.inside_poz, agent_aid.target_row, agent_aid.target_col, agent_aid.target_node, agent_aid.moving_to_node, node[agent_aid.moving_to_node].row, node[agent_aid.moving_to_node].col, agent_aid.moving_to_o, agent_aid.cturns, agent_aid.speed, agent_aid.poz_frac, agent_aid.malfunc, agent_aid.nr_malfunc, agent_aid.status, agent_aid.fresh_malfunc);
assert(0 <= agent_aid.poz_row && agent_aid.poz_row < H);
assert(0 <= agent_aid.poz_col && agent_aid.poz_col < W);
......@@ -429,7 +440,7 @@ void ReadAgentsData(bool replay_mode = false) {
if (TINIT >= 1) {
CheckAgent(aid);
DBG(2, " still moving_to:(%d %d)\n", agent_aid.moving_to_node, agent_aid.moving_to_o);
DBG(2, " still moving_to_node=%d:(%d %d) moving_to_o=%d\n", agent_aid.moving_to_node, node[agent_aid.moving_to_node].row, node[agent_aid.moving_to_node].col, agent_aid.moving_to_o);
}
}
......@@ -691,7 +702,7 @@ inline bool IsFreeTimeWindow(int aid, int t1, int t2, int node, const int covere
int tend_ongoing_move[NMAX];
bool OverlapsOngoingMove(int t1, int t2, int node, const int covered_by[][MAXNODES], const int is_covered[][MAXNODES], int is_covered_idx, const Path tmp_path[]) {
bool OverlapsOngoingMove(int t1, int t2, int node, const int covered_by[][MAXNODES], const int is_covered[][MAXNODES], int is_covered_idx, const Path tmp_path[], int tmax_at_poz_node[]) {
if (t1 < tend_ongoing_move[node]) return true;
for (int tend = t2 + 1; tend <= t2 + 3 && tend <= T; ++tend) {
if (is_covered[tend][node] == is_covered_idx) {
......@@ -703,7 +714,8 @@ bool OverlapsOngoingMove(int t1, int t2, int node, const int covered_by[][MAXNOD
for (int tend = t1 + 1; tend <= t2; ++tend) {
if (is_covered[tend][node] == is_covered_idx) {
const auto& aid_tend = covered_by[tend][node];
if (agent[aid_tend].poz_node != node) return true;
const auto& curr_node_aid_tend = agent[aid_tend].poz_node;
if (curr_node_aid_tend != node || tmax_at_poz_node[aid_tend] < tend) return true;
}
}
const auto& target_node_agents_node = target_node_agents[node];
......@@ -721,7 +733,7 @@ bool OverlapsOngoingMove(int t1, int t2, int node, const int covered_by[][MAXNOD
return false;
}
bool FindBestPath(int aid, const int covered_by[][MAXNODES], const int is_covered[][MAXNODES], int is_covered_idx, int can_reach[][MAXNODES][4], int& can_reach_idx, int can_reach_with_t1[][MAXNODES][4], HeapElement h[], int& hsize, PrevState prev[][MAXNODES][4], Path tmp_path[], Path* tmp_path2) {
bool FindBestPath(int aid, const int covered_by[][MAXNODES], const int is_covered[][MAXNODES], int is_covered_idx, int can_reach[][MAXNODES][4], int& can_reach_idx, int can_reach_with_t1[][MAXNODES][4], HeapElement h[], int& hsize, PrevState prev[][MAXNODES][4], Path tmp_path[], Path* tmp_path2, int tmax_at_poz_node[]) {
const auto& agent_aid = agent[aid];
const auto& ipath_aid = ipath[aid];
auto& tmp_path_aid = tmp_path[aid];
......@@ -755,11 +767,9 @@ bool FindBestPath(int aid, const int covered_by[][MAXNODES], const int is_covere
t1 = TINIT;
rnode = agent_aid.poz_node;
ro = agent_aid.poz_o;
int moving_to_node = ipath_aid.p[TINIT].moving_to_node, moving_to_o = ipath_aid.p[TINIT].moving_to_o;
int moving_to_node = agent_aid.moving_to_node, moving_to_o = agent_aid.moving_to_o;
while (t1 + 1 <= ipath_aid.tmax && ipath_aid.p[t1 + 1].how_i_got_here == MALFUNCTIONED) {
++t1;
assert(ipath_aid.p[t1].moving_to_node == moving_to_node);
assert(ipath_aid.p[t1].moving_to_o == moving_to_o);
if (!CanEnterCell(aid, t1, rnode, rnode, covered_by, is_covered, is_covered_idx, tmp_path)) return false;
}
if (t1 >= T) return false;
......@@ -792,7 +802,7 @@ bool FindBestPath(int aid, const int covered_by[][MAXNODES], const int is_covere
rnode = moving_to_node;
ro = moving_to_o;
}
if (USE_SPACING_TO_AVOID_DEADLOCKS && OverlapsOngoingMove(TINIT, t2, moving_to_node, covered_by, is_covered, is_covered_idx, tmp_path))
if (USE_SPACING_TO_AVOID_DEADLOCKS && OverlapsOngoingMove(TINIT, t2, moving_to_node, covered_by, is_covered, is_covered_idx, tmp_path, tmax_at_poz_node))
return false;
}
const int est_tmin = t2 + dmin_aid[rnode][ro];
......@@ -848,7 +858,7 @@ bool FindBestPath(int aid, const int covered_by[][MAXNODES], const int is_covere
const auto& node2 = next_node_o[onext];
if (node2 >= 0 && node2 < nnodes && CanEnterCell(aid, tarrive_node2, node, node2, covered_by, is_covered, is_covered_idx, tmp_path) &&
(can_reach[tarrive_node2][node2][onext] != can_reach_idx || can_reach_with_t1[tarrive_node2][node2][onext] < ct1)) {
if (USE_SPACING_TO_AVOID_DEADLOCKS && OverlapsOngoingMove(t, tarrive_node2, node2, covered_by, is_covered, is_covered_idx, tmp_path))
if (USE_SPACING_TO_AVOID_DEADLOCKS && OverlapsOngoingMove(t, tarrive_node2, node2, covered_by, is_covered, is_covered_idx, tmp_path, tmax_at_poz_node))
continue;
if (node2 != target_node) {
can_reach[tarrive_node2][node2][onext] = can_reach_idx;
......@@ -1114,7 +1124,7 @@ void CheckNonDeadlockPaths() {
const auto& tdeparture = tuple.first.second;
const auto& aid = tuple.second;
if (prev_tarrival > tdeparture) {
DBG(0, "!!! [CheckNonDeadlockPaths] node=%d: aid=%d tdep=%d tarr=%d target=%d | aid2=%d tdep2=%d tarr=%d\n", node, aid, tdeparture, tarrival, agent[aid].target_node, prev_aid, prev_tdeparture, prev_tarrival, agent[prev_aid].target_node);
DBG(0, "!!! [CheckNonDeadlockPaths] node=%d: aid=%d ct=%d tdep=%d tarr=%d target=%d | aid2=%d ct2=%d tdep2=%d tarr2=%d target2=%d\n", node, aid, agent[aid].cturns, tdeparture, tarrival, agent[aid].target_node, prev_aid, agent[prev_aid].cturns, prev_tdeparture, prev_tarrival, agent[prev_aid].target_node);
DBG(0, " tend_ongoing_move[node]=%d\n", tend_ongoing_move[node]);
exit(1);
}
......@@ -1140,31 +1150,24 @@ void RandomPermutations(int tid, int ntries) {
auto& prev_tid = prev[tid];
auto& tmp_path_tid = tmp_path[tid];
auto& tmp_path2_tid = tmp_path2[tid];
auto& tmax_at_poz_node_tid = tmax_at_poz_node[tid];
if (tid == 0) {
shpaths_sorted.resize(N);
shpaths_sorted.clear();
for (int aid = 0; aid < N; ++aid) {
shpaths_sorted[aid].second = aid;
const auto& agent_aid = agent[aid];
shpaths_sorted[aid].first = agent_aid.malfunc + dmin[aid][agent_aid.poz_node][agent_aid.poz_o];
shpaths_sorted.push_back({agent_aid.malfunc + dmin[aid][agent_aid.poz_node][agent_aid.poz_o], aid});
}
sort(shpaths_sorted.begin(), shpaths_sorted.end());
}
for (int trial = 1; trial <= ntries; ++trial) {
//DBG(0, "trial=%d\n", trial);
if (tid == 0 && trial <= 1) {
for (int i = 0; i < N; ++i) perm_tid[i] = shpaths_sorted[i].second;
reverse(shpaths_sorted.begin(), shpaths_sorted.end());
} else {
for (int i = 0; i < N; ++i) pused_tid[i] = 0;
int idx = 0;
/*for (int i = 0; i < N; ++i) {
do {
perm_tid[idx] = xor128_tid.rand() % N;
} while (pused_tid[perm_tid[idx]]);
pused_tid[perm_tid[idx++]] = 1;
}*/
if ((trial & 1) == 1) {
/*for (int cturns = MAX_CTURNS - 1; cturns >= 0; --cturns) {
const auto& cturns_agents_cturns = cturns_agents[cturns];
......@@ -1207,11 +1210,21 @@ void RandomPermutations(int tid, int ntries) {
}
++is_covered_idx_tid;
for (int aid = 0; aid < N; ++aid) {
auto& tmax_at_poz_node_tid_aid = tmax_at_poz_node_tid[aid];
tmax_at_poz_node_tid_aid = -1;
const auto& agent_aid = agent[aid];
if (agent_aid.status == DONE_REMOVED) continue;
const auto& ipath_aid = ipath[aid];
CoverPath(aid, ipath_aid, covered_by_tid, is_covered_tid, is_covered_idx_tid);
CopyPath(ipath_aid, &tmp_path_tid[aid]);
if (agent_aid.inside_poz) {
tmax_at_poz_node_tid_aid = TINIT;
for (int t = TINIT + 1; t <= ipath_aid.tmax; ++t) {
const auto& path_elem = ipath_aid.p[t];
if (path_elem.node != agent_aid.poz_node) break;
tmax_at_poz_node_tid_aid = t;
}
}
}
for (int idx = 0; idx < N; ++idx) {
const auto& aid = perm_tid[idx];
......@@ -1221,9 +1234,19 @@ void RandomPermutations(int tid, int ntries) {
auto& tmp_path_tid_aid = tmp_path_tid[aid];
UncoverPath(aid, ipath_aid, covered_by_tid, is_covered_tid, is_covered_idx_tid);
DBG(3, "TINIT=%d tid=%d trial=%d/%d idx=%d: aid=%d start state=%d\n", TINIT, tid, trial, ntries, idx, aid, ipath_aid.p[TINIT].how_i_got_here);
FindBestPath(aid, covered_by_tid, is_covered_tid, is_covered_idx_tid, can_reach_tid, can_reach_idx_tid, can_reach_with_t1_tid, heap_tid, heap_size_tid, prev_tid, tmp_path_tid, &tmp_path2_tid[aid]);
FindBestPath(aid, covered_by_tid, is_covered_tid, is_covered_idx_tid, can_reach_tid, can_reach_idx_tid, can_reach_with_t1_tid, heap_tid, heap_size_tid, prev_tid, tmp_path_tid, &tmp_path2_tid[aid], tmax_at_poz_node_tid);
DBG(3, "TINIT=%d tid=%d trial=%d/%d idx=%d: aid=%d done tmax=%d/%d reaches_target=%d(%d:%d) (%.3lf)\n", TINIT, tid, trial, ntries, idx, aid, tmp_path_tid_aid.tmax, T, tmp_path_tid_aid.p[tmp_path_tid_aid.tmax].node == agent_aid.target_node, tmp_path_tid_aid.p[tmp_path_tid_aid.tmax].node, agent_aid.target_node, GetTime() - TSTART);
CoverPath(aid, tmp_path_tid_aid, covered_by_tid, is_covered_tid, is_covered_idx_tid);
auto& tmax_at_poz_node_tid_aid = tmax_at_poz_node_tid[aid];
tmax_at_poz_node_tid_aid = -1;
if (agent_aid.inside_poz) {
tmax_at_poz_node_tid_aid = TINIT;
for (int t = TINIT + 1; t <= tmp_path_tid_aid.tmax; ++t) {
const auto& path_elem = tmp_path_tid_aid.p[t];
if (path_elem.node != agent_aid.poz_node) break;
tmax_at_poz_node_tid_aid = t;
}
}
}
if (RunConsistencyChecks(tmp_path_tid, covered_by_tid, is_covered_tid, is_covered_idx_tid, false)) {
......@@ -1385,9 +1408,9 @@ int qaid[QMOD + 1];
bool AdjustIPaths() {
/*for (int aid = 0; aid < N; ++aid) {
if (aid == 27 || aid == 49 || aid == 56) {
if (aid == 46) {
const auto& ipath_aid = ipath[aid];
for (int t = max(TINIT, 450); t <= 490 && t <= ipath_aid.tmax; ++t) {
for (int t = max(TINIT, 795); t <= 805 && t <= ipath_aid.tmax; ++t) {
const auto& elem = ipath_aid.p[t];
DBG(0, "aid=%d t=%d node=%d npt=%d\n", aid, t, elem.node, elem.num_partial_turns);
}
......@@ -1950,7 +1973,7 @@ int GetMove(int aid) {
const auto& path_elem = path_aid.p[TINIT];
const auto& path_elem_end_turn = path_aid.p[TINIT + 1];
int action = DO_NOTHING;
DBG(3, " GetMove for aid=%d: tmax=%d\n", aid, path_aid.tmax);
DBG(2, " GetMove for aid=%d: tmax=%d\n", aid, path_aid.tmax);
if (path_elem_end_turn.how_i_got_here == ENTERED_SRC) {
assert(path_elem.node == path_elem_end_turn.node);
assert(path_elem.o == path_elem_end_turn.o);
......@@ -2047,7 +2070,7 @@ void SaveDataForReplay(const char* testid) {
fprintf(f, "-1 -1 -1 -1\n%d %d\n%d %d %d\n", N, TINIT, num_reschedules, num_planned, num_adjust_ipaths_without_full_plan_regeneration);
for (int aid = 0; aid < N; ++aid) {
const auto& agent_aid = agent[aid];
fprintf(f, "%d %d %d %d %d %d %.10lf %.10lf %d %d %d %d 1 1 %d %d\n", agent_aid.aid, agent_aid.poz_row, agent_aid.poz_col, agent_aid.poz_o, agent_aid.target_row, agent_aid.target_col, agent_aid.speed, agent_aid.poz_frac, agent_aid.malfunc, agent_aid.nr_malfunc, agent_aid.status, agent_aid.fresh_malfunc, agent_aid.moving_to_node, agent_aid.moving_to_o);
fprintf(f, "%d %d %d %d %d %d %.10lf %.10lf %d %d %d %d 1 1 %d %d %d\n", agent_aid.aid, agent_aid.poz_row, agent_aid.poz_col, agent_aid.poz_o, agent_aid.target_row, agent_aid.target_col, agent_aid.speed, agent_aid.poz_frac, agent_aid.malfunc, agent_aid.nr_malfunc, agent_aid.status, agent_aid.fresh_malfunc, agent_aid.moving_to_node >= 0 ? node[agent_aid.moving_to_node].row : -1, agent_aid.moving_to_node >= 0 ? node[agent_aid.moving_to_node].col : -1, agent_aid.moving_to_o);
const auto& ipath_aid = ipath[aid];
fprintf(f, "%d\n", ipath_aid.tmax);
for (int t = TINIT; t <= ipath_aid.tmax; ++t) {
......@@ -2124,4 +2147,10 @@ int main() {
return 0;
}
// 0 100 74.0740740741 -55391.9166667168
/*
0 100 74.0740740741 -55460.4166667195
1 100 71.4285714286 -45245.7500000240
2 56 46.2809917355 -47283.7500000259
3 80 100.0000000000 -12941.6666666694
4 96 61.9354838710 -97453.3333331954
*/
No preview for this file type
......@@ -25,7 +25,7 @@ def GetTestParams(tid):
return (seed, width, height, nr_trains, nr_cities, max_rails_between_cities, max_rails_in_cities, malfunction_rate, malfunction_min_duration, malfunction_max_duration)
def ShouldRunTest(tid):
return tid >= 1
return tid >= 5
#return tid == 2
return True
......
1 102 72.8571428571 -45028.4166666912
5 126 96.1832061069 -44149.0833333480
6 138 79.7687861272 -70195.6666667112
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