Esempio n. 1
0
def getTrainingSetNeighbors(training_directions, training_individuals, individuals_objectives, z_star, population_size, training_neighborhood_size):
    individual_id    = -1
    training_inputs  = []
    training_outputs = []
    training_scores  = []

    for individual in training_individuals:
        individual_id += 1

        if(individual in training_individuals[0:individual_id]):
            continue

        neighbors_directions = getTrainingNeighborsInclusive(individual_id, training_neighborhood_size, population_size)

        for direction in neighbors_directions:
            current_dir = training_directions[direction].tolist()[0]
            training_input = []
            training_input.extend(current_dir)
            training_input.extend(individual)

            training_output = eval_to.g_tcheby(current_dir, individuals_objectives[individual_id], z_star)

            training_inputs.append(training_input)
            training_outputs.append(training_output)
            training_scores.append(individuals_objectives[individual_id])

    return training_inputs, training_outputs, len(training_inputs), training_scores
Esempio n. 2
0
def retrainSet(training_inputs, training_scores, z_star, nb_inputs, nb_objectives):
    new_outputs = []
    for i in range(nb_inputs):
        new_output = eval_to.g_tcheby(training_inputs[i][0:nb_objectives], training_scores[i], z_star)
        new_outputs.append(new_output)

    return new_outputs
Esempio n. 3
0
def getTrainingSetAll(training_directions, training_individuals, individuals_objectives, z_star):
    individual_id    = -1
    training_inputs  = []
    training_outputs = []
    training_scores  = []

    for individual in training_individuals:
        individual_id += 1

        if(individual in training_individuals[0:individual_id]):
            continue

        for direction in training_directions:
            current_dir = direction.tolist()[0]
            training_input = []
            training_input.extend(current_dir)
            training_input.extend(individual)

            training_output = eval_to.g_tcheby(current_dir, individuals_objectives[individual_id], z_star)

            training_inputs.append(training_input)
            training_outputs.append(training_output)
            training_scores.append(individuals_objectives[individual_id])

    return training_inputs, training_outputs, len(training_inputs), training_scores
Esempio n. 4
0
def predict_and_quality(model, data_free, data_pred, start_fct, problem_size, g, d, nb_fct):
    w = data_free[0:nb_fct]
    offs = data_free[nb_fct:]
    score_freeeval = eval_to.free_eval(start_fct, offs, problem_size)
    tcheby_freeeval = eval_to.g_tcheby(w, score_freeeval, train_to.getTrainingZstar())
    tcheby_predict  = model.predict(data_pred)
    qual_to.add(g, d, tcheby_predict[0], tcheby_freeeval)
    return tcheby_predict[0]
def scorefunction_BestScalar(current_predobjvector, current_freeobjvector, neighborhood_weightvectors, objective_quantity, z_star, neighborhood_size):
    global MAX_INTEGER

    #scores
    pred_bestscore = MAX_INTEGER
    free_bestscore = MAX_INTEGER

    #for each weight vector
    for current_weightvector in neighborhood_weightvectors:
        # get tchebycheff for the current weight vector
        current_predtcheby = eval_to.g_tcheby(current_weightvector, current_predobjvector, z_star)
        current_freetcheby = eval_to.g_tcheby(current_weightvector, current_freeobjvector, z_star)
        # keep the best
        pred_bestscore = min(pred_bestscore, current_predtcheby)
        free_bestscore = min(free_bestscore, current_freetcheby)

    #return the scores
    return pred_bestscore, free_bestscore
