J'ai besoin de vous!!!

baster200x Messages postés 47 Date d'inscription vendredi 7 mars 2008 Statut Membre Dernière intervention 24 juillet 2011 - 18 juin 2011 à 12:07
baster200x Messages postés 47 Date d'inscription vendredi 7 mars 2008 Statut Membre Dernière intervention 24 juillet 2011 - 30 juin 2011 à 10:16
bonjour les amis

j'ai trouvé la solution pour mes problème que je l'ai poser précédemment sur le forum à propos de l'intégration d'un module d'optimisation (algorithme d'optimisation) VEGA dans un outil Open Source nommé Multicube explorer;

la solution consiste à créer la class .CPP + le fichier header .h de l'algorithme VEGA (ce qui est fait!) avant l'installation de l'outil; le problème qui ce pose c'est que pour le faire marcher, je ne sais pas ce qu'il doit prendre comme paramètre .

m3explorer c'est un outil pour l'exploration des architecture paramétrable,

j'ai essayé de lire la documentation sur l'API de m3explorer et j'ai rien pigé.

voilà mon algorithme:
---------------------
main.cpp:
---------
#include <stdio.h>
#include <stdlib.h>
#include "main.h"


FILE *fichier, *cache, *temps_access, *puissance_access, *pop;

int main(int argc, char ** argv)
{
    double ** T = new double* [50];
    for(int i 0; i < 50; ++i) T[i] new double[nbTache];

    double ** C = new double* [50];
    for(int i 0; i < 50; ++i) C[i] new double[nbTache];

    pop = fopen("pop.txt", "r");

    fichier = fopen("resultat.txt", "r");
    for (int i = 0; i < 50; i++)
        for (int j = 0; j < nbTache; j++)
            fscanf(fichier, "%lf%lf", &T[i][j], &C[i][j]);

    fclose(fichier);

    double * Taille = new double [50];
    cache = fopen("cache.txt", "r");
    for (int h = 0; h < 50; h++) fscanf(cache, "%lf", &Taille[h]);
    fclose(cache);

    double *Taccess = new double [50];
    temps_access = fopen("temps_access.txt", "r");
    for (int h = 0; h < 50; h++) fscanf(temps_access, "%lf", &Taccess[h]);
    fclose(temps_access);

    double * Paccess = new double [50];
    puissance_access = fopen("puissance_access.txt", "r");
    for (int h = 0; h < 50; h++) fscanf(puissance_access, "%lf", &Paccess[h]);
    fclose(puissance_access);

    double ** Tc = new double* [50];
    for(int i 0; i < 50; ++i) Tc[i] new double[50];

    for (int i = 0; i < 50; i++)
        for (int j = 0; j < 50; j++)
        {
            if (i j) Tc[i][j] 0;
            else if (Taille[i] < Taille[j]) Tc[i][j] = 0;
            else Tc[i][j] = Taccess[i] * (Taille[i] - Taille[j]);
        }

    double ** Cc = new double* [50];
    for(int i 0; i < 50; ++i) Cc[i] new double[50];

    for (int i = 0; i < 50; i++)
        for (int j = 0; j < 50; j++)
        {
            if (i j) Cc[i][j] 0;
            else if (Taille[i] < Taille[j]) Cc[i][j] = 0;
            else Cc[i][j] = Paccess[i] * (Taille[i] - Taille[j]);
        }


    int ** p = new int* [100];
    for(int i 0; i < 100; ++i) p[i] new int[nbTache];

    for (int i = 0; i < 50; i++)
        for (int j = 0; j < 50; j++)
            p[i][j] = rand();

// Evaluation des individus ou calcul de Fitness
    double * Ft = new double [100];
    double * Fc = new double [100];
    int * par1 = new int [nbTache];
    int * par2 = new int [nbTache];
    int * enf1 = new int [nbTache];
    int * enf2 = new int [nbTache];
    int ** p1 = new int* [100];
    for(int i 0; i < 100; ++i) p1[i] new int[nbTache];
    int ** p2 = new int* [100];
    for(int i 0; i < 100; ++i) p2[i] new int[nbTache];
    int * In = new int [2];

// Evaluation des individus
    f1(100, nbTache, p, T, Tc, Ft, p1);
    f2(100, nbTache, p, C, Cc, Fc, p2);
    selection(100, nbTache, p, Ft, Fc, par1, par2, In);

    for (int i = 0; i < 100; i++)
    {
        printf("%d ", i);

        for (int j = 0; j < nbTache; j++) printf("%d ", p[i][j]);
        printf("\n");
    }

    f1(100, nbTache, p, T, Tc, Ft, p1);
    f2(100, nbTache, p, C, Cc, Fc, p2);

// Sélection
    int k = 0;
    int l = 1;

    while (k < 500)
    {
//nombre de génération
// croisement
        croisement(100, nbTache, p, par1, par2, enf1, enf2, In, T, Tc, C, Cc);
// Mutation
        mutation(100, nbTache, p, T, Tc, C, Cc);
// pop après reclassement, croisement et mutation

        selection(100, nbTache, p, Ft, Fc, par1, par2, In);
//c la j'ai ajoutée une condition qui teste si c la premiere itération alors
//imprimé ou si pair de 10 imprimer aussi car je veux avoir 1 10 20 30 40 50 60 70 80 90 100

        if (l % 50 == 0)
        {
            printf("%d\n", l);
            for (int i = 0; i < 100; i++)
            {
                printf( "%d ", i);

                for (int j = 0; j < nbTache; j++)
                {
                    printf("%d ", p[i][j]);
                }

                printf("\n");

            }

            f1(100, nbTache, p, T, Tc, Ft, p1);

            f2(100, nbTache, p, C, Cc, Fc, p2);
        }

        k++;
        l++;
    }

    fclose(pop);
}



