Skip to content

Commit c7794f4

Browse files
author
Saurav Agarwal
committed
Add tests and main
1 parent 53c864d commit c7794f4

11 files changed

Lines changed: 1190 additions & 18 deletions

File tree

cppsrc/main/eval_dist_gnn.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include <iomanip>
2+
#include <iostream>
3+
#include <random>
4+
#include <string>
5+
#include <cstdlib>
6+
#include <fstream>
7+
#include <chrono>
8+
#include <thread>
9+
#include <memory>
10+
11+
#include <CoverageControl/constants.h>
12+
#include <CoverageControl/parameters.h>
13+
#include <CoverageControl/typedefs.h>
14+
#include <CoverageControl/bivariate_normal_distribution.h>
15+
#include <CoverageControl/world_idf.h>
16+
#include <CoverageControl/robot_model.h>
17+
#include <CoverageControl/generate_world_map.ch>
18+
#include <CoverageControlTorch/coverage_system.h>
19+
20+
using namespace CoverageControl;
21+
using namespace CoverageControlTorch;
22+
int main(int argc, char** argv) {
23+
std::cout << "Processor count: " << std::thread::hardware_concurrency() << std::endl;
24+
Parameters params;
25+
if (argc < 6) {
26+
std::cout << "Usage: ./eval_dist_gnn <parameter_file> <env_dir> <out_dir> <model_dir> <freq>" << std::endl;
27+
return 0;
28+
}
29+
/* params.pSensorSize = 16; */
30+
std::string parameter_file = argv[1];
31+
params = Parameters(parameter_file);
32+
33+
std::unique_ptr <CoverageControlTorch::CoverageSystem> env;
34+
35+
std::string env_dir = argv[2];
36+
std::string out_dir = argv[3];
37+
std::cout << std::setprecision(16);
38+
std::string out_filename = out_dir + "eval.csv";
39+
// CHeck if out_dir exists
40+
if (std::filesystem::exists(out_dir)) {
41+
std::cout << "Output directory exists" << std::endl;
42+
} else {
43+
std::cout << "Output directory does not exist" << std::endl;
44+
std::cout << "Creating output directory" << std::endl;
45+
std::filesystem::create_directory(out_dir);
46+
}
47+
48+
std::ofstream out_file(out_filename);
49+
out_file << std::setprecision(16);
50+
51+
int freq = std::stoi(argv[5]);
52+
53+
for (int data_count = 0; data_count < 30; ++data_count) {
54+
std::cout << "Data count: " << data_count << std::endl;
55+
std::string dir = "./test/";
56+
std::string pos_file = env_dir + std::to_string(data_count) + ".pos";
57+
std::string idf_file = env_dir + std::to_string(data_count) + ".env";
58+
WorldIDF world_idf(params, idf_file);
59+
env = std::make_unique<CoverageControlTorch::CoverageSystem> (params, world_idf, pos_file);
60+
env->InitializeGNNCNN(std::string(argv[4]));
61+
int num_robots = env->GetNumRobots();
62+
/* env->PlotInitMap(dir, "init_map"); */
63+
/* auto goals = oracle.GetGoals(); */
64+
/* env->PlotMapVoronoi(dir, 0, oracle.GetVoronoi(), oracle.GetGoals()); */
65+
/* std::cout << "objective value: " << env->GetObjectiveValue() << std::endl; */
66+
bool cont_flag = true;
67+
/* std::cout << "Step: " << 0 << std::endl; */
68+
/* std::cout << "objective value: " << env->GetObjectiveValue() << std::endl; */
69+
out_file << env->GetObjectiveValue();
70+
for(int ii = 1; ii < 600; ++ii) {
71+
env->StepCNNGNNDist(freq);
72+
out_file << "," << env->GetObjectiveValue();
73+
/* std::cout << "Step: " << ii << std::endl; */
74+
/* std::cout << "objective value: " << env->GetObjectiveValue() << std::endl; */
75+
/* std::cout << "Step: " << ii << std::endl; */
76+
/* bool cont_flag = oracle.Step(); */
77+
/* auto actions = oracle.GetActions(); */
78+
/* env->StepActions(actions); */
79+
if(ii%100 == 0) {
80+
std::cout << "Step: " << ii << std::endl;
81+
std::cout << "objective value: " << env->GetObjectiveValue() << std::endl;
82+
/* env->RecordPlotData(); */
83+
/* env->PlotMapVoronoi(dir, ii, oracle.GetVoronoi(), oracle.GetGoals()); */
84+
}
85+
}
86+
/* env->PlotSystemMap("./", 0); */
87+
/* env->PlotMapVoronoi("./", 0); */
88+
out_file << std::endl;
89+
}
90+
91+
/* auto zero_actions = PointVector(num_robots, Point2(0,0)); */
92+
93+
/* env->PlotMapVoronoi(dir, 1, oracle.GetVoronoi(), oracle.GetGoals()); */
94+
/* env->RenderRecordedMap(dir, "CoverageControl_oracle.mp4"); */
95+
return 0;
96+
}

