diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.hh b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.hh index 552b2648923ec9383125099fb7ad87e4f924c4a9..4d2423e81a83d39af2f15086836bac4c5673eb07 100644 --- a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.hh +++ b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.hh @@ -62,24 +62,26 @@ skeleton newGA Direction direction () const; - int cantDias() const; - int cantEmpleados() const; - int dimension() const; +//------------------------------------------------------------------------------------------------ + + int cantFilas() const; + int cantColumnas() const; - Empleado * empleados() const; - int * tareasEsf() const; - int * tareasIndex() const; - int ** limite_barrios() const; - const char* getfield(char* line, int num); + int** datosColumnas() const; + int** datosFilas() const; + + int* cantGruposColumna() const; + int* cantGruposFila() const; + + const char* getfield(char* line, int num); //??? private: - int _cantDias; - Empleado * _empleados; - int * _tareasEsf; - int * _tareasIndex; - int _cantEmpleados; - int _dimension; - int ** _limite_barrios; + int _cantFilas; + int _cantColumnas; + int** _datosColumnas; + int** _datosFilas; + int* _cantGruposColumna; + int* _cantGruposFila; }; //Solution ---------------------------------------------------------------------------- @@ -113,7 +115,7 @@ skeleton newGA Rarray<int>& array_var(); private: - Rarray<int> _var; + Rarray<Rarray<int>> _var; const Problem& _pbm; }; diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.req.cc b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.req.cc index 653d43b819666278652d4834d7116555127fdffd..c4a7d83225c62a0306ccf299c8651016a96cc10d 100644 --- a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.req.cc +++ b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.req.cc @@ -13,27 +13,18 @@ skeleton newGA // Problem --------------------------------------------------------------- - Problem::Problem ():_dimension(0),_limite_barrios(NULL),_tareasEsf(NULL),_tareasIndex(NULL),_empleados(NULL),_cantDias(0),_cantEmpleados(0) + Problem::Problem ():_dimension(0),_limite_barrios(NULL),_tareasEsf(NULL),_tareasIndex(NULL),_empleados(NULL),_cantFilas(0),_cantColumnas(0),_datosFilas(NULL),_datosColumnas(NULL), _cantGruposColumna(NULL), _cantGruposFila(NULL) { //cout << "uf6"; } ostream& operator<< (ostream& os, const Problem& pbm) { - os << endl << endl << "Number of Variables " << pbm._dimension + os << endl << endl << "Number of Columns " << pbm._cantColumnas; << endl; - //Imprimo el arreglo con los limites de barrios - os<<"Limites de barrios: "<<endl<<endl; - for (int i=0;i<pbm._dimension;i++){ - os<<i<<": "; - int indice=0; - while (pbm._limite_barrios[i][indice]!=-1){ - os<<pbm._limite_barrios[i][indice]<<","; - indice++; - } - os<<endl; - } + os << "Number of Rows " << pbm._cantFilas << endl; + os<<endl; return os; } @@ -42,17 +33,17 @@ skeleton newGA istream& operator>> (istream& is, Problem& pbm) { - + char buffer[MAX_BUFFER]; int i; //is.getline(buffer,MAX_BUFFER,'\n'); //sscanf(buffer,"%d",&pbm._dimension); - + //CARGAR DATO CANT Tareas ifstream inFile; inFile.open("datos_cantidad_tareas"); - if (!inFile) + if (!inFile) { cerr << "Unable to open file datafile.txt"; exit(1); // call system to stop @@ -70,11 +61,11 @@ skeleton newGA for (int j=0;j<10;j++) pbm._limite_barrios[i][j]=-1; } - + //CARGAR DATO CANT EMPLEADOS inFile; inFile.open("datos_cantidad_empleados"); - if (!inFile) + if (!inFile) { cerr << "Unable to open file datafile.txt"; exit(1); // call system to stop @@ -89,12 +80,12 @@ skeleton newGA { pbm._tareasEsf[ii]=0; } - - + + //CARGAR DATOS TAREAS ESFUERZO - + inFile.open("datos_tareas"); - if (!inFile) + if (!inFile) { cerr << "Unable to open file datafile.txt"; exit(1); // call system to stop @@ -104,15 +95,15 @@ skeleton newGA pbm._cantDias = stoi(line2); getline(inFile, line2); getline(inFile, line2); - - int n = line2.length(); - char char_array[n+1]; + + int n = line2.length(); + char char_array[n+1]; int len=1; int t=0; int z=0; - for(int i=0; i<=line2.length(); i++) + for(int i=0; i<=line2.length(); i++) { - if(line2[i] == ' '|| i==line2.length()) + if(line2[i] == ' '|| i==line2.length()) { string rr = line2.substr(z,len); pbm._tareasEsf[t] = stoi(rr); @@ -125,22 +116,22 @@ skeleton newGA else len=len+1; } inFile.close(); - - + + pbm._tareasIndex = new int[pbm._dimension]; - for (int i = 0 ; i != pbm._dimension ; i++) + for (int i = 0 ; i != pbm._dimension ; i++) { pbm._tareasIndex[i] = i; } sort(pbm._tareasIndex, pbm._tareasIndex + pbm._dimension,[&](const int& a, const int& b) { return (pbm._tareasEsf[a] < pbm._tareasEsf[b]); }); /* - for (int i = 0 ; i != pbm._dimension ; i++) + for (int i = 0 ; i != pbm._dimension ; i++) { cout << pbm._tareasIndex[i] << " "; } exit(0); */ - + //INICIALIZAR EMPLEADOS pbm._empleados=new Empleado [pbm._cantEmpleados]; for (int zz=0;zz<pbm._cantEmpleados;zz++) @@ -150,21 +141,21 @@ skeleton newGA pbm._empleados[zz]._habilidad=0; pbm._empleados[zz]._horas=0; } - + //CARGAS DATOS EMPLEADOS - + string line23; inFile.open("datos_empleados"); getline(inFile, line23); getline(inFile, line23); - int no = line23.length(); - char_array[n+1]; + int no = line23.length(); + char_array[n+1]; len=1; t=0; z=0; - for(int i=0; i<=line23.length(); i++) + for(int i=0; i<=line23.length(); i++) { - if(line23[i] == ' ' || i==line23.length()) + if(line23[i] == ' ' || i==line23.length()) { string rrr = line23.substr(z,len); pbm._empleados[t]._horas = stoi(rrr); @@ -177,17 +168,17 @@ skeleton newGA else len=len+1; } cout << pbm._empleados[0]._horas <<" " << pbm._empleados[1]._horas <<" " << pbm._empleados[2]._horas <<" " << pbm._empleados[3]._horas << " " << pbm._empleados[4]._horas << " " << endl; - + getline(inFile, line23); - no = line23.length(); - char_array[n+1]; + no = line23.length(); + char_array[n+1]; len=1; t=0; z=0; // cout << line23; - for(int i=0; i<=line23.length(); i++) + for(int i=0; i<=line23.length(); i++) { - if(line23[i] == ' ' || i==line23.length()) + if(line23[i] == ' ' || i==line23.length()) { string rrr = line23.substr(z,len); pbm._empleados[t]._habilidad = stof(rrr); @@ -200,16 +191,16 @@ skeleton newGA else len=len+1; } cout << pbm._empleados[0]._habilidad <<" " << pbm._empleados[1]._habilidad <<" " << pbm._empleados[2]._habilidad <<" " << pbm._empleados[3]._habilidad << " " << pbm._empleados[4]._habilidad << " " << endl; - + getline(inFile, line23); - no = line23.length(); - char_array[n+1]; + no = line23.length(); + char_array[n+1]; len=1; t=0; z=0; - for(int i=0; i<=line23.length(); i++) + for(int i=0; i<=line23.length(); i++) { - if(line23[i] == ' ' || i==line23.length()) + if(line23[i] == ' ' || i==line23.length()) { string rrr = line23.substr(z,len); pbm._empleados[t]._sueldo = stoi(rrr); @@ -224,7 +215,7 @@ skeleton newGA cout << pbm._empleados[0]._sueldo <<" " << pbm._empleados[1]._sueldo <<" " << pbm._empleados[2]._sueldo <<" " << pbm._empleados[3]._sueldo << " " << pbm._empleados[4]._sueldo << " " << endl; //assert(false); inFile.close(); - + /* //Cargo el archivo con los limites de los barrios FILE* stream = fopen("limite_barrios.csv", "r"); @@ -235,7 +226,7 @@ skeleton newGA char* tmp = strdup(line); const char * barrio = pbm.getfield(tmp,1); int barrio_int=atoi(barrio); - tmp = strdup(line); + tmp = strdup(line); const char * limite= pbm.getfield(tmp,2); int i=0; while (limite!=NULL) @@ -288,42 +279,40 @@ skeleton newGA return minimize; } - int Problem::dimension() const + int Problem::cantColumnas() const { - return _dimension; + return _cantColumnas; } - int Problem::cantEmpleados() const + int Problem::cantFilas() const { - return _cantEmpleados; + return _cantFilas; } - int Problem::cantDias() const + + int ** Problem::datosFilas() const { - return _cantDias; + return _datosFilas; } - - int * Problem::tareasEsf() const + + int ** Problem::datosColumnas() const { - return _tareasEsf; + return _datosColumnas; } - - int * Problem::tareasIndex() const + + int * Problem::cantGruposColumna() const { - return _tareasIndex; + return _cantGruposColumna(); } - - Empleado *Problem::empleados() const + + int * Problem::cantGruposFila() const { - return _empleados; + return _cantGruposFila(); } Problem::~Problem() { //Libero la memoria pedida para almacenar los limites de los barrios - for (int i=0;i<_dimension;i++) - delete [] _limite_barrios[i]; - delete [] _limite_barrios; - delete [] _empleados; - delete [] _tareasEsf; + delete[] _datosColumnas; + delete[] _datosFilas; } // Solution -------------------------------------------------------------- @@ -350,15 +339,15 @@ skeleton newGA ostream& operator<< (ostream& os, const Solution& sol) { - + //for (int i=0;i<sol.pbm().dimension();i++) // os << " " << sol._var[i]; stringstream ss; - + ofstream outfile("solution"); - + os << endl; - + for (int j = 0; j < sol.pbm().cantEmpleados(); j++) { bool found1 = false; @@ -371,22 +360,22 @@ skeleton newGA ss << "e" << j + 1; found1 = true; } - + ss << " t" << sol.pbm().tareasIndex()[i] + 1; } } - + if (found1) { ss << endl; } } - + outfile << ss.str(); outfile.close(); - + os << ss.str(); - + return os; } @@ -424,36 +413,63 @@ skeleton newGA } void Solution::initialize() - { - //cout << _pbm.dimension(); - for (int i=0;i<_pbm.dimension();i++) + { + for (int x = 0; x < _pbm.cantColumnas(); x++) + { + for (int y = 0; y < _pbm.cantFilas(); y++) + { + _var[x][y] = 0; + } + } + + for (int i=0;i<_pbm.cantColumnas();i++) { - _var[i]=rand_int(0,_pbm.cantEmpleados() - 1); - //_var[i]=rand_int(0,3); + int _firstPossible = 0; + int** _grupos = _pbm.datosColumnas(); + int* _cantGruposEnColumna = _pbm.cantGruposColumna(); + + for(int j=0; j < _cantGruposEnColumna[i]; j++) + { + int _lastPossible = _pbm.cantFilas() - _grupos[i][j]; + for (int k=j+1; k < _cantGruposEnColumna[i]; k++) + { + _lastPossible -= _grupos[i][k] + 1; + } + + int _startPosition = rand_int(_firstPossible, _lastPossible); + int _endPosition = _startPosition + _grupos[i][j] - 1; + for(int c = _startPosition; c < _endPosition; c++) + { + _var[j][c] = 1; + } + } } } double Solution::fitness () { + for (int i=0;i<_pbm.cantFilas();i++) { + + } double fitness = 0.0; double* horasTrabajados = new double[_pbm.cantEmpleados()]; for (int q=0; q < _pbm.cantEmpleados(); q++) { horasTrabajados[q] = 0; } - - for (int i=0;i< _pbm.dimension();i++) + + for (int i=0;i< _pbm.dimension();i++) { Empleado tipo = _pbm.empleados()[_var[i]]; double horasReq = ((double)_pbm.tareasEsf()[_pbm.tareasIndex()[i]]/(double)(0.5+tipo._habilidad)); horasTrabajados[_var[i]] += horasReq; } - - + + for (int j = 0; j < _pbm.cantEmpleados(); j++) - { + { double diasTrabajadosTemp = (double)horasTrabajados[j] / (double)_pbm.empleados()[j]._horas; fitness += (double)diasTrabajadosTemp * (double)_pbm.empleados()[j]._sueldo; if (isnan(fitness)) @@ -463,7 +479,7 @@ skeleton newGA fitness += 100000; } } - + return fitness; } @@ -612,19 +628,6 @@ skeleton newGA int i=0; Rarray<int> aux(sol1.pbm().dimension()); aux=sol2.array_var(); - - /* - if (current_time_spent() < 1000000) - { - int limit=rand_int((0,sol1.pbm().dimension()-1)); - - for (i=0;i<limit;i++) - sol2.var(i)=sol1.var(i); - for (i=0;i<limit;i++) - sol1.var(i)=aux[i]; - } - if (current_time_spent() >= 1000000) - {*/ int limit=rand_int((sol1.pbm().dimension()/2)+1,sol1.pbm().dimension()-1); int limit2=rand_int(0,limit-1); @@ -636,7 +639,6 @@ skeleton newGA sol2.var(i)=sol1.var(i); for (i=limit;i<sol1.pbm().dimension();i++) sol1.var(i)=aux[i]; - //} } void Crossover::execute(Rarray<Solution*>& sols) const @@ -685,12 +687,51 @@ skeleton newGA void Mutation::mutate(Solution& sol) const { + _grupos = _pbm.datosColumnas(); for (int i=0;i<sol.pbm().dimension();i++) { if (rand01()<=probability[1]) { - //La mutacion modifica un gen aleatoriamente con probabilidad uniforme en {0,3} - int temp = rand_int(0,sol.pbm().cantEmpleados() - 1); + int inic= rand_int(0,sol1.pbm().cantFilas()-2); //Arranca en 0, si tiene 15 filas va hasta la 14 + inf fin= rand_int(inic+1,sol1.pbm().cantFilas()-1); + if (fin != (sol1.pbm().cantFilas()-1)) { + if (fin-inic == 1) { + if (i != 0) { + int aux= _grupos[i][inic-1]; + _grupos[i][inic-1] = _grupos[i][inic]; + _grupos[i][inic]= aux; + } else { + int aux= _grupos[i][inic+1]; + _grupos[i][inic+1] = _grupos[i][inic]; + _grupos[i][inic]= aux; + } + } else { //fin-inic != 1, ES UN GRUPO + int iter=inic; //ITERADOR + if (i != 0) { //SI ESTA ARRIBA + int var =_grupos[i][iter-1]; + while (iter != fin+1) { + _grupos[i][iter-1] = _grupos[i][inic]; + iter++; + } + _grupos[i][iter] = var; + } else { //ESTOY EN LO MAS ARRIBA POSIBLE, LO HAGO PARA ABAJO + if (fin != sol1.pbm().cantFilas()-1) { //CHEQUEO QUE EFECTIVAMENTE ME PUEDO MOVER PARA ABAJO + int var =_grupos[i][iter+1]; + while (iter != fin+1) { + _grupos[i][iter+1] = _grupos[i][inic]; + iter++; + } + _grupos[i][iter] = var; + } + } + } + } + } + } + } + + + int temp = rand_int(0,sol.pbm().cantEmpleados() - 1); sol.var(i)= temp; //sol.var(i)=rand_int(0,3); } @@ -741,7 +782,7 @@ skeleton newGA {} bool StopCondition_1::EvaluateCondition(const Problem& pbm,const Solver& solver,const SetUpParams& setup) - { + { bool fin=(int) (solver.current_time_spent() - solver.time_best_found_trial()) > 20000000; //Condicion de parada. Si el fitness es 0 terminamos la ejecucion. //bool fin=(int)solver.best_cost_trial() == 0; @@ -757,7 +798,7 @@ skeleton newGA }*/ //return (fin); return fin; - + } StopCondition_1::~StopCondition_1() @@ -775,4 +816,3 @@ skeleton newGA } } #endif - diff --git a/malva/rep/GA/newGA.hh b/malva/rep/GA/newGA.hh index 4d2423e81a83d39af2f15086836bac4c5673eb07..552b2648923ec9383125099fb7ad87e4f924c4a9 100644 --- a/malva/rep/GA/newGA.hh +++ b/malva/rep/GA/newGA.hh @@ -62,26 +62,24 @@ skeleton newGA Direction direction () const; -//------------------------------------------------------------------------------------------------ - - int cantFilas() const; - int cantColumnas() const; + int cantDias() const; + int cantEmpleados() const; + int dimension() const; - int** datosColumnas() const; - int** datosFilas() const; - - int* cantGruposColumna() const; - int* cantGruposFila() const; - - const char* getfield(char* line, int num); //??? + Empleado * empleados() const; + int * tareasEsf() const; + int * tareasIndex() const; + int ** limite_barrios() const; + const char* getfield(char* line, int num); private: - int _cantFilas; - int _cantColumnas; - int** _datosColumnas; - int** _datosFilas; - int* _cantGruposColumna; - int* _cantGruposFila; + int _cantDias; + Empleado * _empleados; + int * _tareasEsf; + int * _tareasIndex; + int _cantEmpleados; + int _dimension; + int ** _limite_barrios; }; //Solution ---------------------------------------------------------------------------- @@ -115,7 +113,7 @@ skeleton newGA Rarray<int>& array_var(); private: - Rarray<Rarray<int>> _var; + Rarray<int> _var; const Problem& _pbm; }; diff --git a/malva/rep/GA/newGA.req.cc b/malva/rep/GA/newGA.req.cc index ed025e013084985062e11d25edfd5cc4ae98b1a8..cba5214c727ba7b14d51703d647f7aa50decdf29 100644 --- a/malva/rep/GA/newGA.req.cc +++ b/malva/rep/GA/newGA.req.cc @@ -20,11 +20,20 @@ skeleton newGA ostream& operator<< (ostream& os, const Problem& pbm) { - os << endl << endl << "Number of Columns " << pbm._cantColumnas; + os << endl << endl << "Number of Variables " << pbm._dimension << endl; - os << "Number of Rows " << pbm._cantFilas << endl; - + //Imprimo el arreglo con los limites de barrios + os<<"Limites de barrios: "<<endl<<endl; + for (int i=0;i<pbm._dimension;i++){ + os<<i<<": "; + int indice=0; + while (pbm._limite_barrios[i][indice]!=-1){ + os<<pbm._limite_barrios[i][indice]<<","; + indice++; + } + os<<endl; + } os<<endl; return os; } @@ -279,40 +288,42 @@ skeleton newGA return minimize; } - int Problem::cantColumnas() const + int Problem::dimension() const { - return _cantColumnas; + return _dimension; } - int Problem::cantFilas() const + int Problem::cantEmpleados() const { - return _cantFilas; + return _cantEmpleados; } - - int ** Problem::datosFilas() const + int Problem::cantDias() const { - return _datosFilas; + return _cantDias; } - int ** Problem::datosColumnas() const + int * Problem::tareasEsf() const { - return _datosColumnas; + return _tareasEsf; } - - int * Problem::cantGruposColumna() const + + int * Problem::tareasIndex() const { - return _cantGruposColumna(); + return _tareasIndex; } - - int * Problem::cantGruposFila() const + + Empleado *Problem::empleados() const { - return _cantGruposFila(); + return _empleados; } Problem::~Problem() { //Libero la memoria pedida para almacenar los limites de los barrios - delete[] _datosColumnas; - delete[] _datosFilas; + for (int i=0;i<_dimension;i++) + delete [] _limite_barrios[i]; + delete [] _limite_barrios; + delete [] _empleados; + delete [] _tareasEsf; } // Solution -------------------------------------------------------------- @@ -414,15 +425,8 @@ skeleton newGA void Solution::initialize() { - for (int x = 0; x < _pbm.cantColumnas(); x++) - { - for (int y = 0; y < _pbm.cantFilas(); y++) - { - _var[x][y] = 0; - } - } - - for (int i=0;i<_pbm.cantColumnas();i++) + //cout << _pbm.dimension(); + for (int i=0;i<_pbm.dimension();i++) { int _firstPossible = 0; int** _grupos = _pbm.datosColumnas();