n_objectives = 2 directions = ["minimize"] * n_objectives sampler_name = sys.argv[1] # Sampler. sampler_cls = getattr( optuna.samplers, sampler_name, getattr(optuna.integration, sampler_name, None), ) if sampler_cls is None: raise ValueError("Unknown sampler: {}.".format(sampler_name)) sampler_kwargs = json.loads(sys.argv[2]) sampler = sampler_cls(**sampler_kwargs) return optuna.create_study( directions=directions, sampler=sampler, pruner=optuna.pruners.NopPruner(), ) if __name__ == "__main__": factory = OptunaSolverFactory(create_study) runner = solver.SolverRunner(factory) runner.run()
def create_solver(self, seed: int, problem: problem.ProblemSpec) -> solver.Solver: return RandomSolver(seed, problem) class RandomSolver(solver.Solver): def __init__(self, seed: int, problem: problem.ProblemSpec): self._rng = np.random.RandomState(seed) self._problem = problem def ask(self, idg: solver.TrialIdGenerator) -> solver.NextTrial: params: List[Optional[float]] = [] for p in self._problem.params: if p.distribution == problem.Distribution.UNIFORM: params.append(self._rng.uniform(p.range.low, p.range.high)) else: low = np.log(p.range.low) high = np.log(p.range.high) params.append(float(np.exp(self._rng.uniform(low, high)))) trial_id = idg.generate() next_step = self._problem.last_step return solver.NextTrial(trial_id, params, next_step) def tell(self, trial: solver.EvaluatedTrial): pass if __name__ == "__main__": runner = solver.SolverRunner(RandomSolverFactory()) runner.run()