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 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),
Beispiel #3
0
def evaluate(particle):
    A = np.copy(particle)
    A = np.reshape(A, (Core.A_row, Core.A_col))
    return Fitness.fitness_function(A),
Beispiel #4
0
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,
def main(args):
    global i_stick, i_pbest, i_gbest, ustks, SUPERVISED

    run_index = int(args[0])
    random.seed(1617 ** 2 * run_index)
    filename = "iteration"+str(args[0])+".txt"
    file = open(filename, 'w+')

    time_start = time.clock()

    SUPERVISED = False
    #supervised = int(args[1])

    #if supervised == 0:
    #    SUPERVISED = False
    #else:
    #    SUPERVISED = True

    cond_index = int(args[1])
    FitnessFunction.tarVersion = cond_index

    #setWeight()
    #FitnessFunction.setWeight(Core.src_feature, Core.src_label, Core.tarU_feature, Core.tarU_soft_label)

    # Set the weight for each components in the fitness function
    #FitnessFunction.setWeight(src_feature=Core.src_feature, src_label=Core.src_label,
    #                          tarU_feature=Core.tarU_feature, tarU_label=Core.tarU_soft_label)
    FitnessFunction.srcWeight = 0.0
    FitnessFunction.margWeight = 1.0
    FitnessFunction.tarWeight = 0.0

    # 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):
        print(g)
        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])
        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 += ("  Accuracy on unlabel target: " + str(acc) + "\n")
        toWrite += "  Position:"+str(best)+"\n"


        # 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

        # Update the pseudo label (only when the cond_index is equal to 2)
        if cond_index == 3 & g % 10==0:
            Core.classifier.fit(src_feature, Core.src_label)
            Core.tarU_soft_label = Core.classifier.predict(tarU_feature)
            FitnessFunction.set_weight(src_feature, Core.src_label, tarU_feature, Core.tarU_soft_label)
            # Need to update the fitness value of best and pbest again
            best.fitness.values = FitnessFunction.fitness_function(src_feature, Core.src_label,
                                                                   tarU_feature, Core.tarU_soft_label,
                                                                   Core.classifier),
            for part in pop:
                indices = [index for index, entry in enumerate(part.best) if entry == 1.0]
                p_src_feature = Core.src_feature[:, indices]
                p_tarU_feature = Core.tarU_feature[:, indices]
                part.best.fitness.values = FitnessFunction.fitness_function(p_src_feature, Core.src_label,
                                                                            p_tarU_feature, Core.tarU_soft_label,
                                                                            Core.classifier),

    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]
    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 += ("Accuracy on unlabel target: " + str(acc) + "\n")
    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 += ("Computation time: %f\n" % time_elapsed)
    toWrite += ("Number of features: %d\n" % len(indices))
    toWrite += str(best)

    file.write(toWrite)
    file.close()