★Simulated Annealing: Texas trip

Source: Internet
Author: User
DescriptionAfter a day trip with his friend Dick, Harry noticed a strange pattern of tiny holes in the door of hisSUV. The local American Tire store sells fiberglass patching material only in square sheets. What is thesmallest patch that Harry needs to fix his door?Assume that the holes are points on the integer lattice in the plane. Your job is to find the area of thesmallest square that will cover all the holes.InputThe first line of input contains a single integer T expressed in decimal with no leading zeroes, denotingthe number of test cases to follow. The subsequent lines of input describe the test cases.Each test case begins with a single line, containing a single integer n expressed in decimal with noleading zeroes, the number of points to follow; each of the following n lines contains two integers xand y, both expressed in decimal with no leading zeroes, giving the coordinates of one of your points.You are guaranteed that T ≤ 30 and that no data set contains more than 30 points. All points in eachdata set will be no more than 500 units away from (0,0).OutputPrint, on a single line with two decimal places of precision, the area of the smallest square containingall of your points.Sample Input24-1 -11 -11 1-1 1410 110 -1-10 1-10 -1Sample Output4.00242.00

After searching for half a day, I finally found a simulated annealing with better readability, although it seems that it is not the same as the conventional simulated annealing ......
The following sections are taken from Baidu encyclopedia.
The simulated annealing algorithm is a general probability algorithm.
The simulated annealing algorithm can be divided into three parts: solution space, target function, and initial solution.
The basic idea of simulated annealing:
(1) initialization: the initial temperature T0 (sufficiently large), the initial solution state s (the starting point of the algorithm iteration), and the number of iterations of each t value L
(2) For k = 1 ,......, L perform steps (3) to 6th:
(3) generate a new solution s'
(4) Calculate the incremental Delta t' = C (S)-C (s), where C (s) is the evaluation function.
△T'
(5) If △t' <0, the system accepts s' as the new current solution. Otherwise, the probability exp (----) accepts s' as the new current solution.
T
(6) If the termination condition is met, the current solution is output as the optimal solution and the program is ended. (The termination condition is usually set to terminate an algorithm when several new successive solutions are not accepted .)
(7) T gradually decreases, and t-> 0, then go to step 1.

There are several ways to reduce t:
T0
1. t = ------;
LG (1 + T)
T0
2. t = ----;
1 + T
3. t = t0 · A ^ t.
4 .......

Optimization of simulated annealing algorithms:
(1) Increase the heating or re-heating process. When the algorithm process is appropriate, the temperature is appropriately increased to activate the acceptance probability of each state to adjust the current state in the search process, so as to avoid the algorithm lagging behind in the local minimum solution.
(2) added the memory function. To avoid losing the current optimal solution due to the acceptance of execution Probability During the search process, you can add a storage link to remember the state of "best so far.
(3) Add the supplemental search process. That is, after the annealing process ends, the simulated annealing process or local search is performed again with the searched optimal solution as the initial state.
(4) For each current status, multiple search strategies are used to accept the optimal status in the region with probability, while a single comparison of non-standard SA is adopted.
(5) algorithms that combine other search mechanisms, such as genetic algorithms and Chaotic search.
(6) comprehensive application of the above methods.

For this question, the orthodox method does not know, can simulate annealing water.

Question from http://blog.csdn.net/jasonzhu8/article/details/5901337

Enumerate rotation angles and gradually reduce the angle (halving the positive cut value each time) to approach the optimal solution.
For each rotation angle, if we set the direction vector to (x, y), we can use the geometric meaning of point multiplication to project all the directed line segments from the origin point to each point to (x, y) and calculate the vector (Y,-x) perpendicular to (x, y) in a similar way. If the two values are greater, the result can be obtained.

Standard Rotation Transformation:

Rotation Transformations used in the program:


In addition, the module length of the vector is set to 100 to reduce the relative error.
Accode:

# Include <cstdio> # include <cstdlib> # include <algorithm> # include <cstring> # include <string> # include <cmath> const int maxn = 40; const double INF = 1e198, zero = 1e-12; const int DX [] = {1,-1}, Dy [] = {-1, 1}; int X [maxn], Y [maxn], n, T; double calc (double _ x, double _ y) {double min = inf, max =-INF; For (INT I = 0; I <n; ++ I) {double TMP = _ x * X [I] + _ y * Y [I]; If (TMP> MAX) max = TMP; if (TMP <m In) min = TMP;} double ans = max-min; min = inf, max =-INF; For (INT I = 0; I <n; ++ I) {double TMP = _ y * X [I]-_ x * Y [I]; If (TMP> MAX) max = TMP; If (TMP <min) min = TMP;} (max-min> ans )? (ANS = max-min): ans; return ans * ANS/(_ x * _ x + _ y * _ y); // remember to divide it by the module length to prevent the open side, returns the area of the square .} Double SAA () {double SX = 100, Sy = 0, T = 1, ANS = calc (sx, Sy); For (; t> zero; T/= 2) while (1) {bool OK = 0; For (INT I = 0; I <2; ++ I) {double Tx = SX + dx [I] * Sy * t, ty = SY + dy [I] * SX * t, now = calc (TX, Ty), TMP = SQRT (1e4/(TX * Tx + ty * ty )); TX * = TMP, Ty * = TMP; // keep the mode length unchanged. If (now <ans) {ans = now; OK = 1; SX = Tx, Sy = ty;} // only a better solution is acceptable here, because the current decision // does not affect whether the next step can reach the optimal solution.} // Rotate in both clockwise and counterclockwise directions. If (! OK) break;} return ans;} int main () {freopen ("texas_trip.in", "r", stdin); freopen ("texas_trip.out", "W", stdout ); for (scanf ("% d", & T); t; -- t) {scanf ("% d", & N); For (INT I = 0; I <n; ++ I) scanf ("% d", x + I, Y + I); printf ("%. 2lf \ n ", SAA ();} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.