1use std::fmt::Display;
2use crate::stats::stddev;
3
4pub struct Cpk {
6 pub cpk: f64,
8 pub ca: f64,
10 pub cp: f64,
12
13 pub usl: f64,
15 pub lsl: f64,
17
18}
19
20impl Cpk {
21 pub fn new<T: Display + std::fmt::Debug>(usl: f64, lsl: f64, array: Vec<T>) -> Self {
23 let mut total = 0.0;
24 let list: Vec<f64> = array.iter().map(|x|
25 {
26 let y = x.to_string().parse::<f64>().unwrap();
27 total += y;
28 y
29 }
30 ).collect();
31
32 let stddev = stddev(array);
34 let n = list.len();
36 let x = total / n as f64;
38 let t = usl - lsl;
40
41 let u = (usl + lsl) / 2.0;
43 let mut cp = t / (6.0 * stddev);
44 let cpu = (usl - x).abs() / (3.0 * stddev);
45 let cpl = (x - lsl).abs() / (3.0 * stddev);
46 let ca = (x - u) / (t / 2.0);
47 let mut cpk = if cpu > cpl { cpl } else { cpu };
48 if cpk.is_infinite() {
49 cpk = 0.0;
50 }
51 if cp.is_infinite() {
52 cp = 0.0;
53 }
54 Self {
55 cpk,
56 ca: ca * 100.0,
57 cp,
58 usl,
59 lsl,
60 }
61 }
62 pub fn cpk_rating_criteria(&self) -> (&'static str, &'static str) {
63 if self.cpk >= 2.0 {
64 return ("A++", "特优,可考虑降低成本。");
65 } else if 1.67 <= self.cpk && self.cpk < 2.0 {
66 return ("A+", "优,应当保持之。");
67 } else if 1.33 <= self.cpk && self.cpk < 1.67 {
68 ("A", "良,能力好,状态稳定,但应尽力提升到A+级。")
69 } else if 1.00 <= self.cpk && self.cpk < 1.33 {
70 ("B", "一般,制程因素稍有变异即有生产不良的危险,应利用各种资源及方法将其提升为A级。")
71 } else if 0.67 <= self.cpk && self.cpk < 1.00 {
72 ("C", "差,制程不良较多。须提升其能力。")
73 } else {
74 ("D", "不可接受,其能力太差,应考虑重新整改设计制程。")
75 }
76 }
77 pub fn ca_rating_criteria(&self) -> (&'static str, &'static str) {
78 if self.ca <= 12.5 {
79 ("A", "作业员遵守作业标准操作并达到要求,需继续保持。")
80 } else if 12.5 <= self.ca && self.ca <= 25.0 {
81 ("B", "有必要将其改进为A级。")
82 } else if 25.0 <= self.ca && self.ca <= 50.0 {
83 ("C", "作业员可能看错规格或不按作业标准操作。须检讨规格及作业标准。")
84 } else {
85 ("D", "应采取紧急措施全面检讨所有可能影响之因素,必要时得停止生产。")
86 }
87 }
88 pub fn cp_rating_criteria(&self) -> (&'static str, &'static str) {
89 if self.cp >= 1.67 {
90 ("A+", "无缺点,可考虑降低成本。")
91 } else if 1.33 <= self.cp && self.cp <= 1.67 {
92 ("A", "状态良好维持现状。")
93 } else if 1.00 <= self.cp && self.cp <= 1.33 {
94 ("B", "可以改进为A级。")
95 } else if 0.67 <= self.cp && self.cp <= 1.00 {
96 ("C", "制程不良较多。须提升能力。")
97 } else {
98 ("D", "制程能力好差,应考虑重新整改设计制程。")
99 }
100 }
101}