def scorefunction_AverageImprovement(current_predobjvector, current_freeobjvector, neighborhood_weightvectors, objective_quantity, z_star, neighborhood_size):

    #prepare the population for prediction
    current_bestsolution = np.array(global_population)
    #get the predicted score for the current best solution with the present models
    current_mainbestsolutionpredictedobjectivevector = [global_modeltab[m].predict(current_bestsolution) for m in range(objective_quantity)]
    #scores
    pred_averagescoresum = 0.0
    free_averagescoresum = 0.0

    #for each weight vector
    for loop_neighbor in range(int(neighborhood_size)):
        # get current neighbor's id
        current_neighbor = global_neighborhoodindexlist[loop_neighbor]

        # get current neighbor's weight vector
        current_weightvector = neighborhood_weightvectors[loop_neighbor]

        # get tchebycheff for the current weight vector
        current_predtcheby = eval_to.g_tcheby(current_weightvector, current_predobjvector, z_star)
        current_freetcheby = eval_to.g_tcheby(current_weightvector, current_freeobjvector, z_star)

        # get the current direction's best solution scores for the current neighbor
        current_neighborobjectivevector = global_populationscore[current_neighbor]

        # get the tchebytcheff of the current best solution for the current neighbor weight vector
        main_predtcheby = eval_to.g_tcheby(current_weightvector, [current_mainbestsolutionpredictedobjectivevector[m][current_neighbor] for m in range(objective_quantity)], z_star)
        main_freetcheby = eval_to.g_tcheby(current_weightvector, current_neighborobjectivevector, z_star)

        # compute improvement
        current_predimprovement = max(0.0, main_predtcheby - current_predtcheby)
        current_freeimprovement = max(0.0, main_freetcheby - current_freetcheby)

        # keep the best
        pred_averagescoresum += current_predimprovement
        free_averagescoresum += current_freeimprovement

        #ponderation of the sums to get the average scores
        pred_averagescore = pred_averagescoresum / neighborhood_size
        free_averagescore = free_averagescoresum / neighborhood_size

    #return the scores
    return pred_averagescore, free_averagescore
def scorefunction_AverageScalar(current_predobjvector, current_freeobjvector, neighborhood_weightvectors, objective_quantity, z_star, neighborhood_size):
    #scores
    current_predscoresum = 0.0
    current_freescoresum = 0.0

    #for each weight vector
    for current_weightvector in neighborhood_weightvectors:
        # get tchebycheff for the current weight vector
        current_predtcheby = eval_to.g_tcheby(current_weightvector, current_predobjvector, z_star)
        current_freetcheby = eval_to.g_tcheby(current_weightvector, current_freeobjvector, z_star)
        # sum for average
        current_predscoresum += current_predtcheby
        current_freescoresum += current_freetcheby

    #ponderation of the sums to get the average scores
    pred_averagescore = current_predscoresum / neighborhood_size
    free_averagescore = current_freescoresum / neighborhood_size

    #return the scores
    return pred_averagescore, free_averagescore
def get2ModelsTrainingSetAll(training_directions, training_individuals, individuals_objectives, z_star):
    individual_id    = -1
    training_inputs  = []
    training_outputs = []

    for individual in training_individuals:
        individual_id += 1

        for direction in training_directions:
            current_dir = direction.tolist()[0]
            training_input = []
            training_input.extend(current_dir)
            training_input.extend(individual)

            training_output = eval_to.g_tcheby(current_dir, individuals_objectives[individual_id], z_star)

            training_inputs.append(training_input)
            training_outputs.append(training_output)
    return training_inputs, training_outputs
Esempio n. 9
0
def getFifoBasedTrainingSet(directions, training_individuals, individuals_objectives, individual_directions ,z_star, strategy, size, training_neighborhood_size):
    training_inputs  = []
    training_outputs = []
    training_scores  = []

    for individual_id in range(0, size):
        if(training_individuals[individual_id] in training_individuals[0:individual_id]):
            continue

        direction = directions[individual_directions[individual_id]]
        current_dir = direction.tolist()[0]
        training_input = []
        training_input.extend(current_dir)
        training_input.extend(training_individuals[individual_id])

        training_output = eval_to.g_tcheby(current_dir, individuals_objectives[individual_id], z_star)

        training_inputs.append(training_input)
        training_outputs.append(training_output)
        training_scores.append(individuals_objectives[individual_id])

    return training_inputs, training_outputs, len(training_inputs), training_scores
