跳到正文

算法·理论

【笔记】随机化算法

Believe_in_dreams约 11 分钟
原文
文章信息与版本

文章信息

发布
2026年8月15日
收录
2026年9月10日
更新
2026年11月13日
篇幅
约 11 分钟

版本

  1. v1当前

模拟退火

Simulated Annealing 基于现实物理规律结论,是一种高效率的随机化算法,能求解最优化问题,要求代价相对连续。骗分使用。

核心是调整法,每次在当前解附近范围随机一个新解,更优则一定接受,否则以一定概率接受。

接受解的概率与温度 tt 有关,具体来说,代价从 xx 变成 yy 时,计算 Δ=y−x\Delta = y - x,那么接受的概率就是 e−Δte^{-\frac{\Delta}{t}}。容易发现,代价变小时 Δ<0\Delta < 0,概率 >1> 1 一定接受。在部分问题中,随机的范围也与温度有关。

SA 的具体流程。温度从 t0t_0 开始,不断 ×α\times \alpha,直到 <te<t_e。每次随机调整,计算代价变化量 Δ\Delta,以 e−Δte^{-\frac{\Delta}{t}} 的概率接受。解决问题时调用若干次 SA 函数。

上述的 t0,α,tet_0, \alpha, t_e 是参数,分别表示 初始温度、温度变化系数、终止温度,需要根据不同题目分别设计,可以在大规模数据上测试进行调整。

通常可以设计区间随机函数 random(l, r): rand() / RAND_MAX * (r - l) + l,这样判定是否接受解可以写作 exp(-delta / t) > random(0, 1)。

UVA10228 A Star not a Tree?:

给定平面上 nn 个点,找一个点到这些点的距离和最小,输出最小距离和,n≤100, xi,yi≤104n\le 100,~x_i,y_i\le 10^4。

卡时进行 SA。从 [0,104][0, 10^4] 随机两点 x,yx, y 构成初始点的位置 (x,y)(x, y)。温度从 10410^4 下降至 10−410^{-4},选取新点 np(x+Δ,y+Δ)np(x + \Delta, y + \Delta),其中 Δ\Delta 取 [−t,t][-t, t] 的随机值。

展示代码
C++
#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define PII pair<double, double>
constexpr int N = 105;
int n;
PII a[N];
double ans = 1e9;
double rand(int l, int r) {
    return 1.0 * rand() / RAND_MAX * (r - l) + l;
}
double dis(PII a, PII b) {
    double s1 = a.x - b.x, s2 = a.y - b.y;
    return sqrt(s1 * s1 + s2 * s2);
}
double getall(PII p) {
    double ret = 0;
    for (int i = 1; i <= n; ++i) ret += dis(p, a[i]);
    ans = min(ans, ret);
    return ret;
}
void SA() {
    PII p = {rand(1, 10000), rand(1, 10000)};
    for (double t = 1e4; t >= 1e-4; t *= 0.99) {
        PII np = {p.x + rand(-t, t), p.y + rand(-t, t)};
        double del = getall(np) - getall(p);
        if (exp(-1.0 * del / t) > rand(0, 1)) p = np;
    }
}
int main() {
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i].x >> a[i].y;
    while (1.0 * clock() / CLOCKS_PER_SEC < 0.9) SA();
    cout << fixed << setprecision(0) << ans;
    return 0;
}

P4044 [AHOI2014/JSOI2014] 保龄球:

有 nn 轮游戏,每轮 1010 个球,击打两次,得分为总击倒球的数量。首次全部击倒称为全中,奖励下一轮击倒球的数量,如果第 nn 轮全中则奖励一轮。两次合起来击倒全部称为补中,奖励下一轮第一次击倒球的数量。

重排一些轮使得合法且得分最大,求最大得分,n≤50n\le 50。

温度从 10410^4 到 10−410^{-4} 每次 ×0.99\times 0.99,每次随机交换两轮,以 eΔte^{\frac\Delta t} 的概率接受新解。卡时调用 SA。

展示代码
C++
#include<bits/stdc++.h>
using namespace std;
constexpr int N = 55;
int n, a[N], b[N], m, ans;
int calc() {
    int ret = 0;
    for (int i = 0; i < m; ++i) {
        ret += a[i] + b[i];
        if (i < n) {
            if (a[i] == 10) ret += a[i + 1] + b[i + 1];
            else if (a[i] + b[i] == 10) ret += a[i + 1];
        }
    }
    ans = max(ans, ret);
    return ret;
}
void SA() {
    for (double t = 1e4; t >= 1e-4; t *= 0.99) {
        int x = rand() % m, y = rand() % m, bef = calc();
        swap(a[x], a[y]), swap(b[x], b[y]);
        if (n + (a[n - 1] == 10) == m) {
          if (exp(1.0 * (calc() - bef) / t) > 1.0 * rand() / RAND_MAX) ;
          else swap(a[x], a[y]), swap(b[x], b[y]);
        } else swap(a[x], a[y]), swap(b[x], b[y]);
    }
}
int main() {
    cin >> n;
    for (int i = 0; i < n; ++i) cin >> a[i] >> b[i];
    if (a[n - 1] == 10) cin >> a[n] >> b[n], m = n + 1;
    else m = n;
    while (1.0 * clock() / CLOCKS_PER_SEC < 0.9) SA();
    cout << ans;
    return 0;
}

P2503 [HAOI2006] 均分数据:

给定 nn 个数字,分成 mm 组使得组间均方差最小,n≤20,m≤6,ai≤50n\le 20, m\le 6, a_i\le 50。

贪心地每次把当前数字加入总和最小的组,发现是假的,结果与加入元素的顺序有关,于是可以随机 5050 个序列贪心,或者用模拟退火调整序列。

随机序列贪心代码
C++
#include<bits/stdc++.h>
using namespace std;
constexpr int N = 55;
double ans = 1e9;
int n, m, a[N], b[N];
struct node {
    int x;
    bool operator<(const node&p) const {
        return x > p.x;
    }
};
priority_queue<node> q;
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    while (1.0 * clock() / CLOCKS_PER_SEC < 0.95) {
        random_shuffle(a + 1, a + n + 1);
        double s = 0;
        for (int i = 1; i <= m; ++i)
            q.push({0});
        for (int i = 1, x; i <= n; ++i)
            x = q.top().x, q.pop(), q.push({x + a[i]});
        for (int i = 1; i <= m; ++i)
            b[i] = q.top().x, q.pop(), s += b[i];
        double ret = 0.0, y;
        for (int i = 1; i <= m; ++i)
            y = b[i] - s / m, ret += y * y;
        ret = sqrt(1.0 * ret / m), ans = min(ans, ret);
    }
    cout << fixed << setprecision(2) << ans;
    return 0;
}
退火调整序列代码
C++
#include<bits/stdc++.h>
using namespace std;
constexpr int N = 55;
struct node {
    int x;
    bool operator<(const node &p) const {
        return x > p.x;
    }
};
priority_queue<node> q;
int n, m, a[N], b[N]; 
double ans = 1e9;
double calc() {
    for (int i = 1; i <= m; ++i) q.push({0});
    for (int i = 1, x; i <= n; ++i) 
        x = q.top().x, q.pop(), q.push({x + a[i]});
    double s = 0.0, ret = 0.0;
    for (int i = 1; i <= m; ++i)
    b[i] = q.top().x, q.pop(), s += b[i];
    s /= m;
    for (int i = 1; i <= m; ++i)
        ret += 1.0 * (b[i] - s) * (b[i] - s);
    ret = sqrt(ret / m), ans = min(ans, ret);
    return ret;
}
void SA() {
    for (double t = 1e4; t >= 1e-4; t *= 0.99) {
        int x = rand() % n + 1, y = rand() % n + 1;
        double la = calc();
        swap(a[x], a[y]);
        if (exp(-(calc() - la) / t) > 1.0 * rand() / RAND_MAX) ;
        else swap(a[x], a[y]);
    }
}
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    while (1.0 * clock() / CLOCKS_PER_SEC < 0.95) SA();
    cout << fixed << setprecision(2) << ans;
    return 0;
}
原文