def current_step(self) -> int: return self._current_step class RosenbrockProblem(problem.Problem): def create_evaluator( self, params: List[Optional[float]]) -> Optional[problem.Evaluator]: return RosenbrockEvaluator(params) class RosenbrockProblemFactory(problem.ProblemFactory): def create_problem(self, seed: int) -> Problem: return RosenbrockProblem() def specification(self) -> problem.ProblemSpec: params = [ problem.Var("x1", problem.ContinuousRange(-5, 10)), problem.Var("x2", problem.ContinuousRange(-5, 10)), ] return problem.ProblemSpec( name="Rosenbrock Function", params=params, values=[problem.Var("Rosenbrock")], ) if __name__ == "__main__": runner = problem.ProblemRunner(RosenbrockProblemFactory()) runner.run()
class NASLibEvaluator(problem.Evaluator): def __init__(self, learning_curve: List[float]) -> None: self._current_step = 0 self._lc = learning_curve def current_step(self) -> int: return self._current_step def evaluate(self, next_step: int) -> List[float]: self._current_step = next_step return [-self._lc[next_step]] if __name__ == "__main__": if len(sys.argv) < 1 + 2: print( "Usage: python3 nas_bench_suite/problems.py <search_space> <dataset>" ) print( "Example: python3 nas_bench_suite/problems.py nasbench201 cifar10") exit(1) search_space_name = sys.argv[1] # We currently do not support other benchmarks. assert search_space_name == "nasbench201" dataset = sys.argv[2] runner = problem.ProblemRunner(NASLibProblemFactory(dataset)) runner.run()
def current_step(self) -> int: return self._current_step class SixHempCamelProblem(problem.Problem): def create_evaluator( self, params: List[Optional[float]]) -> Optional[problem.Evaluator]: return SixHempCamelEvaluator(params) class SixHempCamelProblemFactory(problem.ProblemFactory): def create_problem(self, seed: int) -> Problem: return SixHempCamelProblem() def specification(self) -> problem.ProblemSpec: params = [ problem.Var("x1", problem.ContinuousRange(-5, 10)), problem.Var("x2", problem.ContinuousRange(-5, 10)), ] return problem.ProblemSpec( name="Six-Hemp Camel Function", params=params, values=[problem.Var("Six-Hemp Camel")], ) if __name__ == "__main__": runner = problem.ProblemRunner(SixHempCamelProblemFactory()) runner.run()
k=self._k) elif self._n_wfg == 8: self.wfg = WFG8(n_arguments=self._n_dim, n_objectives=self._n_obj, k=self._k) elif self._n_wfg == 9: self.wfg = WFG9(n_arguments=self._n_dim, n_objectives=self._n_obj, k=self._k) else: raise AssertionError("Invalid specification for WFG number.") def current_step(self) -> int: return self._current_step def evaluate(self, next_step: int) -> List[float]: self._current_step = 1 v = self.wfg(self._x) v = v.tolist() if math.isnan(v[0]) or math.isinf(v[0]): raise ValueError if math.isnan(v[1]) or math.isinf(v[1]): raise ValueError return [v[0], v[1]] if __name__ == "__main__": runner = problem.ProblemRunner(WFGProblemFactory()) runner.run()
def create_problem(self, seed: int) -> problem.Problem: return QuadraticProblem() class QuadraticProblem(problem.Problem): def create_evaluator(self, params: List[Optional[float]]) -> problem.Evaluator: return QuadraticEvaluator(params) class QuadraticEvaluator(problem.Evaluator): def __init__(self, params: List[Optional[float]]): x, y = params assert x is not None assert y is not None self._x = x self._y = y self._current_step = 0 def current_step(self) -> int: return self._current_step def evaluate(self, next_step: int) -> List[float]: self._current_step = 1 return [self._x**2 + self._y] if __name__ == "__main__": runner = problem.ProblemRunner(QuadraticProblemFactory()) runner.run()
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()
class RastriginProblem(problem.Problem): def create_evaluator( self, params: List[Optional[float]]) -> Optional[problem.Evaluator]: return RastriginEvaluator(params) class RastriginProblemFactory(problem.ProblemFactory): def __init__(self, dim): self.dim = dim def create_problem(self, seed: int) -> Problem: return RastriginProblem() def specification(self) -> problem.ProblemSpec: params = [ problem.Var(f"x{i+1}", problem.ContinuousRange(-5.12, 5.12)) for i in range(self.dim) ] return problem.ProblemSpec( name=f"Rastrigin (dim={self.dim})", params=params, values=[problem.Var("Rastrigin")], ) if __name__ == "__main__": dim = int(sys.argv[1]) if len(sys.argv) == 2 else 2 runner = problem.ProblemRunner(RastriginProblemFactory(dim)) runner.run()