Esempio n. 10
0
def runTcheby():
    global param, approx_pareto_front, archiveOK, NO_FILE_TO_WRITE

    ############################################################################
    # PARAMETER

    # clf = SVR(C=1.0, epsilon=0.1, kernel="rbf")
    clf = NuSVR()
    clf2 = -1
    two_models_bool = False

    isReals = True
    start_fct, nb_functions = param[0:2]
    nb_iterations, neighboring_size = param[2:4]
    init_decisions, problem_size = param[4:6]
    max_decisions_maj, delta_neighbourhood = param[6:8]
    CR, search_space = param[8:10]
    F, distrib_index_n = param[10:12]
    pm, operator_fct = param[12:14]
    nb_samples, training_neighborhood_size = param[14:16]
    strategy, file_to_write = param[16:18]
    filter_strat, free_eval = param[18:20]
    param_print_every, file_to_writeR2 = param[20:22]
    filenameDIR, filenameSCORE = param[22:24]

    nb_objectives = len(start_fct)

    # get separatly offspring operator fct
    crossover_fct, mutation_fct, repair_fct = operator_fct

    best_decisions = copy.deepcopy(init_decisions)

    sampling_param = [
        crossover_fct,
        mutation_fct,
        repair_fct,
        best_decisions,
        F,
        problem_size,
        CR,
        search_space,
        distrib_index_n,
        pm,
    ]

    ############################################################################
    # INITIALISATION

    qual_tools.resetGlobalVariables(filenameDIR, filenameSCORE, nb_iterations, nb_functions)

    eval_to.resetEval()

    # get the directions weight for both starting functions
    directions = dec.getDirections(nb_functions, nb_objectives)

    # init the neighboring constant
    nt.initNeighboringTab(nb_functions, neighboring_size, directions, nb_objectives)

    # giving global visibility to the best_decisions to get the result at the end
    approx_pareto_front = best_decisions

    # initial best decisions scores
    best_decisions_scores = [eval_to.free_eval(start_fct, best_decisions[i], problem_size) for i in range(nb_functions)]

    pop_size = nb_functions

    # current optimal scores for both axes
    z_opt_scores = gt.getMinTabOf(best_decisions_scores)

    eval_to.initZstar(z_opt_scores)

    # get the first training part of the item we will learn on
    model_directions = train_to.getDirectionsTrainingMatrix(directions)

    # if the data shall be write in a file
    writeOK = False
    if file_to_write != NO_FILE_TO_WRITE:
        writeOK = True

    writeR2OK = False
    if file_to_writeR2 != NO_FILE_TO_WRITE:
        writeR2OK = True

    ############################################################################
    # MAIN ALGORITHM

    if writeOK:
        iot.printObjectives(file_to_write, eval_to.getNbEvals(), 0, best_decisions_scores, problem_size, nb_objectives)

    # IDs tab to allow a random course through the directions in the main loop
    id_directions = [i for i in range(nb_functions)]

    # iterations loop
    for itera in range(nb_iterations):
        if not free_eval:
            # Update model
            training_inputs, training_outputs, training_set_size, training_scores = train_to.getTrainingSet(
                model_directions,
                best_decisions,
                best_decisions_scores,
                eval_to.getZstar_with_decal(),
                strategy,
                nb_functions,
                training_neighborhood_size,
            )

            clf.fit(training_inputs, training_outputs)

        """
        if(writeR2OK and not free_eval):
            training_inputs_tcheby      = eval_to.getManyTcheby(training_inputs, training_scores, eval_to.getZstar_with_decal(), training_set_size)

            random_index = numpy.arange(0,training_set_size)
            numpy.random.shuffle(random_index)
            n_folds = 10
            folds_sizes = (training_set_size // n_folds) * numpy.ones(n_folds, dtype=numpy.int)
            folds_sizes[:training_set_size % n_folds] += 1

            training_inputs_array = numpy.array(training_inputs)
            training_tcheby_array = numpy.array(training_inputs_tcheby)

            R2_cv = []
            MSE_cv = []
            MAE_cv = []
            MDAE_cv = []

            clfCV = NuSVR()

            current = 0
            for fold_size in folds_sizes:
                start, stop = current, current + fold_size
                mask = numpy.ones(training_set_size, dtype=bool)
                mask[start:stop] = 0
                current = stop

                clfCV.fit(training_inputs_array[random_index[mask]], training_tcheby_array[random_index[mask]])

                test_fold_tcheby = training_tcheby_array[random_index[start:stop]]
                test_fold_predict = clfCV.predict(training_inputs_array[random_index[start:stop]])

                R2_cv  .append(r2_score             (test_fold_tcheby, test_fold_predict))
                MSE_cv .append(mean_squared_error   (test_fold_tcheby, test_fold_predict))
                MAE_cv .append(mean_absolute_error  (test_fold_tcheby, test_fold_predict))
                MDAE_cv.append(median_absolute_error(test_fold_tcheby, test_fold_predict))

            R2 = clf.score(training_inputs, training_outputs)
            MSE_cv_mean = numpy.mean(MSE_cv)
            RMSE_cv_mean = math.sqrt(MSE_cv_mean)
            MAE_cv_mean = numpy.mean(MAE_cv)
            MDAE_cv_mean = numpy.mean(MDAE_cv)
            R2_cv_mean = numpy.mean(R2_cv)

            iot.printR2(file_to_writeR2, eval_to.getNbEvals(), itera,  R2, R2_cv_mean, MSE_cv_mean , MAE_cv_mean, MDAE_cv_mean, RMSE_cv_mean, problem_size, print_every=1)

        """

        # random course through the directions
        random.shuffle(id_directions)

        # functions loop
        for f in id_directions:

            # get all the indice of neighbors of a function in a certain distance of f and include f in
            f_neighbors, current_neighbourhing_size = nt.getNeighborsOf(f, delta_neighbourhood)

            # get a list of offspring from the neighbors
            list_offspring = samp_to.extended_sampling(f, f_neighbors, sampling_param, nb_samples)

            # apply a filter on the offspring list and select the best one
            filter_param = [
                itera,
                f,
                clf,
                clf2,
                two_models_bool,
                f_neighbors,
                list_offspring,
                model_directions,
                start_fct,
                problem_size,
                eval_to.getZstar_with_decal(),
                best_decisions_scores,
                best_decisions,
                nb_objectives,
            ]
            best_candidate = filt_to.model_based_filtring(filter_strat, free_eval, filter_param)

            # evaluation of the newly made solution
            mix_scores = eval_to.eval(start_fct, best_candidate, problem_size)

            # MAJ of the z_star point
            has_changed = eval_to.min_update_Z_star(mix_scores, nb_objectives)

            # retraining of the model with the new z_star
            if has_changed and not free_eval:
                train_to.updateTrainingZstar(eval_to.getZstar_with_decal())
                training_outputs = train_to.retrainSet(
                    training_inputs, training_scores, eval_to.getZstar_with_decal(), training_set_size, nb_objectives
                )
                clf.fit(training_inputs, training_outputs)

            # boolean that is True if the offspring has been add to the archive
            added_to_S = False

            # count how many best decisions has been changed by the newly offspring
            cmpt_best_maj = 0

            # random course through the neighbors list
            random.shuffle(f_neighbors)

            # course through the neighbors list
            for j in f_neighbors:

                # stop if already max number of remplacement reach
                if cmpt_best_maj >= max_decisions_maj:
                    break

                # compute g_tcheby
                # wj = (directions[0][j],directions[1][j])
                wj = [directions[obj][j] for obj in range(0, nb_objectives)]
                g_mix = eval_to.g_tcheby(wj, mix_scores, eval_to.getZstar_with_decal())
                g_best = eval_to.g_tcheby(wj, best_decisions_scores[j], eval_to.getZstar_with_decal())

                # if the g_tcheby of the new solution is less distant from the z_optimal solution than the current best solution of the function j
                if g_mix < g_best:
                    cmpt_best_maj += 1
                    best_decisions[j] = best_candidate
                    best_decisions_scores[j] = mix_scores

                    # if we manage the archive and the solution have not been add already
                    if archiveOK and not (added_to_S):
                        arch_to.archivePut(best_candidate, mix_scores)
                        added_to_S = True
        # print("Update", itera, "done.")

        # if manage archive
        if archiveOK:
            arch_to.maintain_archive()

        # if write the result in a file
        if writeOK:
            iot.printObjectives(
                file_to_write,
                eval_to.getNbEvals(),
                itera + 1,
                best_decisions_scores,
                problem_size,
                nb_objectives,
                print_every=param_print_every,
            )
            continue

        # graphic update
        # yield arch_to.getArchiveScore(), best_decisions_scores, itera+1, eval_to.getNbEvals(), eval_to.getZstar_with_decal(), pop_size, isReals
    if not free_eval and writeR2OK:
        qual_tools.computeQualityEvaluation()
        qual_tools.generateDiffPredFreeFile()
    return
