def avm(self, methods, inputs=None): for method in methods: for j in range(self.attempts): # print("Starting inputs, iteration ", inputs, j) if not inputs: inputs = [ random.randint(-self.range, self.range) for i in range(self.arg_count) ] # start_inputs = copy.deepcopy(inputs) for i, value in enumerate(inputs): try: value, fitness = method(inputs, i) except AnswerFound: # print("Found Answer") # print(self.answer) return self.answer except TimeExceeded: return "Unable to find solution, time exceeded", inputs if fitness <= 0.0 and self.satisfied_condition(inputs): # break return inputs, calculate_fitness( self.tree, inputs, self.path, self.func_name) inputs[i] = value if self.satisfied_condition(inputs): return inputs, calculate_fitness(self.tree, inputs, self.path, self.func_name) inputs = None self.range *= 10 # print(self.results) # print('---') # for res in self.results.items(): # print(res) # print('---') return "Unable to find solution", inputs
def mutate_population(population, scores): for i in range(len(population)): if uniform(0, 1) < MUTATION: population[i] = mutate_conference(population[i]) scores[i] = calculate_fitness(population[i]) return (population, scores)
def get_f(self, inputs, index, value): inputs[index] = value if str(inputs) in self.results: return self.results[str(inputs)] else: fitness, branch_state, approach_level = calculate_fitness( self.tree, inputs, self.path, self.func_name) # self.iterations += 1 # # print(self.iterations) # if self.iteration_limit < self.iterations: # print("Iterations exceeded") # raise IterationsExceeded # if not self.state: # print(fitness, branch_state, inputs) if approach_level == 0 and branch_state == self.state: self.answer = inputs, (fitness, branch_state, approach_level) raise AnswerFound self.results[str(inputs)] = fitness return fitness
def find_next_move(self): grid_backup = copy.deepcopy(self.grid) block_backup = self.block.copy() position_backup = self.position # TODO find an actual good min here best_score = -999 best_x = 0 best_rotation = 0 for num_rotation in range(4): self.move_block((0, 0)) if num_rotation > 0: self.rotate() while self.move_left(): pass for x in range(self.width): self.flash() fitness_score = calculate_fitness(self.grid, self.trait_sets) if fitness_score > best_score: best_score = fitness_score best_x = self.position[0] best_rotation = num_rotation self.move_block((self.position[0], 0)) if self.move_right() == False: break self.hide_current_block() self.position = position_backup self.block = block_backup self.grid = grid_backup return (best_x, best_rotation)
def get_fitness(self,cost): return fit.calculate_fitness(self.pos,cost)
sequence=np.concatenate((sequence,self.vel[0:int(c1)])) #if c2>=1: # sequence=np.concatenate((sequence,seq1)) # c2=c2-1 sequence=np.concatenate((sequence,seq1[0:int(c2)])) #if c3>=1: # sequence=np.concatenate((sequence,seq2)) # c3=c3-1 sequence=np.concatenate((sequence,seq2[0:int(c3)])) return sequence base=np.arange(cities) base=base+1 base=np.random.permutation(base) gbest_fit=fit.calculate_fitness(base,cost) pop=[] flag=0 #Initialize the particle positions,velocities,fitness for i in range(0,no_of_particles): temp1=np.random.permutation(base) temp2=np.random.permutation(base) fit_val1=fit.calculate_fitness(temp1,cost) fit_val2=fit.calculate_fitness(temp2,cost) if fit_val1<=fit_val2: #sequence=swap.get_swap_sequence(temp1,np.copy(temp2)) #sequence=np.random.permutation(sequence)
def test_fitness(): config = generate_nginx_config.generate_random_config() this_fitness = fitness.calculate_fitness(config) assert (this_fitness) assert (this_fitness == 999)
for e in tree.edges: if random.random() < 0.3: return e else: return get_node(e) return tree return tree def crossover(father, mother): node = get_node(mother) father.append(node) return mother fits = [] for t in trees: fits.append(calculate_fitness(t)) fits.sort() print fits[:5] for i in range(60): fits = {} for t in trees: fit = calculate_fitness(t) fits[fit] = t keys = fits.keys() keys.sort() keys = keys[:int(GENERATION_SIZE / 2)] trees = [] for t in keys: for i in range(2):
def satisfied_condition(self, inputs): return calculate_fitness(self.tree, inputs, self.path, self.func_name)[1] == self.state
def get_fitness(self, cost): return fit.calculate_fitness(self.pos, cost)
sequence = np.concatenate((sequence, self.vel[0:int(c1)])) #if c2>=1: # sequence=np.concatenate((sequence,seq1)) # c2=c2-1 sequence = np.concatenate((sequence, seq1[0:int(c2)])) #if c3>=1: # sequence=np.concatenate((sequence,seq2)) # c3=c3-1 sequence = np.concatenate((sequence, seq2[0:int(c3)])) return sequence base = np.arange(cities) base = base + 1 base = np.random.permutation(base) gbest_fit = fit.calculate_fitness(base, cost) pop = [] flag = 0 #Initialize the particle positions,velocities,fitness for i in range(0, no_of_particles): temp1 = np.random.permutation(base) temp2 = np.random.permutation(base) fit_val1 = fit.calculate_fitness(temp1, cost) fit_val2 = fit.calculate_fitness(temp2, cost) if fit_val1 <= fit_val2: #sequence=swap.get_swap_sequence(temp1,np.copy(temp2)) #sequence=np.random.permutation(sequence) sequence = swap.get_random_sequence(cities) if len(sequence) != 0:
temp_pop=np.zeros(shape=(pop_size,cities)) fval=np.zeros(shape=(pop_size,2)) counter=0 #variable used to terminate when convergence occurs #cost=np.loadtxt('gr17_d.txt') cost=np.loadtxt('dantzig42_d.txt') #initializing the population for i in range(0,pop_size): pop[i]=np.random.permutation(base) #print pop[i] #calculate the fitness of all individuals in the population for i in range(0,pop_size): fval[i][0]=i fval[i][1]=fit.calculate_fitness(pop[i],cost) #print fval[i][1] initial_max_fit=min(fval[:, 1]) #print pop print initial_max_fit for k in range(0,generations): new_pop=np.zeros(shape=(pop_size,cities)) #print 'hello' #select parents from matting pool with tournament selection for i in range(0,pop_size): indx1=random.randrange(0,pop_size) indx2=random.randrange(0,pop_size) fit_val1=fit.calculate_fitness(pop[indx1],cost)
pop = np.zeros(shape=(pop_size, cities)) temp_pop = np.zeros(shape=(pop_size, cities)) fval = np.zeros(shape=(pop_size, 2)) counter = 0 #variable used to terminate when convergence occurs #cost=np.loadtxt('gr17_d.txt') cost = np.loadtxt('dantzig42_d.txt') #initializing the population for i in range(0, pop_size): pop[i] = np.random.permutation(base) #print pop[i] #calculate the fitness of all individuals in the population for i in range(0, pop_size): fval[i][0] = i fval[i][1] = fit.calculate_fitness(pop[i], cost) #print fval[i][1] initial_max_fit = min(fval[:, 1]) #print pop print initial_max_fit for k in range(0, generations): new_pop = np.zeros(shape=(pop_size, cities)) #print 'hello' #select parents from matting pool with tournament selection for i in range(0, pop_size): indx1 = random.randrange(0, pop_size) indx2 = random.randrange(0, pop_size) fit_val1 = fit.calculate_fitness(pop[indx1], cost)