项目结构# encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看言語成了邀功盡責的功臣還需要行爲每日來值班嗎 # 描述Gale-Shapley Algorithm # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/7/29 22:43 # User : geovindu # Product : PyCharm # Project : PyAlgorithms # File : gale_shapley_base.py from typing import Dict, List def gale_shapley(proposers: List[str], proposer_prefs: Dict[str, List[str]], acceptors: List[str], acceptor_prefs: Dict[str, List[str]]) - Dict[str, str]: Gale-Shapley 标准稳定匹配【正向】 求婚方proposers接受方acceptors 返回{求婚方: 匹配对象} :param proposers: :param proposer_prefs: :param acceptors: :param acceptor_prefs: :return: free_proposers proposers.copy() next_propose_index: Dict[str, int] {p: 0 for p in proposers} acceptor_matched: Dict[str, str] {} # 预构建优先级索引加速比较 acceptor_rank: Dict[str, Dict[str, int]] {} for acceptor in acceptors: rank_map {} for idx, person in enumerate(acceptor_prefs[acceptor]): rank_map[person] idx acceptor_rank[acceptor] rank_map while free_proposers: proposer free_proposers.pop(0) pref_list proposer_prefs[proposer] target_acceptor pref_list[next_propose_index[proposer]] next_propose_index[proposer] 1 if target_acceptor not in acceptor_matched: acceptor_matched[target_acceptor] proposer else: current_match acceptor_matched[target_acceptor] # 更喜欢新来的求婚者则更换配对 if acceptor_rank[target_acceptor][proposer] acceptor_rank[target_acceptor][current_match]: acceptor_matched[target_acceptor] proposer free_proposers.append(current_match) else: free_proposers.append(proposer) # 转换映射求婚方 - 匹配对象 result {v: k for k, v in acceptor_matched.items()} return result # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看言語成了邀功盡責的功臣還需要行爲每日來值班嗎 # 描述Gale-Shapley Algorithm # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/7/29 22:45 # User : geovindu # Product : PyCharm # Project : PyAlgorithms # File : gs_reverse.py from typing import Dict, List from GaleShapley.core.gale_shapley_base import gale_shapley def gale_shapley_reverse(original_proposers: List[str], original_proposer_prefs: Dict[str, List[str]], original_acceptors: List[str], original_acceptor_prefs: Dict[str, List[str]]) - Dict[str, str]: GS反向模式【企业收益优先策略】 逻辑交换 原接收方(供给方) → 新求婚方 原求婚方(需求方) → 新接收方 返回格式统一{【原需求方】:【原供给方】} 和正向输出结构保持一致上层业务无需改动打印逻辑 :param original_proposers: :param original_proposer_prefs: :param original_acceptors: :param original_acceptor_prefs: :return: # 角色互换 new_proposers original_acceptors new_acceptors original_proposers new_proposer_prefs original_acceptor_prefs new_acceptor_prefs original_proposer_prefs raw_match gale_shapley( proposersnew_proposers, proposer_prefsnew_proposer_prefs, acceptorsnew_acceptors, acceptor_prefsnew_acceptor_prefs ) # raw_match: {供给方:需求方} # 反转成统一输出格式 {需求方:供给方} 和正向接口对齐 result {v: k for k, v in raw_match.items()} return result # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看言語成了邀功盡責的功臣還需要行爲每日來值班嗎 # 描述Gale-Shapley Algorithm # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/7/29 22:46 # User : geovindu # Product : PyCharm # Project : PyAlgorithms # File : score_helper.py from typing import Dict, List, Callable, Any def build_preference_list( subject_list: List[str], candidate_list: List[str], score_func: Callable[[str, str], float] ) - Dict[str, List[str]]: 通用工具自动打分生成偏好序列 :param subject_list: 需要生成偏好的主体列表 :param candidate_list: 可供选择的候选对象 :param score_func: 打分函数 f(主体,候选) → 分数分数越高优先级越高 :return: dict 偏好列表从高分到低分排序 pref_dict {} for subject in subject_list: score_items [] for candidate in candidate_list: s score_func(subject, candidate) score_items.append((candidate, s)) # 分数降序排序 score_items.sort(keylambda x: x[1], reverseTrue) sorted_candidates [item[0] for item in score_items] pref_dict[subject] sorted_candidates return pref_dict def print_match_result(scene_name: str, match_data: Dict[str, str]): 统一格式化打印匹配结果 :param scene_name: :param match_data: :return: print(f\n {scene_name}【企业收益优先-反向GS匹配结果】 ) for demand, supply in match_data.items(): print(f{demand:25s} ----分配---- {supply}) # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看言語成了邀功盡責的功臣還需要行爲每日來值班嗎 # 描述Gale-Shapley Algorithm # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/7/29 22:47 # User : geovindu # Product : PyCharm # Project : PyAlgorithms # File : scene1_customer_consultant.py from typing import Dict, List from GaleShapley.utils.score_helper import build_preference_list, print_match_result from GaleShapley.core.gs_reverse import gale_shapley_reverse def run_scene1(): :return: # 业务基础数据可替换数据库查询 customers: List[str] [ C1钻戒 预算9W, C2古法黄金婚嫁 预算12W, C3蓝宝石吊坠 预算3.2W, C4翡翠手镯 预算6.5W ] consultants: List[str] [ Lisa专精钻石, Tom专精古法黄金, Amy专精彩宝/翡翠 ] # 客户基础属性【模拟数据库字段】 cust_info: Dict[str, Dict[str, any]] { C1钻戒 预算9W: {category: 钻石, budget: 90000, deal_prob: 0.85}, C2古法黄金婚嫁 预算12W: {category: 黄金古法, budget: 120000, deal_prob: 0.90}, C3蓝宝石吊坠 预算3.2W: {category: 彩宝, budget: 32000, deal_prob: 0.60}, C4翡翠手镯 预算6.5W: {category: 翡翠, budget: 65000, deal_prob: 0.72}, } cons_info: Dict[str, Dict[str, any]] { Lisa专精钻石: {skill: 钻石, star: 4.8, load: 2}, Tom专精古法黄金: {skill: 黄金古法, star: 4.7, load: 1}, Amy专精彩宝/翡翠: {skill: 彩宝翡翠, star: 4.9, load: 3}, } # 打分函数 def score_cust_to_consultant(cust: str, cons: str) - float: 客户对顾问打分客户视角偏好 :param cust: :param cons: :return: score 0.0 c_data cust_info[cust] s_data cons_info[cons] # 品类匹配权重最高 if (c_data[category] 钻石 and s_data[skill] 钻石) or \ (c_data[category] 黄金古法 and s_data[skill] 黄金古法) or \ (c_data[category] in [彩宝, 翡翠] and s_data[skill] 彩宝翡翠): score 50 score s_data[star] * 8 score - s_data[load] * 3 return score def score_consultant_to_cust(cons: str, cust: str) - float: 顾问对客户打分门店视角优先高预算、高成交概率客户 :param cons: :param cust: :return: score 0.0 c_data cust_info[cust] s_data cons_info[cons] score c_data[budget] / 1000 score c_data[deal_prob] * 40 # 品类匹配加分 if (c_data[category] 钻石 and s_data[skill] 钻石) or \ (c_data[category] 黄金古法 and s_data[skill] 黄金古法) or \ (c_data[category] in [彩宝, 翡翠] and s_data[skill] 彩宝翡翠): score 30 return score # 自动生成偏好列表 cust_prefs build_preference_list(customers, consultants, score_cust_to_consultant) cons_prefs build_preference_list(consultants, customers, score_consultant_to_cust) # 反向GS执行顾问主动求婚企业收益优先 match_result gale_shapley_reverse( original_proposerscustomers, original_proposer_prefscust_prefs, original_acceptorsconsultants, original_acceptor_prefscons_prefs ) print_match_result(场景1意向客户 ↔ 珠宝销售顾问分配, match_result) return match_result # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看言語成了邀功盡責的功臣還需要行爲每日來值班嗎 # 描述Gale-Shapley Algorithm # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/7/29 22:49 # User : geovindu # Product : PyCharm # Project : PyAlgorithms # File : scene2_order_worker.py from typing import Dict, List, Any from GaleShapley.utils.score_helper import build_preference_list, print_match_result from GaleShapley.core.gs_reverse import gale_shapley_reverse def run_scene2(): :return: orders: List[str] [ ORD01钻戒微镶定制, ORD02古法黄金花丝手镯, ORD03翡翠K金镶嵌, ORD04红宝石戒托雕蜡 ] workers: List[str] [ 张师傅微镶专精, 李师傅古法花丝, 王师傅宝石镶嵌/雕蜡 ] order_info: Dict[str, Dict[str, Any]] { ORD01钻戒微镶定制: {craft: 微镶, profit: 4800, difficulty: 3}, ORD02古法黄金花丝手镯: {craft: 花丝, profit: 6200, difficulty: 4}, ORD03翡翠K金镶嵌: {craft: 镶嵌, profit: 3600, difficulty: 2}, ORD04红宝石戒托雕蜡: {craft: 雕蜡, profit: 4100, difficulty: 3}, } worker_info: Dict[str, Dict[str, Any]] { 张师傅微镶专精: {master_craft: 微镶, speed_score: 4.8}, 李师傅古法花丝: {master_craft: 花丝, speed_score: 4.6}, 王师傅宝石镶嵌/雕蜡: {master_craft: 镶嵌雕蜡, speed_score: 4.7}, } # 订单 对工匠打分 def score_order_to_worker(order: str, worker: str) - float: od order_info[order] wk worker_info[worker] score 0.0 if (od[craft] 微镶 and wk[master_craft] 微镶) or \ (od[craft] 花丝 and wk[master_craft] 花丝) or \ (od[craft] in [镶嵌, 雕蜡] and wk[master_craft] 镶嵌雕蜡): score 55 score wk[speed_score] * 7 return score # 工匠 对订单打分优先高毛利、难度匹配 def score_worker_to_order(worker: str, order: str) - float: od order_info[order] wk worker_info[worker] score od[profit] / 100 if (od[craft] 微镶 and wk[master_craft] 微镶) or \ (od[craft] 花丝 and wk[master_craft] 花丝) or \ (od[craft] in [镶嵌, 雕蜡] and wk[master_craft] 镶嵌雕蜡): score 40 return score order_prefs build_preference_list(orders, workers, score_order_to_worker) worker_prefs build_preference_list(workers, orders, score_worker_to_order) match_result gale_shapley_reverse( original_proposersorders, original_proposer_prefsorder_prefs, original_acceptorsworkers, original_acceptor_prefsworker_prefs ) print_match_result(场景2定制订单 ↔ 工匠工厂派单, match_result) return match_result # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看言語成了邀功盡責的功臣還需要行爲每日來值班嗎 # 描述Gale-Shapley Algorithm # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/7/29 22:50 # User : geovindu # Product : PyCharm # Project : PyAlgorithms # File : scene3_shop_stone.py from typing import Dict, List, Any from GaleShapley.utils.score_helper import build_preference_list, print_match_result from GaleShapley.core.gs_reverse import gale_shapley_reverse def run_scene3(): :return: shops: List[str] [深圳福田店, 广州天河店, 上海南京路店, 杭州湖滨店] stones: List[str] [ D011克拉 D色钻石, R013克拉红宝石, S014克拉蓝宝石 ] shop_info: Dict[str, Dict[str, Any]] { 深圳福田店: {lux_conv: 0.38, customer_group: 钻石客群}, 广州天河店: {lux_conv: 0.32, customer_group: 红蓝宝石客群}, 上海南京路店: {lux_conv: 0.41, customer_group: 彩宝高端客群}, 杭州湖滨店: {lux_conv: 0.30, customer_group: 综合客群}, } stone_info: Dict[str, Dict[str, Any]] { D011克拉 D色钻石: {type: 钻石}, R013克拉红宝石: {type: 红宝}, S014克拉蓝宝石: {type: 蓝宝}, } # 门店对裸石打分 def score_shop_to_stone(shop: str, stone: str) - float: s_data shop_info[shop] st_data stone_info[stone] score 30.0 if (st_data[type] 钻石 and s_data[customer_group] 钻石客群) or \ (st_data[type] in [红宝, 蓝宝] and 彩宝 in s_data[customer_group]): score 45 return score # 裸石(总部)对门店打分优先高端转化率高门店 def score_stone_to_shop(stone: str, shop: str) - float: sh_data shop_info[shop] score sh_data[lux_conv] * 100 return score shop_prefs build_preference_list(shops, stones, score_shop_to_stone) stone_prefs build_preference_list(stones, shops, score_stone_to_shop) match_result gale_shapley_reverse( original_proposersshops, original_proposer_prefsshop_prefs, original_acceptorsstones, original_acceptor_prefsstone_prefs ) print_match_result(场景3线下门店 ↔ 稀缺裸石总部调配, match_result) return match_result # encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看言語成了邀功盡責的功臣還需要行爲每日來值班嗎 # 描述Gale-Shapley Algorithm # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/7/29 22:50 # User : geovindu # Product : PyCharm # Project : PyAlgorithms # File : scene4_anchor_sku.py from typing import Dict, List, Any from GaleShapley.utils.score_helper import build_preference_list, print_match_result from GaleShapley.core.gs_reverse import gale_shapley_reverse def run_scene4(): :return: anchors: List[str] [主播晴晴(钻石赛道), 主播阿凯(黄金赛道), 主播雯雯(彩宝赛道)] skus: List[str] [ SKU00150分钻戒, SKU002足金古法项链, SKU003海蓝宝手链 ] anchor_info: Dict[str, Dict[str, Any]] { 主播晴晴(钻石赛道): {track: 钻石, gmv_index: 88}, 主播阿凯(黄金赛道): {track: 黄金, gmv_index: 92}, 主播雯雯(彩宝赛道): {track: 彩宝, gmv_index: 85}, } sku_info: Dict[str, Dict[str, Any]] { SKU00150分钻戒: {category: 钻石}, SKU002足金古法项链: {category: 黄金}, SKU003海蓝宝手链: {category: 彩宝}, } # 主播对货品打分 def score_anchor_to_sku(anchor: str, sku: str) - float: an anchor_info[anchor] sk sku_info[sku] score 20 if an[track] sk[category]: score 55 return score # SKU运营方对主播打分赛道匹配主播带货能力 def score_sku_to_anchor(sku: str, anchor: str) - float: an anchor_info[anchor] sk sku_info[sku] score an[gmv_index] if an[track] sk[category]: score 30 return score anchor_prefs build_preference_list(anchors, skus, score_anchor_to_sku) sku_prefs build_preference_list(skus, anchors, score_sku_to_anchor) match_result gale_shapley_reverse( original_proposersanchors, original_proposer_prefsanchor_prefs, original_acceptorsskus, original_acceptor_prefssku_prefs ) print_match_result(场景4带货主播 ↔ 珠宝SKU直播选品分配, match_result) return match_result调用# encoding: utf-8 # 版权所有 2026 ©涂聚文有限公司™ ® # 许可信息查看言語成了邀功盡責的功臣還需要行爲每日來值班嗎 # 描述Gale-Shapley Algorithm # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2024.3.6 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j # Datetime : 2026/7/29 22:51 # User : geovindu # Product : PyCharm # Project : PyAlgorithms # File : GaleShapleyBll.py from GaleShapley.business.scene1_customer_consultant import run_scene1 from GaleShapley.business.scene2_order_worker import run_scene2 from GaleShapley.business.scene3_shop_stone import run_scene3 from GaleShapley.business.scene4_anchor_sku import run_scene4 class GaleShapleyBll(object): def Demo(self): :return: print( Gale-Shapley 珠宝稳定匹配系统【企业收益优先模式启动】) run_scene1() run_scene2() run_scene3() run_scene4()输出