Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. import random
  2.  
  3.  
  4. def add_node(graph, node):
  5. """Wstawia wierzchołek do grafu."""
  6. if node not in graph:
  7. graph[node] = {}
  8.  
  9.  
  10. def add_edge_directed(graph, edge):
  11. """Dodaje krawędź do grafu skierowanego."""
  12. source, target, weight = edge
  13. add_node(graph, source)
  14. add_node(graph, target)
  15. # Możemy wykluczyć pętle.
  16. if source == target:
  17. raise ValueError("pętle są zabronione")
  18. if target not in graph[source]:
  19. graph[source][target] = weight
  20.  
  21.  
  22.  
  23.  
  24. def listnodes(graph):
  25. """Zwraca listę wierzchołków grafu."""
  26. return graph.keys()
  27.  
  28.  
  29. def listedges(graph):
  30. """Zwraca listę krawędzi (krotek) grafu."""
  31. L = []
  32. for source in graph:
  33. for target in graph[source]:
  34. L.append((source, target, graph[source][target]))
  35. return L
  36.  
  37.  
  38. def print_graph(graph):
  39. """Wypisuje postać grafu na ekranie."""
  40. for i in graph:
  41. print(i, graph[i])
  42.  
  43.  
  44. def getValues():
  45. """Pobiera wartości od użytkownika"""
  46. x = input('wpisz liczbe wierzchołkow: ')
  47.  
  48. if not (x.isdigit()):
  49. raise ValueError('Wpisz cyfry!')
  50. x = int(x)
  51.  
  52. return x
  53.  
  54. # main()
  55. trees = input('podaj ilosc losowanych drzew: ')
  56. trees = int(trees)
  57.  
  58. generation = input('podaj liczbe potomków: ')
  59. generation = int(generation)
  60.  
  61.  
  62. def generate_trees():
  63. graph = {}
  64.  
  65. x = random.randint(0, 10)
  66.  
  67. for i in range(x):
  68. add_node(graph, i)
  69. for i in range(x):
  70. t = (random.randint(0, x), random.randint(0, x), 0)
  71. if t[0] == t[1]: continue
  72. add_edge_directed(graph, t)
  73. return graph
  74.  
  75. def crossover(tree1, tree2):
  76. subtree1 = random.choice(list(tree1.keys()))
  77. subtree2 = random.choice(list(tree2.keys()))
  78.  
  79. values1 = tree1[subtree1].items()
  80. tree1[subtree1] = tree2[subtree2].items()
  81. tree2[subtree2] = values1
  82. return tree1, tree2
  83.  
  84. parents = []
  85. for i in range(trees):
  86. i = generate_trees()
  87. parents.append(i)
  88.  
  89.  
  90. print(parents)
  91.  
  92. print(crossover(parents[0], parents[1]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement