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

Create optimistic rollouts for CTPs #326

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions domain/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ cc_library(
name = "canadian_traveler",
hdrs = ["canadian_traveler.hh"],
srcs = ["canadian_traveler.cc"],
visibility = ["//visibility:public"],
deps = ["@eigen"],
)

Expand Down
20 changes: 20 additions & 0 deletions experimental/ctp/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

package(features=["warning_compile_flags"])

cc_library(
name = "compute_optimistic_rollout",
hdrs = ["compute_optimistic_rollout.hh"],
srcs = ["compute_optimistic_rollout.cc"],
deps = [
"//domain:canadian_traveler",
]
)

cc_test(
name = "compute_optimistic_rollout_test",
srcs = ["compute_optimistic_rollout_test.cc"],
deps = [
":compute_optimistic_rollout",
"@com_google_googletest//:gtest_main",
]
)
29 changes: 29 additions & 0 deletions experimental/ctp/compute_optimistic_rollout.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

#include "experimental/ctp/compute_optimistic_rollout.hh"

#include "domain/canadian_traveler.hh"

namespace robot::experimental::ctp {
namespace {
using CTG = domain::CanadianTravelerGraph;
}

Rollout compute_optimistic_rollout([[maybe_unused]] const CTG &graph, const int initial_state,
const int goal_state,
[[maybe_unused]] const CTG::Weather &underlying_weather,
[[maybe_unused]] const CTG::Weather &initial_belief) {
Rollout out = {
.path = {initial_state},
.cost = 0.0,
};

while (out.path.back() != goal_state) {
// Observe at the current node given the underlying weather
// Create an optimistic belief
// Run Djikstra towards to goal assuming the belief
// Step in the best direction
}

return out;
}
} // namespace robot::experimental::ctp
18 changes: 18 additions & 0 deletions experimental/ctp/compute_optimistic_rollout.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

#pragma once

#include "domain/canadian_traveler.hh"

namespace robot::experimental::ctp {

struct Rollout {
std::vector<int> path;
double cost;
};

Rollout compute_optimistic_rollout(const domain::CanadianTravelerGraph &graph,
const int initial_state, const int goal_state,
const domain::CanadianTravelerGraph::Weather &underlying_weather,
const domain::CanadianTravelerGraph::Weather &initial_belief);

} // namespace robot::experimental::ctp
78 changes: 78 additions & 0 deletions experimental/ctp/compute_optimistic_rollout_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

#include "experimental/ctp/compute_optimistic_rollout.hh"

#include "domain/canadian_traveler.hh"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace robot::experimental::ctp {
namespace {
using CTG = domain::CanadianTravelerGraph;

constexpr int START_IDX = 0;
constexpr int A_IDX = 1;
constexpr int B_IDX = 2;
constexpr int GOAL_IDX = 3;
std::shared_ptr<CTG> create_test_graph() {
// Create the following graph. There exists a known traversable path from the start
// to the goal. There also exists a lower cost probabilistically traversable path
// that may be cheaper.
//
// S C=1
// ├─────────┐A
// │ :
// C=10│ : C=1
// │ : p=0.1
// │ :
// │ :
// │ :
// ├─────────┘B
// G C=1
// Start, A, B, Goal
std::vector<CTG::Node> nodes = {{{0, 10}}, {{10, 10}}, {{10, 0}}, {{0, 0}}};

std::vector<CTG::Edge> edges = {
{.node_a = START_IDX, .node_b = GOAL_IDX, .cost = 10.0, .traversal_prob = {}},
{.node_a = START_IDX, .node_b = A_IDX, .cost = 1.0, .traversal_prob = {}},
{.node_a = A_IDX, .node_b = B_IDX, .cost = 1.0, .traversal_prob = 0.1},
{.node_a = B_IDX, .node_b = GOAL_IDX, .cost = 1.0, .traversal_prob = {}},
};

return CTG::create(std::move(nodes), std::move(edges));
}
} // namespace

TEST(ComputeOptimisticRolloutTest, rollout_in_traversable_weather) {
// Setup
const auto graph_ptr = create_test_graph();
const auto weather = graph_ptr->create_weather(
{{.id_a = A_IDX, .id_b = B_IDX, .traversability = CTG::EdgeState::Traversable}});
const auto uninformed_belief = graph_ptr->create_weather();

// Action
const auto rollout =
compute_optimistic_rollout(*graph_ptr, START_IDX, GOAL_IDX, weather, uninformed_belief);

// Verification
constexpr double TOL = 1e-6;
EXPECT_THAT(rollout.path, testing::ElementsAre(START_IDX, A_IDX, B_IDX, GOAL_IDX));
EXPECT_NEAR(rollout.cost, 3.0, TOL);
}

TEST(ComputeOptimisticRolloutTest, rollout_in_untraversable_weather) {
// Setup
const auto graph_ptr = create_test_graph();
const auto weather = graph_ptr->create_weather(
{{.id_a = A_IDX, .id_b = B_IDX, .traversability = CTG::EdgeState::Untraversable}});
const auto uninformed_belief = graph_ptr->create_weather();

// Action
const auto rollout =
compute_optimistic_rollout(*graph_ptr, START_IDX, GOAL_IDX, weather, uninformed_belief);

// Verification
constexpr double TOL = 1e-6;
EXPECT_THAT(rollout.path, testing::ElementsAre(START_IDX, A_IDX, START_IDX, GOAL_IDX));
EXPECT_NEAR(rollout.cost, 12.0, TOL);
}
} // namespace robot::experimental::ctp
Loading