void RRSchedule::allocate3D(TripleVector & _invect, int row, int col, int depth) { _invect.clear(); _invect.reserve(row); for (int i = 0; i < row; i++) { allocate2D(_invect[i], col, depth); } }
void multigrid(int m, /* Malla en la que estamos */ double ***d, /* Defecto */ double ***v, int iteraciones_s1, int iteraciones_s2, const char * tipo) /* tipo de suavizador */ { int d_malla; /* dimension de la malla */ int d_malla_inf; /* dimension de la malla inferior */ double **fu; double **vg; if(m==0) { exacta(d[m],v[m]); printf("Exacta"); } else { d_malla=pow(2,m+1)+1; d_malla_inf=pow(2,m)+1; inicializa_cero(v[m],d_malla); fu=allocate2D(d_malla,d_malla); vg=allocate2D(d_malla,d_malla); copia_matriz(d[m],fu,d_malla); suaviza(v[m],fu,iteraciones_s1,tipo,d_malla); copia_matriz(v[m],vg,d_malla); calcula_defecto(d[m],fu,v[m],d_malla); ///muestra_matriz("d",d[m],d_malla,d_malla); restringe(d[m],d[m-1],d_malla_inf); multigrid(m-1,d,v,iteraciones_s1,iteraciones_s2,tipo); interpola(v[m-1],v[m],d_malla_inf,d_malla); suma_matrices(v[m],vg,d_malla); suaviza(v[m],fu,iteraciones_s2,tipo,d_malla); deallocate2D(fu,d_malla); deallocate2D(vg,d_malla); } }
int main (int argc, char const *argv[]) { const int N = 7; int **im; im = allocate2D(im, N); generate_image(im, N); print_image(im, N); rotate_image(im, N); printf("\n"); print_image(im, N); release2D(im, N); return 0; }
int main(int argc,char *argv[]) { int i,j; int n_mallas=4; int iteraciones_s1=1; /* Iteraciones del primer suavizador */ int iteraciones_s2=1; /* Iteraciones del segundo suavizador */ int dimension; int dim_inf; /* Dimension de la malla inferior */ double norma_defecto=1.0; double norma_defecto_anterior; double **u; /* Solución */ double **f; double ***v; double ***d; const char *tipo="gsrb"; const char *uso="\n"; /////////////////////////////////////////// // escaneo de argumentos // if((argc>1) && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) { printf (uso, argv[0]); exit(1); } for(i=1;i<argc;i++) { if(!strcmp(argv[i],"-mallas")) { n_mallas=atoi(argv[++i]); } else { fprintf(stderr,"Opción inválida '%s'\n `%s --help` para mas información\n", argv[i], argv[0]); exit(1); } } // Fin escaneo de argumentos // /////////////////////////////////////////// /////////////////////////////////////////// // Generamos los datos necesarios // // // // x . x . x // // . . . . . // // x . x . x // // . . . . . // // x . x . x n_mallas=2, dimension=5 // dimension = pow(2,n_mallas)+1; u=allocate2D(dimension,dimension); f=allocate2D(dimension,dimension); inicializa_cero(f,dimension); //muestra_matriz("f",f,dimension,dimension); inicializa(u,dimension); //muestra_matriz("u",u,dimension,dimension); v=(double***)malloc(n_mallas*sizeof(double**)); // v y d son matrices p d=(double***)malloc(n_mallas*sizeof(double**)); // en cada malla for(i=0;i<n_mallas;i++) { j=pow(2,i+1)+1; v[i]=allocate2D(j,j); d[i]=allocate2D(j,j); inicializa_cero(v[i],j); inicializa_cero(d[i],j); //muestra_matriz("v",v[i],j,j); } // Fin inicialización // //////////////////////////////////////////// //////////////////////////////////////////// // Bucle principal // for(i=0;i<20;i++) { dim_inf=(dimension+1)/2; /* Suavizado */ suaviza(u,f,iteraciones_s1,tipo,dimension); //muestra_matriz("u",u,dimension,dimension); /* Cálculo del defecto */ calcula_defecto(d[n_mallas-1],f,u,dimension); //muestra_matriz("d",d[n_mallas-1],dimension,dimension); /* Restringimos el defecto */ restringe(d[n_mallas-1],d[n_mallas-2],(dimension+1)/2); //muestra_matriz("d_",d[n_mallas-2],(dimension+1)/2,(dimension+1)/2); /* Llamamos a multigrid */ multigrid(n_mallas-2,d,v,iteraciones_s1,iteraciones_s2,tipo); //muestra_matriz("v_",v[n_mallas-2],(dimension+1)/2,(dimension+1)/2); /* Interpolamos */ interpola(v[n_mallas-2],v[n_mallas-1],(dimension+1)/2,dimension); //muestra_matriz("v",v[n_mallas-1],dimension,dimension); /* Sumamos */ suma_matrices(u,v[n_mallas-1],dimension); //muestra_matriz("u",u,dimension,dimension); /* Volvemos a suavizar */ suaviza(u,f,iteraciones_s2,tipo,dimension); //muestra_matriz("u",u,dimension,dimension); /* Comienza el test de convergencia */ calcula_defecto(d[n_mallas-1],f,u,dimension); //muestra_matriz("d",d[n_mallas-1],dimension,dimension); norma_defecto_anterior=norma_defecto; norma_defecto=calcula_max(d[n_mallas-1],dimension); printf("Iter: %d max(defecto)=%e\tratio=%0.10f\n",i,norma_defecto,norma_defecto/norma_defecto_anterior); } // Fin bucle principal // //////////////////////////////////////////// //////////////////////////////////////////// // Liberamos memoria // deallocate2D(u,dimension); deallocate2D(f,dimension); for(i=0;i<n_mallas;i++) { j=pow(2,i+1)+1; deallocate2D(v[i],j); deallocate2D(d[i],j); } free(v); free(d); return 0; }
//////////////////////////////////////////////////////////////////////////////////////////////// ///// ********** PUBLIC METHODS //////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////// RRSchedule::RRSchedule(int _max_weeks, int _max_times, int _max_courts, int _max_teams, char* _FILENAME, int SKIP_FIRST) { max_weeks = _max_weeks; max_times = _max_times; max_courts = _max_courts; max_teams = _max_teams; skip_first = SKIP_FIRST; FILENAME = _FILENAME; week0 = (skip_first > 0); // Check if special considerations need to be made for the first week (allowing a meeting) min_per_night = (max_courts * max_times * 2) / max_teams; w0_min_per_night = (max_courts * (max_times-skip_first) * 2) / max_teams; max_per_night = (max_courts * max_times * 2) / max_teams + ( ((max_courts * max_times * 2) % max_teams) != 0); w0_max_per_night = (max_courts * (max_times - skip_first) * 2) / max_teams + ( ((max_courts * (max_times - skip_first) * 2) % max_teams) != 0); MAX_PLAYED_GAP = 1; // <= Gap between min times played and max times played if (max_per_night > 2) then: { MAX_WAIT_TIME = min_per_night; // <= Max time a team can wait each night } else { MAX_WAIT_TIME = min_per_night; } MAX_WAIT_GAP = 1; //*max_times ; // /2; // <= Gap between min team wait time and max team wait time MAX_TIMESLOT_GAP = 2 ; //max_weeks / 2; // <= Gap between count of min timeslot vs. max timeslot appearances TIMESLOT_FUDGE = 0; // *max_per_night; // Allow teams to play in a timeslot # over "ideal" max_per_time = (max_weeks * max_courts * 2) / max_teams + ( ((max_weeks * max_courts * 2) % max_teams) != 0) + TIMESLOT_FUDGE; // std::cout << "**" <<std:endl; fullSolution = false; total_wait_time = 0; init1D(this_week_played, max_teams); allocate1D(courts, max_courts*2); init1D(total_played, max_teams); init1D(total_waiting_by_team, max_teams); init2D(opponent_counts, max_teams, max_teams); allocate2D(timeslots, max_times, max_courts*2); allocate2D(matchups, max_weeks * max_times * max_courts, 2); init2D(this_week_matchups, (max_times-skip_first)*max_courts, 2); init2D(timeslots_played, max_teams, max_times); init2D(courts_played, max_teams, max_courts); allocate2D(timePermutes, FACTS[max_times], max_times); compute_permutations(); allocate2D(total_this_week_played,max_weeks, max_teams); allocate3D(weeks, max_weeks, max_times, max_courts*2); // clear old file std::ofstream outputfile; outputfile.open(FILENAME); outputfile.close(); }
bool RRSchedule::add_week() // Add a full set of timeslots to the week { // Guarantee everyone played a sufficient number of times bool mincheck = (min_per_night <= VectMin(this_week_played)); if (week0) { int temp_min_per_night = (max_courts * (max_times - skip_first) * 2) / max_teams; mincheck = temp_min_per_night <= VectMin(this_week_played); } bool goodSort = false; bool acceptableWait = false; if (mincheck) { // Check to ensure sorting is acceptable? goodSort = sort_times_new(timeslots); if (goodSort) { Vector temp_team_waits = total_waiting_by_team; VectDotSum(temp_team_waits, compute_team_waits_for_week(timeslots)); acceptableWait = ( VectMax(temp_team_waits) - VectMin(temp_team_waits) <= MAX_WAIT_GAP); if (acceptableWait) { // Roll up timeslot information into the week and log weeks.push_back(timeslots); store_timeslot(); // No longer first week after successful addition of timeslot week0 = false; // Reset the timeslots and matchups for this week allocate2D(timeslots, max_times, max_courts*2); init2D(this_week_matchups, max_teams, max_teams); total_this_week_played.push_back(this_week_played); init1D(this_week_played,max_teams); compute_total_team_waits(); compute_fitness(); // Note progress for user printf ("Current Size: %d weeks (Max Weeks: %d). Total Wait Time: %d Fitness Level: %d (Scaled Fitness: %.2f) Printed Solutions: %d\n", int(weeks.size()), max_weeks, total_wait_time, fitness_level, scaled_fitness_level, PRINTED_SOLUTIONS); std::cout << weeks.size() << std::endl; if (weeks.size() == max_weeks) { printf("Full Solution?\n"); fullSolution = true; } else{ printf ("Not full Solution\n"); } } } } return mincheck and goodSort and acceptableWait; }