main.h:
--------

void f1(int lig, int col, int ** p, double ** t,double ** tc, double * f, int ** p1);
void f2(int lig, int col, int ** p, double ** c, double ** cc, double * f, int ** p2);
void selection(int lig, int col, int ** p, double * f1, double * f2, int * par1, int * par2, int * ind);
void croisement(int lig, int col, int ** p, int * par1, int * par2, int * enf1, int * enf2, int * ind, double ** t, double ** tc, double ** c, double ** cc);
void mutation(int lig, int col, int ** p, double ** t, double ** tc, double ** c, double ** cc);
double fti(int col, int * individu, double ** t, double ** tc);
double fci(int col, int * individu, double ** c, double ** cc);

int nbTache = 10;
FILE *tempsAg, *puisAg;

void f1(int lig, int col, int ** p, double ** t,double ** tc, double * f, int ** p1)
{
    double s1, s2;
    double * S1 = new double [lig];
    double * S2 = new double [lig];

    for (int i = 0; i < lig; i++)
    {
        s1 = 0;

        for (int j = 0; j < col; j++)
            s1 += t[p[i][j]][j];

        S1[i] = s1;
    }

    for (int i = 0; i < lig; i++)
    {
        s2 = 0;

        for (int j = 0; j < col-1; j++)
            s2 += tc[p[i][j]][p[i][j + 1]];

        S2[i] = s2;
    }

    tempsAg = fopen("tempsAg.txt", "w");
    printf(" Ft\n");

    for (int i = 0; i < lig; i++)
    {
        f[i] = S1[i] + S2[i];
        fprintf(tempsAg, "%lf\n", f[i]);
    }

    fclose(tempsAg);
}

void f2(int lig, int col, int ** p, double ** c, double ** cc, double * f, int ** p2)
{
    double s1, s2;
    double * S1 = new double[lig];
    double * S2 = new double[lig];

    for (int i = 0; i < lig; i++)
    {
        s1 = 0;
        for (int j = 0; j < col; j++) s1 += c[p[i][j]][j];
        S1[i] = s1;
    }

    for (int i = 0; i < lig; i++)
    {
        s2 = 0;
        for (int j = 0; j < col - 1; j++) s2 += cc[p[i][j]][p[i][j + 1]];
        S2[i] = s2;
    }

    puisAg = fopen("puisAg.txt", "w");
    printf("Fc\n");

    for (int i = 0; i < lig; i++)
    {
        f[i] = S1[i] + S2[i];
        fprintf(puisAg, "%lf\n", f[i]);
        printf("%lf\n", f[i]);
    }

    fclose(puisAg);
}

