📄 siman.texi
字号:
@cindex simulated annealing@cindex combinatorial searches@cindex combinatorial optimization@cindex optimization -- combinatorial@cindex energy function@cindex cost functionStochastic search techniques are used when the structure of a space isnot well understood or is not smooth, so that techniques like Newton'smethod (which requires calculating Jacobian derivative matrices) cannotbe used. In particular, these techniques are frequently used to solvecombinatorial optimization problems, such as the traveling salesmanproblem.The goal is to find a point in the space at which a real valued@dfn{energy function} (or @dfn{cost function}) is minimized. Simulatedannealing is a minimization technique which has given good results inavoiding local minima; it is based on the idea of taking a random walkthrough the space at successively lower temperatures, where theprobability of taking a step is given by a Boltzmann distribution.The functions described in this chapter are declared in the header file@file{gsl_siman.h}.@menu* Simulated Annealing algorithm:: * Simulated Annealing functions:: * Examples with Simulated Annealing:: @end menu@node Simulated Annealing algorithm, Simulated Annealing functions, Simulated Annealing, Simulated Annealing@section Simulated Annealing algorithmThe simulated annealing algorithm takes random walks through the problemspace, looking for points with low energies; in these random walks, theprobability of taking a step is determined by the Boltzmann distribution,@tex\beforedisplay$$p = e^{-(E_{i+1} - E_i)/(kT)}$$\afterdisplay@end tex@ifinfo@examplep = e^@{-(E_@{i+1@} - E_i)/(kT)@}@end example@end ifinfo@noindentif @c{$E_{i+1} > E_i$}@math{E_@{i+1@} > E_i}, and @math{p = 1} when @c{$E_{i+1} \le E_i$}@math{E_@{i+1@} <= E_i}.In other words, a step will occur if the new energy is lower. Ifthe new energy is higher, the transition can still occur, and itslikelihood is proportional to the temperature @math{T} and inverselyproportional to the energy difference @c{$E_{i+1} - E_i$}@math{E_@{i+1@} - E_i}.The temperature @math{T} is initially set to a high value, and a randomwalk is carried out at that temperature. Then the temperature islowered very slightly according to a @dfn{cooling schedule}, forexample:@tex$T \rightarrow T/\mu_T$@end tex@ifinfoT -> T/mu_T@end ifinfowhere @math{\mu_T} is slightly greater than 1. @cindex cooling schedule@cindex schedule - coolingThe slight probability of taking a step that gives higher energy is whatallows simulated annealing to frequently get out of local minima.@node Simulated Annealing functions, Examples with Simulated Annealing, Simulated Annealing algorithm, Simulated Annealing@section Simulated Annealing functions@deftypefun void gsl_siman_solve (const gsl_rng * @var{r}, void *@var{x0_p}, gsl_siman_Efunc_t @var{Ef}, gsl_siman_step_t @var{take_step}, gsl_siman_metric_t @var{distance}, gsl_siman_print_t @var{print_position}, gsl_siman_copy_t @var{copyfunc}, gsl_siman_copy_construct_t @var{copy_constructor}, gsl_siman_destroy_t @var{destructor}, size_t @var{element_size}, gsl_siman_params_t @var{params})This function performs a simulated annealing search through a givenspace. The space is specified by providing the functions @var{Ef} and@var{distance}. The simulated annealing steps are generated using therandom number generator @var{r} and the function @var{take_step}.The starting configuration of the system should be given by @var{x0_p}.The routine offers two modes for updating configurations, a fixed-sizemode and a variable-size mode. In the fixed-size mode the configurationis stored as a single block of memory of size @var{element_size}.Copies of this configuration are created, copied and destroyedinternally using the standard library functions @code{malloc},@code{memcpy} and @code{free}. The function pointers @var{copyfunc},@var{copy_constructor} and @var{destructor} should be null pointers infixed-size mode. In the variable-size mode the functions@var{copyfunc}, @var{copy_constructor} and @var{destructor} are used tocreate, copy and destroy configurations internally. The variable@var{element_size} should be zero in the variable-size mode.The @var{params} structure (described below) controls the run byproviding the temperature schedule and other tunable parameters to thealgorithm.On exit the best result achieved during the search is placed in@code{*@var{x0_p}}. If the annealing process has been successful thisshould be a good approximation to the optimal point in the space.If the function pointer @var{print_position} is not null, a debugginglog will be printed to @code{stdout} with the following columns:@examplenumber_of_iterations temperature x x-(*x0_p) Ef(x)@end exampleand the output of the function @var{print_position} itself. If@var{print_position} is null then no information is printed.@end deftypefun@noindentThe simulated annealing routines require several user-specifiedfunctions to define the configuration space and energy function. Theprototypes for these functions are given below.@deftp {Data Type} gsl_siman_Efunc_tThis function type should return the energy of a configuration @var{xp}.@exampledouble (*gsl_siman_Efunc_t) (void *xp)@end example@end deftp@deftp {Data Type} gsl_siman_step_tThis function type should modify the configuration @var{xp} using a random steptaken from the generator @var{r}, up to a maximum distance of@var{step_size}.@examplevoid (*gsl_siman_step_t) (const gsl_rng *r, void *xp, double step_size)@end example@end deftp@deftp {Data Type} gsl_siman_metric_tThis function type should return the distance between two configurations@var{xp} and @var{yp}.@exampledouble (*gsl_siman_metric_t) (void *xp, void *yp)@end example@end deftp@deftp {Data Type} gsl_siman_print_tThis function type should print the contents of the configuration @var{xp}.@examplevoid (*gsl_siman_print_t) (void *xp)@end example@end deftp@deftp {Data Type} gsl_siman_copy_tThis function type should copy the configuration @var{dest} into @var{source}.@examplevoid (*gsl_siman_copy_t) (void *source, void *dest)@end example@end deftp@deftp {Data Type} gsl_siman_copy_construct_tThis function type should create a new copy of the configuration @var{xp}.@examplevoid * (*gsl_siman_copy_construct_t) (void *xp)@end example@end deftp@deftp {Data Type} gsl_siman_destroy_tThis function type should destroy the configuration @var{xp}, freeing itsmemory.@examplevoid (*gsl_siman_destroy_t) (void *xp)@end example@end deftp@deftp {Data Type} gsl_siman_params_tThese are the parameters that control a run of @code{gsl_siman_solve}.This structure contains all the information needed to control thesearch, beyond the energy function, the step function and the initialguess.@table @code@item int n_tries The number of points to try for each step@item int iters_fixed_T The number of iterations at each temperature@item double step_size The maximum step size in the random walk@item double k, t_initial, mu_t, t_minThe parameters of the Boltzmann distribution and cooling schedule@end table@end deftp@node Examples with Simulated Annealing, , Simulated Annealing functions, Simulated Annealing@section ExamplesThe simulated Annealing package is clumsy, and it has to be because itis written in C, for C callers, and tries to be polymorphic at the sametime. But here we provide some examples which can be pasted into yourapplication with little change and should make things easier.@menu* Trivial example:: * Traveling Salesman Problem:: @end menu@node Trivial example, Traveling Salesman Problem, Examples with Simulated Annealing, Examples with Simulated Annealing@subsection Trivial exampleThe first example, in one dimensional cartesian space, sets up an energyfunction which is a damped sine wave; this has many local minima, butonly one global minimum, somewhere between 1.0 and 1.5. The initialguess given is 15.5, which is several local minima away from the globalminimum.@smallexample@verbatiminclude examples/siman.c@end smallexampleHere are a couple of plots that are generated by running@code{siman_test} in the following way:@example./siman_test | grep -v "^#" | xyplot -xyil -y -0.88 -0.83 -d "x...y" | xyps -d > siman-test.eps./siman_test | grep -v "^#" | xyplot -xyil -xl "generation" -yl "energy" -d "x..y" | xyps -d > siman-energy.eps@end example@iftex@sp 1@center @image{siman-test,3.4in} @center @image{siman-energy,3.4in}@quotationExample of a simulated annealing run: at higher temperatures (early inthe plot) you see that the solution can fluctuate, but at lowertemperatures it converges.@end quotation@end iftex@node Traveling Salesman Problem, , Trivial example, Examples with Simulated Annealing@subsection Traveling Salesman Problem@cindex TSP@cindex Traveling Salesman ProblemThe TSP (@dfn{Traveling Salesman Problem}) is the classic combinatorialoptimization problem. I have provided a very simple version of it,based on the coordinates of twelve cities in the southwestern UnitedStates. This should maybe be called the @dfn{Flying Salesman Problem},since I am using the great-circle distance between cities, rather thanthe driving distance. Also: I assume the earth is a sphere, so I don'tuse geoid distances.The @code{gsl_siman_solve()} routine finds a route which is 3490.62Kilometers long; this is confirmed by an exhaustive search of allpossible routes with the same initial city.The full code can be found in @file{siman/siman_tsp.c}, but I includehere some plots generated with in the following way:@example./siman_tsp > tsp.outputgrep -v "^#" tsp.output | xyplot -xyil -d "x................y" -lx "generation" -ly "distance" -lt "TSP -- 12 southwest cities" | xyps -d > 12-cities.epsgrep initial_city_coord tsp.output | awk '@{print $2, $3, $4, $5@}' | xyplot -xyil -lb0 -cs 0.8 -lx "longitude (- means west)" -ly "latitude" -lt "TSP -- initial-order" | xyps -d > initial-route.epsgrep final_city_coord tsp.output | awk '@{print $2, $3, $4, $5@}' | xyplot -xyil -lb0 -cs 0.8 -lx "longitude (- means west)" -ly "latitude" -lt "TSP -- final-order" | xyps -d > final-route.eps@end exampleThis is the output showing the initial order of the cities; longitude isnegative, since it is west and I want the plot to look like a map.@smallexample# initial coordinates of cities (longitude and latitude)###initial_city_coord: -105.95 35.68 Santa Fe###initial_city_coord: -112.07 33.54 Phoenix###initial_city_coord: -106.62 35.12 Albuquerque###initial_city_coord: -103.2 34.41 Clovis###initial_city_coord: -107.87 37.29 Durango###initial_city_coord: -96.77 32.79 Dallas###initial_city_coord: -105.92 35.77 Tesuque###initial_city_coord: -107.84 35.15 Grants###initial_city_coord: -106.28 35.89 Los Alamos###initial_city_coord: -106.76 32.34 Las Cruces###initial_city_coord: -108.58 37.35 Cortez###initial_city_coord: -108.74 35.52 Gallup###initial_city_coord: -105.95 35.68 Santa Fe@end smallexampleThe optimal route turns out to be:@smallexample# final coordinates of cities (longitude and latitude)###final_city_coord: -105.95 35.68 Santa Fe###final_city_coord: -106.28 35.89 Los Alamos###final_city_coord: -106.62 35.12 Albuquerque###final_city_coord: -107.84 35.15 Grants###final_city_coord: -107.87 37.29 Durango###final_city_coord: -108.58 37.35 Cortez###final_city_coord: -108.74 35.52 Gallup###final_city_coord: -112.07 33.54 Phoenix###final_city_coord: -106.76 32.34 Las Cruces###final_city_coord: -96.77 32.79 Dallas###final_city_coord: -103.2 34.41 Clovis###final_city_coord: -105.92 35.77 Tesuque###final_city_coord: -105.95 35.68 Santa Fe@end smallexample@iftex@sp 1@center @image{initial-route,3in} @center @image{final-route,3in}@quotationInitial and final (optimal) route for the 12 southwestern cities FlyingSalesman Problem.@end quotation@end iftex@noindentHere's a plot of the cost function (energy) versus generation (point inthe calculation at which a new temperature is set) for this problem:@iftex@sp 1@center @image{12-cities,3.4in}@quotationExample of a simulated annealing run for the 12 southwestern citiesFlying Salesman Problem.@end quotation@end iftex@comment @node Simulated Annealing References and Further Reading@comment @section References and Further Reading@comment Modern Heuristic Techniques for Combinatorial Problems, Colin R. Reeves@comment (ed.), McGraw-Hill, 1995@comment ISBN 0-07-709239-2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -