diff --git a/malva/environment b/malva/environment index fecad06feb28400820808414edec463c0d0aaf73..4976de6761bf2491e7a5976290961d6410764b4b 100644 --- a/malva/environment +++ b/malva/environment @@ -1,5 +1,5 @@ # Malva Configuration. -MALLBA_DIR=/ens/home01/l/lucas.bruzzone/AEPractico/malva +MALLBA_DIR=/ens/home01/g/gonzalo.menendez/AEPractico/malva # MPI library MPI_BIN=/lib64/mpich/bin diff --git a/malva/lib/libmallba.a b/malva/lib/libmallba.a index caca61bfac3b2578381e72a4e29daf4a541a0b2c..db7f3cab7df60324f9676e75019a0ef727333a3e 100644 Binary files a/malva/lib/libmallba.a and b/malva/lib/libmallba.a differ diff --git a/malva/rep/GA/MainLan b/malva/rep/GA/MainLan index 3c264da8176cea8ef985745f1886486fdf6ec58e..d96e058d4a226ffee113e32b7c5ed75c28789619 100755 Binary files a/malva/rep/GA/MainLan and b/malva/rep/GA/MainLan differ diff --git a/malva/rep/GA/MainSeq b/malva/rep/GA/MainSeq index fb205232104f6ea6d779d642900ef7cf2bfe4b27..40a7b5e51cc3e975a8ab68a6be5291901a0ba57b 100755 Binary files a/malva/rep/GA/MainSeq and b/malva/rep/GA/MainSeq differ diff --git a/malva/rep/GA/asignacion_colores.csv b/malva/rep/GA/asignacion_colores.csv new file mode 100644 index 0000000000000000000000000000000000000000..83840363d469cec56434d0f6c25e841eb42688df --- /dev/null +++ b/malva/rep/GA/asignacion_colores.csv @@ -0,0 +1,10 @@ +1,5 +2,5 +3,5 +4,5 +5,5 +6,5 +7,5 +8,0 +9,4 +10,4 diff --git a/malva/rep/GA/newGA.hh b/malva/rep/GA/newGA.hh index def4981d220d0d5153dba4e95233cb43ba7588fe..9397c031ff7c3a4c28d7009c953cc6f422265e4f 100644 --- a/malva/rep/GA/newGA.hh +++ b/malva/rep/GA/newGA.hh @@ -1,7 +1,7 @@ /********************************************************************************************************* *** *** *** new Genetic Algorithm Skeleton v1.5 *** -*** Developed by: Gabriel Jesús Luque Polo *** +*** Developed by: Gabriel Jesús Luque Polo *** *** Last Update: 27-01-2003 *** *** *** *** tabular size = 4 *** @@ -13,9 +13,9 @@ skeleton newGA { -// Si se definen más de 5 nuevos operadores por parte del usuario, se debe cambiar esta constante. +// Si se definen más de 5 nuevos operadores por parte del usuario, se debe cambiar esta constante. #define MAX_OP_USER 5 -// Si se algún operador tiene más de 5 parámetros se debe modificar esta variable +// Si se algún operador tiene más de 5 parámetros se debe modificar esta variable #define MAX_PROB_PER_OP 5 provides class SetUpParams; @@ -61,19 +61,23 @@ skeleton newGA bool operator!= (const Problem& pbm) const; Direction direction () const; - - int dimension() const; - int cantEmpleados() const; + int cantDias() const; - Rarray<Empleado> empleados() const; - Rarray<int> tareasEsf() const; + int cantEmpleados() const; + int dimension() const; + + Empleado * empleados() const; + int * tareasEsf() const; + int ** limite_barrios() const; + const char* getfield(char* line, int num); private: + int _cantDias; + Empleado * _empleados; + int * _tareasEsf; int _cantEmpleados; int _dimension; - int _cantDias; - Rarray<Empleado> _empleados; - Rarray<int> _tareasEsf; + int ** _limite_barrios; }; //Solution ---------------------------------------------------------------------------- diff --git a/malva/rep/GA/newGA.req.cc b/malva/rep/GA/newGA.req.cc index c65fa03efb15e5504eac45513eca88d244eb87fa..87a14e59f90937391acd6fe323e62844ee10520b 100644 --- a/malva/rep/GA/newGA.req.cc +++ b/malva/rep/GA/newGA.req.cc @@ -2,6 +2,9 @@ #define INC_REQ_newGA #include "newGA.hh" #include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> #include <sstream> skeleton newGA @@ -9,98 +12,259 @@ skeleton newGA // Problem --------------------------------------------------------------- - Problem::Problem ():_dimension(0) - {} + Problem::Problem ():_dimension(0),_limite_barrios(NULL),_tareasEsf(NULL),_empleados(NULL),_cantDias(0),_cantEmpleados(0) + { + //cout << "uf6"; + } ostream& operator<< (ostream& os, const Problem& pbm) { os << endl << endl << "Number of Variables " << pbm._dimension << 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; } + + istream& operator>> (istream& is, Problem& pbm) { - /* + char buffer[MAX_BUFFER]; int i; - is.getline(buffer,MAX_BUFFER,'\n'); - sscanf(buffer,"%d",&pbm._dimension); - */ - - string line; - ifstream cantTareasFile ("datos_cantidad_tareas"); - getline(cantTareasFile, line); - pbm._dimension = stoi(line); - cout << pbm._dimension << endl; + //is.getline(buffer,MAX_BUFFER,'\n'); + //sscanf(buffer,"%d",&pbm._dimension); - //string line; - ifstream cantEmpleadosFile ("datos_cantidad_empleados"); - getline(cantEmpleadosFile, line); - pbm._cantEmpleados = stoi(line); - cout << pbm._cantEmpleados << endl; + //CARGAR DATO CANT Tareas + ifstream inFile; + inFile.open("datos_cantidad_tareas"); + if (!inFile) + { + cerr << "Unable to open file datafile.txt"; + exit(1); // call system to stop + } + string x; + getline(inFile,x); + pbm._dimension = stoi(x); + inFile.close(); + + //Pido memoria para almacenar los limites de los barrios + pbm._limite_barrios=new int *[pbm._dimension]; + for (int i=0;i<pbm._dimension;i++) + { + pbm._limite_barrios[i]=new int [10]; + 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) + { + cerr << "Unable to open file datafile.txt"; + exit(1); // call system to stop + } + //string x; + getline(inFile,x); + pbm._cantEmpleados = stoi(x); + inFile.close(); + //INICIALIZAR DATOS TAREAS ESFUERZO + pbm._tareasEsf=new int [pbm._dimension]; + for (int ii=0;ii<pbm._dimension;ii++) + { + pbm._tareasEsf[ii]=0; + } + //CARGAR DATOS TAREAS ESFUERZO + + inFile.open("datos_tareas"); + if (!inFile) + { + cerr << "Unable to open file datafile.txt"; + exit(1); // call system to stop + } + string line2; + getline(inFile,line2); + pbm._cantDias = stoi(line2); + getline(inFile, line2); + getline(inFile, line2); + + 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++) + { + if(line2[i] == ' '|| i==line2.length()) + { + string rr = line2.substr(z,len); + pbm._tareasEsf[t] = stoi(rr); + //cout << rr << " "; + t=t+1; + z=i+1; + len=1; + //s.erase(i,1); + } + else len=len+1; + } + inFile.close(); + //cout << pbm._tareasEsf[0] << " " << pbm._tareasEsf[1] << " " << pbm._tareasEsf[2] << " " << pbm._tareasEsf[3] << " " << pbm._tareasEsf[4] << endl; + //cout << s[1] << s[2] << endl; + + //pbm._tareasEsf[counter4] = i3; + //std::string::iterator end_pos = std::remove(str.begin(), str.end(), ' '); + //str.erase(end_pos, str.end()); + +/* + strcpy(char_array, s.c_str()); + for (int i=0; i<n; i++) + cout << char_array[i]; - string line23; - ifstream datosEmpleadosFile ("datos_empleados"); - getline(datosEmpleadosFile, line23); - getline(datosEmpleadosFile, line23); - int i1; - int counter1 = 0; - stringstream os(line23); - /*Rarray<Empleado> tempEmpleados*/ pbm._empleados = Rarray<Empleado>(pbm._cantEmpleados); - //pbm._empleados = tempEmpleados; - for (int zz=0;zz<pbm._cantEmpleados;zz++) + */ + //INICIALIZAR EMPLEADOS + pbm._empleados=new Empleado [pbm._cantEmpleados]; + for (int zz=0;zz<pbm._cantEmpleados;zz++) { - //+pbm._empleados[zz] = new Empleado; + pbm._empleados[zz]._sueldo = 0; pbm._empleados[zz]._habilidad=0; pbm._empleados[zz]._horas=0; } - while (os >> i1) + + //CARGAS DATOS EMPLEADOS + + string line23; + inFile.open("datos_empleados"); + getline(inFile, line23); + getline(inFile, line23); + int no = line23.length(); + char_array[n+1]; + len=1; + t=0; + z=0; + for(int i=0; i<=line23.length(); i++) { - pbm._empleados[counter1]._horas = i1; - cout << pbm._empleados[counter1]._horas << endl; - counter1 += 1; + if(line23[i] == ' ' || i==line23.length()) + { + string rrr = line23.substr(z,len); + pbm._empleados[t]._horas = stoi(rrr); + //cout << rr << " "; + t=t+1; + z=i+1; + len=1; + //s.erase(i,1); + } + else len=len+1; } - getline(datosEmpleadosFile, line); - float f1; - int counter2 = 0; - stringstream os2(line); - while (os2 >> f1) + 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]; + len=1; + t=0; + z=0; + // cout << line23; + for(int i=0; i<=line23.length(); i++) { - pbm._empleados[counter2]._habilidad = f1; - counter2 += 1; - } - getline(datosEmpleadosFile, line); - int i2; - int counter3 = 0; - stringstream os3(line); - while (os3 >> i2) - { - pbm._empleados[counter3]._sueldo = i2; - counter3 += 1; + if(line23[i] == ' ' || i==line23.length()) + { + string rrr = line23.substr(z,len); + pbm._empleados[t]._habilidad = stof(rrr); + //cout << rrr << " "; + t=t+1; + z=i+1; + len=1; + //s.erase(i,1); + } + 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; - //string line; - ifstream datosTareasFile ("datos_tareas"); - getline(datosTareasFile, line); - pbm._cantDias = stoi(line); - getline(datosTareasFile, line); - getline(datosTareasFile, line); - int i3; - int counter4 = 0; - stringstream os4(line); - while (os4 >> i3) + getline(inFile, line23); + no = line23.length(); + char_array[n+1]; + len=1; + t=0; + z=0; + for(int i=0; i<=line23.length(); i++) { - pbm._tareasEsf[counter4] = i3; - counter4 += 1; + if(line23[i] == ' ' || i==line23.length()) + { + string rrr = line23.substr(z,len); + pbm._empleados[t]._sueldo = stoi(rrr); + //cout << rr << " "; + t=t+1; + z=i+1; + len=1; + //s.erase(i,1); + } + else len=len+1; } - - + 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"); + + char line[1024]; + while (fgets(line, 1024, stream)) + { + char* tmp = strdup(line); + const char * barrio = pbm.getfield(tmp,1); + int barrio_int=atoi(barrio); + tmp = strdup(line); + const char * limite= pbm.getfield(tmp,2); + int i=0; + while (limite!=NULL) + { + pbm._limite_barrios[barrio_int-1][i]=atoi(limite)-1; + i++; + tmp = strdup(line); + limite=pbm.getfield(tmp,i+2); + } + free(tmp); + } + */ + cout<<pbm; return is; } + //Funcion para leer el archivo CSV + const char* Problem::getfield(char* line, int num) + { + const char* tok; + for (tok = strtok(line, ","); + tok && *tok; + tok = strtok(NULL, ",\n")) + { + if (!--num) + return tok; + } + return NULL; + } + + int ** Problem::limite_barrios() const{ + return _limite_barrios; + } + + bool Problem::operator== (const Problem& pbm) const { if (_dimension!=pbm.dimension()) return false; @@ -122,26 +286,36 @@ skeleton newGA { return _dimension; } - int Problem::cantEmpleados() const + int Problem::cantEmpleados() const { return _cantEmpleados; } - Problem::~Problem() + int Problem::cantDias() const { + return _cantDias; } - int Problem::cantDias() const + + int * Problem::tareasEsf() const { - return _cantDias; + return _tareasEsf; } - Rarray<Empleado> Problem::empleados() const + + Empleado *Problem::empleados() const { return _empleados; } - - Rarray<int> Problem::tareasEsf() const + + Problem::~Problem() { - return _tareasEsf; + //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; + cout << "ufdasd"; + delete [] _tareasEsf; } + // Solution -------------------------------------------------------------- Solution::Solution (const Problem& pbm):_pbm(pbm),_var(pbm.dimension()) @@ -166,8 +340,43 @@ skeleton newGA ostream& operator<< (ostream& os, const Solution& sol) { - for (int i=0;i<sol.pbm().dimension();i++) - os << " " << sol._var[i]; + + //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; + for (int i = 0; i < sol.pbm().dimension(); i++) + { + if (sol._var[i] == j) + { + if (!found1) + { + ss << j + 1; + found1 = true; + } + + ss << " " << i + 1; + } + } + + if (found1) + { + ss << endl; + } + } + + outfile << ss.str(); + outfile.close(); + + os << ss.str(); + return os; } @@ -205,21 +414,38 @@ skeleton newGA } void Solution::initialize() - { + { + //cout << _pbm.dimension(); for (int i=0;i<_pbm.dimension();i++) - _var[i]=rand() % _pbm.cantEmpleados() + 1;//numero random entre 1 y CANT EMPLEADOS + { + _var[i]=rand_int(0,_pbm.cantEmpleados() - 1); + //_var[i]=rand_int(0,3); + } } double Solution::fitness () { + double fitness = 0.0; - Rarray<Empleado> grupo= _pbm.empleados(); - Rarray<int> soFun = _pbm.tareasEsf(); - for (int i=0;i< _var.size();i++) { - Empleado tipo = grupo[i]; - fitness += tipo._sueldo*soFun[i]; + int* diasTrabajados = new int[_pbm.cantEmpleados()]; + for (int i=0;i< _pbm.dimension();i++) + { + Empleado tipo = _pbm.empleados()[_var[i]]; + int diasReq = (_pbm.tareasEsf()[i]/(0.5+tipo._habilidad)) / tipo._horas; + diasTrabajados[_var[i]] += diasReq; + fitness += tipo._sueldo * diasReq; + } + + for (int j = 0; j < _pbm.cantEmpleados(); j++) + { + if (diasTrabajados[j] > _pbm.cantDias()) + { + fitness += 100000; + } } + return fitness; + } char *Solution::to_String() const @@ -362,6 +588,7 @@ skeleton newGA void Crossover::cross(Solution& sol1,Solution& sol2) const // dadas dos soluciones de la poblacion, las cruza { + //Usamos cruzamiento de dos puntos (2PX) int i=0; Rarray<int> aux(sol1.pbm().dimension()); aux=sol2.array_var(); @@ -429,9 +656,10 @@ skeleton newGA { if (rand01()<=probability[1]) { - sol.var(i) = rand() % sol.pbm().cantEmpleados() + 1; - //if (sol.var(i)==1) sol.var(i)=0; - //else sol.var(i)=1; + //La mutacion modifica un gen aleatoriamente con probabilidad uniforme en {0,3} + int temp = rand_int(0,sol.pbm().cantEmpleados() - 1); + sol.var(i)= temp; + //sol.var(i)=rand_int(0,3); } } } @@ -480,9 +708,23 @@ skeleton newGA {} bool StopCondition_1::EvaluateCondition(const Problem& pbm,const Solver& solver,const SetUpParams& setup) - { - return ((int)solver.best_cost_trial() == pbm.dimension()); - } + { + //Condicion de parada. Si el fitness es 0 terminamos la ejecucion. + bool fin=(int)solver.best_cost_trial() == 0; + //cout << pbm.cantEmpleados(); + /*if (fin){ + //Escribo el resultado en el archivo de salida + FILE * pFile; + pFile = fopen ("asignacion_colores.csv","w"); + for (int i=0;i<pbm.dimension();i++){ + fprintf (pFile, "%d,%d\n",i+1,solver.best_solution_trial().var(i)); + } + fclose (pFile); + }*/ + //return (fin); + return false; + + } StopCondition_1::~StopCondition_1() {} diff --git a/malva/rep/GA/newGA.req.o b/malva/rep/GA/newGA.req.o index 4f82e569fc1e56f70197a1983c5ad02662110b10..283670cb85547df1d706371b120197be0558692c 100644 Binary files a/malva/rep/GA/newGA.req.o and b/malva/rep/GA/newGA.req.o differ diff --git a/malva/rep/GA/res/sol.txt b/malva/rep/GA/res/sol.txt index 1687ecab0c103fa5a7cf6e29a4f5e75aec744d66..2c2640332dc468c0d8e0322030def6b45c2beabf 100644 --- a/malva/rep/GA/res/sol.txt +++ b/malva/rep/GA/res/sol.txt @@ -3,14 +3,14 @@ STATISTICS OF TRIALS ------------------------------------------------------------------ -1 10 1 160 1 260 260 -2 10 1 360 3 610 610 -3 10 2 260 2 451 451 -4 10 1 160 1 250 250 -5 10 2 360 3 645 645 -6 10 1 360 3 679 679 -7 10 2 360 3 672 672 -8 10 2 360 3 636 636 -9 10 1 360 3 693 693 -10 10 0 460 4 824 824 +1 174340 665865 60 0 0 13926 +2 173017 567169 60 0 0 11581 +3 166149 662569 60 0 0 14247 +4 256269 569548 6960 69 7283 10187 +5 165324 576086 60 0 0 10918 +6 256269 667850 2860 28 3686 13166 +7 173365 573089 60 0 0 17748 +8 165161 664025 7260 72 8987 11607 +9 179840 664189 60 0 0 9365 +10 170629 569643 60 0 0 11567 ------------------------------------------------------------------ diff --git a/malva/rep/GA/solution b/malva/rep/GA/solution new file mode 100644 index 0000000000000000000000000000000000000000..4f80418ee7845f5482f7cbc62b73e7f59eb49222 --- /dev/null +++ b/malva/rep/GA/solution @@ -0,0 +1,3 @@ +1 1 2 5 6 7 +2 3 8 +5 4