# The random variables being independent, the pdf of X # is the product of the individual pdfs return reduce(lambda x,y: x*y, [sampling.exp_pdf(xi, [1.0/ui]) for xi,ui in zip(x,u)]) ############################################## # MonteCarlo estimation Nmc = 10**4 # With Monte-Carlo, we compute the mean of the indicator function [mc_estimation_mu, mc_estimation_var] = sampling.monte_carlo(Nmc, lambda X: performance(X) >= gamma, lambda:my_sampling_func(u)) print "Monte Carlo with %g samples : P(X >= %e) = %.2e (std %.3e)" %(Nmc, gamma, mc_estimation_mu, math.sqrt(mc_estimation_var)) ############################################### # CE Method N = 1000 Nmc_ce = 10**5 rho = 0.1 [v_ce,gamma_ce] = CE.algorithm_2_1(u, performance, my_sampling_func, my_pdf, [N, rho, gamma],0) print "Cross Entropy : v = [%s] ; gamma = %.3f" % (' '.join('{0:.3f};'.format(k) for k in v_ce), gamma_ce) # We now compute the mean of the indicator times the ratio likelihood [mc_ce_estimation_mu, mc_ce_estimation_var] = sampling.monte_carlo(Nmc_ce, lambda X: (performance(X) >= gamma)*my_pdf(X,u)/my_pdf(X,v_ce), lambda:my_sampling_func(v_ce)) print "Monte Carlo with %g samples : P(X >= %e) = %.2e (std %.3e)" %(Nmc_ce, gamma, mc_ce_estimation_mu, math.sqrt(mc_ce_estimation_var))
# The weight of the connections are independently drawn from # an exponential distribution with means in v # i.e. the parameter of the exponential distributions are 1/v_i return [sampling.generate_sample_IT(lambda x: sampling.exp_cdf_1(x,[1.0/vi])) for vi in v] def my_pdf(x, u): # The random variables being independent, the pdf of X # is the product of the individual pdfs return reduce(lambda x,y: x*y, [sampling.exp_pdf(xi, [1.0/ui]) for xi,ui in zip(x,u)]) def my_stochastic_solver(samples, performances, gamma, u, vt_1): norm_fac = 0.0 vt = [0.0 for i in range(len(vt_1))] for (Xi, Sxi) in zip(samples,performances): norm_fac += (Sxi >= gamma) * my_pdf(Xi, u)/my_pdf(Xi,vt_1) vt = [vtj + Xij * (Sxi >= gamma) * my_pdf(Xi, u)/my_pdf(Xi,vt_1) for (vtj,Xij) in zip(vt,Xi)] vt = [vtj/norm_fac for vtj in vt] return vt def should_stop(history, t): return (t >= 10) N = 100 rho = 0.1 alpha = 0.1 v0 = [1.0, 1.00] params = [N, rho, alpha] p = CE.optimization(v0, f, my_sampling_func, my_stochastic_solver, params, should_stop) print p
# A tutorial on the Cross Entropy Method # Algorithm 2.2 # An example of optimization problem # Find the binary vector that maximizes performance_func import sampling import CE import random import math Np = 10 N = 50 rho = 0.1 y = [(random.random() < 0.5) for i in range(Np)] p0 = [1.0/2.0 for i in range(Np)] def sampling_func(p): return [(random.random()<pi) for pi in p] def performance_func(x): global y return Np - sum([math.fabs(xi-yi) for xi,yi in zip(x,y)]) pt= CE.algorithm_2_2(p0, performance_func, sampling_func, [N, rho], 0) print "Optimal value : [%s] \n" % (' '.join('{0:.1f};'.format(k) for k in y)) print "Found : [%s] \n" % (' '.join('{0:.1f};'.format(k) for k in pt)) print "Performance : %.2f [optimal : %.2f]" % (performance_func(pt), Np)
import CE from time import sleep E = CE.Extraction() RedExtraction = E.Extract("test.jpg", "demo.jpg", 0, 100) if RedExtraction: print("Color extration went successfully! You can find you picture here:") print(RedExtraction)