diff --git a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.hh b/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.hh index 45ebc6cce6ab989aa0d4a315fb00f9204228ca22..3837580e57b039cd7eda3dd72862096cc35edb11 100644 --- a/ProyectoFinal/AlgoritmoGenetico/malva/rep/GA/newGA.hh +++ b/ProyectoFinal/AlgoritmoGenetico/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 *** @@ -15,9 +15,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; @@ -63,12 +63,12 @@ skeleton newGA bool operator!= (const Problem& pbm) const; Direction direction () const; - + //------------------------------------------------------------------------------------------------ int cantFilas() const; int cantColumnas() const; - + std::vector<std::vector<int>> datosColumnas() const; std::vector<std::vector<int>> datosFilas() const; @@ -113,18 +113,18 @@ skeleton newGA double fitness (); unsigned int size() const; - Rarray<int>& var(const int indexC); + Rarray<int>& var(const int indexC); int& var(const int indexC, const int indexF); Rarray<Rarray<int>>& array_var(); - - int& init(); - - + + int& init(); + + private: Rarray<Rarray<int>> _var; const Problem& _pbm; - int _init; + int _init; }; @@ -357,7 +357,7 @@ skeleton newGA unsigned int _upper_cost,_lower_cost; // lower and upper fitness of individuals in population unsigned long _evaluations; double _average_cost; - + inline void Evaluate(Solution* sols,struct individual &_f); public: @@ -404,7 +404,7 @@ skeleton newGA Solution& worst_solution() const; double average_cost() const; double standard_deviation() const; - }; + }; // Inter_Operator ( abstract )----------------------------------------------------------- @@ -420,7 +420,7 @@ skeleton newGA unsigned int _number_operator; const Direction direction; - + public: Inter_Operator(const unsigned int _number_op, const Direction dir); virtual ~Inter_Operator(); @@ -453,18 +453,18 @@ skeleton newGA provides class Selection { protected: - unsigned int _number_selection; + unsigned int _number_selection; const Direction direction; public: Selection(const Direction dir); Selection(const unsigned int _number_sel, const Direction dir); - virtual ~Selection(); + virtual ~Selection(); friend ostream& operator<< (ostream& os, const Selection& sel); - virtual void prepare(Rarray<struct individual>& fitness_values,const bool remplace); // const; + virtual void prepare(Rarray<struct individual>& fitness_values,const bool remplace); // const; virtual struct individual select_one(const Rarray<Solution*>& to_select_1,const Rarray<Solution*>& to_select_2,const Rarray<struct individual>& fitness_values,const unsigned int dummy,const bool remplace) const; unsigned int number_selection() const; }; @@ -479,11 +479,11 @@ skeleton newGA friend ostream& operator<< (ostream& os, const Selection_Tournament& sel); - virtual struct individual select_one(const Rarray<Solution*>& to_select_1,const Rarray<Solution*>& to_select_2,const Rarray<struct individual>& fitness_values,const unsigned int tourment_size,const bool remplace) const; + virtual struct individual select_one(const Rarray<Solution*>& to_select_1,const Rarray<Solution*>& to_select_2,const Rarray<struct individual>& fitness_values,const unsigned int tourment_size,const bool remplace) const; }; // Selection_Roulette_Wheel --------------------------------------------------------------------------------- - + provides class Selection_Roulette_Wheel: public Selection { public: @@ -518,7 +518,7 @@ skeleton newGA provides class Selection_Best: public Selection_Rank { private: - mutable unsigned int selection_best_position; + mutable unsigned int selection_best_position; public: Selection_Best(const Direction); @@ -535,7 +535,7 @@ skeleton newGA provides class Selection_Worst: public Selection_Rank { private: - mutable unsigned int selection_worst_position; + mutable unsigned int selection_worst_position; public: Selection_Worst(const Direction); @@ -677,8 +677,8 @@ skeleton newGA unsigned int current_trial() const; unsigned long current_iteration() const; - unsigned long current_evaluations() const; - Solution current_best_solution() const; + unsigned long current_evaluations() const; + Solution current_best_solution() const; double current_best_cost() const; double current_worst_cost() const; double current_average_cost() const; @@ -794,7 +794,7 @@ skeleton newGA float _time_best_found_in_trial; unsigned long _iteration_best_found_in_trial; unsigned long _evaluations_best_found_in_trial; - + // Termination phase // bool final_phase; unsigned long acum_iterations; @@ -873,7 +873,7 @@ skeleton newGA void check_for_refresh_global_state(); void reset(); }; - + } #endif diff --git a/ProyectoFinal/CHC/malva/rep/CHC/CHC.hh b/ProyectoFinal/CHC/malva/rep/CHC/CHC.hh index 0c89fe4077468e8dfd5084c3c754a083e50b4978..25e5bafadf01ee940378c7e3eb64043e76b44de3 100644 --- a/ProyectoFinal/CHC/malva/rep/CHC/CHC.hh +++ b/ProyectoFinal/CHC/malva/rep/CHC/CHC.hh @@ -1,7 +1,7 @@ /********************************************************************************************************* *** *** *** CHC Skeleton v1.5 *** -*** Developed by: Gabriel Jesús Luque Polo *** +*** Developed by: Gabriel Jes�s Luque Polo *** *** *** *** Last Update: 30-01-2004 *** *** *** @@ -57,11 +57,25 @@ skeleton CHC bool operator!= (const Problem& pbm) const; Direction direction () const; - int dimension() const; + //int dimension() const; - private: + int cantFilas() const; + int cantColumnas() const; + + std::vector<std::vector<int>> datosColumnas() const; + std::vector<std::vector<int>> datosFilas() const; + + std::vector<int> cantGruposColumna() const; + std::vector<int> cantGruposFila() const; - int _dimension; + private: + int _cantFilas; + int _cantColumnas; + std::vector<std::vector<int>> _datosColumnas; + std::vector<std::vector<int>> _datosFilas; + std::vector<int> _cantGruposColumna; + std::vector<int> _cantGruposFila; + //int _dimension; }; //Solution ---------------------------------------------------------------------------- @@ -97,12 +111,16 @@ skeleton CHC void swap(const int index, Solution &s); void invalid(); - int& var(const int index); - Rarray<int>& array_var(); + Rarray<int>& var(const int indexC); + int& var(const int indexC, const int indexF); + Rarray<Rarray<int>>& array_var(); + + int& init(); private: - Rarray<int> _var; + Rarray<Rarray<int>> _var; const Problem& _pbm; + int _init; }; @@ -345,7 +363,7 @@ skeleton CHC public: inline void Evaluate(Solution *s, struct individual & _f); - + Population(const Problem& pbm,const SetUpParams& setup); // crea un array de objetos population; ~Population(); @@ -414,7 +432,7 @@ skeleton CHC friend ostream& operator<< (ostream& os, const Inter_Operator& inter); virtual void execute(Population& pop,const unsigned long current_generation,NetStream& _netstream,const bool synchronized,const unsigned int check_asyncrhonous) const=0; - virtual void setup(char line[MAX_BUFFER]); + virtual void setup(char line[MAX_BUFFER]); unsigned int number_operator() const; virtual void RefreshState(const StateCenter& _sc) const; @@ -517,7 +535,7 @@ skeleton CHC }; // Selection_Roulette_Wheel --------------------------------------------------------------------------------- - + provides class Selection_Roulette_Wheel: public Selection { public: @@ -526,7 +544,7 @@ skeleton CHC friend ostream& operator<< (ostream& os, const Selection_Roulette_Wheel& sel); - virtual void prepare(Rarray<struct individual>& fitness_values,const bool remplace); // const; + virtual void prepare(Rarray<struct individual>& fitness_values,const bool remplace); // const; virtual struct individual select_one(const Rarray<Solution*>& to_select_1,const Rarray<Solution*>& to_select_2,const Rarray<struct individual>& fitness_values,const unsigned int dummy,const bool remplace) const; }; @@ -543,7 +561,7 @@ skeleton CHC virtual void prepare(Rarray<struct individual>& fitness_values,const bool remplace); // const; virtual void reset(); - + virtual struct individual select_one(const Rarray<Solution*>& to_select_1,const Rarray<Solution*>& to_select_2,const Rarray<struct individual>& fitness_values,const unsigned int portion,const bool remplace) const; }; @@ -552,7 +570,7 @@ skeleton CHC provides class Selection_Best: public Selection_Rank { private: - mutable unsigned int selection_best_position; + mutable unsigned int selection_best_position; public: Selection_Best(const Direction); @@ -569,7 +587,7 @@ skeleton CHC provides class Selection_Worst: public Selection_Rank { private: - mutable unsigned int selection_worst_position; + mutable unsigned int selection_worst_position; public: Selection_Worst(const Direction); diff --git a/ProyectoFinal/CHC/malva/rep/CHC/CHC.req.cc b/ProyectoFinal/CHC/malva/rep/CHC/CHC.req.cc index df05c01fec88760531488a5f2bb22cb7abd1e435..f23da9bfe3628d36b5f8b17d894a6b56e11f8db9 100644 --- a/ProyectoFinal/CHC/malva/rep/CHC/CHC.req.cc +++ b/ProyectoFinal/CHC/malva/rep/CHC/CHC.req.cc @@ -5,29 +5,164 @@ skeleton CHC { - + int currentBestFitness = INT_MAX; // Problem --------------------------------------------------------------- - Problem::Problem ():_dimension(0) - {} + Problem::Problem ():_cantFilas(0),_cantColumnas(0),_datosFilas(NULL),_datosColumnas(NULL), _cantGruposColumna(NULL), _cantGruposFila(NULL) + { + //TUR + //cout << "uf6"; + } ostream& operator<< (ostream& os, const Problem& pbm) { - os << endl << endl << "Number of Variables " << pbm._dimension - << endl; + os << endl << endl << "Number of Columns " << pbm._cantColumnas << endl; + + os << "Number of Rows " << pbm._cantFilas << endl; + return os; } - istream& operator>> (istream& is, Problem& pbm) + std::string trim(std::string const& str) { - char buffer[MAX_BUFFER]; - int i; + std::string word; + std::stringstream stream(str); + stream >> word; + return word; + } - is.getline(buffer,MAX_BUFFER,'\n'); - sscanf(buffer,"%d",&pbm._dimension); +istream& operator>> (istream& is, Problem& pbm) +{ - return is; - } + char buffer[MAX_BUFFER]; + int i; + + //is.getline(buffer,MAX_BUFFER,'\n'); + //sscanf(buffer,"%d",&pbm._dimension); + + //CARGAR DATOS COLUMNAS + ifstream inFile; + inFile.open("datos_columnas"); + if (!inFile) + { + cerr << "Unable to open file datafile.txt"; + exit(1); // call system to stop + } + /* + string x; + getline(inFile, x); + pbm._cantColumnas = stoi(x); + for(int cCol = 0; cCol < pbm._cantColumnas; cCol++) + { + getline(inFile, x); + std::vector<std::string> divided = split(x, " "); + pbm._datosColumnas.push_back(divided); + pbm._cantGruposColumna.push_back(divided.size()); + } + inFile.close(); + */ + + string line2; + getline(inFile,line2); + line2 = trim(line2); + pbm._cantColumnas = stoi(line2); + cout << pbm._cantColumnas; + for(int c = 0; c < pbm.cantColumnas(); c++) + { + getline(inFile, line2); + + std::vector<int> divided; + + int n = line2.length(); + int len=1; + int t=0; + int z=0; + for(int i=0; i<=line2.length(); i++) + { + //cout << line2.length() << "LENGTH LINEA 2" << endl; + if(line2[i] == ' '|| i==line2.length()) + { + string rr = line2.substr(z,len); + if (rr != "") + { + //cout << rr << "ANTES DE TRIM" << endl; + rr = trim(rr); + //cout << "columna" << c << rr << endl; + //cout << rr << "LUEGO DE TRIM" << endl; + divided.push_back(stoi(rr)); + } + + //cout << rr << " "; + t=t+1; + z=i+1; + len=1; + //s.erase(i,1); + } + else len=len+1; + + } + pbm._datosColumnas.push_back(divided); + pbm._cantGruposColumna.push_back(divided.size()); + + } + inFile.close(); + + //CARGAR DATOS FILAS + ifstream inFile2; + inFile2.open("datos_filas"); + if (!inFile2) + { + cerr << "Unable to open file datafile.txt"; + exit(1); // call system to stop + } + + //string line2; + getline(inFile2,line2); + line2 = trim(line2); + pbm._cantFilas = stoi(line2); + //cout << "CANT FILAS" << pbm._cantFilas << endl; + for(int c = 0; c < pbm.cantFilas(); c++) + { + getline(inFile2, line2); + + std::vector<int> divided; + + int n = line2.length(); + 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); + if (rr != "") + { + //cout << rr << "ANTES DE TRIM" << endl; + rr = trim(rr); + //cout << "linea" << c << rr << endl; + //cout << rr << "LUEGO DE TRIM" << endl; + divided.push_back(stoi(rr)); + } + + //cout << rr << " "; + t=t+1; + z=i+1; + len=1; + //s.erase(i,1); + } + else len=len+1; + } + pbm._datosFilas.push_back(divided); + pbm._cantGruposFila.push_back(divided.size()); + + } + exit; + inFile2.close(); + + cout<<pbm; + return is; +} Problem& Problem::operator= (const Problem& pbm) { @@ -36,7 +171,7 @@ skeleton CHC bool Problem::operator== (const Problem& pbm) const { - if (_dimension!=pbm.dimension()) return false; + if (_cantColumnas != pbm.cantColumnas() || _cantFilas != pbm.cantFilas()) return false; return true; } @@ -47,13 +182,43 @@ skeleton CHC Direction Problem::direction() const { - return maximize; - //return minimize; + //return maximize; + return minimize; } - int Problem::dimension() const + /*int Problem::dimension() const { return _dimension; + }*/ + + int Problem::cantColumnas() const + { + return _cantColumnas; + } + + int Problem::cantFilas() const + { + return _cantFilas; + } + + std::vector<std::vector<int>> Problem::datosFilas() const + { + return _datosFilas; + } + + std::vector<std::vector<int>> Problem::datosColumnas() const + { + return _datosColumnas; + } + + std::vector<int> Problem::cantGruposColumna() const + { + return _cantGruposColumna; + } + + std::vector<int> Problem::cantGruposFila() const + { + return _cantGruposFila; } Problem::~Problem() @@ -61,8 +226,29 @@ skeleton CHC // Solution -------------------------------------------------------------- - Solution::Solution (const Problem& pbm):_pbm(pbm),_var(pbm.dimension()) - {} + Solution::Solution (const Problem& pbm):_pbm(pbm) + { + /* + 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 @@ -72,41 +258,96 @@ skeleton CHC Solution::Solution(const Solution& sol):_pbm(sol.pbm()) { + _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) { - for (int i=0;i<sol.pbm().dimension();i++) - is >> sol._var[i]; - + for (int i=0;i<sol.pbm().cantColumnas();i++) + { + for (int j = 0; j<sol.pbm().cantFilas(); j++) + { + is >> sol._var[i][j]; + } + } + return is; } ostream& operator<< (ostream& os, const Solution& sol) { - for (int i=0;i<sol.pbm().dimension();i++) - os << " " << sol._var[i]; + cout << "Empieza os << sol" << endl; + //for (int i=0;i<sol.pbm().dimension();i++) + // os << " " << sol._var[i]; + stringstream ss; + + ofstream outfile("solution"); + + os << endl; + for (int fil = 0; fil < sol.pbm().cantFilas(); fil++) + { + for(int col = 0; col < sol.pbm().cantColumnas(); col++) + { + cout << sol._var[col][fil] << " "; + } + cout << endl; + } + + outfile << ss.str(); + outfile.close(); + + os << ss.str(); + cout << "Termina os << sol" << endl; + return os; } NetStream& operator << (NetStream& ns, const Solution& sol) { - for (int i=0;i<sol._var.size();i++) - ns << sol._var[i]; + cout << "Empieza ns << sol" << endl; + for(int i = 0; i < sol._pbm.cantColumnas(); i++) + { + for (int j=0; j<sol._pbm.cantFilas();j++) + { + ns << sol._var[i][j]; + + } + } + cout << "Termina ns << sol" << endl; + return ns; } - NetStream& operator >> (NetStream& ns, Solution& sol) { - for (int i=0;i<sol._var.size();i++) - ns >> sol._var[i]; - return ns; + for(int i = 0; i < sol._pbm.cantColumnas(); i++) + { + for (int j=0; j<sol._pbm.cantFilas();j++) + { + ns >> sol._var[i][j]; + } + } } - Solution& Solution::operator= (const Solution &sol) { _var=sol._var; @@ -117,6 +358,13 @@ skeleton CHC bool Solution::operator== (const Solution& sol) const { if (sol.pbm() != _pbm) return false; + for(int i = 0; i < _pbm.cantColumnas(); i++) + { + for (int j=0; j<sol._pbm.cantFilas();j++) + { + if(_var[i][j] != sol._var[i][j]) return false; + } + } return true; } @@ -127,83 +375,341 @@ skeleton CHC void Solution::initialize() { - for (int i=0;i<_pbm.dimension();i++) - _var[i]=rand_int(0,1); - } - - - double Solution::fitness () const - { - double fitness = 0.0; - - if(_var[0] == 2) return 0.0; - - for (int i=0;i<_var.size();i++) - fitness += _var[i]; + //cout << "ESTOY ENTRANDO A INITIALIZE" << endl; + + _var = Rarray<Rarray<int>>(_pbm.cantColumnas()); + _init = 1; + 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; + } + } + //cout << "MID1" << endl; + for (int i=0;i<_pbm.cantColumnas();i++) + { + int _firstPossible = 0; + std::vector<std::vector<int>> _grupos = _pbm.datosColumnas(); + std::vector<int> _cantGruposEnColumna = _pbm.cantGruposColumna(); + /* + cout << "Grupos en columna " << i << ": "; + for(int j=0; j < _cantGruposEnColumna[i]; j++) + { + cout << _grupos[i][j] << " "; + } + cout << endl; + */ + 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; + } + //cout << "firstPos = " << _firstPossible << endl; + //cout << "lastPos = " << _lastPossible << endl; + + int _startPosition = rand_int(_firstPossible, _lastPossible); + //cout << "start = " << _startPosition << endl; + int _endPosition = _startPosition + _grupos[i][j] - 1; + //cout << "end = " << _endPosition << endl; + for(int c = _startPosition; c <= _endPosition; c++) + { + _var[i][c] = 1; + } + + _firstPossible = _endPosition + 2; + } + cout << endl; + } + /* + for (int fil = 0; fil < _pbm.cantFilas(); fil++) + { + for(int col = 0; col < _var.size(); col++) + { + cout << _var[col][fil] << " "; + } + cout << endl; + } + */ + + // cout << "ESTOY SALIENDO A INITIALIZE" << endl; + } + + + double Solution::fitness () + { + //cout << "ESTOY ENTRANDO A FITNESS" << endl; + double fitness = 0.0; + std::vector<std::vector<int>> datosDeFilas = _pbm.datosFilas(); + std::vector<int> cantGruposDeFila = _pbm.cantGruposFila(); + for (int i=0;i<_pbm.cantFilas();i++) + { + int cantSeguidos=0; + int columna=0; + int cantGrupos= cantGruposDeFila[i]; + int iteradorGrupos=0; + int resultado=0; //Resultado de la diferencia entre el bloque que tiene que haber y el bloque en el juego + int cantUnos=0; + bool terminoBloque = true; + while (columna < _pbm.cantColumnas()) + { + if (_var[columna][i] == 1) + { + cantSeguidos++; + terminoBloque=false; + cantUnos++; + } + else + { + if (cantSeguidos > 0) + { + 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; + } + cantSeguidos=0; + } + columna++; + //Si se termino la columna y faltan bloques + + } + if (cantSeguidos > 0) + { + 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; + } + if(iteradorGrupos != cantGrupos) + { + int restar= abs(cantGrupos - iteradorGrupos); + fitness= fitness + 1100*restar; + } + int totalUnos = 0; + for(int j = 0; j< cantGrupos; j++) + { + totalUnos += datosDeFilas[i][j]; + } + if(cantUnos != totalUnos) + { + int restar= abs(totalUnos - cantUnos); + fitness= fitness + 300*restar; + } + } + extern int currentBestFitness; + if (fitness < currentBestFitness) + { + currentBestFitness = fitness; + } + //cout << "FIL" << _var[0] << endl; + //cout << "FIL" << _var[1] << endl; + + for (int fil = 0; fil < _pbm.cantFilas(); fil++) + { + for(int col = 0; col < _pbm.cantColumnas(); col++) + { + //cout << sol.var(col, fil) << " "; + if (_var[col][fil] != 0 && _var[col][fil] != 1) + { + cout << "fitness end" << endl; + //exit(EXIT_FAILURE); + } + } + //cout << endl; + } + + //cout << "ESTOY SALIENDO DE FITNESS" << endl; + if (fitness == 0) + { + cout << "found sol lmao" << endl; + } return fitness; } char *Solution::to_String() const { - return (char *)_var.get_first(); + cout << "EN TO STRING" << endl; + + for (int fil = 0; fil < _pbm.cantFilas(); fil++) + { + for(int col = 0; col < _pbm.cantColumnas(); col++) + { + //cout << sol.var(col, fil) << " "; + if (_var[col][fil] != 0 && _var[col][fil] != 1) + { + cout << "tostring begin" << endl; + exit(EXIT_FAILURE); + } + } + //cout << 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) +// { + if (i == 0) + { + cout << "j=TO STRING " << j << " -> " << _var[i][j] << endl; + } + aux[c] = _var[i][j]; + c++; + } + } +// for(int i = 0; i < _pbm.cantColumnas(); i++) +// { +// for(int j = 0; j < _pbm.cantFilas(); j++) +// { cout << _var[i][j] << " "; +// } +// cout << endl; +// +// } + + //char * k = (char *)_var.get_first(); + //cout << "ACA" << (int *)k << endl; + return (char *)aux.get_first(); } - void Solution::to_Solution(char *_string_) { + cout << "toSolution" << endl; int *ptr=(int *)_string_; - for (int i=0;i<_pbm.dimension();i++) + Rarray<int> aux = Rarray<int>((_pbm.cantColumnas()+1) * _pbm.cantFilas()); + for (int i=0;i< (_pbm.cantColumnas() * _pbm.cantFilas());i++) { - _var[i]=*ptr; + aux[i]=*ptr; ptr++; } + + + int c = _pbm.cantColumnas(); + 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; + } + + c++; + } + } + + for (int fil = 0; fil < _pbm.cantFilas(); fil++) + { + for(int col = 0; col < _pbm.cantColumnas(); col++) + { + //cout << sol.var(col, fil) << " "; + if (_var[col][fil] != 0 && _var[col][fil] != 1) + { + cout << "tosolution end" << endl; + exit(EXIT_FAILURE); + } + } + //cout << endl; + } + + // int *ptr=(int *)_string_; + // for (int i=0;i<_pbm.cantColumnas();i++) + // { for(int j = 0; j < _pbm.cantFilas(); j++) + // { //cout << _var[0][0]; + // //_var[i][j]=*ptr; + // //ptr++; + // + // } + // } + _init = 1; } unsigned int Solution::size() const { - return (_pbm.dimension() * sizeof(int)); + return (_pbm.cantColumnas() * _pbm.cantFilas() * sizeof(int)); } - int Solution::lengthInBits() const + + Rarray<int>& Solution::var(const int indexC) { - return _pbm.dimension(); + return _var[indexC]; } - void Solution::flip(const int index) + + int& Solution::var(const int indexC, const int indexF) { - _var[index] = 1 - _var[index]; + return _var[indexC][indexF]; } - bool Solution::equalb(const int index,Solution &s) + int Solution::lengthInBits() const { - return _var[index] == s._var[index]; + return(_pbm.cantColumnas() * _pbm.cantFilas()); } - void Solution::swap(const int index, Solution &s) + void Solution::flip(const int indexC, const int indexF) { - int aux = s._var[index]; - s._var[index] = _var[index]; - _var[index] = aux; + _var[indexC][indexF] = 1 - _var[indexC][indexF]; } - void Solution::invalid() + bool Solution::equalb(const int index,Solution &s) { - _var[0] = 2; + return _var[indexC][indexF] == s._var[indexC][indexF]; } - int& Solution::var(const int index) + void Solution::swap(const int indexC, const int indexF, Solution &s) { - return _var[index]; + int aux = s._var[indexC][indexF]; + s._var[indexC][indexF] = _var[indexC][indexF]; + _var[indexC][indexF] = aux; } + void Solution::invalid() + { + _var[0][0] = 2; + } - Rarray<int>& Solution::array_var() + Rarray<Rarray<int>>& Solution::array_var() { return _var; } + int& Solution::init() + { + return _init; + } + Solution::~Solution() {} @@ -310,7 +816,8 @@ skeleton CHC bool StopCondition_1::EvaluateCondition(const Problem& pbm,const Solver& solver,const SetUpParams& setup) { - return ((int)solver.best_cost_trial() == pbm.dimension()); + bool fin=(currentBestFitness == 0 || (int) (solver.current_time_spent() - solver.time_best_found_trial()) > 20000000); + return return fin; } StopCondition_1::~StopCondition_1() @@ -329,4 +836,3 @@ skeleton CHC } } #endif - diff --git a/ProyectoFinal/CHC/malva/rep/CHC/gennongon.py b/ProyectoFinal/CHC/malva/rep/CHC/gennongon.py new file mode 100644 index 0000000000000000000000000000000000000000..155b367f97dd7c1767d5ad0b940d5bdae1f94a23 --- /dev/null +++ b/ProyectoFinal/CHC/malva/rep/CHC/gennongon.py @@ -0,0 +1,94 @@ +import sys +import random +import math +from collections import defaultdict + +try: + #Constantes + width = 10 + height = 10 + archivo_salida = "placeholder" + + #Sanity check + if (len(sys.argv)<4): + print "Ejecutar con la siguiente linea: ./generador <width> <height> <archivo_salida>" + sys.exit(1) + + width=int(sys.argv[1]) + height=int(sys.argv[2]) + archivo_salida=sys.argv[3] + + if (width<1): + print "La cantidad de columnas debe ser mayor o igual a 1." + sys.exit(1) + + + if (height<1): + print "La cantidad de filas debe ser mayor o igual a 1." + sys.exit(1) + + + #VARIABLES: + + #inicializacion tablero + tablero = [] + + for i in range(0,width): + columna = [] + for j in range(0, height): + columna.append(random.randint(0,1)) + + tablero.append(columna) + + ###################### + #calculo de datos de fila / columna + ###################### + + datosColumnas = [] + for i in range(0, width): + datos = [] + counter = 0 + for j in range (0, height): + if (tablero[i][j] == 0): + if (counter != 0): + datos.append(counter) + counter = 0 + else: + counter += 1 + if (counter != 0): + datos.append(counter) + datosColumnas.append(datos) + + datosFilas = [] + for i in range(0, height): + datos = [] + counter = 0 + for j in range (0, width): + if (tablero[j][i] == 0): + if (counter != 0): + datos.append(counter) + counter = 0 + else: + counter += 1 + if (counter != 0): + datos.append(counter) + datosFilas.append(datos) + + + #archivo de salida de columnas + archivo = open ("datos_columnas", "w") + archivo.write("%d\n"%width) + for i in range(0, width): + for j in range(0, len(datosColumnas[i])): + archivo.write("%d "%datosColumnas[i][j]) + archivo.write("\n") + + archivo = open ("datos_filas", "w") + archivo.write("%d\n"%height) + for i in range(0, height): + for j in range(0, len(datosFilas[i])): + archivo.write("%d "%datosFilas[i][j]) + archivo.write("\n") + +except IOError as error: + print error