-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPert-Interactive.py
105 lines (83 loc) · 3.85 KB
/
Pert-Interactive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import math
import pandas as pd
from graphviz import Digraph
def pert(tasks, dependencies):
task_data={}
#Calculate the expected time, variance and standard deviation of each task
for task, times in tasks.items():
O, P, M = times
expected_time = (O + M*4 + P)/6
variance = ((P-O)**2)/36
standard_deviation = math.sqrt(variance)
# Convert to float and round to two
float(expected_time)
float(variance)
float(standard_deviation)
expected_time = round(expected_time, 2)
variance = round(variance, 2)
standard_deviation = round(standard_deviation, 2)
# Add the task data to the currently empty dictionary
task_data[task] = {
"expected_time": float(expected_time),
"variance": float(variance),
"standard_deviation": float(standard_deviation),
"earliest_start": 0,
"latest_start": None,
"earliest_finish": float(expected_time),
"latest_finish": None,
"slack": 0
}
dependencies = {key: [dep] if isinstance(dep, str) else dep for key, dep in dependencies.items()}
#Calculate the earliest start and earliest finish of each task
for _ in range(len(task_data)):
for task in task_data:
if task in dependencies:
earliest_start = max([task_data[dep]["earliest_finish"] for dep in dependencies[task]], default=0)
task_data[task]["earliest_start"] = earliest_start
task_data[task]["earliest_finish"] = earliest_start + task_data[task]["expected_time"]
project_duration = max(task_data[task]["earliest_finish"] for task in task_data)
for task in task_data:
task_data[task]["latest_finish"] = project_duration
# Calculate the latest start, finish and slack of each task
for _ in range(len(task_data)):
for task in task_data:
if task not in dependencies or not dependencies[task]:
continue
for dep in dependencies[task]:
task_data[dep]["latest_finish"] = min(task_data[dep]["latest_finish"], task_data[task]["earliest_start"])
task_data[dep]["latest_start"] = task_data[dep]["latest_finish"] - task_data[dep]["expected_time"]
task_data[dep]["slack"] = task_data[dep]["latest_start"] - task_data[dep]["earliest_start"]
return task_data
def create_pert_graph(task_data):
dot = Digraph()
for task, data in task_data.items():
dot.node(task, label=f"{task}\nExpected time: {data['expected_time']} (SD: {data['standard_deviation']})")
for task, deps in dependencies.items():
for dep in deps:
label = str(task_data[dep]['expected_time'])
dot.edge(dep, task, label=label)
return dot
#Define the tasks
tasks = {}
dependencies = {}
number_of_tasks = int(input("Enter the number of tasks you have: "))
for i in range(number_of_tasks):
task = input("Enter the name of the task(ex: A, B): ")
optimistic_time = float(input("Enter the optimistic time: "))
pessimistic_time = float(input("Enter the pessimistic time: "))
most_likely_time = float(input("Enter the most likely time: "))
tasks[task] = [optimistic_time, pessimistic_time, most_likely_time]
# Define the dependencies of each task
for task in tasks:
number_of_dependencies = int(input(f"Enter the number of dependencies of task {task}: "))
dependencies[task] = []
for i in range(number_of_dependencies):
dependency = input(f"Enter the name of the dependency {i+1}: ")
dependencies[task].append(dependency)
results = pert(tasks, dependencies)
pert_graph = create_pert_graph(results)
pert_graph.render("pert_graph_interactive.png")
# Print the Results
results_df = pd.DataFrame.from_dict(results, orient='index')
print(results_df)
results_df.to_csv('personal_results.csv')