|
@@ -0,0 +1,130 @@
|
|
|
|
|
+package com.etotem.cfc.service;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.LinkedHashMap;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 食材推荐加权规则引擎(能力键 {@code food.rule.weightEngine},S-cfc-464)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>§4.2 多因子公式:</p>
|
|
|
|
|
+ * <pre>
|
|
|
|
|
+ * final_score = clamp( base_report_score × w1
|
|
|
|
|
+ * + health_adjust_score × w2
|
|
|
|
|
+ * + preference_boost × w3
|
|
|
|
|
+ * + pairing_effect × w4
|
|
|
|
|
+ * + processing_effect × w5
|
|
|
|
|
+ * + disease_group_adjust × w6
|
|
|
|
|
+ * , -100, 100 )
|
|
|
|
|
+ * </pre>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>纯计算服务:不依赖 Spring / DB / 外部服务,规则表 {@code food_recommend_weight_rule}
|
|
|
|
|
+ * 的后台 CRUD 与灰度由独立能力键 {@code ops.foodRuleManagement} 承载。默认权重表 v1 以
|
|
|
|
|
+ * {@code base_report=1}、其余因子 0 保持既有「报告分直写 index_score」口径,待规则表配置后
|
|
|
|
|
+ * 由 {@link #finalScoreWithWeights} 按显式权重启用多因子。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+public class FoodRecommendWeightEngine {
|
|
|
|
|
+
|
|
|
|
|
+ /** §4.2 factor_type 枚举:报告分因子。 */
|
|
|
|
|
+ public static final String FACTOR_BASE_REPORT = "base_report";
|
|
|
|
|
+ /** §4.2 factor_type 枚举:健康调整因子(11-09 四态标记升级)。 */
|
|
|
|
|
+ public static final String FACTOR_HEALTH_ADJUST = "health_adjust";
|
|
|
|
|
+ /** §4.2 factor_type 枚举:偏好加成因子。 */
|
|
|
|
|
+ public static final String FACTOR_PREFERENCE = "preference";
|
|
|
|
|
+ /** §4.2 factor_type 枚举:搭配效应因子。 */
|
|
|
|
|
+ public static final String FACTOR_PAIRING = "pairing";
|
|
|
|
|
+ /** §4.2 factor_type 枚举:加工效应因子。 */
|
|
|
|
|
+ public static final String FACTOR_PROCESSING = "processing";
|
|
|
|
|
+ /** §4.2 factor_type 枚举:病症分组调整因子。 */
|
|
|
|
|
+ public static final String FACTOR_DISEASE_GROUP = "disease_group";
|
|
|
|
|
+
|
|
|
|
|
+ /** final_score 下界(§4.2)。 */
|
|
|
|
|
+ private static final double SCORE_MIN = -100.0;
|
|
|
|
|
+ /** final_score 上界(§4.2)。 */
|
|
|
|
|
+ private static final double SCORE_MAX = 100.0;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 6 因子默认权重表,和 = 1(§4.2)。
|
|
|
|
|
+ * v1 基线仅启用 base_report,保证接入后既有指数口径不变。
|
|
|
|
|
+ */
|
|
|
|
|
+ public static Map<String, Double> defaultWeights() {
|
|
|
|
|
+ Map<String, Double> weights = new LinkedHashMap<String, Double>();
|
|
|
|
|
+ weights.put(FACTOR_BASE_REPORT, 1.0);
|
|
|
|
|
+ weights.put(FACTOR_HEALTH_ADJUST, 0.0);
|
|
|
|
|
+ weights.put(FACTOR_PREFERENCE, 0.0);
|
|
|
|
|
+ weights.put(FACTOR_PAIRING, 0.0);
|
|
|
|
|
+ weights.put(FACTOR_PROCESSING, 0.0);
|
|
|
|
|
+ weights.put(FACTOR_DISEASE_GROUP, 0.0);
|
|
|
|
|
+ return weights;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 11-09 标记升级(§4.2):四态 → health_adjust_score。
|
|
|
|
|
+ * blocked=-50 / warning=-15 / boost=+10 / normal=0;null 与未知标记兜底 0。
|
|
|
|
|
+ */
|
|
|
|
|
+ public static double healthAdjustScore(String adjustment) {
|
|
|
|
|
+ if (adjustment == null) {
|
|
|
|
|
+ return 0.0;
|
|
|
|
|
+ }
|
|
|
|
|
+ if ("blocked".equalsIgnoreCase(adjustment)) {
|
|
|
|
|
+ return -50.0;
|
|
|
|
|
+ }
|
|
|
|
|
+ if ("warning".equalsIgnoreCase(adjustment)) {
|
|
|
|
|
+ return -15.0;
|
|
|
|
|
+ }
|
|
|
|
|
+ if ("boost".equalsIgnoreCase(adjustment)) {
|
|
|
|
|
+ return 10.0;
|
|
|
|
|
+ }
|
|
|
|
|
+ // normal 及其他未知标记统一视为无调整
|
|
|
|
|
+ return 0.0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 截断到 [-100, 100](§4.2 / G-2)。 */
|
|
|
|
|
+ public static double clamp(double rawScore) {
|
|
|
|
|
+ if (rawScore > SCORE_MAX) {
|
|
|
|
|
+ return SCORE_MAX;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (rawScore < SCORE_MIN) {
|
|
|
|
|
+ return SCORE_MIN;
|
|
|
|
|
+ }
|
|
|
|
|
+ return rawScore;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 按显式权重表精确计算 §4.2 六因子加权分并 clamp。
|
|
|
|
|
+ * 权重缺失的因子按 0 处理(规则表尚未配置该因子时保持中性)。
|
|
|
|
|
+ */
|
|
|
|
|
+ public static double finalScoreWithWeights(Map<String, Double> weights,
|
|
|
|
|
+ double baseReportScore,
|
|
|
|
|
+ double healthAdjustScore,
|
|
|
|
|
+ double preferenceBoost,
|
|
|
|
|
+ double pairingEffect,
|
|
|
|
|
+ double processingEffect,
|
|
|
|
|
+ double diseaseGroupAdjust) {
|
|
|
|
|
+ double raw = baseReportScore * weightOf(weights, FACTOR_BASE_REPORT)
|
|
|
|
|
+ + healthAdjustScore * weightOf(weights, FACTOR_HEALTH_ADJUST)
|
|
|
|
|
+ + preferenceBoost * weightOf(weights, FACTOR_PREFERENCE)
|
|
|
|
|
+ + pairingEffect * weightOf(weights, FACTOR_PAIRING)
|
|
|
|
|
+ + processingEffect * weightOf(weights, FACTOR_PROCESSING)
|
|
|
|
|
+ + diseaseGroupAdjust * weightOf(weights, FACTOR_DISEASE_GROUP);
|
|
|
|
|
+ return clamp(raw);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 默认权重路径:等价于 {@code finalScoreWithWeights(defaultWeights(), ...)}(§4.2 / G-2)。 */
|
|
|
|
|
+ public static double finalScore(double baseReportScore,
|
|
|
|
|
+ double healthAdjustScore,
|
|
|
|
|
+ double preferenceBoost,
|
|
|
|
|
+ double pairingEffect,
|
|
|
|
|
+ double processingEffect,
|
|
|
|
|
+ double diseaseGroupAdjust) {
|
|
|
|
|
+ return finalScoreWithWeights(defaultWeights(), baseReportScore, healthAdjustScore,
|
|
|
|
|
+ preferenceBoost, pairingEffect, processingEffect, diseaseGroupAdjust);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static double weightOf(Map<String, Double> weights, String factorType) {
|
|
|
|
|
+ if (weights == null) {
|
|
|
|
|
+ return 0.0;
|
|
|
|
|
+ }
|
|
|
|
|
+ Double w = weights.get(factorType);
|
|
|
|
|
+ return w == null ? 0.0 : w.doubleValue();
|
|
|
|
|
+ }
|
|
|
|
|
+}
|