Skip to content
Snippets Groups Projects
Commit 4466452d authored by Gonzalo Menendez Berch - ENS's avatar Gonzalo Menendez Berch - ENS
Browse files

TisAlive

parent 54ada6f8
No related branches found
No related tags found
No related merge requests found
# Malva Configuration. # Malva Configuration.
MALLBA_DIR=/ens/home01/l/lucas.bruzzone/AEPractico/malva MALLBA_DIR=/ens/home01/g/gonzalo.menendez/AEPractico/malva
# MPI library # MPI library
MPI_BIN=/lib64/mpich/bin MPI_BIN=/lib64/mpich/bin
......
No preview for this file type
No preview for this file type
No preview for this file type
1,5
2,5
3,5
4,5
5,5
6,5
7,5
8,0
9,4
10,4
/********************************************************************************************************* /*********************************************************************************************************
*** *** *** ***
*** new Genetic Algorithm Skeleton v1.5 *** *** new Genetic Algorithm Skeleton v1.5 ***
*** Developed by: Gabriel Jesús Luque Polo *** *** Developed by: Gabriel Jess Luque Polo ***
*** Last Update: 27-01-2003 *** *** Last Update: 27-01-2003 ***
*** *** *** ***
*** tabular size = 4 *** *** tabular size = 4 ***
...@@ -13,9 +13,9 @@ ...@@ -13,9 +13,9 @@
skeleton newGA skeleton newGA
{ {
// Si se definen más de 5 nuevos operadores por parte del usuario, se debe cambiar esta constante. // Si se definen ms de 5 nuevos operadores por parte del usuario, se debe cambiar esta constante.
#define MAX_OP_USER 5 #define MAX_OP_USER 5
// Si se algún operador tiene más de 5 parámetros se debe modificar esta variable // Si se algn operador tiene ms de 5 parmetros se debe modificar esta variable
#define MAX_PROB_PER_OP 5 #define MAX_PROB_PER_OP 5
provides class SetUpParams; provides class SetUpParams;
...@@ -61,19 +61,23 @@ skeleton newGA ...@@ -61,19 +61,23 @@ skeleton newGA
bool operator!= (const Problem& pbm) const; bool operator!= (const Problem& pbm) const;
Direction direction () const; Direction direction () const;
int dimension() const;
int cantEmpleados() const;
int cantDias() const; int cantDias() const;
Rarray<Empleado> empleados() const; int cantEmpleados() const;
Rarray<int> tareasEsf() const; int dimension() const;
Empleado * empleados() const;
int * tareasEsf() const;
int ** limite_barrios() const;
const char* getfield(char* line, int num);
private: private:
int _cantDias;
Empleado * _empleados;
int * _tareasEsf;
int _cantEmpleados; int _cantEmpleados;
int _dimension; int _dimension;
int _cantDias; int ** _limite_barrios;
Rarray<Empleado> _empleados;
Rarray<int> _tareasEsf;
}; };
//Solution ---------------------------------------------------------------------------- //Solution ----------------------------------------------------------------------------
......
...@@ -2,6 +2,9 @@ ...@@ -2,6 +2,9 @@
#define INC_REQ_newGA #define INC_REQ_newGA
#include "newGA.hh" #include "newGA.hh"
#include <math.h> #include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sstream> #include <sstream>
skeleton newGA skeleton newGA
...@@ -9,98 +12,259 @@ skeleton newGA ...@@ -9,98 +12,259 @@ skeleton newGA
// Problem --------------------------------------------------------------- // 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) ostream& operator<< (ostream& os, const Problem& pbm)
{ {
os << endl << endl << "Number of Variables " << pbm._dimension os << endl << endl << "Number of Variables " << pbm._dimension
<< endl; << 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; return os;
} }
istream& operator>> (istream& is, Problem& pbm) istream& operator>> (istream& is, Problem& pbm)
{ {
/*
char buffer[MAX_BUFFER]; char buffer[MAX_BUFFER];
int i; int i;
is.getline(buffer,MAX_BUFFER,'\n'); //is.getline(buffer,MAX_BUFFER,'\n');
sscanf(buffer,"%d",&pbm._dimension); //sscanf(buffer,"%d",&pbm._dimension);
*/
string line;
ifstream cantTareasFile ("datos_cantidad_tareas");
getline(cantTareasFile, line);
pbm._dimension = stoi(line);
cout << pbm._dimension << endl;
//string line; //CARGAR DATO CANT Tareas
ifstream cantEmpleadosFile ("datos_cantidad_empleados"); ifstream inFile;
getline(cantEmpleadosFile, line); inFile.open("datos_cantidad_tareas");
pbm._cantEmpleados = stoi(line); if (!inFile)
cout << pbm._cantEmpleados << endl; {
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"); //INICIALIZAR EMPLEADOS
getline(datosEmpleadosFile, line23); pbm._empleados=new Empleado [pbm._cantEmpleados];
getline(datosEmpleadosFile, line23); for (int zz=0;zz<pbm._cantEmpleados;zz++)
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++)
{ {
//+pbm._empleados[zz] = new Empleado;
pbm._empleados[zz]._sueldo = 0; pbm._empleados[zz]._sueldo = 0;
pbm._empleados[zz]._habilidad=0; pbm._empleados[zz]._habilidad=0;
pbm._empleados[zz]._horas=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; if(line23[i] == ' ' || i==line23.length())
cout << pbm._empleados[counter1]._horas << endl; {
counter1 += 1; 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); cout << pbm._empleados[0]._horas <<" " << pbm._empleados[1]._horas <<" " << pbm._empleados[2]._horas <<" " << pbm._empleados[3]._horas << " " << pbm._empleados[4]._horas << " " << endl;
float f1;
int counter2 = 0; getline(inFile, line23);
stringstream os2(line); no = line23.length();
while (os2 >> f1) 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; if(line23[i] == ' ' || i==line23.length())
counter2 += 1; {
} string rrr = line23.substr(z,len);
getline(datosEmpleadosFile, line); pbm._empleados[t]._habilidad = stof(rrr);
int i2; //cout << rrr << " ";
int counter3 = 0; t=t+1;
stringstream os3(line); z=i+1;
while (os3 >> i2) len=1;
{ //s.erase(i,1);
pbm._empleados[counter3]._sueldo = i2; }
counter3 += 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; getline(inFile, line23);
ifstream datosTareasFile ("datos_tareas"); no = line23.length();
getline(datosTareasFile, line); char_array[n+1];
pbm._cantDias = stoi(line); len=1;
getline(datosTareasFile, line); t=0;
getline(datosTareasFile, line); z=0;
int i3; for(int i=0; i<=line23.length(); i++)
int counter4 = 0;
stringstream os4(line);
while (os4 >> i3)
{ {
pbm._tareasEsf[counter4] = i3; if(line23[i] == ' ' || i==line23.length())
counter4 += 1; {
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; 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 bool Problem::operator== (const Problem& pbm) const
{ {
if (_dimension!=pbm.dimension()) return false; if (_dimension!=pbm.dimension()) return false;
...@@ -122,26 +286,36 @@ skeleton newGA ...@@ -122,26 +286,36 @@ skeleton newGA
{ {
return _dimension; return _dimension;
} }
int Problem::cantEmpleados() const int Problem::cantEmpleados() const
{ {
return _cantEmpleados; 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; 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::Solution (const Problem& pbm):_pbm(pbm),_var(pbm.dimension()) Solution::Solution (const Problem& pbm):_pbm(pbm),_var(pbm.dimension())
...@@ -166,8 +340,43 @@ skeleton newGA ...@@ -166,8 +340,43 @@ skeleton newGA
ostream& operator<< (ostream& os, const Solution& sol) 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; return os;
} }
...@@ -205,21 +414,38 @@ skeleton newGA ...@@ -205,21 +414,38 @@ skeleton newGA
} }
void Solution::initialize() void Solution::initialize()
{ {
//cout << _pbm.dimension();
for (int i=0;i<_pbm.dimension();i++) 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 Solution::fitness ()
{ {
double fitness = 0.0; double fitness = 0.0;
Rarray<Empleado> grupo= _pbm.empleados(); int* diasTrabajados = new int[_pbm.cantEmpleados()];
Rarray<int> soFun = _pbm.tareasEsf(); for (int i=0;i< _pbm.dimension();i++)
for (int i=0;i< _var.size();i++) { {
Empleado tipo = grupo[i]; Empleado tipo = _pbm.empleados()[_var[i]];
fitness += tipo._sueldo*soFun[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; return fitness;
} }
char *Solution::to_String() const char *Solution::to_String() const
...@@ -362,6 +588,7 @@ skeleton newGA ...@@ -362,6 +588,7 @@ skeleton newGA
void Crossover::cross(Solution& sol1,Solution& sol2) const // dadas dos soluciones de la poblacion, las cruza 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; int i=0;
Rarray<int> aux(sol1.pbm().dimension()); Rarray<int> aux(sol1.pbm().dimension());
aux=sol2.array_var(); aux=sol2.array_var();
...@@ -429,9 +656,10 @@ skeleton newGA ...@@ -429,9 +656,10 @@ skeleton newGA
{ {
if (rand01()<=probability[1]) if (rand01()<=probability[1])
{ {
sol.var(i) = rand() % sol.pbm().cantEmpleados() + 1; //La mutacion modifica un gen aleatoriamente con probabilidad uniforme en {0,3}
//if (sol.var(i)==1) sol.var(i)=0; int temp = rand_int(0,sol.pbm().cantEmpleados() - 1);
//else sol.var(i)=1; sol.var(i)= temp;
//sol.var(i)=rand_int(0,3);
} }
} }
} }
...@@ -480,9 +708,23 @@ skeleton newGA ...@@ -480,9 +708,23 @@ skeleton newGA
{} {}
bool StopCondition_1::EvaluateCondition(const Problem& pbm,const Solver& solver,const SetUpParams& setup) 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() StopCondition_1::~StopCondition_1()
{} {}
......
No preview for this file type
...@@ -3,14 +3,14 @@ ...@@ -3,14 +3,14 @@
STATISTICS OF TRIALS STATISTICS OF TRIALS
------------------------------------------------------------------ ------------------------------------------------------------------
1 10 1 160 1 260 260 1 174340 665865 60 0 0 13926
2 10 1 360 3 610 610 2 173017 567169 60 0 0 11581
3 10 2 260 2 451 451 3 166149 662569 60 0 0 14247
4 10 1 160 1 250 250 4 256269 569548 6960 69 7283 10187
5 10 2 360 3 645 645 5 165324 576086 60 0 0 10918
6 10 1 360 3 679 679 6 256269 667850 2860 28 3686 13166
7 10 2 360 3 672 672 7 173365 573089 60 0 0 17748
8 10 2 360 3 636 636 8 165161 664025 7260 72 8987 11607
9 10 1 360 3 693 693 9 179840 664189 60 0 0 9365
10 10 0 460 4 824 824 10 170629 569643 60 0 0 11567
------------------------------------------------------------------ ------------------------------------------------------------------
1 1 2 5 6 7
2 3 8
5 4
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment