Esempio n. 1
0
def test_rosenbrock_evaluation():
    simulator = RosenbrockSimulator()

    evaluator = Evaluator(problem = RosenbrockProblem(3), simulator = simulator)
    identifier = evaluator.submit([1, 1, 1])
    assert evaluator.get(identifier)[0] == 0.0

    evaluator = Evaluator(problem = RosenbrockProblem(5), simulator = simulator)
    identifier = evaluator.submit([-1, 1, 1, 1, 1])
    assert evaluator.get(identifier)[0] == 4.0

    evaluator = Evaluator(problem = RosenbrockProblem(4), simulator = simulator)

    identifier1 = evaluator.submit([-1, 1, 1, 1])
    assert evaluator.get(identifier1)[0] == 4.0

    identifier2 = evaluator.submit([-1, 1, 2, 1])
    assert evaluator.get(identifier2)[0] != 4.0
Esempio n. 2
0
def test_random_walk_quadratic():
    problem = QuadraticProblem([2.0, 1.0])

    evaluator = Evaluator(simulator=QuadraticSimulator(), problem=problem)

    algorithm = RandomWalk(problem, seed=1000)

    assert Loop(threshold=1e-2).run(evaluator=evaluator,
                                    algorithm=algorithm) == pytest.approx(
                                        (2.0, 1.0), 1e-1)
Esempio n. 3
0
def test_random_walk():
    evaluator = Evaluator(simulator=QuadraticSimulator(),
                          problem=QuadraticProblem([2.0, 1.0]))

    for seed in (1000, 2000, 3000, 4000):
        algorithm = RandomWalk(evaluator, seed=seed)

        assert Loop(threshold=1e-2).run(evaluator=evaluator,
                                        algorithm=algorithm) == pytest.approx(
                                            (2.0, 1.0), 1e-1)
Esempio n. 4
0
def test_cobyla_quadratic():
    problem = QuadraticProblem([2.0, 1.0], [0.0, 0.0])

    evaluator = Evaluator(simulator=QuadraticSimulator(), problem=problem)

    algorithm = ScipyAlgorithm(problem, method="COBYLA")

    assert Loop(threshold=1e-4).run(evaluator=evaluator,
                                    algorithm=algorithm) == pytest.approx(
                                        (2.0, 1.0), 1e-2)
Esempio n. 5
0
def test_scipy_traffic():
    problem = TrafficProblem()

    evaluator = Evaluator(simulator=TrafficSimulator(), problem=problem)

    algorithm = ScipyAlgorithm(problem, method="Nelder-Mead")

    assert Loop(threshold=5.0).run(evaluator=evaluator,
                                   algorithm=algorithm) == pytest.approx(
                                       (510.0, 412.0), 1.0)
Esempio n. 6
0
def test_nelder_mead_quadratic():
    problem = QuadraticProblem([2.0, 1.0], [0.0, 0.0])

    evaluator = Evaluator(simulator=QuadraticSimulator(), problem=problem)

    algorithm = NelderMead(problem)

    assert Loop(threshold=1e-4).run(evaluator=evaluator,
                                    algorithm=algorithm) == pytest.approx(
                                        (2.0, 1.0), 1e-2)
Esempio n. 7
0
def test_random_walk_traffic():
    problem = TrafficProblem()

    evaluator = Evaluator(simulator=TrafficSimulator(), problem=problem)

    algorithm = RandomWalk(problem, seed=1000)

    assert Loop(threshold=10.0).run(evaluator=evaluator,
                                    algorithm=algorithm) == pytest.approx(
                                        (510.0, 412.0), 10.0)
Esempio n. 8
0
def test_cmaes_quadratic():
    problem = QuadraticProblem([2.0, 1.0], [0.0, 0.0])

    evaluator = Evaluator(simulator=QuadraticSimulator(), problem=problem)

    for seed in (1000, 2000, 3000, 4000):
        algorithm = CMAES(problem, initial_step_size=0.1, seed=seed)

        assert Loop(threshold=1e-4).run(evaluator=evaluator,
                                        algorithm=algorithm) == pytest.approx(
                                            (2.0, 1.0), 1e-2)
