-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrincipal.cpp
More file actions
365 lines (298 loc) · 9.45 KB
/
Principal.cpp
File metadata and controls
365 lines (298 loc) · 9.45 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "Principal.h"
//VARIABLES ESTÁTICAS
//===================
bool Principal::debug = false;
//CONSTRUCTORES Y DESTRUCTORES
//===========================
Principal::Principal(): metaheuristica(0) {
//Activación del modo debug
activarDebug();
//Definición del fichero
elegirFichero();
//Elección de algoritmo
elegirAlgoritmo();
//Elección de semilla
elegirSemilla();
}
Principal::~Principal() {
if(metaheuristica)
delete metaheuristica;
}
//MÉTODOS PROTECTED
//=================
void Principal::activarDebug(){
char d;
do {
std::cout << "El modo debug mostrara, por pantalla, informacion adicional"
<< " sobre la ejecucion"<< std::endl <<"del programa..."
<< std::endl << "Desea activar el modo debug?"
<< " (s/n): ";
std::cin >> d;
std::cout << std::endl;
}while(d != 's' && d != 'n');
if(d == 's')
Principal::debug = true;
}
void Principal::elegirFichero() {
fstream f;
string ruta;
do {
std::cout << "Nombre del fichero (sin extension): " << std::flush;
getline(std::cin >> std::ws, fichero);
std::cout << std::endl;
ruta = "./DAT/" + fichero + ".dat";
f.open ( ruta.c_str() , std::ios::in);
if(!f.is_open())
cout << "No existe el fichero: " << fichero << ".dat" << endl;
} while(!f.is_open());
f.close();
}
void Principal::elegirAlgoritmo() {
unsigned short opcion;
do {
cout << "Seleccione algoritmo: " << endl;
cout << "1.Greedy" << endl
<< "2.BL" << endl
<< "3.BT" << endl;
cout << "Opcion: ";
cin >> opcion;
cout << endl;
} while (opcion < 1 || opcion > 3);
switch(opcion) {
case 1:
tipo = ALG_Greedy;
break;
case 2:
tipo = ALG_BL;
break;
case 3:
tipo = ALG_BT;
break;
}
}
void Principal::elegirSemilla() {
std::cout << std::endl << "Introduzca la semilla: ";
std::cin >> semilla;
std::cout << std::endl;
srand(semilla);
}
void Principal::construirAlgoritmo() {
if(metaheuristica)
delete metaheuristica;
//Instanciación de la metaheurística
switch(tipo) {
case ALG_Greedy:
metaheuristica = new Greedy("./DAT/" + fichero + ".dat");
break;
case ALG_BL:
metaheuristica = new BL("./DAT/" + fichero + ".dat");
break;
case ALG_BT:
Config_BT config_bt;
bool vecinos_prop;
bool reinicios_prop;
bool tabuActivo_prop;
configurarBT(config_bt, vecinos_prop, reinicios_prop, tabuActivo_prop);
metaheuristica = new BT("./DAT/" + fichero + ".dat",
config_bt, vecinos_prop, reinicios_prop,
tabuActivo_prop);
break;
}
}
void Principal::configurarBT(Config_BT& config, bool& vecinos_prop,
bool& reinicios_prop, bool& tabuActivo_prop)
{
char v;
int opcion = 0;
do {
cout << "Seleccione opcion:" << endl;
cout << "1.Usar configuracion estandar:" << endl;
cout << " Max. Evaluaciones: 10000" << endl;
cout << " Vecinos: 30" << endl;
cout << " Reinicio en: 10 sin mejorar" << endl;
cout << " Tabu activo: proporcional a tam" << endl;
cout << endl;
cout << "2.Usar configuracion recomendada:" << endl;
cout << " Max. Evaluaciones: 30000" << endl;
cout << " Vecinos: proporcional a tam" << endl;
cout << " Reinicio en: proporcional a tam" << endl;
cout << " Tabu activo: proporcional a tam" << endl;
cout << endl;
cout << "3.Usar configuracion personalizada:" << endl;
cout << endl;
cout << "Opcion: ";
cin >> opcion;
cout << endl;
} while (opcion < 1 || opcion > 3);
switch(opcion) {
case 1:
{
config.max_evaluaciones = 10000;
config.vecinos = 30;
config.reinicio = 10;
vecinos_prop = false;
reinicios_prop = false;
tabuActivo_prop = true;
break;
}
case 2:
{
config.max_evaluaciones = 30000;
vecinos_prop = true;
reinicios_prop = true;
tabuActivo_prop = true;
break;
}
case 3:
{
config.max_evaluaciones = 0;
do {
cout << "Maximo de evaluaciones (minimo 10000): ";
cin >> config.max_evaluaciones;
cout << endl;
} while (config.max_evaluaciones < 10000);
do {
cout << "Generacion de vecinos proporcional a tam (s/n): ";
cin >> v;
cout << endl;
} while(v != 's' && v != 'n');
if(v =='s') {
vecinos_prop = true;
} else {
vecinos_prop = false;
config.vecinos = 0;
do{
cout << "Numero de vecinos (minimo 30): ";
cin >> config.vecinos;
cout << endl;
} while (config.vecinos < 30);
}
do {
cout << "Iteraciones hasta reinicio proporcional a tam (s/n): ";
cin >> v;
cout << endl;
} while(v != 's' && v != 'n');
if(v =='s') {
reinicios_prop = true;
} else {
reinicios_prop = false;
config.reinicio = 0;
do{
cout << "Iteraciones hasta reinicio (minimo 10): ";
cin >> config.reinicio;
cout << endl;
} while (config.reinicio < 10);
}
do {
cout << "Iteraciones que un mov. es tabu proporcional a tam (s/n): ";
cin >> v;
cout << endl;
} while(v != 's' && v != 'n');
if(v =='s') {
tabuActivo_prop = true;
} else {
tabuActivo_prop = false;
config.tabuActivo = 0;
do{
cout << "Iteraciones que un mov es Tabu (minimo 5): ";
cin >> config.tabuActivo;
cout << endl;
} while (config.tabuActivo < 10);
}
break;
}
}
}
void Principal::ejecutarAlgoritmo() {
unsigned long coste;
double tiempo;
Timer timer;
construirAlgoritmo();
timer.start();
coste = metaheuristica->ejecutar();
timer.stop();
tiempo = timer.getElapsedTimeInMilliSec();
cout << "Coste: " << coste << endl;
cout << "Tiempo (ms): " << tiempo << endl;
cout << endl;
cout << "Guardando resultados..." << endl;
guardarResultados(coste, tiempo);
cout << "Resultados almacenados" << endl;
cout << endl;
}
void Principal::guardarResultados(unsigned long costeObtenido, unsigned tiempo) {
fstream f;
string ruta = "./RESULTADOS/" + fichero + ".txt";
f.open(ruta.c_str(), ios::out | ios::app);
f << "Algoritmo: " << tipo_str() << "\r\n";
f << "Semilla: " << semilla << "\r\n";
f << "Coste: " << costeObtenido << "\r\n";
f << "Tiempo (ms): " << tiempo << "\r\n";
f << "\r\n";
f.close();
}
string const Principal::tipo_str() {
switch(tipo) {
case ALG_Greedy:
return "Greedy";
break;
case ALG_BL:
return "BL";
break;
case ALG_BT:
return "BT";
break;
}
}
//MÉTODOS PUBLIC
//==============
void Principal::iniciarMenu() {
unsigned short int opcion;
do {
cout << "//////////////////////////////////////////////////////////////////" << endl;
cout << "///////////////////Metaheuristicas - Practica 1///////////////////" << endl;
cout << "//////////////////////////////////////////////////////////////////" << endl << endl;
cout << "Algoritmo: " << tipo_str() << endl;
cout << "Fichero: " << fichero << ".dat" << endl;
cout << "Semilla: " << semilla << endl << endl;
cout << "¿Que desea hacer?" << endl << endl;
cout << "1. Ejecutar algoritmo" << endl;
cout << "2. Cambiar algoritmo" << endl;
cout << "3. Cambiar fichero" << endl;
cout << "4. Cambiar semilla" << endl;
cout << "5. Cerrar el programa" << endl << endl;
do {
cout << "Opcion: ";
cin >> opcion;
skipline(cin);
cout << endl;
} while (opcion < 1 || opcion > 5);
switch (opcion) {
case 1:
{
ejecutarAlgoritmo();
break;
}
case 2:
{
elegirAlgoritmo();
break;
}
case 3:
{
elegirFichero();
break;
}
case 4:
{
elegirSemilla();
break;
}
case 5:
{
exit(0);
break;
}
}
} while (true);
}