def runTcheby():
    global param, approx_pareto_front, archiveOK, NO_FILE_TO_WRITE

    ############################################################################
    # PARAMETER

    isReals = True
    start_fct, nb_functions                = param[0:2]
    nb_iterations, neighboring_size        = param[2:4]
    init_decisions, problem_size           = param[4:6]
    max_decisions_maj, delta_neighbourhood = param[6:8]
    CR, search_space                       = param[8:10]
    F, distrib_index_n                     = param[10:12]
    pm, operator_fct                       = param[12:14]
    file_to_write, param_print_every       = param[14:16]


    nb_objectives = len(start_fct)

    #get separatly offspring operator fct
    crossover_fct, mutation_fct, repair_fct = operator_fct

    best_decisions = copy.deepcopy(init_decisions)

    sampling_param = [crossover_fct, mutation_fct, repair_fct, best_decisions, F, problem_size, CR, search_space, distrib_index_n, pm]

    ############################################################################
    # INITIALISATION

    eval_to.resetEval()

    #get the directions weight for both starting functions
    directions = dec.getDirections(nb_functions, nb_objectives)

    #init the neighboring constant
    nt.initNeighboringTab(nb_functions, neighboring_size, directions, nb_objectives)

    #giving global visibility to the best_decisions to get the result at the end
    approx_pareto_front = best_decisions

    #initial best decisions scores
    best_decisions_scores = [eval_to.free_eval(start_fct, best_decisions[i], problem_size) for i in range(nb_functions)]

    pop_size = nb_functions

    #current optimal scores for both axes
    z_opt_scores = gt.getMinTabOf(best_decisions_scores)

    eval_to.initZstar(z_opt_scores)

    #if the data shall be write in a file
    writeOK = False
    if(file_to_write != NO_FILE_TO_WRITE):
        writeOK = True

    ############################################################################
    # MAIN ALGORITHM

    if(writeOK):
        iot.printObjectives(file_to_write, eval_to.getNbEvals(), 0, best_decisions_scores, problem_size, nb_objectives)

    #IDs tab to allow a random course through the directions in the main loop
    id_directions = [i for i in range(nb_functions)]

    #iterations loop
    for itera in range(nb_iterations):

        #random course through the directions
        random.shuffle(id_directions)

        #functions loop
        for f in id_directions:

            #get all the indice of neighbors of a function in a certain distance of f and include f in
            f_neighbors, current_neighbourhing_size = nt.getNeighborsOf(f, delta_neighbourhood)

            #generate a new valide offspring
            mix_ter = samp_to.sampling(f, f_neighbors, sampling_param)

            #evaluation of the newly made solution
            mix_scores = eval_to.eval(start_fct, mix_ter, problem_size)

            #MAJ of the z_star point
            has_changed = eval_to.min_update_Z_star(mix_scores, nb_objectives)

            #boolean that is True if the offspring has been add to the archive
            added_to_S = False

            #count how many best decisions has been changed by the newly offspring
            cmpt_best_maj = 0

            #random course through the neighbors list
            random.shuffle(f_neighbors)

            #course through the neighbors list
            for j in f_neighbors:

                #stop if already max number of remplacement reach
                if(cmpt_best_maj >= max_decisions_maj):
                    break

                #compute g_tcheby
                #wj = (directions[0][j],directions[1][j])
                wj = [directions[obj][j] for obj in range(0,nb_objectives)]
                g_mix = eval_to.g_tcheby(wj, mix_scores, eval_to.getZstar_with_decal())
                g_best = eval_to.g_tcheby(wj, best_decisions_scores[j], eval_to.getZstar_with_decal())
                #if the g_tcheby of the new solution is less distant from the z_optimal solution than the current best solution of the function j
                if( g_mix < g_best):
                    cmpt_best_maj += 1
                    best_decisions[j] = mix_ter
                    best_decisions_scores[j] = mix_scores
                    #if we manage the archive and the solution have not been add already
                    if(archiveOK and not(added_to_S)):
                       arch_to.archivePut(mix_ter, mix_scores)
                       added_to_S = True
        #print("Update", itera, "done.")

        #if manage archive
        if(archiveOK):
           arch_to.maintain_archive()

        #if write the result in a file
        if(writeOK):
            iot.printObjectives(file_to_write, eval_to.getNbEvals(), itera+1, best_decisions_scores, problem_size, nb_objectives, print_every=param_print_every)
            continue

        #graphic update
        #yield arch_to.getArchiveScore(), best_decisions_scores, itera, eval_to.getNbEvals(), eval_to.getZstar_with_decal(), pop_size, isReals

    return
