Spaces:
Running
on
Zero
Running
on
Zero
Update scoring_calculation_system.py
Browse files- scoring_calculation_system.py +77 -49
scoring_calculation_system.py
CHANGED
@@ -1518,22 +1518,22 @@ def calculate_breed_compatibility_score(scores: dict, user_prefs: UserPreference
|
|
1518 |
3. 更嚴格的不適配懲罰
|
1519 |
4. 非線性分數調整
|
1520 |
"""
|
1521 |
-
#
|
1522 |
critical_params = {
|
1523 |
'space': {
|
1524 |
-
'threshold': 0.35,
|
1525 |
'conditions': lambda p: True,
|
1526 |
-
'penalty': 0.
|
1527 |
},
|
1528 |
'noise': {
|
1529 |
'threshold': 0.35,
|
1530 |
'conditions': lambda p: p.living_space == 'apartment',
|
1531 |
-
'penalty': 0.
|
1532 |
},
|
1533 |
'experience': {
|
1534 |
'threshold': 0.35,
|
1535 |
'conditions': lambda p: p.experience_level == 'beginner',
|
1536 |
-
'penalty': 0.
|
1537 |
}
|
1538 |
}
|
1539 |
|
@@ -1542,66 +1542,95 @@ def calculate_breed_compatibility_score(scores: dict, user_prefs: UserPreference
|
|
1542 |
if scores[param] < config['threshold'] and config['conditions'](user_prefs):
|
1543 |
return config['penalty']
|
1544 |
|
1545 |
-
#
|
1546 |
base_weights = {
|
1547 |
-
'space': 0.
|
1548 |
-
'exercise': 0.
|
1549 |
-
'experience': 0.
|
1550 |
'grooming': 0.15,
|
1551 |
'health': 0.10,
|
1552 |
'noise': 0.10
|
1553 |
}
|
1554 |
|
1555 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1556 |
adjusted_weights = {}
|
1557 |
for param, weight in base_weights.items():
|
1558 |
multiplier = 1.0
|
1559 |
|
1560 |
-
#
|
1561 |
if param == 'space':
|
1562 |
if user_prefs.living_space == 'apartment':
|
1563 |
-
|
1564 |
-
|
1565 |
-
|
1566 |
-
|
|
|
|
|
|
|
|
|
1567 |
# 運動需求調整
|
1568 |
elif param == 'exercise':
|
1569 |
-
|
1570 |
-
|
1571 |
-
|
1572 |
-
|
1573 |
-
|
|
|
|
|
|
|
|
|
1574 |
# 經驗需求調整
|
1575 |
elif param == 'experience':
|
1576 |
-
if
|
1577 |
-
|
1578 |
-
|
1579 |
-
|
1580 |
-
|
|
|
1581 |
adjusted_weights[param] = weight * multiplier
|
1582 |
|
1583 |
# 重新正規化權重
|
1584 |
total_weight = sum(adjusted_weights.values())
|
1585 |
normalized_weights = {k: v/total_weight for k, v in adjusted_weights.items()}
|
1586 |
|
1587 |
-
#
|
1588 |
-
|
1589 |
for param, weight in normalized_weights.items():
|
1590 |
-
|
1591 |
-
|
1592 |
-
|
1593 |
-
|
1594 |
-
|
1595 |
-
|
1596 |
-
|
1597 |
-
|
1598 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1599 |
|
1600 |
-
#
|
1601 |
breed_bonus = calculate_breed_bonus(breed_info, user_prefs)
|
1602 |
|
1603 |
-
#
|
1604 |
-
final_score = (
|
1605 |
|
1606 |
return final_score
|
1607 |
|
@@ -1621,23 +1650,23 @@ def amplify_score_extreme(score: float) -> float:
|
|
1621 |
ranges = {
|
1622 |
'poor': {
|
1623 |
'range': (0.0, 0.3),
|
1624 |
-
'out_min':
|
1625 |
-
'out_max':
|
1626 |
},
|
1627 |
'mediocre': {
|
1628 |
'range': (0.3, 0.6),
|
1629 |
-
'out_min':
|
1630 |
-
'out_max':
|
1631 |
},
|
1632 |
'good': {
|
1633 |
'range': (0.6, 0.8),
|
1634 |
-
'out_min':
|
1635 |
-
'out_max':
|
1636 |
},
|
1637 |
'excellent': {
|
1638 |
'range': (0.8, 1.0),
|
1639 |
-
'out_min':
|
1640 |
-
'out_max': 95
|
1641 |
}
|
1642 |
}
|
1643 |
|
@@ -1645,7 +1674,6 @@ def amplify_score_extreme(score: float) -> float:
|
|
1645 |
for config in ranges.values():
|
1646 |
range_min, range_max = config['range']
|
1647 |
if range_min <= score <= range_max:
|
1648 |
-
# 計算在當前區間的相對位置(0-1)
|
1649 |
position = (score - range_min) / (range_max - range_min)
|
1650 |
|
1651 |
# 線性映射到目標範圍
|
@@ -1655,4 +1683,4 @@ def amplify_score_extreme(score: float) -> float:
|
|
1655 |
return round(result, 1)
|
1656 |
|
1657 |
# 處理超出範圍的情況
|
1658 |
-
return
|
|
|
1518 |
3. 更嚴格的不適配懲罰
|
1519 |
4. 非線性分數調整
|
1520 |
"""
|
1521 |
+
# 關鍵不適配參數檢查 - 加強懲罰機制
|
1522 |
critical_params = {
|
1523 |
'space': {
|
1524 |
+
'threshold': 0.35,
|
1525 |
'conditions': lambda p: True,
|
1526 |
+
'penalty': 0.3
|
1527 |
},
|
1528 |
'noise': {
|
1529 |
'threshold': 0.35,
|
1530 |
'conditions': lambda p: p.living_space == 'apartment',
|
1531 |
+
'penalty': 0.35
|
1532 |
},
|
1533 |
'experience': {
|
1534 |
'threshold': 0.35,
|
1535 |
'conditions': lambda p: p.experience_level == 'beginner',
|
1536 |
+
'penalty': 0.35
|
1537 |
}
|
1538 |
}
|
1539 |
|
|
|
1542 |
if scores[param] < config['threshold'] and config['conditions'](user_prefs):
|
1543 |
return config['penalty']
|
1544 |
|
1545 |
+
# 基礎權重設定
|
1546 |
base_weights = {
|
1547 |
+
'space': 0.35,
|
1548 |
+
'exercise': 0.30,
|
1549 |
+
'experience': 0.20,
|
1550 |
'grooming': 0.15,
|
1551 |
'health': 0.10,
|
1552 |
'noise': 0.10
|
1553 |
}
|
1554 |
|
1555 |
+
# 計算各項指標的調整後分數
|
1556 |
+
adjusted_scores = {}
|
1557 |
+
for param, score in scores.items():
|
1558 |
+
# 基礎分數調整:優秀匹配得到更高分數
|
1559 |
+
if score > 0.8:
|
1560 |
+
adjusted_scores[param] = min(1.0, score * 1.3) # 優秀匹配額外加分
|
1561 |
+
elif score < 0.4:
|
1562 |
+
adjusted_scores[param] = score * 0.7 # 較差匹配更大懲罰
|
1563 |
+
else:
|
1564 |
+
adjusted_scores[param] = score
|
1565 |
+
|
1566 |
+
# 權重動態調整
|
1567 |
adjusted_weights = {}
|
1568 |
for param, weight in base_weights.items():
|
1569 |
multiplier = 1.0
|
1570 |
|
1571 |
+
# 空間適配性調整
|
1572 |
if param == 'space':
|
1573 |
if user_prefs.living_space == 'apartment':
|
1574 |
+
if breed_info['Size'] in ['Large', 'Giant']:
|
1575 |
+
multiplier *= 0.5 # 大型犬在公寓極不適合
|
1576 |
+
elif breed_info['Size'] == 'Small':
|
1577 |
+
multiplier *= 1.4 # 小型犬在公寓更合適
|
1578 |
+
elif user_prefs.living_space == 'house_large':
|
1579 |
+
if breed_info['Size'] in ['Large', 'Giant']:
|
1580 |
+
multiplier *= 1.3 # 大型犬在大房子更合適
|
1581 |
+
|
1582 |
# 運動需求調整
|
1583 |
elif param == 'exercise':
|
1584 |
+
exercise_needs = breed_info.get('Exercise Needs', 'Moderate').upper()
|
1585 |
+
if exercise_needs == 'VERY HIGH':
|
1586 |
+
if user_prefs.exercise_time > 150:
|
1587 |
+
multiplier *= 1.4 # 高運動量需求與高運動時間匹配
|
1588 |
+
elif user_prefs.exercise_time < 60:
|
1589 |
+
multiplier *= 0.6 # 運動時間嚴重不足
|
1590 |
+
elif exercise_needs == 'LOW' and user_prefs.exercise_time > 120:
|
1591 |
+
multiplier *= 0.8 # 過度運動對低運動需求品種不利
|
1592 |
+
|
1593 |
# 經驗需求調整
|
1594 |
elif param == 'experience':
|
1595 |
+
if breed_info.get('Care Level') == 'High':
|
1596 |
+
if user_prefs.experience_level == 'beginner':
|
1597 |
+
multiplier *= 0.6
|
1598 |
+
elif user_prefs.experience_level == 'advanced':
|
1599 |
+
multiplier *= 1.3
|
1600 |
+
|
1601 |
adjusted_weights[param] = weight * multiplier
|
1602 |
|
1603 |
# 重新正規化權重
|
1604 |
total_weight = sum(adjusted_weights.values())
|
1605 |
normalized_weights = {k: v/total_weight for k, v in adjusted_weights.items()}
|
1606 |
|
1607 |
+
# 計算加權分數
|
1608 |
+
weighted_scores = {}
|
1609 |
for param, weight in normalized_weights.items():
|
1610 |
+
weighted_scores[param] = adjusted_scores[param] * weight
|
1611 |
+
|
1612 |
+
# 分開計算主要參數和次要參數
|
1613 |
+
primary_params = {'space', 'exercise', 'experience'}
|
1614 |
+
primary_score = sum(weighted_scores[p] for p in primary_params) / sum(normalized_weights[p] for p in primary_params)
|
1615 |
+
secondary_score = sum(weighted_scores[p] for p in weighted_scores if p not in primary_params) / \
|
1616 |
+
sum(normalized_weights[p] for p in normalized_weights if p not in primary_params)
|
1617 |
+
|
1618 |
+
# 綜合評分,主要參數占更大權重
|
1619 |
+
base_score = (primary_score * 0.7) + (secondary_score * 0.3)
|
1620 |
+
|
1621 |
+
# 計算完美匹配加成
|
1622 |
+
perfect_match_bonus = 0.0
|
1623 |
+
if all(adjusted_scores[p] > 0.8 for p in primary_params):
|
1624 |
+
perfect_match_bonus = 0.1
|
1625 |
+
|
1626 |
+
# 計算最終分數
|
1627 |
+
final_score = base_score + perfect_match_bonus
|
1628 |
|
1629 |
+
# 品種特性加成
|
1630 |
breed_bonus = calculate_breed_bonus(breed_info, user_prefs)
|
1631 |
|
1632 |
+
# 整合最終分數
|
1633 |
+
final_score = (final_score * 0.8) + (breed_bonus * 0.2)
|
1634 |
|
1635 |
return final_score
|
1636 |
|
|
|
1650 |
ranges = {
|
1651 |
'poor': {
|
1652 |
'range': (0.0, 0.3),
|
1653 |
+
'out_min': 0.6,
|
1654 |
+
'out_max': 0.7
|
1655 |
},
|
1656 |
'mediocre': {
|
1657 |
'range': (0.3, 0.6),
|
1658 |
+
'out_min': 0.7,
|
1659 |
+
'out_max': 0.8
|
1660 |
},
|
1661 |
'good': {
|
1662 |
'range': (0.6, 0.8),
|
1663 |
+
'out_min': 0.8,
|
1664 |
+
'out_max': 0.9
|
1665 |
},
|
1666 |
'excellent': {
|
1667 |
'range': (0.8, 1.0),
|
1668 |
+
'out_min': 0.9,
|
1669 |
+
'out_max': 0.95
|
1670 |
}
|
1671 |
}
|
1672 |
|
|
|
1674 |
for config in ranges.values():
|
1675 |
range_min, range_max = config['range']
|
1676 |
if range_min <= score <= range_max:
|
|
|
1677 |
position = (score - range_min) / (range_max - range_min)
|
1678 |
|
1679 |
# 線性映射到目標範圍
|
|
|
1683 |
return round(result, 1)
|
1684 |
|
1685 |
# 處理超出範圍的情況
|
1686 |
+
return 0.6 if score < 0.0 else 0.95
|