时间:20210928

背景:有个相亲活动,需要暗地里给男女进行匹配,毕竟明面上直接说不喜欢哪个异性总是尴尬的。匹配的话,方法众多,并不能让每个人都满意,根据各自的意向,总能计算整体意向都不错的。


太长了不看,直接操作:

线下让N对男女:

  1. 写个小纸条,各自给N个异性排序,更喜欢的排在前面
  2. 得到:
    1. 女生的选择:womanChoices

      1. 女1:男2,男5,男1,....
      2. 女2:...
      3. ...
      4. 女N:...
    2. 男生的选择:manChoices
      1. 同理

操作:

  1. 进入网站:代码在线运行 - 在线工具
  2. 选择python
    1. 不要用python3
  3. 代码复制进去
  4. 修改代码里的两个变量:womanChoices、manChoices
    1. 将之前线下统计得到的,放在对应位置
  5. 点击“执行Run”
  6. 右侧出结果即可


算法:GS算法(盖尔-沙普利算法(Gale-Shapley algorithm))

算法思想:略

算法伪代码:


样例解释:

1、1对男女,不考虑

2、两对男女,分别优秀男1、一般男2;优秀女1、一般女2(“优秀”、“普通”方便理解,换成“青菜”、“萝卜”一个意思)

1.0、优秀女、男互相看中,普通女、男互相看中

  • 女1喜欢:男1>男2
  • 女2喜欢:男2>男1
  • 男1喜欢:女1>女2
  • 男2喜欢:女2>女1
  • 结果:男1——女1、男2——女2分别匹配
  • 解释:萝卜青菜各有所爱,分别各自找到自己最中意的

2.1.1、两女争优秀男,两男争优秀女

  • 女1喜欢:男1>男2
  • 女2喜欢:男1>男2
  • 男1喜欢:女1>女2
  • 男2喜欢:女1>女2
  • 结果:优秀男1——优秀女1、一般男2——一般女2
  • 解释:优者则优

2.2.1、两女争优秀男,两男争普通女

  • 女1喜欢:男1>男2
  • 女2喜欢:男1>男2
  • 男1喜欢:女2>女1
  • 男2喜欢:女2>女1
  • 结果:优秀男1——普通女2、普通男2——优秀女1
  • 解释:
    • 两男争普通女,该普通女不普通,优先级高,是个“优秀女”,优秀男1同理,故两个被相争的在一起
    • 该情况和上面一种“两女争优秀男,两男争优秀女”是类似

2.1.2、两女争普通男,两男争普通女:换身份,转换为“两女争优秀男,两男争优秀女”情况一样,

2.2.2、两女争普通男,两男争优质女:换身份,转换为“两女争优秀男,两男争普通女”情况一样

3.1.1、两女争优秀男,优秀男争优秀女、普通男争普通女

  • 女1喜欢:男1>男2
  • 女2喜欢:男1>男2
  • 男1喜欢:女1>女2
  • 男2喜欢:女2>女1
  • 结果:优秀男1——优秀女1、一般男2——一般女2
  • 解释:普通男1没被优秀女1优先选。

3.2.1、两女争优秀男,优秀男争普通女、普通男争优秀女

  • 结果:优秀男1——普通女2、普通男2——优秀女1
  • 解释:和3.1.1情况类似,优秀男权重更高(被两女相争)

3.1.2、两女争普通男,普通男争优秀女、优秀男争普通女

  • 解释:和3.1.1情况类似,普通男、优秀男身份互换即可

3.2.2、两女争普通男,普通男争普通女、优秀男争优秀女

  • 解释:和3.2.1情况类似,普通男、优秀男身份互换;

4、四角恋:女1->男1->女2->男2->女1

  • 女1喜欢:男1>男2
  • 女2喜欢:男2>男1
  • 男1喜欢:女2>女1
  • 男2喜欢:女1>女2
  • 结果:
    • 女士优先时:女1——男1、女2——男2
    • 男士优先时:女2——男1、女1——男2

代码:

# coding:utf-8
"""
Demo of Gale-Shapley stable marriage algorithm.Usage is:python marriage.py  [menfile]  [womenfile]orpython marriage.py  [menfile]  [womenfile]  Vfor verbose mode.For simplicity, the file format is assumed (without checking) to match
the following format:bob:     alice,caroldavid:   carol,aliceand likewise for the womenfile, where there should be precisely same
number of men as with women, and the identifiers should be
self-consistent between the two files.
"""
import sys# 原作者的例子:
womanChoices = '''
W:       D,B,C,A
X:       D,B,A,C
Y:       A,D,B,C
Z:       B,A,D,C
'''
manChoices = '''
A:      W,X,Z,Y
B:      W,Y,Z,X
C:      X,W,Y,Z
D:      W,Z,X,Y
'''# 1.0、优秀女、男互相看中,普通女、男互相看中
womanChoices = '''
女1:男1,男2
女2:男2,男1
'''
manChoices = '''
男1:女1,女2
男2:女2,女1
'''# 2.1.1、两优秀女争两优秀男、两优秀男争两优秀女
womanChoices = '''
女1:男1,男2
女2:男1,男2
'''
manChoices = '''
男1:女1,女2
男2:女1,女2
'''# 3.1.1、两女争优秀男,优秀男争优秀女、普通男争普通女
womanChoices = '''
女1:男1,男2
女2:男1,男2
'''
manChoices = '''
男1:女1,女2
男2:女2,女1
'''# 4、四角恋:女1->男1->女2->男2->女1
womanChoices = '''
女1:男1,男2
女2:男2,男1
'''
manChoices = '''
男1:女2,女1
男2:女1,女2
'''
# ---------------------------------------------------------------------------------------
# 现场活动男女们的选择:(格式类似上面的,一人一行,冒号、逗号注意)
# 女生
womanChoices = ''''''# 男生选择的结果:放这里
manChoices = ''''''
# ---------------------------------------------------------------------------------------class Person:"""Represent a generic person"""def __init__(self, name, priorities):"""name is a string which uniquely identifies this personpriorities is a list of strings which specifies a ranking of allpotential partners, from best to worst"""self.name = nameself.priorities = prioritiesself.partner = Nonedef __repr__(self):return 'Name is ' + self.name + '\n' + \'Partner is currently ' + str(self.partner) + '\n' + \'priority list is ' + str(self.priorities)class Man(Person):"""Represents a man"""def __init__(self, name, priorities):"""name is a string which uniquely identifies this personpriorities is a list of strings which specifies a ranking of allpotential partners, from best to worst"""Person.__init__(self, name, priorities)self.proposalIndex = 0                   # next person in our list to whom we might proposedef nextProposal(self):goal = self.priorities[self.proposalIndex]self.proposalIndex += 1return goaldef __repr__(self):return Person.__repr__(self) + '\n' + \'next proposal would be to ' + self.priorities[self.proposalIndex]class Woman(Person):"""Represents a woman"""def __init__(self, name, priorities):"""name is a string which uniquely identifies this personpriorities is a list of strings which specifies a ranking of allpotential partners, from best to worst"""Person.__init__(self, name, priorities)# now compute a reverse lookup for efficient candidate ratingself.ranking = {}for rank in range(len(priorities)):self.ranking[priorities[rank]] = rankdef evaluateProposal(self, suitor):"""Evaluates a proposal, though does not enact it.suitor is the string identifier for the man who is proposingreturns True if proposal should be accepted, False otherwise"""return self.partner == None or self.ranking[suitor] < self.ranking[self.partner]def parseFileOld(filename):"""Returns a list of (name,priority) pairs."""people = []f = file(filename)for line in f:pieces = line.split(':')name = pieces[0].strip()if name:priorities = pieces[1].strip().split(',')for i in range(len(priorities)):priorities[i] = priorities[i].strip()people.append((name, priorities))f.close()return peopledef parseFile(choices):choices = choices.replace(':', ':')choices = choices.replace(',', ',')people = []for line in choices.split('\n'):pieces = line.split(':')name = pieces[0].strip()if name:priorities = pieces[1].strip().split(',')for i in range(len(priorities)):priorities[i] = priorities[i].strip()people.append((name, priorities))return peopledef printPairings(men):for man in men.values():print man.name, 'is paired with', str(man.partner)def main_old():verbose = len(sys.argv) > 3# initialize dictionary of menmenlist = parseFile(sys.argv[1])men = dict()for person in menlist:men[person[0]] = Man(person[0], person[1])unwedMen = men.keys()# initialize dictionary of womenwomenlist = parseFile(sys.argv[2])women = dict()for person in womenlist:women[person[0]] = Woman(person[0], person[1])############################### the real algorithm ##################################while unwedMen:m = men[unwedMen[0]]             # pick arbitrary unwed manw = women[m.nextProposal()]      # identify highest-rank woman to which#    m has not yet proposedif verbose:print m.name, 'proposes to', w.nameif w.evaluateProposal(m.name):if verbose:print '  ', w.name, 'accepts the proposal'if w.partner:# previous partner is getting dumpedmOld = men[w.partner]mOld.partner = NoneunwedMen.append(mOld.name)unwedMen.remove(m.name)w.partner = m.namem.partner = w.nameelse:if verbose:print '  ', w.name, 'rejects the proposal'if verbose:print "Tentative Pairings are as follows:"printPairings(men)print# we should be doneprint "Final Pairings are as follows:"printPairings(men)def main(manChoices, WomanChoices):# initialize dictionary of menmenlist = parseFile(manChoices)men = dict()for person in menlist:men[person[0]] = Man(person[0], person[1])unwedMen = men.keys()# initialize dictionary of womenwomenlist = parseFile(WomanChoices)women = dict()for person in womenlist:women[person[0]] = Woman(person[0], person[1])############################### the real algorithm ##################################while unwedMen:m = men[unwedMen[0]]             # pick arbitrary unwed manw = women[m.nextProposal()]      # identify highest-rank woman to which#    m has not yet proposedif w.evaluateProposal(m.name):if w.partner:# previous partner is getting dumpedmOld = men[w.partner]mOld.partner = NoneunwedMen.append(mOld.name)unwedMen.remove(m.name)w.partner = m.namem.partner = w.name###################################################################################### we should be doneprint "Final Pairings are as follows:"printPairings(men)if __name__ == "__main__":# 女士优先,womanChoices放前面, 否则放后面main(womanChoices, manChoices)

