Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_seed_generator(seed: Optional[int], randsize: int, expected: List[Optional[int]]) -> None:
output = []
generator = xpbase.create_seed_generator(seed)
for _ in range(4):
if randsize: # call the standard random generator
np.random.normal(0, 1, size=randsize)
value = next(generator)
output.append(value if value is None else value % 1000)
np.testing.assert_array_equal(output, expected)
def multimodal(seed: Optional[int] = None) -> Iterator[Experiment]:
# prepare list of parameters to sweep for independent variables
seedg = create_seed_generator(seed)
names = ["hm", "rastrigin", "griewank", "rosenbrock", "ackley", "lunacek", "deceptivemultimodal"]
# Keep in mind that Rosenbrock is multimodal in high dimension http://ieeexplore.ieee.org/document/6792472/.
optims = ["NaiveTBPSA", "TBPSA",
"CMA", "PSO", "DE", "MiniDE", "QrDE", "MiniQrDE", "LhsDE", "OnePlusOne", "SQP", "Cobyla", "Powell",
"TwoPointsDE", "OnePointDE", "AlmostRotationInvariantDE", "RotationInvariantDE",
"Portfolio", "ASCMADEthird", "ASCMADEQRthird", "ASCMA2PDEthird", "CMandAS2", "CMandAS", "CM",
"MultiCMA", "TripleCMA", "MultiScaleCMA", "RSQP", "RCobyla", "RPowell", "SQPCMA"] + list(
sorted(x for x, y in ng.optimizers.registry.items() if "chain" in x or "BO" in x))
functions = [
ArtificialFunction(name, block_dimension=bd, useless_variables=bd * uv_factor)
for name in names
for bd in [3, 25]
for uv_factor in [0, 5]
]
# functions are not initialized and duplicated at yield time, they will be initialized in the experiment
for func in functions:
def oneshot1(seed: Optional[int] = None) -> Iterator[Experiment]:
"""Comparing one-shot optimizers as initializers for Bayesian Optimization.
"""
seedg = create_seed_generator(seed)
for budget in [25, 31, 37, 43, 50, 60]: # , 4000, 8000, 16000, 32000]:
for optim in sorted(x for x, y in optimization.registry.items() if "BO" in x):
for rotation in [False]:
for d in [20]:
for name in ["sphere", "cigar", "hm", "ellipsoid"]: # , "hm"]:
for u in [0]:
function = ArtificialFunction(name=name, rotation=rotation, block_dimension=d,
useless_variables=d*u, translation_factor=1.)
yield Experiment(function, optim, budget=budget, seed=next(seedg))
def multiobjective_example(seed: Optional[int] = None) -> Iterator[Experiment]:
# prepare list of parameters to sweep for independent variables
seedg = create_seed_generator(seed)
optims = ["NaiveTBPSA", "PSO", "DE", "SQP", "LhsDE", "RandomSearch", "NGO", "CMA", "BO", "LBO", "SQP", "RSQP"]
mofuncs: List[PackedFunctions] = []
for name1 in ["sphere", "cigar"]:
for name2 in ["sphere", "cigar", "hm"]:
mofuncs += [PackedFunctions([ArtificialFunction(name1, block_dimension=7),
ArtificialFunction(name2, block_dimension=7)],
upper_bounds=np.array((50., 50.)))]
for name3 in ["sphere", "ellipsoid"]:
mofuncs += [PackedFunctions([ArtificialFunction(name1, block_dimension=6),
ArtificialFunction(name3, block_dimension=6),
ArtificialFunction(name2, block_dimension=6)],
upper_bounds=np.array((100, 100, 1000.)))]
# functions are not initialized and duplicated at yield time, they will be initialized in the experiment (no need to seed here)
for mofunc in mofuncs:
for optim in optims:
for budget in list(range(100, 2901, 400)):
def dim10_smallbudget(seed: Optional[int] = None) -> Iterator[Experiment]:
# prepare list of parameters to sweep for independent variables
seedg = create_seed_generator(seed)
names = ["sphere"]
optims = sorted(x for x, y in optimization.registry.items() if y.one_shot and "arg" not in x and "mal" not in x)
functions = [ArtificialFunction(name, block_dimension=bd, num_blocks=n_blocks, useless_variables=bd * uv_factor * n_blocks)
for name in names for bd in [10] for uv_factor in [0] for n_blocks in [1]]
# functions are not initialized and duplicated at yield time, they will be initialized in the experiment (no need to seed here)
for func in functions:
for optim in optims:
for budget in [4, 8, 16, 32]:
# duplicate -> each Experiment has different randomness
yield Experiment(func.duplicate(), optim, budget=budget, num_workers=1, seed=next(seedg))
def metanoise(seed: Optional[int] = None) -> Iterator[Experiment]:
seedg = create_seed_generator(seed)
optims = ["NoisyBandit", "TBPSA", "NaiveTBPSA"]
for budget in [15, 31, 62, 125, 250, 500, 1000, 2000, 4000, 8000]:
for optim in optims:
for noise_dissymmetry in [False, True]:
function = ArtificialFunction(name="sphere", rotation=True, block_dimension=1, noise_level=10,
noise_dissymmetry=noise_dissymmetry, translation_factor=10.)
yield Experiment(function, optim, budget=budget, seed=next(seedg))
def oneshotcec(seed: Optional[int] = None) -> Iterator[Experiment]:
seedg = create_seed_generator(seed)
names = ["sphere", "rastrigin", "cigar"]
optims = sorted(x for x, y in optimization.registry.items() if y.one_shot and
not any(z in x for z in ["Large", "Small", "Stupid", "Zero"]))
optims.append("CustomOptimizer")
functions = [ArtificialFunction(name, block_dimension=bd, useless_variables=bd * uv_factor)
for name in names for bd in [3, 25] for uv_factor in [0, 5]]
for func in functions:
for optim in optims:
for budget in [30, 100, 300, 1000, 3000]:
yield Experiment(func.duplicate(), optim, budget=budget, num_workers=budget, seed=next(seedg))
def yabbob(seed: Optional[int] = None, parallel: bool = False, big: bool = False, noise: bool = False, hd: bool = False) -> Iterator[Experiment]:
"""Yet Another Black-Box Optimization Benchmark.
"""
seedg = create_seed_generator(seed)
optims = ["NaiveTBPSA", "TBPSA", "NGO", "CMA", "PSO", "DE", "MiniDE", "QrDE", "MiniQrDE", "LhsDE", "OnePlusOne",
"TwoPointsDE", "OnePointDE", "AlmostRotationInvariantDE", "RotationInvariantDE"]
if not parallel:
optims += ["SQP", "Cobyla", "Powell", "chainCMASQP"]
#optims += [x for x, y in ng.optimizers.registry.items() if "chain" in x]
names = ["hm", "rastrigin", "griewank", "rosenbrock", "ackley", "lunacek", "deceptivemultimodal", "bucherastrigin", "multipeak"]
names += ["sphere", "doublelinearslope", "stepdoublelinearslope"]
names += ["cigar", "altcigar", "ellipsoid", "altellipsoid", "stepellipsoid", "discus", "bentcigar"]
names += ["deceptiveillcond", "deceptivemultimodal", "deceptivepath"]
# Deceptive path is related to the sharp ridge function; there is a long path to the optimum.
# Deceptive illcond is related to the difference of powers function; the conditioning varies as we get closer to the optimum.
# Deceptive multimodal is related to the Weierstrass function and to the Schaffers function.
functions = [
ArtificialFunction(name, block_dimension=d, rotation=rotation, noise_level=100 if noise else 0) for name in names
for rotation in [True, False]
for num_blocks in [1]
def illcondicec(seed: Optional[int] = None) -> Iterator[Experiment]:
"""All optimizers on ill cond problems
"""
seedg = create_seed_generator(seed)
optims = ["CMA", "PSO", "DE", "MiniDE", "QrDE", "MiniQrDE", "LhsDE", "OnePlusOne", "SQP",
"Cobyla", "Powell", "RPowell", "RCobyla", "RSQP", "ParaSQPCMA"]
optims.append("CustomOptimizer")
functions = [ArtificialFunction(name, block_dimension=50, rotation=rotation)
for name in ["cigar", "ellipsoid"] for rotation in [True, False]]
for optim in optims:
for function in functions:
for budget in [400, 4000, 40000]:
yield Experiment(function.duplicate(), optim,
budget=budget, num_workers=1, seed=next(seedg))
def powersystemsbig(seed: Optional[int] = None) -> Iterator[Experiment]:
funcs: List[InstrumentedFunction] = []
funcs += [PowerSystem(3)]
funcs += [PowerSystem(num_dams=3, depth=5, width=5)]
funcs += [PowerSystem(num_dams=3, depth=9, width=9)]
funcs += [PowerSystem(5)]
funcs += [PowerSystem(num_dams=5, depth=5, width=5)]
funcs += [PowerSystem(num_dams=5, depth=9, width=9)]
funcs += [PowerSystem(9)]
funcs += [PowerSystem(num_dams=9, width=5, depth=5)]
funcs += [PowerSystem(num_dams=9, width=9, depth=9)]
funcs += [PowerSystem(13)]
funcs += [PowerSystem(num_dams=13, width=5, depth=5)]
funcs += [PowerSystem(num_dams=13, width=9, depth=9)]
seedg = create_seed_generator(seed)
algos = ["NaiveTBPSA", "SQP", "Powell", "LargeScrHammersleySearch", "ScrHammersleySearch", "NGO", "PSO", "OnePlusOne",
"CMA", "TwoPointsDE", "QrDE", "LhsDE", "Zero", "StupidRandom", "RandomSearch", "HaltonSearch",
"RandomScaleRandomSearch", "MiniDE", "SplitOptimizer5", "SplitOptimizer9", "SplitOptimizer", "SplitOptimizer3", "SplitOptimizer13"]
for budget in [25600, 51200, 102400]:
for num_workers in [1]:
if num_workers < budget:
for algo in algos:
for fu in funcs:
xp = Experiment(fu, algo, budget, num_workers=num_workers, seed=next(seedg))
if not xp.is_incoherent:
yield xp