Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def add_node(graph, node):
- """Wstawia wierzchołek do grafu."""
- if node not in graph:
- graph[node] = {}
- def add_edge_directed(graph, edge):
- """Dodaje krawędź do grafu skierowanego."""
- source, target, weight = edge
- add_node(graph, source)
- add_node(graph, target)
- # Możemy wykluczyć pętle.
- if source == target:
- raise ValueError("pętle są zabronione")
- if target not in graph[source]:
- graph[source][target] = weight
- def listnodes(graph):
- """Zwraca listę wierzchołków grafu."""
- return graph.keys()
- def listedges(graph):
- """Zwraca listę krawędzi (krotek) grafu."""
- L = []
- for source in graph:
- for target in graph[source]:
- L.append((source, target, graph[source][target]))
- return L
- def print_graph(graph):
- """Wypisuje postać grafu na ekranie."""
- for i in graph:
- print(i, graph[i])
- def getValues():
- """Pobiera wartości od użytkownika"""
- x = input('wpisz liczbe wierzchołkow: ')
- if not (x.isdigit()):
- raise ValueError('Wpisz cyfry!')
- x = int(x)
- return x
- # main()
- trees = input('podaj ilosc losowanych drzew: ')
- trees = int(trees)
- generation = input('podaj liczbe potomków: ')
- generation = int(generation)
- def generate_trees():
- graph = {}
- x = random.randint(0, 10)
- for i in range(x):
- add_node(graph, i)
- for i in range(x):
- t = (random.randint(0, x), random.randint(0, x), 0)
- if t[0] == t[1]: continue
- add_edge_directed(graph, t)
- return graph
- def crossover(tree1, tree2):
- subtree1 = random.choice(list(tree1.keys()))
- subtree2 = random.choice(list(tree2.keys()))
- values1 = tree1[subtree1].items()
- tree1[subtree1] = tree2[subtree2].items()
- tree2[subtree2] = values1
- return tree1, tree2
- parents = []
- for i in range(trees):
- i = generate_trees()
- parents.append(i)
- print(parents)
- print(crossover(parents[0], parents[1]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement