コード例 #1
0
ファイル: defc_v2.c プロジェクト: LorenzoPaciotti/DEFCM
void init(int n, int c, int d) {
    puts("");
    int row;
    //###INIT POPOLAZIONE 0
    //init V e calcolo U

    for (pop_index = 0; pop_index < num_pop_iniziale; pop_index++) {
        //alloc struttura
        POP_NEW[pop_index] = malloc(sizeof (el_pop));

        //alloc V_p
        POP_NEW[pop_index] -> V_p = malloc(c * sizeof (double*));
        for (row = 0; row < c; row++) {
            POP_NEW[pop_index] -> V_p[row] = malloc(d * sizeof (double));
        }
        //init V_p
        for (i = 0; i < c; i++) {
            for (j = 0; j < d; j++) {
				srand48(time(0));
				int riga = random_at_most(n-1);
				POP_NEW[pop_index] -> V_p[i][j] = X[riga][j]+drand48();
                //POP_NEW[pop_index] -> V_p[i][j] = X[random_at_most(n-1)][random_at_most(d-1)];//+drand48();//;
                //POP_NEW[pop_index] -> V_p[i][j] = drand48();
                //POP_NEW[pop_index] -> V_p[i][j] = dbl_rnd_inRange(0,range_init_max);
                //POP_NEW[pop_index] -> V_p[i][j] = random_at_most(range_init_max);
            }
        }

        //alloc U_p
        POP_NEW[pop_index] -> U_p = malloc(c * sizeof (double*));
        for (row = 0; row < c; row++) {
            POP_NEW[pop_index] -> U_p[row] = malloc(n * sizeof (double));
        }

        //init U
        for (i = 0; i < c; i++) {
            for (j = 0; j < n; j++) {
                double denom = 0.0;
                double dist_x_j__v_i = calcDistanza(X[j], POP_NEW[pop_index] -> V_p[i]);

                int k;
                for (k = 0; k < c; k++) {
                    double dist_xj_vk = calcDistanza(X[j], POP_NEW[pop_index] -> V_p[k]);
                    denom += pow((dist_x_j__v_i / dist_xj_vk), esponente_U);
                }
                POP_NEW[pop_index] -> U_p[i][j] = 1.0 / denom;
                if (POP_NEW[pop_index] -> U_p[i][j] < 0) {
                    printf("ERR: INIT POP:inizializzato U di un membro con negativo::");
                    exit(-1);
                }
            }
        }

        //calcolo fitness
        double xb = calcolaFitness(POP_NEW[pop_index]->V_p, POP_NEW[pop_index]->U_p, 0);
        POP_NEW[pop_index]->fitness = xb;
    }
    //###END INIT POPOLAZIONE 0
    puts("");
}
コード例 #2
0
ファイル: minheap.c プロジェクト: dr107/CPrimer
/*
  Generate an array of siz random numbers
 */
