Beispiel #1
0
def test_multi_optimisation():
    """Test that the optimiser gives expected results for two objectives."""
    dummy_1 = QuadraticComponent()  # Default values for the first component
    # Second component is customised so that its maximum is at x=3
    dummy_2 = QuadraticComponent()
    dummy_2.parameters['a'] = -1.0  # must be floats as they are not coerced
    dummy_2.parameters['b'] = +6.0
    dummy_2.parameters['c'] = -9.0
    # Make optimiser for the combined component
    dummy = MultiComponent(dummy_1, dummy_2)
    optimiser = opt.Optimiser(dummy)
    optimiser.add_variable(gen=QuadraticGenerator(dummy_1),
                           component=opt.sel.facility(),
                           collection='inputs',
                           item='x1')
    optimiser.add_variable(gen=QuadraticGenerator(dummy_2),
                           component=opt.sel.facility(),
                           collection='inputs',
                           item='x2')
    optimiser.add_objective(component=opt.sel.facility(),
                            item='y1',
                            minimise=True)  # assuming a > 0 in params!
    optimiser.add_objective(component=opt.sel.facility(),
                            item='y2',
                            maximise=True)
    # Sometimes the true solution is not reached in 10 generations:
    optimiser.parameters["maxGenerations"] = 20
    optimiser.run()
    best = optimiser.outputs["bestIndividuals"][0]
    best_sol = [
        best.get_variable('MultiComponent', 'x1', collection='inputs'),
        best.get_variable('MultiComponent', 'x2', collection='inputs')
    ]
    assert_allclose([sol.value for sol in best_sol], dummy.true_optimum())
Beispiel #2
0
def test_run_opt_load_results(default_model):
    """
    This test checks that we can serialise optimiser results to YAML and reload,
    getting the same information after loading.

    It also checks that resetting the seed and re-running gives the same results.
    """
    facility, product, steps = default_model

    # Set up the optimiser
    from biopharma import optimisation as opt

    optimiser = opt.Optimiser(facility)

    # Specify the variables to optimise.
    optimiser.add_variable(gen=opt.gen.Binary(),
                           component=opt.sel.step('test_step'),
                           item='bool_param')
    optimiser.add_variable(gen=opt.gen.RangeGenerator(0, 10),
                           component=opt.sel.step('test_step'),
                           item='int_param')

    # Specify the objective(s)
    optimiser.add_objective(component=opt.sel.product(0),
                            item='cogs',
                            minimise=True)

    # Run optimisation
    optimiser.parameters['populationSize'] = 2
    optimiser.parameters['maxGenerations'] = 1
    optimiser.run()

    original_pop = optimiser.outputs['finalPopulation']
    original_best_fitness = optimiser.outputs['bestObjectiveValues']

    # Save results and re-load
    results = optimiser.save_results()
    optimiser.load_results(results)

    # Check loaded individuals are distinct but equivalent
    for orig, loaded in zip(original_pop,
                            optimiser.outputs['finalPopulation']):
        assert orig is not loaded
        assert orig == loaded
    assert original_best_fitness == optimiser.outputs['bestObjectiveValues']

    # Reset the seed and re-run
    optimiser.set_seed(optimiser.outputs['seed'])
    optimiser.run()

    # Check for equivalent individuals again
    for orig, rerun in zip(original_pop, optimiser.outputs['finalPopulation']):
        assert orig is not rerun
        assert orig == rerun
    # And as an extra sanity check, verify we get the same fitness for the best solution
    assert original_best_fitness == optimiser.outputs['bestObjectiveValues']
Beispiel #3
0
def create_optimiser(component):
    """Create an optimiser instance for the given component (facility or
    sensitivity analyser).

    No setup will be performed, i.e. no variables to optimise or objectives set.
    """
    from biopharma import optimisation as opt

    optimiser = opt.Optimiser(component)
    return optimiser
Beispiel #4
0
def test_optimisation():
    """Test that the optimiser gives expected results for a simple problem."""
    dummy = QuadraticComponent()
    # Pretend that the test component is a facility!
    optimiser = opt.Optimiser(dummy)
    optimiser.add_variable(gen=QuadraticGenerator(dummy),
                           component=opt.sel.facility(),
                           collection='inputs',
                           item='x')
    optimiser.add_objective(component=opt.sel.facility(),
                            item='y',
                            minimise=True)  # assuming a > 0 in params!
    optimiser.run()
    best = optimiser.outputs["bestIndividuals"][0]
    best_sol = best.get_variable('QuadraticComponent',
                                 'x',
                                 collection='inputs')
    assert_allclose(best_sol.value, dummy.true_optimum())
def test_minimise_variance():
    """Check that minimising the variance of an output works as expected."""
    target = 5 * units("dimensionless")
    # Set up the sensitivity analysis, which will not actually vary anything
    analyser = opt.SensitivityAnalyser(Fussy(target, 100))
    analyser.add_output("y", component=opt.sel.facility(), item="y")
    analyser.parameters["numberOfSamples"] = 10
    # Set up the optimiser to minimise the variance of the output
    optimiser = opt.Optimiser(analyser)
    optimiser.add_variable(gen=opt.gen.RangeGenerator(0 * target, 2 * target),
                           component=opt.sel.facility(),
                           item='p')
    optimiser.add_objective(component=opt.sel.self(),
                            item=("y", "var"),
                            minimise=True)
    optimiser.parameters["maxGenerations"] = 5
    # Run combined optimisation + SA, and check we get the expected result
    optimiser.run()
    best = optimiser.outputs["bestIndividuals"][0]
    assert best.get_variable('Fussy', 'p').value == target