diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/MainLan b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/MainLan index 084a42db6326b5de42f77b939f818132fbe605b0..98b7ca3a11f6bba9a06427709a234279a43463b5 100755 Binary files a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/MainLan and b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/MainLan differ diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/MainSeq b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/MainSeq index 21c2dbec314e3026e9032678bd7bc584a15458ef..30047a9d68025a5ea304598c2e8d327aa6c82d67 100755 Binary files a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/MainSeq and b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/MainSeq differ diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.pro.cc b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.pro.cc index dfdd244b4322ca4b6ef4b9066b857263a7a4c784..11895442d251a4bb2ba9ecb26fd6061ff173d22e 100644 --- a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.pro.cc +++ b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.pro.cc @@ -1416,6 +1416,7 @@ skeleton newGA char data_stored[_current_best_solution.get_nitems() + _current_best_solution.get_length()]; _current_best_solution.get_contents(data_stored, nitems, length); sol.to_Solution(data_stored); + cout << "Estoy entrando en este" << endl; return sol; } diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.pro.o b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.pro.o index 64351f3c24ea6c9c8774005a8e64ab8653ce6229..052c3abf4a7d31ecb07a7212d34348d256bae1d4 100644 Binary files a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.pro.o and b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.pro.o differ diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.req.cc b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.req.cc index 3718842a33099531d7d51614204c96afc20f648d..5520c96f0f9cf52543b2c5d1ad865276221feef9 100644 --- a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.req.cc +++ b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.req.cc @@ -259,12 +259,26 @@ skeleton newGA Solution::Solution (const Problem& pbm):_pbm(pbm),_var(pbm.cantColumnas()) { - _init = 0; + for (int i = 0; i <pbm.cantColumnas(); i++) { Rarray<int> aux(pbm.cantFilas()); _var[i] = aux; } + + + _var = Rarray<Rarray<int>>(_pbm.cantColumnas()); + + for (int x = 0; x < _pbm.cantColumnas(); x++) + { + //cout<<"x=" << x << endl; + _var[x] = Rarray<int>(_pbm.cantFilas()); + for (int y = 0; y < _pbm.cantFilas(); y++) + { + //cout<<"y=" << y << endl; + _var[x][y] = 0; + } + } } const Problem& Solution::pbm() const @@ -274,8 +288,24 @@ skeleton newGA Solution::Solution(const Solution& sol):_pbm(sol.pbm()) { - _init = 1; + _var = Rarray<Rarray<int>>(_pbm.cantColumnas()); + + for (int x = 0; x < _pbm.cantColumnas(); x++) + { + //cout<<"x=" << x << endl; + _var[x] = Rarray<int>(_pbm.cantFilas()); + for (int y = 0; y < _pbm.cantFilas(); y++) + { + //cout<<"y=" << y << endl; + _var[x][y] = 0; + } + } *this=sol; + if (_var[0][0] != 0 && _var[0][0] != 1) + { + cout << "cons sol" << endl; + exit(EXIT_FAILURE); + } } istream& operator>> (istream& is, Solution& sol) @@ -322,9 +352,9 @@ skeleton newGA NetStream& operator << (NetStream& ns, const Solution& sol) { cout << "Empieza ns << sol" << endl; - for (int i=0;i<sol._var.size();i++) - { - for (int j=0; j<sol._var[i].size();j++) + for(int i = 0; i < sol._pbm.cantColumnas(); i++) + { + for (int j=0; j<sol._pbm.cantFilas();j++) { ns << sol._var[i][j]; @@ -337,9 +367,9 @@ skeleton newGA NetStream& operator >> (NetStream& ns, Solution& sol) { - for (int i=0;i<sol._var.size();i++) - { - for (int j=0; j<sol._var[i].size();j++) + for(int i = 0; i < sol._pbm.cantColumnas(); i++) + { + for (int j=0; j<sol._pbm.cantFilas();j++) { ns >> sol._var[i][j]; } @@ -349,15 +379,21 @@ skeleton newGA Solution& Solution::operator= (const Solution &sol) { _var=sol._var; + if (_var[0][0] != 0 && _var[0][0] != 1) + { + cout << "=======" << endl; + exit(EXIT_FAILURE); + } + return *this; } bool Solution::operator== (const Solution& sol) const { if (sol.pbm() != _pbm) return false; - for(int i = 0; i < _var.size(); i++) + for(int i = 0; i < _pbm.cantColumnas(); i++) { - for (int j=0; j<sol._var[i].size();j++) + for (int j=0; j<sol._pbm.cantFilas();j++) { if(_var[i][j] != sol._var[i][j]) return false; } @@ -453,17 +489,24 @@ skeleton newGA bool terminoBloque = true; while (columna < _pbm.cantColumnas()) { - if (_var[i][columna] == 1) + if (_var[columna][i] == 1) { cantSeguidos++; terminoBloque=false; } else { - if (!terminoBloque) + if (cantSeguidos > 0) { - resultado= abs(datosDeFilas[i][iteradorGrupos] - cantSeguidos); //valor absoluto de la resta - fitness= 1000*resultado; + if (iteradorGrupos < cantGruposDeFila[i]) + { + resultado= abs(datosDeFilas[i][iteradorGrupos] - cantSeguidos); //valor absoluto de la resta + } + else + { + resultado = cantSeguidos; + } + fitness= fitness + 1000 * resultado; terminoBloque=true; iteradorGrupos++; resultado=0; @@ -493,10 +536,14 @@ skeleton newGA for(int col = 0; col < _pbm.cantColumnas(); col++) { cout << _var[col][fil] << " "; + if (_var[col][fil] != 0 && _var[col][fil] != 1) + { + //exit(EXIT_FAILURE); + } } cout << endl; } - + cout << "ESTOY SALIENDO DE FITNESS" << endl; if (fitness == 0) { @@ -507,29 +554,38 @@ skeleton newGA } char *Solution::to_String() const - { - - cout << "init: " << _init << endl; - Rarray<int> aux = Rarray<int>(_pbm.cantColumnas() * _pbm.cantFilas()); + { cout << "EN TO STRING" << endl; + Rarray<int> aux = Rarray<int>((_pbm.cantColumnas() + 1) * _pbm.cantFilas()); int c = 0; + for (int k = 0; k < _pbm.cantFilas(); k++) + { + aux[c] = -1; + c++; + } for(int i = 0; i < _pbm.cantColumnas(); i++) { for(int j = 0; j < _pbm.cantFilas(); j++) { // if (_init == 1) // { - cout << "i = " << i << endl; - cout << "j = " << j << endl; - cout << "c = " << c << endl; + if (i == 0) + { + cout << "j=TO STRING " << j << " -> " << _var[i][j] << endl; + } + if (_var[i][j] != 0 && _var[i][j] != 1) + { + exit(EXIT_FAILURE); + } aux[c] = _var[i][j]; - cout << "hi" <<endl; // } // else // { // aux[c] = 0; // } + cout << _var[i][j] << " "; c++; } + cout << endl; } //char * k = (char *)_var.get_first(); //cout << "ACA" << (int *)k << endl; @@ -538,20 +594,30 @@ skeleton newGA void Solution::to_Solution(char *_string_) { + cout << "toSolution" << endl; int *ptr=(int *)_string_; - Rarray<int> aux = Rarray<int>(_pbm.cantColumnas() * _pbm.cantFilas()); + Rarray<int> aux = Rarray<int>((_pbm.cantColumnas()+1) * _pbm.cantFilas()); for (int i=0;i< (_pbm.cantColumnas() * _pbm.cantFilas());i++) { aux[i]=*ptr; ptr++; } - int c = 0; + int c = _pbm.cantFilas(); for(int i = 0; i < _pbm.cantColumnas(); i++) { for(int j = 0; j < _pbm.cantFilas(); j++) { _var[i][j] = aux[c]; + if (i == 0) + { + cout << "j= " << j << " -> " << _var[i][j] << endl; + } + + if (_var[i][j] != 0 && _var[i][j] != 1) + { + //exit(EXIT_FAILURE); + } c++; } } @@ -824,91 +890,92 @@ skeleton newGA } */ void Mutation::mutate(Solution& sol) const - { cout << "ESTOY ENTRANDO A MUTATION" << endl; - std::vector<std::vector<int>> _grupos = sol.pbm().datosColumnas(); - - Rarray<Rarray<int>> tempVar = sol.array_var(); - for (int col = 0; col < tempVar.size(); col++) - { - for(int fil = 0; fil < tempVar[col].size(); fil++) - { - cout << tempVar[col][fil] << " "; - } - cout << endl; - } - - cout << "printed sol" << endl; - - for (int i=0;i<sol.pbm().cantColumnas();i++) - { - cout << "forColumnasBegin" << endl; - int j = 0; - cout << "grupos.size() = " << _grupos[i].size() << endl; - for (int g=0; g<_grupos[i].size(); g++) - { - cout << "forGruposBegin" << endl; - cout << "i = " << i << endl; - cout << "j = " << j << endl; - //cout << "sol.var(i, j) = " << sol.var(i,j) << endl; - while (sol.var(i, j) != 1) - { - j+= 1; - //cout << "sol.var(i, j) = " << sol.var(i,j) << endl; - } - cout << "j after while = " << j << endl; - cout << "here?" << endl; - if (rand01()<=probability[1]) - { - cout << "selectedForMutation" << endl; - - int selector = rand_int(0, 1); - if (selector == 0) - { - cout << "selector0Begin" << endl; - if ((j-2 >= 0) && (sol.var(i, j - 2) == 0)) - { - sol.var(i, j - 1) = 1; - sol.var(i, j + _grupos[i][g] - 1) = 0; - j = j + _grupos [i][g] - 1; - } - else if ((j + _grupos[i][g] + 1 < sol.pbm().cantFilas()) && (sol.var(i, j + _grupos[i][g] + 1) == 0)) - { - sol.var(i, j) = 0; - sol.var(i, j + _grupos[i][g]) = 1; - j = j + _grupos [i][g] + 1; - } - cout << "selector0End" << endl; - } - else - { - cout << "selector1Begin" << endl; - if ((j + _grupos[i][g] + 1 < sol.pbm().cantFilas()) && (sol.var(i, j + _grupos[i][g] + 1) == 0)) - { - sol.var(i, j) = 0; - sol.var(i, j + _grupos[i][g]) = 1; - j = j + _grupos [i][g] + 1; - } - else if ((j-2 >= 0) && (sol.var(i, j - 2) == 0)) - { - sol.var(i, j - 1) = 1; - sol.var(i, j + _grupos[i][g] - 1) = 0; - j = j + _grupos [i][g] - 1; - } - cout << "selector1End" << endl; - } - } - else - { - j = j + _grupos[i][g]; - } - - cout << "forColumnasEnd" << endl; - } - - cout << "forColumnasEnd" << endl; - } - - cout << "ESTOY SALIENDO DE MUTATION" << endl; + { +// cout << "ESTOY ENTRANDO A MUTATION" << endl; +// std::vector<std::vector<int>> _grupos = sol.pbm().datosColumnas(); +// +// Rarray<Rarray<int>> tempVar = sol.array_var(); +// for (int col = 0; col < tempVar.size(); col++) +// { +// for(int fil = 0; fil < tempVar[col].size(); fil++) +// { +// cout << tempVar[col][fil] << " "; +// } +// cout << endl; +// } +// +// cout << "printed sol" << endl; +// +// for (int i=0;i<sol.pbm().cantColumnas();i++) +// { +// cout << "forColumnasBegin" << endl; +// int j = 0; +// cout << "grupos.size() = " << _grupos[i].size() << endl; +// for (int g=0; g<_grupos[i].size(); g++) +// { +// cout << "forGruposBegin" << endl; +// cout << "i = " << i << endl; +// cout << "j = " << j << endl; +// //cout << "sol.var(i, j) = " << sol.var(i,j) << endl; +// while (sol.var(i, j) != 1) +// { +// j+= 1; +// //cout << "sol.var(i, j) = " << sol.var(i,j) << endl; +// } +// cout << "j after while = " << j << endl; +// cout << "here?" << endl; +// if (rand01()<=probability[1]) +// { +// cout << "selectedForMutation" << endl; +// +// int selector = rand_int(0, 1); +// if (selector == 0) +// { +// cout << "selector0Begin" << endl; +// if ((j-2 >= 0) && (sol.var(i, j - 2) == 0)) +// { +// sol.var(i, j - 1) = 1; +// sol.var(i, j + _grupos[i][g] - 1) = 0; +// j = j + _grupos [i][g] - 1; +// } +// else if ((j + _grupos[i][g] + 1 < sol.pbm().cantFilas()) && (sol.var(i, j + _grupos[i][g] + 1) == 0)) +// { +// sol.var(i, j) = 0; +// sol.var(i, j + _grupos[i][g]) = 1; +// j = j + _grupos [i][g] + 1; +// } +// cout << "selector0End" << endl; +// } +// else +// { +// cout << "selector1Begin" << endl; +// if ((j + _grupos[i][g] + 1 < sol.pbm().cantFilas()) && (sol.var(i, j + _grupos[i][g] + 1) == 0)) +// { +// sol.var(i, j) = 0; +// sol.var(i, j + _grupos[i][g]) = 1; +// j = j + _grupos [i][g] + 1; +// } +// else if ((j-2 >= 0) && (sol.var(i, j - 2) == 0)) +// { +// sol.var(i, j - 1) = 1; +// sol.var(i, j + _grupos[i][g] - 1) = 0; +// j = j + _grupos [i][g] - 1; +// } +// cout << "selector1End" << endl; +// } +// } +// else +// { +// j = j + _grupos[i][g]; +// } +// +// cout << "forColumnasEnd" << endl; +// } +// +// cout << "forColumnasEnd" << endl; +// } +// +// cout << "ESTOY SALIENDO DE MUTATION" << endl; } void Mutation::execute(Rarray<Solution*>& sols) const diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.req.o b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.req.o index 31f51e1b7708fe411aceb3faefcaf3b082468ed1..bf75e3bb74e95103c2dc4c05f0132026453f27b3 100644 Binary files a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.req.o and b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.req.o differ diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/res/sol.txt b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/res/sol.txt index e998daed0dcc264d793822a4f9949d6a57802c9e..4ce60fb2a68c2cd84f1ad181bc4066f3a062c2d3 100644 --- a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/res/sol.txt +++ b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/res/sol.txt @@ -3,14 +3,14 @@ STATISTICS OF TRIALS ------------------------------------------------------------------ -1 0 2100 60 0 0 0 -2 0 2100 60 0 0 0 -3 0 2100 60 0 0 0 -4 0 2100 60 0 0 0 -5 0 2100 60 0 0 0 -6 0 2100 60 0 0 0 -7 0 2100 60 0 0 0 -8 0 2100 60 0 0 0 -9 0 2100 60 0 0 0 -10 0 2100 60 0 0 0 +1 22300 57300 860 8 40337 383813 +2 21300 54200 1160 11 47287 413008 +3 17200 58400 160 1 4412 373711 +4 23300 52200 60 0 0 377470 +5 15000 62400 1060 10 39140 377810 +6 19500 53400 760 7 29629 410845 +7 20300 59200 460 4 13027 378379 +8 15100 57100 1060 10 31828 394500 +9 18300 61300 560 5 30163 407316 +10 18300 61400 560 5 15957 393246 ------------------------------------------------------------------