def infer_x(self, y, sigma = 1.0, tolerance = 1e-2): """Infer probable x from input y @param y the desired output for infered x. @return a list of probable x """ OptimizedInverseModel.infer_x(self, y) if self.fmodel.size() == 0: return self._random_x() x_guesses = [self._guess_x_simple(y)[0]] #x_guesses = self._guess_x_kmeans(y) if CMA_IMPLEM == "C++": # C++ IMPLEM result = [] for xg in x_guesses: sigma = 0.05 #print self.lower, self.upper p = lci.to_params(list(xg), sigma, lbounds=self.lower, ubounds=self.upper, max_fevals=20 ) objfunc = lci.to_fitfunc(self._error) cmasols = lci.pcmaes(objfunc, p) # collect and inspect results bcand = cmasols.best_candidate() error = bcand.get_fvalue() x = lcmaes.get_candidate_x(bcand) result.append((error, x)) #print "x", [xi for fi, xi in sorted(result)] return [xi for fi, xi in sorted(result)] elif CMA_IMPLEM == "python": # PYTHON IMPLEM result = [] for xg in x_guesses: res = cma.fmin(self._error, xg, 0.05, options={'bounds':[self.lower, self.upper], 'verb_log':0, 'verb_disp':False, #'ftarget':tolerance/2, 'maxfevals':20}) result.append((res[1], res[0])) # return [xi for fi, i, xi in sorted(result)[:min(N_RESULT, len(result))]] return [xi for fi, xi in sorted(result)]
def ncsurrfunc(c,m): x = [] y = [] for ci in c: x.append(lcmaes.get_candidate_x(ci)) y.append(ci.get_fvalue()) #nx = encode(x,m) traindt(x,y) return 1
def ncsurrfunc(c, m): x = [] y = [] for ci in c: x.append(lcmaes.get_candidate_x(ci)) y.append(ci.get_fvalue()) #nx = encode(x,m) traindt(x, y) return 1
def nsurrfunc(c,m): x = [] for ci in c: x.append(lcmaes.get_candidate_x(ci)) #nx = encode(x,m) y = predict(x) i = 0 for ci in c: ci.set_fvalue(y[i]) i = i + 1 return 1
def nsurrfunc(c, m): x = [] for ci in c: x.append(lcmaes.get_candidate_x(ci)) #nx = encode(x,m) y = predict(x) i = 0 for ci in c: ci.set_fvalue(y[i]) i = i + 1 return 1
def fit(self, X, y, opt_fit=False, libcma=False, **params): if opt_fit: def err_function(params): mdl = NrmModel(bias=params[0], conf_threshold=params[1], mu0=params[2], kappa0=params[3], alpha0=params[4], beta0=params[5]) return -mdl.score(X, y) start = np.array([self.bias, self.conf_threshold] + list(self.prior)) if not libcma: import cma x = cma.fmin(err_function, start, 1.5, restarts=5, options={'verbose': -9})[0] else: import lcmaes import lcmaes_interface as lci print(('Start:', err_function(start))) ffunc = lci.to_fitfunc(err_function) lbounds = [-np.inf, 0, -np.inf] + 3 * [0] fopt = lci.to_params( list(start), 1.5, str_algo=b'aipop', lbounds=lbounds, restarts=5, ) res = lci.pcmaes(ffunc, fopt) bcand = res.best_candidate() print(('End:', bcand.get_fvalue())) x = lcmaes.get_candidate_x(bcand) return self.set_params(bias=x[0], conf_threshold=x[1], mu0=x[2], kappa0=x[3], alpha0=x[4], beta0=x[5]) return self
# input parameters for a 10-D problem x = [10]*10 olambda = 10 # lambda is a reserved keyword in python, using olambda instead. seed = 0 # 0 for seed auto-generated within the lib. sigma = 0.1 p = lcmaes.make_simple_parameters(x,sigma,olambda,seed) p.set_str_algo("acmaes") # objective function. def nfitfunc(x,n): val = 0.0 for i in range(0,n): val += x[i]*x[i] return val # generate a function object objfunc = lcmaes.fitfunc_pbf.from_callable(nfitfunc); # pass the function and parameter to cmaes, run optimization and collect solution object. cmasols = lcmaes.pcmaes(objfunc,p) # collect and inspect results bcand = cmasols.best_candidate() bx = lcmaes.get_candidate_x(bcand) print "best x=",bx print "distribution mean=",lcmaes.get_solution_xmean(cmasols) cov = lcmaes.get_solution_cov(cmasols) # numpy array print "cov=",cov print "elapsed time=",cmasols.elapsed_time(),"ms"
seed = 0 # 0 for seed auto-generated within the lib. sigma = 0.1 scaling = [int(1000 * random.random()) for i in xrange(10)] shift = [int(1000 * random.random()) for i in xrange(10)] gp = lcmaes.make_genopheno_ls(scaling, shift) p = lcmaes.make_parameters_ls(x, sigma, gp, lambda_, seed) # objective function. def nfitfunc(x, n): val = 0.0 for i in range(0, n): val += x[i] * x[i] return val # generate a function object objfunc = lcmaes.fitfunc_pbf.from_callable(nfitfunc) # pass the function and parameter to cmaes, run optimization and collect solution object. cmasols = lcmaes.pcmaes_ls(objfunc, p) # collect and inspect results bcand = cmasols.best_candidate() bx = lcmaes.get_candidate_x(bcand) print("best x=", bx) print("distribution mean=", lcmaes.get_solution_xmean(cmasols)) cov = lcmaes.get_solution_cov(cmasols) # numpy array print("cov=", cov) print("elapsed time=", cmasols.elapsed_time(), "ms")