Esempio n. 12
0
def computeTchebyFreeEval(data, start_fct, problem_size, z_star, nb_fct):
    w = data[0:nb_fct]
    offs = data[nb_fct:]
    score_eval = eval_to.free_eval(start_fct, offs, problem_size)
    return eval_to.g_tcheby(w, score_eval, z_star)
Esempio n. 13
0
def NumberOfImprovement(free_eval, param, withTruescore):

    current_g, current_f, model, model2, two_models_bool, f_neighbors, list_offspring, model_directions, start_fct, problem_size, z_star, population_scores, population_indiv, nb_fct = param


    id_offspring = -1
    offspring_result_pred = []
    offspring_result_free = []

    for offspring in list_offspring:
        id_offspring += 1
        numberdir_score_pred = 0
        numberdir_score_free = 0

        f_input_data = getInputData(f_neighbors, model_directions, offspring)
        if(not free_eval):
           f_input_data_pred = np.matrix(f_input_data)
           f = 0
           for data in f_input_data_pred:
               tmp_pred = predict_and_quality(model, f_input_data[f], data, start_fct, problem_size, current_g, f_neighbors[f], nb_fct)
               tmp_free = computeTchebyFreeEval(f_input_data[f], start_fct, problem_size, z_star, nb_fct)
               if(withTruescore):
                   current_gtcheby = eval_to.g_tcheby(model_directions[f_neighbors[f]].tolist()[0], population_scores[f_neighbors[f]], z_star)
               else:
                   current_gtcheby = model.predict([f_input_data[f][0:nb_fct] + population_indiv[f_neighbors[f]]])[0]

               if(current_gtcheby > tmp_pred):
                   numberdir_score_pred += 1
               if(current_gtcheby > tmp_free):
                   numberdir_score_free += 1

               f +=1
           offspring_result_pred.append(numberdir_score_pred)
        else:
            f = 0
            for data in f_input_data:
                tmp_free = computeTchebyFreeEval(data, start_fct, problem_size, z_star, nb_fct)
                current_gtcheby = eval_to.g_tcheby(model_directions[f_neighbors[f]].tolist()[0] , population_scores[f_neighbors[f]], z_star)
                if(current_gtcheby > tmp_free):
                    numberdir_score_free += 1
                f +=1

        offspring_result_free.append(numberdir_score_free)

    score_best_free = max(offspring_result_free)
    best_indexes_free = [i for i, j in enumerate(offspring_result_free) if j == score_best_free]
    choice_free = random.choice(best_indexes_free)

    choice = -1
    if(free_eval):
        choice = choice_free
    else:
        score_best_pred = max(offspring_result_pred)
        best_indexes_pred = [i for i, j in enumerate(offspring_result_pred) if j == score_best_pred]
        choice_pred = random.choice(best_indexes_pred)
        save_best_free_pred_score = offspring_result_pred[choice_free]
        save_best_pred_free_score = offspring_result_free[choice_pred]
        choice = choice_pred
        diffFreePredict(current_g, current_f, score_best_pred, save_best_pred_free_score, choice_pred, score_best_free, save_best_free_pred_score,  choice_free)

    return list_offspring[choice]
