# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/ShirleyLiu11/google-cloud-iot-arduino/blob/master/CPM_PERT.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="fAv73Tjy6V3i" colab={"base_uri": "https://localhost:8080/", "height": 799} outputId="d613985c-3a64-4340-891a-27f76336f051"
import pandas as pd
import tensorflow_probability as tfp
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt

PERTCPM=pd.read_csv("/content/drive/MyDrive/Colab Notebooks/PERT-CPM.csv")
print(PERTCPM)
print('--------------------\n')

taskNames=PERTCPM['tasks']
resultDataFrame=pd.DataFrame(columns=taskNames)

paths=['ADEJ', 'BCDEJ', 'BCFGJ', 'BCFHJ', 'BI']
countOfCriticalsOfEachPath = [0, 0, 0, 0, 0]

tfd = tfp.distributions

for index, task in PERTCPM.iterrows():
  dist=tfd.PERT(low=task['ai '], peak=task['mi'], high=task['bi'], temperature=4)
  resultDataFrame[task['tasks']]=dist.sample(1000)
  

overAllCriticalLength=0

#loop each path based on 1000 simples
for indexOfSimple, simple in resultDataFrame.iterrows():
  
  criticalLength=0
  criticalIndex = -1  #the index of the critical path within paths

  for indexOfPath in range(len(paths)):
    path = paths[indexOfPath]
    length = 0
    

    for indexOfTask in range(len(path)):
      task = path[indexOfTask]
      length += simple[task]
    
    if(length > criticalLength): 
      criticalLength = length
      criticalIndex = indexOfPath

    countOfCriticalsOfEachPath[criticalIndex] += 1

  if(criticalLength > overAllCriticalLength): 
    overAllCriticalLength = criticalLength
# declares the longest path as critical
print("Critical Path: ")
print(paths[criticalIndex])
print("Critical Path Length: ")
print(overAllCriticalLength)
# path critical frequency
allPathFrequency=[]
for index in countOfCriticalsOfEachPath:
  frequency=round((index/5000*100), 4)
  allPathFrequency+=[frequency]


# create chart
objects = paths
y_pos = np.arange(len(objects))
performance = allPathFrequency

plt.bar(y_pos, performance, align='center', alpha=0.5)
plt.xticks(y_pos, objects)
plt.ylabel('Frequency(%)')
plt.title('Path Probabilities')
print("The times each path as critical path: ")
print(countOfCriticalsOfEachPath)

plt.show()








# + [markdown] id="FwVimyAYpOUR"
#

# + [markdown] id="Y7ImyRlWoz5T"
# # New Section

StarcoderdataJupyterScriptsDedupFiltered