DawnC commited on
Commit
deab3f4
1 Parent(s): 94d53d5

Update scoring_calculation_system.py

Browse files
Files changed (1) hide show
  1. scoring_calculation_system.py +135 -19
scoring_calculation_system.py CHANGED
@@ -2133,38 +2133,154 @@ def calculate_breed_compatibility_score(scores: dict, user_prefs: UserPreference
2133
  final_score = score * severity_multiplier
2134
  return max(0.2, min(1.0, final_score))
2135
 
2136
- # 評估完美匹配條件
2137
- perfect_conditions = evaluate_perfect_conditions()
2138
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2139
  # 計算動態權重
2140
  weights = calculate_weights()
2141
 
2142
- # 正規化權重確保總和為1
2143
  total_weight = sum(weights.values())
2144
  normalized_weights = {k: v/total_weight for k, v in weights.items()}
2145
 
2146
  # 計算基礎分數
2147
- base_score = sum(scores[k] * normalized_weights[k] for k in scores.keys())
2148
 
2149
- # 計算完美匹配獎勵(降低獎勵影響以避免過高分數)
2150
- perfect_bonus = 1.0
2151
- perfect_bonus += 0.10 * perfect_conditions['size_match'] # 降低單項獎勵
2152
- perfect_bonus += 0.10 * perfect_conditions['exercise_match']
2153
- perfect_bonus += 0.10 * perfect_conditions['experience_match']
2154
- perfect_bonus += 0.05 * perfect_conditions['living_condition_match']
2155
- perfect_bonus += 0.05 * perfect_conditions['breed_trait_match'] # 新增品種特性獎勵
2156
 
2157
- # 計算品種特性加成(使用更嚴格的係數)
2158
- breed_bonus = calculate_breed_bonus(breed_info, user_prefs) * 0.15 # 降低品種加成的影響
 
2159
 
2160
  # 計算初步分數
2161
- initial_score = (base_score * 0.85 + breed_bonus * 0.15) * perfect_bonus
 
 
 
2162
 
2163
- # 應用特殊情況調整
2164
- final_score = apply_special_case_adjustments(initial_score)
 
2165
 
2166
- # 確保最終分數在有效範圍內
2167
- return min(1.0, max(0.3, final_score))
2168
 
2169
 
2170
  def amplify_score_extreme(score: float) -> float:
 
2133
  final_score = score * severity_multiplier
2134
  return max(0.2, min(1.0, final_score))
2135
 
2136
+ def calculate_base_score(scores: dict, weights: dict) -> float:
2137
+ """
2138
+ 計算基礎分數,加入更嚴格的評估機制。
2139
+ 就像學校的評分系統,某些科目不及格會嚴重影響總成績。
2140
+ """
2141
+ # 檢查關鍵指標是否有嚴重不足
2142
+ critical_thresholds = {
2143
+ 'space': 0.7,
2144
+ 'exercise': 0.7,
2145
+ 'experience': 0.7,
2146
+ 'noise': 0.65
2147
+ }
2148
+
2149
+ critical_failures = []
2150
+ for metric, threshold in critical_thresholds.items():
2151
+ if scores[metric] < threshold:
2152
+ critical_failures.append((metric, scores[metric]))
2153
+
2154
+ # 計算基礎加權分數
2155
+ base_score = sum(scores[k] * weights[k] for k in scores.keys())
2156
+
2157
+ # 根據關鍵指標的不足程度進行懲罰
2158
+ if critical_failures:
2159
+ # 計算最嚴重的不足程度
2160
+ worst_failure = min(score for _, score in critical_failures)
2161
+ penalty = (critical_thresholds['space'] - worst_failure) * 0.6
2162
+ base_score *= (1 - penalty)
2163
+
2164
+ # 多個指標不足時的額外懲罰
2165
+ if len(critical_failures) > 1:
2166
+ base_score *= (0.9 ** (len(critical_failures) - 1))
2167
+
2168
+ return base_score
2169
+
2170
+ def evaluate_condition_interactions(scores: dict) -> float:
2171
+ """
2172
+ 評估不同條件之間的相互影響。
2173
+ 就像運動訓練中,不同因素之間的配合度會影響整體效果。
2174
+ """
2175
+ interaction_penalty = 1.0
2176
+
2177
+ # 居住空間與運動需求的互動
2178
+ if user_prefs.living_space == 'house_small':
2179
+ if user_prefs.exercise_time > 120:
2180
+ interaction_penalty *= 0.85
2181
+ elif user_prefs.exercise_time > 90:
2182
+ interaction_penalty *= 0.9
2183
+
2184
+ # 經驗等級與其他因素的互動
2185
+ if user_prefs.experience_level == 'beginner':
2186
+ if breed_info.get('Care Level') == 'HIGH':
2187
+ interaction_penalty *= 0.8
2188
+ if user_prefs.exercise_time > 150:
2189
+ interaction_penalty *= 0.85
2190
+ if breed_info.get('Exercise Needs', 'MODERATE').upper() == 'VERY HIGH':
2191
+ interaction_penalty *= 0.85
2192
+
2193
+ # 空間限制與品種大小的互動
2194
+ if user_prefs.living_space != 'house_large':
2195
+ if breed_info['Size'] in ['Large', 'Giant']:
2196
+ interaction_penalty *= 0.8
2197
+
2198
+ # 運動時間與類型的互動
2199
+ exercise_needs = breed_info.get('Exercise Needs', 'MODERATE').upper()
2200
+ if exercise_needs == 'VERY HIGH' and user_prefs.exercise_type == 'light_walks':
2201
+ interaction_penalty *= 0.85
2202
+
2203
+ return interaction_penalty
2204
+
2205
+ def calculate_adjusted_perfect_bonus(perfect_conditions: dict) -> float:
2206
+ """
2207
+ 計算完美匹配獎勵,但更注重條件的整體表現。
2208
+ 就像全能運動員的評分,需要在各個項目都表現出色。
2209
+ """
2210
+ bonus = 1.0
2211
+
2212
+ # 降低單項獎勵的影響力
2213
+ bonus += 0.06 * perfect_conditions['size_match']
2214
+ bonus += 0.06 * perfect_conditions['exercise_match']
2215
+ bonus += 0.06 * perfect_conditions['experience_match']
2216
+ bonus += 0.03 * perfect_conditions['living_condition_match']
2217
+
2218
+ # 如果有任何條件表現不佳,降低整體獎勵
2219
+ low_scores = [score for score in perfect_conditions.values() if score < 0.6]
2220
+ if low_scores:
2221
+ bonus *= (0.85 ** len(low_scores))
2222
+
2223
+ # 確保獎勵不會過高
2224
+ return min(1.25, bonus)
2225
+
2226
+ def apply_breed_specific_adjustments(score: float) -> float:
2227
+ """
2228
+ 根據品種特性進行最終調整。
2229
+ 考慮品種的特殊性質和限制因素。
2230
+ """
2231
+ # 檢查是否存在極端不匹配的情況
2232
+ exercise_mismatch = False
2233
+ size_mismatch = False
2234
+ experience_mismatch = False
2235
+
2236
+ # 運動需求極端不匹配
2237
+ if breed_info.get('Exercise Needs', 'MODERATE').upper() == 'VERY HIGH':
2238
+ if user_prefs.exercise_time < 90 or user_prefs.exercise_type == 'light_walks':
2239
+ exercise_mismatch = True
2240
+
2241
+ # 體型與空間極端不匹配
2242
+ if user_prefs.living_space == 'apartment' and breed_info['Size'] in ['Large', 'Giant']:
2243
+ size_mismatch = True
2244
+
2245
+ # 經驗需求極端不匹配
2246
+ if user_prefs.experience_level == 'beginner' and breed_info.get('Care Level') == 'HIGH':
2247
+ experience_mismatch = True
2248
+
2249
+ # 根據不匹配的數量進行懲罰
2250
+ mismatch_count = sum([exercise_mismatch, size_mismatch, experience_mismatch])
2251
+ if mismatch_count > 0:
2252
+ score *= (0.8 ** mismatch_count)
2253
+
2254
+ return score
2255
+
2256
  # 計算動態權重
2257
  weights = calculate_weights()
2258
 
2259
+ # 正規化權重
2260
  total_weight = sum(weights.values())
2261
  normalized_weights = {k: v/total_weight for k, v in weights.items()}
2262
 
2263
  # 計算基礎分數
2264
+ base_score = calculate_base_score(scores, normalized_weights)
2265
 
2266
+ # 評估條件互動
2267
+ interaction_multiplier = evaluate_condition_interactions(scores)
 
 
 
 
 
2268
 
2269
+ # 計算完美匹配獎勵
2270
+ perfect_conditions = evaluate_perfect_conditions()
2271
+ perfect_bonus = calculate_adjusted_perfect_bonus(perfect_conditions)
2272
 
2273
  # 計算初步分數
2274
+ preliminary_score = base_score * interaction_multiplier * perfect_bonus
2275
+
2276
+ # 應用品種特定調整
2277
+ final_score = apply_breed_specific_adjustments(preliminary_score)
2278
 
2279
+ # 確保分數在合理範圍內,並降低最高可能分數
2280
+ max_possible_score = 0.96 # 降低最高可能分數
2281
+ min_possible_score = 0.3
2282
 
2283
+ return min(max_possible_score, max(min_possible_score, final_score))
 
2284
 
2285
 
2286
  def amplify_score_extreme(score: float) -> float: