DawnC commited on
Commit
e6e4287
1 Parent(s): 1cceba1

Update scoring_calculation_system.py

Browse files
Files changed (1) hide show
  1. scoring_calculation_system.py +102 -171
scoring_calculation_system.py CHANGED
@@ -1510,193 +1510,124 @@ def calculate_environmental_fit(breed_info: dict, user_prefs: UserPreferences) -
1510
 
1511
  def calculate_breed_compatibility_score(scores: dict, user_prefs: UserPreferences, breed_info: dict) -> float:
1512
  """
1513
- 改進的品種相容性評分系統,著重於:
1514
- 1. 強化參數變化的影響力
1515
- 2. 提高品種差異化
1516
- 3. 更精確的條件匹配評估
1517
  """
1518
- # 1. 基礎特徵評估 - 更極端的調整
1519
- def evaluate_feature_score(feature: str) -> float:
1520
- score = scores[feature]
 
 
 
 
 
 
 
 
1521
 
1522
- if feature == 'exercise':
1523
- exercise_needs = breed_info.get('Exercise Needs', 'MODERATE').upper()
1524
- exercise_time = user_prefs.exercise_time
1525
-
1526
- if exercise_needs == 'VERY HIGH':
1527
- if exercise_time < 120:
1528
- return score * 0.4 # 嚴重懲罰運動不足
1529
- elif exercise_time > 150:
1530
- return min(1.0, score * 1.5) # 顯著獎勵充足運動
1531
- elif exercise_needs == 'LOW' and exercise_time > 120:
1532
- return score * 0.6 # 懲罰過度運動
1533
-
1534
- elif feature == 'space':
1535
- size = breed_info['Size']
1536
- if user_prefs.living_space == 'apartment':
1537
- if size in ['Large', 'Giant']:
1538
- return score * 0.3 # 更嚴格的空間限制
1539
- elif size == 'Small':
1540
- return min(1.0, score * 1.4) # 更高的小型犬獎勵
1541
-
1542
- elif feature == 'experience':
1543
- care_level = breed_info.get('Care Level', 'MODERATE')
1544
- if care_level == 'High' and user_prefs.experience_level == 'beginner':
1545
- return score * 0.4
1546
- elif care_level == 'Low' and user_prefs.experience_level == 'advanced':
1547
- return score * 0.8 # 略微降低過度資格
1548
 
1549
- return score
 
 
 
 
1550
 
1551
- # 2. 計算調整後的分數
1552
  adjusted_scores = {
1553
- feature: evaluate_feature_score(feature)
1554
- for feature in scores.keys()
 
 
 
 
1555
  }
1556
 
1557
- # 3. 動態權重分配 - 根據條件強化關鍵特徵
1558
- def calculate_feature_weight(feature: str) -> float:
1559
- base_weights = {
1560
- 'space': 0.35,
1561
- 'exercise': 0.30,
1562
- 'experience': 0.20,
1563
- 'grooming': 0.15,
1564
- 'health': 0.10,
1565
- 'noise': 0.10
1566
- }
1567
-
1568
- weight = base_weights[feature]
1569
-
1570
- # 條件相關的權重調整
1571
- if feature == 'space' and user_prefs.living_space == 'apartment':
1572
- weight *= 1.6 # 加強空間限制的影響
1573
-
1574
- elif feature == 'exercise':
1575
- if user_prefs.exercise_time > 150:
1576
- weight *= 1.4
1577
- elif user_prefs.exercise_time < 60:
1578
- weight *= 1.3
1579
-
1580
- elif feature == 'experience':
1581
- if user_prefs.experience_level in ['beginner', 'advanced']:
1582
- weight *= 1.3 # 強化極端經驗等級的影響
1583
-
1584
- return weight
1585
 
1586
- # 4. 計算加權分數
1587
- weights = {feature: calculate_feature_weight(feature)
1588
- for feature in scores.keys()}
 
1589
 
 
 
 
1590
  # 正規化權重
1591
  total_weight = sum(weights.values())
1592
  normalized_weights = {k: v/total_weight for k, v in weights.items()}
1593
-
1594
- # 5. 計算基礎分數 - 分離主要和次要特徵
1595
- primary_features = {'space', 'exercise', 'experience'}
1596
- secondary_features = set(scores.keys()) - primary_features
1597
-
1598
- primary_score = sum(adjusted_scores[f] * normalized_weights[f]
1599
- for f in primary_features)
1600
- secondary_score = sum(adjusted_scores[f] * normalized_weights[f]
1601
- for f in secondary_features)
1602
-
1603
- # 6. 特殊條件評估
1604
- condition_multiplier = 1.0
1605
-
1606
- # 空間條件
1607
- if user_prefs.living_space == 'apartment':
1608
- if breed_info['Size'] in ['Large', 'Giant']:
1609
- condition_multiplier *= 0.7
1610
- elif breed_info['Size'] == 'Small':
1611
- condition_multiplier *= 1.2
1612
-
1613
- # 運動條件
1614
- exercise_needs = breed_info.get('Exercise Needs', 'MODERATE').upper()
1615
- if exercise_needs == 'VERY HIGH' and user_prefs.exercise_time < 120:
1616
- condition_multiplier *= 0.8
1617
- elif exercise_needs == 'LOW' and user_prefs.exercise_time > 150:
1618
- condition_multiplier *= 0.85
1619
-
1620
- # 7. 計算最終分數
1621
- base_score = (primary_score * 0.7 + secondary_score * 0.3)
1622
  breed_bonus = calculate_breed_bonus(breed_info, user_prefs)
1623
 
1624
- final_score = (base_score * condition_multiplier * 0.8) + (breed_bonus * 0.2)
1625
-
1626
- return max(0.0, min(1.0, final_score))
1627
 
1628
 
1629
  def amplify_score_extreme(score: float) -> float:
1630
  """
1631
- 優化的分數映射函數
1632
-
1633
- 主要改進:
1634
- 1. 更細緻的分數區間劃分,讓分數轉換更平滑
1635
- 2. 根據不同分數段使用不同的轉換曲線
1636
- 3. 確保極端分數能得到更極端的映射
1637
-
1638
- 分數區間的設計邏輯:
1639
- - 極差匹配 (0.0-0.2): 60-63% - 快速的線性增長
1640
- - 差匹配 (0.2-0.4): 63-68% - 緩慢的增長
1641
- - 中等匹配 (0.4-0.6): 68-75% - 穩定的線性增長
1642
- - 良好匹配 (0.6-0.75): 75-85% - 加速增長
1643
- - 優秀匹配 (0.75-0.9): 85-92% - 減速增長
1644
- - 完美匹配 (0.9-1.0): 92-95% - 非常緩慢的增長
1645
  """
1646
- ranges = {
1647
- 'very_poor': {
1648
- 'range': (0.0, 0.2),
1649
- 'out_min': 0.60,
1650
- 'out_max': 0.63,
1651
- 'curve': 1.0 # 線性
1652
- },
1653
- 'poor': {
1654
- 'range': (0.2, 0.4),
1655
- 'out_min': 0.63,
1656
- 'out_max': 0.68,
1657
- 'curve': 1.2 # 稍微非線性
1658
- },
1659
- 'mediocre': {
1660
- 'range': (0.4, 0.6),
1661
- 'out_min': 0.68,
1662
- 'out_max': 0.75,
1663
- 'curve': 1.0 # 線性
1664
- },
1665
- 'good': {
1666
- 'range': (0.6, 0.75),
1667
- 'out_min': 0.75,
1668
- 'out_max': 0.85,
1669
- 'curve': 0.8 # 加速增長
1670
- },
1671
- 'excellent': {
1672
- 'range': (0.75, 0.9),
1673
- 'out_min': 0.85,
1674
- 'out_max': 0.92,
1675
- 'curve': 1.2 # 減速增長
1676
- },
1677
- 'perfect': {
1678
- 'range': (0.9, 1.0),
1679
- 'out_min': 0.92,
1680
- 'out_max': 0.95,
1681
- 'curve': 1.5 # 強烈的減速
1682
- }
1683
- }
1684
-
1685
- # 找出分數所屬區間並進行映射
1686
- for config in ranges.values():
1687
- range_min, range_max = config['range']
1688
- if range_min <= score <= range_max:
1689
- # 計算區間內的相對位置(0-1)
1690
- position = (score - range_min) / (range_max - range_min)
1691
-
1692
- # 應用非線性曲線來調整增長速度
1693
- position = pow(position, config['curve'])
1694
-
1695
- # 映射到輸出範圍
1696
- result = config['out_min'] + (config['out_max'] - config['out_min']) * position
1697
-
1698
- # 確保結果精確到小數點後三位
1699
- return round(result, 3)
1700
-
1701
- # 處理超出範圍的情況
1702
- return 0.60 if score < 0.0 else 0.95
 
1510
 
1511
  def calculate_breed_compatibility_score(scores: dict, user_prefs: UserPreferences, breed_info: dict) -> float:
1512
  """
1513
+ 改進的品種相容性評分系統
1514
+ 通過更細緻的特徵評估和動態權重調整,自然產生分數差異
 
 
1515
  """
1516
+ # 評估關鍵特徵的匹配度,使用更極端的調整係數
1517
+ def evaluate_key_features():
1518
+ # 空間適配性評估
1519
+ space_multiplier = 1.0
1520
+ if user_prefs.living_space == 'apartment':
1521
+ if breed_info['Size'] == 'Giant':
1522
+ space_multiplier = 0.3 # 嚴重不適合
1523
+ elif breed_info['Size'] == 'Large':
1524
+ space_multiplier = 0.4 # 明顯不適合
1525
+ elif breed_info['Size'] == 'Small':
1526
+ space_multiplier = 1.4 # 明顯優勢
1527
 
1528
+ # 運動需求評估
1529
+ exercise_multiplier = 1.0
1530
+ exercise_needs = breed_info.get('Exercise Needs', 'MODERATE').upper()
1531
+ if exercise_needs == 'VERY HIGH':
1532
+ if user_prefs.exercise_time < 60:
1533
+ exercise_multiplier = 0.3 # 嚴重不足
1534
+ elif user_prefs.exercise_time > 150:
1535
+ exercise_multiplier = 1.5 # 完美匹配
1536
+ elif exercise_needs == 'LOW' and user_prefs.exercise_time > 150:
1537
+ exercise_multiplier = 0.5 # 運動過度
1538
+
1539
+ return space_multiplier, exercise_multiplier
1540
+
1541
+ # 計算經驗匹配度
1542
+ def evaluate_experience():
1543
+ exp_multiplier = 1.0
1544
+ care_level = breed_info.get('Care Level', 'MODERATE')
1545
+
1546
+ if care_level == 'High':
1547
+ if user_prefs.experience_level == 'beginner':
1548
+ exp_multiplier = 0.4
1549
+ elif user_prefs.experience_level == 'advanced':
1550
+ exp_multiplier = 1.3
1551
+ elif care_level == 'Low':
1552
+ if user_prefs.experience_level == 'advanced':
1553
+ exp_multiplier = 0.9 # 略微降低評分,因為可能不夠有挑戰性
1554
 
1555
+ return exp_multiplier
1556
+
1557
+ # 取得特徵調整係數
1558
+ space_mult, exercise_mult = evaluate_key_features()
1559
+ exp_mult = evaluate_experience()
1560
 
1561
+ # 調整基礎分數
1562
  adjusted_scores = {
1563
+ 'space': scores['space'] * space_mult,
1564
+ 'exercise': scores['exercise'] * exercise_mult,
1565
+ 'experience': scores['experience'] * exp_mult,
1566
+ 'grooming': scores['grooming'],
1567
+ 'health': scores['health'],
1568
+ 'noise': scores['noise']
1569
  }
1570
 
1571
+ # 計算加權平均,關鍵特徵佔更大權重
1572
+ weights = {
1573
+ 'space': 0.35,
1574
+ 'exercise': 0.30,
1575
+ 'experience': 0.20,
1576
+ 'grooming': 0.15,
1577
+ 'health': 0.10,
1578
+ 'noise': 0.10
1579
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1580
 
1581
+ # 動態調整權重
1582
+ if user_prefs.living_space == 'apartment':
1583
+ weights['space'] *= 1.5
1584
+ weights['noise'] *= 1.3
1585
 
1586
+ if abs(user_prefs.exercise_time - 120) > 60: # 運動時間極端情況
1587
+ weights['exercise'] *= 1.4
1588
+
1589
  # 正規化權重
1590
  total_weight = sum(weights.values())
1591
  normalized_weights = {k: v/total_weight for k, v in weights.items()}
1592
+
1593
+ # 計算最終分數
1594
+ final_score = sum(adjusted_scores[k] * normalized_weights[k] for k in scores.keys())
1595
+
1596
+ # 品種特性加成
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1597
  breed_bonus = calculate_breed_bonus(breed_info, user_prefs)
1598
 
1599
+ # 整合最終分數,保持在0-1範圍內
1600
+ return min(1.0, max(0.0, (final_score * 0.85) + (breed_bonus * 0.15)))
 
1601
 
1602
 
1603
  def amplify_score_extreme(score: float) -> float:
1604
  """
1605
+ 改進的分數轉換函數
1606
+ 提供更大的分數範圍和更明顯的差異
1607
+
1608
+ 轉換邏輯:
1609
+ - 極差匹配 (0.0-0.3) -> 60-68%
1610
+ - 較差匹配 (0.3-0.5) -> 68-75%
1611
+ - 中等匹配 (0.5-0.7) -> 75-85%
1612
+ - 良好匹配 (0.7-0.85) -> 85-92%
1613
+ - 優秀匹配 (0.85-1.0) -> 92-95%
 
 
 
 
 
1614
  """
1615
+ if score < 0.3:
1616
+ # 極差匹配:快速線性增長
1617
+ return 0.60 + (score / 0.3) * 0.08
1618
+ elif score < 0.5:
1619
+ # 較差匹配:緩慢增長
1620
+ position = (score - 0.3) / 0.2
1621
+ return 0.68 + position * 0.07
1622
+ elif score < 0.7:
1623
+ # 中等匹配:穩定線性增長
1624
+ position = (score - 0.5) / 0.2
1625
+ return 0.75 + position * 0.10
1626
+ elif score < 0.85:
1627
+ # 良好匹配:加速增長
1628
+ position = (score - 0.7) / 0.15
1629
+ return 0.85 + position * 0.07
1630
+ else:
1631
+ # 優秀匹配:最後衝刺
1632
+ position = (score - 0.85) / 0.15
1633
+ return 0.92 + position * 0.03