def __call__(self, vars, tag=None): wd, state = self.create_workdir( pj( env.runs_dir , "mc" if tag is None else tag ) ) if state is None: state = State(self.seed) state.dump(wd) rng = np.random.RandomState(state.seed) asks, tells, pool = [], [], [] run_ids = range(sum([ len(v[1]) for v in state.vals ]), self.number) dim_size = len(vars) X = np.zeros((len(run_ids), dim_size)) for dim_idx in xrange(dim_size): X[:,dim_idx] = self.min_bound + self.max_bound*lhs_sample(len(run_ids), rng) for x_id, run_id in enumerate(run_ids): x = X[x_id, :] pool.append( (run_id, runner(x, vars, wd, wait=False, id=run_id, min=self.min_bound, max=self.max_bound)) ) asks.append( (run_id, x) ) if len(pool)>=GlobalConfig.Jobs: Algo.wait_pool(pool, tells) state, asks, tells, pool = Algo.dump_state(wd, state, asks, tells, pool) while len(pool)>0: Algo.wait_pool(pool, tells) state, asks, tells, pool = Algo.dump_state(wd, state, asks, tells, pool)
def __call__(self, vars, tag=None): wd, state = self.create_workdir( pj( env.runs_dir , "cma_es" if tag is None else tag ) ) if state is None: state = State(self.seed) state.dump(wd) #start_vals = get_vars( # const = read_json(GlobalConfig.ConstFilename) # , var_specs = read_json(GlobalConfig.VarSpecsFile) # , vars = vars # , min = self.min_bound # , max = self.max_bound #) rng = np.random.RandomState(state.seed) start_vals = self.min_bound + self.max_bound*rng.random_sample(len(vars)) es = cma.CMAEvolutionStrategy( start_vals , self.sigma , { 'bounds' : [ self.min_bound , self.max_bound ], 'popsize' : self.popsize , 'seed' : state.seed } ) for X, tells in state.vals: X = es.ask() es.tell(X, tells) id = sum([ len(v[1]) for v in state.vals ]) while not es.stop(): X = es.ask() asks, tells, pool = [], [], [] tells, pool = [], [] for Xi in X: p = runner(Xi, vars, wd, wait=False, id=id, min=self.min_bound, max=self.max_bound) pool.append( (id, p) ) id+=1 if len(pool)>=GlobalConfig.Jobs: Algo.wait_pool(pool, tells) state, asks, tells, pool = Algo.dump_state(wd, state, asks, tells, pool) while len(pool)>0: self.wait_pool(pool, tells) state, asks, tells, pool = Algo.dump_state(wd, state, asks, tells, pool) tells = [ out for _, out in sorted(tells, key=lambda x: x[0]) ] es.tell(X, tells) es.disp()
def __call__(self, vars, tag=None): wd, state = self.create_workdir( pj(env.runs_dir, "cma_es" if tag is None else tag)) if state is None: state = State(self.seed) state.dump(wd) #start_vals = get_vars( # const = read_json(GlobalConfig.ConstFilename) # , var_specs = read_json(GlobalConfig.VarSpecsFile) # , vars = vars # , min = self.min_bound # , max = self.max_bound #) rng = np.random.RandomState(state.seed) start_vals = self.min_bound + self.max_bound * rng.random_sample( len(vars)) es = cma.CMAEvolutionStrategy( start_vals, self.sigma, { 'bounds': [self.min_bound, self.max_bound], 'popsize': self.popsize, 'seed': state.seed }) for X, tells in state.vals: X = es.ask() es.tell(X, tells) id = sum([len(v[1]) for v in state.vals]) while not es.stop(): X = es.ask() asks, tells, pool = [], [], [] tells, pool = [], [] for Xi in X: p = runner(Xi, vars, wd, wait=False, id=id, min=self.min_bound, max=self.max_bound) pool.append((id, p)) id += 1 if len(pool) >= GlobalConfig.Jobs: Algo.wait_pool(pool, tells) state, asks, tells, pool = Algo.dump_state( wd, state, asks, tells, pool) while len(pool) > 0: self.wait_pool(pool, tells) state, asks, tells, pool = Algo.dump_state(wd, state, asks, tells, pool) tells = [out for _, out in sorted(tells, key=lambda x: x[0])] es.tell(X, tells) es.disp()
def __call__(self, vars, tag=None): wd, state = self.create_workdir( pj( env.runs_dir , "grid_search" if tag is None else tag ) ) if state is None: state = State(0) # doesn't matter state.dump(wd) if self.freeze_point: self.freeze_point = [ float(p) for p in self.freeze_point.split(" ") if p.strip() ] if self.non_freeze_vars is None: raise Exception("Got freeze point but freeze variables are not defined") if self.non_freeze_vars: self.non_freeze_vars = [ v.strip() for v in self.non_freeze_vars.split(" ") if v.strip() ] else: self.non_freeze_vars = vars dim_of_problem = len(self.non_freeze_vars) number_of_dim_slice = (self.max_bound - self.min_bound)/self.step axis_slices = [ np.linspace(self.min_bound, self.max_bound, num=number_of_dim_slice) for _ in xrange(dim_of_problem) ] if len(axis_slices) == 1: points = axis_slices[0].reshape(1, -1).T else: points = np.vstack(np.meshgrid(*axis_slices)).reshape(dim_of_problem, -1).T number_of_steps = len(points) if number_of_steps > GlobalConfig.NumberOfCalcutationsUpperBound: raise Exception("There are a lot of calculations ({}). Reconsider your setup".format(number_of_steps)) nsteps_done = sum([ len(v[1]) for v in state.vals ]) points = points[nsteps_done:] asks, tells, pool = [], [], [] for id, point in enumerate(points): if self.freeze_point: x = list(self.freeze_point) for vi, v in enumerate(self.non_freeze_vars): try: var_idx = vars.index(v) except ValueError: raise Exception("Can't find non freeze var {} in specification".format(v)) x[var_idx] = point[vi] else: x = point pool.append( (id, runner(x, vars, wd, wait=False, id=id, min=self.min_bound, max=self.max_bound)) ) asks.append( (id, x) ) if len(pool)>=GlobalConfig.Jobs: Algo.wait_pool(pool, tells) state, asks, tells, pool = Algo.dump_state(wd, state, asks, tells, pool) while len(pool)>0: Algo.wait_pool(pool, tells) state, asks, tells, pool = Algo.dump_state(wd, state, asks, tells, pool)
def __call__(self, vars, tag=None): wd, state = self.create_workdir( pj(env.runs_dir, "mc" if tag is None else tag)) if state is None: state = State(self.seed) state.dump(wd) rng = np.random.RandomState(state.seed) asks, tells, pool = [], [], [] run_ids = range(sum([len(v[1]) for v in state.vals]), self.number) dim_size = len(vars) X = np.zeros((len(run_ids), dim_size)) for dim_idx in xrange(dim_size): X[:, dim_idx] = self.min_bound + self.max_bound * lhs_sample( len(run_ids), rng) for x_id, run_id in enumerate(run_ids): x = X[x_id, :] pool.append((run_id, runner(x, vars, wd, wait=False, id=run_id, min=self.min_bound, max=self.max_bound))) asks.append((run_id, x)) if len(pool) >= GlobalConfig.Jobs: Algo.wait_pool(pool, tells) state, asks, tells, pool = Algo.dump_state( wd, state, asks, tells, pool) while len(pool) > 0: Algo.wait_pool(pool, tells) state, asks, tells, pool = Algo.dump_state(wd, state, asks, tells, pool)
def create_workdir(self, wd): state = None if os.path.exists(wd): while True: ans = raw_input("%s already exists. Continue learning? (y/n): " % (wd)) if ans in ["Y","y"]: state = State.read_from_dir(wd) break elif ans in ["N", "n"]: logging.warning("Deleting {}".format(wd)) shutil.rmtree(wd) make_dir(wd) break else: logging.warning("incomprehensible answer") else: make_dir(wd) return wd, state
def create_workdir(self, wd): state = None if os.path.exists(wd): while True: ans = raw_input( "%s already exists. Continue learning? (y/n): " % (wd)) if ans in ["Y", "y"]: state = State.read_from_dir(wd) break elif ans in ["N", "n"]: logging.warning("Deleting {}".format(wd)) shutil.rmtree(wd) make_dir(wd) break else: logging.warning("incomprehensible answer") else: make_dir(wd) return wd, state
#!/usr/bin/env python from lib.evolve_state import State import sys state = State.read_from_dir(sys.argv[1]) for v in state.vals: for vv in v[0]: for ii, vvv in enumerate(vv): print "{0:.12f}".format(vvv), print v[1][0]
def __call__(self, vars, tag=None): wd, state = self.create_workdir( pj(env.runs_dir, "grid_search" if tag is None else tag)) if state is None: state = State(0) # doesn't matter state.dump(wd) if self.freeze_point: self.freeze_point = [ float(p) for p in self.freeze_point.split(" ") if p.strip() ] if self.non_freeze_vars is None: raise Exception( "Got freeze point but freeze variables are not defined") if self.non_freeze_vars: self.non_freeze_vars = [ v.strip() for v in self.non_freeze_vars.split(" ") if v.strip() ] else: self.non_freeze_vars = vars dim_of_problem = len(self.non_freeze_vars) number_of_dim_slice = (self.max_bound - self.min_bound) / self.step axis_slices = [ np.linspace(self.min_bound, self.max_bound, num=number_of_dim_slice) for _ in xrange(dim_of_problem) ] if len(axis_slices) == 1: points = axis_slices[0].reshape(1, -1).T else: points = np.vstack(np.meshgrid(*axis_slices)).reshape( dim_of_problem, -1).T number_of_steps = len(points) if number_of_steps > GlobalConfig.NumberOfCalcutationsUpperBound: raise Exception( "There are a lot of calculations ({}). Reconsider your setup". format(number_of_steps)) nsteps_done = sum([len(v[1]) for v in state.vals]) points = points[nsteps_done:] asks, tells, pool = [], [], [] for id, point in enumerate(points): if self.freeze_point: x = list(self.freeze_point) for vi, v in enumerate(self.non_freeze_vars): try: var_idx = vars.index(v) except ValueError: raise Exception( "Can't find non freeze var {} in specification". format(v)) x[var_idx] = point[vi] else: x = point pool.append((id, runner(x, vars, wd, wait=False, id=id, min=self.min_bound, max=self.max_bound))) asks.append((id, x)) if len(pool) >= GlobalConfig.Jobs: Algo.wait_pool(pool, tells) state, asks, tells, pool = Algo.dump_state( wd, state, asks, tells, pool) while len(pool) > 0: Algo.wait_pool(pool, tells) state, asks, tells, pool = Algo.dump_state(wd, state, asks, tells, pool)