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_is_incoherent(optimizer: str, num_workers: int, expected: bool) -> None:
func = ArtificialFunction(name="sphere", block_dimension=2)
xp = xpbase.Experiment(func, optimizer=optimizer, budget=300, num_workers=num_workers)
np.testing.assert_equal(xp.is_incoherent, expected)
def test_equality() -> None:
func = ArtificialFunction(name="sphere", block_dimension=2)
xp1 = xpbase.Experiment(func, optimizer="OnePlusOne", budget=300, num_workers=2)
xp2 = xpbase.Experiment(func, optimizer="RandomSearch", budget=300, num_workers=2)
assert xp1 != xp2
def spsa_benchmark(seed: Optional[int] = None) -> Iterator[Experiment]:
"""All optimizers on ill cond problems. This benchmark is based on the noise benchmark.
"""
seedg = create_seed_generator(seed)
optims = sorted(x for x, y in ng.optimizers.registry.items() if (any(e in x for e in "TBPSA SPSA".split()) and "iscr" not in x))
for budget in [500, 1000, 2000, 4000, 8000, 16000, 32000, 64000, 128000]:
for optim in optims:
for rotation in [True, False]:
for name in ["sphere", "sphere4", "cigar"]:
function = ArtificialFunction(name=name, rotation=rotation, block_dimension=20, noise_level=10)
yield Experiment(function, optim, budget=budget, seed=next(seedg))
def dim10_select_one_feature(seed: Optional[int] = None) -> Iterator[Experiment]: # One and only one variable matters - LHS wins.
# 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 [1] for uv_factor in [10] 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 [8, 10, 12, 14, 16, 18, 20]:
# duplicate -> each Experiment has different randomness
yield Experiment(func.duplicate(), optim, budget=budget, num_workers=1, seed=next(seedg))
def dim10_select_two_features(seed: Optional[int] = None) -> Iterator[Experiment]: # 2 variables matter - Scrambled Hammersley rules.
# 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 [2] for uv_factor in [5] 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 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)):
yield Experiment(mofunc.to_instrumented(), optim, budget=budget, num_workers=1, 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)):
yield Experiment(mofunc.to_instrumented(), optim, budget=budget, num_workers=1, seed=next(seedg))
def oneshot(seed: Optional[int] = None) -> Iterator[Experiment]:
# prepare list of parameters to sweep for independent variables
seedg = create_seed_generator(seed)
names = ["sphere", "rastrigin", "cigar"]
optims = sorted(x for x, y in ng.optimizers.registry.items() if y.one_shot)
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:
for optim in optims:
for budget in [30, 100, 3000]:
# duplicate -> each Experiment has different randomness
yield Experiment(func.duplicate(), optim, budget=budget, num_workers=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))
"""
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]
for d in ([100, 1000, 3000] if hd else [2, 10, 50])
]
for optim in optims:
for function in functions:
for budget in [50, 200, 800, 3200, 12800] if (not big and not noise) else [40000, 80000]:
xp = Experiment(function.duplicate(), optim, num_workers=100 if parallel else 1,
budget=budget, seed=next(seedg))
if not xp.is_incoherent:
yield xp