def experiment(work_dir, problem_cls=None, solver_cls=None, num_rounds=None, batch_size=None, seed=None): """Run experiment.""" # Must be specified via Gin. assert problem_cls assert solver_cls assert num_rounds assert batch_size tf.gfile.MakeDirs(work_dir) if seed is not None: utils.set_seed(seed) print('Running experiment with %s on %s' % (problem_cls, solver_cls)) problem = problem_cls() solver = solver_cls(problem.domain) population = controller.run(problem, solver, num_rounds=num_rounds, batch_size=batch_size) print('Writing output to %s/population.csv' % work_dir) population.to_csv(os.path.join(work_dir, 'population.csv')) return population
def __init__(self, problem, solver, initial_population=None, callbacks=None, output_dir=None, verbose=False, seed=None): """Creates an instance of this class. Args: problem: An instance of a `BaseProblem`. solver: An instance of a `BaseSolver`. initial_population: An instance of a `Population` that is used to initialize the optimization. callbacks: A list of callables that take a population as input argument and that are called at the end of each round. output_dir: An output directory for storing data. verbose: Whether to log messages to the console. seed: The global seed of random number generators. If `None`, no seed is set explicitly. """ if seed is not None: utils.set_seed(seed) self._callbacks = utils.to_list(callbacks) self._output_dir = output_dir self._problem = problem self._solver = solver self._population = ( data.Population() if initial_population is None else initial_population.copy()) self._verbose = verbose self._log('Initial population size: %d', len(self._population)) if self._population: for callback in self._callbacks: callback(initial_population)