def Crossover(): global FunEval for i in range(0, PopSize): if (random.random() <= CR): # Choose two random indices i1, i2 = random.sample(range(0, PopSize), 2) # Parents p1 = Pop[i1] p2 = Pop[i2] # Choose a crossover point pt = random.randint(1, FF.D - 2) # Generate new childs c1 = p1.Chromosome[0:pt] + p2.Chromosome[pt:] c2 = p2.Chromosome[0:pt] + p1.Chromosome[pt:] # Get the fitness of childs c1Fitness = FF.MyFitFun(q, c1) FunEval = FunEval + 1 c2Fitness = FF.MyFitFun(q, c2) FunEval = FunEval + 1 # Select between parent and child if c1Fitness < p1.Fitness: Pop[i1].Fitness = c1Fitness Pop[i1].Chromosome = c1 if c2Fitness < p2.Fitness: Pop[i2].Fitness = c2Fitness Pop[i2].Chromosome = c2
def setWeight(): if SUPERVISED: src_err, diff_marg, tar_err = FitnessFunction.domain_differece(src_feature=Core.src_feature, src_label=Core.src_label, classifier=Core.classifier, tarU_feature=Core.tarU_feature, tarL_feature=Core.tarL_feature, tarL_label=Core.tarL_label) else: src_err, diff_marg, tar_err = FitnessFunction.domain_differece(src_feature=Core.src_feature, src_label=Core.src_label, classifier=Core.classifier, tarU_feature=Core.tarU_feature) print(src_err, diff_marg, tar_err) if diff_marg == 0: FitnessFunction.margWeight = 0 else: FitnessFunction.margWeight = 1.0/diff_marg if tar_err == 0: FitnessFunction.tarWeight = 0 else: FitnessFunction.tarWeight = 1.0 / tar_err if src_err == 0: FitnessFunction.srcWeight = 0 else: FitnessFunction.srcWeight = 1.0/src_err
def genetic_algorithm(pop, points, generation_number): fitness_array = pop_fitness(pop, points) fitness_array_cumlative = cumulative_sum(fitness_array) summation = fitness_array_cumlative[len(fitness_array_cumlative) - 1] # Proof that the value converges # print("%f && %f " % (min(fitness_array), max(fitness_array))) next_pop = [] for _ in range(pop_size): r1, r2 = random.uniform(0, summation), random.uniform(0, summation) idx1 = bisect.bisect_left(fitness_array_cumlative, r1) idx2 = bisect.bisect_left(fitness_array_cumlative, r2) Offspring1, Offspring2 = cross_over(pop[idx1], pop[idx2]) mutate(Offspring1, generation_number) mutate(Offspring2, generation_number) next_pop.append(Offspring1) next_pop.append(Offspring2) # Merge the new and old populations and take the best of both pop = pop + next_pop pop.sort(key=lambda x: ff.fitness(x, points), reverse=True) next_gen = pop[:pop_size // 2] + create_population(len(pop[0]) - 1, pop_size // 2) best_chromosome = next_gen[0] best_val_old_pop = ff.fitness(best_chromosome, points) return next_gen, best_val_old_pop, best_chromosome
def Crossover(): global funEval for i in range(0, popSize): if (random.random() <= CR): # Choose two random indices i1, i2 = random.sample(range(0, popSize), 2) # Parents p1 = deepcopy(pop[i1]) p2 = deepcopy(pop[i2]) # Choose a crossover point pt = random.randint(1, ff.D - 2) # Generate new childs c1 = p1.chromosome[0:pt] + p2.chromosome[pt:] c2 = p2.chromosome[0:pt] + p1.chromosome[pt:] # Get the fitness of childs c1Fitness = ff.FitnessFunction(c1) funEval = funEval + 1 c2Fitness = ff.FitnessFunction(c2) funEval = funEval + 1 # Select between parent and child if c1Fitness < p1.fitness: pop[i1].fitness = c1Fitness pop[i1].chromosome = c1 if c2Fitness < p2.fitness: pop[i2].fitness = c2Fitness pop[i2].chromosome = c2
def test_angle(self): v1 = np.array([1, 0, 0]) v1.reshape(-1, 1) v2 = np.array([1, 1, 0]) v1.reshape(-1, 1) v3 = np.array([1, -1, 0]) v1.reshape(-1, 1) v4 = np.array([0, 1, 0]) v1.reshape(-1, 1) self.assertAlmostEqual(FitnessFunction.angle(v1, v1), 0, 5) self.assertAlmostEqual(FitnessFunction.angle(v1, v2), 1.5707969665527344 / 2, 5) self.assertAlmostEqual(FitnessFunction.angle(v1, v3), 1.5707969665527344 / 2, 5) self.assertAlmostEqual(FitnessFunction.angle(v1, v4), 1.5707969665527344, 5)
def setWeight(): if SUPERVISED: src_err, diff_marg, tar_err = FitnessFunction.domain_differece(src_feature=Core.src_feature, src_label=Core.src_label, classifier=Core.classifier, tarU_feature=Core.tarU_feature, tarU_soft_label=Core.tarU_soft_label, tarL_feature=Core.tarL_feature, tarL_label=Core.tarL_label) else: src_err, diff_marg, tar_err = FitnessFunction.domain_differece(src_feature=Core.src_feature, src_label=Core.src_label, classifier=Core.classifier, tarU_feature=Core.tarU_feature, tarU_soft_label=Core.tarU_soft_label) FitnessFunction.margWeight= 1.0 / diff_marg FitnessFunction.tarWeight = 1.0 / tar_err FitnessFunction.srcWeight = 1.0/src_err
def test_rotate(self): v1 = np.array([0, 0, -1]) v1.reshape(-1, 1) genome = np.array([0, 0, 0, math.pi/2, 0]) v2 = v1.dot(FitnessFunction.rotate(genome)) self.assertAlmostEqual(v2[0], 0, 5) self.assertAlmostEqual(v2[1], 1, 5) self.assertAlmostEqual(v2[2], 0, 5) genome = np.array([0, 0, 0, 0, math.pi/2]) v2 = v1.dot(FitnessFunction.rotate(genome)) self.assertAlmostEqual(v2[0], 0, 5) self.assertAlmostEqual(v2[1], 0, 5) self.assertAlmostEqual(v2[2], -1, 5) genome = np.array([0, 0, 0, 0, math.pi*2.5]) v2 = v1.dot(FitnessFunction.rotate(genome)) self.assertAlmostEqual(v2[0], 0, 5) self.assertAlmostEqual(v2[1], 0, 5) self.assertAlmostEqual(v2[2], -1, 5) genome = np.array([0, 0, 0, math.pi/2, math.pi]) v2 = v1.dot(FitnessFunction.rotate(genome)) self.assertAlmostEqual(v2[0], 0, 5) self.assertAlmostEqual(v2[1], -1, 5) self.assertAlmostEqual(v2[2], 0, 5) genome = np.array([0, 0, 0, math.pi/2, math.pi*1.5]) v2 = v1.dot(FitnessFunction.rotate(genome)) self.assertAlmostEqual(v2[0], 1, 5) self.assertAlmostEqual(v2[1], 0, 5) self.assertAlmostEqual(v2[2], 0, 5) genome = np.array([0, 0, 0, math.pi/2, math.pi*2.5]) v2 = v1.dot(FitnessFunction.rotate(genome)) self.assertAlmostEqual(v2[0], -1, 5) self.assertAlmostEqual(v2[1], 0, 5) self.assertAlmostEqual(v2[2], 0, 5) genome = np.array([0, 0, 0, math.pi/2, math.pi/2]) v2 = v1.dot(FitnessFunction.rotate(genome)) self.assertAlmostEqual(v2[0], -1, 5) self.assertAlmostEqual(v2[1], 0, 5) self.assertAlmostEqual(v2[2], 0, 5) v1 = np.array([0, 0, -1]) v1.reshape(-1, 1) genome = np.array([0, 0, 0, math.pi/2, math.pi/4]) v2 = v1.dot(FitnessFunction.rotate(genome)) self.assertAlmostEqual(FitnessFunction.angle(v2, np.array([-1, 1, 0])), 0, 5) it = 1.0 for x in range(0,int(it*math.pi)): for y in range(0,int(it*math.pi)): genome = np.array([0,0,0,y/it,x/it]) v2 = v1.dot(FitnessFunction.rotate(genome)) self.assertAlmostEqual(np.sqrt(v2.dot(v2)),1,5)
def DEOperation(): global FunEval for i in range(0, PopSize): # Choose three random indices i1, i2, i3 = random.sample(range(0, PopSize), 3) # Iterate for every Dimension NewChild = [] for j in range(FF.D): if (random.random() <= CR): k = Pop[i1].Chromosome[j] + F_Inertia * ( Pop[i2].Chromosome[j] - Pop[i3].Chromosome[j]) # If new dimention cross LB if k < FF.LB: k = random.uniform(FF.LB, FF.UB) # If new dimention cross LB if k > FF.UB: k = random.uniform(FF.LB, FF.UB) NewChild.append(round(k, 2)) else: NewChild.append(Pop[i].Chromosome[j]) # Child Fitness NewChildFitness = round(FF.MyFitFun(q, NewChild), 2) FunEval = FunEval + 1 # Select between parent and child if NewChildFitness < Pop[i].Fitness: Pop[i].Fitness = NewChildFitness Pop[i].Chromosome = NewChild
def opposite_weight(): # Make sure that all the weights are not 0, so all components are evaluated FitnessFunction.srcWeight = 0.3 FitnessFunction.margWeight = 0.3 FitnessFunction.tarWeight = 0.3 src_err, diff_marg, tar_err = \ FitnessFunction.domain_differece(src_feature=Core.Xs, src_label=Core.Ys, classifier=Core.classifier, tar_feature=Core.Xt) sum = abs(src_err) + abs(diff_marg) + abs(tar_err) if diff_marg == 0: FitnessFunction.margWeight = 0 else: FitnessFunction.margWeight = diff_marg / sum if tar_err == 0: FitnessFunction.tarWeight = 0 else: FitnessFunction.tarWeight = tar_err / sum if src_err == 0: FitnessFunction.srcWeight = 0 else: FitnessFunction.srcWeight = src_err / sum
def Mutation(): global UB, LB, funEval for i in range(0, popSize): if (random.random() <= MR): # Choose random index r = random.randint(0, popSize - 1) # Choose a parent p = deepcopy(pop[r]) # Choose mutation point pt = random.randint(0, ff.D - 1) # Generate new childs c = deepcopy(p.chromosome) # Mutation c[pt] = round(random.uniform(ff.LB, ff.UB), 2) #Get the fitness of childs cFitness = ff.FitnessFunction(c) funEval = funEval + 1 # Select between parent and child if cFitness < p.fitness: pop[r].fitness = cFitness pop[r].chromosome = c
def Mutation(): global FunEval, q for i in range(0, PopSize): if (random.random() <= MR): # Choose random index r = random.randint(0, PopSize - 1) # Choose a parent p = deepcopy(Pop[r]) # Choose mutation point pt = random.randint(0, FF.D - 1) # Generate new childs c = deepcopy(p.Chromosome) # Mutation c[pt] = round(random.uniform(FF.LB, FF.UB), 2) #Get the fitness of childs cFitness = FF.MyFitFun(q, c) FunEval = FunEval + 1 # Select between parent and child if cFitness < p.Fitness: Pop[r].Fitness = cFitness Pop[r].Chromosome = c
def main(): population = InitialPopulation.create() for x in range(1000): fitness_matrix = FitnessFunction.do(population).copy() population = NewPopulation.create(fitness_matrix).copy() Results.write(fitness_matrix[0], x)
def evaluate(particle): # Find all selected features indices = [index for index, entry in enumerate(particle) if entry == 1.0] # Build new dataset with selected features src_feature = Core.src_feature[:, indices] tarU_feature = Core.tarU_feature[:, indices] tarL_feature = Core.tarL_feature[:, indices] if SUPERVISED: return FitnessFunction.fitness_function(src_feature=src_feature, src_label=Core.src_label, tarU_feature=tarU_feature, classifier=Core.classifier, tarL_feature=tarL_feature, tarL_label=Core.tarL_label) else: return FitnessFunction.fitness_function(src_feature=src_feature, src_label=Core.src_label, tarU_feature=tarU_feature, classifier=Core.classifier)
def test_plot_rotation(self): c = Camera(0.48271098732948303, 1920, 1080, [5, 0, 0], [math.pi / 2, 0, math.pi]) genome = np.array([5, 0, 0, math.pi / 2, 0, math.pi]) t = Person('Max Mustermann', np.array([0, 0, 0]), 1, np.array([0.7, -0.2, 0]), np.array([0.7, 0.2, 0])) lt = Person('Agnes Angeschaute', np.array([2, 0, 0]), 1, np.array([1.3, -0.2, 0]), np.array([1.3, 0.2, 0])) persons = [t, lt] #PositionProcess.optimizeAllShots(t, t, c, [t], [], genome, [2]) snapshot = SceneSnapshot(t, lt, c, persons, [], [], [2]) optimizer = PositionProcess.CameraOptimizer(snapshot, 2) #optimizer.fitness(genome) winkel = arange(-math.pi, math.pi, 0.01) s = arange(-math.pi, math.pi, 0.01) startposition = np.array([5, 0, 0]) #startposition.reshape(-1, 1) for i in range(0, len(winkel)): p = startposition.dot(FitnessFunction.rotate(np.array([0, 0, 0, math.pi / 2, winkel[i]]))) s[i] = FitnessFunction.fitness(np.array([p[0],p[1],p[2],math.pi/2,winkel[i]+0.5*math.pi]), optimizer) #s[i] = optimizer.fitness(np.array([5,0,0,math.pi/2,winkel[i]+math.pi/2])) #s[i] = optimizer.getPersonQuality(np.array([p[0],p[1],p[2], math.pi / 2, winkel[i] + math.pi/2]), t, 1.2, # personFitnessByImage, occultationWeight) #s[i] = optimizer.getPersonQuality(np.array([5,0,0, math.pi / 2, winkel[i] + math.pi/2]), t, 1.2, # personFitnessByImage, occultationWeight) #s[i] = PositionProcess.getImageAngles(np.array([p[0],p[1],p[2], math.pi / 2, winkel[i] + math.pi*0.5]), t)[0] #s[i] = PositionProcess.getImageAngles(np.array([5,0,0,winkel[i]+math.pi / 2, math.pi/2]), t)[0] ax = subplot(111) ax.plot(winkel, s) ax.grid(True) ticklines = ax.get_xticklines() ticklines.extend(ax.get_yticklines()) gridlines = ax.get_xgridlines() gridlines.extend(ax.get_ygridlines()) ticklabels = ax.get_xticklabels() ticklabels.extend(ax.get_yticklabels()) for line in ticklines: line.set_linewidth(3) for line in gridlines: line.set_linestyle('-') for label in ticklabels: label.set_color('r') label.set_fontsize('medium') show()
def evaluate(individual): for sub_ind in individual: if sub_ind.height > MAX_DEPTH: print("Too high in GP: "+individual.height) funcs = [toolbox.compile(expr=tree) for tree in individual] src_feature = GPUtility.buildNewFeatures(Core.src_feature, funcs) tarU_feature = GPUtility.buildNewFeatures(Core.tarU_feature, funcs) tarL_feature = GPUtility.buildNewFeatures(Core.tarL_feature, funcs) if SUPERVISED: return FitnessFunction.fitness_function(src_feature=src_feature, src_label=Core.src_label, tarU_feature=tarU_feature, tarU_soft_label= Core.tarU_soft_label, classifier=Core.classifier, tarL_feature=tarL_feature, tarL_label=Core.tarL_label), else: return FitnessFunction.fitness_function(src_feature=src_feature, src_label=Core.src_label, tarU_feature=tarU_feature, tarU_soft_label=Core.tarU_soft_label, classifier=Core.classifier),
def Init(): global funEval for i in range(0, popSize): chromosome = [] for j in range(0, ff.D): chromosome.append(round(random.uniform(ff.LB, ff.UB), 2)) fitness = ff.FitnessFunction(chromosome) funEval = funEval + 1 newIndividual = Individual(chromosome, fitness) pop.append(newIndividual)
def Init(): global FunEval for i in range(0, PopSize): Chromosome = [] for j in range(0, FF.D): Chromosome.append(round(random.uniform(FF.LB, FF.UB), 2)) Fitness = FF.MyFitFun(q, Chromosome) FunEval = FunEval + 1 NewIndividual = Individual(Chromosome, Fitness) Pop.append(NewIndividual)
def Init(): global funEval for i in range(0, PopSize): particle = [] velocity = [] for j in range(0, FF.D): particle.append(round(random.uniform(FF.LB, FF.UB), 2)) velocity.append(round(random.uniform(vLB, vUB), 2)) particleFitness = round(FF.MyFitFun(q, particle), 2) funEval = funEval + 1 newIndividual = Individual(particle, particleFitness, velocity, deepcopy(particle), particleFitness) Pop.append(newIndividual)
def setWeight(): # Make sure that all the weights are not 0, so all components are evaluated FitnessFunction.srcWeight = 0.3 FitnessFunction.margWeight = 0.3 FitnessFunction.tarWeight = 0.3 if SUPERVISED: src_err, diff_marg, tar_err = FitnessFunction.domain_differece( src_feature=Core.src_feature, src_label=Core.src_label, classifier=Core.classifier, tarU_feature=Core.tarU_feature, tarL_feature=Core.tarL_feature, tarL_label=Core.tarL_label) else: src_err, diff_marg, tar_err = FitnessFunction.domain_differece( src_feature=Core.src_feature, src_label=Core.src_label, classifier=Core.classifier, tarU_feature=Core.tarU_feature) if diff_marg == 0: FitnessFunction.margWeight = 0 else: FitnessFunction.margWeight = 1.0 / diff_marg if tar_err == 0: FitnessFunction.tarWeight = 0 else: FitnessFunction.tarWeight = 1.0 / tar_err if src_err == 0: FitnessFunction.srcWeight = 0 else: FitnessFunction.srcWeight = 1.0 / src_err
def evaluate(particle): # Find all selected features indices = [index for index, entry in enumerate(particle) if entry == 1.0] # Build new dataset with selected features src_feature = Core.Xs[:, indices] tar_feature = Core.Xt[:, indices] # return FitnessFunction.fitness_confuse(src_feature, Core.src_label, tar_feature, Core.classifier), # return FitnessFunction.fitness_function(src_feature=src_feature, src_label=Core.src_label, # tar_feature=tar_feature, classifier=Core.classifier)[0], return FitnessFunction.fitness_function_framework( src_feature=src_feature, src_label=Core.Ys, classifier=Core.classifier, tar_feature=tar_feature)[0],
def write(path_list, x): matrix = Matrix.create() citys_list = CitysList.create() print() print("Geracao: {}".format(x)) print() print("Distancia em 100km: {}".format( FitnessFunction.calculate(path_list, matrix))) print() print("Brasilia => ", end='') for y in range(9): print("{} => ".format(citys_list[path_list[y]]), end='') print("Brasilia")
def normalize_weight_exp(): # Make sure that all the weights are not 0, so all components are evaluated FitnessFunction.srcWeight = 0.3 FitnessFunction.margWeight = 0.3 FitnessFunction.tarWeight = 0.3 src_err, diff_marg, tar_err = \ FitnessFunction.domain_differece(src_feature=Core.src_feature, src_label=Core.src_label, classifier=Core.classifier, tar_feature=Core.tar_feature) exp_src = math.exp(-abs(src_err)) exp_marg = math.exp(-abs(diff_marg)) exp_tar = math.exp(-abs(tar_err)) FitnessFunction.srcWeight = exp_src/((exp_src+exp_marg+exp_tar)) FitnessFunction.margWeight = exp_marg/((exp_src+exp_marg+exp_tar)) FitnessFunction.tarWeight = exp_tar /((exp_src+exp_marg+exp_tar))
def generate(size, pmin, pmax, smin, smax): # create position position = np.random.uniform(pmin, pmax, size) # refine the position beta = np.copy(position) beta = np.reshape(beta, (len(Core.Xs) + len(Core.Xt), Core.C)) beta = FitnessFunction.refine(beta) position = np.reshape( beta, ((len(Core.Xs) + len(Core.Xt)) * Core.C), ) part = creator.Particle(position) # create speed part.speed = np.zeros(size) part.smin = smin part.smax = smax return part
def test_angles(self): c = Camera(math.pi/2, 1920, 1080, [0,0,0], [math.pi/2, 0]) genome = np.array([0, 0, 0, math.pi/2, 0]) o1 = Object("o1", np.array([1, 1, 0])) xa, ya = FitnessFunction.getImageAngles(genome, o1) x = xa * 2.0 / c.aperture_angle y = ya * 2.0 * c.resolution_x / (c.aperture_angle * c.resolution_y ) self.assertAlmostEqual(x, 1, 5) self.assertAlmostEqual(y, 0, 5) o2 = Object("o2", np.array([0, 1, tan(c.aperture_angle / 2 * c.resolution_y / c.resolution_x)])) xa, ya = FitnessFunction.getImageAngles(genome, o2) x = xa * 2.0 / c.aperture_angle y = ya * 2.0 * c.resolution_x / (c.aperture_angle * c.resolution_y ) self.assertAlmostEqual(x, 0, 5) self.assertAlmostEqual(y, 1, 5) o3 = Object("o3", np.array([0.5, 1, 0.5 * tan(c.aperture_angle / 2 * c.resolution_y / c.resolution_x)])) xa, ya = FitnessFunction.getImageAngles(genome, o3) x = xa * 2.0 / c.aperture_angle y = ya * 2.0 * c.resolution_x / (c.aperture_angle * c.resolution_y ) self.assertAlmostEqual(x, arctan(0.5) / (c.aperture_angle / 2), 2) self.assertAlmostEqual(y, arctan(0.5) / (c.aperture_angle * c.resolution_y / c.resolution_x), 0) genome = np.array([0, 0, 0, math.pi/2, -math.pi/4]) xa, ya = FitnessFunction.getImageAngles(genome, o1) x = xa * 2.0 / c.aperture_angle y = ya * 2.0 * c.resolution_x / (c.aperture_angle * c.resolution_y ) self.assertAlmostEqual(x, 0, 5) self.assertAlmostEqual(y, 0, 5) genome = np.array([0, 0, 0, math.pi/2, math.pi/4]) xa, ya = FitnessFunction.getImageAngles(genome, o1) x = xa * 2.0 / c.aperture_angle y = ya * 2.0 * c.resolution_x / (c.aperture_angle * c.resolution_y ) self.assertAlmostEqual(x, 2, 5) self.assertAlmostEqual(y, 0, 5) genome = np.array([0, 0, 0, math.pi/2, math.pi/2]) xa, ya = FitnessFunction.getImageAngles(genome, o1) x = xa * 2.0 / c.aperture_angle y = ya * 2.0 * c.resolution_x / (c.aperture_angle * c.resolution_y ) self.assertAlmostEqual(x, 3, 5) self.assertAlmostEqual(y, 0, 5) genome = np.array([0, 0, 0, math.pi/2, math.pi]) xa, ya = FitnessFunction.getImageAngles(genome, o1) x = xa * 2.0 / c.aperture_angle y = ya * 2.0 * c.resolution_x / (c.aperture_angle * c.resolution_y ) self.assertAlmostEqual(x, 3, 5) self.assertAlmostEqual(y, 0, 5)
def PSOOperation(): global funEval for i in range(0, PopSize): for j in range(0, FF.D): # Choose two random numbers r1 = random.random() r2 = random.random() # Velocity update Pop[i].velocity[j] = w * Pop[i].velocity[j] + \ c1 * r1 * (Pop[i].pBest[j] - Pop[i].particle[j]) + \ c2 * r2 * (gBest[j] - Pop[i].particle[j]) if Pop[i].velocity[j] < vLB: Pop[i].velocity[j] = random.uniform(vLB, vUB) if Pop[i].velocity[j] > vUB: Pop[i].velocity[j] = random.uniform(vLB, vUB) # Particle update Pop[i].particle[j] = Pop[i].particle[j] + Pop[i].velocity[j] if Pop[i].particle[j] < FF.LB: Pop[i].particle[j] = round(random.uniform(FF.LB, FF.UB), 2) if Pop[i].particle[j] > FF.UB: Pop[i].particle[j] = round(random.uniform(FF.LB, FF.UB), 2) Pop[i].particleFitness = round(FF.MyFitFun(q, Pop[i].particle), 2) funEval = funEval + 1 # Select between particle and pBest if Pop[i].particleFitness <= Pop[i].pBestFitness: Pop[i].pBest = deepcopy(Pop[i].particle) Pop[i].pBestFitness = deepcopy(Pop[i].particleFitness)
def normalize_weight(): # Make sure that all the weights are not 0, so all components are evaluated FitnessFunction.srcWeight = 0.3 FitnessFunction.margWeight = 0.3 FitnessFunction.tarWeight = 0.3 src_err, diff_marg, tar_err = \ FitnessFunction.domain_differece(src_feature=Core.src_feature, src_label=Core.src_label, classifier=Core.classifier, tar_feature=Core.tar_feature) if diff_marg == 0: FitnessFunction.margWeight = 0 else: FitnessFunction.margWeight = 1.0 / abs(diff_marg) if tar_err == 0: FitnessFunction.tarWeight = 0 else: FitnessFunction.tarWeight = 1.0 / abs(tar_err) if src_err == 0: FitnessFunction.srcWeight = 0 else: FitnessFunction.srcWeight = 1.0/abs(src_err)
def run_swarm_process(self): # First we find the combination of creature that cover the space the more thoroughly. # To achieve that, we use KMEANS with k=2 on the list of creature position. kmeans = KMeans(n_clusters=2) swarm_positions = self._swarm.get_list_position() # Get the list of point in the space for KMeans # Normalize the dimension of each point so the point chosen is irrelevant of the possible different unities # of each dimensions normalized_swarm_position = np.array(swarm_positions) / np.array(self._bound_distance) kmeans.fit(normalized_swarm_position) # Train KMeans centers = kmeans.cluster_centers_ # Get the centers centers *= self._bound_distance # Go back to the original dimension print "Centers: ", centers # Add two new creatures with their position corresponding to the centers of kmeans. creature0_position = centers[0] creature0_fitness = FitnessFunction.calculate_fitness(self._fitness_function, creature0_position, self._number_of_dimensions) self._number_of_real_evaluation -= 1 # Did a real evaluation creature1_position = centers[1] creature1_fitness = FitnessFunction.calculate_fitness(self._fitness_function, creature1_position, self._number_of_dimensions) self._number_of_real_evaluation -= 1 # Did a real evaluation self._swarm.add_creature_to_swarm(position=creature0_position) self._swarm.add_creature_to_swarm(position=creature1_position) # Add the creatures position and fitness to the list of position and fitness evaluated self._list_real_evaluation_position.append(creature0_position) self._list_real_evaluation_fitness.append(creature0_fitness) self._list_real_evaluation_position.append(creature1_position) self._list_real_evaluation_fitness.append(creature1_fitness) # Train the regressor self._regressor.train_regressor(self._list_real_evaluation_position, self._list_real_evaluation_fitness) # From here, we alternate between exploration and exploitation randomly based on an heuristic except for the # Very first pass where we for the algorithm to be in exploration mode for one more evaluation # (3 evaluations total) self.exploration() self._number_of_real_evaluation -= 1 # Did a real evaluation # Now that we have three points evaluated, we are ready to start the algorithm for the requested amount of real # evaluations. Or until the user stop the program for generation in range(self._number_of_real_evaluation): print self._list_real_evaluation_position print self._list_real_evaluation_fitness print self._fitness_before_real_evalutation # Decide if we explore or exploite. exploitation_threshold = max(0.2, 1/math.sqrt((generation+2)/2)) exploitation_threshold = 0.0 if self._random.rand() < exploitation_threshold: best_creature_ever = self.exploration() # TODO once the exploitation algorithm is finish. Remove the if/else and move this block after # We are almost done with this generation, get the real value of the point of interest found new_point_to_add_fitness = FitnessFunction.calculate_fitness(self._fitness_function, best_creature_ever.get_position(), self._number_of_dimensions) # Finish the generation by adding the new creature to the list and updating the regressor self._list_real_evaluation_position.append(best_creature_ever.get_position()) self._list_real_evaluation_fitness.append(new_point_to_add_fitness) self._fitness_before_real_evalutation.append(self._regressor.get_estimation_fitness_value(best_creature_ever.get_position())[0]) choose_kernel = False if len(self._list_real_evaluation_fitness) % 10 == 0: choose_kernel = True self._regressor.train_regressor(self._list_real_evaluation_position, self._list_real_evaluation_fitness, choose_kernel=choose_kernel) print "Smallest point found: ", new_point_to_add_fitness, "Fitness found by the PSO:", \ best_creature_ever.get_fitness(), " At position: ", best_creature_ever.get_position() # Reset swarm fitness self._swarm.reset_swarm() else: best_creature_ever = self.exploitation() index = self._list_real_evaluation_fitness.index(min(self._list_real_evaluation_fitness)) print "Smallest point found: ", self._list_real_evaluation_fitness[index], " At position: ", \ self._list_real_evaluation_position[index]
def main(args): global SUPERVISED, NO_SUB_IND, toolbox #For elitism, at least the best individual #is recorded NO_ELI = (int)(POP_SIZE * GP_ELI) if NO_ELI < 10: NO_ELI = 10 filename = "iteration"+str(args[0])+".txt" file = open(filename,'w+') run_index = int(args[0]) supervised = int(args[1]) if supervised == 0: SUPERVISED = False else: SUPERVISED = True #setWeight() NO_SUB_IND = int(args[2]) toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.sub_individual, n=NO_SUB_IND) toolbox.register("population", tools.initRepeat, list, toolbox.individual, n=POP_SIZE) random.seed(1617**2*run_index) #FitnessFunction.setWeight(src_feature=Core.src_feature, src_label=Core.src_label, # tarU_feature=Core.tarU_feature, tarU_label=Core.tarU_soft_label) time_start = time.clock() pop = toolbox.population() hof = tools.HallOfFame(NO_ELI) #evaluate the population fitness = toolbox.map(toolbox.evaluate, pop) for ind, fit in zip(pop, fitness): ind.fitness.values = fit #Update the HoF hof.update(pop) towrite = "Supervised: %r \n" \ "Number of sub tree: %d\n" \ "Source weight: %f\n" \ "Diff source and target weight: %f\n" \ "Target weight: %g \n" % (SUPERVISED, NO_SUB_IND, FitnessFunction.srcWeight, FitnessFunction.margWeight, FitnessFunction.tarWeight) for gen in range(NGEN): print(gen) towrite = towrite + ("----Generation %i -----\n" %gen) #Select the next generation individuals #Leave space for elitism offspringS = toolbox.select(pop, len(pop)-NO_ELI) # Clone the selected individuals offspring = [toolbox.clone(ind) for ind in offspringS] #go through each individual for i in range(1, len(offspring), 2): if random.random() < GP_CXPB: #perform crossover for all the features first = offspring[i-1] second = offspring[i] first, second = crossoverEach(first, second) del first.fitness.values del second.fitness.values for i in range(len(offspring)): if random.random() < GP_MUTBP: parent = pop[i] for j in range(1, len(parent)): if random.random() < GP_MUTSUB: parent[j] = toolbox.mutate(parent[j]) del parent.fitness.values #Now put HOF back to offspring for ind in hof: offspring.append(toolbox.clone(ind)) # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in offspring if not ind.fitness.valid] fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit #Now update the hof for the next iteration hof.update(offspring) pop[:] = offspring # Gather all the fitnesses in one list and print the stats fits = [ind.fitness.values[0] for ind in pop] length = len(pop) mean = sum(fits) / length sum2 = sum(x*x for x in fits) std = abs(sum2 / length - mean**2)**0.5 towrite = towrite + (" Min %s\n" % min(fits)) towrite = towrite + (" Max %s\n" % max(fits)) towrite = towrite + (" Avg %s\n" % mean) towrite = towrite + (" Std %s\n" % std) bestInd = hof[0] funcs = [toolbox.compile(expr=tree) for tree in bestInd] src_feature = GPUtility.buildNewFeatures(Core.src_feature, funcs) tarU_feature = GPUtility.buildNewFeatures(Core.tarU_feature, funcs) tarL_feature = GPUtility.buildNewFeatures(Core.tarL_feature, funcs) if SUPERVISED: src_err, diff_marg, tar_err = FitnessFunction.domain_differece(src_feature=src_feature, src_label=Core.src_label, classifier=Core.classifier, tarU_feature=tarU_feature, tarU_soft_label=Core.tarU_soft_label, tarL_feature=tarL_feature, tarL_label=Core.tarL_label) else: src_err, diff_marg, tar_err = FitnessFunction.domain_differece(src_feature=src_feature, src_label=Core.src_label, classifier=Core.classifier, tarU_feature=tarU_feature, tarU_soft_label=Core.tarU_soft_label) towrite = towrite + (" Source Error: %f \n Diff Marg: %f \n Target Error: %f \n" %(src_err, diff_marg, tar_err)) acc = 1.0 - FitnessFunction.classification_error(training_feature=src_feature, training_label=Core.src_label, classifier=Core.classifier, testing_feature=tarU_feature, testing_label=Core.tarU_label) towrite = towrite + (" Accuracy on unlabel target: "+str(acc) + "\n") # Update the pseudo label and weight Core.classifier.fit(src_feature, Core.src_label) Core.tarU_soft_label = Core.classifier.predict(tarU_feature) #FitnessFunction.setWeight(Core.src_feature, Core.src_label, Core.tarU_feature, Core.tarU_SoftLabel) time_elapsed = (time.clock() - time_start) #process the result bestInd = hof[0] towrite = towrite + "----Final -----\n" funcs = [toolbox.compile(expr=tree) for tree in bestInd] src_feature = GPUtility.buildNewFeatures(Core.src_feature, funcs) tarU_feature = GPUtility.buildNewFeatures(Core.tarU_feature, funcs) acc = 1.0 - FitnessFunction.classification_error(training_feature=src_feature, training_label=Core.src_label, classifier=Core.classifier, testing_feature=tarU_feature, testing_label=Core.tarU_label) towrite = towrite + ("Accuracy on the target (TL): %f\n" % acc) towrite = towrite + "Accuracy on the target (No TL): %f\n" % ( 1.0 - FitnessFunction.classification_error(training_feature=Core.src_feature, training_label=Core.src_label, classifier=Core.classifier, testing_feature=Core.tarU_feature, testing_label=Core.tarU_label)) towrite = towrite + ("Computation time: %f\n" % time_elapsed) towrite = towrite + ("Number of features: %d\n" % len(bestInd)) file.write(towrite) file.close()
def evaluate(particle): A = np.copy(particle) A = np.reshape(A, (Core.A_row, Core.A_col)) return Fitness.fitness_function(A),
# Randomization #------------------------------------------------------------- # To solve optimization problem (minimization) using randomly. #------------------------------------------------------------- # Python version used: 2.6 / 2.7 #------------------------------------------------------------- #------------------------------------------------------------- # Step 1: Library Inclusion #------------------------------------------------------------- import random import time import FitnessFunction as FF # Fitness Function and Parameters from __main__ import * # Import 'q' variable from Merge.py where q defines which fitness function to use FF.INIT(q) # INIT Function in FitnessFunction File #------------------------------------------------------------- # Step 2: Random Algorithm Parameters #------------------------------------------------------------- AlgoName = "Random" + str(q) # Algo Name Iterations = 50 # Number of Iterations BestFitness = 9999999999 # Store Best Fitness Value BestChromosome = [] # Store Best Chromosome FunEval = 0 Runs_Random = [] # No of iterations Fitness_Random = [ ] # Set of sets of best fitness for all iterations in each run eg:fitnesses in 1st run --(15,12,6),fitnesses in 2nd run --(17,15,12) so it has [[15,12,6],[17,15,12]] Buffer = [] # Stores best fitness obtained in each run
def main(args): global i_stick, i_pbest, i_gbest, ustks run_index = int(args[0]) random.seed(1617**2 * run_index) marg_index = int(args[1]) tar_index = int(args[2]) FitnessFunction.margVersion = marg_index FitnessFunction.tarVersion = tar_index FitnessFunction.srcVersion = 1 filename = "iteration" + str(args[0]) + ".txt" output_file = open(filename, 'w+') time_start = time.clock() # Set the weight for each components in the fitness function # normalize_weight() FitnessFunction.srcWeight = 1.0 FitnessFunction.margWeight = 0.0 FitnessFunction.tarWeight = 1.0 # print("Start setting weight") # FitnessFunction.set_weight(Core.src_feature, Core.src_label, Core.tar_feature) # print(FitnessFunction.srcWeight, FitnessFunction.margWeight, FitnessFunction.tarWeight) # print("End setting") # opposite_weight() # Initialize population and the gbest pop = toolbox.population(n=NPART) best = None to_write = ( "Core classifier: %s\nSource weight: %f\nDiff source and target weight: %f\n" "Target weight: %g\nMarginal version: %d\nTarget version: %d\n" % (str(Core.classifier), FitnessFunction.srcWeight, FitnessFunction.margWeight, FitnessFunction.tarWeight, FitnessFunction.margVersion, FitnessFunction.tarVersion)) archive = [] for g in range(NGEN): print(g) to_write += ("=====Gen %d=====\n" % g) for part in pop: # Evaluate all particles part.fitness.values = toolbox.evaluate(part) if part.best is None or part.best.fitness < part.fitness: part.best = creator.Particle(part) part.best.fitness.values = part.fitness.values # update gbest if best is None or best.fitness < part.fitness: best = creator.Particle(part) best.fitness.values = part.fitness.values if TEST: print("is=", i_stick, "ip=", i_pbest, "ig=", i_gbest, "ustks=", ustks) print("best=", best) print(best.fitness.values) print("\n") for i, part in enumerate(pop): print("Particle %d: " % i) print("Particle position:", part) print("Particle pbest:", part.best) print("Particle stickiness:", part.stk) print("\n") archive.append(best) # now update the position of each particle for part in pop: toolbox.update(part, best) # Gather all the fitness components of the gbest and print the stats indices = [index for index, entry in enumerate(best) if entry == 1.0] src_feature = Core.Xs[:, indices] tar_feature = Core.Xt[:, indices] src_err, diff_marg, tar_err = \ FitnessFunction.domain_differece(src_feature=src_feature, src_label=Core.Ys, classifier=Core.classifier, tar_feature=tar_feature) to_write += ( " Source Error: %f \n Marginal Difference: %f \n Target Error: %f \n" % (src_err, diff_marg, tar_err)) to_write += (" Fitness function of real best: %f\n" % best.fitness.values[0]) acc = 1.0 - FitnessFunction.classification_error( training_feature=src_feature, training_label=Core.Ys, classifier=Core.classifier, testing_feature=tar_feature, testing_label=Core.Yt) to_write += (" Accuracy on unlabel target: " + str(acc) + "\n") to_write += " Position:" + str(best) + "\n" print(src_err, acc, best.fitness.values[0]) # update the parameters i_stick = is_up - (is_up - is_low) * (g + 1) / NGEN i_gbest = (1 - i_stick) / (pg_rate + 1) i_pbest = pg_rate * i_gbest ustks = ustks_low + (ustks_up - ustks_low) * (g + 1) / NGEN time_elapsed = (time.clock() - time_start) to_write += "----Final -----\n" indices = [index for index, entry in enumerate(best) if entry == 1.0] src_feature = Core.Xs[:, indices] tar_feature = Core.Xt[:, indices] if WRITE_OUT: src_full = np.concatenate( (src_feature, np.reshape(Core.Ys, (Core.Ys.shape[0], 1))), axis=1) tar_full = np.concatenate( (tar_feature, np.reshape(Core.Yt, (Core.Yt.shape[0], 1))), axis=1) np.savetxt("OutGP/Source", src_full, delimiter=",") np.savetxt("OutGP/Target", tar_full, delimiter=",") nf_file = open("OutGP/noFeatures", "w") nf_file.write(str(len(indices))) acc = 1.0 - FitnessFunction.classification_error( training_feature=src_feature, training_label=Core.Ys, classifier=Core.classifier, testing_feature=tar_feature, testing_label=Core.Yt) to_write += ("Accuracy of the core classifier: " + str(acc) + "\n") to_write += ("Accuracy on the target (No TL) (core classifier): %f\n\n" % (1.0 - FitnessFunction.classification_error( training_feature=Core.ori_src_feature, training_label=Core.Ys, classifier=Core.classifier, testing_feature=Core.ori_tar_feature, testing_label=Core.Yt))) new_classifier = LinearSVC(random_state=1617) acc = 1.0 - FitnessFunction.classification_error( training_feature=src_feature, training_label=Core.Ys, classifier=new_classifier, testing_feature=tar_feature, testing_label=Core.Yt) to_write += ("Accuracy of the Linear SVM classifier: " + str(acc) + "\n") to_write += ("Accuracy on the target (No TL) of Linear SVM: %f\n\n" % (1.0 - FitnessFunction.classification_error( training_feature=Core.ori_src_feature, training_label=Core.Ys, classifier=new_classifier, testing_feature=Core.ori_tar_feature, testing_label=Core.Yt))) new_classifier = DecisionTreeClassifier(random_state=1617) acc = 1.0 - FitnessFunction.classification_error( training_feature=src_feature, training_label=Core.Ys, classifier=new_classifier, testing_feature=tar_feature, testing_label=Core.Yt) to_write += ("Accuracy of the Linear DT classifier: " + str(acc) + "\n") to_write += ("Accuracy on the target (No TL) of DT: %f\n\n" % (1.0 - FitnessFunction.classification_error( training_feature=Core.ori_src_feature, training_label=Core.Ys, classifier=new_classifier, testing_feature=Core.ori_tar_feature, testing_label=Core.Yt))) new_classifier = GaussianNB() acc = 1.0 - FitnessFunction.classification_error( training_feature=src_feature, training_label=Core.Ys, classifier=new_classifier, testing_feature=tar_feature, testing_label=Core.Yt) to_write += ("Accuracy of the Linear NB classifier: " + str(acc) + "\n") to_write += ("Accuracy on the target (No TL) of NB: %f\n\n" % (1.0 - FitnessFunction.classification_error( training_feature=Core.ori_src_feature, training_label=Core.Ys, classifier=new_classifier, testing_feature=Core.ori_tar_feature, testing_label=Core.Yt))) to_write += ("Computation time: %f\n" % time_elapsed) to_write += ("Number of features: %d\n" % len(indices)) to_write += str(best) output_file.write(to_write) output_file.close()
def main(args): global i_stick, i_pbest, i_gbest, ustks, SUPERVISED run_index = int(args[0]) random.seed(1617**2 * run_index) time_start = time.clock() SUPERVISED = False #supervised = int(args[1]) #if supervised == 0: # SUPERVISED = False #else: # SUPERVISED = True cond_index = 1 FitnessFunction.tarVersion = cond_index # set weight for normalization # setWeight() # after normalization, we assign a weight to that normalized values # weight = int(args[1])/10.0 FitnessFunction.margWeight = 0.0 # weight*FitnessFunction.margWeight FitnessFunction.tarWeight = 0.9 # (1.0-weight)*FitnessFunction.condWeight FitnessFunction.srcWeight = 0.1 filename = args[0] + "_" + args[1] + ".txt" file = open("Optimize/" + filename, 'w+') # Initialize population and the gbest pop = toolbox.population(n=NPART) best = None toWrite = ("Supervised: %r \n" \ "Source weight: %f\n" \ "Diff source and target weight: %f\n" \ "Target weight: %g\n" \ "Conditional version: %d\n" % (SUPERVISED, FitnessFunction.srcWeight, FitnessFunction.margWeight, FitnessFunction.tarWeight, FitnessFunction.tarVersion)) for g in range(NGEN): toWrite += ("=====Gen %d=====\n" % g) for part in pop: # Evaluate all particles part.fitness.values = toolbox.evaluate(part) if part.best is None or part.best.fitness < part.fitness: part.best = creator.Particle(part) part.best.fitness.values = part.fitness.values # update gbest if best is None or best.fitness < part.fitness: best = creator.Particle(part) best.fitness.values = part.fitness.values if TEST: print("is=", i_stick, "ip=", i_pbest, "ig=", i_gbest, "ustks=", ustks) print("best=", best) print(best.fitness.values) print("\n") for i, part in enumerate(pop): print("Particle %d: " % i) print("Particle position:", part) print("Particle pbest:", part.best) print("Particle stickiness:", part.stk) print("\n") # now update the position of each particle for part in pop: toolbox.update(part, best) # Gather all the fitness components of the gbest and print the stats indices = [index for index, entry in enumerate(best) if entry == 1.0] src_feature = Core.src_feature[:, indices] tarU_feature = Core.tarU_feature[:, indices] tarL_feature = Core.tarL_feature[:, indices] if SUPERVISED: src_err, diff_marg, tar_err = FitnessFunction.domain_differece( src_feature=src_feature, src_label=Core.src_label, classifier=Core.classifier, tarU_feature=tarU_feature, tarL_feature=tarL_feature, tarL_label=Core.tarL_label) else: src_err, diff_marg, tar_err = FitnessFunction.domain_differece( src_feature=src_feature, src_label=Core.src_label, classifier=Core.classifier, tarU_feature=tarU_feature) toWrite += ( " Source Error: %f \n Diff Marg: %f \n Target Error: %f \n" % (src_err, diff_marg, tar_err)) toWrite += (" Fitness function of real best: %f\n" % best.fitness.values[0]) # update the parameters i_stick = is_up - (is_up - is_low) * (g + 1) / NGEN i_gbest = (1 - i_stick) / (pg_rate + 1) i_pbest = pg_rate * i_gbest ustks = ustks_low + (ustks_up - ustks_low) * (g + 1) / NGEN time_elapsed = (time.clock() - time_start) toWrite += "----Final -----\n" indices = [index for index, entry in enumerate(best) if entry == 1.0] src_feature = Core.src_feature[:, indices] tarU_feature = Core.tarU_feature[:, indices] src_err = FitnessFunction.nfold_classification_error( src_feature, Core.src_label, Core.classifier, 10) toWrite += ("Accuracy on source: " + str(1 - src_err) + "\n") toWrite += ("Number of features: %d\n" % len(indices)) toWrite += str(best) file.write(toWrite) file.close()
#------------------------------------------------------------- # To solve optimization problem (minimization) using GA. #------------------------------------------------------------- # Python version used: 2.6 / 2.7 #------------------------------------------------------------- #------------------------------------------------------------- # Step 1: Library Inclusion #------------------------------------------------------------- import random import time from copy import deepcopy import FitnessFunction as FF # Fitness Function and Parameters from __main__ import * # Import 'q' variable from Merge.py where q defines which fitness function to use FF.INIT(q) # INIT Function in FitnessFunction File #------------------------------------------------------------- # Step 2: GA parameters #------------------------------------------------------------- AlgoName = "GA" + str(q) # Algo Name Iterations = 50 # Number of Iterations PopSize = 10 # Population Size(i.e Number of Chromosomes) Pop = [] # Store Population with Fitness CR = 0.9 # Crossover Rate MR = 0.8 # Mutation Rate MaxFunEval = 100000 # Maximum allowable function evaluations FunEval = 0 # Count function evaluations GlobalBest = [] # Rember Global Best after every iteration Runs_GA = [] # No of iterations
def evaluate(particle): beta = np.copy(particle) beta = np.reshape(beta, (len(Core.Xs) + len(Core.Xt), Core.C)) fitness = FitnessFunction.fitness_function(beta) return fitness,