cppsrc/main/eval_gnn.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <iomanip>
2+
#include <iostream>
3+
#include <random>
4+
#include <string>
5+
#include <cstdlib>
6+
#include <fstream>
7+
#include <chrono>
8+
#include <thread>
9+
#include <memory>
10+
11+
#include <CoverageControl/constants.h>
12+
#include <CoverageControl/parameters.h>
13+
#include <CoverageControl/typedefs.h>
14+
#include <CoverageControl/bivariate_normal_distribution.h>
15+
#include <CoverageControl/world_idf.h>
16+
#include <CoverageControl/robot_model.h>
17+
#include <CoverageControl/generate_world_map.ch>
18+
#include <CoverageControlTorch/coverage_system.h>
19+
20+
using namespace CoverageControl;
21+
using namespace CoverageControlTorch;
22+
int main(int argc, char** argv) {
23+
std::cout << "Processor count: " << std::thread::hardware_concurrency() << std::endl;
24+
Parameters params;
25+
if (argc < 5) {
26+
std::cout << "Usage: ./eval_gnn <parameter_file> <env_dir> <out_dir> <model_dir>" << std::endl;
27+
return 0;
28+
}
29+
/* params.pSensorSize = 16; */
30+
std::string parameter_file = argv[1];
31+
params = Parameters(parameter_file);
32+
33+
std::unique_ptr <CoverageControlTorch::CoverageSystem> env;
34+
35+
std::string env_dir = argv[2];
36+
std::string out_dir = argv[3];
37+
std::cout << std::setprecision(16);
38+
std::string out_filename = out_dir + "eval.csv";
39+
std::ofstream out_file(out_filename);
40+
out_file << std::setprecision(16);
41+
42+
43+
for (int data_count = 0; data_count < 100; ++data_count) {
44+
std::cout << "Data count: " << data_count << std::endl;
45+
std::string dir = "./test/";
46+
std::string pos_file = env_dir + std::to_string(data_count) + ".pos";
47+
std::string idf_file = env_dir + std::to_string(data_count) + ".env";
48+
WorldIDF world_idf(params, idf_file);
49+
env = std::make_unique<CoverageControlTorch::CoverageSystem> (params, world_idf, pos_file);
50+
env->InitializeGNNCNN(std::string(argv[4]));
51+
int num_robots = env->GetNumRobots();
52+
/* env->PlotInitMap(dir, "init_map"); */
53+
/* auto goals = oracle.GetGoals(); */
54+
/* env->PlotMapVoronoi(dir, 0, oracle.GetVoronoi(), oracle.GetGoals()); */
55+
/* std::cout << "objective value: " << env->GetObjectiveValue() << std::endl; */
56+
bool cont_flag = true;
57+
std::cout << "Step: " << 0 << std::endl;
58+
std::cout << "objective value: " << env->GetObjectiveValue() << std::endl;
59+
out_file << env->GetObjectiveValue();
60+
for(int ii = 1; ii < 600; ++ii) {
61+
env->StepCNNGNN();
62+
out_file << "," << env->GetObjectiveValue();
63+
/* std::cout << "Step: " << ii << std::endl; */
64+
/* bool cont_flag = oracle.Step(); */
65+
/* auto actions = oracle.GetActions(); */
66+
/* env->StepActions(actions); */
67+
if(ii%100 == 0) {
68+
std::cout << "Step: " << ii << std::endl;
69+
std::cout << "objective value: " << env->GetObjectiveValue() << std::endl;
70+
/* env->RecordPlotData(); */
71+
/* env->PlotMapVoronoi(dir, ii, oracle.GetVoronoi(), oracle.GetGoals()); */
72+
}
73+
out_file << std::endl;
74+
}
75+
}
76+
77+
/* auto zero_actions = PointVector(num_robots, Point2(0,0)); */
78+
79+
/* env->PlotMapVoronoi(dir, 1, oracle.GetVoronoi(), oracle.GetGoals()); */
80+
/* env->RenderRecordedMap(dir, "CoverageControl_oracle.mp4"); */
81+
return 0;
82+
}

