Ejemplo n.º 1
0
    def test_verify_mco_parameters(self):
        optimizer = ScipyOptimizer()
        parameter_factory = self.factory.parameter_factories[0]
        wrong_parameter = parameter_factory.create_model()

        with self.assertRaises(ScipyTypeError):
            optimizer.verify_mco_parameters([wrong_parameter])
Ejemplo n.º 2
0
    def run(self, evaluator):

        model = evaluator.mco_model

        optim = ScipyOptimizer(algorithms=model.algorithms)

        optimizer = WeightedOptimizerEngine(
            kpis=model.kpis,
            parameters=model.parameters,
            num_points=model.num_points,
            space_search_mode=model.space_search_mode,
            single_point_evaluator=evaluator,
            verbose_run=model.verbose_run,
            optimizer=optim,
        )

        for (
                optimal_point,
                optimal_kpis,
                scaled_weights,
        ) in optimizer.optimize():

            # When there is new data, this operation informs the system that
            # new data has been received. It must be a dictionary as given.
            evaluator.mco_model.notify_progress_event(
                [DataValue(value=v) for v in optimal_point],
                [DataValue(value=v) for v in optimal_kpis],
                weights=scaled_weights,
            )
class WeightedMCOModel(BaseMCOModel):

    #: Algorithms available to work with
    algorithms = Enum(
        *ScipyOptimizer.class_traits()["algorithms"].handler.values)

    #: Search grid resolution per KPI
    num_points = PositiveInt(7)

    #: Display the generated points at runtime
    verbose_run = Bool(True)

    #: Space search distribution for weight points sampling
    space_search_mode = Enum("Uniform", "Dirichlet")

    #: 'Subprocess' mode performs evaluation of a state in the workflow via
    #: calling force_bdss on a new subprocess with SubprocessWorkflowEvaluator
    evaluation_mode = Enum("Internal", "Subprocess")

    def default_traits_view(self):
        return View(
            Item("evaluation_mode"),
            Item("algorithms"),
            Item("num_points", label="Weights grid resolution per KPI"),
            Item("space_search_mode"),
            Item("verbose_run"),
        )

    def __start_event_type_default(self):
        return WeightedMCOStartEvent

    def __progress_event_type_default(self):
        return WeightedMCOProgressEvent
Ejemplo n.º 4
0
class TestScipyOptimizer(TestCase):
    def setUp(self):
        self.optimizer = ScipyOptimizer()

        self.plugin = {"id": "pid", "name": "Plugin"}
        self.factory = DummyMCOFactory(self.plugin)

    def test_init(self):
        self.assertEqual("SLSQP", self.optimizer.algorithms)

    def test_verify_mco_parameters(self):
        optimizer = ScipyOptimizer()
        parameter_factory = self.factory.parameter_factories[0]
        wrong_parameter = parameter_factory.create_model()

        with self.assertRaises(ScipyTypeError):
            optimizer.verify_mco_parameters([wrong_parameter])

    def test_optimize_function(self):

        factory = RangedVectorMCOParameterFactory(self.factory)

        params = [
            RangedVectorMCOParameter(factory=factory,
                                     name='coordinates',
                                     dimension=2,
                                     lower_bound=[-2, -2],
                                     upper_bound=[2, 2],
                                     initial_value=[1, 1])
        ]

        # test single-objective (scalar) function
        for point in self.optimizer.optimize_function(
                lambda arg: arg[0][0]**2 + arg[0][1]**2, params):
            x, y = point[0]
            self.assertAlmostEqual(x, 0.0)
            self.assertAlmostEqual(y, 0.0)

        # test multi-objective function (objectives should be summed)
        for point in self.optimizer.optimize_function(
                lambda arg: [arg[0][0]**2, arg[0][1]**2], params):
            x, y = point[0]
            self.assertAlmostEqual(x, 0.0)
            self.assertAlmostEqual(y, 0.0)
Ejemplo n.º 5
0
    def setUp(self):
        self.optimizer = ScipyOptimizer()

        self.plugin = {"id": "pid", "name": "Plugin"}
        self.factory = DummyMCOFactory(self.plugin)
 def _optimizer_default(self):
     return ScipyOptimizer()