-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript_RiskEvalPlat_v01.m
More file actions
1694 lines (1345 loc) · 61.5 KB
/
Script_RiskEvalPlat_v01.m
File metadata and controls
1694 lines (1345 loc) · 61.5 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%% Manufacturing Simulator Input Script, V1
% Varying Machine Fail Times
%
% Instructions
% ------------
%
% Testing different machine fail times, dependent on underlying
% fail time probability distributions
%
% Parts 1-3 go through both Manufacturing Simulators & Visualizations
% Part 2 has 10 Steps
% Parts 4-7 go through risk evaluation of each algorithm
%
% This script calls on the following functions:
% FunMfgSimulator_v02.m (which in turn calls on FunMeanFilt_v02.m)
% FunMakePlots_v02.m
% randfixedsum.m
%% ============== Part 1: Initialization & Machining Paths ================
% Step 1: Initialization
%clearvars -except Experiment;
clear all; clc; close all
NumSims=20;
% Simulation/operation duration is 45 minutes (2700 seconds)
NumParts3=2700;
% Shape parameter, beta, unitless:
zwblB=1.3;
% Shape parameter, beta, unitless:
xwblB=4;
CMSOngoingCosts=20000/60;
CMSOneTimeCosts=0;
% Step 2: Defining the machining paths
% We start by defining the setup of the manufacturing system
%Machines that perform Class A: Drilling operations
ClassAOpMach = [1 2 3];
%Machines that perform Class B: Boring operations
ClassBOpMach = [4 5 6 7 8];
%Machines that perform Class C: Laser Engraving operations
ClassCOpMach = [9 10];
permA=perms(ClassAOpMach); permA=permA(:,1); permA=unique(permA,'rows');
permB=perms(ClassBOpMach); permB=permB(:,1:2); permB=unique(permB,'rows');
permC=perms(ClassCOpMach); permC=permC(:,1); permC=unique(permC,'rows');
indMatLen=length(permA)*length(permB)*length(permC);
indMat=ones(indMatLen,3).*repmat((1:indMatLen)',1,3);
indMat(:,1)=(rem((indMat(:,1)-1),length(permA)))+1;
indMat(:,2)=(rem((indMat(:,2)-1-indMat(:,1)+1)/length(permA),length(permB)))+1;
mMat=(indMat(:,3)-1-indMat(:,1)+1)/length(permA);
indMat(:,3)=(rem((mMat-indMat(:,2)+1)/length(permB),length(permC)))+1;
permAmat=permA(indMat(:,1));
permBmat=permB(indMat(:,2),:);
permCmat=permC(indMat(:,3));
% Due to the order of machines in a path:
AllPossiblePaths=[permAmat permBmat(:,1) permCmat permBmat(:,2)];
%Select the paths considered by simulation:
NumRows=120;
SelRows=randperm(size(AllPossiblePaths,1),NumRows);
SelUniquePaths=AllPossiblePaths(SelRows,:);
% Now we can create a Unique Paths matrix with:
UniquePaths=mat2cell(SelUniquePaths,ones(1,size(SelUniquePaths,1)),size(SelUniquePaths,2));
%% =============== Part 2: Include Fail Time Probability Distributions ================
% Example to incorporate probability distributions of machine failure times
% Look for failure times for our 10 machines, with different sets of
% underlying fail-time probability distributions
% =============================
% Step #1: Initializations
% Total number of available machines
NumNodes = max(horzcat(UniquePaths{:}));
% Added value by each 'good' machine
ProdVal3 = 2;
% Subtracted value by each 'bad' machine (post-degradation)
BadLevel3 = -3;
% Acceptable limit of product value
GoodLim=0;
% ============================
% Step #2: Each class of machines (Class A, Class B, Class C) has
% its own life distribution, by which where the failure times come from
% Class A-Operation Machine Exponential Distributions:
theta=4800;
y=floor(sort(exprnd(theta*ones(1,length(ClassAOpMach)))));
fy=(1/theta)*exp(-y./theta);
ExpShapes=20;
yShape=floor(sort(exprnd(theta*ones(1,ExpShapes*length(ClassAOpMach))))); % ceil() or floor() is optional
fyShape=(1/theta)*exp(-yShape./theta);
% Class B-operation Machine Distributions:
% 1st Weibull distribution set:
%Scale parameter, alpha, in seconds:
% close def'n: at what point of time does the curve has the smallest
% derivative determines "spread" of the distribution
zwblA=16000;
z=floor(sort(wblrnd(zwblA,zwblB,[1,length(ClassBOpMach)])));
fz=(zwblB/(zwblA^zwblB)).*(z.^(zwblB-1)).*exp(-((z./zwblA).^(zwblB)));
% For a more clear shape of the distribution:
WeibullShapes=50;
zShape=floor(sort(wblrnd(zwblA,zwblB,[1,WeibullShapes*length(ClassBOpMach)])));
fzShape=(zwblB/(zwblA^zwblB)).*(zShape.^(zwblB-1)).*exp(-((zShape./zwblA).^(zwblB)));
% Class C-operation Machine Distributions:
% 2nd Weibull distribution set:
%Scale parameter, alpha, in seconds:
% close def'n: at what point of time does the curve has the smallest
% derivative determines "spread" of the distribution
xwblA=3800;
x=floor(sort(wblrnd(xwblA,xwblB,[1,length(ClassCOpMach)])));
fx=(xwblB/(xwblA^xwblB)).*(x.^(xwblB-1)).*exp(-((x./xwblA).^(xwblB)));
% For a more clear shape of the distribution:
WeibullShapes2=50;
xShape=floor(sort(wblrnd(xwblA,xwblB,[1,WeibullShapes2*length(ClassCOpMach)])));
fxShape=(xwblB/(xwblA^xwblB)).*(xShape.^(xwblB-1)).*exp(-((xShape./xwblA).^(xwblB)));
% Plot of the machines by underlying distribution of fail times
figure(22); plot(y,fy,'ro',yShape,fyShape,'r',...
z,fz,'bo',zShape,fzShape,'b',...
x,fx,'go',xShape,fxShape,'g')
xlim([0 3*NumParts3])
hold on;
xline(NumParts3);
hold off;
legend('Class A: Exponential Machines','Exponential Distribution; (Theta) = (4800)',...
'Class B: Weibull-1 Machines','Weibull-1 Distribution; (Alpha, Beta) = (16000, 1.3)',...
'Class C: Weibull-2 Machines','Weibull-2 Distribution; (Alpha, Beta) = (3800, 4)',...
'Simulation Cutoff','location','northeast')
title({'Estimated Life Distributions',...
'for Different Classes of Machines'})
xlabel('Time (Minutes)')
%%
% ============================
% % Alternate Step #2: Use a distribution generator to get failures at different times
% % for all N machines (nodes)
%
% % Set A proportion of machines will have a Weibull distribution for their
% % fail times
% setA=0.41;
%
% % Set B proportion of machines will have an Exponential distribution for their
% % fail times
% setB=0.59;
%
% % Exponential distribution set:
% % with known 1 failure per 4800 seconds (one hour, 20 minutes)
% theta=4800;
% y=floor(sort(exprnd(theta*ones(1,ceil(NumNodes*setB))))); % ceil() or floor() is optional
% fy=(1/theta)*exp(-y./theta);
% % For a more clear shape of the distribution:
% ExpShapes=20;
% yShape=floor(sort(exprnd(theta*ones(1,ceil(ExpShapes*NumNodes*setB))))); % ceil() or floor() is optional
% fyShape=(1/theta)*exp(-yShape./theta);
% % Weibull distribution set:
% %Shape parameter, beta, unitless:
% zwblB=1.3;
% %Scale parameter, alpha, in seconds:
% % close def'n: at what point of time does the curve has the smallest
% % derivative determines "spread" of the distribution
% zwblA=14400;
% z=floor(sort(wblrnd(zwblA,zwblB,[1,floor(NumNodes*setA)])));
% fz=(zwblB/(zwblA^zwblB)).*(z.^(zwblB-1)).*exp(-((z./zwblA).^(zwblB)));
% % For a more clear shape of the distribution:
% WeibullShapes=20;
% zShape=floor(sort(wblrnd(zwblA,zwblB,[1,floor(WeibullShapes*NumNodes*setA)])));
% fzShape=(zwblB/(zwblA^zwblB)).*(zShape.^(zwblB-1)).*exp(-((zShape./zwblA).^(zwblB)));
% % Plot of the machines by underlying distribution of fail times
% figure(22); plot(y,fy,'ro',yShape,fyShape,'r--',z,fz,'bo',zShape,fzShape,'b--')
% hold on;
% xline(NumParts3);
% hold off;
% legend('Exponential Machines','Exponential Shape','Weibull Machines','Weibull Shape','Simulation Cutoff','location','northeast')
% title({'Visualizing the failure time distributions','for different "types" of machines'})
% ============================
% Step #3: Not all machines start as brand new machines; some may have been used before
% y has fail times for Exponential Machines
% z has fail times for Weibull Machines
% Let us assume all the Weibull Machines have already been used for 4000 time units:
yUsedShift=100;
zUsedShift=5000;
xUsedShift=2700;
% h(t) or the hazard function
tiempo=1:NumParts3;
% Find an instantaneous time-dependent failure
% each class of machines each have an instantaneous component
% failure rate, e.g. hY(t) and hZ(t), respectively
% Class A-operation Machine, Exponential instantaneous failure rate:
hY=(1/theta).*ones(1,length(tiempo+yUsedShift));
% Class B-operation Machine, Weibull-1 instantaneous failure rate:
hZ=(zwblB/zwblA)*(((tiempo+zUsedShift)./zwblA).^(zwblB-1));
% Class C-operation Machine, Weibull-2 instantaneous failure rate:
hX=(xwblB/xwblA)*(((tiempo+xUsedShift)./xwblA).^(xwblB-1));
% ============================
% Step #7: FMEA-Derived Values for Occurrence, or likelihood of a failure mode:
% Integral of h(t) (cumulative hazard function)
% a(t) or the proportion of hazard function per failure mode
% Beta, the conditional probability that a failure mode will
% actually cost that much given the failure mode occurs
% Machines in different sets/classes have different failure rates (known as the hazard
% function), but different failure modes contribute to it. At any moment,
% different failure modes make up different ratios of the hazard function.
% Class A-operation Machines (Exponential distribution):
numFailModesClassAy=1;
aY= ones(numFailModesClassAy,length(tiempo));
% Class B-operation Machines (Weibull-1 distribution):
numFailModesClassBz=2;
minaZ=0;
maxaZ=1;
aZ=[0.3*ones(1,length(tiempo)); 0.7*ones(1,length(tiempo))];
%aZ=randfixedsum(numFailModesClassBz,length(tiempo),1,minaZ,maxaZ); %Using a function from Mathworks File Exchange
% Class C-operation Machines (Weibull-2 distribution):
numFailModesClassCx=3;
minaX=0;
maxaX=1;
aX=[0.2*ones(1,length(tiempo)); 0.5*ones(1,length(tiempo));0.3*ones(1,length(tiempo))];
%aX=randfixedsum(numFailModesClassCx,length(tiempo),1,minaX,maxaX); %Using a function from Mathworks File Exchange
figure(23);
plot(tiempo,hY,'r--','DisplayName','Class A Machines - Exponential Instantaneous Failure Rate')
title('Instantaneous Failure Rate & Their Failure Mode Ratios');
xlabel('Time (Minutes)');
hold on;
plot(tiempo,hZ,'b--','DisplayName','Class B Machines - Weibull-1 Instantaneous Failure Rate')
hold on;
plot(tiempo,hX,'g--','DisplayName','Class C Machines - Weibull-2 Instantaneous Failure Rate')
for jj=1:numFailModesClassAy
clr2=jj/numFailModesClassAy;
plot(tiempo,mean(aY(jj,:)).*hY,'LineStyle',':','Color',[clr2 0 0],'DisplayName',strcat('Class A - Exponential Failure Mode Ratio ', num2str(jj)));
end
for ii=1:numFailModesClassBz
clr1=ii/numFailModesClassBz;
plot(tiempo,mean(aZ(ii,:)).*hZ,'LineStyle',':','Color',[0 0 clr1],'DisplayName',strcat('Class B - Weibull-1 Failure Mode Ratio ', num2str(ii)));
% get(legend(gca),); % get legend from current axes.
% legend(strcat('Weibull Ratio', num2str(ii)))
end
for kk=1:numFailModesClassCx
clr3=kk/numFailModesClassCx;
plot(tiempo,mean(aX(kk,:)).*hX,'LineStyle',':','Color',[0 clr3 0],'DisplayName',strcat('Class C - Weibull-2 Failure Mode Ratio ', num2str(kk)));
end
legend('location','east')
%for journal sim:
% mean(aX(kk,end-4:end))
hold off
% Beta represents the conditional probability that a failure will result in
% identified severities, given that the failure mode actually occurs
BetaY=0.6;%unifrnd(.5,1,[numFailModesClassAy,1]);
BetaZ=[0.85; 0.99];%unifrnd(.75,1,[numFailModesClassBz,1]);
BetaX=[0.56; .72; 0.95]; %unifrnd(.6,1,[numFailModesClassCx,1]);
figure(24);
hold on
for jj=1:numFailModesClassAy
clr2=jj/numFailModesClassAy;
% BetaY(jj) if using unifrnd:
plot(tiempo,BetaY(jj).*ones(1,length(tiempo)),'LineStyle',':','Color',[clr2 0 0],'DisplayName', ...
strcat('Conditional Probability for Class A - Exponential Failure Mode: ', num2str(jj)));
end
for ii=1:numFailModesClassBz
clr1=ii/numFailModesClassBz;
% BetaZ(ii) if using unifrnd:
plot(tiempo,BetaZ(ii).*ones(1,length(tiempo)),'LineStyle','-','Color',[0 0 clr1],'DisplayName', ...
strcat('Conditional Probability for Class B - Weibull-1 Failure Mode: ', num2str(ii)));
end
for kk=1:numFailModesClassCx
clr3=kk/numFailModesClassCx;
% BetaX(kk) if using unifrnd:
plot(tiempo,BetaX(kk).*ones(1,length(tiempo)),'LineStyle','--','Color',[0 clr3 0],'DisplayName', ...
strcat('Conditional Probability for Class C - Weibull-2 Failure Mode: ', num2str(kk)));
end
xlabel('Time (Minutes)');
title('Conditional Probability to Result in Identified Severities');
legend('location','east')
hold off
tiempoAy=1:NumParts3+yUsedShift;
hYTot=(1/theta).*ones(1,length(tiempoAy));
hYinteg=cumtrapz(hYTot);
hYinteg=hYinteg(yUsedShift+1:NumParts3+yUsedShift);
tiempoBz=1:NumParts3+zUsedShift;
hZTot=(zwblB/zwblA)*(((tiempoBz)./zwblA).^(zwblB-1));
hZinteg=cumtrapz(hZTot);
hZinteg=hZinteg(zUsedShift+1:NumParts3+zUsedShift);
tiempoCx=1:NumParts3+xUsedShift;
hXTot=(xwblB/xwblA)*(((tiempoCx)./xwblA).^(xwblB-1));
hXinteg=cumtrapz(hXTot);
hXinteg=hXinteg(xUsedShift+1:NumParts3+xUsedShift);
figure(25);
plot(tiempo,hYinteg,'r--','DisplayName','Cumulative Class A - Exponential Hazard Function')
xlabel('Time (Minutes)');
hold on;
plot(tiempo,hZinteg,'b--','DisplayName','Cumulative Class B - Weibull-1 Hazard Function')
hold on;
plot(tiempo,hXinteg,'b--','DisplayName','Cumulative Class C - Weibull-2 Hazard Function')
hold off;
legend('location','northeast')
% tiempoAy=1:NumParts3+yUsedShift;
% hYTot=(1/theta).*ones(1,length(tiempoAy));
% hYinteg=cumtrapz(hYTot);
% hYinteg=hYinteg(yUsedShift+1:NumParts3+yUsedShift);
%
% tiempoBz=1:NumParts3+zUsedShift;
% hZTot=(zwblB/zwblA)*(((tiempoBz)./zwblA).^(zwblB-1));
% hZinteg=cumtrapz(hZTot);
% hZinteg=hZinteg(zUsedShift+1:NumParts3+zUsedShift);
% Step #8: FMEA-Derived Values for Severity, also known as Costs:
% SEVERITY - Costs of each failure mode may in the future be
% further divided between:
%(cost of failed component) + (cost of down-the-line consequences of failed
%component) + (cost of down-the-line consequences of the failure mode)
CostsAy=[118]; %unifrnd(110,130,[numFailModesClassAy,1]); %Class A, Exponential Machines, 1 failure mode
CostsBz=[155; 189]; %unifrnd(170,190,[numFailModesClassBz,1]); %Class B, Weibull-1 Machines, 2 failure modes
CostsCx=[67; 96; 150]; %unifrnd(50,180,[numFailModesClassCx,1]); %Class C, Weibull-2 Machines, 3 failure modes
figure(26);
title('Severity of Failure Modes')
ylabel('Dollars, in Thousands');
xlabel('Minutes');
hold on
for jj=1:numFailModesClassAy
clr2=jj/numFailModesClassAy;
plot(tiempo,CostsAy(jj).*ones(1,length(tiempo)),'LineStyle',':','Color',[clr2 0 0],'DisplayName', ...
sprintf('Exponential Machine Failure Mode %i', jj));
end
for ii=1:numFailModesClassBz
clr1=ii/numFailModesClassBz;
plot(tiempo,CostsBz(ii).*ones(1,length(tiempo)),'LineStyle','-','Color',[0 0 clr1],'DisplayName', ...
sprintf('Weibull-1 Machine Failure Mode %i', ii));
end
for kk=1:numFailModesClassCx
clr3=kk/numFailModesClassCx;
plot(tiempo,CostsCx(kk).*ones(1,length(tiempo)),'LineStyle','--','Color',[0 clr3 0],'DisplayName', ...
sprintf('Weibull-2 Machine Failure Mode %i', kk));
end
ylim([0 210]);
xlim([0 NumParts3]);
legend('location','northwest')
hold off
% ============================
% Step #9: Calculating Occurrence:
figure(27);
title('Failure Mode Occurrence, as Criticality(Cm)')
xlabel('Time (Minutes)');
hold on
BetaYFun=BetaY.*ones(numFailModesClassAy,length(tiempo)); %1 by 2700
BetaZFun=BetaZ.*ones(numFailModesClassBz,length(tiempo)); %2 by 2700
BetaXFun=BetaX.*ones(numFailModesClassCx,length(tiempo)); %3 by 2700
hYintegFun=repmat(hYinteg,numFailModesClassAy,1);
hZintegFun=repmat(hZinteg,numFailModesClassBz,1);
hXintegFun=repmat(hXinteg,numFailModesClassCx,1);
CriticalityAy = BetaYFun.*aY.*hYintegFun;%Failure Mode Criticalities for Class A, Exponential Machines
CriticalityBz = BetaZFun.*aZ.*hZintegFun;%Failure Mode Criticalities for Class B, Weibull-1 Machines
CriticalityCx = BetaXFun.*aX.*hXintegFun;%Failure Mode Criticalities for Class C, Weibull-2 Machines
for jj=1:numFailModesClassAy
clr2=jj/numFailModesClassAy;
plot(tiempo,CriticalityAy(jj,:),'LineStyle',':','Color',[clr2 0 0],'DisplayName', ...
strcat('Criticality for Class A, Exponential Machine Failure Mode: ', num2str(jj)));
end
for ii=1:numFailModesClassBz
clr1=ii/numFailModesClassBz;
plot(tiempo,CriticalityBz(ii,:),'LineStyle','-','Color',[0 0 clr1],'DisplayName', ...
strcat('Criticality for Class B, Weibull-1 Machine Failure Mode: ', num2str(ii)));
end
for kk=1:numFailModesClassCx
clr3=kk/numFailModesClassCx;
plot(tiempo,CriticalityCx(kk,:),'LineStyle','--','Color',[0 clr3 0],'DisplayName', ...
strcat('Criticality for Class C, Weibull-2 Machine Failure Mode: ', num2str(kk)));
end
legend('location','east')
hold off
% ============================
% Step #10: Calculating Risks:
figure(28);
title('Individual Failure Mode Risk')
ylabel('Cost Risk (in Thousands of Dollars)');
xlim([0 NumParts3]);
xlabel('Time (Minutes)');
hold on
CostAyFun=CostsAy.*ones(numFailModesClassAy,length(tiempo));
CostBzFun=CostsBz.*ones(numFailModesClassBz,length(tiempo));
CostCxFun=CostsCx.*ones(numFailModesClassCx,length(tiempo));
%Risk of failure modes of a Class A, Exponential Machine
RiskAyFun = CostAyFun.*CriticalityAy;
%Risk of failure modes of a Class B, Weibull-1 Machine
RiskBzFun = CostBzFun.*CriticalityBz;
%Risk of failure modes of a Class C, Weibull-2 Machine
RiskCxFun = CostCxFun.*CriticalityCx;
for jj=1:numFailModesClassAy
clr2=jj/numFailModesClassAy;
plot(tiempo,RiskAyFun(jj,:),'LineStyle',':','Color',[clr2 0 0],'DisplayName', ...
strcat('Class A, Exponential Machine Failure Mode: ', num2str(jj)));
end
for ii=1:numFailModesClassBz
clr1=ii/numFailModesClassBz;
plot(tiempo,RiskBzFun(ii,:),'LineStyle','-','Color',[0 0 clr1],'DisplayName', ...
strcat('Class B, Weibull-1 Machine Failure Mode: ', num2str(ii)));
end
for kk=1:numFailModesClassCx
clr3=kk/numFailModesClassCx;
plot(tiempo,RiskCxFun(kk,:),'LineStyle','--','Color',[0 clr3 0],'DisplayName', ...
strcat('Class C, Weibull-2 Machine Failure Mode: ', num2str(kk)));
end
legend('location','northwest')
hold off
ComponentRiskAy=sum(RiskAyFun,1);
ComponentRiskBz=sum(RiskBzFun,1);
ComponentRiskCx=sum(RiskCxFun,1);
figure(29);
plot(tiempo,ComponentRiskAy,'LineStyle',':','Color',[1 0 0],'DisplayName', ...
'One Class A, Exponential Machine (Sum of Failure Mode Risks)');
hold on;
plot(tiempo,ComponentRiskBz,'LineStyle','-','Color',[0 0 1],'DisplayName', ...
'One Class B, Weibull-1 Machines (Sum of Failure Mode Risks)');
hold on;
plot(tiempo,ComponentRiskCx,'LineStyle','--','Color',[0 1 0],'DisplayName', ...
'One Class C, Weibull-2 Machines (Sum of Failure Mode Risks)');
legend('location','northwest')
title('Aggregated Cost Risks')
xlabel('Minutes');
ylabel('Cost Risk (in Thousands of Dollars)');
xlim([0 NumParts3]);
hold off
figure(30);
SystemRisk=length(ClassAOpMach)*ComponentRiskAy + length(ClassBOpMach)*ComponentRiskBz +length(ClassCOpMach)*ComponentRiskCx;
% ALT:
%SystemRisk = 1*ComponentRiskAy + 2*ComponentRiskBz + 1*ComponentRiskCx;
plot(tiempo,SystemRisk,'k--');
hold on;
title('Total System Risk (All Machines Considered)');
legend('location','east')
NodePresence=nan(1,NumNodes);
for ii=1:NumNodes
NodePresence(1,ii)=length(find(horzcat(UniquePaths{:})==ii));
end
NodePresencePreNorm=NodePresence/max(NodePresence);
NodePresenceNorm=NodePresencePreNorm + (1-mean(NodePresencePreNorm));
%NodePresenceNorm=(NodePresence-min(NodePresence))/(max(NodePresence)-min(NodePresence))
a=zeros(1,10);a(ClassAOpMach)=1;
b=zeros(1,10);b(ClassBOpMach)=1;
c=zeros(1,10);c(ClassCOpMach)=1;
WeightedSystemRisk=sum(NodePresenceNorm.*a)*ComponentRiskAy + ...
sum(NodePresenceNorm.*b)*ComponentRiskBz + sum(NodePresenceNorm.*c)*ComponentRiskCx;
% ALT:
%WeightedSystemRisk=1*mean(nonzeros(NodePresenceNorm.*ClassAidx))*ComponentRiskAy + ...
% 2*mean(nonzeros(NodePresenceNorm.*ClassBidx))*ComponentRiskBz + 1*mean(nonzeros(NodePresenceNorm.*ClassCidx))*ComponentRiskCx;
plot(tiempo,WeightedSystemRisk,'LineStyle',':','Color',[0.25 0.25 0.25]);
legend('System (Scenario) Risk','Weighted System Risk','location','northwest');
hold off;
PathsMap = zeros(length(UniquePaths),NumNodes);
for upi = 1:length(UniquePaths)
PathsMap(upi,UniquePaths{upi}) = 1;
end
PathRisk=zeros(length(UniquePaths),length(SystemRisk));
for kk=1:length(UniquePaths)
PathSetAy=sum(PathsMap(kk,:).*a);
PathSetBz=sum(PathsMap(kk,:).*b);
PathSetCx=sum(PathsMap(kk,:).*c);
PathRisk(kk,:)=PathSetAy*ComponentRiskAy+PathSetBz*ComponentRiskBz +PathSetCx*ComponentRiskCx ;
end
PathNames = {};
figure(31);
%xline(NumParts3);
hold on
for ppi = 1:length(UniquePaths) %for each of the unique paths
plot(tiempo,PathRisk(ppi,:));% +unifrnd(0,10),'.');
PathNames{ppi} = sprintf('Path #%i Risk',ppi);
end
xlabel('Time (s)');
legend(PathNames{:},'location','eastoutside')
hold off
%% actual failure times for different simulations
% y has fail times for Exponential Machines
% z has fail times for Weibull Machines
% Let us assume all the Weibull Machines have already been used for 4000 time units:
%close all;
y=nan(NumSims,length(ClassAOpMach));
z=nan(NumSims,length(ClassBOpMach));
x=nan(NumSims,length(ClassCOpMach));
for qq=1:NumSims
y(qq,:)=floor(sort(exprnd(theta*ones(1,length(ClassAOpMach)))));
z(qq,:)=floor(sort(wblrnd(zwblA,zwblB,[1,length(ClassBOpMach)])));
x(qq,:)=floor(sort(wblrnd(xwblA,xwblB,[1,length(ClassCOpMach)])));
end
y=y-yUsedShift;
z=z-zUsedShift;
x=x-xUsedShift;
allTimesTemp=[y z x];
allTimesTemp(allTimesTemp<1)=randi(1,size(allTimesTemp(allTimesTemp<1)));
yAll=allTimesTemp(:,1:length(ClassAOpMach));
zAll=allTimesTemp(:,1+length(ClassAOpMach):length(ClassAOpMach)+length(ClassBOpMach));
xAll=allTimesTemp(:,1+length(ClassAOpMach)+length(ClassBOpMach):end);
% ============================
% Step #4: If some fail times are outside simulation/operation duration
% (consider 45 minutes or 2700 seconds), drop them
% Vector of all machine failure times, regardless of distribution
AllMachineFailTimesAll=[yAll,zAll,xAll]; %Order of Class A, Class B, Class C
PCPostCMSRisk=nan(NumParts3,NumSims);
PCNewValue_Alg=nan(NumParts3,NumSims);
PCNoAlg_RISK_scenario=nan(NumParts3,NumSims);
NoAlgoRiskAy=nan(NumParts3,NumSims);
NoAlgoRiskBz=nan(NumParts3,NumSims);
NoAlgoRiskCx=nan(NumParts3,NumSims);
tpProdCont=nan(NumParts3,NumSims);
tnProdCont=nan(NumParts3,NumSims);
fpProdCont=nan(NumParts3,NumSims);
fnProdCont=nan(NumParts3,NumSims);
for vv = 1:NumSims
AllMachineFailTimes=AllMachineFailTimesAll(vv,:);
while length(AllMachineFailTimes) ~= length(unique(AllMachineFailTimes))
[~,ind]=unique(AllMachineFailTimes); ind=ind';
duplicateInd=setdiff(1:length(AllMachineFailTimes),ind);
AllMachineFailTimes(duplicateInd)=AllMachineFailTimes(duplicateInd)+1;
end
y=AllMachineFailTimes(1:length(ClassAOpMach));
z=AllMachineFailTimes(1+length(ClassAOpMach):length(ClassAOpMach)+length(ClassBOpMach));
x=AllMachineFailTimes(1+length(ClassAOpMach)+length(ClassBOpMach):end);
% Dropping fail times that do not fall within simulation
DegradeStart3=AllMachineFailTimes(AllMachineFailTimes<=NumParts3);
%Store fail times that happen after the simulation
DegaradePostSim=AllMachineFailTimes(AllMachineFailTimes>NumParts3);
% ============================
% Step #5: Define Set of Bad Machines.
% Each column is a machine that has gone bad and its
% degradation start time
%BadSet3 = sort(randperm(NumNodes,length(DegradeStart3)));
BadSet3 = find(ismember(AllMachineFailTimes,DegradeStart3));
MachineFailureTimes=[BadSet3;DegradeStart3];
% Also defining the set of Machines that didn't go bad during the
% simulated duration of operation
PostSimFailSet=1:length(AllMachineFailTimes);
PostSimFailSet(BadSet3)=[];
MachinePostSimFailTimes=[PostSimFailSet;DegaradePostSim];
% ============================
% Step #6: Differentiate machines based on their distributions (class with
% exponential distribution, class with weibull distribution, and so on)
MachinesFailsInSeq=[sortrows(MachineFailureTimes',2)', sortrows(MachinePostSimFailTimes',2)'];
% Class A: Exponential Machines
ClassAidxFailSeq=ismember(MachinesFailsInSeq(2,:),y);
ClassAMachinesAndFailTimes=[sortrows(MachinesFailsInSeq(:,ClassAidxFailSeq)',1)';ClassAOpMach]; %Last row provides us with original machine ID #s
ClassAidx=zeros(1,NumNodes);
ClassAidx(MachinesFailsInSeq(1,ClassAidxFailSeq))=1;
% Class B: Weibull Type-1 Machines
ClassBidxFailSeq=ismember(MachinesFailsInSeq(2,:),z);
ClassBMachinesAndFailTimes=[sortrows(MachinesFailsInSeq(:,ClassBidxFailSeq)',1)';ClassBOpMach]; %Last row provides us with original machine ID #s
ClassBidx=zeros(1,NumNodes);
ClassBidx(MachinesFailsInSeq(1,ClassBidxFailSeq))=1;
% Class C: Weibull Type-2 Machines
ClassCidxFailSeq=ismember(MachinesFailsInSeq(2,:),x);
ClassCMachinesAndFailTimes=[sortrows(MachinesFailsInSeq(:,ClassCidxFailSeq)',1)';ClassCOpMach]; %Last row provides us with original machine ID #s
ClassCidx=zeros(1,NumNodes);
ClassCidx(MachinesFailsInSeq(1,ClassCidxFailSeq))=1;
%sort(AllMachineFailTimes)
MFT=[BadSet3; DegradeStart3];
BadSet4=nan(NumParts3,NumNodes);
BadSet4(sub2ind(size(BadSet4),MFT(2,:),MFT(1,:)))=MFT(1,:);
[r,c]=find(~isnan(BadSet4));
for dd=1:length(r)
BadSet4(r(dd):end,c(dd))=BadSet4(r(dd),c(dd));
end
BadSet4Baseline=BadSet4;
GoodSet=1:10;
GoodSet(BadSet3)=[];
GoodSet4=ones(NumParts3,NumNodes);
GoodSet4(~isnan(BadSet4))=nan;
GoodSet4=GoodSet4.*(1:NumNodes);
%StartValue=Exp3.Output.PartPath;
StartValue=0; %temporary, until I redo another example where I run
% FunMfgSimulator_v02.m all over again.
BadShift = ProdVal3*ones(length(BadSet3)+1,NumNodes);
for i=1:length(BadSet3)
BadShift(i+1,BadSet3(1:i)) = BadLevel3;
end
for pp = 1:NumParts3
PartPath{pp} = UniquePaths{randi(length(UniquePaths))};
if (isempty(DegradeStart3)==1)
Shift=BadShift(1,:);
PartQuality(pp) = StartValue + sum(Shift(PartPath{pp}))-rand*.01;
elseif pp <= DegradeStart3(1)
Shift=BadShift(1,:);
PartQuality(pp) = StartValue + sum(Shift(PartPath{pp}))-rand*.01;
else
ShiftFinder=find(DegradeStart3<pp);
ShiftFinder=ShiftFinder(end);
Shift=BadShift(ShiftFinder+1,:);
PartQuality(pp) = StartValue + sum(Shift(PartPath{pp}))-rand*.01;
end
%Parts output time: they come out pretty uniformly, per unit of time
%given some small randomness
PartOutTime(pp) = pp+rand*.2;
end
%Sort Parts by Production Time
[PartOutTime, ti] = sort(PartOutTime); %ti is the index of sorted parts
%Using the same sort index, here we make sure the part quality and path
%values refer to the same product:
PartQuality = PartQuality(ti);
PartPath = PartPath(ti);
Win = max(10,floor(NumParts3/100)); %Win=10 in the example with NumParts=500
%Moving Window of:
PWN = nan(Win,NumNodes);%Part Quality With Node
PWON = nan(Win,NumNodes);%Part Qualty Without Node
%Average of Windows
QWithN = nan(NumParts3,NumNodes);%Average Quality With Node
QWithoutN = nan(NumParts3,NumNodes);%Average Quality Without Node
ProdContThresh=-0.1;
ProdCont=nan(NumParts3,NumNodes);
ProdContNorm=nan(NumParts3,NumNodes);
% tpProdCont=nan(NumParts3,1);
% tnProdCont=nan(NumParts3,1);
% fpProdCont=nan(NumParts3,1);
% fnProdCont=nan(NumParts3,1);
fprProdCont=nan(NumParts3,1);
tprProdCont=nan(NumParts3,1);
fnrProdCont=nan(NumParts3,1);
tpAy_PC=nan(NumParts3,1);
tpBz_PC=nan(NumParts3,1);
tpCx_PC=nan(NumParts3,1);
fpAy_PC=nan(NumParts3,1);
fpBz_PC=nan(NumParts3,1);
fpCx_PC=nan(NumParts3,1);
fnAy_PC=nan(NumParts3,1);
fnBz_PC=nan(NumParts3,1);
fnCx_PC=nan(NumParts3,1);
tnAy_PC=nan(NumParts3,1);
tnBz_PC=nan(NumParts3,1);
tnCx_PC=nan(NumParts3,1);
PartQualityUB = StartValue+(length(UniquePaths{1})*ProdVal3);
if length(BadSet3)<length(UniquePaths{1})
PartQualityLB = StartValue + (length(BadSet3)*BadLevel3) + ((length(UniquePaths{1}) - length(BadSet3))*ProdVal3);
else
PartQualityLB = StartValue+length(UniquePaths{1})*BadLevel3;
end
ProdContRange = PartQualityUB - PartQualityLB;
NodeNames={};
for Ni = 1:NumNodes
NodeNames{Ni} = sprintf('Node #%i',Ni);
end
CostCheckAy=20;
% CostFixAy=4;
% CostMissAy=16;
PCCorrMaintAy=nan(NumParts3,1);
PCContFailAy= nan(NumParts3,1);
PCUnnecessaryMaintAy=nan(NumParts3,1);
% For the weibull-1 machines:
CostCheckBz=30;
%CostFixBz=8;
%CostMissBz=12;
PCCorrMaintBz=nan(NumParts3,1);
PCContFailBz= nan(NumParts3,1);
PCUnnecessaryMaintBz=nan(NumParts3,1);
% For the weibull-2 machines:
CostCheckCx=45;
%CostFixCx=10;
% CostMissCx=20;
PCCorrMaintCx=nan(NumParts3,1);
PCContFailCx= nan(NumParts3,1);
PCUnnecessaryMaintCx=nan(NumParts3,1);
PCPostAlg_RISK_scenario=nan(NumParts3,1);
PCValue_Alg=nan(NumParts3,1);
PCAlgorithmsRisk=nan(NumParts3,1);
PostAlgoRiskAy=nan(NumParts3,1);
PostAlgoRiskBz=nan(NumParts3,1);
PostAlgoRiskCx=nan(NumParts3,1);
for pp = 1:NumParts3
%Place Part Quality in Appropriate Windowed Bins
for Ni = 1:NumNodes
%If Part Passed this Node (machining stage)
if any(PartPath{pp}==Ni)
PWN(:,Ni) = [PWN(2:end,Ni); PartQuality(pp)]; %moving window, row is the window of (10) products and column is each machine/node
else
%If Part Did Not Pass Through This Node
PWON(:,Ni) = [PWON(2:end,Ni); PartQuality(pp)];
end
end
QWithN(pp,:) = nanmean(PWN); %Average of each PWN column; each column characterizes a machine/node
QWithoutN(pp,:)= nanmean(PWON); %Average of each PWON column; each column characterizes a machine/node
ProdCont(pp,:)=QWithN(pp,:)-QWithoutN(pp,:);
ProdContNorm(pp,:)=ProdCont(pp,:)/abs(ProdContRange);
%plot(1:pp,ProdContNorm(1:pp,:))
%pause(2);
%clf;
BS4=BadSet4(pp,~isnan(BadSet4(pp,:)));
GS4=GoodSet4(pp,~isnan(GoodSet4(pp,:)));
tpProdCont(pp,vv)=sum(ProdContNorm(pp,BS4)<=ProdContThresh);
fpProdCont(pp,vv)=sum(ProdContNorm(pp,GS4)<=ProdContThresh);
fnProdCont(pp,vv)=sum(ProdContNorm(pp,BS4)>ProdContThresh);
tnProdCont(pp,vv)=sum(ProdContNorm(pp,GS4)>ProdContThresh);
fprProdCont(pp)=fpProdCont(pp,vv)/(tnProdCont(pp,vv)+fpProdCont(pp,vv));
tprProdCont(pp)=tpProdCont(pp,vv)/(tpProdCont(pp,vv)+fnProdCont(pp,vv));
fnrProdCont(pp)=1-tprProdCont(pp);
tpAy_PC(pp)=sum((ProdContNorm(pp,BS4)<=ProdContThresh).*ClassAidx(BS4));
tpBz_PC(pp)=sum((ProdContNorm(pp,BS4)<=ProdContThresh).*ClassBidx(BS4));
tpCx_PC(pp)=sum((ProdContNorm(pp,BS4)<=ProdContThresh).*ClassCidx(BS4));
fpAy_PC(pp)=sum((ProdContNorm(pp,GS4)<=ProdContThresh).*ClassAidx(GS4));
fpBz_PC(pp)=sum((ProdContNorm(pp,GS4)<=ProdContThresh).*ClassBidx(GS4));
fpCx_PC(pp)=sum((ProdContNorm(pp,GS4)<=ProdContThresh).*ClassCidx(GS4));
fnAy_PC(pp)=sum((ProdContNorm(pp,BS4)>ProdContThresh).*ClassAidx(BS4));
fnBz_PC(pp)=sum((ProdContNorm(pp,BS4)>ProdContThresh).*ClassBidx(BS4));
fnCx_PC(pp)=sum((ProdContNorm(pp,BS4)>ProdContThresh).*ClassCidx(BS4));
tnAy_PC(pp)=sum((ProdContNorm(pp,GS4)>ProdContThresh).*ClassAidx(GS4));
tnBz_PC(pp)=sum((ProdContNorm(pp,GS4)>ProdContThresh).*ClassBidx(GS4));
tnCx_PC(pp)=sum((ProdContNorm(pp,GS4)>ProdContThresh).*ClassCidx(GS4));
% For the exponential machines:
NoAlgoRiskAy(pp,vv)=sum(~isnan(BadSet4Baseline(pp,:)).*ClassAidx)*sum(aY(:,pp).*CostAyFun(:,pp));
% For the weibull-1 machines:
NoAlgoRiskBz(pp,vv)=sum(~isnan(BadSet4Baseline(pp,:)).*ClassBidx)*sum(aZ(:,pp).*CostBzFun(:,pp));
% For the weibull-2 machines:
NoAlgoRiskCx(pp,vv)=sum(~isnan(BadSet4Baseline(pp,:)).*ClassCidx)*sum(aX(:,pp).*CostCxFun(:,pp));
tpCoeff=0.3; %Cost of check and fix (repair)
fnCoeff=1.3; %Cost Miss
% For the exponential machines:
PostAlgoRiskAy(pp)=...%(tnAy_PC(pp)+fpAy_PC(pp))*sum(aY(:,pp).*CostAyFun(:,pp))...
tpAy_PC(pp)*tpCoeff*sum(aY(:,pp).*CostAyFun(:,pp))...
+ fpAy_PC(pp)*CostCheckAy...
+ fnAy_PC(pp)*fnCoeff*sum(aY(:,pp).*CostAyFun(:,pp));
%+ tpAy_PC(pp)*(CostCheckAy+CostFixAy) ...
%+ fnAy_PC(pp)*CostMissAy ...
% For the weibull-1 machines:
PostAlgoRiskBz(pp)=...%(tnBz_PC(pp)+fpBz_PC(pp))*sum(aZ(:,pp).*CostBzFun(:,pp))...
tpBz_PC(pp)*tpCoeff*sum(aZ(:,pp).*CostBzFun(:,pp))...
+ fpBz_PC(pp)*CostCheckBz...
+ fnBz_PC(pp)*fnCoeff*sum(aZ(:,pp).*CostBzFun(:,pp));
% + fnBz_PC(pp)*CostMissBz ...
%+ tpBz_PC(pp)*(CostCheckBz+CostFixBz) ...
% For the weibull-2 machines:
PostAlgoRiskCx(pp)=...%(tnCx_PC(pp)+fpCx_PC(pp))*sum(aX(:,pp).*CostCxFun(:,pp))...
tpCx_PC(pp)*tpCoeff* sum(aX(:,pp).*CostCxFun(:,pp))...
+ fpCx_PC(pp)*CostCheckCx ...
+ fnCx_PC(pp)*fnCoeff*sum(aX(:,pp).*CostCxFun(:,pp));
% + tpCx_PC(pp)*(CostCheckCx+CostFixCx) ...
% + fnCx_PC(pp)*CostMissCx ...
PCPostCMSRisk(pp,vv)=PostAlgoRiskAy(pp) + PostAlgoRiskBz(pp) + PostAlgoRiskCx(pp);
%PCNoAlg_RISK_scenario = SystemRisk(pp); % inherent system risk
PCNoAlg_RISK_scenario(pp,vv) = NoAlgoRiskAy(pp,vv) + NoAlgoRiskBz(pp,vv) + NoAlgoRiskCx(pp,vv);
PCNewValue_Alg(pp,vv) = PCNoAlg_RISK_scenario(pp,vv) - PCPostCMSRisk(pp,vv);
if tpProdCont(pp,vv)>0
RepairedMachines=nonzeros((ProdContNorm(pp,BS4)<=ProdContThresh).*BS4)';
for ww=1:length(RepairedMachines)
RepairedIndx=zeros(1,NumNodes);
RepairedIndx(RepairedMachines(ww))=1;
RepairedNewTime=sum(RepairedIndx.*ClassAidx)*floor(sort(exprnd(theta*ones(1,1))))...
+ sum(RepairedIndx.*ClassBidx)*floor(sort(wblrnd(zwblA,zwblB,[1,1])))...
+ sum(RepairedIndx.*ClassCidx)*floor(sort(wblrnd(xwblA,xwblB,[1,1])));
DegradeStart3(BadSet3==RepairedMachines(ww))=DegradeStart3(BadSet3==RepairedMachines(ww)) + RepairedNewTime;
% BadSet3(BadSet3==RepairedMachines(ww))=[];
BadSet4(pp+1:end,RepairedMachines(ww))=nan;
if DegradeStart3(BadSet3==RepairedMachines(ww)) < NumParts3
BadSet4(DegradeStart3(BadSet3==RepairedMachines(ww)):end,RepairedMachines(ww))=RepairedMachines(ww);
end
end
GoodSet=1:10;
GoodSet(BadSet3)=[];
GoodSet4=ones(NumParts3,NumNodes);
GoodSet4(~isnan(BadSet4))=nan;
GoodSet4=GoodSet4.*(1:NumNodes);
BadShift = ProdVal3*ones(length(BadSet3)+1,NumNodes);
for i=1:length(BadSet3)
BadShift(i+1,BadSet3(1:i)) = BadLevel3;
end
for zz = pp+1:NumParts3
PartPath{zz} = UniquePaths{randi(length(UniquePaths))};
if (isempty(DegradeStart3)==1)
Shift=BadShift(1,:);
PartQuality(zz) = StartValue + sum(Shift(PartPath{zz}))-rand*.01;
elseif zz <= min(DegradeStart3)
Shift=BadShift(1,:);
PartQuality(zz) = StartValue + sum(Shift(PartPath{zz}))-rand*.01;
else
ShiftFinder=find(DegradeStart3<zz);
ShiftFinder=ShiftFinder(end);
Shift=BadShift(ShiftFinder+1,:);
PartQuality(zz) = StartValue + sum(Shift(PartPath{zz}))-rand*.01;
end
%Parts output time: they come out pretty uniformly, per unit of time
%given some small randomness
PartOutTime(zz) = zz+rand*.2;
end
%Sort Parts by Production Time
[PartOutTime, ti] = sort(PartOutTime); %ti is the index of sorted parts
%Using the same sort index, here we make sure the part quality and path
%values refer to the same product:
PartQuality = PartQuality(ti);
PartPath = PartPath(ti);
end
end
end
NoAlgoRiskAyCompAVG=mean(NoAlgoRiskAy,2)/sum(ClassAidx);
NoAlgoRiskBzCompAVG=mean(NoAlgoRiskBz,2)/sum(ClassBidx);
NoAlgoRiskCxCompAVG=mean(NoAlgoRiskCx,2)/sum(ClassCidx);
PCPostCMSRiskSTD=std(PCPostCMSRisk,0,2);
PCPostCMSRiskAVG=mean(PCPostCMSRisk,2);
PCNoAlg_RISK_scenarioSTD=std(PCNoAlg_RISK_scenario,0,2);
PCNoAlg_RISK_scenarioAVG=mean(PCNoAlg_RISK_scenario,2);
% PCNewValue_AlgSTD=std(PCNewValue_Alg,0,2);
PCNewValue_AlgAVG=mean(PCNewValue_Alg,2);
% ROI Stuff Here
PCNewValue_AlgAVGUpd=PCNewValue_AlgAVG-CMSOngoingCosts-CMSOneTimeCosts;
upperPCPostCMSRiskAVGSTD=PCPostCMSRiskAVG+PCPostCMSRiskSTD;
lowerPCPostCMSRiskAVGSTD=PCPostCMSRiskAVG-PCPostCMSRiskSTD;
PCNoAlg_RISK_scenarioUpper=PCNoAlg_RISK_scenarioAVG + PCNoAlg_RISK_scenarioSTD;
PCNoAlg_RISK_scenarioLower=PCNoAlg_RISK_scenarioAVG - PCNoAlg_RISK_scenarioSTD;
PCNewValue_AlgSTD=sqrt((PCNoAlg_RISK_scenarioSTD.^2)+(PCPostCMSRiskSTD.^2)-2*sum(((PCNoAlg_RISK_scenario-PCNoAlg_RISK_scenarioAVG).*(PCPostCMSRisk-PCPostCMSRiskAVG)/NumSims),2) );
%size(tst)
upperROI=PCNewValue_AlgAVGUpd + PCNewValue_AlgSTD ;
lowerROI=PCNewValue_AlgAVGUpd - PCNewValue_AlgSTD;
RiskValMat=[PCNewValue_AlgAVGUpd];
RiskValMat2=[PCNoAlg_RISK_scenarioAVG, PCPostCMSRiskAVG];
figure(35);
h9=plot(1:pp,RiskValMat(1:pp,:));
hold on;