Esempio n. 9
0
def test_cmaes_traffic():
    problem = TrafficProblem()

    evaluator = Evaluator(simulator=TrafficSimulator(), problem=problem)

    for seed in (1000, 2000, 3000, 4000):
        algorithm = CMAES(problem, initial_step_size=10.0, seed=seed)

        assert Loop(threshold=1e-2).run(evaluator=evaluator,
                                        algorithm=algorithm) == pytest.approx(
                                            (510.0, 412.0), 1.0)
Esempio n. 10
0
def test_cma_es():
    for seed in (1000, 2000, 3000, 4000):
        evaluator = Evaluator(simulator=CongestionSimulator(),
                              problem=CongestionProblem(0.3, iterations=200))

        algorithm = CMAES(evaluator, initial_step_size=50, seed=seed)

        assert abs(
            np.round(
                Loop(threshold=1e-4).run(evaluator=evaluator,
                                         algorithm=algorithm)) - 230) < 10
Esempio n. 11
0
def __test_sis_single_fidelity():
    evaluator = Evaluator(simulator=SISSimulator(), problem=SISProblem(0.6))

    algorithm = BatchBayesianOptimization(evaluator, batch_size=4)

    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        np.random.seed(0)

        assert Loop(threshold=1e-2).run(evaluator=evaluator,
                                        algorithm=algorithm) == pytest.approx(
                                            0.625, 1e-1)
Esempio n. 12
0
def test_nelder_mead_traffic():
    problem = TrafficProblem()
    problem.use_bounds([[400.0, 600.0]] * 2)

    evaluator = Evaluator(simulator=TrafficSimulator(),
                          problem=TrafficProblem())

    algorithm = NelderMead(problem)

    assert Loop(threshold=1e-2).run(evaluator=evaluator,
                                    algorithm=algorithm) == pytest.approx(
                                        (510.0, 412.0), 1.0)
Esempio n. 13
0
def test_scipy():
    evaluator = Evaluator(
        simulator = QuadraticSimulator(),
        problem = QuadraticProblem([2.0, 1.0], [0.0, 0.0])
    )

    algorithm = ScipyAlgorithm(evaluator, method = "COBYLA")

    assert Loop(threshold = 1e-3).run(
        evaluator = evaluator,
        algorithm = algorithm
    ) == pytest.approx((2.0, 1.0), 1e-3)
Esempio n. 14
0
def __test_bayesian_optimization():
    evaluator = Evaluator(simulator=QuadraticSimulator(),
                          problem=QuadraticProblem([2.0, 1.0]))

    algorithm = BatchBayesianOptimization(evaluator, batch_size=4)

    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        np.random.seed(0)

        assert Loop(threshold=1e-2).run(evaluator=evaluator,
                                        algorithm=algorithm) == pytest.approx(
                                            (2.0, 1.0), 1e-1)
Esempio n. 15
0
def test_spsa():
    evaluator = Evaluator(simulator=QuadraticSimulator(),
                          problem=QuadraticProblem([2.0, 1.0], [0.0, 0.0]))

    for seed in (1000, 2000, 3000, 4000):
        algorithm = SPSA(evaluator,
                         perturbation_factor=2e-2,
                         gradient_factor=0.2,
                         seed=seed)

        assert Loop(threshold=1e-4).run(evaluator=evaluator,
                                        algorithm=algorithm) == pytest.approx(
                                            (2.0, 1.0), 1e-2)
Esempio n. 16
0
def test_spsa_traffic():
    problem = TrafficProblem()

    evaluator = Evaluator(simulator=TrafficSimulator(), problem=problem)

    for seed in (1000, 2000, 3000, 4000):
        algorithm = SPSA(problem,
                         perturbation_factor=10.0,
                         gradient_factor=1.0,
                         seed=seed)

        assert Loop(threshold=1e-2).run(evaluator=evaluator,
                                        algorithm=algorithm) == pytest.approx(
                                            (510.0, 412.0), 1.0)
Esempio n. 17
0
def test_fdsa_quadratic():
    problem = QuadraticProblem([2.0, 1.0], [0.0, 0.0])

    evaluator = Evaluator(simulator=QuadraticSimulator(), problem=problem)

    algorithm = FDSA(
        problem,
        perturbation_factor=2e-2,
        gradient_factor=0.2,
    )

    assert Loop(threshold=1e-4).run(evaluator=evaluator,
                                    algorithm=algorithm) == pytest.approx(
                                        (2.0, 1.0), 1e-2)
