()
| 30 | |
| 31 | # pylint: disable=function-redefined |
| 32 | def test_base_example() -> None: |
| 33 | # DOC_BASE_0 |
| 34 | import nevergrad as ng |
| 35 | |
| 36 | def square(x, y=12): |
| 37 | return sum((x - 0.5) ** 2) + abs(y) |
| 38 | |
| 39 | # optimization on x as an array of shape (2,) |
| 40 | optimizer = ng.optimizers.NGOpt(parametrization=2, budget=100) |
| 41 | recommendation = optimizer.minimize(square) # best value |
| 42 | print(recommendation.value) |
| 43 | # >>> [0.49971112 0.5002944 ] |
| 44 | # DOC_BASE_1 |
| 45 | instrum = ng.p.Instrumentation( |
| 46 | ng.p.Array(shape=(2,)).set_bounds(lower=-12, upper=12), |
| 47 | y=ng.p.Scalar() |
| 48 | ) |
| 49 | optimizer = ng.optimizers.NGOpt(parametrization=instrum, budget=100) |
| 50 | recommendation = optimizer.minimize(square) |
| 51 | print(recommendation.value) |
| 52 | # >>> ((array([0.52213095, 0.45030925]),), {'y': -0.0003603100877068604}) |
| 53 | # DOC_BASE_2 |
| 54 | from concurrent import futures |
| 55 | |
| 56 | optimizer = ng.optimizers.NGOpt(parametrization=instrum, budget=10, num_workers=2) |
| 57 | # We use ThreadPoolExecutor for CircleCI but please |
| 58 | # use the line just below, with ProcessPoolExecutor instead (unless your |
| 59 | # code is I/O bound rather than CPU bound): |
| 60 | with futures.ThreadPoolExecutor(max_workers=optimizer.num_workers) as executor: |
| 61 | #with futures.ProcessPoolExecutor(max_workers=optimizer.num_workers) as executor: |
| 62 | recommendation = optimizer.minimize(square, executor=executor, batch_mode=False) |
| 63 | # DOC_BASE_3 |
| 64 | import nevergrad as ng |
| 65 | |
| 66 | def square(x, y=12): |
| 67 | return sum((x - 0.5) ** 2) + abs(y) |
| 68 | |
| 69 | instrum = ng.p.Instrumentation(ng.p.Array(shape=(2,)), y=ng.p.Scalar()) # We are working on R^2 x R. |
| 70 | optimizer = ng.optimizers.NGOpt(parametrization=instrum, budget=100, num_workers=1) |
| 71 | |
| 72 | for _ in range(optimizer.budget): |
| 73 | x = optimizer.ask() |
| 74 | loss = square(*x.args, **x.kwargs) |
| 75 | optimizer.tell(x, loss) |
| 76 | |
| 77 | recommendation = optimizer.provide_recommendation() |
| 78 | print(recommendation.value) |
| 79 | # DOC_BASE_4 |
| 80 | import nevergrad as ng |
| 81 | |
| 82 | def onemax(x): |
| 83 | return len(x) - x.count(1) |
| 84 | |
| 85 | # Discrete, ordered |
| 86 | param = ng.p.TransitionChoice(range(7), repetitions=10) |
| 87 | optimizer = ng.optimizers.DiscreteOnePlusOne(parametrization=param, budget=100, num_workers=1) |
| 88 | |
| 89 | recommendation = optimizer.provide_recommendation() |
nothing calls this directly
no test coverage detected
searching dependent graphs…