-
Notifications
You must be signed in to change notification settings - Fork 1
/
newspaper and manufacturing problem hw6
100 lines (86 loc) · 2.9 KB
/
newspaper and manufacturing problem hw6
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
Code:
M = matrix(c(0.7,0.1,0.1,0.1,0.1,0.7,0.1,0.1,0.5,0.5,0,0,0.5,0.5,0,0),nrow=4,byrow=TRUE)
Machine = new("markovchain",transitionMatrix=M,states=c("DConly","CNonly","Both","None"),name="Machine Process")
steadyStates(Machine)
rPath = function(){
x = character(13)
x[1]= "None"
for (i in 2:13){
x[i] = sample(c("DConly","CNonly","Both","None"), size= 1, prob = Machine[x[i-1],])
}
return(x)
}
plot(Machine)
CreateProcess = function(p){
M = matrix(c(0.7,0.1,0.1,0.1,0.1,0.7,0.1,0.1,p,(1-p),0,0,p,(1-p),0,0),nrow=4,byrow=TRUE)
return(new("markovchain",transitionMatrix = M, states=c("DConly","CNonly","Both","None"),name="Machine Process"))
}
rPathRevenue = function(n,r){
x = character(n+1)
x[1]= "None"
Revenue=c(r,r,2*r,0)
names(Revenue) = c("DConly","CNonly","Both","None") #Label the cost vector
y = 0
for (i in 2:n+1){
x[i] = sample(c("DConly","CNonly","Both","None"), size= 1, prob = Machine[x[i-1],])
y = y + Revenue[x[i]]
}
return(y)
}
ui = fluidPage(
sliderInput("r","Enter r", min = 0.1, max = 5, value = 1, step =0.01 ),
sliderInput("p","Enter p", min = 0.01, max = 0.99,value=0.5, step = 0.01),
sliderInput("n","Enter number of weeks", min = 2,value=4, max = 100),
plotOutput("BarChart"),
textOutput("AR"),
textOutput("SDR")
)
server = function(input, output){
Y = reactive({
CreateProcess(input$p)
})
X = reactive({
replicate(1000, rPathRevenue(input$n,input$r))
})
output$BarChart = renderPlot({barplot(steadyStates(Y()))})
output$AR=renderText({paste("The average revenue per customer", sum(X())/1000)})
output$SDR=renderText({paste("The standard deviation of revenue per customer", sd(X()))})
}
shinyApp(ui,server)
Problem 2:
p1=dpois(0,lambda = 2)
p2=dpois(1,lambda = 2)
p3=dpois(2,lambda = 2)
p4=1-p1-p2-p3
M = matrix(c(p1,p2,p3,p4,p1,p2,p3,p4,0,p1,p2,1-p1-p2,0,0,p1,1-p1),nrow=4,byrow=TRUE)
Machine = new("markovchain",transitionMatrix=M,states=c("0","1","2","3"),name="Machine Process")
steadyStates(Machine)
plot(Machine)
rPathRevenue = function(n,C,M,Xt){
x = character(n+1)
x[1]= "0"
Revenue=c(0,M,2*M,2*M-C*Xt)
names(Revenue) = c("0","1","2","3")
y = 0
for (i in 2:n+1){
x[i] = sample(c("0","1","2","3"), size= 1, prob = Machine[x[i-1],])
y = y + Revenue[x[i]]
}
return(y)
}
ui = fluidPage(
sliderInput("C","Enter C", min = 1, max = 500, value = 10, step =1 ),
sliderInput("M","Enter M", min = 1, max = 500,value=10, step = 1),
sliderInput("n","Enter number of weeks", min = 2,value=4, max = 100),
sliderInput("Xt","Enter Xt", min = 0, max = 10,value=0, step = 1),
textOutput("AR"),
textOutput("SDR")
)
server = function(input, output){
X = reactive({
replicate(1000, rPathRevenue(input$n,input$C,input$M,input$Xt))
})
output$AR=renderText({paste("The average revenue per day", sum(X())/1000)})
output$SDR=renderText({paste("The standard deviation of revenue per day", sd(X()))})
}
shinyApp(ui,server)