Example #1
0
		int jde_alg::run()
		{
			if ( !m_ppara )
				return -1;

			timer elapsed_t;
			// retrieve algorithm parameters
			size_t pop_size=m_ppara->get_pop_size();
			size_t num_dims=m_ppara->get_dim();
			double vtr=m_ppara->get_vtr();
			double f_low_bnd,f_up_bnd;
			f_low_bnd=m_ppara->get_f_low_bnd();
			f_up_bnd=m_ppara->get_f_up_bnd();
			double tau_1,tau_2;
			tau_1=m_ppara->get_tau_1();
			tau_2=m_ppara->get_tau_2();
			
			int m_cur_run;
			int max_run=m_ppara->get_max_run();// run/trial number
			// shared_ptr<progress_display> pprog_dis;// algorithm progress indicator from boost
			// alloc_prog_indicator(pprog_dis);

			// allocate original pop and trial pop
			population pop(pop_size);
			allocate_pop(pop,num_dims,stra_num);
			population trial_pop(pop_size);
			allocate_pop(trial_pop,num_dims,stra_num);

			// generate algorithm statistics output file name
			ofstream stat_file(m_com_out_path.stat_path.c_str());
			// allocate stop condition object dynamically
			alloc_stop_cond();

			size_t shuffle_size=pop_size-1;
			vector<int> vec_idx1(shuffle_size);

			// random U(0,1) generator
			uniform_01<> dist_01;
			variate_generator<mt19937&, uniform_01<> > rnd_01(gen, dist_01);

			// generator for random DIMENSION index
			uniform_int<> dist_dim(0,num_dims-1);
			variate_generator<mt19937&, uniform_int<> > rnd_dim_idx(gen, dist_dim);

			// iteration start
			for ( m_cur_run=0;m_cur_run<max_run;m_cur_run++ )
			{
				reset_run_stat();
				m_de_stat.reset();
				// jde SPECIFIC,initialize F value vector EVERY single run
				size_t i;
				for ( i=0;i<pop_size;i++ )
				{
					pop[i].stra[pr].assign(1,0.0);
					pop[i].stra[f].assign(1,0.0);
				}
				set_orig_pop(pop);
				update_diversity(pop);

				calc_de_para_stat(pop);
				record_de_para_stat(m_cur_run);
				record_gen_vals(m_alg_stat,m_cur_run);

				print_run_times(stat_file,m_cur_run+1);
				print_run_title(stat_file);
				// output original population statistics
				print_gen_stat(stat_file,1,m_alg_stat);

				m_cur_gen=1;
				while ( false==(*m_pstop_cond) ) // for every iteration
				{
					m_de_stat.reset();
					int rnd_dim;
					double f_i;
					double pr_i;
					double dim_mut_chance;
					size_t i,j,k;

					trial_pop=pop;// operator =
					for ( i=0;i<pop_size;i++ )
					{
						// generating three mutually different individual index other than i using random shuffle
						// initialize index vector
						for ( k=0;k<shuffle_size;k++ )
						{
							if ( k<i )
								vec_idx1[k]=k;
							else
								// EXCLUDE i
								vec_idx1[k]=(k+1)%pop_size;
						}
						// random shuffle
						for ( k=0;k<shuffle_size;k++ )
						{
							// generator for random SHUFFLE VECTOR index
							uniform_int<> dist_uni_shuf(k,shuffle_size-1);
							variate_generator<mt19937&, uniform_int<> > rnd_shuf_idx(gen, dist_uni_shuf);
							int idx_tmp=rnd_shuf_idx();
							swap(vec_idx1[k],vec_idx1[idx_tmp]);
						}

						int i1,i2,i3;// i!=i1!=i2!=i3
						i1=vec_idx1[0];
						i2=vec_idx1[1];
						i3=vec_idx1[2];

						rnd_dim=rnd_dim_idx();

						double pr_chance=rnd_01();
						if ( pr_chance<=tau_2 )
						{
							pr_i=rnd_01();
							trial_pop[i].stra[pr][0]=pr_i;
						}
						else
							pr_i=trial_pop[i].stra[pr][0];
						
						// scaling factor F self-adaptive update equation
						double f_chance=rnd_01();
						if ( f_chance<=tau_1 )
						{
							f_i=f_low_bnd+rnd_01()*f_up_bnd;
							trial_pop[i].stra[f][0]=f_i;
						}
						else
							f_i=trial_pop[i].stra[f][0];// keep unchanged at thsi iteration

						for ( j=0;j<num_dims;j++ )
						{
							dim_mut_chance=rnd_01();
							if ( rnd_dim==j || dim_mut_chance<=pr_i )
							{
								trial_pop[i].x[j]=trial_pop[i1].x[j]+f_i*(trial_pop[i2].x[j]-trial_pop[i3].x[j]);
								// boundaries check
								bound_check(trial_pop[i].x[j],j);
							}
						}// for every dimension
					}// for every particle
					// evaluate pop
					eval_pop(trial_pop,*m_pfunc,m_alg_stat);
					update_pop(pop,trial_pop);
					stat_pop(pop,m_alg_stat);
					update_search_radius();
					update_diversity(pop);

					calc_de_para_stat(pop);
					record_de_para_stat(m_cur_run);
					record_gen_vals(m_alg_stat,m_cur_run);

					print_gen_stat(stat_file,m_cur_gen+1,m_alg_stat);
					update_conv_stat(vtr);

					/*if ( run_once )
					++(*pprog_dis);*/

					m_cur_gen++;
				}// while single run stop_condition is false

				// single run end
				stat_run(pop,m_cur_run);// stat single run for algorithm analysis
				if ( is_final_run(m_cur_run,max_run) )
					print_run_stat(stat_file,m_alg_stat,max_run);
				/*if ( !run_once )
				++(*pprog_dis);*/
			}// for every run

			print_avg_gen(stat_file,m_alg_stat.run_avg_gen);
			// stat and output average time per run by second
			m_alg_stat.run_avg_time=elapsed_t.elapsed();
			m_alg_stat.run_avg_time /= (max_run*1.0);
			print_avg_time(stat_file,m_alg_stat.run_avg_time);

			print_best_x(stat_file,m_alg_stat.bst_ind);
			write_stat_vals();

			cout<<endl;// flush cout output
			return 0;
		}// end function Run
