Skip to content

Commit

Permalink
refactor(instance): change state machine to 5 states
Browse files Browse the repository at this point in the history
  • Loading branch information
HarukiMoriarty committed Aug 8, 2024
1 parent abef10c commit 531e39e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 93 deletions.
3 changes: 1 addition & 2 deletions calmapf/include/instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ struct Instance {
// 2 -> cache hit, going to cache to get cargo (add read lock)
// 3 -> cache cleared, going to warehouse to bring back cargo
// 4 -> warehouse get cargo, find empty block, going back to insert cache (get write lock)
// 5 -> warehouse get cargo, cannot find empty block, going back to unloading port
// 6 -> cache get cargo from cache, going back to unloading port
// 5 -> warehouse get cargo, cannot find empty block / cache get cargo / cache insert cargo, going back to unloading port
std::vector<uint> bit_status;

std::vector<int> agent_group; // agents group
Expand Down
90 changes: 7 additions & 83 deletions calmapf/src/instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,28 @@ uint Instance::update_on_reaching_goals_with_cache(
assert(graph.cache->clear_cargo_from_cache(garbages[j], goals[j]));
goals[j] = garbages[j];
}
// Status 2 finished. ==> Status 6
// Status 2 finished. ==> Status 5
// Agent has moved to cache cargo target.
// Update cache lock info, directly move back to unloading port.
else if (bit_status[j] == 2) {
instance_console->debug(
"Agent {} status 2 -> status 6, reach cached cargo {} at cache "
"Agent {} status 2 -> status 5, reach cached cargo {} at cache "
"block {}, return to unloading port",
j, *cargo_goals[j], *goals[j]);
bit_status[j] = 6;
bit_status[j] = 5;
assert(graph.cache->update_cargo_from_cache(cargo_goals[j], goals[j]));
// Update goals
goals[j] = graph.unloading_ports[cargo_goals[j]->group];
}
// Status 4 finished. ==> Status 6
// Status 4 finished. ==> Status 5
// Agent has bring uncached cargo back to cache.
// Update cache, move to unloading port.
else if (bit_status[j] == 4) {
instance_console->debug(
"Agent {} status 4 -> status 6, bring cargo {} to cache block "
"Agent {} status 4 -> status 5, bring cargo {} to cache block "
"{}, then return to unloading port",
j, *cargo_goals[j], *goals[j]);
bit_status[j] = 6;
bit_status[j] = 5;
assert(graph.cache->update_cargo_into_cache(cargo_goals[j], goals[j]));
// Update goals
goals[j] = graph.unloading_ports[cargo_goals[j]->group];
Expand Down Expand Up @@ -165,7 +165,6 @@ uint Instance::update_on_reaching_goals_with_cache(
}
else if (bit_status[j] == 5) {
// Status 5 finished.
// Agent has back to unloading port, assigned with new cargo target
if (vertex_list[step][j] == goals[j]) {
if (remain_goals > 0) {
// Update statistics.
Expand Down Expand Up @@ -194,7 +193,7 @@ uint Instance::update_on_reaching_goals_with_cache(
j, *cargo_goals[j], *result.goal);
cache_access++;
cache_hit++;
bit_status[j] = 1;
bit_status[j] = 2;
goals[j] = result.goal;
}
// Cache miss, go to warehouse to get cargo
Expand Down Expand Up @@ -222,81 +221,6 @@ uint Instance::update_on_reaching_goals_with_cache(
goals[j] = trash_result.goal;
}
}
// Agent has yet not back to unloading port, we check if there is an empty
// cache block to insert
else {
if (parser->optimization) {
CacheAccessResult result = graph.cache->try_insert_cache(cargo_goals[j], graph.unloading_ports[agent_group[j]]);
// Check if the cache is available during the period
if (result.result) {
instance_console->debug(
"Agent {} status 3 -> status 4, find cache block to insert during the moving, go to cache block {}",
j, *cargo_goals[j], *result.goal);
bit_status[j] = 4;
goals[j] = result.goal;
}
}
}
}
else if (bit_status[j] == 6) {
// Status 6 finished.
// We only check status 6 if it is finished
if (vertex_list[step][j] == goals[j]) {
if (remain_goals > 0) {
// Update statistics.
// Otherwise we still let agent go to fetch new cargo, but we do
// not update the statistics.
remain_goals--;
reached_count++;
// Record finished cargo steps
cargo_steps.push_back(cargo_cnts[j]);
cargo_cnts[j] = 0;
}

instance_console->debug("Agent {} has bring cargo {} to unloading port", j, *cargo_goals[j]);

// Generate new cargo goal
Vertex* cargo = graph.get_next_goal(agent_group[j], parser->look_ahead_num);
cargo_goals[j] = cargo;
CacheAccessResult result = graph.cache->try_cache_cargo(cargo);

// Cache hit, go to cache to get cached cargo
// ==> Status 2
if (result.result) {
instance_console->debug(
"Agent {} assigned with new cargo {}, cache hit. Go to cache {}, "
"status 6 -> status 2",
j, *cargo_goals[j], *result.goal);
cache_access++;
cache_hit++;
bit_status[j] = 2;
goals[j] = result.goal;
}
// Cache miss, go to warehouse to get cargo
else {
CacheAccessResult trash_result = graph.cache->try_cache_garbage_collection(cargo);
if (trash_result.result) {
// Need to do trash collection ==> Status 0
instance_console->debug(
"Agent {} assigned with new cargo {}, cache miss. Need to do trash collection. Go to "
"clear cache {}, status 6 -> status 0",
j, *cargo_goals[j], *trash_result.goal);
garbages[j] = trash_result.garbage;
cache_access++;
bit_status[j] = 0;
}
else {
// Directly go to warehouse ==> Status 1
instance_console->debug(
"Agent {} assigned with new cargo {}, cache miss. Go to "
"warehouse, status 6 -> status 1",
j, *cargo_goals[j]);
cache_access++;
bit_status[j] = 1;
}
goals[j] = trash_result.goal;
}
}
}
}

Expand Down
16 changes: 8 additions & 8 deletions tests/test_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ TEST(Instance, state_machine_test)

goal = instance.goals[0];

// Status 4 -> Status 6
// Status 4 -> Status 5
step.clear();
vertex_list.clear();
step.push_back(goal);
vertex_list.push_back(step);

ASSERT_EQ(0, instance.update_on_reaching_goals_with_cache(vertex_list, 100, cache_access, cache_hit));
ASSERT_EQ(6, instance.bit_status[0]);
ASSERT_EQ(5, instance.bit_status[0]);

goal = instance.goals[0];

// Status 6 -> Status 0
// Status 5 -> Status 0
step.clear();
vertex_list.clear();
step.push_back(goal);
Expand Down Expand Up @@ -102,18 +102,18 @@ TEST(Instance, state_machine_test)

goal = instance.goals[0];

// Status 4 -> Status 6
// Status 4 -> Status 5
step.clear();
vertex_list.clear();
step.push_back(goal);
vertex_list.push_back(step);

ASSERT_EQ(0, instance.update_on_reaching_goals_with_cache(vertex_list, 100, cache_access, cache_hit));
ASSERT_EQ(6, instance.bit_status[0]);
ASSERT_EQ(5, instance.bit_status[0]);

goal = instance.goals[0];

// Status 6 -> Status 2
// Status 5 -> Status 2
step.clear();
vertex_list.clear();
step.push_back(goal);
Expand All @@ -124,12 +124,12 @@ TEST(Instance, state_machine_test)

goal = instance.goals[0];

// Status 2 -> Status 6
// Status 2 -> Status 5
step.clear();
vertex_list.clear();
step.push_back(goal);
vertex_list.push_back(step);

ASSERT_EQ(0, instance.update_on_reaching_goals_with_cache(vertex_list, 100, cache_access, cache_hit));
ASSERT_EQ(6, instance.bit_status[0]);
ASSERT_EQ(5, instance.bit_status[0]);
}

0 comments on commit 531e39e

Please sign in to comment.