Esempio n. 14
0
def AverageScalar(free_eval, param, normalize, withTruescore):
    global MAX_INTEGER

    current_g, current_f, model, model2, two_models_bool, f_neighbors, list_offspring, model_directions, start_fct, problem_size, z_star, population_scores, population_indiv, nb_fct = param


    id_offspring = -1
    score_best_pred = MAX_INTEGER
    save_best_pred_free_score = -1
    score_best_free = MAX_INTEGER
    save_best_free_pred_score = -1

    index_best_free_list = [-1]
    index_best_pred_list = [-1]

    for offspring in list_offspring:
        id_offspring += 1
        average_score_pred = 0
        average_score_free = 0

        f_input_data = getInputData(f_neighbors, model_directions, offspring)
        count = 0
        if(not free_eval):
           f_input_data_pred = np.matrix(f_input_data)
           f = 0
           for data in f_input_data_pred:
               tmp_pred = predict_and_quality(model, f_input_data[f], data, start_fct, problem_size, current_g, f_neighbors[f], nb_fct)
               tmp_free = computeTchebyFreeEval(f_input_data[f], start_fct, problem_size, z_star, nb_fct)
               if(normalize):
                   if(withTruescore):
                       current_gtcheby = eval_to.g_tcheby(model_directions[f_neighbors[f]].tolist()[0], population_scores[f_neighbors[f]], z_star)
                       average_score_pred += ( tmp_pred / current_gtcheby )
                       average_score_free += ( tmp_free / current_gtcheby )
                   else:
                       current_pred = model.predict([f_input_data[f][0:nb_fct] + population_indiv[f_neighbors[f]]])[0]
                       average_score_pred += ( tmp_pred / current_pred )
                       average_score_free += ( tmp_free / current_pred )
               else:
                   average_score_pred += tmp_pred
                   average_score_free += tmp_free
               count +=1
               f +=1

           average_score_pred /= float(count)
           average_score_free /= float(count)
           if(index_best_pred_list[0] == -1):
                score_best_pred = average_score_pred
                save_best_pred_free_score = average_score_free
                index_best_pred_list = [id_offspring]
           elif(average_score_pred < score_best_pred):
                score_best_pred = average_score_pred
                save_best_pred_free_score = average_score_free
                index_best_pred_list = [id_offspring]
           elif(average_score_pred == score_best_pred):
                index_best_pred_list.append(id_offspring)
           else :
                pass

        else: # free_eval
            f = 0
            for data in f_input_data:
                tmp_free = computeTchebyFreeEval(data, start_fct, problem_size, z_star, nb_fct)
                if(normalize):
                    current_gtcheby = eval_to.g_tcheby(model_directions[f_neighbors[f]].tolist()[0], population_scores[f_neighbors[f]], z_star)
                    average_score_free += ( tmp_free / current_gtcheby )
                else:# normalize false
                    average_score_free += tmp_free
                count +=1
                f += 1
            average_score_free /= float(count)

        if(index_best_free_list[0] == -1):
            score_best_free = average_score_free
            save_best_free_pred_score = average_score_pred
            index_best_free_list = [id_offspring]
        elif(average_score_free < score_best_free):
            score_best_free = average_score_free
            save_best_free_pred_score = average_score_pred
            index_best_free_list = [id_offspring]
        elif(average_score_free == score_best_free):
            index_best_free_list.append(id_offspring)
        else :
            pass

    index_best_pred = random.choice(index_best_pred_list)
    index_best_free = random.choice(index_best_free_list)
    index_best = -1
    if(free_eval):
         index_best = index_best_free
    else:
         diffFreePredict(current_g, current_f, score_best_pred, save_best_pred_free_score, index_best_pred, score_best_free, save_best_free_pred_score,  index_best_free)

         index_best = index_best_pred

    return list_offspring[index_best]