作者源代码:

  • Gale-Shapley Stable Marriage Algorithm

参考资料:

  • [算法]Gale-Shapley Algorithm-稳定匹配算法的设计、实现与探讨
  • 算法 | 盖尔-沙普利(Gale-Shapley)婚姻稳定匹配算法
  • Gale-Shapley算法(基于python3.6)

匹配算法——相亲男女匹配相关推荐

  1. 用Python分析了5万条相亲网站数据,看相亲男女画像

    这短短的一生,我们最终都会失去.你不妨大胆一些,爱一个人,攀一座山,追一个梦. 一.前言 数据来源:https://www.zhenai.com/zhenghun/ 本文利用 Python 分析了按城 ...

  2. Python分析了5万条相亲网站数据 | 看相亲男女画像

    文章目录 一.前言 二.数据查看和预处理 三.数据分析 这短短的一生,我们最终都会失去 你不妨大胆一些 爱一个人,攀一座山,追一个梦 一.前言 数据来源:http://www.zhenai.com/z ...

  3. 【指纹识别】基于模板匹配算法指纹识别匹配门禁系统matlab源码

    一.简介 1 指纹识别的引入和原理 1.1 指纹的基本知识 指纹,由于其具有终身不变性.唯一性和方便性,已几乎成为生物特征识别的代名词.指纹是指人的手指末端正面皮肤上凸凹不平产生的纹线.纹线有规律的排 ...

  4. 算法设计与分析男女匹配问题C语言,C语言解决新郎和新娘配对问题代码解析

    问题描述 有3对情侣结婚,假设2个新郎为A.B.C,3个新娘为X.Y.Z,有参加婚礼的人搞不清谁和谁结婚,所以去询问了这6位新人中的3位,得到的回答如下:新郎A说他要和新娘X结婚:新娘X说她的未婚夫是 ...

  5. 双目视觉(五)立体匹配算法之动态规划全局匹配

    系列文章: 双目视觉(一)双目视觉系统 双目视觉(二)双目匹配的困难和评判标准 双目视觉(三)立体匹配算法 双目视觉(四)匹配代价 双目视觉(五)立体匹配算法之动态规划全局匹配 双目视觉(六)U-V视 ...

  6. 算法 | 盖尔-沙普利(Gale-Shapley)婚姻稳定匹配算法

    盖尔-沙普利[Gale-Shapley]婚姻稳定匹配算法 1 背景说明 2 原理及思路 2.1 问题的描述 2.2 盖尔-沙普利算法的思路 3 程序实现 4 结果分析 5 后记 概要: 本文将要介绍的 ...

  7. 微信群互动h5游戏推荐:旅行匹配度大考验

    近日,TOM游戏出品的一款"测测你适合与我一起旅行吗"的网红H5小游戏风靡网络,许多网友纷纷晒出与朋友.家人的匹配结果,画面令人惊奇又有趣! 有结婚七年的夫妻默契度测试仅为30分的 ...

  8. 初识滴滴交易策略之二:司乘匹配

    ‍ 前文(初识滴滴交易策略之一:交易市场)整体介绍了交易市场的定义.特点.技术特点和技术领域.在交易市场中,市场交易撮合--通常称之为"派单"--无疑是最重要的环节,以下将介绍滴滴 ...

  9. c语言 循环队列实现舞伴匹配

    题目: 循环队列的应用:舞伴配对 1.在舞会上男女各自排成一队,舞会开始时每一次从男队和女队的对头各出一人配成舞伴, 直到某队为空,如果两队初始人数不等,则较长的那一队中未配对者等待下一轮舞曲. 某轮 ...

最新文章

  1. 十三五乐山全力推进智慧城市和新能源汽车等项目
  2. 最强 AWS 的十条军规,首席技术官总结过去十年的经验
  3. 【机器学习】LR与最大熵模型的关系
  4. C++ override 关键字用法
  5. JVM 学习一:JVM 的构架模型及生命周期
  6. rtt面向对象oopc——1.rtt定义的这些类有什么用?
  7. Pentium II Pentium III架构/微架构/流水线 (2) - P6详解 - 前端(指令预取/译码/动态分支预测静态分支预测)
  8. python读取指定路径txt文件-python读取txt文件并取其某一列数据的示例
  9. Echarts:Vue3中使用Echarts
  10. 继承几近失传的经典吟诵-余觉中
  11. 有什么办法可以让微信群二维码永久有效?这类的二维码生成器怎么制作?
  12. 【语言-c#】应用程序正常初始化(0xc000007b)失败。请单击“确定”,终止应用程序。
  13. 2022年Unity 面试题 |五萬字 二佰道| Unity面试题大全,面试题总结【全网最全,收藏一篇足够面试】
  14. javase(8)_集合框架_List、Set、Map
  15. 25 行 Python 代码实现人脸检测——OpenCV 技术教程
  16. 苹果发布iOS 12.5.1,以修复旧设备上的COVID-19暴露通知
  17. iOS开发-技术知识盘点总结(二)
  18. 运算放大器的datasheet参数介绍
  19. 机器学习的工作原理是什么?
  20. Axure RP实例教程:组合弹出菜单效果

热门文章

  1. 微信和支付宝的服务器在哪里,手机恢复出厂设置,里面支付宝与微信怎么办?原来数据在这里...
  2. php可以发短信的代码,PHP代码函数实现PHP发送短信功能
  3. linux下qt虚拟键盘
  4. QB期刊:纪念人类基因组草图发表20周年系列文章 | Michael Q. Zhang教授分享个人研究历程及学科发展思考...
  5. 2020.2.22GDUT寒假训练排位赛2-H
  6. Leetcode 827. 最大人工岛 C++
  7. 编程菜鸟看云计算、移动互联网和物联网
  8. RT-Thread 隐藏的宝藏之ringbuff
  9. 越狱Season 1-Episode 6: Riots, Drills and the Devil: Part 1
  10. Android FM模块学习之四源码学习(2)