-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_arrde_f24.cpp
More file actions
112 lines (94 loc) · 3.08 KB
/
Copy pathtest_arrde_f24.cpp
File metadata and controls
112 lines (94 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "algorithms/arrde.h"
#include "cec/cec2017.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <chrono>
#include <vector>
#include <string>
#include <limits> // for infinity
int main(int argc, char **argv)
{
int func_num = 24;
size_t D = 30;
size_t max_evals = 300000;
int seed = 42;
if (argc > 1)
func_num = std::stoi(argv[1]);
if (argc > 2)
D = std::stoull(argv[2]);
if (argc > 3)
max_evals = std::stoull(argv[3]);
if (argc > 4)
seed = std::stoi(argv[4]);
// CEC-2017 objective
arrde::CEC2017Functions cec(func_num, static_cast<int>(D));
// Batch objective
auto batch_obj = [](const std::vector<std::vector<double>> &X, void *data)
{
auto *c = static_cast<arrde::CEC2017Functions *>(data);
return (*c)(X);
};
std::vector<std::pair<double, double>> bounds(D, {-100.0, 100.0});
/*
// Real ARRDE
arrde::ARRDE opt(
batch_obj,
bounds,
{}, // x0
&cec, // data pointer
nullptr, // callback
max_evals,
seed);
*/
// ---------- progress every `report_every` evaluations ----------
size_t report_every = 10000;
if (argc > 5)
report_every = std::stoull(argv[5]);
size_t next_report = report_every;
double best_ever = std::numeric_limits<double>::infinity();
auto progress_cb = [&](arrde::Result *r) -> bool
{
// keep the true global best seen so far
if (r->fun < best_ever)
best_ever = r->fun;
if (r->nfev >= next_report || r->nfev >= max_evals)
{
std::cout << std::setw(12) << r->nfev
<< " best = " << std::scientific << std::setprecision(6)
<< best_ever << std::endl;
// align to the next multiple of report_every
next_report = ((r->nfev / report_every) + 1) * report_every;
}
return false; // continue
};
// Real ARRDE – pass the callback
arrde::ARRDE opt(
batch_obj,
bounds,
{}, // x0
&cec, // data
progress_cb, // ← here
max_evals,
seed);
auto t0 = std::chrono::steady_clock::now();
arrde::Result res = opt.optimize();
std::cout << std::setw(12) << res.nfev
<< " best = " << std::scientific << std::setprecision(6)
<< res.fun << " (final)" << std::endl;
auto t1 = std::chrono::steady_clock::now();
double sec = std::chrono::duration<double>(t1 - t0).count();
std::cout << "final f = " << res.fun
<< " nfev = " << res.nfev
<< " time = " << sec << " s\n";
// optional: write result file (same style as before)
std::ofstream out("arrde_real_F" + std::to_string(func_num) + ".txt");
out << "final_fopt " << res.fun << "\n";
out << "error " << (res.fun - 100.0 * func_num) << "\n";
out << "nfev " << res.nfev << "\n";
out << "xopt";
for (double v : res.x)
out << " " << v;
out << "\n";
return 0;
}