Spaces:
Running
on
Zero
Running
on
Zero
Update scoring_calculation_system.py
Browse files- 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 |
-
|
1515 |
-
2. 提高品種差異化
|
1516 |
-
3. 更精確的條件匹配評估
|
1517 |
"""
|
1518 |
-
#
|
1519 |
-
def
|
1520 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1521 |
|
1522 |
-
|
1523 |
-
|
1524 |
-
|
1525 |
-
|
1526 |
-
if
|
1527 |
-
|
1528 |
-
|
1529 |
-
|
1530 |
-
|
1531 |
-
|
1532 |
-
|
1533 |
-
|
1534 |
-
|
1535 |
-
|
1536 |
-
|
1537 |
-
|
1538 |
-
|
1539 |
-
|
1540 |
-
|
1541 |
-
|
1542 |
-
|
1543 |
-
|
1544 |
-
|
1545 |
-
|
1546 |
-
|
1547 |
-
|
1548 |
|
1549 |
-
return
|
|
|
|
|
|
|
|
|
1550 |
|
1551 |
-
#
|
1552 |
adjusted_scores = {
|
1553 |
-
|
1554 |
-
|
|
|
|
|
|
|
|
|
1555 |
}
|
1556 |
|
1557 |
-
#
|
1558 |
-
|
1559 |
-
|
1560 |
-
|
1561 |
-
|
1562 |
-
|
1563 |
-
|
1564 |
-
|
1565 |
-
|
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 |
-
#
|
1587 |
-
|
1588 |
-
|
|
|
1589 |
|
|
|
|
|
|
|
1590 |
# 正規化權重
|
1591 |
total_weight = sum(weights.values())
|
1592 |
normalized_weights = {k: v/total_weight for k, v in weights.items()}
|
1593 |
-
|
1594 |
-
#
|
1595 |
-
|
1596 |
-
|
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 |
-
|
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 |
-
|
1635 |
-
|
1636 |
-
3.
|
1637 |
-
|
1638 |
-
|
1639 |
-
-
|
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 |
-
|
1647 |
-
|
1648 |
-
|
1649 |
-
|
1650 |
-
|
1651 |
-
|
1652 |
-
|
1653 |
-
|
1654 |
-
|
1655 |
-
|
1656 |
-
|
1657 |
-
|
1658 |
-
|
1659 |
-
|
1660 |
-
|
1661 |
-
|
1662 |
-
|
1663 |
-
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|