Example #2
0
File: ga.c Project: haiy/XF_PRISM
/*
psz the population size
n the generations number to run
pc the cross over probability
pm the mutation probability
*/
chro_ptr ga(mic_matrix M,int psz,int n,float pc,float pm)
{
    srand((double)time(NULL));
    population pop;
    pop.n=psz;
    pop.acu=(float*)malloc(sizeof(float)*pop.n);
    if(pop.acu==NULL)
    {
        puts("GA acu memory error!");
        exit(1);
    }
    int i=0;
    for(i=0;i<pop.n;i++)
    {
        pop.acu[i]=0;
    }
    pop.pc=pc;
    pop.pm=pm;
    ini_pop(M,&pop);
    population *S=ini_tmpop(pop);
    int T=pop.n;
    int Gn=0;
    int N=n; 
    do
    {//代数不够或者没达到最优解
//#define check_ga
#ifdef check_ga
        printf("The %d generation's best %f.\n",Gn,pop.m[0].sig);
#endif
        int n=0;
        cal_acu(&pop);
        do
        {//种群的成员数不够
            chro_ptr father=sel_one(pop);
            chro_ptr mother=sel_one(pop);
            cpy_chro(father,&(S->m[n]));
            cpy_chro(mother,&(S->m[n+1]));
            if(CY(pop))
            {
                cross_over(&(S->m[n]),&(S->m[n+1]));
            }
            if(MT(pop))
            {
                mutation(&(S->m[n]));
            }
            if(MT(pop))
            {
                mutation(&(S->m[n+1]));
            }
            n+=2;
        }while(n<T);
        update_pop(S,M);
        elitist_sel(&pop,S);
    }while(++Gn<N);
    chro_ptr bst=(chro_ptr)malloc(sizeof(chrosome));
    bst->l=pop.m[0].l;
    bst->chro=(int *)malloc(sizeof(int)*(bst->l));
    cpy_chro(&(pop.m[0]),bst);
    brk_pop(&pop);
    brk_pop(S);
    return bst;
}
Example #3
0
int main (int argc, char **argv)
{
    int i;
    int index, index1, index2;
    FILE *fpt1;
    FILE *fpt2;
    FILE *fpt3;
    FILE *fpt4;
    FILE *fpt5;
    individual *ea;
    individual *parent1, *parent2, *child1, *child2;
    ind_list *elite, *cur;
    if (argc<2)
    {
        printf("\n Usage ./main random_seed \n");
        exit(1);
    }
    seed = (double)atof(argv[1]);
    if (seed<=0.0 || seed>=1.0)
    {
        printf("\n Entered seed value is wrong, seed value must be in (0,1) \n");
        exit(1);
    }
    fpt1 = fopen("initial_pop.out","w");
    fpt2 = fopen("final_pop.out","w");
    fpt3 = fopen("final_archive.out","w");
    fpt4 = fopen("all_archive.out","w");
    fpt5 = fopen("params.out","w");
    fprintf(fpt1,"# This file contains the data of initial population\n");
    fprintf(fpt2,"# This file contains the data of final population\n");
    fprintf(fpt3,"# This file contains the best obtained solution(s)\n");
    fprintf(fpt4,"# This file contains the data of archive for all generations\n");
    fprintf(fpt5,"# This file contains information about inputs as read by the program\n");
    printf("\n Enter the problem relevant and algorithm relevant parameters ... ");
    printf("\n Enter the population size (>1) : ");
    scanf("%d",&popsize);
    if (popsize<2)
    {
        printf("\n population size read is : %d",popsize);
        printf("\n Wrong population size entered, hence exiting \n");
        exit (1);
    }
    printf("\n Enter the number of function evaluations : ");
    scanf("%d",&neval);
    if (neval<popsize)
    {
        printf("\n number of function evaluations read is : %d",neval);
        printf("\n Wrong nuber of evaluations entered, hence exiting \n");
        exit (1);
    }
    printf("\n Enter the number of objectives (>=2): ");
    scanf("%d",&nobj);
    if (nobj<2)
    {
        printf("\n number of objectives entered is : %d",nobj);
        printf("\n Wrong number of objectives entered, hence exiting \n");
        exit (1);
    }
    epsilon = (double *)malloc(nobj*sizeof(double));
    min_obj = (double *)malloc(nobj*sizeof(double));
    for (i=0; i<nobj; i++)
    {
        printf("\n Enter the value of epsilon[%d] : ",i+1);
        scanf("%lf",&epsilon[i]);
        if (epsilon[i]<=0.0)
        {
            printf("\n Entered value of epsilon[%d] is non-positive, hence exiting\n",i+1);
            exit(1);
        }
        printf("\n Enter the value of min_obj[%d] (if not known, enter 0.0) : ",i+1);
        scanf("%lf",&min_obj[i]);
    }
    printf("\n Enter the number of constraints : ");
    scanf("%d",&ncon);
    if (ncon<0)
    {
        printf("\n number of constraints entered is : %d",ncon);
        printf("\n Wrong number of constraints enetered, hence exiting \n");
        exit (1);
    }
    printf("\n Enter the number of real variables : ");
    scanf("%d",&nreal);
    if (nreal<0)
    {
        printf("\n number of real variables entered is : %d",nreal);
        printf("\n Wrong number of variables entered, hence exiting \n");
        exit (1);
    }
    if (nreal != 0)
    {
        min_realvar = (double *)malloc(nreal*sizeof(double));
        max_realvar = (double *)malloc(nreal*sizeof(double));
        for (i=0; i<nreal; i++)
        {
            printf ("\n Enter the lower limit of real variable %d : ",i+1);
            scanf ("%lf",&min_realvar[i]);
            printf ("\n Enter the upper limit of real variable %d : ",i+1);
            scanf ("%lf",&max_realvar[i]);
            if (max_realvar[i] <= min_realvar[i])
            {
                printf("\n Wrong limits entered for the min and max bounds of real variable %d, hence exiting \n",i+1);
                exit(1);
            }
        }
        printf ("\n Enter the probability of crossover of real variable (0.6-1.0) : ");
        scanf ("%lf",&pcross_real);
        if (pcross_real<0.0 || pcross_real>1.0)
        {
            printf("\n Probability of crossover entered is : %e",pcross_real);
            printf("\n Entered value of probability of crossover of real variables is out of bounds, hence exiting \n");
            exit (1);
        }
        printf ("\n Enter the probablity of mutation of real variables (1/nreal) : ");
        scanf ("%lf",&pmut_real);
        if (pmut_real<0.0 || pmut_real>1.0)
        {
            printf("\n Probability of mutation entered is : %e",pmut_real);
            printf("\n Entered value of probability of mutation of real variables is out of bounds, hence exiting \n");
            exit (1);
        }
        printf ("\n Enter the value of distribution index for crossover (5-20): ");
        scanf ("%lf",&eta_c);
        if (eta_c<=0)
        {
            printf("\n The value entered is : %e",eta_c);
            printf("\n Wrong value of distribution index for crossover entered, hence exiting \n");
            exit (1);
        }
        printf ("\n Enter the value of distribution index for mutation (5-50): ");
        scanf ("%lf",&eta_m);
        if (eta_m<=0)
        {
            printf("\n The value entered is : %e",eta_m);
            printf("\n Wrong value of distribution index for mutation entered, hence exiting \n");
            exit (1);
        }
    }
    printf("\n Enter the number of binary variables : ");
    scanf("%d",&nbin);
    if (nbin<0)
    {
        printf ("\n number of binary variables entered is : %d",nbin);
        printf ("\n Wrong number of binary variables entered, hence exiting \n");
        exit(1);
    }
    if (nbin != 0)
    {
        nbits = (int *)malloc(nbin*sizeof(int));
        min_binvar = (double *)malloc(nbin*sizeof(double));
        max_binvar = (double *)malloc(nbin*sizeof(double));
        for (i=0; i<nbin; i++)
        {
            printf ("\n Enter the number of bits for binary variable %d : ",i+1);
            scanf ("%d",&nbits[i]);
            if (nbits[i] < 1)
            {
                printf("\n Wrong number of bits for binary variable entered, hence exiting");
                exit(1);
            }
            printf ("\n Enter the lower limit of binary variable %d : ",i+1);
            scanf ("%lf",&min_binvar[i]);
            printf ("\n Enter the upper limit of binary variable %d : ",i+1);
            scanf ("%lf",&max_binvar[i]);
            if (max_binvar[i] <= min_binvar[i])
            {
                printf("\n Wrong limits entered for the min and max bounds of binary variable entered, hence exiting \n");
                exit(1);
            }
        }
        printf ("\n Enter the probability of crossover of binary variable (0.6-1.0): ");
        scanf ("%lf",&pcross_bin);
        if (pcross_bin<0.0 || pcross_bin>1.0)
        {
            printf("\n Probability of crossover entered is : %e",pcross_bin);
            printf("\n Entered value of probability of crossover of binary variables is out of bounds, hence exiting \n");
            exit (1);
        }
        printf ("\n Enter the probability of mutation of binary variables (1/nbits): ");
        scanf ("%lf",&pmut_bin);
        if (pmut_bin<0.0 || pmut_bin>1.0)
        {
            printf("\n Probability of mutation entered is : %e",pmut_bin);
            printf("\n Entered value of probability  of mutation of binary variables is out of bounds, hence exiting \n");
            exit (1);
        }
    }
    if (nreal==0 && nbin==0)
    {
        printf("\n Number of real as well as binary variables, both are zero, hence exiting \n");
        exit(1);
    }
    printf("\n Input data successfully entered, now performing initialization \n");
    fprintf(fpt5,"\n Population size = %d",popsize);
    fprintf(fpt5,"\n Number of function evaluations = %d",neval);
    fprintf(fpt5,"\n Number of objective functions = %d",nobj);
    for (i=0; i<nobj; i++)
    {
        fprintf(fpt5,"\n Epsilon for objective %d = %e",i+1,epsilon[i]);
        fprintf(fpt5,"\n Minimum value of objective %d = %e",i+1,min_obj[i]);
    }
    fprintf(fpt5,"\n Number of constraints = %d",ncon);
    fprintf(fpt5,"\n Number of real variables = %d",nreal);
    if (nreal!=0)
    {
        for (i=0; i<nreal; i++)
        {
            fprintf(fpt5,"\n Lower limit of real variable %d = %e",i+1,min_realvar[i]);
            fprintf(fpt5,"\n Upper limit of real variable %d = %e",i+1,max_realvar[i]);
        }
        fprintf(fpt5,"\n Probability of crossover of real variable = %e",pcross_real);
        fprintf(fpt5,"\n Probability of mutation of real variable = %e",pmut_real);
        fprintf(fpt5,"\n Distribution index for crossover = %e",eta_c);
        fprintf(fpt5,"\n Distribution index for mutation = %e",eta_m);
    }
    fprintf(fpt5,"\n Number of binary variables = %d",nbin);
    if (nbin!=0)
    {
        for (i=0; i<nbin; i++)
        {
            fprintf(fpt5,"\n Number of bits for binary variable %d = %d",i+1,nbits[i]);
            fprintf(fpt5,"\n Lower limit of binary variable %d = %e",i+1,min_binvar[i]);
            fprintf(fpt5,"\n Upper limit of binary variable %d = %e",i+1,max_binvar[i]);
        }
        fprintf(fpt5,"\n Probability of crossover of binary variable = %e",pcross_bin);
        fprintf(fpt5,"\n Probability of mutation of binary variable = %e",pmut_bin);
    }
    fprintf(fpt5,"\n Seed for random number generator = %e",seed);
    bitlength = 0;
    if (nbin!=0)
    {
        for (i=0; i<nbin; i++)
        {
            bitlength += nbits[i];
        }
    }
    fprintf(fpt1,"# of objectives = %d, # of constraints = %d, # of real_var = %d, # of bits of bin_var = %d, constr_violation\n",nobj,ncon,nreal,bitlength);
    fprintf(fpt2,"# of objectives = %d, # of constraints = %d, # of real_var = %d, # of bits of bin_var = %d, constr_violation\n",nobj,ncon,nreal,bitlength);
    fprintf(fpt3,"# of objectives = %d, # of constraints = %d, # of real_var = %d, # of bits of bin_var = %d, constr_violation\n",nobj,ncon,nreal,bitlength);
    fprintf(fpt4,"# of objectives = %d, # of constraints = %d, # of real_var = %d, # of bits of bin_var = %d, constr_violation\n",nobj,ncon,nreal,bitlength);
    nbinmut = 0;
    nrealmut = 0;
    nbincross = 0;
    nrealcross = 0;
    currenteval = 0;
    elite_size = 0;
    randomize();
    ea = (individual *)malloc(popsize*sizeof(individual));
    array = (int *)malloc(popsize*sizeof(int));
    for (i=0; i<popsize; i++)
    {
        allocate (&ea[i]);
        initialize(&ea[i]);
        decode(&ea[i]);
        eval(&ea[i]);
    }
    report_pop (ea, fpt1);
    elite = (ind_list *)malloc(sizeof(ind_list));
    elite->ind = (individual *)malloc(sizeof(individual));
    allocate (elite->ind);
    elite->parent = NULL;
    elite->child = NULL;
    insert (elite, &ea[0]);
    for (i=1; i<popsize; i++)
    {
        update_elite (elite, &ea[i]);
    }
    child1 = (individual *)malloc(sizeof(individual));
    allocate (child1);
    child2 = (individual *)malloc(sizeof(individual));
    allocate (child2);
    cur = elite;
    while (currenteval<neval)
    {
        index1 = rnd(0, popsize-1);
        index2 = rnd(0, popsize-1);
        parent1 = tournament (&ea[index1], &ea[index2]);
        index = rnd(0, elite_size-1);
        cur = elite->child;
        for (i=1; i<=index; i++)
        {
            cur=cur->child;
        }
        parent2 = cur->ind;
        crossover (parent1, parent2, child1, child2);
        mutation (child1);
        decode (child1);
        eval (child1);
        update_elite (elite, child1);
        update_pop (ea, child1);
        mutation (child2);
        decode (child2);
        eval (child2);
        update_elite (elite, child2);
        update_pop (ea, child2);
        printf("\n Currenteval = %d and Elite_size = %d",currenteval,elite_size); 
		/* Comment following three lines if information at all
		evaluation is not desired, it will speed up execution of the code */
	fprintf(fpt4,"# eval id = %d\n",currenteval);
		report_archive (elite, fpt4);
		fflush(fpt4);
    }
    printf("\n Generations finished, now reporting solutions");
    report_pop (ea, fpt2);
    report_archive (elite, fpt3);
    if (nreal!=0)
    {
        fprintf(fpt5,"\n Number of crossover of real variable = %d",nrealcross);
        fprintf(fpt5,"\n Number of mutation of real variable = %d",nrealmut);
    }
    if (nbin!=0)
    {
        fprintf(fpt5,"\n Number of crossover of binary variable = %d",nbincross);
        fprintf(fpt5,"\n Number of mutation of binary variable = %d",nbinmut);
    }
    fflush(stdout);
    fflush(fpt1);
    fflush(fpt2);
    fflush(fpt3);
    fflush(fpt4);
    fflush(fpt5);
    fclose(fpt1);
    fclose(fpt2);
    fclose(fpt3);
    fclose(fpt4);
    fclose(fpt5);
    if (nreal!=0)
    {
        free (min_realvar);
        free (max_realvar);
    }
    if (nbin!=0)
    {
        free (min_binvar);
        free (max_binvar);
        free (nbits);
    }
    free (epsilon);
    free (min_obj);
    free (array);
    for (i=0; i<popsize; i++)
    {
        deallocate (&ea[i]);
    }
    free (ea);
    cur = elite->child;
    while (cur!=NULL)
    {
        cur = del(cur);
        cur = cur->child;
    }
    deallocate (elite->ind);
    free (elite->ind);
    free (elite);
    printf("\n Routine successfully exited \n");
    return (0);
}