Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. from itertools import chain, combinations
  2. import time
  3.  
  4. conjuntoEjercicioUno = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
  5. conjuntoEjercicioDos = {0, 1, 2}
  6.  
  7. def crearSubconjuntos(iterable):
  8. """Crea todos los subconjuntos posibles a partir de un conjunto de elementos."""
  9. s = list(iterable)
  10. return chain.from_iterable(combinations(s,r) for r in range(len(s)+1))
  11.  
  12. class elemento:
  13. """Objeto elemento (objetos a introducirse en la mochila)."""
  14.  
  15. def __init__(self, cod, vol, val):
  16. self.codigo = cod
  17. self.volumen = vol
  18. self.valor = val
  19. self.rvv = val/vol
  20.  
  21. def getCod(self):
  22. return self.codigo
  23.  
  24. def getVol(self):
  25. return self.volumen
  26.  
  27. def getVal(self):
  28. return self.valor
  29.  
  30. def getRVV(self):
  31. return self.rvv
  32.  
  33. def mostrarObjeto(self):
  34. print("ID: ", self.codigo, ", Volumen: ", self.volumen, ", Valor: ", self.valor)
  35.  
  36. def iniciarElementosUno():
  37. """Valores de los elementos del ejercicio apartado 1."""
  38. elementos = []
  39. obj1 = elemento('1', 150, 20)
  40. obj2 = elemento('2', 325, 40)
  41. obj3 = elemento('3', 600, 50)
  42. obj4 = elemento('4', 805, 36)
  43. obj5 = elemento('5', 430, 25)
  44. obj6 = elemento('6', 1200, 64)
  45. obj7 = elemento('7', 770, 54)
  46. obj8 = elemento('8', 60, 18)
  47. obj9 = elemento('9', 930, 46)
  48. obj10 = elemento('10', 353, 28)
  49. elementos.extend((obj1, obj2, obj3, obj4, obj5, obj6, obj7, obj8, obj9, obj10))
  50. return elementos
  51.  
  52. def iniciarElementosDos():
  53. """Valores de los elementos del ejercicio apartado 1."""
  54. elementos = []
  55. obj1 = elemento('1', 1800, 72)
  56. obj2 = elemento('2', 600, 36)
  57. obj3 = elemento('3', 1200, 60)
  58. elementos.extend((obj1, obj2, obj3))
  59. return elementos
  60.  
  61. def busExhaustiva(conjunto, volMochila):
  62. """Ejecición y muestra de resultados de la búsqueda exhaustiva."""
  63. start = time.time()
  64. subconjuntos = crearSubconjuntos(conjunto)
  65. subOrd = sorted(subconjuntos)
  66. if conjunto == conjuntoEjercicioUno:
  67. elementos = iniciarElementosUno()
  68. else:
  69. elementos = iniciarElementosDos()
  70. valor = 0
  71. indiceMayor = int
  72. for x in range(0, len(subOrd)):
  73. volTotal = 0
  74. valTotal = 0
  75. for y in range (0, len(subOrd[x])):
  76. volTotal += elemento.getVol(elementos[subOrd[x][y]])
  77. valTotal += elemento.getVal(elementos[subOrd[x][y]])
  78. if volTotal <= volMochila and valTotal > valor:
  79. valor = valTotal
  80. indiceMayor = x
  81. valorTotal = 0
  82. volumenTotal = 0
  83. for i in range(0, (len(subOrd[indiceMayor]))):
  84. elemento.mostrarObjeto(elementos[subOrd[indiceMayor][i]])
  85. valorTotal += elemento.getVal(elementos[subOrd[indiceMayor][i]])
  86. volumenTotal += elemento.getVol(elementos[subOrd[indiceMayor][i]])
  87. end = time.time()
  88. print('Valor: ', valorTotal)
  89. print('Volumen: ', volumenTotal)
  90. print('Tiempo total de ejecucion:', end - start, 'segundos.')
  91. return
  92.  
  93. def busHeuristica(conjunto, volMochila):
  94. """Ejecución y muestra de los resultados de la búsqueda heurística (greedy)."""
  95. start = time.time()
  96. if conjunto == conjuntoEjercicioUno:
  97. elementos = iniciarElementosUno()
  98. else:
  99. elementos = iniciarElementosDos()
  100. elemOrd = sorted(elementos, key=lambda elemento: elemento.rvv, reverse=True)
  101. elemMochila = []
  102. valor = 0
  103. volumen = 0
  104. for x in range (0, len(elemOrd)):
  105. if (volumen + elemento.getVol(elemOrd[x]) <= volMochila):
  106. volumen += elemento.getVol(elemOrd[x])
  107. elemMochila.append(elemOrd[x])
  108.  
  109. for y in range (0, len(elemMochila)):
  110. elemento.mostrarObjeto(elemMochila[y])
  111. valor += elemento.getVal(elemMochila[y])
  112. end = time.time()
  113. print('Volumen: ', volumen)
  114. print('Valor: ', valor)
  115. print('Tiempo total de ejecucion:', end - start, 'segundos.')
  116. return
  117.  
  118. def main():
  119. """Menú de opciones del ejercicio."""
  120. op = 5
  121. while op != 0:
  122. print('MENU: \n'
  123. '1.- Exhaustivo (1A)\n'
  124. '2.- Heuristico (1A)\n'
  125. '3.- Exhaustivo variante (2A)\n'
  126. '4.- Exhaustivo variante (2B)\n'
  127. '0.- SALIR \n')
  128. op = int(input('Seleccione una opción: '))
  129. print('\n')
  130. if op == 1:
  131. busExhaustiva(conjuntoEjercicioUno, 4200)
  132. if op == 2:
  133. busHeuristica(conjuntoEjercicioUno, 4200)
  134. if op == 3:
  135. busExhaustiva(conjuntoEjercicioDos, 3000)
  136. if op == 4:
  137. busHeuristica(conjuntoEjercicioDos, 3000)
  138. if op ==0:
  139. break
  140. print('\n')
  141. input("Presione enter para continuar")
  142. print('\n \n')
  143. return ()
  144.  
  145. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement