Advertisement
Guest User

Untitled

a guest
May 25th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.50 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. using namespace std;
  7.  
  8. struct NODO
  9. {
  10. int *X;
  11. int k;
  12. float cota;
  13. };
  14. void quicksort(int *V, int izq, int der)
  15. {
  16. int i, d, p;
  17. int pivote;
  18. int aux;
  19. p=(izq+der)/2;
  20. if(p>0)
  21. {
  22. pivote=V[p];
  23. i=izq;
  24. d=der;
  25. while(i<=d)
  26. {
  27. while(V[i]<pivote)
  28. {
  29. i++;
  30. }
  31. while(V[d]>pivote)
  32. {
  33. d--;
  34. }
  35. if(i<=d)
  36. {
  37. aux=V[i];
  38. V[i]=V[d];
  39. V[d]=aux;
  40. i++;
  41. d--;
  42. }
  43. }
  44. if(izq<d)
  45. {
  46. quicksort(V, izq, d);
  47. }
  48. if(i<der)
  49. {
  50. quicksort(V, i, der);
  51. }
  52. }
  53. }
  54. int tecnicos()
  55. {
  56. int tecnicos;
  57. cout<<"Introduzca el numero de tecnicos: "<<endl;
  58. cin>>tecnicos;
  59. return tecnicos;
  60. }
  61. int leer(int &numRep, int* &tiempos)
  62. {
  63. char *linea;
  64. int i=1;
  65. cout<<"Introduce el nombre del fichero .txt"<<endl;
  66. char nombre[100];
  67. cin>>nombre;
  68. strcat(nombre, ".txt");
  69. ifstream f;
  70. f.open(nombre);
  71. if (f.is_open()==0)
  72. {
  73. cout<<"No se encontro el archivo"<<endl;
  74. return -1;
  75. }
  76. f.getline(linea,999);
  77. numRep=atoi(linea);
  78. tiempos=(int*)malloc((numRep+1) * sizeof(int));
  79. while(f.eof()==0)
  80. {
  81. for(i=1;i<=numRep;i++)
  82. {
  83. f.getline(linea,999);
  84. tiempos[i]=atoi(linea);
  85. }
  86. }
  87. f.close();
  88. }
  89. int CalcularCota(NODO nodo, int *V, int numTec, int numRep)
  90. {
  91. cout<<"k"<<nodo.k<<"k";
  92. int i, j, k, sumaTec, sumatotal;
  93. int cota;
  94. int *veces;
  95. sumatotal=0;
  96. veces=new int[numTec+1];
  97. for(i=1;i<=numTec;i++)
  98. {
  99. veces[i]=0;
  100. }
  101. for(i=1;i<=numTec;i++)
  102. {
  103. for(j=1;j<=nodo.k;j++)
  104. {
  105. if (nodo.X[j]==i)
  106. {
  107. veces[i]++;
  108. }
  109. }
  110. }
  111. int *T;
  112. for(i=1;i<=numTec;i++)
  113. {
  114. k=0;
  115. sumaTec=0;
  116. T=new int[veces[i]+1];
  117. for(j=1;j<=veces[i];j++)
  118. {
  119. T[i]=0;
  120. }
  121. for(j=1;j<=nodo.k;j++)
  122. {
  123. if (nodo.X[j]==i)
  124. {
  125. k++;
  126. if(k==1)
  127. {
  128. T[k]=V[j];
  129. }
  130. else
  131. {
  132. T[k]=T[k-1]+V[j];
  133. }
  134. }
  135. }
  136. for(j=1;j<=veces[i];j++)
  137. {
  138. if(j<=nodo.k)
  139. {
  140. sumatotal+=T[j];
  141. }
  142. }
  143. }
  144. cota=sumatotal/numTec;
  145. cout<<"calculamos cota"<<cota<<"cota"<<endl;
  146. return cota;
  147. }
  148. NODO NodoInicial(int *V, int numRep, int numTec, int &numnodos)
  149. {
  150. NODO nodo;
  151. int *X;
  152. X=new int[numRep+1];
  153. nodo.X=X;
  154. nodo.k=0;
  155. nodo.cota=CalcularCota(nodo, V, numTec, numRep);
  156. numnodos++;
  157. cout<<endl<<"inicial"<<numnodos<<"nodos";
  158. return nodo;
  159. }
  160. NODO Seleccionar(NODO *lista, int &numnodos, int *V, int numTec, int numRep)
  161. {
  162. int i;
  163. int j=1;
  164. int posicion=1;
  165. int minimo=CalcularCota(lista[1], V, numTec, numRep);
  166. NODO *aux;
  167. NODO nodoseleccionado;
  168. aux=new NODO[numnodos+1];
  169. for(i=2;i<=numnodos;i++)
  170. {
  171. if(CalcularCota(lista[i], V, numTec, numRep)<minimo)
  172. {
  173. posicion=1;
  174. }
  175. }
  176. for(i=1;i<=numnodos;i++)
  177. {
  178. aux[i]=lista[i];
  179. }
  180. lista=new NODO[numnodos];
  181. for(i=1;i<numnodos;i++)
  182. {
  183. if(i==posicion)
  184. {
  185. nodoseleccionado=aux[j];
  186. j++;
  187. }
  188. lista[i]=aux[j];
  189. j++;
  190. }
  191. numnodos--;
  192. cout<<endl<<"seleccionar"<<numnodos<<"nodos left";
  193. cout<<endl<<"seleccionamos nodo"<<nodoseleccionado.cota;
  194. return nodoseleccionado;
  195.  
  196. }
  197. int Expandir(NODO nodo, NODO *hijos, int *V, int numTec, int numRep)
  198. {
  199. cout<<endl<<"comienza expandir";
  200. int numhijos, xi;
  201. int i;
  202. NODO nodohijo;
  203. numhijos=0;
  204. for(xi=1;xi<=numTec;xi++)
  205. {
  206.  
  207. nodohijo.X=nodo.X;
  208. nodohijo.k=nodo.k+1;
  209. nodohijo.X[nodohijo.k]=xi;
  210. for(i=1;i<=nodohijo.k;i++)
  211. {
  212. cout<<"vector"<<nodohijo.X[i]<<"vector";
  213. }
  214. nodohijo.cota=CalcularCota(nodohijo, V, numTec, numRep);
  215. numhijos++;
  216. hijos[numhijos]=nodohijo;
  217. }
  218. cout<<endl<<"fin expandir";
  219. return numhijos;
  220. }
  221. int Admisible(NODO nodo, int valorsolucion)
  222. {
  223. cout<<endl<<"comprueba admisible";
  224. if(nodo.cota<valorsolucion)
  225. {
  226. return 1;
  227. }
  228. else return 0;
  229. }
  230. int Solucion(NODO nodo, int numRep)
  231.  
  232. {
  233. cout<<endl<<"comprueba solucion";
  234. if(nodo.k==numRep)
  235. {
  236. return 1;
  237. }
  238. else return 0;
  239. }
  240. NODO maria(int numTec, int numRep, int* tiempos)
  241. {
  242. int i, j;
  243. int *V;
  244. int solucionoptima;
  245. int numnodos=0;
  246. NODO nodo;
  247. NODO *lista;
  248. NODO nodosolucion;
  249. NODO hijos[numTec+1];
  250. int totalhijos;
  251. V=new int[numRep+1];
  252. for(i=1;i<=numRep;i++)
  253. {
  254. V[i]=tiempos[i];
  255. }
  256. quicksort(V, 1, numRep);
  257. nodo=NodoInicial(V, numRep, numTec, numnodos);
  258. lista=new NODO[numnodos+1];
  259. lista[numnodos]=nodo;
  260. solucionoptima=2147483647;
  261. cout<<endl<<"DECLARACION DE VARIABLES TERMINADA"<<endl;
  262. while(numnodos>0)
  263. {
  264. nodo=Seleccionar(lista, numnodos, V, numTec, numRep);
  265. //AQUI HAY QUE HACER UNA FUNCION QUE COMPARE TODOS LOS NODOS DE LA LISTA Y ELIJA EL DE MENOR COTA, EXTRAYENDOLO DE LA LISTA
  266. if(CalcularCota(nodo, V, numTec, numRep)<solucionoptima)
  267. {
  268. totalhijos=Expandir(nodo, hijos, V, numTec, numRep);
  269. for(i=1;i<=totalhijos;i++)
  270. {
  271. cout<<endl<<"EJECUTAMOS EL FOR"<<endl;
  272. if(Admisible(hijos[i], solucionoptima))
  273. {
  274. if(Solucion(hijos[i], numRep))
  275. {
  276. cout<<endl<<"POSIBLE SOLUCION"<<endl;
  277. if(hijos[i].cota<solucionoptima)
  278. {
  279. cout<<endl<<"SOLUCION ENCONTRADA"<<endl;
  280. nodosolucion=hijos[i];
  281. solucionoptima=hijos[i].cota;
  282. }
  283. }
  284. else
  285. {
  286. NODO *aux;
  287. aux=new NODO[numnodos+1];
  288. for(j=1;j<=numnodos;j++)
  289. {
  290. aux[j]=lista[j];
  291. }
  292. lista=new NODO[numnodos+2];
  293. for(j=1;j<numnodos;j++)
  294. {
  295. lista[i]=aux[j];
  296. }
  297. lista[j]=hijos[j];
  298. numnodos++;
  299. cout<<endl<<"numnodos aumenta ahora es"<<numnodos<<endl;
  300. }
  301. cout<<endl<<"numnodos es"<<numnodos<<endl;
  302. }
  303. }
  304. }
  305. //AQUI COMPROBAR SI LA COTA DEL NODO ES MAS OPTIMA QUE VALORSOLUCION
  306. //SI SE CUMPLE, EJECUTAMOS EXPANDIR
  307. //BUCLE FOR QUE SE EJECUTA TANTAS VECES COMO HIJOS HAY
  308. //COMPROBAR SI EL HIJO ES MAS OPTIMO QUE LA COTA
  309. //SI LO ES COMPROBAR SI ES SOLUCION (SI ESTA EL VECTOR LLENO)
  310. //SI LO ES, COMPARAR SU VALOR CON LA COTA Y CAMBIAR LA COTA SI ES MAS OPTIMO
  311. //SI NO ES SOLUCION, ANYADIRLO A LA LISTA DE NODOS VIVOSN
  312.  
  313.  
  314. }
  315. return nodosolucion;
  316. }
  317. int main()
  318. {
  319. int numTec;
  320. int numRep;
  321. int* tiempos;
  322. string comando;
  323. NODO nodofinal;
  324. while(1)
  325. {
  326. cout<<"1.- Obtener listado de reparaciones."<<endl<<"2.- Numero de tecnicos de la empresa."<<endl<<"3.- Calcular distribucion."<<endl<<"4.- Salir."<<endl;
  327. cin>>comando;
  328. if(comando.compare("1")==0)
  329. {
  330. int i=1;
  331. if (leer(numRep, tiempos)==-1)
  332. {
  333. continue;
  334. }
  335. cout<<numRep<<endl;
  336. for(i=1;i<=numRep;i++)
  337. {
  338. cout<<tiempos[i]<<endl;
  339. }
  340. }
  341. else if(comando.compare("2")==0)
  342. {
  343. numTec=tecnicos();
  344. }
  345. else if(comando.compare("3")==0)
  346. {
  347. nodofinal=maria(numTec, numRep, tiempos);
  348. cout<<"RESULTADOFINAL"<<nodofinal.cota<<endl;
  349. }
  350. else if(comando.compare("4")==0)
  351. {
  352. return 0;
  353. }
  354. }
  355. system("pause");
  356. return 0;
  357. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement