diff --git a/malva/rep/GA/MainLan b/malva/rep/GA/MainLan index d96e058d4a226ffee113e32b7c5ed75c28789619..4412f1cad77b8b6332c1c698973a12e75d51ddba 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 40a7b5e51cc3e975a8ab68a6be5291901a0ba57b..da4383cc2eb7ad38bb626ee134b3475b21c1aae0 100755 Binary files a/malva/rep/GA/MainSeq and b/malva/rep/GA/MainSeq differ diff --git a/malva/rep/GA/datos_empleados b/malva/rep/GA/datos_empleados index 31d0f2b3083101b6300fe88f3df853f0c7bf5d80..f7cc5e5ffd3d2a61ae5180d3f7da918b35f73197 100644 --- a/malva/rep/GA/datos_empleados +++ b/malva/rep/GA/datos_empleados @@ -1,4 +1,4 @@ e1 e2 e3 e4 e5 -10 7 1 6 10 -0.47 0.49 0.67 0.08 0.23 -1827 1288 207 839 1563 \ No newline at end of file +4 5 5 3 3 +0.05 0.20 0.30 0.95 0.50 +120 200 210 230 180 diff --git a/malva/rep/GA/datos_tareas b/malva/rep/GA/datos_tareas index 73250e0ac45ee5ff4c5fba725c03bd1fccf7ef0e..589272f5c15492f95741ff1b7c8bd9669e9d4993 100644 --- a/malva/rep/GA/datos_tareas +++ b/malva/rep/GA/datos_tareas @@ -1,3 +1,3 @@ -49 +20 t1 t2 t3 t4 t5 t6 t7 t8 -59 29 183 88 200 176 18 150 \ No newline at end of file +16 28 11 51 2 23 43 15 diff --git a/malva/rep/GA/ejercicio2.py b/malva/rep/GA/ejercicio2.py new file mode 100644 index 0000000000000000000000000000000000000000..6705a027263a0079907ffb4f8b9905db20c4001a --- /dev/null +++ b/malva/rep/GA/ejercicio2.py @@ -0,0 +1,131 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import sys +import random +import math +import os + +try: + #Sanity check + if (len(sys.argv)<4): + print "Ejecutar con la siguiente linea: ./ejercicio2 <ruta_tareas> <ruta_empleados> <ruta_solucion>" + sys.exit(1) + + ruta_tareas=sys.argv[1] + ruta_empleados=sys.argv[2] + ruta_solucion=sys.argv[3] + + os.system("make clean") + os.system("make all") + os.system("make SEQ") + + #os.system("python verificador.py " + ruta_tareas + " " + ruta_empleados + " " + ruta_solucion) + + """ + habilidad_empleados=[] + sueldo_diario_empleado=[] + dedicacion_diaria_diponible_empleado=[] + esfuerzo_requerido_tarea=[] + nombres_tareas=[] + nombre_empleados=[] + costo_proyecto=0 + tiempo_solucion_proyecto=0 + + # se sabe que son 3 lineas + archivo_tareas=open(ruta_tareas) + lineasTareas=archivo_tareas.readlines() + deadLine=lineasTareas[0] + nombres_tareas=lineasTareas[1].strip().split(" ") + esfuerzo_requerido_tarea=lineasTareas[2].strip().split(" ") + + # se sabe que son 4 lineas + archivo_empleados=open(ruta_empleados) + lineasEmpleados=archivo_empleados.readlines() + nombre_empleados=lineasEmpleados[0].strip().split(" ") + dedicacion_diaria_diponible_empleado=lineasEmpleados[1].strip().split(" ") + habilidad_empleados=lineasEmpleados[2].strip().split(" ") + sueldo_diario_empleado=lineasEmpleados[3].strip().split(" ") + + # se convierten a entero + dedicacion_diaria_diponible_empleado = map(int, dedicacion_diaria_diponible_empleado) + sueldo_diario_empleado = map(int, sueldo_diario_empleado) + deadLine=int(deadLine) + esfuerzo_requerido_tarea = map(int, esfuerzo_requerido_tarea) + + # se convierte a float + habilidad_empleados = map(float, habilidad_empleados) + + # se levanta la solucion del archivo + matriz_solucion=[] + + archivo_solucion=open(ruta_solucion) + + solucion_empleados_tareas=[] + solucion_empleados=[] + solucion_tareas=[] + + for line in archivo_solucion.readlines(): + empleados_tareas=line.strip().split(" ") + if( not (empleados_tareas[0] in nombre_empleados)): + print "No existe en la instancia el empleado: " + empleados_tareas[0] + sys.exit(1) + solucion_empleados.append(empleados_tareas[0]) + for i in range(1,len(empleados_tareas)): + if( not (empleados_tareas[i] in nombres_tareas)): + print "No existe en la instancia la terea: " + empleados_tareas[i] + sys.exit(1) + solucion_tareas.append(empleados_tareas[i]) + + + for i in range(0,len(solucion_empleados)): + for j in range(i+1,len(solucion_empleados)): + if(solucion_empleados[i]==solucion_empleados[j]): + print "Empleado repetido: " + solucion_empleados[i] + sys.exit(1) + + for i in range(0,len(solucion_tareas)): + for j in range(i+1,len(solucion_tareas)): + if(solucion_tareas[i]==solucion_tareas[j]): + print "Tarea asignada más de una vez: " + solucion_tareas[i] + sys.exit(1) + + + # todas las tareas asignadas + if(len(solucion_tareas)!=len(nombres_tareas)): + print "La cantidad de tareas asignadas es distinta a la cantidad de tareas de la instancia." + sys.exit(1) + + for i in range(0,len(nombres_tareas)): + if( not (nombres_tareas[i] in solucion_tareas)): + print "Tarea no asignada " + nombres_tareas[i] + sys.exit(1) + + costo_total=0 + archivo_solucion=open(ruta_solucion) + maximo_tiempo=0 + tiempo_en_dias=0 + # se hacen los calculos con la informacion en los archivos + for line in archivo_solucion.readlines(): + empleados_tareas=line.strip().split(" ") + indice_empleado = nombre_empleados.index(empleados_tareas[0]) + tiempo_en_horas=0 + for i in range(1,len(empleados_tareas)): + indice_tarea = nombres_tareas.index(empleados_tareas[i]) + tiempo_en_horas=tiempo_en_horas+(esfuerzo_requerido_tarea[indice_tarea] / (0.5 + habilidad_empleados[indice_empleado])) + tiempo_en_dias= int(math.ceil(tiempo_en_horas/dedicacion_diaria_diponible_empleado[indice_empleado])) + costo_total=costo_total+sueldo_diario_empleado[indice_empleado]*tiempo_en_dias + if(tiempo_en_dias>maximo_tiempo): + maximo_tiempo=tiempo_en_dias + + if(maximo_tiempo<=deadLine): + # todo OK! + print costo_total,maximo_tiempo + else: + print "No cumple con el tiempo máximo de finalización ({0} > {1}).".format(maximo_tiempo, deadLine) + sys.exit(1) + + """ + +except IOError as error: + print error diff --git a/malva/rep/GA/newGA.hh b/malva/rep/GA/newGA.hh index 9397c031ff7c3a4c28d7009c953cc6f422265e4f..552b2648923ec9383125099fb7ad87e4f924c4a9 100644 --- a/malva/rep/GA/newGA.hh +++ b/malva/rep/GA/newGA.hh @@ -68,6 +68,7 @@ skeleton newGA Empleado * empleados() const; int * tareasEsf() const; + int * tareasIndex() const; int ** limite_barrios() const; const char* getfield(char* line, int num); @@ -75,6 +76,7 @@ skeleton newGA int _cantDias; Empleado * _empleados; int * _tareasEsf; + int * _tareasIndex; int _cantEmpleados; int _dimension; int ** _limite_barrios; diff --git a/malva/rep/GA/newGA.req.cc b/malva/rep/GA/newGA.req.cc index 87a14e59f90937391acd6fe323e62844ee10520b..653d43b819666278652d4834d7116555127fdffd 100644 --- a/malva/rep/GA/newGA.req.cc +++ b/malva/rep/GA/newGA.req.cc @@ -6,13 +6,14 @@ #include <stdlib.h> #include <string.h> #include <sstream> +#include <algorithm> skeleton newGA { // Problem --------------------------------------------------------------- - Problem::Problem ():_dimension(0),_limite_barrios(NULL),_tareasEsf(NULL),_empleados(NULL),_cantDias(0),_cantEmpleados(0) + Problem::Problem ():_dimension(0),_limite_barrios(NULL),_tareasEsf(NULL),_tareasIndex(NULL),_empleados(NULL),_cantDias(0),_cantEmpleados(0) { //cout << "uf6"; } @@ -88,6 +89,8 @@ skeleton newGA { pbm._tareasEsf[ii]=0; } + + //CARGAR DATOS TAREAS ESFUERZO inFile.open("datos_tareas"); @@ -122,19 +125,22 @@ skeleton newGA 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]; - */ + + pbm._tareasIndex = new int[pbm._dimension]; + 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++) + { + cout << pbm._tareasIndex[i] << " "; + } + exit(0); + */ + //INICIALIZAR EMPLEADOS pbm._empleados=new Empleado [pbm._cantEmpleados]; for (int zz=0;zz<pbm._cantEmpleados;zz++) @@ -300,6 +306,11 @@ skeleton newGA return _tareasEsf; } + int * Problem::tareasIndex() const + { + return _tareasIndex; + } + Empleado *Problem::empleados() const { return _empleados; @@ -312,7 +323,6 @@ skeleton newGA delete [] _limite_barrios[i]; delete [] _limite_barrios; delete [] _empleados; - cout << "ufdasd"; delete [] _tareasEsf; } @@ -358,11 +368,11 @@ skeleton newGA { if (!found1) { - ss << j + 1; + ss << "e" << j + 1; found1 = true; } - ss << " " << i + 1; + ss << " t" << sol.pbm().tareasIndex()[i] + 1; } } @@ -427,18 +437,28 @@ skeleton newGA { double fitness = 0.0; - int* diasTrabajados = new int[_pbm.cantEmpleados()]; + 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++) { 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; + 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++) - { - if (diasTrabajados[j] > _pbm.cantDias()) + { + double diasTrabajadosTemp = (double)horasTrabajados[j] / (double)_pbm.empleados()[j]._horas; + fitness += (double)diasTrabajadosTemp * (double)_pbm.empleados()[j]._sueldo; + if (isnan(fitness)) + exit(0); + if (diasTrabajadosTemp > _pbm.cantDias()) { fitness += 100000; } @@ -593,17 +613,30 @@ skeleton newGA Rarray<int> aux(sol1.pbm().dimension()); aux=sol2.array_var(); - int limit=rand_int((sol1.pbm().dimension()/2)+1,sol1.pbm().dimension()-1); - int limit2=rand_int(0,limit-1); + /* + if (current_time_spent() < 1000000) + { + int limit=rand_int((0,sol1.pbm().dimension()-1)); - for (i=0;i<limit2;i++) - sol2.var(i)=sol1.var(i); - for (i=0;i<limit2;i++) - sol1.var(i)=aux[i]; - for (i=limit;i<sol1.pbm().dimension();i++) - sol2.var(i)=sol1.var(i); - for (i=limit;i<sol1.pbm().dimension();i++) - sol1.var(i)=aux[i]; + 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); + + for (i=0;i<limit2;i++) + sol2.var(i)=sol1.var(i); + for (i=0;i<limit2;i++) + sol1.var(i)=aux[i]; + for (i=limit;i<sol1.pbm().dimension();i++) + 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 @@ -708,9 +741,10 @@ 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; + //bool fin=(int)solver.best_cost_trial() == 0; //cout << pbm.cantEmpleados(); /*if (fin){ //Escribo el resultado en el archivo de salida @@ -722,7 +756,7 @@ skeleton newGA fclose (pFile); }*/ //return (fin); - return false; + return fin; } diff --git a/malva/rep/GA/newGA.req.o b/malva/rep/GA/newGA.req.o index 283670cb85547df1d706371b120197be0558692c..b38ae8dbb12876fd40eff199783aea8c7db92186 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/datos_cantidad_empleados b/malva/rep/GA/res/datos_cantidad_empleados new file mode 100644 index 0000000000000000000000000000000000000000..7813681f5b41c028345ca62a2be376bae70b7f61 --- /dev/null +++ b/malva/rep/GA/res/datos_cantidad_empleados @@ -0,0 +1 @@ +5 \ No newline at end of file diff --git a/malva/rep/GA/res/datos_cantidad_tareas b/malva/rep/GA/res/datos_cantidad_tareas new file mode 100644 index 0000000000000000000000000000000000000000..301160a93062df23030a69f4b5e4d9bf71866ee9 --- /dev/null +++ b/malva/rep/GA/res/datos_cantidad_tareas @@ -0,0 +1 @@ +8 \ No newline at end of file diff --git a/malva/rep/GA/res/datos_empleados b/malva/rep/GA/res/datos_empleados new file mode 100644 index 0000000000000000000000000000000000000000..f7cc5e5ffd3d2a61ae5180d3f7da918b35f73197 --- /dev/null +++ b/malva/rep/GA/res/datos_empleados @@ -0,0 +1,4 @@ +e1 e2 e3 e4 e5 +4 5 5 3 3 +0.05 0.20 0.30 0.95 0.50 +120 200 210 230 180 diff --git a/malva/rep/GA/res/datos_tareas b/malva/rep/GA/res/datos_tareas new file mode 100644 index 0000000000000000000000000000000000000000..589272f5c15492f95741ff1b7c8bd9669e9d4993 --- /dev/null +++ b/malva/rep/GA/res/datos_tareas @@ -0,0 +1,3 @@ +20 +t1 t2 t3 t4 t5 t6 t7 t8 +16 28 11 51 2 23 43 15 diff --git a/malva/rep/GA/res/sol.txt b/malva/rep/GA/res/sol.txt index 2c2640332dc468c0d8e0322030def6b45c2beabf..94d8af392a9a33d04b7d961d7eabcdded57dc43a 100644 --- a/malva/rep/GA/res/sol.txt +++ b/malva/rep/GA/res/sol.txt @@ -3,14 +3,14 @@ STATISTICS OF TRIALS ------------------------------------------------------------------ -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 +1 10029.7 210883 1060 10 1591 14184 +2 10010.4 210882 5560 55 6233 12361 +3 10010.4 210656 4160 41 8672 14731 +4 10009.9 210921 160 1 106 9399 +5 10010.4 210807 4660 46 4349 9349 +6 10010.4 210921 760 7 657 9224 +7 10016.5 210990 3060 30 5449 11825 +8 10015.4 210452 4960 49 5341 10215 +9 10035.1 210876 5960 59 5591 9491 +10 10017.7 210873 360 3 295 9422 ------------------------------------------------------------------ diff --git a/malva/rep/GA/solution b/malva/rep/GA/solution index 4f80418ee7845f5482f7cbc62b73e7f59eb49222..534d0b7a1dbc05e068cc2ef7d4af1f20ef8ed28c 100644 --- a/malva/rep/GA/solution +++ b/malva/rep/GA/solution @@ -1,3 +1,3 @@ -1 1 2 5 6 7 -2 3 8 -5 4 +e1 t3 t1 +e3 t5 t6 t4 +e4 t8 t2 t7