Esempio n. 15
0
def AverageImprovement  (free_eval, param, normalize, withTruescore):

    current_g, current_f, model, model2, two_models_bool, f_neighbors, list_offspring, model_directions, start_fct, problem_size, z_star, population_scores, population_indiv, nb_fct = param


    id_offspring = -1
    score_best_pred = 0
    save_best_pred_free_score = -1
    score_best_free = 0
    save_best_free_pred_score = -1

    index_best_free_list = [-1]
    index_best_pred_list = [-1]

    nb_fct = len(start_fct)
    for offspring in list_offspring:
        id_offspring += 1
        diff_score_pred = 0
        diff_score_free = 0

        f_input_data = getInputData(f_neighbors, model_directions, offspring)
        if(not free_eval):
           f_input_data_pred = np.matrix(f_input_data)
           f = 0
           for data in f_input_data_pred:
               tmp_pred = predict_and_quality(model, f_input_data[f], data, start_fct, problem_size, current_g, f_neighbors[f], nb_fct)
               tmp_free = computeTchebyFreeEval(f_input_data[f], start_fct, problem_size, z_star, nb_fct)
               current_gtcheby = eval_to.g_tcheby(model_directions[f_neighbors[f]].tolist()[0], population_scores[f_neighbors[f]], z_star)
               if(withTruescore):
                    if(normalize):
                        diff_score_pred += max(0, (current_gtcheby - tmp_pred) / current_gtcheby )
                        diff_score_free += max(0, (current_gtcheby - tmp_free) / current_gtcheby )
                    else:
                        diff_score_pred += max(0, current_gtcheby - tmp_pred)
                        diff_score_free += max(0, current_gtcheby - tmp_free)
               else:

                   current_pred = model.predict([f_input_data[f][0:nb_fct] + population_indiv[f_neighbors[f]]])[0]
                   if(normalize):
                       diff_score_pred += max(0, (current_pred - tmp_pred) / current_pred )
                       diff_score_free += max(0, (current_pred - tmp_free) / current_pred )
                   else:
                       diff_score_pred += max(0, current_pred - tmp_pred)
                       diff_score_free += max(0, current_pred - tmp_free)
               f +=1

           diff_score_pred /= float(f)
           diff_score_free /= float(f)
           if(index_best_pred_list[0] == -1):
                 score_best_pred = diff_score_pred
                 save_best_pred_free_score = diff_score_free
                 index_best_pred_list = [id_offspring]
           elif(diff_score_pred > score_best_pred):
                 score_best_pred = diff_score_pred
                 save_best_pred_free_score = diff_score_free
                 index_best_pred_list = [id_offspring]
           elif(diff_score_pred == score_best_pred):
                 index_best_pred_list.append(id_offspring)
           else :
                 pass
        else:
            f = 0
            for data in f_input_data:
                tmp_free = computeTchebyFreeEval(data, start_fct, problem_size, z_star, nb_fct)
                current_gtcheby = eval_to.g_tcheby(model_directions[f_neighbors[f]].tolist()[0] , population_scores[f_neighbors[f]], z_star)
                if(normalize):
                    diff_score_free += max(0, (current_gtcheby - tmp_free) / current_gtcheby )
                else:
                    diff_score_free += max(0, current_gtcheby - tmp_free)
                f +=1
            diff_score_free /= float(f)

        if(index_best_free_list[0] == -1):
            score_best_free = diff_score_free
            save_best_free_pred_score = diff_score_pred
            index_best_free_list = [id_offspring]
        elif(diff_score_free > score_best_free):
            score_best_free = diff_score_free
            save_best_free_pred_score = diff_score_pred
            index_best_free_list = [id_offspring]
        elif(diff_score_free == score_best_free):
            index_best_free_list.append(id_offspring)
        else :
            pass

    index_best_pred = random.choice(index_best_pred_list)
    index_best_free = random.choice(index_best_free_list)
    index_best = -1
    if(free_eval):
         index_best = index_best_free
    else:
         diffFreePredict(current_g, current_f, score_best_pred, save_best_pred_free_score, index_best_pred, score_best_free, save_best_free_pred_score,  index_best_free)

         index_best = index_best_pred

    return list_offspring[index_best]
