模拟退火
Simulated Annealing 基于现实物理规律结论,是一种高效率的随机化算法,能求解最优化问题,要求代价相对连续。骗分使用。
核心是调整法,每次在当前解附近范围随机一个新解,更优则一定接受,否则以一定概率接受。
接受解的概率与温度 有关,具体来说,代价从 变成 时,计算 ,那么接受的概率就是 。容易发现,代价变小时 ,概率 一定接受。在部分问题中,随机的范围也与温度有关。
SA 的具体流程。温度从 开始,不断 ,直到 。每次随机调整,计算代价变化量 ,以 的概率接受。解决问题时调用若干次 SA 函数。
上述的 是参数,分别表示 初始温度、温度变化系数、终止温度,需要根据不同题目分别设计,可以在大规模数据上测试进行调整。
通常可以设计区间随机函数 random(l, r): rand() / RAND_MAX * (r - l) + l,这样判定是否接受解可以写作 exp(-delta / t) > random(0, 1)。
给定平面上 个点,找一个点到这些点的距离和最小,输出最小距离和,。
卡时进行 SA。从 随机两点 构成初始点的位置 。温度从 下降至 ,选取新点 ,其中 取 的随机值。
展示代码
#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] 保龄球:
有 轮游戏,每轮 个球,击打两次,得分为总击倒球的数量。首次全部击倒称为全中,奖励下一轮击倒球的数量,如果第 轮全中则奖励一轮。两次合起来击倒全部称为补中,奖励下一轮第一次击倒球的数量。
重排一些轮使得合法且得分最大,求最大得分,。
温度从 到 每次 ,每次随机交换两轮,以 的概率接受新解。卡时调用 SA。
展示代码
#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;
}给定 个数字,分成 组使得组间均方差最小,。
贪心地每次把当前数字加入总和最小的组,发现是假的,结果与加入元素的顺序有关,于是可以随机 个序列贪心,或者用模拟退火调整序列。
随机序列贪心代码
#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;
}退火调整序列代码
#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;
}