// Appliquer l'algorithme VEGA pour la selection des individus qui feront
// objet d'un croisement et/ou Mutation
void selection(int lig, int col, int ** p, double * f1, double * f2, int * par1, int * par2, int * ind)
{
// Sous pop 1 les 50 premiers individus sont classés selon F1
    double z;
    int x;

//Classement selon F1
    for (int i = 0; i < ( (lig / 2) - 1); i++)
    {
        for (int j = i + 1; j < lig / 2; j++)
        {
            if (f1[j] < f1[i])
            {
                z = f1[i];
                f1[i] = f1[j];
                f1[j] = z;
                for (int h = 0; h < col; h++)
                {
                    x = p[i][h];
                    p[i][h] = p[j][h];
                    p[j][h] = x;
                }

            }
        }
    }

    double z1;
    int x1; //classement selon F2
    for (int i = (lig / 2); i < lig - 1; i++)
    {
        for (int j = i + 1; j < lig; j++)
        {
            if (f2[j] < f2[i])
            {
                z1 = f2[i];
                f2[i] = f2[j];
                f2[j] = z1;
                for (int h = 0; h < col; h++)
                {
                    x1 = p[i][h];
                    p[i][h] = p[j][h];
                    p[j][h] = x1;
                }

            }
        }
    }

    ind[0] = rand() % 50;
    ind[1] = rand() % 50;

    for (int i = 0; i < lig; i++)
    {
        if (i == ind[0])
        {
            for (int j = 0; j < col; j++)
            {
                par1[j] = p[i][j];
            }
        }

    }
    for (int i = 0; i < lig; i++)
    {
        if (i == ind[1])
        {
            for (int j = 0; j < col; j++)
            {
                par2[j] = p[i][j];
            }
        }
    }
}

void croisement(int lig, int col, int ** p, int * par1, int * par2, int * enf1, int * enf2, int * ind, double ** t, double ** tc, double ** c, double ** cc)
{

    for (int j = 0; j < col / 2; j++)
    {
        enf1[j] = par1[j];
        enf2[j] = par2[j];
    }
    for (int j = col / 2; j < col; j++)
    {
        enf1[j] = par2[j];
        enf2[j] = par1[j];
    }

    if (fti(col, enf1, t, tc) < fti(col, par1, t, tc))
    {
        for (int j = 0; j < col; j++)
        {
            p[ind[0]][j] = enf1[j];
        }
    }

    if (fci(col, enf2, c, cc) < fci(col, par2, c, cc))
    {
        for (int j = 0; j < nbTache; j++)
        {
            p[ind[1]][j] = enf2[j];
        }
    }

}

void mutation(int lig, int col, int ** p, double ** t, double ** tc, double ** c, double ** cc)
{
    int * mut = new int[lig];
    int * mut1 = new int[lig];
    int id = rand() % 100;

    for (int i = 0; i < lig; i++)
    {

        if (i == id)
        {
            for (int j = 0; j < col; j++)
            {
                mut[j] = p[i][j];
                mut1[j] = mut[j];
            }
        }
    }

    int id1 = rand() % col;

    for (int j = 0; j < col; j++)
    {
        if (j == id1)
        {
            mut1[j] = 49 - mut[j];
        }
    }

    if ( (fti(col, mut1, t, tc) < fti(col, mut, t, tc)) && (fti(col, mut1, t, tc) < fti(col, mut, t, tc)))
    {
        for (int j = 0; j < col; j++)
        {
            p[id][j] = mut1[j];
        }
    }

}

