def flat_init(self): # 无向Graph自动补全 for i in self.relat.node(): for j in range(i): self.relat.add_weighted_edges_from([ (i, j, 0.5 + unif(-0.01, 0.01)) ]) # 关系初始值和扰动大小 for i in self.power.node(): for j in self.power.node(): self.power.add_weighted_edges_from([(i, j, 0.5 + unif(-0.01, 0.01))])
def randproportional(weights): """http://eli.thegreenplace.net/2010/01/22/weighted-random-generation-in-python/ -- very clever""" threshold = unif() * sum(weights) for i, weight in enumerate(weights): threshold -= weight if threshold <= 0: return i
def new_flat_init(self): # 无向Graph自动补全 for i in self.relat.node(): for j in range(i): # self.relat.add_weighted_edges_from([(i, j, self.arg['relat_init'])]) #测试稳态用公式 self.relat.add_weighted_edges_from([ (i, j, self.arg['relat_init'] + unif(-self.arg['relat_turb'], self.arg['relat_turb'])) ]) # 关系初始值和扰动大小 for i in self.power.node(): for j in self.power.node(): if i != j: self.power.add_weighted_edges_from([ (i, j, self.arg['power_init'] + unif(-self.arg['power_turb'], self.arg['power_turb'])) ])
def add_newborn(self): """ add k newborn to queue i of current barn """ k = 0 free_capacity = self.compute_free_capacity() if(free_capacity > 0): #if(birth_rate[self.stage_type] != 0): #check birth_rate for current barn k0 = 1 + int(-math.log(unif()) * (birth_rate[self.stage_type] - 1) + 0.5) #k=min(poisson.rvs(birth_rate[self.stage_type]),free_capacity) k = min( k0 , free_capacity ) self.Dlist = self.Dlist + [0] * k #add k newborn to queue i
def randproportional_mine(distribution): # given a list of floats, pick one with probability proportional # to the float relative to the other floats. return an int index. cumulative = [] last = 0 for event in distribution: last += event cumulative.append(last) # cumulative[-1] should equal the sum (=last), now uniform = unif() * last for i, threshold in enumerate(cumulative): if uniform <= threshold: return i
def branch (self, Pmore, Pless): assert 0<=Pmore and 0<=Pless and Pmore+Pless<=1, "invalid Pmore,Pless" prev = self.levels[-1] next = [] for n,i,j in prev: n += 1 w = random.unif(0.0,1.0) if w > Pless: continue if 1-w <= Pmore: next.append((n,j,self.new_id())) next.append((n,j,self.new_id())) else: next.append((n,j,j)) self.levels.append(next)
def initBidders(size,bidlist,a,b): for i in size: bidlist.append(random.unif(a,b)) return (clock,bidlist)
def randrange(max): return int(unif() * max)
def run(stdscr, debug): solvers = repeat(Candidate, solvers_size) parasites = repeat(Parasite, parasites_size) for iteration in range(iterations): trials = [Trial(solver, parasite) for parasite in parasites for solver in solvers] by_solver_mistakes = [] by_solver = [] for solver, solver_trials in groupby(sorted(trials, key=solver_key), solver_key): solver_trials = list(solver_trials) total_mistakes = sum(map(mistakes_key, solver_trials)) total_penalty = sum(map(penalty_key, solver_trials)) by_solver_mistakes.append((total_mistakes, solver)) by_solver.append((total_penalty, solver)) if total_mistakes == 0: print print "Perfect solver: %r" % solver print " len: %d" % len(solver) for trial in solver_trials: print trial.parasite print solver.sort(trial.parasite) raise Exception("DONE!") # we prefer lower penalties # randpenalty(best_solvers) by_parasite = [] for parasite, parasite_trials in groupby(sorted(trials, key=parasite_key), parasite_key): total_mistakes = sum(map(mistakes_key, parasite_trials)) by_parasite.append((total_mistakes, parasite)) # we prefer higher penalties # randproportionalvalue(easiest_parasites) best_solvers = sorted(by_solver, key=first_key) best_score, best_candidate = best_solvers[0] worst_score, worst_candidate = best_solvers[-1] if debug and iteration % 10 == 0: best_solvers_by_mistakes = sorted(by_solver_mistakes, key=first_key) best_mistakes, best_candidate_by_mistakes = best_solvers_by_mistakes[0] # worst_score, worst_candidate = best_solvers[-1] print "#%5d, mean: %7.2f" % (iteration, mean([score for score, solver in best_solvers])) # print ' worst: %3d (%3d)' % (worst_score, len(worst_candidate.swaps)) print " best: %3d - %3.1f (%3d)" % (best_mistakes, best_score, len(best_candidate_by_mistakes)) # print ' %r' % best_candidate.swaps easiest_parasites = sorted(by_parasite, key=first_key) hardest_parasite_mistakes, hardest_parasite = easiest_parasites[-1] # print ' hardest_parasite: %r' % hardest_parasite.test_values # print ' score: %d' % hardest_parasite_mistakes # print " best's attempt: %r" % best_candidate.sort(hardest_parasite) # print ' eval seq %r' % eval_pool_choice # print ' best sort %r' % best_candidate.sort(eval_pool_choice) # print ' perfect.fitness: %s' % perfect.fitness(eval_pool_choice) # print ' perfect sort %r' % perfect.sort(eval_pool_choice) next_solvers = [] # for m in range(best_count) reproductions = (solvers_size - solvers_carryover) / 2 for n in range(reproductions): a = randpenalty(by_solver) b = randpenalty(by_solver) next_solvers += a.crossover(b) # these are copied, they are not the originals next_solvers += [randpenalty(by_solver).clone() for c in range(solvers_carryover)] for solver in next_solvers: if unif() < 0.1: solver.mutate() next_solvers[0] = best_solvers[0][1] solvers = next_solvers next_parasites = [randproportionalvalue(by_parasite).clone() for p in range(parasites_size)] for parasite in next_parasites: parasite.mutate() parasites = next_parasites output.csv(iteration, best_score, len(best_candidate), worst_score, len(worst_candidate))