cppsrc/tests/test_maps.cpp

Lines changed: 95 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
#include <torch/torch.h>
1+
#include <torch/script.h> // One-stop header.
2+
/* #include <torch/torch.h> */
23
#include <iostream>
34
#include <CoverageControlTorch/coverage_system.h>
5+
#include <CoverageControlTorch/type_conversions.h>
46

57
using namespace CoverageControl;
6-
int main() {
8+
9+
int main(int argc, const char* argv[]) {
710

811
Parameters params;
912
std::vector<Point2> robot_positions;
@@ -21,21 +24,103 @@ int main() {
2124

2225
WorldIDF world_idf(params);
2326
world_idf.AddNormalDistribution(BivariateNormalDistribution(Point2(500, 500), 10));
27+
world_idf.AddNormalDistribution(BivariateNormalDistribution(Point2(300, 500), 10));
2428

2529
CoverageControlTorch::CoverageSystem env(params, world_idf, robot_positions);
2630

31+
std::string base_dir = "./k3_params/";
32+
std::string py_dir = base_dir + "py/";
33+
std::string cpp_dir = base_dir + "cpp/";
34+
35+
// Get names of all files in py_dir
36+
for (const auto & entry : std::filesystem::directory_iterator(py_dir)) {
37+
std::string filename = entry.path().filename();
38+
std::string py_filename = py_dir + filename;
39+
std::string cpp_filename = cpp_dir + filename;
40+
std::cout << filename << std::endl;
41+
torch::Tensor tensor = GetTensorFromBytes(py_filename);
42+
std::cout << filename << " " << tensor.sizes() << std::endl;
43+
torch::save(tensor, cpp_filename);
44+
torch::Tensor temp;
45+
torch::load(temp, cpp_filename);
46+
std::cout << temp.sizes() << std::endl;
47+
std::cout << torch::equal(tensor, temp) << std::endl;
48+
}
49+
/* int nlayers = 5; */
50+
/* int K = 3; */
51+
52+
/* for(int l = 0; l < nlayers; ++l) { */
53+
/* std::cout << "Layer: " << l << std::endl; */
54+
/* std::string bname = "bias_" + std::to_string(l); */
55+
/* std::string bfilename = "./k3_params/py/" + bname + ".pt"; */
56+
/* torch::Tensor btensor = GetTensorFromBytes(bfilename); */
57+
/* std::cout << bname << " " << btensor.sizes() << std::endl; */
58+
/* torch::save(btensor, "./k3_params/cpp/" + bname + ".pt"); */
59+
/* for(int k = 0; k < K + 1; ++k) { */
60+
/* std::cout << "K: " << k << std::endl; */
61+
/* std::string name = "lin_" + std::to_string(l) + "_" + std::to_string(k); */
62+
/* std::string filename = "./k3_params/py/" + name + ".pt"; */
63+
/* torch::Tensor tensor = GetTensorFromBytes(filename); */
64+
/* std::cout << name << " " << tensor.sizes() << std::endl; */
65+
/* torch::save(tensor, "./k3_params/cpp/" + name + ".pt"); */
66+
/* torch::Tensor temp; */
67+
/* torch::load(temp, "./k3_params/cpp/" + name + ".pt"); */
68+
/* std::cout << temp.sizes() << std::endl; */
69+
/* std::cout << torch::equal(tensor, temp) << std::endl; */
70+
/* } */
71+
/* } */
72+
73+
74+
75+
/* env.LoadCNNBackBoneJIT(argv[1]); */
76+
/* env.LoadCNNBackBone(argv[1]); */
77+
78+
/* std::string tensor_file_name = std::string(argv[2]); */
79+
/* std::vector<char> f = get_the_bytes(tensor_file_name); */
80+
/* torch::IValue x = torch::pickle_load(f); */
81+
/* torch::Tensor my_tensor = x.toTensor(); */
82+
/* std::cout << my_tensor.sizes() << std::endl; */
83+
/* std::cout << torch::sum(my_tensor) << std::endl; */
84+
85+
/* my_tensor = my_tensor.view({-1, my_tensor.size(-3), my_tensor.size(-2), my_tensor.size(-1)}); */
86+
/* torch::Tensor out = env.GetGNNFeatures(my_tensor); */
87+
/* torch::save(out, "out.pt"); */
88+
/* torch::jit::script::Module container = torch::jit::load(tensor_file_name); */
89+
/* torch::Tensor tensor = container.attr("tensor").toTensor(); */
90+
/* std::cout << tensor.sizes() << std::endl; */
91+
/* std::cout << torch::sum(tensor) << std::endl; */
92+
93+
94+
2795
/* env.PlotInitMap("./", "init"); */
2896

29-
auto comm_map = env.GetAllRobotsCommunicationMaps(256);
30-
std::cout << "Computed communicaiton maps" << std::endl;
31-
std::cout << comm_map[0][0].to_sparse() << std::endl;
32-
std::cout << comm_map[0][1].to_sparse() << std::endl;
97+
/* auto comm_map = env.GetAllRobotsCommunicationMaps(32); */
98+
/* torch::save(comm_map, "comm_map.pt"); */
99+
100+
/* MapType world_map = env.GetWorldIDF(); */
101+
/* torch::Tensor world_map_tensor = CoverageControlTorch::ToTensor(world_map).clone(); */
102+
/* torch::save(world_map_tensor, "world_map.pt"); */
33103

104+
/* torch::jit::script::Module torchvision_resizer_; */
105+
/* torchvision_resizer_ = torch::jit::load("./python/ts_jit/TorchVisionResize_32.pt"); */
34106

35-
torch::Tensor a = torch::rand({5, 2});
36-
std::cout << a << std::endl;
37-
torch::Tensor b = a.transpose(1, 0);
38-
std::cout << b << std::endl;
107+
/* torch::Tensor output = torchvision_resizer_.forward({comm_map}).toTensor(); */
108+
/* torch::save(output, "comm_map_resized.pt"); */
39109

110+
/* torch::jit::script::Module module; */
111+
/* try { */
112+
/* // Deserialize the ScriptModule from a file using torch::jit::load(). */
113+
/* module = torch::jit::load(argv[1]); */
114+
/* for (auto const &params : module.named_parameters()) { */
115+
/* std::cout << params.name << " " << params.value.sizes() << std::endl; */
116+
/* std::cout << params.value << std::endl; */
117+
/* } */
118+
/* } */
119+
/* catch (const c10::Error& e) { */
120+
/* std::cerr << "error loading the model\n"; */
121+
/* return -1; */
122+
/* } */
40123

124+
std::cout << "ok\n";
125+
return 0;
41126
}

cppsrc/torch/src/base.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#include <torch/torch.h>
22
#include <iostream>
3-
#include <CoverageControl/coverage_system.h>
3+
/* #include <CoverageControl/coverage_system.h> */
44

5-
using namespace CoverageControl;
5+
/* using namespace CoverageControl; */
66
int base() {
77
torch::Tensor tensor = torch::rand({2, 3});
8-
std::cout << tensor << std::endl;
9-
torch::Tensor gpu_tensor = tensor.to(torch::kCUDA);
10-
std::cout << gpu_tensor << std::endl;
11-
Parameters params;
12-
WorldIDF world_idf(params);
13-
CoverageSystem system(params, 2, 2);
8+
/* std::cout << tensor << std::endl; */
9+
/* torch::Tensor gpu_tensor = tensor.to(torch::kCUDA); */
10+
/* std::cout << gpu_tensor << std::endl; */
11+
/* Parameters params; */
12+
/* WorldIDF world_idf(params); */
13+
/* CoverageSystem system(params, 2, 2); */
1414
return 0;
1515
}

0 commit comments

Comments
 (0)