File: problem_himmelblau.py

package info (click to toggle)
python-cmaes 0.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 544 kB
  • sloc: python: 5,136; sh: 88; makefile: 4
file content (49 lines) | stat: -rw-r--r-- 1,400 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from kurobako import problem
from kurobako.problem import Problem

from typing import List
from typing import Optional


class HimmelblauEvaluator(problem.Evaluator):
    def __init__(self, params: List[Optional[float]]):
        self._x1, self._x2 = params
        self._current_step = 0

    def evaluate(self, next_step: int) -> List[float]:
        self._current_step = 1
        value = (self._x1**2 + self._x2 - 11.0) ** 2 + (
            self._x1 + self._x2**2 - 7.0
        ) ** 2
        return [value]

    def current_step(self) -> int:
        return self._current_step


class HimmelblauProblem(problem.Problem):
    def create_evaluator(
        self, params: List[Optional[float]]
    ) -> Optional[problem.Evaluator]:
        return HimmelblauEvaluator(params)


class HimmelblauProblemFactory(problem.ProblemFactory):
    def create_problem(self, seed: int) -> Problem:
        return HimmelblauProblem()

    def specification(self) -> problem.ProblemSpec:
        params = [
            problem.Var("x1", problem.ContinuousRange(-4, 4)),
            problem.Var("x2", problem.ContinuousRange(-4, 4)),
        ]
        return problem.ProblemSpec(
            name="Himmelblau Function",
            params=params,
            values=[problem.Var("Himmelblau")],
        )


if __name__ == "__main__":
    runner = problem.ProblemRunner(HimmelblauProblemFactory())
    runner.run()