double fti(int col, int * individu, double ** t, double ** tc)
{
    double s1 0, s2 0;

    for (int j = 0; j < col; j++)
    {
        s1 += t[individu[j]][j];
// p[i]{j]= N°Configuration [j]=N° de la
// tache
    }

    for (int j = 0; j < col - 1; j++)
    {
        s2 += tc[individu[j]][individu[j + 1]];
// p[i]{j]=
// N°Configuration
// [j+1]=N°configuration
// suivante dans la solution
// en cours
    }

    return s1 + s2;
}

double fci(int col, int * individu, double ** c, double ** cc)
{
    double s1 0, s2 0;

    for (int j = 0; j < col; j++)
    {
        s1 += c[individu[j]][j];
// p[i]{j]= N°Configuration [j]=N° de la
// tache
    }

    for (int j = 0; j < col - 1; j++)
    {
        s2 += cc[individu[j]][individu[j + 1]]; // p[i]{j]=
// N°Configuration
// [j+1]=N°configuration
// suivante dans la solution
// en cours
    }

    return s1 + s2;
}



SVP.... aidez moi à trouver ce que je doit mettre comme paramètre dans les fonctions pour que l'algorithme marche. je serai très reconnaissant

merci beaucoup, je n'oublierai jamais votre soutien .

2 réponses

baster200x Messages postés 47 Date d'inscription vendredi 7 mars 2008 Statut Membre Dernière intervention 24 juillet 2011
18 juin 2011 à 17:05
désolé voilà un algorithme qui travail de la même façon:
NSGA_II:
---------
#include 
#include <sstream>
#include <m3_object.h>
#include <m3_map.h>
#include <m3_vector.h>
#include <m3_shell_variables.h>
#include <m3_parser.h>
#include <m3_optimizer.h>
#include <m3_opt_utils.h>
#include <m3_sim_utils.h>
//#include <stdlib.h>
#include <stdio.h>




class m3_point_EA : public m3_point
{
    public:
    m3_point_EA();
    m3_point_EA(m3_point &p);
    m3_point_EA(const m3_point_EA &p);
    int dominated_p;
    int dominating_p;
    vector dominating_v;
    int rank;
    bool ranked;
    bool evaluated;
    bool edge;
    double distance;
};

m3_point_EA::m3_point_EA():m3_point() {
    dominating_p = 0;
evaluated = false;
ranked = false;
edge = false;
distance = 0.0;
}

m3_point_EA::m3_point_EA(m3_point &l):m3_point(l){
    dominating_p = 0;
evaluated = false;
ranked = false;
edge = false;
distance = 0.0;
}

m3_point_EA::m3_point_EA(const m3_point_EA &l):m3_point(l){
    dominating_p = 0;
    rank = l.rank;
evaluated = l.evaluated;
ranked = false;
edge = false;
distance = 0.0;
}



class m3_nsga_II: public m3_optimizer
{
    public:
        m3_nsga_II() {}
        string get_information();
        void explore(m3_env *env);
    private:

        void get_valid_points(m3_env *env, vector<m3_point> &doe_orig, vector<m3_point_EA> &doe);

        void compare_points(m3_env *env, vector<m3_point_EA> &doe);
        void compare_this_point(m3_env *env, vector<m3_point_EA> & doe, m3_point_EA &current_point);

        void rank_points(m3_env *env, vector<m3_point_EA> & doe_ranked, vector<m3_point_EA> & doe);

        void diverse_points(m3_env *env, vector<m3_point_EA> &doe_ranked);
        void generate_next_pop (m3_env *env, vector<m3_point_EA> &children,vector<m3_point_EA> &doe_ranked);
        void select_best_half(m3_env *env, vector<m3_point_EA> &doe_ranked, vector<m3_point_EA> &doe_merged_ranked);
};


string m3_nsga_II::get_information()
{
    return "Nondominated Sorting Genetic Algorithm II optimizer";
}


void m3_nsga_II::explore(m3_env *env)
{
    prs_display_message("Starting with the NSGA-II optimization process");
    m3_assert(env->current_driver);


    bool temporary_save = false;
    string filename;
    if (get_string_from_variables(env, "temp_database", filename)) 
        temporary_save = true; 


    vector<m3_point> doe_orig = env->current_doe->generate_doe(env);

    int doe_orig_size = doe_orig.size();

    char tempInt [20];
    vector<m3_point_EA> doe;

    get_valid_points(env, doe_orig, doe);

    /*the main algorithm that compare the points regarding domination
    this algorithm adds the following attributes to each point,    the vector of dominated nodes, 
    the No. of dominated points and the No. of dominating points...*/
    compare_points(env, doe);

    for(int i = 0; i < doe.size(); i++)
    {
        prs_display_message("point: " + env->current_design_space->get_point_representation(env, doe[i]));
        string error;
        if(!doe[i].get_error(error))
        {
            for(int j = 0; j < env->current_design_space->objectives.size(); j++)
            {
                double my_value = doe[i].get_objective(env, j);
                sprintf(tempInt,"%.0f",my_value);
                prs_display_message(tempInt);
            }
        }
    }

    /*the main algorithm that rank the points by going through sccessive fronts
    this algorithm adds the following attributes to each point
    the rank No., it also sort the points by the rank No. in the first argument*/
    vector<m3_point_EA> doe_ranked;
    rank_points(env, doe_ranked, doe);

    //compute the crowding parameter to support the diversity feature this algorithm adds average distance to indicate the crowding
    diverse_points(env, doe_ranked);

    int generations;
    if(!get_integer_from_variables(&current_environment,"generations", generations))
    {
        prs_display_message("No 'generations' defined; defaulting to 100 generations as stopping criteria");
        generations = 100;
    }
    for(int genNo = 0; genNo<generations; genNo++)
    {
        //for each generaton the points would be already added in doe_ranked, so start by generating the new population 
        //produce the next population in children, by crossover and permutation
        vector<m3_point_EA> children;
        generate_next_pop (env, children, doe_ranked);

        //merge the two populations parents "doe_ranked" and children to have population of double size
        vector<m3_point_EA> doe_merged;
        vector<m3_point_EA> doe_merged_ranked;
        for(int i=0; i<doe_ranked.size(); i++)
            doe_merged.push_back(doe_ranked[i]);
        for(int i=0; i<children.size(); i++)
            doe_merged.push_back(children[i]);
        //should be empty for next generations
        doe_ranked.clear();
        children.clear();

        //rank the new double size population, parents+children as previous, "compare_points", rank_points and "diverse_points"
        compare_points(env, doe_merged);

        rank_points(env, doe_merged_ranked, doe_merged);
        diverse_points(env, doe_merged_ranked);

        //create the new generation by getting the best N solutions of the 2N solutions,
        //this process gurantees the eliticism
        select_best_half(env, doe_ranked, doe_merged_ranked);

        //should be empty for next generations
        doe_merged.clear();
        doe_merged_ranked.clear();
        //now doe_ranked would be already ranked "best ranks are selected" but diversity should be computed again
        //diverse_points(env, doe_ranked);
        //now doe_ranked is considered the new generation

        //cout << endl;
        sprintf(tempInt,"%d",genNo);
        prs_display_message("Generation No.: ");
        prs_display_message(tempInt);

        if (temporary_save)
        {
            prs_display_message("Writing temporary database..");
            env->available_dbs["root"]->write_to_file(filename.c_str());
        }
    }
    cout << endl;
}


void m3_nsga_II::get_valid_points(m3_env *env, vector<m3_point> &doe_orig, vector<m3_point_EA> &doe)
{
    m3_point_EA current_point;
    int doe_orig_size = doe_orig.size();
    int doe_size = doe.size();

    for (int i = 0; i < doe_orig_size ; i++)
    {
        current_point = doe_orig[i];
        //it always return true if the used driver is "m3_driver" ,!!!
        // but if the used driver is m3_xml_driver, it seems that it checks the rules
        if(env->current_design_space->is_valid(env, & doe_orig[i]))
        {
            prs_display_message("Adding point: " + env->current_design_space->get_point_representation(env, current_point));
            doe.push_back(current_point);
        }
        else
        {
            prs_display_message("Skipping point: " + env->current_design_space->get_point_representation(env,current_point));
        }
    }
}


void m3_nsga_II::compare_points(m3_env *env, vector<m3_point_EA> &doe)
{
    prs_display_message("Comparing Points");
    int exp_size = doe.size();

    for(int experiment = 0; experiment < exp_size; experiment ++)
    {
        if(!doe[experiment].evaluated)
        {
            //m3_point &current_point = doe[experiment];
            //prs_display_message("Evaluating point: " + env->current_design_space->get_point_representation(env, doe[experiment]));
            //why the clear metrics should be called
            //doe[experiment].clear_metrics(env);
            env->current_design_space->evaluate(env, & doe[experiment]);
            
            if(sim_recompute_pareto_with_new_point(env, env->available_dbs[env->current_db_name], &doe[experiment]))
            {
                if( env->available_dbs[env->current_db_name]->find_point(doe[experiment])==NULL )
                    env->available_dbs[env->current_db_name]->insert_point(& doe[experiment]);
            }
            doe[experiment].evaluated = true;
            string error;
            int err_code;
            /*it seems that == operator should be replaced by the assignment
             operator =, which returns false if the right hand expression = 0*/
            err_code = doe[experiment].get_error(error);
            if(err_code == M3_POINT_FATAL_ERROR)
               break;
        }
    }

    char tempInt [20];
    int obj_size = env->current_design_space->objectives.size();

    for(int experiment = 0; experiment < exp_size; experiment ++)
    {
        compare_this_point(env, doe, doe[experiment]);
    }
}    


void m3_nsga_II::compare_this_point(m3_env *env, vector<m3_point_EA> & doe, m3_point_EA & current_point)
{
    int dominating_p = 0;                // No. of points current_point domintates
    int    dominated_p = 0;            // No. of points domintate current_point
    int exp_size = doe.size();
    int obj_size = env->current_design_space->objectives.size();
    vector dominating_v(exp_size);        // dominating vector space
    char tempInt [20];
    string error;

    if( current_point.get_error(error) )        // if the current point is an error point
    {        
        for (int i=0; i<exp_size; i++)
        {
            if(!doe[i].get_error(error))
            {
                dominated_p++;
            }
        }
        current_point.dominating_p = 0;
        current_point.dominated_p = dominated_p;
        return;
    }

    //for(int j = experiment+1; j < exp_size; j++)
    for(int j = 0; j < exp_size; j++)
    {
        m3_point_EA compared_point = doe[j];
        if (&current_point != &compared_point)         //don't compare a point to itself 
        {
            if (!compared_point.get_error(error))    // if the compared point is not error point
            {
                bool m_worse=false;
                bool c_worse=false;
                //scan for objectives
                for(int i=0; i < obj_size ;i++)
                {
                    double my_value = current_point.get_objective(env, i);
                    double comp_value = compared_point.get_objective(env, i);
    
                    if (my_value>comp_value)    m_worse=true;
                    if (comp_value>my_value)    c_worse=true;
                }
    
                if(c_worse && !m_worse)            //my current point dominates the compared
                {
                    dominating_v[dominating_p] = j;    //push the dominated point ID
                    dominating_p ++;
                }
                if(!c_worse && m_worse)            //the compared point dominates my point
                {
                    dominated_p ++;
                }
            }
            else    // error points are always dominated
            {
                dominating_v[dominating_p] = j;    //push the dominated point ID
                dominating_p ++;
                
            }
        }
    }

    current_point.dominating_p = dominating_p;
    current_point.dominated_p = dominated_p;
    current_point.dominating_v.clear();
    for (int i=0; i<dominating_p; i++){
//        cout << " d_v_i " << dominating_v[i] << endl;
        current_point.dominating_v.push_back(dominating_v[i]);
    }
}

void m3_nsga_II::rank_points(m3_env *env, vector<m3_point_EA> &doe_ranked, vector<m3_point_EA> &doe)
{
    prs_display_message("Ranking Points");
    int dominating_p;
    int rank = 1;
    char tempInt [20];
    string temp;
    string error;
    for(int i = 0; i < doe.size(); i++)
        doe[i].ranked = false;
    while (doe_ranked.size() < doe.size())
    {
        bool changed=false;//will become true when only error points will remain
        for(int i = 0; i < doe.size(); i++)
        {
            //if the point is an error point rank it now and modify it rank at the end as the maximum
            if( (doe[i].get_error(error)) )
                continue;
            if(doe[i].ranked || doe[i].dominated_p)
                continue;        //already ranked or still not pareto jump to the next point

            changed=true;
            //pareto regarding this front
            sprintf(tempInt,"%d",rank);
            temp = tempInt;
            
            ostringstream objs;
            int obj_size = env->current_design_space->objectives.size();

            for(int j=0; j < obj_size ;j++)
            {
                objs << doe[i].get_objective(env, j) << " ";
            }

            for (int j = 0; j < doe[i].dominating_p; j++)
            {
                //get points dominated by current_point, msut not be ranked yet
                int index = doe[i].dominating_v[j];
                if(doe[index].ranked)//an algorithmic error
                    prs_display_message("Algorithmic Error");
                doe[index].dominated_p --;
                if(doe[index].dominated_p < 0)//an algorithmic error
                    prs_display_message("Algorithmic Error");
            }
            doe[i].rank = rank;
            doe[i].ranked = true;
            doe_ranked.push_back(doe[i]);
        }
        rank ++;

        //missing only error points
        if(!changed){
            for(int i = 0; i < doe.size(); i++){
                if((doe[i].get_error(error))){
                    doe[i].rank = rank;
                    doe[i].ranked = true;
                    doe_ranked.push_back(doe[i]);
                }
            }
        }
    }
}

void m3_nsga_II::generate_next_pop (m3_env *env, vector<m3_point_EA> &children, vector<m3_point_EA> &doe_ranked)
{
    prs_display_message("Generating Next Pop");
    while(children.size() < doe_ranked.size())
    {
        int randNo1 = rand()%doe_ranked.size();
        int randNo2 = rand()%doe_ranked.size();
        int randNo3 = rand()%doe_ranked.size();
        int randNo4 = rand()%doe_ranked.size();
        int parentNo1;
        int parentNo2;

        if (doe_ranked[randNo1].rank < doe_ranked[randNo2].rank)
            parentNo1 = randNo1;
        else if(doe_ranked[randNo2].rank < doe_ranked[randNo1].rank)
            parentNo1 = randNo2;
        else            //the same rank
        {
            if(doe_ranked[randNo1].edge)        //point on the edge, injfinite crowding distance
                parentNo1 = randNo1;
            else if(doe_ranked[randNo2].edge)    //point on the edge, injfinite crowding distance
                parentNo1 = randNo2;
            else
            {
                if(doe_ranked[randNo1].distance > doe_ranked[randNo2].distance)
                    parentNo1 = randNo1;
                else
                    parentNo1 = randNo2;
            }
        }

        if (doe_ranked[randNo3].rank < doe_ranked[randNo4].rank)
            parentNo2 = randNo3;
        else if(doe_ranked[randNo4].rank < doe_ranked[randNo3].rank)
            parentNo2 = randNo4;
        else            //the same rank
        {
            if(doe_ranked[randNo3].edge)        //point on the edge, injfinite crowding distance
                parentNo2 = randNo3;
            else if(doe_ranked[randNo4].edge)    //point on the edge, injfinite crowding distance
                parentNo2 = randNo4;
            else
            {
                if(doe_ranked[randNo3].distance > doe_ranked[randNo4].distance)
                    parentNo2 = randNo3;
                else
                    parentNo2 = randNo4;
            }
        }


        m3_point parent1 = doe_ranked[parentNo1];
        m3_point parent2 = doe_ranked[parentNo2];
        m3_point cross_p = env->current_design_space->genetic_crossover(env, &parent1, &parent2);
        m3_point perm_p = env->current_design_space->genetic_mutation(env, &cross_p, 0.1);
        if(env->current_design_space->is_valid(env, & perm_p))
        {
            prs_display_message("Adding point: " + env->current_design_space->get_point_representation(env, perm_p));
            children.push_back(perm_p);
        }
        else
        {
            prs_display_message("Skipping point: " + env->current_design_space->get_point_representation(env, perm_p));
        }
    }

    prs_display_message("Finished Generating Next Pop");
}

void m3_nsga_II::select_best_half(m3_env *env, vector<m3_point_EA> &doe_ranked, vector<m3_point_EA> &doe_merged_ranked)
{
    prs_display_message("Selecting Best half");
    int rank = 1, count=0, lastCount=0;
    int doe_merged_size = doe_merged_ranked.size();
    int first = 0;
    for(int p=0; p<doe_merged_size; p++)
    {
        if(doe_merged_ranked[p].rank == rank && (p!=(doe_merged_size-1)))    //check the end of input vector
            continue;
        if(p<doe_merged_size/2)            //all this front should be taken, and should continue with next fronts
        {
            for(int i=first; i highestDist)
                		{
                			highestDist = doe_merged_ranked[i].distance;
                			index = i;
                		}
                	}

                }
                doe_ranked.push_back(doe_merged_ranked[index]);
                doe_merged_ranked[index].edge = true;            //don't consider it next time
            }
            break;
        }
    }
    char tempInt [20];
}

//asuming that the vector is ranked and sorted.
void m3_nsga_II::diverse_points(m3_env *env, vector<m3_point_EA> &doe_ranked)
{
    int first=0, rank=1;
    int obj_size = env->current_design_space->objectives.size();
    double *lowest = new double [obj_size];
    double *highest = new double [obj_size];

    for(int i = 0; i < doe_ranked.size(); i++)
        doe_ranked[i].edge = false;

    for(int p=0; p<doe_ranked.size(); p++)
    {
        string error;
        if( (doe_ranked[p].rank == rank) || (doe_ranked[p].get_error(error)) )
            continue;
        else
        {            //points belong to the same nondominating front, from first to i-1
            for(int k=0; k<obj_size; k++)
            {
                lowest[k]=doe_ranked[first].get_objective(env, k);
                highest[k]=doe_ranked[first].get_objective(env, k);
            }
            for(int j=first+1; j highest[k])
                        highest[k]=doe_ranked[j].get_objective(env, k);
                }
            }
            for(int j=first; j= 0)    //the point j is higher than l, lower value may be updated
                            {
                                if (diff < dist2lower)
                                    dist2lower = diff;
                            }
                        }
                    }    //end of compared point scanning
                    distance = distance + (dist2lower+dist2higher)/(highest[k]-lowest[k]);
                }    //end of objectives scanning
                doe_ranked[j].distance = distance;
            }    //end of scanning this nondominating front
            first = p;    //begin of a new non dominating front
        }
    }
    delete [] lowest;
    delete [] highest;
}


extern "C"
{
    m3_optimizer *opt_generate_optimizer()
    {
        return new m3_nsga_II();
    }
}

0
baster200x Messages postés 47 Date d'inscription vendredi 7 mars 2008 Statut Membre Dernière intervention 24 juillet 2011
30 juin 2011 à 10:16
BONJOUR .
JE voix qu'il y a personne pour m'aider... j'ai voulu juste savoir quel paramètre utilise dans le premier algorithme .. je sais que je doit utilisé les paramètre suivants:

m3point pour modeliser les points

m3_env te permettra de récuperer tout ce qui concerne les entrées et les sorties ainsi que le plan d’expérience sélectionné par exemple

et autre objet pour faire tourner le code comme m3_map, m3_vector, m3_parser, ....

y on a surement d'autre mais je les connais pas et pour cela j'ai demander l'aide de vous car vous savez plus que moi en programment avec C/C++

merci pour votre aide !
0
Rejoignez-nous