Bstar 算法

  Bstart 算法原理: 一般寻路的方向 上、下、左、右

   1、根据开始和结束2个点确定移动的方向

   2、每走一步都需要确定一下方向

     3、如果中途遇到有障碍物,根据当前位置和终点的位置,确定移动方向,变成2条线路

   4、这2 条线交替向终点方向移动

   5、如果中途又碰见障碍物了,继续分2个线,此时四条线交替移动

      6、最先到终点的就是最终的路线;

 

/**************************************************************************/

/下面的是伪代码,而且还没有完成,主要原因是,不知道该用 树 的哪个结构来存储每个路线,希望对这里比较熟悉的大佬们可以 不吝赐教 ; 小白在这里先谢过各位大神啦!!

在搞这块的老铁们,有什么见解 咱一起盘他

/**************************************************************************/

 

  1 // 上下左右
  2 enum Dir {
  3     NONE,
  4     UP,
  5     DOWN;
  6     RIGHT,
  7     LEFT
  8 }
  9 
 10 // 伪代码
 11 struct Point {
 12     int x = 0;
 13     int y = 0;
 14     bool visit = false;
 15     Dir dir = NONE;
 16 };
 17 
 18 
 19 // 确定移动时的 方向(上下左右)
 20 void move_dir(Point cur_point, const Point& end_point) {
 21     auto dis_x =  | cur_point.x - end_point.x | ; //取绝对值
 22     auto dis_y =  | cur_point.y - end_point.y | ; //取绝对值
 23     if (dis_x >= dis_y) {
 24         if (cur_point.x >= end_point.x) {
 25             cur_point.dir = Dir::LEFT;
 26         } else {
 27              cur_point.dir = Dir::RIGHT;
 28         }
 29     } else {
 30         if (cur_point.y >= end_point.y) {
 31             cur_point.dir = Dir::DOWN;
 32         } else {
 33              cur_point.dir = Dir::UP;
 34         }
 35     }
 36     cur_point.visit = true;
 37 
 38 }
 39 
 40 // 是否可走
 41 bool check_can_move(const Point& next_point) {
 42     // 判断障碍物列表中 是否有next_point
 43     if (有) {
 44         return false;
 45     }
 46     return true;
 47 }
 48 
 49 //不可走,重新设置方向,以及分线走
 50 
 51 void split_cur_dir(const Point& next_point) {
 52     auto path_size = paths_.size();
 53     paths_[2 * (path_size - 1) + 1] = next_point;
 54 }
 55 
 56 
 57 void main() {
 58     Point start_point(3, 8);
 59     Point end_point(10, 30);
 60     Point cur_point_ = start_point; // 当前点(坐标)
 61     move_dir(cur_point_, end_point);
 62     vector<Point> paths_; // 路线(当做 二叉堆用)
 63     paths_.push_back(cur_point_);
 64 
 65     Point next_point_;
 66     
 67     while(true) {
 68         auto iter = paths_.end--;
 69         if (iter->x == end_point.x && iter->start_point.y) {
 70             //find path done, break while
 71             break;
 72         } else {
 73             bool change_dir = false;
 74             switch(cur_point_.dir) {
 75                 case Dir::RIGHT: {
 76                     next_point_ = cur_point_;
 77                     next_point_.x = cur_point_.x + 1 78                     change_dir = true;
 79                     break;
 80                 }
 81                 case Dir::LEFT: {
 82                     next_point_ = cur_point_;
 83                     next_point_.x = cur_point_.x - 1 84                     change_dir = true;
 85                     break;
 86                 }
 87                 case Dir::UP: {
 88                     next_point_ = cur_point_;
 89                     next_point_.y = cur_point_.y + 1 90                     change_dir = true;
 91                     break;
 92                 }
 93                 case Dir::DOWN: {
 94                     next_point_ = cur_point_;
 95                     next_point_.y = cur_point_.y - 1 96                     change_dir = true;
 97                     break;
 98                 }
 99                 default:
100                     break;
101             }
102             // 判断是否有障碍物
103             if (check_can_move(next_point_)) {
104                 cur_point_ = next_point_;
105                 if (change_dir) {
106                     move_dir(cur_point_, end_point);
107                 }
108                 paths_.push_back(cur_point_);
109             } else {
110                 next_point_ = cur_point_;
111                 split_cur_dir(next_point_);
112             }
113         }
114     }
115 
116     // 最后的paths_ 就是路线
117     // cout << paths_ << endl;
118 }

 

转载于:https://www.cnblogs.com/0x-1119/p/10603162.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
set.seed(0) n = 50 p = 30 x = matrix(rnorm(n*p),nrow=n) bstar = c(runif(30,0.5,1)) mu = as.numeric(x%*%bstar) par(mar=c(4.5,4.5,0.5,0.5)) hist(bstar,breaks=30,col="gray",main="", xlab="True coefficients") library(MASS) set.seed(1) R = 100 nlam = 60 lam = seq(0,25,length=nlam) fit.ls = matrix(0,R,n) fit.rid = array(0,dim=c(R,nlam,n)) err.ls = numeric(R) err.rid = matrix(0,R,nlam) for (i in 1:R) { cat(c(i,", ")) y = mu + rnorm(n) ynew = mu + rnorm(n) a = lm(y~x+0) bls = coef(a) fit.ls[i,] = x%*%bls err.ls[i] = mean((ynew-fit.ls[i,])^2) aa = lm.ridge(y~x+0,lambda=lam) brid = coef(aa) fit.rid[i,,] = brid%*%t(x) err.rid[i,] = rowMeans(scale(fit.rid[i,,],center=ynew,scale=F)^2) } aveerr.ls = mean(err.ls) aveerr.rid = colMeans(err.rid) bias.ls = sum((colMeans(fit.ls)-mu)^2)/n var.ls = sum(apply(fit.ls,2,var))/n bias.rid = rowSums(scale(apply(fit.rid,2:3,mean),center=mu,scale=F)^2)/n var.rid = rowSums(apply(fit.rid,2:3,var))/n mse.ls = bias.ls + var.ls mse.rid = bias.rid + var.rid prederr.ls = mse.ls + 1 prederr.rid = mse.rid + 1 bias.ls var.ls p/n prederr.ls aveerr.ls cbind(prederr.rid,aveerr.rid) par(mar=c(4.5,4.5,0.5,0.5)) plot(lam,prederr.rid,type="l", xlab="Amount of shrinkage",ylab="Prediction error") abline(h=prederr.ls,lty=2) text(c(1,24),c(1.48,1.48),c("Low","High")) legend("topleft",lty=c(2,1), legend=c("Linear regression","Ridge regression")) par(mar=c(4.5,4.5,0.5,0.5)) plot(lam,mse.rid,type="l",ylim=c(0,max(mse.rid)), xlab=expression(paste(lambda)),ylab="") lines(lam,bias.rid,col="red") lines(lam,var.rid,col="blue") abline(h=mse.ls,lty=2) legend("bottomright",lty=c(2,1,1,1), legend=c("Linear MSE","Ridge MSE","Ridge Bias^2","Ridge Var"), col=c("black","black","red","blue")) 为每句代码加上注释解释
05-23

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值