-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.R
More file actions
357 lines (292 loc) · 10.4 KB
/
main.R
File metadata and controls
357 lines (292 loc) · 10.4 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
library(igraph)
library(GGally)
library(network)
library(sna)
library(ggplot2)
library(intergraph)
library(hrbrthemes)
library(plotly)
library(ggplotify)
pert_mean= 0
critical_path = 0
cpm = function(name)
{
#Reading data
data = read.csv(paste0("Data/cpm/", name , ".csv"))
names(data) = c("Activity" ,"Title" ,"Immediate.Predecessor" , "Activity.Time")
#Replacing the "factor" class of columns
data$Immediate.Predecessor = as.character(data$Immediate.Predecessor)
data$Activity = as.character(data$Activity)
#Cleaning Immediate.Predecessor column from multible nodes
for(i in 1:nrow(data))
{
if(substr(data[i, "Immediate.Predecessor"] , 2, 2 )== '-')
{
temp = strsplit(data[i , "Immediate.Predecessor"] ,"-")[[1]]
data[i , "Immediate.Predecessor"] = temp[1]
for(j in 2:length(temp) )
{
r = data[i , ]
r$Immediate.Predecessor = temp[j]
data = rbind(data , r )
}
}
}
#Creating the start and End points ###########################################################################3
start_node = "Start"
start_nodes = data[data$Immediate.Predecessor == "-" , "Immediate.Predecessor"]
r = data[1 , ]
r$Activity = "Start"
r$Title = 'Starting the project'
r$Immediate.Predecessor = "-"
r$Activity.Time = 0
data[data$Immediate.Predecessor=="-","Immediate.Predecessor"] = "Start"
data = rbind(r , data)
end_nodes = unique(data[(! data$Activity %in% unique(data$Immediate.Predecessor) ), "Activity"] )
for(i in end_nodes)
{
r = data[1 , ]
r$Activity = "End"
r$Title = 'Ending the project'
r$Immediate.Predecessor = i
r$Activity.Time = 0
data = rbind(data , r)
}
end_node = "End"
##############################################################################################################
#return parents of a node
parents = function(data , node)
{
p = data[data$Activity ==node , "Immediate.Predecessor"]
p
}
#return children of a node
children = function(data , node)
{
c = data[data$Immediate.Predecessor == node , "Activity"]
c
}
#return levels of the tree from a starting node
levels = function(data , start)
{
queue = c(start)
level = data.frame(Activity = unique(data$Activity) , level = rep(0 , length(unique(data$Activity))))
level$Activity = as.character(level$Activity)
while(length(queue) > 0)
{
node =queue[1]
queue = queue[-1]
c = children(data , node)
for(i in c)
{
level[level$Activity == i ,"level"]= max(level[level$Activity == node , "level"] + 1 , level[level$Activity == i ,"level"])
queue= c(queue , i)
}
}
level
}
#Forward path
bfs = function (data )
{
level = levels(data, "Start")
start = min(level$level)
end = max(level$level)
queue = c(start)
path = vector()
data$visited = rep(FALSE, nrow(data))
data[data$Activity ==start , "visited"] = TRUE
while(length(queue) > 0)
{
node = queue[1]
queue = queue[-1]
path = c(path , level[level$level ==node ,"Activity"] )
c = level[level$level == node +1 , "Activity"]
for(i in c)
{
if(!data[data$Activity == i , "visited"])
{
p = parents(data , i)
mx = max(data[data$Activity %in% p , "EF"])
data[data$Activity ==i , "ES"] = mx
data[data$Activity ==i , "EF"]= mx+ data[data$Activity == i ,"Activity.Time"]
data[data$Activity==i , "visited" ] = TRUE
}
}
if(node<=end)
queue = c(queue, node + 1)
}
l = list(data , path )
l
}
#backward path
backward = function(data , path)
{
#Setting the Backward start node LS & LF
data[data$Activity==end_node , c("LS" , "LF")] = c(data[data$Activity==end_node , c("ES" ,"EF")])
path = rev(path )
path = path[2:length(path)]
for(i in path )
{
p = children(data , i )
mn = min(data[data$Activity %in% p , "LS"])
data[data$Activity == i , "LF" ] = mn
data[data$Activity == i , "LS"] = mn - data[data$Activity == i , "Activity.Time"]
}
data
}
#Creating the nodes (ES , EF , LS , LF , visited status ) columns
data$ES = rep(0 , nrow(data))
data$EF = rep(0 , nrow(data))
data$LS = rep(0 , nrow(data))
data$LF = rep(0 , nrow(data))
#Setting the start node ES & EF
data[data$Activity==start_node, c("ES" , "EF")] = c(0 , 0 )
#Starting a forward bfs path
forward = bfs(data)
data = forward[[1]]
path = forward[[2]]
data = data[ , !names(data) %in% "visited"]
#Starting a backward bfs path
data = backward(data , path )
#dropping the visited status column
data = data[, !names(data) %in% "status"]
#Creating "S" column = LS - ES for each node
data$S = data$LS - data$ES
#Creating a type column for each node to determine the Critical path
data$type = rep("Critical" , nrow(data))
#Setting the type column based on the "S" Column
for(i in 1:nrow(data))
{
if(data[i, "S"] != 0)
data[i, "type"] = "Normal"
}
assign("pert_mean" , unique(data[data$Activity == "End" , "ES"] ) , envir = .GlobalEnv)
assign("critical_path" , unique(data[data$type=="Critical" , "Activity"]) , envir = .GlobalEnv)
#Plotting the graph
df = data.frame(source= data$Immediate.Predecessor , target = data$Activity , type = data$type )
df = df[2:nrow(df) , ]
df$type = data[1:(nrow(data)-1) , "type"]
network <- graph_from_data_frame(d=df, vertices = unique(data$Activity) , directed=T)
col = vector()
labels = vector()
for(i in unique(data[2:nrow(data), "Immediate.Predecessor" ] ) )
{
labels = c(labels , i)
if(data[data$Activity == i, "type"]=="Critical")
col = c(col , "#30d155")
else
col = c(col , "#ffd609")
}
col = c(col , "#30d155")
labels = c(labels , end_node)
r = unique(data[2:nrow(data) , "Immediate.Predecessor"])
r = c(r , end_node)
Time = vector()
ES = vector()
EF = vector()
LS = vector()
LF = vector()
Slack = vector()
for(i in r )
{
indx = which(data$Activity==i )
if(length(indx) >1)
indx = indx[1]
Time = c(Time , data[indx , "Activity.Time"])
ES = c(ES , data[indx , "ES"])
EF = c(EF , data[indx , "EF"])
LS = c(LS , data[indx , "LS"])
LF = c(LF , data[indx , "LF"])
Slack = c(Slack , data[indx , "S"])
}
net = graph.data.frame( df, directed = TRUE)
file_name = paste0("images/", name , "/directed graph.png" )
png(file_name , width = 1366 , height = 768 , units ="px")
p = ggnet2 (net , arrow.size = 12 , arrow.gap = 0.055 , color = col , directed = T )+
# edge.label = data[2:nrow(data) , "S"] ,
# edge.label.fill = "#252a32" ,
# edge.label.color = "white" )+
geom_point(aes(color = col), size = 12, alpha = 0.5) +
geom_point(aes(color = col , Time = Time , ES = ES , EF = EF , LS = LS , LF = LF , Slack = Slack ), size = 9 ) +
geom_text( label = labels , color = "#000000", fontface = "bold" ) +
#theme_ft_rc() +
xlab("")+
ylab("")+
theme(legend.position = "none")
print(p)
dev.off()
if(FALSE)
{
fig = ggplotly(p , tooltip = c("Time" , "ES" , "EF" , "LS" , "LF" , "Slack") )
axis <- list(title = "", showgrid = FALSE, showticklabels = FALSE, zeroline = FALSE)
fig <- fig %>% layout( xaxis = axis , yaxis = axis)
fig
}
t = p$data
fig = ggplotly(p , tooltip = c("Time" , "ES" , "EF" , "LS" , "LF" ,"S"), width = 1366 , height = 768 )
axis <- list(title = "", showgrid = FALSE, showticklabels = FALSE, zeroline = FALSE)
fig <- fig %>% layout( xaxis = axis , yaxis = axis) %>%
add_annotations(x = t$x + 0.02,
y = t$y + 0.02 ,
text = paste("Time : ", Time , "<br>" , "ES : " ,ES , "<br>" , "EF : " , EF , "<br>" , "LS : " , LS , "<br>" , "LF : " , LF , "<br>" , "S : " , Slack ),
xref = "x",
yref = "y",
showarrow = TRUE,
arrowhead = 4,
arrowsize = .5,
ax = 20,
ay = -40
)
print(fig)
#Plot directed tree
layout <- layout.reingold.tilford(net)
layout = layout[, 2:1]
file_name = paste0("images/", name , "/directed tree.png" )
png(file_name , width = 1366 , height = 768 , units ="px")
p = plot(net,
layout = layout, # draw graph as tree
vertex.size = 20, # node size
vertex.color = col, # node color
vertex.label = r , # node labels
vertex.label.cex = 1.2, # node label size
vertex.label.family = "Helvetica", # node label family
vertex.label.font = 2, # node label type (bold)
vertex.label.color = '#000000', # node label size
#edge.label = data[2:nrow(data) , "S"] , # edge label
edge.label.cex = .7, # edge label size
edge.label.family = "Helvetica", # edge label family
edge.label.font = 2, # edge label font type (bold)
edge.label.color = '#000000', # edge label color
edge.arrow.size = 1.2, # arrow size
edge.arrow.width = 1.2 # arrow width
)
p
dev.off()
}
pert = function(name)
{
data = read.csv(paste0("Data/pert/" , name , ".csv"))
names(data)= c("Activity" , "Immediate.Predecessor" , "a","m","b")
data2 = data
data2$Title = rep("test",nrow(data))
data2$Activity.Time = rep(0,nrow(data))
data2$Activity.Time =( data$a + data$m * 4 + data$b ) / 6
data2 = data2[,names(data)%in% c("Activity", "Title", "Immediate.Predecessor", "Activity.Time") ]
data2 = data2[,c("Activity" , "Title", "Immediate.Predecessor" , "Activity.Time")]
data2[,1] = as.character(data2[,1])
data2[,2] = as.character(data2[,2])
data2[,3] = as.character(data2[,3])
data2[,4] = as.character(data2[,4])
write.csv(data2 ,"Data/cpm/test7.csv", row.names = FALSE)
cpm("test7")
data$variance = ((data$b-data$a)^2) / 36
print(data)
v = sum(data[data$Activity %in% critical_path , "variance"] )
std = sqrt(v )
End = pert_mean
p = dnorm(26, mean= End, sd = std) * 100
print(v)
answers = data.frame(Task = c("Variance" , "Standard Deviation" , "Propability on 26 day") , ans = c(v , std ,p))
print(answers)
}
pert("tset1")