int *genarr(unsigned siz) {
        int *arr=malloc(siz*sizeof(int));
        for(unsigned i=0; i<siz; i++) {
                arr[i]=random_at_most(siz);
        }
        return arr;
}
コード例 #3
0
ファイル: rand.c プロジェクト: otikkito/cWorld
int main(){
    srand(time(NULL));   // should only be called once to set the seed
    int r = rand();      // returns a pseudo-random integer between 0 and RAND_MAX
    printf("The value of RAND_MAX = %d and the random number generated is: %d\n",RAND_MAX, r);
    int j;
    for( j=0; j<100 ;j++){
        printf("The value of the random integer in the range of 0 and 30 is: %ld\n",random_at_most(30));
    }
    
    
}
void randomization_init(int sublattice[], int * c, int * Number_up, int * Number_down, int sublatticesize)
{
	int max_index = sublatticesize - 1;
	int site = 0;
	while (*c > 0) {
        	site = random_at_most(max_index);  /*select a random site to make "up", within the array*/
		if (sublattice[site] == DOWN) {  /*make sure that site is "down"*/
        		sublattice[site] = UP;   /*make the "down" site "up"*/
	       		*c = *c - 1;                         /*decrement c, as we have decreased the amount of sites we need to make "up" by 1*/
        		*Number_up = *Number_up + 1;       /*increase by 1 the amount of "up" sites*/
	       		*Number_down = sublatticesize - (*Number_up);   /*number_down = Num_of_sites - Number_up*/
        	}
    	}
}
void neighbor_find_update(int lattice[], int array[], int * number_up, int * number_down, double p)
{ 
	int latticesize = array[10];
	int ss = random_at_most((latticesize - 1)); // pick random site within lattice  //int global_ss = rsn + (taskid)*(sublatticesize);
	int ss2 = 0, rn = 0, ln = 0;
	double p_cA = 0;
	double p_cB = 0;
	p_cA = ((double)rand()) / ((double)RAND_MAX);
	p_cB = ((double)rand()) / ((double)RAND_MAX);
	global_assignments(ss, &ss2, &ln, &rn,latticesize); // determine what the four sites of interest are. PROBLEM!!!!!
	
	array[5] = ln;    //ln
	array[6] = ss;    // ss, not ss2 (THIS IS the global version of rsn)
	array[7] = rn;    //rn

	if (lattice[ss] == lattice[ss2]) {
	  update_neighbors(lattice, array, p_cA, p_cB, p);
	}
}
コード例 #6
0
int main(int argc, char* argv[]) {
	//Set cpu affinity
	cpu_set_t set;
	CPU_ZERO(&set);
	CPU_SET(0, &set);
	if (sched_setaffinity(0, sizeof(cpu_set_t), &set) == -1) {
		printf("Affinity Failed!\n");
		exit(1);
	}

	int size = 4096;
	int runs = 100000;

	if (argc == 2) {
		size = atoi(argv[1]);
	} else {
	}

	//Make array
	int* arr = (int*)malloc(size);
	memset(arr, 1234, size);
	int n_items = size/sizeof(int);
	int item;

	//Measure latency
	unsigned long long start, end, diff;
	start = rdtsc();
	int i;
	for (i = 0; i < runs; i++) {
		int rand = random_at_most(n_items - 1);
		item = arr[rand];
	}
	end = rdtsc();
	diff = end - start;
	printf("%llu,%i,%i,%i\n", diff/runs, size, n_items, i);
	
	free(arr);
}
コード例 #7
0
ファイル: defc_v2.c プロジェクト: LorenzoPaciotti/DEFCM
void lavora(int n, int c, int d) {
    int row;
    numero_generazione_attuale = 0;
    do {//NUOVA GENERAZIONE
        //SCAMBIO VETTORI POPOLAZIONE
        numero_generazione_attuale++;
        int i_target;
        for (i_target = 0; i_target < num_pop_iniziale; i_target++) {
            if (POP_NEW[i_target] != POP_NOW[i_target]) {
                if (POP_NOW[i_target] != 0) {
                    for (row = 0; row < c; row++) {
                        free(POP_NOW[i_target] -> V_p[row]);
                        free(POP_NOW[i_target] -> U_p[row]);
                    }
                    free(POP_NOW[i_target]->V_p);
                    free(POP_NOW[i_target]->U_p);
                    free(POP_NOW[i_target]);
                }
                POP_NOW[i_target] = POP_NEW[i_target];
            }
        }
        /////////////////DE////////////////////
        for (i_target = 0; i_target < num_pop_iniziale; i_target++) {//PER OGNI COMPONENTE DELLA POP
            /*if(indice_base == i_target){
                printf("!");
                continue;
            }*/
                
            //SCELTA CANDIDATI
            //tre vettori devono essere scelti a caso nella popolazione
            //diversi dal target (indice i) e mutualmente
            
            int indice_1, indice_2, indice_base;
            do {
                indice_1 = random_at_most(((long) num_pop_iniziale) - 1);
            } while (indice_1 == i_target); // || indice_1 == indice_base

            do {
                indice_2 = random_at_most(((long) num_pop_iniziale) - 1);
            } while (indice_2 == i_target || indice_2 == indice_1); // || indice_2 == indice_base

            do {
                indice_base = random_at_most(((long) num_pop_iniziale) - 1);
            } while (indice_base == i_target || indice_base == indice_1 || indice_base == indice_2);

            //l'elemento mutante
            el_pop *mutant = malloc(sizeof (el_pop));
            //alloc V_p del mutante
            mutant -> V_p = malloc(c * sizeof (double*));
            for (row = 0; row < c; row++) {
                mutant -> V_p[row] = malloc(d * sizeof (double));
            }
            //alloc U_p mutante
            mutant -> U_p = malloc(c * sizeof (double*));
            for (row = 0; row < c; row++) {
                mutant -> U_p[row] = malloc(n * sizeof (double));
            }


            //MUTATION

            int i1, j1;
            for (i1 = 0; i1 < c; i1++) {
				double f;
                if (tipo_dw == 2)//dithering
                    f = dbl_rnd_inRange(dw_lowerbound, dw_upperbound);
                else if (tipo_dw == 3) //dither + jitter
                    f = dbl_rnd_inRange(dw_lowerbound, dw_upperbound) + 0.001 * (dbl_rnd_inRange(0, 1) - 0.5);
                else
                    f = dw_upperbound;
                for (j1 = 0; j1 < d; j1++) {
                    mutant->V_p[i1][j1] = POP_NOW[indice_base]->V_p[i1][j1] + f * (POP_NOW[indice_1]->V_p[i1][j1] - POP_NOW[indice_2]->V_p[i1][j1]);
                }
            }
            //CROSSOVER CON IL VETTORE TARGET (TIPO 1)
            for (i1 = 0; i1 < c; i1++) {
                double prob_crossover = dbl_rnd_inRange(0.0, 1.0);
                for (j1 = 0; j1 < d; j1++) {
                    if (prob_crossover < CR) {
                        //prendo il cromosoma del target
                        mutant->V_p[i1][j1] = POP_NOW[i_target]->V_p[i1][j1];
                    }
                }
            }

            //CROSSOVER CON IL VETTORE ATTUALE (TIPO 2) (BAD)
            /*for (i1 = 0; i1 < c; i1++) {
                for (j1 = 0; j1 < d; j1++) {
                    double prob_crossover = fRand(0.0, 1.0);
                    if (prob_crossover < CR) {
                        mutant->V_p[i1][j1] = POP_NOW[i_CPop]->V_p[i1][j1];
                    }
                }
            }*/

            //CROSSOVER CON IL VETTORE ATTUALE (TIPO 3)
            /*for (i1 = 0; i1 < c; i1++) {
                    double prob_crossover = fRand(0.0, 1.0);
                    if (prob_crossover < CR) {
                        //prendo tutto il vettore del compagno
                        copiaVettore(d,POP_NOW[i_CPop]->V_p[i1],mutant->V_p[i1]);
                    }
            }*/

            ///CALCOLO FITNESS DEL MUTANTE
            //calcolo U mutante
            for (i = 0; i < c; i++) {
                for (j = 0; j < n; j++) {
                    double denom = 0.0;
                    double dist_x_j__v_i = calcDistanza(X[j], mutant -> V_p[i]);
                    if (dist_x_j__v_i == 0) {
                        puts("calcolo U mutante, distanza nulla");
                        exit(-1);
                    }
                    int k;
                    for (k = 0; k < c; k++) {
                        double dist_xj_vk = calcDistanza(X[j], mutant -> V_p[k]);
                        if (dist_xj_vk == 0) {
                            puts("calcolo U mutante, distanza nulla");
                            exit(-1);
                        }
                        denom += pow((dist_x_j__v_i / dist_xj_vk), esponente_U);
                        if (denom == 0) {
                            puts("calcolo U mutante, denom nullo");
                            exit(-1);
                        }
                    }
                    mutant -> U_p[i][j] = 1.0 / denom;
                }
            }

            //calcolo fitness mutante            
            mutant->fitness = calcolaFitness(mutant->V_p, mutant->U_p, 1);

            //SELECTION
            //TIPO 2 (STANDARD)
            if (mutant->fitness < POP_NOW[i_target]->fitness) {
                xb_selezionato = mutant->fitness;
                POP_NEW[i_target] = mutant;
            } else {
                for (row = 0; row < c; row++) {
                    free(mutant -> V_p[row]);
                    free(mutant -> U_p[row]);
                }
                free(mutant->V_p);
                free(mutant->U_p);
                free(mutant);
                POP_NEW[i_target] = POP_NOW[i_target];
                xb_selezionato = POP_NOW[i_target]->fitness;
            }
        }//END DE
        numero_generazioni--;
    } while (numero_generazioni > 0);


    //computazione fitness della popolazione finale
    double best_fitness = DBL_MAX;
    int indice_best;
    for (pop_index = 0; pop_index < num_pop_iniziale; pop_index++) {
        POP_NEW[pop_index]->fitness = calcolaFitness(POP_NEW[pop_index]->V_p, POP_NEW[pop_index]->U_p, 0);
        if (POP_NEW[pop_index]->fitness < best_fitness) {
            best_fitness = POP_NEW[pop_index]->fitness;
            indice_best = pop_index;
        }
    }
    //calcolo xb del migliore
    best_xb = calcolaXB(POP_NEW[indice_best]->V_p, POP_NEW[indice_best]->U_p, 0);

    printf("miglior XB:%lf\n", best_xb);
    fprintf(out_LOG_RIS, "\nmiglior XB:%lf\n\n", best_xb);
    stampaMatriceSuFile(c, d, POP_NEW[indice_best]->V_p, out_LOG_RIS);
    puts("matrice V:");
    stampaMatrice(c, d, POP_NEW[indice_best]->V_p);
    stampaMatriceSuFile(c, d, POP_NEW[indice_best]->V_p, out_V);
    stampaMatriceSuFile(c, n, POP_NEW[indice_best]->U_p, out_U);
    puts("***********************************************");
}
コード例 #8
0
ファイル: defc_v7.c プロジェクト: LorenzoPaciotti/DEFCM
void lavora(int n, int c, int d) {
    int row;
    numero_generazione_attuale = 0;
    do {//NUOVA GENERAZIONE
        //SCAMBIO VETTORI POPOLAZIONE
        conteggio_successi_generazione_attuale = 0;
        numero_generazione_attuale++;
        int i_target;
        for (i = 0; i < num_pop_iniziale; i++) {
            if (POP_NEW[i] != POP_NOW[i]) {
                if (POP_NOW[i] != 0) {//FREE
                    for (row = 0; row < c; row++) {
                        free(POP_NOW[i] -> V_p[row]);
                        free(POP_NOW[i] -> U_p[row]);
                    }
                    free(POP_NOW[i]->V_p);
                    free(POP_NOW[i]->U_p);
                    free(POP_NOW[i]);
                }
                POP_NOW[i] = POP_NEW[i]; //l'individuo era stato sostituito
            }
        }


        /////////////////DE////////////////////
        for (i_target = 0; i_target < num_pop_iniziale; i_target++) {//PER OGNI COMPONENTE DELLA POP
            //SCELTA CANDIDATI
            //tre vettori devono essere scelti a caso nella popolazione
            //diversi dal target (indice i) e mutualmente

            int indice_1, indice_2, indice_base;
            do {
                indice_1 = random_at_most(((long) num_pop_iniziale) - 1);
            } while (indice_1 == i_target); // || indice_1 == indice_base

            do {
                indice_2 = random_at_most(((long) num_pop_iniziale) - 1);
            } while (indice_2 == i_target || indice_2 == indice_1); // || indice_2 == indice_base

            do {
                indice_base = random_at_most(((long) num_pop_iniziale) - 1);
            } while (indice_base == i_target || indice_base == indice_1 || indice_base == indice_2);

            //l'elemento mutante
            el_pop *mutant = malloc(sizeof (el_pop));
            //alloc V_p del mutante
            mutant -> V_p = malloc(c * sizeof (double*));
            for (row = 0; row < c; row++) {
                mutant -> V_p[row] = malloc(d * sizeof (double));
            }
            //alloc U_p mutante
            mutant -> U_p = malloc(c * sizeof (double*));
            for (row = 0; row < c; row++) {
                mutant -> U_p[row] = malloc(n * sizeof (double));
            }


            int i1, j1;
            double f;
            ///MUTATION (TIPO 1 rand-rand-rand) singolo punto
            //scelta f
            if (tipo_dw == 2)//dithering
                f = dbl_rnd_inRange(dw_lowerbound, dw_upperbound);
            else if (tipo_dw == 3) //dither + jitter
                f = dbl_rnd_inRange(dw_lowerbound, dw_upperbound) + 0.001 * (dbl_rnd_inRange(0, 1) - 0.5);
            else
                f = dw_upperbound;

            for (i1 = 0; i1 < c; i1++) {//centroide
                for (j1 = 0; j1 < d; j1++) {
                    mutant->V_p[i1][j1] = POP_NOW[indice_base]->V_p[i1][j1] + f * (POP_NOW[indice_1]->V_p[i1][j1] - POP_NOW[indice_2]->V_p[i1][j1]);
                }
            }

            //MUTATION (TIPO 2 best-rand-rand) con f omogeneo per vettore dopo 3/4 delle gen., rand-rand-rand altrimenti
            /*aggiornaVettoreFitness(); //per il best
            for (i1 = 0; i1 < c; i1++) {
                if (tipo_dw == 2)//dithering
                    f = dbl_rnd_inRange(dw_lowerbound, dw_upperbound);
                else if (tipo_dw == 3) //dither + jitter
                    f = dbl_rnd_inRange(dw_lowerbound, dw_upperbound) + 0.001 * (dbl_rnd_inRange(0, 1) - 0.5);
                else
                    f = dw_upperbound;
                for (j1 = 0; j1 < d; j1++) {
                    if (numero_generazione_attuale > floor(numero_generazioni_iniziale * (7 / 8)))
                        mutant->V_p[i1][j1] = POP_NOW[bestFitIndex]->V_p[i1][j1] + f * (POP_NOW[indice_1]->V_p[i1][j1] - POP_NOW[indice_2]->V_p[i1][j1]);
                    else
                        mutant->V_p[i1][j1] = POP_NOW[indice_base]->V_p[i1][j1] + f * (POP_NOW[indice_1]->V_p[i1][j1] - POP_NOW[indice_2]->V_p[i1][j1]);
                }
            }*/


            //CROSSOVER CON IL VETTORE TARGET UNIFORME
            double prob_crossover;
            for (i1 = 0; i1 < c; i1++) {
                for (j1 = 0; j1 < d; j1++) {
                    prob_crossover = dbl_rnd_inRange(0.0, 1.0);
                    if (prob_crossover < CR) {
                        mutant->V_p[i1][j1] = POP_NOW[i_target]->V_p[i1][j1];
                    }
                }
            }

            //CROSSOVER SPLIT
            /*for (i1 = 0; i1 < c; i1++) {//centroide
                for (j1 = 0; j1 < d; j1++) {//coordinata centroide
                    if (i1 >= floor((double) c / 2)) {//prendo i cromosomi dal target so oltre il punto di CO
                        mutant->V_p[i1][j1] = POP_NOW[i_target]->V_p[i1][j1];
                    }
                }
            }*/

            //SORT MATRICE MUTANTE
            sortMatrice(mutant->V_p);

            ///CALCOLO FITNESS DEL MUTANTE
            //calcolo U mutante
            for (i = 0; i < c; i++) {
                for (j = 0; j < n; j++) {
                    double denom = 0.0;
                    double dist_x_j__v_i = calcDistanza(X[j], mutant -> V_p[i]);
                    if (dist_x_j__v_i == 0) {
                        puts("calcolo U mutante, distanza nulla");
                        exit(-1);
                    }
                    int k;
                    for (k = 0; k < c; k++) {
                        double dist_xj_vk = calcDistanza(X[j], mutant -> V_p[k]);
                        if (dist_xj_vk == 0) {
                            puts("calcolo U mutante, distanza nulla");
                            exit(-1);
                        }
                        denom += pow((dist_x_j__v_i / dist_xj_vk), esponente_U);
                        if (denom == 0) {
                            puts("calcolo U mutante, denom nullo");
                            exit(-1);
                        }
                    }
                    mutant -> U_p[i][j] = 1.0 / denom;
                }
            }

            //calcolo fitness mutante   
            mutant->fitness = calcolaFitness(mutant->V_p, mutant->U_p, 1);

            //impostazione timer età mutante
            mutant -> age = starting_age;

            //SELECTION
            //TIPO 2 (STANDARD)
            if (mutant->fitness < POP_NOW[i_target]->fitness) {//IL MUTANTE RIMPIAZZA IL TGT
                POP_NEW[i_target] = mutant;
                fitness_vector[i_target] = mutant->fitness;
                conteggio_successi_generazione_attuale++;
            } else {//IL MUTANTE è SCARTATO
                for (row = 0; row < c; row++) {
                    free(mutant -> V_p[row]);
                    free(mutant -> U_p[row]);
                }
                free(mutant->V_p);
                free(mutant->U_p);
                free(mutant);

                //INVECCHIAMENTO del sopravvissuto
                aggiornaVettoreFitness(); //MIGLIORE
                if (i_target != bestFitIndex) { //se non è il migliore invecchia
                    if (POP_NOW[i_target] -> age > 0)//non sono in modalità reset
                        POP_NOW[i_target] -> age--;
                    if (POP_NOW[i_target] -> age <= 0) {//morte
                        //RINASCITA
                        //REINIT V_p
                        for (i = 0; i < c; i++) {
                            for (j = 0; j < d; j++) {
                                POP_NOW[i_target]->V_p[i][j] = drand48();
                            }
                        }
                        //SORT MATRICE V
                        sortMatrice(POP_NOW[i_target]->V_p);

                        //calcola U
                        for (i = 0; i < c; i++) {
                            for (j = 0; j < n; j++) {
                                double denom = 0.0;
                                double dist_x_j__v_i = calcDistanza(X[j], POP_NOW[i_target] -> V_p[i]);

                                int k;
                                for (k = 0; k < c; k++) {
                                    double dist_xj_vk = calcDistanza(X[j], POP_NOW[i_target] -> V_p[k]);
                                    denom += pow((dist_x_j__v_i / dist_xj_vk), esponente_U);
                                }
                                POP_NOW[i_target] -> U_p[i][j] = 1.0 / denom;
                                if (POP_NOW[i_target] -> U_p[i][j] < 0) {
                                    printf("ERR: INIT POP:inizializzato U di un membro con negativo::");
                                    exit(-1);
                                }
                            }
                        }

                        //calcolo fitness
                        double fit = calcolaFitness(POP_NOW[i_target]->V_p, POP_NOW[i_target]->U_p, 0);
                        POP_NOW[i_target]->fitness = fit;
                        fitness_vector[i_target] = fit;

                        //impostazione età
                        POP_NOW[i_target] -> age = starting_age;
                    }
                }

                POP_NEW[i_target] = POP_NOW[i_target];
            }
            //END SELECTION
        }//END DE//END GENERATION
        numero_generazioni--;

        //ADATTAMENTO PARAMETRI
        if (conteggio_successi_generazione_attuale == 0 && ultimo_conteggio_successi == 0) {
            printf("DW!\n");
            dw_upperbound = dbl_rnd_inRange(dw_upperbound - 0.1, dw_upperbound + 0.1);
            conteggio_adattamenti++;
        }

        //RESET
        if (conteggio_adattamenti == reset_threshold) {
            puts("#RESET GLOBALE#");
            for (i = 0; i < num_pop; i++) {
                if (i != bestFitIndex)
                    POP_NOW[i] -> age = 0;
            }
            conteggio_adattamenti = 0; //reset contatore
        }
        //END RESET
        ultimo_conteggio_successi = conteggio_successi_generazione_attuale;
    } while (numero_generazioni > 0);
    //END GENERAZIONI

    puts("end lavora");

    //ULTIMO SCAMBIO POPOLAZIONI
    for (i = 0; i < num_pop_iniziale; i++) {
        if (POP_NEW[i] != POP_NOW[i]) {
            if (POP_NOW[i] != 0) {
                for (row = 0; row < c; row++) {
                    free(POP_NOW[i] -> V_p[row]);
                    free(POP_NOW[i] -> U_p[row]);
                }
                free(POP_NOW[i]->V_p);
                free(POP_NOW[i]->U_p);
                free(POP_NOW[i]);
            }
            POP_NOW[i] = POP_NEW[i];
        }
    }

    //computazione fitness della popolazione finale
    aggiornaVettoreFitness();

    //calcolo xb del migliore
    best_xb = calcolaXB(POP_NOW[bestFitIndex]->V_p, POP_NOW[bestFitIndex]->U_p, 0);

    printf("\nmiglior XB: \t%lf\n", best_xb);
    stampaMatrice(c, d, POP_NOW[bestFitIndex]->V_p);
    stampaMatriceSuFile(c, d, POP_NOW[bestFitIndex]->V_p, out_V);
    stampaMatriceSuFile(c, n, POP_NOW[bestFitIndex]->U_p, out_U);
    puts("###################################################################");
}