【笔记】随机化算法
模拟退火
Simulated Annealing 基于现实物理规律结论,是一种高效率的随机化算法,能求解最优化问题,要求代价相对连续。骗分使用。
核心是调整法,每次在当前解附近范围随机一个新解,更优则一定接受,否则以一定概率接受。
接受解的概率与温度
SA 的具体流程。温度从
上述的
通常可以设计区间随机函数 random(l, r): rand() / RAND_MAX * (r - l) + l,这样判定是否接受解可以写作 exp(-delta / t) > random(0, 1)。
UVA10228 A Star not a Tree?:
给定平面上
卡时进行 SA。从
:::success[展示代码]
#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] 保龄球:
有
重排一些轮使得合法且得分最大,求最大得分,
温度从
:::success[展示代码]
#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] 均分数据:
给定
贪心地每次把当前数字加入总和最小的组,发现是假的,结果与加入元素的顺序有关,于是可以随机
:::success[随机序列贪心代码]
#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;
}
:::
:::success[退火调整序列代码]
#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;
}
:::