Skip to content

Commit 1ff8a28

Browse files
committed
fixed f13
1 parent 99806ae commit 1ff8a28

3 files changed

Lines changed: 23 additions & 18 deletions

File tree

include/sampling.hpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ namespace sampling
1717
struct Sampler
1818
{
1919
Sampler(const size_t d) : d(d) {}
20-
[[nodiscard]] virtual Vector operator()() = 0;
2120
size_t d;
22-
21+
2322
virtual void reset(const parameters::Modules &, const size_t)
2423
{
2524
}
26-
25+
2726
virtual Float expected_length()
2827
{
2928
return std::sqrt(static_cast<Float>(d));
3029
}
30+
[[nodiscard]] virtual Vector operator()() = 0;
3131
};
3232

3333
/**
@@ -271,6 +271,9 @@ namespace sampling
271271

272272
[[nodiscard]] inline Vector box_muller(const Vector& u)
273273
{
274+
static Vector u_extra;
275+
static int n_extra_used = 0;
276+
274277
size_t n = u.size();
275278
size_t m = n / 2;
276279

@@ -282,7 +285,14 @@ namespace sampling
282285
}
283286

284287
if (n % 2 != 0)
285-
z(n - 1) = box_muller(u(0), u(n - 1)).first;
288+
{
289+
if (u_extra.size() <= n_extra_used)
290+
{
291+
u_extra = (*sampler)();
292+
n_extra_used = 0;
293+
}
294+
z(n - 1) = box_muller(u(n - 1), u_extra(n_extra_used++)).first;
295+
}
286296

287297
return z;
288298
}

modcma/modularcmaes.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -481,10 +481,13 @@ def evaluate_bbob(
481481
# This speeds up the import, this import is quite slow, so import it lazy here
482482
# pylint: disable=import-outside-toplevel
483483
import ioh
484+
from modcma.c_maes import Settings, parameters, options, constants, utils
485+
from modcma.c_maes import ModularCMAES as cModularCMAES
484486

485487
evals, fopts = np.array([]), np.array([])
486488
if seed:
487489
np.random.seed(seed)
490+
utils.set_seed(seed)
488491
fitness_func = ioh.get_problem(
489492
fid, dimension=dim, instance=instance
490493
)
@@ -516,13 +519,11 @@ def evaluate_bbob(
516519
hs = []
517520

518521
if cpp:
519-
from modcma.c_maes import Settings, parameters, options
520-
from modcma.c_maes import ModularCMAES as cModularCMAES
521522

522523
modules = parameters.Modules()
523524
modules.matrix_adaptation = options.COVARIANCE
524525
modules.ssa = options.StepSizeAdaptation.CSA
525-
modules.restart_strategy = options.RestartStrategy.STOP
526+
modules.restart_strategy = options.RestartStrategy.NONE
526527

527528
settings = Settings(
528529
fitness_func.meta_data.n_variables,
@@ -534,10 +535,9 @@ def evaluate_bbob(
534535
sigma0=2.0,
535536
target=fitness_func.optimum.y + 1e-8,
536537
budget=fitness_func.meta_data.n_variables * 10_000,
537-
cs=0.4149090010980616,
538-
cmu=0.0512430870135861,
539538
)
540539
optimizer = cModularCMAES(settings)
540+
541541
while not optimizer.break_conditions():
542542
optimizer.step(fitness_func)
543543
ps_norm.append(np.linalg.norm(optimizer.p.adaptation.ps))
@@ -549,6 +549,7 @@ def evaluate_bbob(
549549
f_values.append(optimizer.p.pop.f.mean())
550550
dm.append(optimizer.p.adaptation.dm.copy())
551551
hs.append(optimizer.p.adaptation.hs)
552+
# print(optimizer)
552553
title = "modcmacpp"
553554
else:
554555
optimizer = ModularCMAES(fitness_func, dim, x0 = np.zeros(dim), target=target, **kwargs)
@@ -602,7 +603,7 @@ def evaluate_bbob(
602603

603604
for ax in ax0, ax1, ax2, ax3, ax4:
604605
ax.grid()
605-
ax.set_xlim(0, 300)
606+
ax.set_xlim(0, 500)
606607
ax.set_yscale("log", base=10)
607608
plt.show()
608609

src/matrix_adaptation.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,9 @@ namespace matrix_adaptation
117117
}
118118
return false;
119119
}
120-
121-
for (int i = 0; i < d.size(); ++i)
122-
d[i] = std::max(d[i], 1e-10);
123-
124-
d.noalias() = d.cwiseSqrt();
125-
// inv_root_C.noalias() = eigen_solver.operatorInverseSqrt();
126120

127-
Matrix D_inv_sqrt = d.cwiseInverse().asDiagonal();
128-
inv_root_C.noalias() = B * D_inv_sqrt * B.transpose();
121+
d.noalias() = d.cwiseSqrt().eval();
122+
inv_root_C.noalias() = B * d.cwiseInverse().asDiagonal() * B.transpose();
129123
A.noalias() = B * d.asDiagonal();
130124
return true;
131125
}

0 commit comments

Comments
 (0)