Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
cpp-python-bridge.cpp
Go to the documentation of this file.
1#include <cstdlib> // For std::atof
2#include <iostream>
3#include <vector>
4
5#include "../../src/planning/include/planning/cone_coloring.hpp"
6
7Cone find_equivalent(Cone cone, const std::vector<Cone>& cones) {
8 for (Cone c : cones) {
9 if (c.position.euclidean_distance(cone.position) < 0.1) {
10 return c;
11 }
12 }
13 throw std::runtime_error("No equivalent cone found");
14}
15
16double evaluate_cone_coloring(const std::vector<Cone>& track,
17 std::pair<std::vector<Cone>, std::vector<Cone>> result) {
18 double score = 0;
19 // std::cout << "Left cones: ";
20 for (Cone cone : result.first) {
21 Cone match = find_equivalent(cone, track);
22 // std::cout << "(" << cone.position.x << ", " << cone.position.y << "), ";
23 if (match.color == Color::BLUE) {
24 score++;
25 } else {
26 score--;
27 }
28 }
29 // std::cout << std::endl << "Right cones: ";
30 for (Cone cone : result.second) {
31 Cone match = find_equivalent(cone, track);
32 // std::cout << "(" << cone.position.x << ", " << cone.position.y << "), ";
33 if (match.color == Color::YELLOW) {
34 score++;
35 } else {
36 score--;
37 }
38 }
39 // std::cout << std::endl;
40
41 double final_score = score / static_cast<double>(track.size());
42 double final_final_score = 100 * (1.0 - final_score);
43 return final_final_score;
44}
45
46// Function to process the parameters (you can customize this as needed)
47double evaluate_function(const ConeColoringConfig& params, const std::vector<Cone>& track) {
48 ConeColoring cone_coloring(params);
49
50 std::vector<Pose> poses = {
51 Pose(-10, 10.8, 0), Pose(2, 11, 0.26), Pose(11.5, 13.5, 0.52), Pose(14.2, 14.2, 0),
52 Pose(15.7, 13.6, 6), Pose(18.5, 10, 4.71), Pose(17.5, 2, 4.45), Pose(-7, -14, 3.93),
53 Pose(-12.3, -14.5, 2.62), Pose(-15, -11.5, 2.36), Pose(-16.5, -9, 1.57), Pose(-15, 7, 1.57),
54 Pose(-13.5, 9, 0.785)};
55
56 double score = 0;
57 for (const auto& pose : poses) {
58 // std::cout << "\n\n\n NEW SCENARIO \n\n\n" << std::endl;
59 std::pair<std::vector<Cone>, std::vector<Cone>> colored_cones =
60 cone_coloring.color_cones(track, pose);
61 // std::cout << "Number of output cones: "
62 // << colored_cones.first.size() + colored_cones.second.size() << std::endl;
63 score += evaluate_cone_coloring(track, colored_cones);
64 }
65
66 return score;
67}
68
69int main(int argc, char* argv[]) {
70 // Ensure we have the correct number of arguments
71 if (argc != 7) { // 6 parameters + program name
72 std::cerr << "Usage: ./my_cpp_executable" << std::endl;
73 return 1;
74 }
75
76 std::vector<Cone> track = {
77 Cone(-16.7219, 9.56934, "blue_cone"), Cone(-12.7553, 12.49, "blue_cone"),
78 Cone(-6.89062, 13.0757, "blue_cone"), Cone(9.62956, 15.8379, "blue_cone"),
79 Cone(12.492, 17.0404, "blue_cone"), Cone(15.1389, 16.5025, "blue_cone"),
80 Cone(19.5034, 13.6361, "blue_cone"), Cone(20.8922, 11.7012, "blue_cone"),
81 Cone(17.5221, 15.3812, "blue_cone"), Cone(21.4761, 8.79574, "blue_cone"),
82 Cone(20.9992, 5.27662, "blue_cone"), Cone(19.9925, 2.24053, "blue_cone"),
83 Cone(19.0983, 0.0, "blue_cone"), Cone(17.1824, -3.23994, "blue_cone"),
84 Cone(11.114, -6.74408, "blue_cone"), Cone(-4.06552, 13.3637, "blue_cone"),
85 Cone(14.2831, -5.26437, "blue_cone"), Cone(8.25363, -8.53889, "blue_cone"),
86 Cone(5.06185, -10.1551, "blue_cone"), Cone(1.42086, -11.9634, "blue_cone"),
87 Cone(-2.4975, -14.0305, "blue_cone"), Cone(-5.74864, -16.1217, "blue_cone"),
88 Cone(-9.34841, -17.1551, "blue_cone"), Cone(-12.2114, -16.6459, "blue_cone"),
89 Cone(-14.4625, -14.9249, "blue_cone"), Cone(-16.2427, -13.276, "blue_cone"),
90 Cone(-0.131239, 13.3125, "blue_cone"), Cone(-18.1431, -11.086, "blue_cone"),
91 Cone(-18.6174, -7.56085, "blue_cone"), Cone(-18.9382, -5.15509, "blue_cone"),
92 Cone(-18.5558, -2.56017, "blue_cone"), Cone(-18.1206, 0.0, "blue_cone"),
93 Cone(-17.7841, 3.04246, "blue_cone"), Cone(-17.8432, 6.27091, "blue_cone"),
94 Cone(-15.1864, 11.7137, "blue_cone"), Cone(3.50416, 13.7245, "blue_cone"),
95 Cone(7.13676, 14.727, "blue_cone"), Cone(-12.2184, 7.60803, "yellow_cone"),
96 Cone(7.14787, 9.92656, "yellow_cone"), Cone(10.4312, 10.799, "yellow_cone"),
97 Cone(12.9655, 11.8014, "yellow_cone"), Cone(14.9652, 11.2833, "yellow_cone"),
98 Cone(16.6054, 9.12035, "yellow_cone"), Cone(-6.90998, 8.44454, "yellow_cone"),
99 Cone(16.7063, 6.10772, "yellow_cone"), Cone(15.876, 3.47906, "yellow_cone"),
100 Cone(15.106, 1.5027, "yellow_cone"), Cone(13.6765, 0.0, "yellow_cone"),
101 Cone(9.41953, -2.84739, "yellow_cone"), Cone(12.0732, -1.43122, "yellow_cone"),
102 Cone(7.26282, -3.9408, "yellow_cone"), Cone(4.65159, -5.15509, "yellow_cone"),
103 Cone(1.78774, -6.66723, "yellow_cone"), Cone(-1.97969, -8.56329, "yellow_cone"),
104 Cone(-4.01695, 8.50322, "yellow_cone"), Cone(-5.18123, -10.5555, "yellow_cone"),
105 Cone(-7.57043, -12.1125, "yellow_cone"), Cone(-9.76388, -12.7081, "yellow_cone"),
106 Cone(-12.0338, -11.6718, "yellow_cone"), Cone(-13.9298, -8.98291, "yellow_cone"),
107 Cone(-14.1575, -5.77329, "yellow_cone"), Cone(-14.0043, -2.62998, "yellow_cone"),
108 Cone(-13.6087, 0.0, "yellow_cone"), Cone(-13.3478, 3.05712, "yellow_cone"),
109 Cone(-0.059759, 8.37591, "yellow_cone"), Cone(-13.3455, 5.78808, "yellow_cone"),
110 Cone(3.5151, 8.6968, "yellow_cone"), Cone(-9.61314, 13.0, "blue_cone"),
111 Cone(-9.99934, 12.989, "blue_cone"), Cone(-9.62148, 8.39323, "yellow_cone"),
112 Cone(-9.98667, 8.39348, "yellow_cone")};
113
114 // Convert command-line arguments to doubles and store in the struct
115 ConeColoringConfig params;
116 params.angle_weight_ = std::atof(argv[1]); // Convert first argument to double
117 params.distance_weight_ = std::atof(argv[2]); // Convert second argument to double
118 params.ncones_weight_ = std::atof(argv[3]); // Convert third argument to double
119 params.angle_exponent_ = std::atof(argv[4]); // Convert first argument to double
120 params.distance_exponent_ = std::atof(argv[5]); // Convert second argument to double
121 params.max_cost_ = std::atof(argv[6]); // Convert third argument to double
122
123 // for (Cone cone : track) {
124 // std::cout << "(" << cone.position.x << ", " << cone.position.y << "),";
125 // }
126 // std::cout << "\n\n\n" << std::endl;
127
128 // Now params contains the parameters as doubles
129 // You can use this struct to run your logic, simulations, etc.
130
131 // Evaluate the function with the given parameters
132 double result = evaluate_function(params, track);
133
134 // Output the result (this will be captured by the Python subprocess)
135 std::cout << result << std::endl;
136
137 return 0;
138}
common_lib::structures::Cone Cone
double evaluate_cone_coloring(const std::vector< Cone > &track, std::pair< std::vector< Cone >, std::vector< Cone > > result)
double evaluate_function(const ConeColoringConfig &params, const std::vector< Cone > &track)
Cone find_equivalent(Cone cone, const std::vector< Cone > &cones)
Definition main.py:1
Definition types.hpp:66
common_lib::competition_logic::Color color
Definition cone.hpp:14