-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetaheuristica.cpp
More file actions
198 lines (152 loc) · 5.54 KB
/
Metaheuristica.cpp
File metadata and controls
198 lines (152 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "Metaheuristica.h"
#include "Principal.h"
//CONSTRUCTORES Y DESTRUCTORES
//===========================
/*
* Dada la ruta de un fichero, debidamente formateado, construye
* el objeto.
*/
Metaheuristica::Metaheuristica(const std::string& rutaFichero) {
//Leemos el fichero
leerFicheroDAT(rutaFichero);
//Creamos el vector de la solución
solucion = new unsigned[tam];
}
Metaheuristica::~Metaheuristica() {
destruyeMatriz(flujo);
destruyeMatriz(distancias);
delete [] solucion;
}
//MÉTODOS PROTECTED
//=================
void Metaheuristica::construyeMatriz(unsigned**& m){
m = new unsigned*[tam];
for(unsigned i = 0; i < tam; i++){
m[i] = new unsigned[tam];
}
}
void Metaheuristica::destruyeMatriz(unsigned**& m){
for(unsigned i = 0; i < tam; i++) {
delete [] m[i];
}
delete [] m;
}
void Metaheuristica::visualizarMatriz(unsigned**& m) {
for(unsigned i = 0; i < tam; i++) {
std::cout << std::endl;
for(unsigned j = 0; j < tam; j++) {
std::cout << "[" << m[i][j] << "] ";
}
}
}
void Metaheuristica::leerMatriz(unsigned**& m, std::fstream& f){
unsigned row, col; //Indices
row = col = 0;
std::string linea; //Línea actual
std::string valorActual = ""; //Valor a almacenar, en formato texto
unsigned valor; //Valor a almacenar, en formato entero.
//Leemos todos los valores de la matriz
while(row < tam) {
getline(f, linea); //Lectura de la línea
if(Principal::debug) {
std::cout << "Linea actual: " << linea << std::endl;
std::cout << "Tam: " << linea.size() << std::endl;
}
linea += ' ';
//Añadimos un espacio en blanco, en caso de que el útlimo carácter de
//la línea fuera un dato, o parte de él, este no es ignorado y se almacena
//correctamente. Así podemos leer, de manera más simple, la línea.
//Iteramos por toda la línea...
for(unsigned int i = 0; i < linea.size(); i++) {
if(Principal::debug)
std::cout << "Caracter " << i << ": " << linea[i] << std::endl;
//Si no es un espacio en blanco, acumulamos en valorActual
if(linea[i] != ' ')
valorActual += linea[i];
else { //Espacio en blanco
if(valorActual.size() > 0) { //Hay algo que almacenar
//Pasamos de texto a valor entero
std::istringstream buffer(valorActual);
buffer >> valor;
//Reiniciamos el acumulador, valorActual
valorActual = "";
if(Principal::debug)
std::cout << "Guardando " << valor << " en row: " << row << " col: " << col << std::endl;
//Almacenamos en la matriz
m[row][col] = valor;
//Actualizamos los índices
col++;
if(col == tam){
row++;
col = 0;
}
}
}
}
}
}
void Metaheuristica::leerFicheroDAT(const std::string& rutaFichero){
//Variables auxiliares
std::fstream ficheroEntrada;
std::string linea;
ficheroEntrada.open ( rutaFichero.c_str() , std::ios::in);
//Abrimos el fichero
if (ficheroEntrada.is_open()) { //Comprobamos apertura correcta
//Leemos el tamaño
getline(ficheroEntrada, linea);
std::istringstream bufferTam(linea);
bufferTam >> tam;
if(Principal::debug)
std::cout << std::endl << "El tamanio es: " << tam << std::endl;
//Creamos las matrices
construyeMatriz(flujo);
construyeMatriz(distancias);
getline(ficheroEntrada, linea);
//Leemos, e ignoramos, la línea en blanco
if(Principal::debug)
std::cout << "Linea en blanco: " << linea << std::endl;
leerMatriz(flujo, ficheroEntrada);
//Rellenamos la matriz de flujo, con los datos del fichero
if(Principal::debug)
std::cout << std::endl << "FLUJO LEIDO" << std::endl << std::endl;
getline(ficheroEntrada, linea);
//Leemos, e ignoramos, la línea en blanco
if(Principal::debug)
std::cout << "Linea en blanco: " << linea << std::endl;
leerMatriz(distancias, ficheroEntrada);
//Rellenamos la matriz de distancias, con los datos del fichero
if(Principal::debug)
std::cout << std::endl << "DISTANCIAS LEIDAS" << std::endl << std::endl;
ficheroEntrada.close(); //Cerramos el fichero
}
else std::cout << "Fichero inexistente o faltan permisos para abrirlo" << std::endl;
}
unsigned long Metaheuristica::calculaCoste() {
return calculaCoste(solucion);
}
unsigned long Metaheuristica::calculaCoste(unsigned *p) {
unsigned long coste = 0;
for(unsigned i = 0; i < tam; i++)
for(unsigned j = 0; j < tam; j++){
coste += flujo[i][j] * distancias[p[i]][p[j]];
}
return coste;
}
void Metaheuristica::generarSolucion() {
generarSolucion(solucion);
}
void Metaheuristica::generarSolucion(unsigned* p) {
bool* asignados = new bool[tam];
for(unsigned i = 0; i < tam; i++) {
asignados[i] = false;
}
unsigned valor = 0;
for(unsigned i = 0; i < tam; i++) {
do{
valor = rand()%tam;
}while(asignados[valor]);
p[i] = valor;
asignados[valor] = true;
}
delete [] asignados;
}