模擬退火法求橢圓離原點最短距離
阿新 • • 發佈:2018-12-22
有一個橢圓方程 ax^2+by^2+cz^2+dyz+exz+fxy ,其中輸入為,a,b,c,d,e,求結果精確到小數點後8位
百度了一下,學習到了模擬退火法,寫這篇部落格進行記錄
#include <cstdio> #include <algorithm> #include <cstring> #include <cmath> using namespace std; const double eps = 1e-8; //精度 const double r = 0.99; //降溫速度 const int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 }; //向8個方向降溫 const int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 }; double a, b, c, d, e, f; double dis(double x, double y, double z) //求距離 { return sqrt(x * x + y * y + z * z); } double getz(double x, double y) //已知x,y,求z,二元一次方程 { double A = c, B = e * x + d * y,C = a * x * x + b * y * y + f * x * y - 1; double delta = B * B - 4 * A * C; if (delta < 0) return 1e60; double z1 = (-B + sqrt(delta)) / 2 / A,z2 = (-B - sqrt(delta)) / 2 / A; if (z1 * z1 < z2 * z2) return z1; else return z2; } double solve() //模擬退火 { double step = 1; //步長 double x = 0, y = 0, z; while (step > eps) { z = getz(x, y); for (int i = 0; i < 8; ++i) { double nx = x + dx[i] * step, ny = y + dy[i] * step, nz = getz(nx, ny); if (nz > 1e30) continue; if (dis(nx, ny, nz) < dis(x, y, z)) { x = nx; y = ny; z = nz; } } step *= r; } return dis(x, y, z); } int main() { while (scanf("%lf%lf%lf%lf%lf%lf", &a, &b, &c, &d, &e, &f) != EOF) { printf("%.8f\n", solve()); } return 0; }