C++代码在不同环境下运行结果不一致
问题说明
下面是一段RRT算法的C++代码,在VS2022下可以正常运行并输出路径,但是在dev c++下却无法正常输出正常路径,结束时的return value还非常奇怪。附图是dev c++的编译运行结果。
不知道是环境的问题还是代码本身的问题,本人c++水平比较一般。

// RRT Algorithm, Writter: Yuhang Zhou
#include<iostream>
#include<vector>
#include<cmath>
#include<random>
#include<float.h>
#include<time.h>
using namespace std;
class Node {
public:
vector<float> pos;
Node* parent;
Node(vector<float> _pos) {
pos = _pos;
}; // init
};
struct Point {
float x, y;
};
// add two vectors
vector<float> vec_add_sub(vector<float> v1, vector<float> v2, string opt) {
vector<float> ans;
for (int i = 0; i < v1.size(); i++) {
float ele1 = v1[i];
float ele2 = v2[i];
if (opt == "+") {
ans.push_back(ele1 + ele2);
}
else {
ans.push_back(ele1 - ele2);
}
}
return ans;
}
//
vector<float> vec_time(vector<float> v, float n_time) {
vector<float> ans;
for (int i = 0; i < v.size(); i++) {
float ele1 = v[i];
ans.push_back(ele1 * n_time);
}
return ans;
}
class RRT {
private:
vector<float> start;
vector<float> end;
vector<float> x_range, y_range;
float step_len;
int iter_max;
int resolution;
public:
vector<Node*> node_lst;
// Init
RRT(
vector<float> _start,
vector<float> _end,
vector<float> _x_range,
vector<float> _y_range,
float _step_len,
int _iter_max,
int _resolution) {
start = _start;
end = _end;
x_range = _x_range;
y_range = _y_range;
step_len = _step_len;
iter_max = _iter_max;
resolution = _resolution;
Node* start_nd = new Node(_start);
node_lst.push_back(start_nd);
}
vector<vector<float>> linspace(vector<float> _start, vector<float> _end, int n) {
if (n == 0) return {};
if (n == 1) return { _start };
vector<vector<float>> result;
result.reserve(n); // n at least
float dx = (_end[0] - _start[0]) / (n - 1);
float dy = (_end[1] - _start[1]) / (n - 1);
for (int i = 0; i < n; ++i) {
Point p;
p.x = _start[0] + dx * i;
p.y = _start[1] + dy * i;
vector<float> cur_pos = { p.x, p.y };
result.push_back(cur_pos);
}
return result;
}
// Collision function
bool is_branch_valid(vector<float> pnt1, vector<float> pnt2) {
vector<vector<float>> pnts = linspace(pnt1, pnt2, resolution);
for (auto pnt : pnts) {
if (!is_point_valid(pnt)) {
return false;
}
}
return true;
}
bool is_point_valid(vector<float> pnt) {
vector<vector<float>> obstacles(3, vector<float>(3));
obstacles = { {20., 15., 1.5}, {35., 15., 1.} };
for (auto obst : obstacles) {
vector<float> cur_obst_pos = vector<float>{ obst[0], obst[1] };
float cur_r = obst[2];
if (dist(cur_obst_pos, pnt) < cur_r) {
return false;
}
}
return true;
}
vector<float> step_forward(vector<float> par, vector<float> qrand) {
vector<float> direct_vec = vec_add_sub(qrand, par, "-");
float length = dist(par, qrand);
direct_vec = vec_time(direct_vec, 1 / length);
direct_vec = vec_time(direct_vec, min(dist(qrand, par), step_len));
return vec_add_sub(direct_vec, par, "+");
}
// calc dist
static float dist(vector<float> pnt1, vector<float> pnt2) {
float ans = sqrt((pnt1[0] - pnt2[0]) * (pnt1[0] - pnt2[0]) + (pnt1[1] - pnt2[1]) * (pnt1[1] - pnt2[1]));
return ans;
}
// rand point generate
vector<float> gen_rand_pnt(vector<float> x_range, vector<float> y_range) {
mt19937 gen(random_device{}());
uniform_real_distribution<float> x_dist(x_range[0], x_range[1]);
uniform_real_distribution<float> y_dist(y_range[0], y_range[1]);
// 生成随机的x和y坐标
float x = x_dist(gen);
float y = y_dist(gen);
return vector<float> {x, y};
}
Node* select_best_parent(Node* qrand) {
float min_dist = 1e7;
int best_id = -1;
for (int i = 0; i < node_lst.size(); i++) {
Node* ndd = node_lst[i];
vector<float> cur_pos = ndd->pos;
if (dist(cur_pos, qrand->pos) < min_dist) {
min_dist = dist(cur_pos, qrand->pos);
best_id = i;
}
}
return node_lst[best_id];
}
void print_ans() {
vector<vector<float>> pth = extract_path();
for (auto wp : pth) {
cout << "[" << wp[0] << ", " << wp[1] << "]" << endl;
}
}
vector<vector<float>> extract_path() {
Node* goal_nears_par = node_lst[0];
float goal_nears_dist = 1e7;
for (int i = 0; i < node_lst.size(); i++) {
Node* cur_nd = node_lst[i];
if (dist(cur_nd->pos, end) < goal_nears_dist) {
goal_nears_dist = dist(cur_nd->pos, end);
goal_nears_par = cur_nd;
break;
}
}
vector<vector<float>> ans_path;
while (goal_nears_par->parent != NULL) {
ans_path.push_back(goal_nears_par->pos);
goal_nears_par = goal_nears_par->parent;
}
return ans_path;
}
void print_vec(vector<float> v) {
cout << "[" << v[0] << ", " << v[1] << "]" << endl;
}
void plan() {
for (int i = 0; i <= iter_max; i++) {
vector<float> rand_pos = gen_rand_pnt(x_range, y_range);
Node* qrand = new Node(rand_pos);
Node* best_par = select_best_parent(qrand);
vector<float> qnew_pos = step_forward(best_par->pos, qrand->pos);
Node* qnew = new Node(qnew_pos);
if (is_branch_valid(best_par->pos, qnew->pos)) {
cout << "Valid Branch!!" << i << endl;
qnew->parent = best_par;
node_lst.push_back(qnew);
// reached?
if ((dist(qnew->pos, end) <= step_len) && (is_branch_valid(qnew->pos, end))) {
vector<float> goal_qnew = step_forward(qnew->pos, end);
cout << "+++++++++++ Plann done!!!" << endl;
// print_ans();
return;
}
}
}
}
};
int main() {
vector<float> strt = vector<float>{ 2, 10 };
vector<float> ed = vector<float>{ 45, 10 };
vector<float> x_border = vector<float>{ 0., 50. };
vector<float> y_border = vector<float>{ 0., 30. };
float step_length = 1.5;
int max_iter = 500, resolutn = 10;
RRT rrt = RRT(strt, ed, x_border, y_border, step_length, max_iter, resolutn);
rrt.plan();
cout << "Length of the rrt Nodes: " << rrt.node_lst.size() << endl;
rrt.print_ans();
system("pause");
return 0;
}