Esempio n. 18
0
def test_fdsa_traffic():
    problem = TrafficProblem()

    evaluator = Evaluator(simulator=TrafficSimulator(), problem=problem)

    algorithm = FDSA(
        problem,
        perturbation_factor=10.0,
        gradient_factor=1.0,
    )

    assert Loop(threshold=1e-2).run(evaluator=evaluator,
                                    algorithm=algorithm) == pytest.approx(
                                        (510.0, 412.0), 1.0)
Esempio n. 19
0
def test_nelder_mead_congestion():
    evaluator = Evaluator(
        simulator = RosenbrockSimulator(),
        problem = RosenbrockProblem(2)
    )

    algorithm = NelderMead(evaluator, bounds = [
        [-6.0, 6.0],
        [-6.0, 6.0]
    ])

    assert Loop(threshold = 1e-6).run(
        evaluator = evaluator,
        algorithm = algorithm
    ) == pytest.approx((1.0, 1.0), 1e-2)
Esempio n. 20
0
def test_opdyts_traffic():
    problem = TrafficProblem(iterations=40)

    evaluator = Evaluator(simulator=TrafficSimulator(), problem=problem)

    for seed in (1000, 2000, 3000, 4000):
        algorithm = Opdyts(problem,
                           candidate_set_size=16,
                           number_of_transitions=10,
                           perturbation_length=50,
                           seed=seed)

        assert Loop(threshold=1e-2).run(evaluator=evaluator,
                                        algorithm=algorithm) == pytest.approx(
                                            (510.0, 412.0), 1.0)
Esempio n. 21
0
def test_opdyts():
    loop = Loop()

    evaluator = Evaluator(simulator=CongestionSimulator(),
                          problem=CongestionProblem(0.3, iterations=10))

    algorithm = Opdyts(evaluator,
                       candidate_set_size=16,
                       number_of_transitions=20,
                       perturbation_length=50,
                       seed=0)

    assert np.round(
        Loop(threshold=1e-3,
             maximum_cost=10000).run(evaluator=evaluator,
                                     algorithm=algorithm)) == 231
Esempio n. 22
0
output_path = "optimization_output.p"

# First, build the project in toy_example with
#  mvn package

simulator = MATSimSimulator(
    working_directory=
    "/home/shoerl/bo/test2",  # Change to an existing empty directory
    class_path="toy_example/target/optimization_toy_example-1.0.0.jar",
    main_class="ch.ethz.matsim.optimization_toy_example.RunToyExample",
    iterations=200,
    java="/home/shoerl/.java/jdk8u222-b10/bin/java")

problem = ToyExampleProblem(0.5)

evaluator = Evaluator(problem=problem, simulator=simulator, parallel=4)

algorithm = CMAES(evaluator=evaluator, initial_step_size=0.1, seed=0)

#algorithm = BatchBayesianOptimization(
#    evaluator = evaluator,
#    batch_size = 4
#)

#algorithm = RandomWalk(
#    evaluator = evaluator
#)

tracker = PickleTracker(output_path)

Loop().run(evaluator=evaluator, algorithm=algorithm, tracker=tracker)
Esempio n. 23
0
File: run.py Progetto: kaghog/octras
simulator = MATSimSimulator(
    working_directory="work",
    class_path="resources/ile_de_france-1.2.0.jar",
    main_class="org.eqasim.ile_de_france.RunSimulation")

analyzer = ParisAnalyzer(threshold=0.05,
                         number_of_bounds=40,
                         minimum_distance=100.0,
                         maximum_distance=40 * 1e3,
                         reference_path=reference_path)

problem = ParisProblem(analyzer,
                       threads=threads_per_simulation,
                       iterations=iterations,
                       config_path=config_path)

evaluator = Evaluator(problem=problem,
                      simulator=simulator,
                      parallel=parallel_simulations)

algorithm = SPSA(evaluator=evaluator,
                 perturbation_factor=0.1,
                 gradient_factor=0.5,
                 perturbation_exponent=0.101,
                 gradient_exponent=0.602,
                 gradient_offset=0)

tracker = PickleTracker(output_path)

Loop().run(evaluator=evaluator, algorithm=algorithm, tracker=tracker)