parent
c5ba5a7915
commit
916f11b1d6
@ -1,105 +0,0 @@
|
|||||||
import csv
|
|
||||||
import networkx as nx
|
|
||||||
import matplotlib.pyplot as plt
|
|
||||||
from typing import Dict, Tuple
|
|
||||||
|
|
||||||
pert = nx.DiGraph()
|
|
||||||
|
|
||||||
with open('tasks-sae.csv') as csvfile:
|
|
||||||
tasks = list(csv.reader(csvfile, delimiter=','))
|
|
||||||
for task, duration, predecessors, label, _ in tasks:
|
|
||||||
pert.add_node(task, duration=int(duration))
|
|
||||||
|
|
||||||
for task, duration, predecessors, label, _ in tasks:
|
|
||||||
if predecessors == '':
|
|
||||||
continue
|
|
||||||
for predecessor in predecessors.split('-'):
|
|
||||||
pert.add_edge(predecessor, task)
|
|
||||||
|
|
||||||
layout = nx.nx_pydot.graphviz_layout(pert, prog='dot')
|
|
||||||
nx.draw(pert, pos=layout)
|
|
||||||
nx.draw_networkx_labels(pert, pos=layout)
|
|
||||||
|
|
||||||
labels: Dict[Tuple[str, str], int] = {}
|
|
||||||
for task in pert.nodes:
|
|
||||||
for edge in pert.out_edges(task):
|
|
||||||
labels[edge] = pert.nodes[task]['duration']
|
|
||||||
|
|
||||||
nx.draw_networkx_edge_labels(pert, pos=layout, edge_labels=labels)
|
|
||||||
plt.show()
|
|
||||||
|
|
||||||
sources = [task for task in pert.nodes if pert.in_degree(task) == 0]
|
|
||||||
puits = [task for task in pert.nodes if pert.out_degree(task) == 0]
|
|
||||||
|
|
||||||
asap: Dict[str, str] = dict()
|
|
||||||
for source in sources:
|
|
||||||
asap[source] = pert.nodes[source]['duration']
|
|
||||||
|
|
||||||
frontiere = []
|
|
||||||
for task in sources:
|
|
||||||
frontiere.extend(pert.successors(task))
|
|
||||||
|
|
||||||
while len(frontiere):
|
|
||||||
sucessors = []
|
|
||||||
for task in frontiere:
|
|
||||||
start = 0
|
|
||||||
stop = False
|
|
||||||
for predecessor in pert.predecessors(task):
|
|
||||||
if predecessor not in asap:
|
|
||||||
stop = True
|
|
||||||
break
|
|
||||||
start = max(start, asap[predecessor])
|
|
||||||
if stop:
|
|
||||||
continue
|
|
||||||
asap[task] = start + pert.nodes[task]['duration']
|
|
||||||
sucessors.extend(pert.successors(task))
|
|
||||||
frontiere = sucessors
|
|
||||||
|
|
||||||
total_duration = 0
|
|
||||||
for task in pert.nodes:
|
|
||||||
total_duration = max(asap[task], total_duration)
|
|
||||||
|
|
||||||
alap: Dict[str, str] = dict()
|
|
||||||
for puit in puits:
|
|
||||||
alap[puit] = total_duration - pert.nodes[puit]['duration']
|
|
||||||
|
|
||||||
frontiere = []
|
|
||||||
for task in puits:
|
|
||||||
frontiere.extend(pert.predecessors(task))
|
|
||||||
|
|
||||||
while len(frontiere):
|
|
||||||
predecessors = []
|
|
||||||
for task in frontiere:
|
|
||||||
end = total_duration
|
|
||||||
stop = False
|
|
||||||
for successor in pert.successors(task):
|
|
||||||
if successor not in alap:
|
|
||||||
stop = True
|
|
||||||
break
|
|
||||||
end = min(end, alap[successor])
|
|
||||||
if stop:
|
|
||||||
continue
|
|
||||||
alap[task] = end - pert.nodes[task]['duration']
|
|
||||||
predecessors.extend(pert.predecessors(task))
|
|
||||||
frontiere = predecessors
|
|
||||||
|
|
||||||
print('Durée totale :', total_duration, 'heures')
|
|
||||||
for task in pert.nodes:
|
|
||||||
duration = pert.nodes[task]['duration']
|
|
||||||
print('|', task, '|', duration, '|', '-'.join(pert.predecessors(task)), '|', asap[task] - duration, '|', asap[task], '|', alap[task], '|', alap[task] + duration, '|', alap[task] + duration - asap[task], '|', sep='')
|
|
||||||
|
|
||||||
|
|
||||||
nrow = pert.number_of_nodes()
|
|
||||||
plt.figure()
|
|
||||||
bar_width = 0.9
|
|
||||||
|
|
||||||
tasks = list(pert.nodes)
|
|
||||||
|
|
||||||
for i, task in enumerate(reversed(tasks)):
|
|
||||||
duration = pert.nodes[task]['duration']
|
|
||||||
plt.broken_barh([(asap[task] - duration, duration)], (i - bar_width / 2, bar_width))
|
|
||||||
#plt.broken_barh([(asap[task], alap[task] - asap[task])], (i - bar_width / 2, bar_width), color='green')
|
|
||||||
|
|
||||||
plt.yticks([i for i in range(nrow)], reversed(tasks))
|
|
||||||
|
|
||||||
plt.show()
|
|
@ -1,111 +0,0 @@
|
|||||||
import networkx as nx
|
|
||||||
from random import randint
|
|
||||||
from typing import Dict
|
|
||||||
import matplotlib.pyplot as plt
|
|
||||||
|
|
||||||
pert = nx.DiGraph()
|
|
||||||
"""pert.add_node('A', duration=3)
|
|
||||||
pert.add_node('B', duration=4)
|
|
||||||
pert.add_node('C', duration=8)
|
|
||||||
pert.add_node('D', duration=6)
|
|
||||||
pert.add_node('E', duration=2)
|
|
||||||
pert.add_node('F', duration=10)
|
|
||||||
pert.add_node('G', duration=5)
|
|
||||||
pert.add_node('H', duration=3)
|
|
||||||
pert.add_node('I', duration=2)
|
|
||||||
pert.add_node('J', duration=4)
|
|
||||||
pert.add_node('K', duration=4)
|
|
||||||
pert.add_node('L', duration=4)
|
|
||||||
pert.add_node('M', duration=10)
|
|
||||||
pert.add_edge('A', 'C')
|
|
||||||
pert.add_edge('B', 'D')
|
|
||||||
pert.add_edge('C', 'D')
|
|
||||||
pert.add_edge('D', 'E')
|
|
||||||
pert.add_edge('D', 'F')
|
|
||||||
pert.add_edge('F', 'G')
|
|
||||||
pert.add_edge('D', 'H')
|
|
||||||
pert.add_edge('E', 'I')
|
|
||||||
pert.add_edge('C', 'J')
|
|
||||||
pert.add_edge('B', 'J')
|
|
||||||
pert.add_edge('G', 'K')
|
|
||||||
pert.add_edge('H', 'K')
|
|
||||||
pert.add_edge('I', 'L')
|
|
||||||
pert.add_edge('J', 'L')
|
|
||||||
pert.add_edge('K', 'L')
|
|
||||||
pert.add_edge('L', 'M')"""
|
|
||||||
pert.add_edge('A', 'B')
|
|
||||||
pert.add_edge('B', 'C')
|
|
||||||
pert.add_edge('B', 'D')
|
|
||||||
pert.add_edge('C', 'E')
|
|
||||||
pert.add_edge('D', 'E')
|
|
||||||
pert.add_edge('E', 'I')
|
|
||||||
pert.add_edge('I', 'J')
|
|
||||||
pert.add_edge('J', 'K')
|
|
||||||
pert.add_edge('I', 'G')
|
|
||||||
pert.add_edge('G', 'J')
|
|
||||||
|
|
||||||
for task in pert.nodes:
|
|
||||||
pert.nodes[task]['duration'] = randint(1, 10)
|
|
||||||
|
|
||||||
layout = nx.nx_pydot.graphviz_layout(pert, prog='dot')
|
|
||||||
nx.draw(pert, pos=layout)
|
|
||||||
nx.draw_networkx_labels(pert, pos=layout)
|
|
||||||
|
|
||||||
labels = {}
|
|
||||||
for task in pert.nodes:
|
|
||||||
for edge in pert.out_edges(task):
|
|
||||||
labels[edge] = pert.nodes[task]['duration']
|
|
||||||
|
|
||||||
nx.draw_networkx_edge_labels(pert, pos=layout, edge_labels=labels)
|
|
||||||
plt.show()
|
|
||||||
|
|
||||||
sources = [task for task in pert.nodes if pert.in_degree(task) == 0]
|
|
||||||
puits = [task for task in pert.nodes if pert.out_degree(task) == 0]
|
|
||||||
|
|
||||||
asap: Dict[str, str] = dict()
|
|
||||||
for source in sources:
|
|
||||||
asap[source] = pert.nodes[source]['duration']
|
|
||||||
|
|
||||||
frontiere = []
|
|
||||||
for task in sources:
|
|
||||||
frontiere.extend(pert.successors(task))
|
|
||||||
|
|
||||||
while len(frontiere):
|
|
||||||
sucessors = []
|
|
||||||
for task in frontiere:
|
|
||||||
start = 0
|
|
||||||
for predecessor in pert.predecessors(task):
|
|
||||||
start = max(start, asap[predecessor])
|
|
||||||
asap[task] = start + pert.nodes[task]['duration']
|
|
||||||
sucessors.extend(pert.successors(task))
|
|
||||||
frontiere = sucessors
|
|
||||||
|
|
||||||
m = 0
|
|
||||||
for task in pert.nodes:
|
|
||||||
m = max(asap[task], m)
|
|
||||||
|
|
||||||
alap: Dict[str, str] = dict()
|
|
||||||
for puit in puits:
|
|
||||||
alap[puit] = m
|
|
||||||
|
|
||||||
frontiere = []
|
|
||||||
for task in puits:
|
|
||||||
frontiere.extend(pert.predecessors(task))
|
|
||||||
|
|
||||||
while len(frontiere):
|
|
||||||
predecessors = []
|
|
||||||
for task in frontiere:
|
|
||||||
end = m
|
|
||||||
for successor in pert.successors(task):
|
|
||||||
end = min(end, alap[successor] - pert.nodes[successor]['duration'])
|
|
||||||
alap[task] = end - pert.nodes[task]['duration']
|
|
||||||
predecessors.extend(pert.predecessors(task))
|
|
||||||
frontiere = predecessors
|
|
||||||
|
|
||||||
print(pert)
|
|
||||||
print(asap)
|
|
||||||
print(alap)
|
|
||||||
|
|
||||||
for task in pert.nodes:
|
|
||||||
duration = pert.nodes[task]['duration']
|
|
||||||
print(task, '|', '-'.join(pert.predecessors(task)), '|', asap[task] - duration, asap[task], '|')#, alap[task] - duration, alap[task], '|', alap[task] - asap[task])
|
|
|
|
|
|
Loading…
Reference in new issue