Skip to content

Commit

Permalink
Merge pull request #108 from inverted-ai/update-jan-06
Browse files Browse the repository at this point in the history
Update jan 06
  • Loading branch information
AlirezaMorsali committed Jan 7, 2023
2 parents 4925151 + a402eb7 commit 101152e
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 54 deletions.
1 change: 1 addition & 0 deletions invertedai_cpp/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
startup --output_base=/home/user/.cache/bazel_docker/base
startup --output_user_root=/home/user/.cache/bazel_docker/root
build --cxxopt='-std=c++17'
1 change: 1 addition & 0 deletions invertedai_cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ docker compose run --rm dev
- [Bazel](https://bazel.build/install)
- Opencv (`sudo apt-get install -y libopencv-dev`)
- Boost (`sudo apt install openssl libssl-dev libboost-all-dev`)
- [Rules Boost](https://github.com/nelhage/rules_boost) (Please update the [latest commit](https://github.com/nelhage/rules_boost/commits/master) in the `WORKSPACE`)
- [JSON for Modern C++](https://json.nlohmann.me/) (under `invertedai/externals/json.hpp`)

## Usage
Expand Down
3 changes: 1 addition & 2 deletions invertedai_cpp/WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

_RULES_BOOST_COMMIT = "652b21e35e4eeed5579e696da0facbe8dba52b1f"
_RULES_BOOST_COMMIT = "a96f8482a80c875a020cd9ed590b30b44e67785d"

http_archive(
name = "com_github_nelhage_rules_boost",
sha256 = "c1b8b2adc3b4201683cf94dda7eef3fc0f4f4c0ea5caa3ed3feffe07e1fb5b15",
strip_prefix = "rules_boost-%s" % _RULES_BOOST_COMMIT,
urls = [
"https://github.com/nelhage/rules_boost/archive/%s.tar.gz" % _RULES_BOOST_COMMIT,
Expand Down
14 changes: 7 additions & 7 deletions invertedai_cpp/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ services:
# restart: always
environment:
DISPLAY: $DISPLAY
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: 1
capabilities: [ gpu ]
# deploy:
# resources:
# reservations:
# devices:
# - driver: nvidia
# count: 1
# capabilities: [ gpu ]
privileged: true
# stdin_open: true # docker run -i
# tty: true # docker run -t
Expand Down
3 changes: 2 additions & 1 deletion invertedai_cpp/examples/client_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int main(int argc, char **argv) {
invertedai::LocationInfoRequest loc_info_req(
"{\"location\": \"" + location +
"\", "
"\"include_map_source\": true}");
"\"include_map_source\": false}");
// get response of location information
invertedai::LocationInfoResponse loc_info_res =
invertedai::location_info(loc_info_req, &session);
Expand Down Expand Up @@ -61,6 +61,7 @@ int main(int argc, char **argv) {
// construct request for stepping the simulation (driving the NPCs)
invertedai::DriveRequest drive_req(
invertedai::read_file("examples/drive_body.json"));
drive_req.set_location(location);
drive_req.update(init_res);

for (int t = 0; t < timestep; t++) {
Expand Down
2 changes: 1 addition & 1 deletion invertedai_cpp/examples/drive_body.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
"get_infractions": true,
"traffic_lights_states": null,
"random_seed": null
}
}
10 changes: 7 additions & 3 deletions invertedai_cpp/examples/initialize_body.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"get_birdview": true,
"get_infractions": true,
"random_seed": null,
"conditional_agent_states": [[1,1,1,1], [1,1,1,1]],
"conditional_agent_attributes": [[1,1,1], [1,1,1]]
}
"location_of_interest": [
100,
100
],
"conditional_agent_states": null,
"conditional_agent_attributes": null
}
8 changes: 5 additions & 3 deletions invertedai_cpp/invertedai/data_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#define DATA_UTILS_H

#include <map>
#include <optional>
#include <string>
#include <vector>

namespace invertedai {

Expand Down Expand Up @@ -96,7 +98,7 @@ struct StaticMapActor {
/**
* Not currently used, there may be more traffic signals in the future.
*/
int agent_type;
std::string agent_type;
/**
* The position of the stop line.
*/
Expand All @@ -109,8 +111,8 @@ struct StaticMapActor {
/**
* Size of the stop line, in meters, along its orientation.
*/
double length, width;
int dependant;
std::optional<double> length, width;
std::vector<int> dependant;
};

/**
Expand Down
20 changes: 14 additions & 6 deletions invertedai_cpp/invertedai/drive_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ DriveRequest::DriveRequest(const std::string &body_str) {
this->get_infractions_ = this->body_json_["get_infractions"].is_boolean()
? this->body_json_["get_infractions"].get<bool>()
: false;
this->random_seed_ = this->body_json_["random_seed"].is_number_integer()
? this->body_json_["random_seed"].get<int>()
: 0;
this->random_seed_ =
this->body_json_["random_seed"].is_number_integer()
? std::optional<int>{this->body_json_["random_seed"].get<int>()}
: std::nullopt;
}

void DriveRequest::refresh_body_json_() {
Expand Down Expand Up @@ -71,7 +72,12 @@ void DriveRequest::refresh_body_json_() {
}
this->body_json_["get_birdview"] = this->get_birdview_;
this->body_json_["get_infractions"] = this->get_infractions_;
this->body_json_["random_seed"] = this->random_seed_;
if (this->random_seed_.has_value()) {
this->body_json_["random_seed"] = this->random_seed_.value();

} else {
this->body_json_["random_seed"] = nullptr;
}
}

void DriveRequest::update(const InitializeResponse &init_res) {
Expand Down Expand Up @@ -112,7 +118,9 @@ bool DriveRequest::get_birdview() const { return this->get_birdview_; }

bool DriveRequest::get_infractions() const { return this->get_infractions_; }

int DriveRequest::random_seed() const { return this->random_seed_; }
std::optional<int> DriveRequest::random_seed() const {
return this->random_seed_;
}

void DriveRequest::set_location(const std::string &location) {
this->location_ = location;
Expand Down Expand Up @@ -146,7 +154,7 @@ void DriveRequest::set_get_infractions(bool get_infractions) {
this->get_infractions_ = get_infractions;
}

void DriveRequest::set_random_seed(int random_seed) {
void DriveRequest::set_random_seed(std::optional<int> random_seed) {
this->random_seed_ = random_seed;
}

Expand Down
7 changes: 4 additions & 3 deletions invertedai_cpp/invertedai/drive_request.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef DRIVE_REQUEST_H
#define DRIVE_REQUEST_H

#include <optional>
#include <string>
#include <vector>

Expand All @@ -23,7 +24,7 @@ class DriveRequest {
std::vector<std::vector<double>> recurrent_states_;
bool get_birdview_;
bool get_infractions_;
int random_seed_;
std::optional<int> random_seed_;
json body_json_;

void refresh_body_json_();
Expand Down Expand Up @@ -82,7 +83,7 @@ class DriveRequest {
/**
* Get random_seed.
*/
int random_seed() const;
std::optional<int> random_seed() const;

// setters
/**
Expand Down Expand Up @@ -128,7 +129,7 @@ class DriveRequest {
* Set random_seed, which controls the stochastic aspects of agent behavior
* for reproducibility.
*/
void set_random_seed(int random_seed);
void set_random_seed(std::optional<int> random_seed);
};

} // namespace invertedai
Expand Down
55 changes: 43 additions & 12 deletions invertedai_cpp/invertedai/initialize_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ InitializeRequest::InitializeRequest(const std::string &body_str) {
AgentAttributes agent_attribute = {element[0], element[1], element[2]};
this->conditional_agent_attributes_.push_back(agent_attribute);
}

this->states_history_.clear();
for (const auto &elements : this->body_json_["states_history"]) {
std::vector<AgentState> agent_states;
Expand All @@ -48,15 +48,21 @@ InitializeRequest::InitializeRequest(const std::string &body_str) {
}
this->traffic_light_state_history_.push_back(traffic_light_states);
}
this->location_of_interest_ =
this->body_json_["location_of_interest"].is_null()
? std::nullopt
: std::optional<std::pair<double, double>>{
this->body_json_["location_of_interest"]};
this->get_birdview_ = this->body_json_["get_birdview"].is_boolean()
? this->body_json_["get_birdview"].get<bool>()
: false;
this->get_infractions_ = this->body_json_["get_infractions"].is_boolean()
? this->body_json_["get_infractions"].get<bool>()
: false;
this->random_seed_ = this->body_json_["random_seed"].is_number_integer()
? this->body_json_["random_seed"].get<int>()
: 0;
this->random_seed_ =
this->body_json_["random_seed"].is_number_integer()
? std::optional<int>{this->body_json_["random_seed"].get<int>()}
: std::nullopt;
}

void InitializeRequest::refresh_body_json_() {
Expand Down Expand Up @@ -90,9 +96,19 @@ void InitializeRequest::refresh_body_json_() {
}
this->body_json_["traffic_light_state_history"].push_back(elements);
}
if (this->location_of_interest_.has_value()) {
this->body_json_["location_of_interest"] =
this->location_of_interest_.value();
} else {
this->body_json_["location_of_interest"] = nullptr;
}
this->body_json_["get_birdview"] = this->get_birdview_;
this->body_json_["get_infractions"] = this->get_infractions_;
this->body_json_["random_seed"] = this->random_seed_;
if (this->random_seed_.has_value()) {
this->body_json_["random_seed"] = this->random_seed_.value();
} else {
this->body_json_["random_seed"] = nullptr;
}
};

std::string InitializeRequest::body_str() {
Expand All @@ -114,7 +130,8 @@ std::vector<AgentState> InitializeRequest::conditional_agent_states() const {
return this->conditional_agent_states_;
}

std::vector<AgentAttributes> InitializeRequest::conditional_agent_attributes() const {
std::vector<AgentAttributes>
InitializeRequest::conditional_agent_attributes() const {
return this->conditional_agent_attributes_;
}

Expand All @@ -127,13 +144,20 @@ InitializeRequest::traffic_light_state_history() const {
return this->traffic_light_state_history_;
}

std::optional<std::pair<double, double>>
InitializeRequest::location_of_interest() const {
return this->location_of_interest_;
}

bool InitializeRequest::get_birdview() const { return this->get_birdview_; }

bool InitializeRequest::get_infractions() const {
return this->get_infractions_;
}

int InitializeRequest::random_seed() const { return this->random_seed_; }
std::optional<int> InitializeRequest::random_seed() const {
return this->random_seed_;
}

void InitializeRequest::set_location(const std::string &location) {
this->location_ = location;
Expand All @@ -143,14 +167,16 @@ void InitializeRequest::set_num_agents_to_spawn(int num_agents_to_spawn) {
this->num_agents_to_spawn_ = num_agents_to_spawn;
}

void InitializeRequest::set_conditional_agent_states(const std::vector<AgentState> &conditional_agent_states) {
void InitializeRequest::set_conditional_agent_states(
const std::vector<AgentState> &conditional_agent_states) {
this->conditional_agent_states_ = conditional_agent_states;
}

void InitializeRequest::set_conditional_agent_attributes(const std::vector<AgentAttributes> &conditional_agent_attributes) {

void InitializeRequest::set_conditional_agent_attributes(
const std::vector<AgentAttributes> &conditional_agent_attributes) {
this->conditional_agent_attributes_ = conditional_agent_attributes;
}

void InitializeRequest::set_states_history(
const std::vector<std::vector<AgentState>> &states_history) {
this->states_history_ = states_history;
Expand All @@ -167,6 +193,11 @@ void InitializeRequest::set_traffic_light_state_history(
this->traffic_light_state_history_ = traffic_light_state_history;
}

void InitializeRequest::set_location_of_interest(
const std::optional<std::pair<double, double>> &location_of_interest) {
this->location_of_interest_ = location_of_interest;
}

void InitializeRequest::set_get_birdview(bool get_birdview) {
this->get_birdview_ = get_birdview;
}
Expand All @@ -175,7 +206,7 @@ void InitializeRequest::set_get_infractions(bool get_infractions) {
this->get_infractions_ = get_infractions;
}

void InitializeRequest::set_random_seed(int random_seed) {
void InitializeRequest::set_random_seed(std::optional<int> random_seed) {
this->random_seed_ = random_seed;
}

Expand Down

0 comments on commit 101152e

Please sign in to comment.