Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self,env):
'''
Input:
'''
self.env = env
self.env.reset(False)
self.controller = Keras_NN(env.observation_space.low.shape[0],env.action_space.low.shape[0])
self.nb_episodes = 0
self.options = cma.CMAOptions()
self.options['maxiter']=200
self.options['popsize']=50
self.options['CMA_diagonal']=True
self.options['verb_log']=50
self.options['verb_disp']=1
self.best_perf = -10000.0
def __init__(self,env):
'''
Input:
'''
self.env = env
self.env.reset(False)
self.controller = Keras_NN(env.observation_space.low.shape[0],env.action_space.low.shape[0])
self.controller.load_theta('cma_agents/best_agent.theta-2772.2691621')
self.nb_episodes = 0
self.options = cma.CMAOptions()
self.options['maxiter']=40
self.options['popsize']=50
# self.options['CMA_diagonal']=True
self.options['verb_log']=50
self.options['verb_disp']=1
self.best_perf = -10000.0
self.start_x = 0.73
self.start_y = -0.26
self.controller.set_param(layer_nb, numpoids1, self.start_x)
self.controller.set_param(layer_nb, numpoids2, self.start_y)
self.white_x = []
self.white_y = []
self.best_x = []
def __init__(self, prob):
Solver.__init__(self, prob)
opts = cma.CMAOptions()
# for k, v in opts.iteritems():
# print k, v
# exit(0)
self.p_dir = 'optim_data/cma/'
opts.set('verb_disp', 1)
opts.set('popsize', 8)
opts.set('verb_filenameprefix', self.p_dir)
opts.set('maxiter', 2000)
self.options = opts
self.cen = None
self.rng = None
def train(config):
task_queue = SimpleQueue()
result_queue = SimpleQueue()
stop = mp.Value('i', False)
stats = SharedStats(config.state_dim)
normalizers = [StaticNormalizer(config.state_dim) for _ in range(config.num_workers)]
for normalizer in normalizers:
normalizer.offline_stats.load(stats)
workers = [Worker(id, normalizers[id], task_queue, result_queue, stop, config) for id in range(config.num_workers)]
for w in workers: w.start()
opt = cma.CMAOptions()
opt['tolfun'] = -config.target
opt['popsize'] = config.pop_size
opt['verb_disp'] = 0
opt['verb_log'] = 0
opt['maxiter'] = sys.maxsize
es = cma.CMAEvolutionStrategy(config.initial_weight, config.sigma, opt)
total_steps = 0
initial_time = time.time()
training_rewards = []
training_steps = []
training_timestamps = []
test_mean, test_ste = test(config, config.initial_weight, stats)
logger.info('total steps %d, %f(%f)' % (total_steps, test_mean, test_ste))
training_rewards.append(test_mean)
training_steps.append(0)
def _initialise(self):
"""
Initialises the optimiser for the first iteration.
"""
assert(not self._running)
# Import cma (may fail!)
# Only the first time this is called in a running program incurs
# much overhead.
import cma
# Set up simulation
options = cma.CMAOptions()
# Set boundaries, or use manual boundary checking
self._manual_boundaries = False
if isinstance(self._boundaries, pints.RectangularBoundaries):
options.set(
'bounds',
[list(self._boundaries._lower), list(self._boundaries._upper)]
)
elif self._boundaries is not None:
self._manual_boundaries = True
# Set stopping criteria
#options.set('maxiter', max_iter)
#options.set('tolfun', min_significant_change)
# options.set('ftarget', target)
def test_cma(centroids_fname, dim):
centroids = np.loadtxt(centroids_fname)
opts = cma.CMAOptions()
#for i in opts:
# print(i, ' => ', opts[i])
max_evals = 1e6
opts.set('tolfun', 1e-20)
opts['tolx'] = 1e-20
opts['verb_disp'] = 1e10
opts['maxfevals'] = max_evals / centroids.shape[0]
opts['BoundaryHandler'] = cma.BoundPenalty
opts['bounds'] = [0, 1]
es_vector = []
for c in range(0, centroids.shape[0]):
es_vector += [cma.CMAEvolutionStrategy(dim * [0.5], 0.5, opts)]
total_evals = 0
log = open('cover_max_mean.dat', 'w')
if not callable(callback):
raise ValueError('Argument `callback` must be a callable function'
' or `None`.')
# Check if verbose mode is enabled
verbose = bool(verbose)
# Report first point
if callback is not None:
callback(np.array(hint, copy=True), f(hint, *args))
# Create evaluator object
if parallel:
evaluator = ParallelEvaluator(f, args=args)
else:
evaluator = SequentialEvaluator(f, args=args)
# Set up simulation options
best_solution = cma.BestSolution()
options = cma.CMAOptions()
options.set('bounds', [lower, upper])
options.set('tolfun', fatol)
options.set('ftarget', target)
if not verbose:
options.set('verbose', -9)
# Start one or multiple runs
for i in xrange(1 + ipop):
# Set population size, increase for ipop restarts
options.set('popsize', n)
if verbose:
print('Run ' + str(1+i) + ': population size ' + str(n))
n *= 2
# Run repeats from random points
if i > 0:
hint = lower + brange * np.random.uniform(0, 1, d)
# Search
def test_cma(urdf_directory, dim):
print('loading files...', end='')
centroids, tasks = load(urdf_directory, 2000)
print('data loaded')
opts = cma.CMAOptions()
#for i in opts:
# print(i, ' => ', opts[i])
max_evals = 1e6
opts.set('tolfun', 1e-20)
opts['tolx'] = 1e-20
opts['verb_disp'] = 1e10
opts['maxfevals'] = max_evals / centroids.shape[0]
opts['BoundaryHandler'] = cma.BoundPenalty
opts['bounds'] = [0, 1]
es_vector = []
for c in range(0, centroids.shape[0]):
es_vector += [cma.CMAEvolutionStrategy(dim * [0.5], 0.5, opts)]
num_cores = multiprocessing.cpu_count()
pool = multiprocessing.Pool(num_cores)
def __init__(self, hyperparameters, config, logger):
self.hyperparameters = hyperparameters
self.config = config
self.logger = logger
self.opts = cma.CMAOptions()
for param, value in self.config['cmaes_config'].iteritems():
if param in self.opts and value is not None:
self.opts[param] = value
self.dim = 0
for h in self.hyperparameters:
assert self.dim == h.index
self.dim += h.array_length if h.array_length is not None else 1
self.init = np.empty(self.dim)
# self.sigma = np.random.random(self.dim) # not allowed
self.sigma = self.config['cmaes_config']['sigma0']
self.opts['CMA_stds'] = np.ones(self.dim)
self.gen = 0
self.start_time = time.time()
self.best_fitnesses = []
self.mean_fitnesses = []