1 #ifndef __RAT_SimulatedAnnealing__
2 #define __RAT_SimulatedAnnealing__
16 virtual double operator()(
double *args) = 0;
26 double simplex[D + 1][D], val[D + 1];
27 double global_best_pt[D], global_best_val;
34 void SetSimplexPoint(
size_t pt, std::vector<double> &point) {
35 for (
size_t j = 0; j < D; j++) {
36 simplex[pt][j] = point[j];
38 val[pt] = (*funk)(simplex[pt]);
41 void Anneal(
double temp0,
size_t nAnneal,
size_t nEval,
double alpha) {
42 global_best_val = DBL_MAX;
43 for (
size_t cycle = 0; cycle < nAnneal; cycle++) {
44 temp = temp0 * pow(1 - (
double)cycle / nAnneal, alpha);
50 void GetBestPoint(std::vector<double> &point) {
51 for (
size_t j = 0; j < D; j++) {
52 point[j] = global_best_pt[j];
57 inline double replace_project(
const double (&offset)[D],
const double (&by)[D],
const double factor,
59 for (
size_t j = 0; j < D; j++) {
60 into[j] = offset[j] + factor * by[j];
62 double val_try = (*funk)(into);
63 if (val_try < global_best_val) {
64 global_best_val = val_try;
65 for (
size_t j = 0; j < D; j++) {
66 global_best_pt[j] = into[j];
72 void amoeba(
int *iter) {
74 size_t i_best, i_worst, i_nextworst;
75 double val_best, val_worst, val_nextworst;
77 const double alpha = 1.0;
78 const double gamma = 2.0;
79 const double rho = -0.5;
80 const double sigma = 0.5;
90 i_best = i_worst = i_nextworst = -1;
91 val_best = DBL_MAX, val_worst = val_nextworst = -DBL_MAX;
92 for (
size_t i = 0; i <= D; i++) {
93 const double val_thermal = val[i] - temp * log(gRandom->Rndm());
95 if (val_thermal < val_best) {
97 val_best = val_thermal;
99 if (val_thermal > val_worst) {
100 i_nextworst = i_worst;
101 val_nextworst = val_worst;
103 val_worst = val_thermal;
104 }
else if (val_thermal > val_nextworst) {
106 val_nextworst = val_thermal;
114 for (
size_t j = 0; j < D; j++) {
116 for (
size_t i = 0; i <= D; i++) {
117 if (i == i_worst)
continue;
118 centroid[j] += simplex[i][j];
121 dworst[j] = centroid[j] - simplex[i_worst][j];
126 val[i_worst] = replace_project(centroid, dworst, alpha, simplex[i_worst]);
127 const double val_ref_therm = val[i_worst] + temp * log(gRandom->Rndm());
130 if (val_ref_therm < val_nextworst && val_ref_therm >= val_best) {
135 }
else if (val_ref_therm < val_best) {
137 const double val_try = replace_project(centroid, dworst, gamma, try_pt);
139 if (val_try + temp * log(gRandom->Rndm()) < val_ref_therm) {
141 for (
size_t j = 0; j < D; j++) {
142 simplex[i_worst][j] = try_pt[j];
144 val[i_worst] = val_try;
152 const double val_con = replace_project(centroid, dworst, rho, simplex[i_worst]);
153 val[i_worst] = val_con;
155 if (val_con + temp * log(gRandom->Rndm()) >= val_worst) {
158 for (
size_t i = 0; i <= D; i++) {
159 if (i == i_best)
continue;
160 for (
size_t j = 0; j < D; j++) {
161 simplex[i][j] = (1.0 - sigma) * simplex[i_best][j] + sigma * simplex[i][j];
163 val[i] = (*funk)(simplex[i]);
Definition: SimulatedAnnealing.hh:14
Definition: SimulatedAnnealing.hh:20
Definition: CCCrossSecMessenger.hh:29