动态加权 A*
动态加权 A*
前置知识
- 加权 A*
- 贪心思想
权重的改良
加权 A* 是一种玄学算法,需在搜索速度与正确率之间做权衡,
加权 A* 的估价函数是
但对于不断调参会消耗大量时间进行数据测试,于是便引入动态加权 A*,
动态加权 A* 的估价函数是
设计动态权重
-
稳定型
稳定型的贪心策略是随着搜索的深入,逐步降低权重
w(n) ,逐渐过渡到标准 A*。因为这样能够保证在搜索前期具有高效的搜索速度,并且最后得到的结果仍具有最优性的特点。
用数学表示,动态权重公式为
w(d) = \max(w_{init} - k \cdot d,\ w_{\text{min}}) ,其中:
当
d \geq \frac{w_{init} - w_{\text{min}}}{k} 时,退化为标准 A*。 -
double w(int d,double w_init=1.5,double k=0.02,double w_min=1.0){
return max(w_init-k*static_cast<double>(d),w_min);
}
-
自适应平衡型
这里需要引入一个概念:混乱度。
混乱度就是当前状态的启发式值与理论最大启发式值的比值,也就是
\frac{h(n)}{h_{\text{max}}} 。如果混乱度值越大,就意味着当前搜索状态距离目标状态越远,所以需要加大权重
w ,使其更倾向于贪心搜索。同时需要考虑搜索深度,这里搜索深度作为一个平衡因素。深度越大,减小权重
w ,这样能抑制无效搜索。
double w(int h,int d){
double c=min(h,h_max)/h_max;
double p=min(d/100.0,0.3);
return clamp(1.2+0.6*c-0.3*p, 1.1,1.8);
}
例题
仍以八数码为例,再原加权 A* 的基础上加上稳定型的函数。
设
#include <bits/stdc++.h>
#define int long long
using namespace std;
int read(){
int x=0,f=1;
char ch=getchar_unlocked();
while(!isdigit(ch)){
if(ch=='-') f=-1;
ch=getchar_unlocked();
}
while(isdigit(ch)){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar_unlocked();
}
return x*f;
}
void write(int x){
if(x<0){
putchar('-');
x=-x;
}
if(x>9) write(x/10);
putchar(x%10+'0');
}
const string target="123804765";
//权重调参,用最优性换取搜索速度
const double w_init=1.2,k=0.02,w_min=1.0;
//方向:上、下、左、右
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};
//启发式函数(曼哈顿距离)
int manhattan(const string&state){
int dist=0;
for(int i=0;i<9;i++){
if(state[i]=='0') continue;
int pos=target.find(state[i]);//寻找当前状态在目标状态的位置
int current_row=i/3,current_col=i%3;
int target_row=pos/3,target_col=pos%3;
dist+=abs(current_row-target_row)+abs(current_col-target_col);//曼哈顿距离公式
}
return dist;
}
struct node{
int g,h,zero_pos;
double f;//总估价=g+weight*h
string state;
//小根堆比较函数
bool operator>(const node&other) const{
return f>other.f;
}
};
double dy_weight(int d){
return max(w_init-k*d,w_min);
}
//加权A*
int weight_Astar(const string&init){
priority_queue<node,vector<node>,greater<node>>dq;
//记录每个状态g的最小值,用于剪枝
unordered_map<string,int>vis;
int init_h=manhattan(init),d=0;
double w=dy_weight(d);
dq.push({0,init_h,init.find('0'),0+w*init_h,init});
vis[init]=0;
while(!dq.empty()){
node current=dq.top();
dq.pop();
d++;
//可行性剪枝:当前状态与目标状态相符时,剪枝
if(current.state==target){
return current.g;
}
//最优性剪枝:不是状态最优,剪枝
if(current.g>vis[current.state]){
continue;
}
w=dy_weight(d);
//向四个方向进行扩展
int zero_row=current.zero_pos/3,zero_col=current.zero_pos%3;
for(int i=0;i<4;i++){
int new_row=zero_row+dx[i],new_col=zero_col+dy[i];
//检查是否合法
if(new_row>=0&&new_row<3&&new_col>=0&&new_col<3){
//建立新的状态
string new_state=current.state;
int new_zero_pos=new_row*3+new_col;
//交换空格位置
swap(new_state[current.zero_pos],new_state[new_zero_pos]);
//计算新的g值
int new_g=current.g+1;
//检查是否有更优路径或未访问状态
if(!vis.count(new_state)||new_g<vis[new_state]){
int new_h=manhattan(new_state);
double new_f=new_g+w*new_h;
//更新加入队列
vis[new_state]=new_g;
dq.push({new_g,new_h,new_zero_pos,new_f,new_state});
}
}
}
}
return -1;
}
signed main(){
string init;
getline(cin,init);
int step=weight_Astar(init);
write(step);
return 0;
}
总结
动态加权 A* 的动态权重设计还有很多类型(我不会),但效率以及最优性稳定型是最好的。
后记
谨以此文,纪念我的童年,那是一段小有遗憾的 OI 时光。