Spaces:
Running
on
Zero
Running
on
Zero
Update scoring_calculation_system.py
Browse files- scoring_calculation_system.py +76 -88
scoring_calculation_system.py
CHANGED
|
@@ -1510,151 +1510,134 @@ 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 |
-
|
| 1516 |
-
|
| 1517 |
-
|
| 1518 |
-
|
| 1519 |
-
|
| 1520 |
-
Returns:
|
| 1521 |
-
最終相容性分數 (0.3-0.95)
|
| 1522 |
"""
|
| 1523 |
-
# 1.
|
| 1524 |
critical_params = {
|
| 1525 |
'space': {
|
| 1526 |
-
'threshold': 0.
|
| 1527 |
'conditions': lambda p: True,
|
| 1528 |
-
'penalty': 0.
|
| 1529 |
},
|
| 1530 |
'noise': {
|
| 1531 |
-
'threshold': 0.
|
| 1532 |
'conditions': lambda p: p.living_space == 'apartment',
|
| 1533 |
-
'penalty': 0.
|
| 1534 |
},
|
| 1535 |
'experience': {
|
| 1536 |
-
'threshold': 0.
|
| 1537 |
'conditions': lambda p: p.experience_level == 'beginner',
|
| 1538 |
-
'penalty': 0.
|
| 1539 |
}
|
| 1540 |
}
|
| 1541 |
|
| 1542 |
-
#
|
| 1543 |
for param, config in critical_params.items():
|
| 1544 |
if scores[param] < config['threshold'] and config['conditions'](user_prefs):
|
| 1545 |
return config['penalty']
|
| 1546 |
|
| 1547 |
-
# 2. 基礎權重設定
|
| 1548 |
base_weights = {
|
| 1549 |
-
'space': 0.
|
| 1550 |
-
'exercise': 0.
|
| 1551 |
-
'experience': 0.
|
| 1552 |
'grooming': 0.15,
|
| 1553 |
'health': 0.10,
|
| 1554 |
'noise': 0.10
|
| 1555 |
}
|
| 1556 |
|
| 1557 |
-
# 3.
|
| 1558 |
adjusted_weights = {}
|
| 1559 |
for param, weight in base_weights.items():
|
| 1560 |
multiplier = 1.0
|
| 1561 |
|
| 1562 |
-
#
|
| 1563 |
if param == 'space':
|
| 1564 |
if user_prefs.living_space == 'apartment':
|
| 1565 |
-
multiplier *= 1.
|
| 1566 |
elif breed_info['Size'] in ['Large', 'Giant']:
|
| 1567 |
-
multiplier *= 1.
|
| 1568 |
|
| 1569 |
-
#
|
| 1570 |
elif param == 'exercise':
|
| 1571 |
if user_prefs.exercise_time > 150:
|
| 1572 |
-
multiplier *= 1.
|
| 1573 |
elif user_prefs.exercise_time < 60:
|
| 1574 |
-
multiplier *= 1.
|
| 1575 |
-
|
| 1576 |
-
#
|
| 1577 |
-
elif param == 'experience'
|
| 1578 |
-
|
| 1579 |
-
|
| 1580 |
-
|
| 1581 |
-
|
| 1582 |
-
|
| 1583 |
-
|
| 1584 |
-
# 健康相關調整
|
| 1585 |
-
elif param == 'health' and user_prefs.health_sensitivity == 'high':
|
| 1586 |
-
multiplier *= 1.3
|
| 1587 |
-
|
| 1588 |
-
# 噪音相關調整
|
| 1589 |
-
elif param == 'noise' and user_prefs.living_space == 'apartment':
|
| 1590 |
-
multiplier *= 1.4
|
| 1591 |
-
|
| 1592 |
adjusted_weights[param] = weight * multiplier
|
| 1593 |
|
| 1594 |
# 重新正規化權重
|
| 1595 |
total_weight = sum(adjusted_weights.values())
|
| 1596 |
normalized_weights = {k: v/total_weight for k, v in adjusted_weights.items()}
|
| 1597 |
|
| 1598 |
-
# 4.
|
| 1599 |
base_score = 0
|
| 1600 |
for param, weight in normalized_weights.items():
|
| 1601 |
score = scores[param]
|
| 1602 |
|
| 1603 |
# 非線性分數調整
|
| 1604 |
if score > 0.8:
|
| 1605 |
-
score = min(1.0, score * 1.
|
| 1606 |
-
elif score < 0.
|
| 1607 |
-
score = score * 0.
|
| 1608 |
-
|
| 1609 |
base_score += score * weight
|
| 1610 |
|
| 1611 |
-
# 5.
|
| 1612 |
-
adaptability_bonus = calculate_environmental_fit(breed_info, user_prefs)
|
| 1613 |
breed_bonus = calculate_breed_bonus(breed_info, user_prefs)
|
| 1614 |
-
|
| 1615 |
-
# 6. 計算最終分數
|
| 1616 |
-
final_score = (base_score * 0.70) + (breed_bonus * 0.20) + (adaptability_bonus * 0.10)
|
| 1617 |
-
|
| 1618 |
-
# 7. 轉換並限制分數範圍
|
| 1619 |
-
return amplify_score_extreme(final_score)
|
| 1620 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1621 |
|
| 1622 |
def amplify_score_extreme(score: float) -> float:
|
| 1623 |
"""
|
| 1624 |
-
|
| 1625 |
-
|
| 1626 |
-
|
| 1627 |
-
|
| 1628 |
-
|
| 1629 |
-
|
| 1630 |
-
Returns:
|
| 1631 |
-
放大後的分數 (0.3-0.95 範圍)
|
| 1632 |
"""
|
| 1633 |
-
# 定義分數區間的轉換參數
|
| 1634 |
ranges = {
|
| 1635 |
'poor': {
|
| 1636 |
-
'range': (0, 0.
|
| 1637 |
-
'out_min':
|
| 1638 |
-
'out_max':
|
| 1639 |
-
'amplification': 1.
|
| 1640 |
},
|
| 1641 |
'mediocre': {
|
| 1642 |
-
'range': (0.
|
| 1643 |
-
'out_min':
|
| 1644 |
-
'out_max':
|
| 1645 |
-
'amplification':
|
| 1646 |
},
|
| 1647 |
'good': {
|
| 1648 |
-
'range': (0.
|
| 1649 |
-
'out_min':
|
| 1650 |
-
'out_max':
|
| 1651 |
-
'amplification': 1.1
|
| 1652 |
},
|
| 1653 |
'excellent': {
|
| 1654 |
-
'range': (0.
|
| 1655 |
-
'out_min':
|
| 1656 |
-
'out_max':
|
| 1657 |
-
'amplification': 1.
|
| 1658 |
}
|
| 1659 |
}
|
| 1660 |
|
|
@@ -1662,16 +1645,21 @@ def amplify_score_extreme(score: float) -> float:
|
|
| 1662 |
for range_name, config in ranges.items():
|
| 1663 |
range_min, range_max = config['range']
|
| 1664 |
if range_min <= score <= range_max:
|
| 1665 |
-
#
|
| 1666 |
range_position = (score - range_min) / (range_max - range_min)
|
| 1667 |
|
| 1668 |
-
#
|
| 1669 |
range_position = min(1.0, range_position * config['amplification'])
|
| 1670 |
|
| 1671 |
-
#
|
| 1672 |
amplified = config['out_min'] + (config['out_max'] - config['out_min']) * range_position
|
| 1673 |
|
| 1674 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1675 |
|
| 1676 |
-
#
|
| 1677 |
-
return
|
|
|
|
| 1510 |
|
| 1511 |
def calculate_breed_compatibility_score(scores: dict, user_prefs: UserPreferences, breed_info: dict) -> float:
|
| 1512 |
"""
|
| 1513 |
+
改進的品種相容性評分系統,提供更明顯的分數差異和更準確的品種匹配
|
| 1514 |
|
| 1515 |
+
主要改進:
|
| 1516 |
+
1. 提高關鍵參數權重
|
| 1517 |
+
2. 加強條件權重調整
|
| 1518 |
+
3. 更嚴格的不適配懲罰
|
| 1519 |
+
4. 非線性分數調整
|
|
|
|
|
|
|
| 1520 |
"""
|
| 1521 |
+
# 1. 關鍵不適配參數檢查 - 加強懲罰機制
|
| 1522 |
critical_params = {
|
| 1523 |
'space': {
|
| 1524 |
+
'threshold': 0.35, # 提高門檻
|
| 1525 |
'conditions': lambda p: True,
|
| 1526 |
+
'penalty': 0.25 # 更嚴重的懲罰
|
| 1527 |
},
|
| 1528 |
'noise': {
|
| 1529 |
+
'threshold': 0.35,
|
| 1530 |
'conditions': lambda p: p.living_space == 'apartment',
|
| 1531 |
+
'penalty': 0.3
|
| 1532 |
},
|
| 1533 |
'experience': {
|
| 1534 |
+
'threshold': 0.35,
|
| 1535 |
'conditions': lambda p: p.experience_level == 'beginner',
|
| 1536 |
+
'penalty': 0.3
|
| 1537 |
}
|
| 1538 |
}
|
| 1539 |
|
| 1540 |
+
# 檢查關鍵不適配
|
| 1541 |
for param, config in critical_params.items():
|
| 1542 |
if scores[param] < config['threshold'] and config['conditions'](user_prefs):
|
| 1543 |
return config['penalty']
|
| 1544 |
|
| 1545 |
+
# 2. 基礎權重設定 - 提高關鍵參數權重
|
| 1546 |
base_weights = {
|
| 1547 |
+
'space': 0.40, # 提高空間權重
|
| 1548 |
+
'exercise': 0.35, # 提高運動需求權重
|
| 1549 |
+
'experience': 0.25, # 提高經驗需求權重
|
| 1550 |
'grooming': 0.15,
|
| 1551 |
'health': 0.10,
|
| 1552 |
'noise': 0.10
|
| 1553 |
}
|
| 1554 |
|
| 1555 |
+
# 3. 動態權重調整 - 加強調整幅度
|
| 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 |
+
multiplier *= 1.4 # 加大調整幅度
|
| 1564 |
elif breed_info['Size'] in ['Large', 'Giant']:
|
| 1565 |
+
multiplier *= 1.5
|
| 1566 |
|
| 1567 |
+
# 運動需求調整
|
| 1568 |
elif param == 'exercise':
|
| 1569 |
if user_prefs.exercise_time > 150:
|
| 1570 |
+
multiplier *= 1.6 # 加大高運動需求的影響
|
| 1571 |
elif user_prefs.exercise_time < 60:
|
| 1572 |
+
multiplier *= 1.4
|
| 1573 |
+
|
| 1574 |
+
# 經驗需求調整
|
| 1575 |
+
elif param == 'experience':
|
| 1576 |
+
if user_prefs.experience_level == 'beginner':
|
| 1577 |
+
multiplier *= 1.5
|
| 1578 |
+
elif breed_info.get('Care Level') == 'High':
|
| 1579 |
+
multiplier *= 1.4
|
| 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 |
+
# 4. 分數計算 - 加強非線性調整
|
| 1588 |
base_score = 0
|
| 1589 |
for param, weight in normalized_weights.items():
|
| 1590 |
score = scores[param]
|
| 1591 |
|
| 1592 |
# 非線性分數調整
|
| 1593 |
if score > 0.8:
|
| 1594 |
+
score = min(1.0, score * 1.3) # 加大高分獎勵
|
| 1595 |
+
elif score < 0.5: # 降低門檻,加大懲罰
|
| 1596 |
+
score = score * 0.7 # 加重低分懲罰
|
| 1597 |
+
|
| 1598 |
base_score += score * weight
|
| 1599 |
|
| 1600 |
+
# 5. 特性加成整合
|
|
|
|
| 1601 |
breed_bonus = calculate_breed_bonus(breed_info, user_prefs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1602 |
|
| 1603 |
+
# 6. 最終分數計算 - 調整加成比例
|
| 1604 |
+
final_score = (base_score * 0.80) + (breed_bonus * 0.20)
|
| 1605 |
+
|
| 1606 |
+
return final_score
|
| 1607 |
|
| 1608 |
def amplify_score_extreme(score: float) -> float:
|
| 1609 |
"""
|
| 1610 |
+
改進的分數放大函數,提供更大的分數範圍和更明顯的差異
|
| 1611 |
+
|
| 1612 |
+
改進:
|
| 1613 |
+
1. 擴大分數範圍(45-85%)
|
| 1614 |
+
2. 加強極端值效果
|
| 1615 |
+
3. 減少中間分數的出現
|
|
|
|
|
|
|
| 1616 |
"""
|
|
|
|
| 1617 |
ranges = {
|
| 1618 |
'poor': {
|
| 1619 |
+
'range': (0, 0.35), # 擴大低分區間
|
| 1620 |
+
'out_min': 45, # 降低最低分數
|
| 1621 |
+
'out_max': 55,
|
| 1622 |
+
'amplification': 1.3 # 加強懲罰效果
|
| 1623 |
},
|
| 1624 |
'mediocre': {
|
| 1625 |
+
'range': (0.35, 0.55), # 縮小中等區間
|
| 1626 |
+
'out_min': 55,
|
| 1627 |
+
'out_max': 65,
|
| 1628 |
+
'amplification': 0.9 # 略微抑制中等分數
|
| 1629 |
},
|
| 1630 |
'good': {
|
| 1631 |
+
'range': (0.55, 0.75),
|
| 1632 |
+
'out_min': 65,
|
| 1633 |
+
'out_max': 75,
|
| 1634 |
+
'amplification': 1.1
|
| 1635 |
},
|
| 1636 |
'excellent': {
|
| 1637 |
+
'range': (0.75, 1.0), # 擴大高分區間
|
| 1638 |
+
'out_min': 75,
|
| 1639 |
+
'out_max': 85, # 提高最高分數
|
| 1640 |
+
'amplification': 1.4 # 加強獎勵效果
|
| 1641 |
}
|
| 1642 |
}
|
| 1643 |
|
|
|
|
| 1645 |
for range_name, config in ranges.items():
|
| 1646 |
range_min, range_max = config['range']
|
| 1647 |
if range_min <= score <= range_max:
|
| 1648 |
+
# 計算區間位置
|
| 1649 |
range_position = (score - range_min) / (range_max - range_min)
|
| 1650 |
|
| 1651 |
+
# 應用非線性調整
|
| 1652 |
range_position = min(1.0, range_position * config['amplification'])
|
| 1653 |
|
| 1654 |
+
# 轉換到目標範圍
|
| 1655 |
amplified = config['out_min'] + (config['out_max'] - config['out_min']) * range_position
|
| 1656 |
|
| 1657 |
+
# 加入小幅隨機變化,避免重複分數
|
| 1658 |
+
import random
|
| 1659 |
+
variation = random.uniform(-0.5, 0.5)
|
| 1660 |
+
amplified += variation
|
| 1661 |
+
|
| 1662 |
+
return round(max(45, min(85, amplified)), 1)
|
| 1663 |
|
| 1664 |
+
# 處理超出範圍的分數
|
| 1665 |
+
return 55 if score < 0 else 95
|