32 #define CSA_ITER_MONO 0 42 template<
typename Scalar_x,
typename Scalar_fx>
49 std::vector<Scalar_x>
x;
70 State(
int n,
const Scalar_x* x0, Scalar_fx fx0)
72 this->x = std::vector<Scalar_x>(x0, x0 + n);
73 this->best_x = std::vector<Scalar_x>(x0, x0 + n);
75 this->best_cost = fx0;
85 void step(std::vector<Scalar_x> &y, Scalar_fx y_cost)
93 template<
typename Scalar_x,
typename Scalar_fx>
108 std::vector<State<Scalar_x, Scalar_fx>>
states;
111 return this->states[i];
115 return this->states[i];
134 template<
typename Scalar_x,
typename Scalar_fx>
205 Scalar_fx (*fx)(
void*, Scalar_x*),
206 void (*step)(
void*, Scalar_x* y,
const Scalar_x*,
float tgen),
207 void (*progress)(
void*,
215 Scalar_fx fx0 = fx(instance, x);
221 float tmp, sum_a, prob_var, gamma =
m;
224 omp_init_lock(&lock);
226 #pragma omp parallel shared(n, shared_states, tacc, tgen, gamma) num_threads(this->m) default(none) 228 int k, opt_id = omp_get_thread_num();
230 Scalar_fx max_cost = shared_states[0].cost;
232 std::vector<Scalar_x> y(n, Scalar_x(0));
235 #if CSA_ITER_MONO == 0 238 #pragma omp for schedule(monotonic:static) 242 step(instance, y.data(), shared_states[opt_id].x.data(), tgen);
243 cost = fx(instance, y.data());
246 if (cost < shared_states[opt_id].cost) {
249 if (cost < shared_states[opt_id].best_cost) {
250 shared_states[opt_id].best_cost = cost;
251 shared_states[opt_id].best_x = y;
252 if (progress !=
nullptr)
253 progress(instance, cost, tgen, tacc, opt_id, iter);
257 shared_states[opt_id].step(y, cost);
258 omp_unset_lock(&lock);
262 prob = std::exp((shared_states[opt_id].cost - max_cost) / tacc) / gamma;
265 shared_states[opt_id].step(y, cost);
266 omp_unset_lock(&lock);
271 if (omp_test_lock(&lock)) {
274 max_cost = shared_states[0].cost;
275 for (k = 0; k < this->
m; ++k)
276 if (shared_states[k].cost > max_cost)
277 max_cost = shared_states[k].cost;
281 for (k = 0; k < this->
m; ++k) {
282 tmp = (shared_states[k].cost - max_cost) / tacc;
283 gamma += std::exp(tmp);
284 sum_a += std::exp(2.0 * tmp);
286 prob_var = (this->m * (sum_a / (gamma * gamma)) - 1.) /
290 if (prob_var > this->desired_variance)
291 tacc += this->tacc_schedule * tacc;
293 tacc -= this->tacc_schedule * tacc;
296 tgen = this->tgen_schedule * tgen;
299 omp_unset_lock(&lock);
306 Scalar_fx best_cost = shared_states[0].best_cost;
307 for (
int k = 0; k < this->
m; ++k) {
308 if (shared_states[k].best_cost < best_cost) {
309 best_cost = shared_states[k].best_cost;
314 for (
int i = 0; i < n; ++i)
315 x[i] = best_state.
best_x[i];
318 omp_destroy_lock(&lock);
float tgen_initial
Definition: csa.hpp:149
const int m
Definition: csa.hpp:100
std::vector< Scalar_x > x
Definition: csa.hpp:49
State(int n, const Scalar_x *x0, Scalar_fx fx0)
Definition: csa.hpp:70
std::vector< Scalar_x > best_x
Definition: csa.hpp:53
Scalar_fx best_cost
Definition: csa.hpp:61
float tgen_schedule
Definition: csa.hpp:153
Scalar_fx cost
Definition: csa.hpp:57
int minimize(int n, Scalar_x *x, Scalar_fx(*fx)(void *, Scalar_x *), void(*step)(void *, Scalar_x *y, const Scalar_x *, float tgen), void(*progress)(void *, Scalar_fx cost, float tgen, float tacc, int opt_id, int iter), void *instance)
Definition: csa.hpp:202
Solver()
Definition: csa.hpp:171
float desired_variance
Definition: csa.hpp:166
int m
Definition: csa.hpp:141
float tacc_schedule
Definition: csa.hpp:162
int max_iterations
Definition: csa.hpp:145
float tacc_initial
Definition: csa.hpp:157
void step(std::vector< Scalar_x > &y, Scalar_fx y_cost)
Definition: csa.hpp:85
std::vector< State< Scalar_x, Scalar_fx > > states
Definition: csa.hpp:108
SharedStates(int m, int n, const Scalar_x *x0, Scalar_fx fx0)
Definition: csa.hpp:127
const int n
Definition: csa.hpp:104