def main(): # param length # 10 fights # rating_mu, rating_sig, wins, losses, odds input_params_len = 10 * 2 * 4 es = CMAEvolutionStrategy([0] * input_params_len, 0.5) while not es.stop(): solutions = es.ask() func_vals = [get_betting_result(x) for x in solutions] es.tell(solutions, func_vals) es.logger.add() # write data to disc to be plotted es.disp() es.result_pretty()
class CMAES(Algorithm): def initialize(self, **kwargs): super().initialize(**kwargs) if self.x0 is None: self.x0 = self.domain.l + self.domain.range / 2 # cma operates on normalized scale x0 = self.domain.normalize(self.x0) self.cma = CMAEvolutionStrategy(x0=x0, sigma0=self.config.sigma0, inopts={'bounds': [0, 1]}) self._X = None self._X_i = 0 self._Y = None def _next(self, context=None): if self._X is None: # get new population self._X = self.cma.ask() self._Y = np.empty(len(self._X)) self._X_i = 0 return self.domain.denormalize(self._X[self._X_i]) def finalize(self): self.cma.result_pretty() def best_predicted(self): xbest = None if self.cma.result.xbest is not None: xbest = self.domain.denormalize(self.cma.result.xbest) return xbest if not xbest is None else self.x0 def add_data(self, data): self._Y[self._X_i] = data['y'] self._X_i += 1 # population complete if self._X_i == len(self._X): self.cma.tell(self._X, -self._Y) self._X = None super().add_data(data)
def run(): train = 0 names = [ # 'bet_pred_a', 'bet_pred_b', 'bet_odds_a', 'bet_odds_b', 'bet_wnl_a', 'bet_wnl_b', 'bet_ts_a', 'bet_ts_b', 'bet_tmi_a', 'bet_tmi_b', 'bet_tma_a', 'bet_tma_b', ] params = [ 0, 0, 0, 0, 0, 0 ] bounds = [[-np.inf], [np.inf]] assert len(params) == len(names) # assert len(params) == len(bounds[0]) if train: sigma = 1 opts = CMAOptions() # opts['tolx'] = 1E-2 opts['bounds'] = bounds es = CMAEvolutionStrategy(params, sigma, inopts=opts) while not es.stop(): solutions = es.ask() fitness = [main(x, train=1) for x in solutions] es.tell(solutions, fitness) es.disp() print(list(es.result[0])) print(list(es.result[5])) es.result_pretty() print('') print('best') print(list(es.result[0])) print('') print('xfavorite: distribution mean in "phenotype" space, to be considered as current best estimate of the optimum') print(list(es.result[5])) else: main(params)
def run(): train = 0 names = [ # 'pred_a', 'pred_b', # 0.0 # 'odds_a', 'odds_b', # 2.1 # 'bet_wnl_a', 'bet_wnl_b', # 2.1 # 'bet_ts_a', 'bet_ts_b', # 1.5 # 'bet_tmi_a', 'bet_tmi_b', # 1.1 # 'bet_tma_a', 'bet_tma_b', # 0.9 # 'bet_drs_a', 'bet_drs_b', # 0.5 'bet_sfc_a', 'bet_sfc_b', # -0.1 # 'bet_spd_a', 'bet_spd_b', # 0.8 # 'bet_set_a', 'bet_set_b', # 1.0 # 'bet_gms_a', 'bet_gms_b', # -0.2 # 'bet_tie_a', 'bet_tie_b', # 4.1 # 'bet_ups_a', 'bet_ups_b', # -0.2 # 'bet_age_a', 'bet_age_b', # -2.5 ] params = [0, 0] bounds = [[-np.inf], [np.inf]] assert len(params) == len(names) assert len(params) == len(bounds) if train: sigma = 1 opts = CMAOptions() # opts['tolx'] = 1E-2 opts['bounds'] = bounds es = CMAEvolutionStrategy(params, sigma, inopts=opts) while not es.stop(): solutions = es.ask() try: fitness = [main(x, train) for x in solutions] except ValueError as exc: print(str(exc)) continue es.tell(solutions, fitness) es.disp() print(list(es.result[0])) print(list(es.result[5])) es.result_pretty() print( f'finished after {es.result[3]} evaluations and {es.result[4]} iterations' ) print('') print('best') print(list(es.result[0])) print('') print( 'xfavorite: distribution mean in "phenotype" space, to be considered as current best estimate of the optimum' ) print(list(es.result[5])) # res = minimize(main, params, (train,), bounds=bounds) # print('') # print(f'{res.nit} iterations') # print(f'Success: {res.success} {res.message}') # print(f'Solution: {res.x}') # return else: main(params)
def run(): train = 0 names = [ 'bet_multi_param', 'bet_tma_a', 'bet_tma_b', 'bet_lati_a', 'bet_lati_b', 'bet_tiew_a', 'bet_tiew_b', # 'bet_upsr_a', 'bet_upsr_b', # 'bet_sfcw_a', 'bet_sfcw_b', # 'bet_wnll_a', 'bet_wnll_b', # 'bet_tier_a', 'bet_tier_b', # 'bet_upsl_a', 'bet_upsl_b', # 'bet_ts_a', 'bet_ts_b', # 'bet_wnlw_a', 'bet_wnlw_b', # 'bet_setw_a', 'bet_setw_b', # 'bet_setl_a', 'bet_setl_b', # 'bet_gms_a', 'bet_gms_b', # 'bet_drsl_a', 'bet_drsl_b', # 'bet_tmi_a', 'bet_tmi_b', # 'bet_wnlr_a', 'bet_wnlr_b', # 'bet_upsw_a', 'bet_upsw_b', # 'bet_drs_a', 'bet_drs_b', # 'bet_setr_a', 'bet_setr_b', # 'bet_drsw_a', 'bet_drsw_b', # 'bet_tiel_a', 'bet_tiel_b', # 'bet_age_a', 'bet_age_b', # 'bet_spd_a', 'bet_spd_b', # 'bet_sfcr_a', 'bet_sfcr_b', ] tolx = 10000 # more higher then longer time params = [-14, 0, 0, 0, 0, 0, 0] bounds = [[-np.inf], [np.inf]] assert len(params) == len(names) # assert len(params) == len(bounds) if train: time_start = time() mins = 60 * 4 sigma = 1 opts = CMAOptions() opts['bounds'] = bounds es = CMAEvolutionStrategy(params, sigma, inopts=opts) while not es.stop(): solutions = es.ask() try: fitness = [main(x, train) for x in solutions] except ValueError as exc: print(str(exc)) continue es.tell(solutions, fitness) es.disp() # print(list(es.result[0])) print(f'tolx={es.opts["tolx"]:.3f} sol={list(es.result[5])}') es.opts['tolx'] = es.result[3] / tolx if time() - time_start > 60 * mins: print(f'{mins}min limit reached') break es.result_pretty() print( f'finished after {es.result[3]} evaluations and {es.result[4]} iterations' ) # print('') # print('best') # print(list(es.result[0])) print('') print( 'xfavorite: distribution mean in "phenotype" space, to be considered as current best estimate of the optimum' ) print(list(es.result[5])) # pmin = -20 # pmax = 20 # step = (pmax - pmin) / 10 # rranges = [ # slice(pmin, pmax, step), # slice(pmin, pmax, step), # ] # res = optimize.brute(main, rranges, (train,), finish=None) # print(res) # return # res = minimize(main, params, (train,), bounds=bounds) # print('') # print(f'{res.nit} iterations') # print(f'Success: {res.success} {res.message}') # print(f'Solution: {res.x}') # return else: main(params)
fitness_func = Fitness(img, decoder) best_img = None best_z = None best_score = -1 for i in range(args.runs): print('Runs: %d / %d' % (i + 1, args.runs)) if encoder is None: init = np.random.randn(decoder.input_shape[-1]) * args.std else: init = x_mean[0] es = ES(init, args.sigma) for ite in range(args.iterations): dnas = np.asarray(es.ask()) es.tell(dnas, fitness_func(dnas)) es.disp() es.result_pretty() z = np.asarray(es.result[0]) img_reconstruct = decoder.predict(z[np.newaxis, ...])[0] mse = np.mean(np.square(img_reconstruct - img)) print('mse: {:.2f}'.format(mse)) if mse > best_score: best_score = mse best_z = z best_img = img_reconstruct output_img = np.round( np.concatenate( (np.squeeze(img), np.squeeze(img_reconstruct)), axis=1) * 127.5 + 127.5).astype(np.uint8) filename, ext = os.path.splitext(args.output) imsave(filename + '_%d' % i + ext, output_img) output_img = np.round(