Esempio n. 16
0
def by_direction_score(free_eval, param):
    global MAX_INTEGER

    current_g, current_f, model, model2, two_models_bool, f_neighbors, list_offspring, model_directions, start_fct, problem_size, z_star, population_scores, population_indiv, nb_fct = param

    id_offspring = -1
    score_best_pred = MAX_INTEGER
    score_best_free = MAX_INTEGER
    save_best_pred_free_score = MAX_INTEGER
    save_best_free_pred_score = MAX_INTEGER

    index_best_free_list = [-1]
    index_best_pred_list = [-1]

    current_f_w = model_directions[current_f].tolist()[0]

    for offspring in list_offspring:
        id_offspring += 1
        tmp_free = -1
        tmp_pred = -1

        f_input_data = []

        f_input_data.extend(current_f_w)

        f_input_data.extend(offspring)

        if(not free_eval):
           f_input_data_pred = np.matrix(f_input_data)
           tmp_pred = predict_and_quality(model, f_input_data, f_input_data_pred, start_fct, problem_size, current_g, current_f, nb_fct)
           score_eval = eval_to.free_eval(start_fct, offspring, problem_size)
           tmp_free = eval_to.g_tcheby(current_f_w, score_eval, z_star)

           if(index_best_pred_list[0] == -1):
               score_best_pred = tmp_pred
               save_best_pred_free_score = tmp_free
               index_best_pred_list = [id_offspring]
           elif(tmp_pred < score_best_pred):
               score_best_pred = tmp_pred
               save_best_pred_free_score = tmp_free
               index_best_pred_list = [id_offspring]
           elif(tmp_pred == score_best_pred):
               index_best_pred_list.append(id_offspring)
           else:
               pass

        if(free_eval):
            score_eval = eval_to.free_eval(start_fct, offspring, problem_size)
            tmp_free = eval_to.g_tcheby(current_f_w, score_eval, z_star)

        if(index_best_free_list[0] == -1):
            score_best_free = tmp_free
            save_best_free_pred_score = tmp_pred
            index_best_free_list = [id_offspring]
        elif(tmp_free < score_best_free):
            score_best_free = tmp_free
            save_best_free_pred_score = tmp_pred
            index_best_free_list = [id_offspring]
        elif(tmp_free == score_best_free):
            index_best_free_list.append(id_offspring)
        else :
            pass

    index_best_pred = random.choice(index_best_pred_list)
    index_best_free = random.choice(index_best_free_list)
    index_best = -1
    if(free_eval):
        index_best = index_best_free
    else :
        index_best = index_best_pred
        diffFreePredict(current_g, current_f, score_best_pred, save_best_pred_free_score, index_best_pred, score_best_free, save_best_free_pred_score,  index_best_free)


    return list_offspring[index_best]