中兴捧月——婚姻匹配问题

题目:

建立一个模型,来模拟推导社会男女择偶过程。

为了模型简化,一个人的特性指标有三个,这里假设为财富、样貌、品格,每个指标均可取值1-100之间任意数字。同样也对这3项指标有自己的需求。这3个需求值取值范围都在1-98间,当然三者的和必须为100.所以任意一个人可以用以下数组来表述:

G(A、B、C、A1、B1、C1)G代表男,M代表女。

举例G11(80、50、40、10、30、60),表示男11号,拥有财富80、样貌50、品格40,对异性品格的偏好为:财富在乎程度百分之10、样貌在乎程度百分之30、品格在乎程度百分之60。

同样为了模型简化,假设信息是完全对称的,即是说,每个人都能一眼就能看清楚任意一个人的财富、样貌、品格。

还是为了模型简化,我建模所用样本为男女各100个,即男女人数相同。

每个人对异性的满意度将如下定义:每个偏好指标与异性的对应的禀赋指标相乘,三个指标的乘积再相加,即他(她)对某个异性的满意度。

举例G11(80、50、40、10、30、60)对M(50、60、80、40、10、50)的满意度为:

(10*50+30*60+60*80)= 7100分

相对的 MM 对 GG的满意度则为:

(40*80+10*50+50*40) = 5700分

好了,配对活动开始,设计的配对法则如下:

1、100个男方,顺序,轮流从0号到99号女方中挑选自己最满意的一位,然后向她发出配对邀请。

2、接受邀请最多的女方开始行动,对这些邀请的男性中,选择最满意的一位。

3、那么这两位配对成功,剔除出样本,剩下的99对继续这样配对。

4、循环该配对法则,直到最后一对男女配对成功。

package 男女匹配问题;
/**
* 类说明
* 描述:Person 类
* @author 佳萌
* @date 2018年7月25日
*/

public class Person {
	/**
	 * ID
	 */
	private Integer id;
	private Integer appearance;
	private Integer character;
	private Integer rich;
	private  Integer expeappearance;
	private Integer expecharacter;
	private Integer experich;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Integer getAppearance() {
		return appearance;
	}
	public void setAppearance(Integer appearance) {
		this.appearance = appearance;
	}
	public Integer getCharacter() {
		return character;
	}
	public void setCharacter(Integer character) {
		this.character = character;
	}
	public Integer getRich() {
		return rich;
	}
	public void setRich(Integer rich) {
		this.rich = rich;
	}
	public Integer getExpeappearance() {
		return expeappearance;
	}
	public void setExpeappearance(Integer expeappearance) {
		this.expeappearance = expeappearance;
	}
	public Integer getExpecharacter() {
		return expecharacter;
	}
	public void setExpecharacter(Integer expecharacter) {
		this.expecharacter = expecharacter;
	}
	public Integer getExperich() {
		return experich;
	}
	public void setExperich(Integer experich) {
		this.experich = experich;
	}
	public Person(Integer id, Integer appearance, Integer character, Integer rich, Integer expeappearance,
			Integer expecharacter, Integer experich) {
		super();
		this.id = id;
		this.appearance = appearance;
		this.character = character;
		this.rich = rich;
		this.expeappearance = expeappearance;
		this.expecharacter = expecharacter;
		this.experich = experich;
	}
}
package 男女匹配问题;

import java.io.BufferedReader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;

/**
* 类说明
* 描述:转化类,把文件转化成集合
* @author 佳萌
* @date 2018年7月25日
*/

public class FileToList {
	/**
	 * 男士女士和主角的文件
	 */
	String malePath = "E:\\图论\\Java\\中期作业\\Java男女匹配作业(集合部分大作业)\\作业\\male.txt";
	String femalePath = "E:\\图论\\Java\\中期作业\\Java男女匹配作业(集合部分大作业)\\作业\\female.txt";
	String playerPath = "E:\\图论\\Java\\中期作业\\Java男女匹配作业(集合部分大作业)\\作业\\players.txt";
	
	/**
	 * 
	 * @param path  文件的地址
	 * @return 一个集合
	 */
	public ArrayList convey(String path){
		/**
		 * 存储 Person 的集合
		 */
		ArrayList list = new ArrayList();
		try {
			BufferedReader br = new BufferedReader(new FileReader(path));
			String str = "";
			String tmp[] = null;
			while((str = br.readLine()) != null){
				tmp = str.split(",");
				list.add(new Person(
						Integer.parseInt(tmp[0]),
						Integer.parseInt(tmp[1]),
						Integer.parseInt(tmp[2]),				
						Integer.parseInt(tmp[3]),		
						Integer.parseInt(tmp[4]),
						Integer.parseInt(tmp[5]),
						Integer.parseInt(tmp[6])
					));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public static void show(ArrayList m){
		for(int i = 0;i < m.size();i++){
			Person per = m.get(i);
			System.out.println(per.getId()+","+per.getAppearance()+","+per.getCharacter()+
					","+per.getRich()+","+per.getExpeappearance()+","+
					per.getExpecharacter()+","+per.getExperich());
		}
	}
	
}
package 男女匹配问题;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


/**
* 类说明
* 描述:TODO
* @author 佳萌
* @date 2018年7月25日
*/

public class Match {
	static  int x = 1;
	/**
	 * 男女配对
	 */
	static HashMap boyTogirl = new HashMap();
	static HashMap girlToboy = new HashMap();
	/**
	 * 
	 * @param male  男士集合
	 * @param female 女士集合
	 * @param player 主角
	 */
	public static void match(ArrayList male,ArrayList female,Person player){
		/**
		 * 女生被选择的情况
		 */
		HashMap> maleFemale;	
		
		//增加主角
		addPlayer(male,female,player);
		for(int i = 0;i < 100;i++){
				//男生选女生
				maleFemale = maleInvite(male,female);
				//统计被邀请最多的女生
				Person girl = getMaxFemale(maleFemale);
				//女生反选
				Person boy = femaleAgree(girl,maleFemale.get(girl));
				//剔除匹配成功的 
				male.remove(boy);
				female.remove(girl);
				boyTogirl.put(boy, girl);
				girlToboy.put(girl, boy);	
				
				//maleFemale.clear();
		}
		if(boyTogirl.containsKey(player)){
			System.out.println("第" + (x++) +"组player加入:"+"-1"+":"+boyTogirl.get(player).getId());	
		}else if(girlToboy.containsKey(player)){
			System.out.println("第" + (x++) +"组player加入:"+girlToboy.get(player).getId()+":"+"-1");		
		}else{
			System.out.println("第" + (x++) +"组player加入:"+"无结果");
		}
	}
	//获得两个人的匹配度
	public  static int getScore(Person one,Person two){	
		return one.getExpeappearance()*two.getAppearance()+
				one.getExpecharacter()*two.getCharacter()+one.getExperich()*two.getRich();
	}
	
	/**
	 * 获得两个人的优先级
	 * @param one
	 * @param two
	 * @return
	 */
	public static boolean prority(Person one,Person two){
		int num1 = one.getAppearance() + one.getCharacter() + one.getRich();
		int num2 = two.getAppearance() + two.getCharacter() + two.getRich();
		if(num1 > num2){
			return true;
		}
		if(num1 == num2){
			return one.getId() < two.getId();
		}
		return false;
	}
	/**
	 * 加入主角
	 */
	public static void addPlayer(ArrayList male,ArrayList female,Person player){
		int id = player.getId();//获得主角的性别
		if(id == 0){//女生
			player.setId(-1);//将ID设为-1
			female.add(player);//加入女生的集合
		}else{
			player.setId(-1);
			male.add(player);
		}
	}
	/**
	 * 一个男生选择心仪的女生
	 * @return 匹配度最高的女生
	 */
	public static  Person oneSelect(ArrayList female,Person boy){
		int femaleSie = female.size();
		Person Maxgirl = null;
		int maxScorer = 0;
		for(int i = 0;i < femaleSie;i++){
			Person girl  = female.get(i);
			int value = getScore(boy,girl);
			if(value > maxScorer){
				maxScorer = value;
				Maxgirl = girl;
			}else if(value == maxScorer){
				if(prority(girl,Maxgirl)){
					maxScorer = value;
					Maxgirl = girl;
				}
			}
		}
		return Maxgirl;
	}
	

	/**
	 * 所有男生选择最合适的女生
	 * @return 返回一个HashMap>的集合
	 * person 女生,
	 * ArrayList 选择这个女生的男生
	 */	
	public static HashMap> maleInvite(ArrayList male,ArrayList female){
		HashMap> maleinvite = new HashMap>();
		int maleSize = male.size();
		int femaleSie = female.size();
		for(int i = 0;i < maleSize;i++){
			Person boy = male.get(i);
			Person girl = oneSelect(female,boy);
			if(maleinvite.containsKey(girl)){
				ArrayList boys = maleinvite.get(girl);
				boys.add(boy);
				maleinvite.put(girl, boys);
			}else{
				ArrayList boys = new ArrayList();
				boys.add(boy);
				maleinvite.put(girl, boys);
			}
		}
		return maleinvite;
	}
	/**
	 * 获得最受欢迎的女生
	 * @param map  男生选择女生的集合
	 * @return 最受欢迎的女生
	 */
	public static Person getMaxFemale(HashMap> map){
		Person Maxgirl = null;
		int max = 0;
		Iterator>> iterator = map.entrySet().iterator();
		while(iterator.hasNext()){
			Map.Entry> next = iterator.next();
			Person girl = next.getKey();
			int size = next.getValue().size();
			if(size > max){
				max = size;
				Maxgirl = girl;
			}else if(size == max){
				if(prority(girl,Maxgirl)){
					max = size;
					Maxgirl = girl;
				}
			}
		}
		return Maxgirl;
	}
	/**
	 * 女生反选男生
	 * @param girl  最受欢迎的女生
	 * @param boys 选择这个女生的男生的集合
	 * @return 匹配的男生
	 */
	public static Person femaleAgree(Person girl,ArrayList boys){
		int size = boys.size();
		Person Maxboy = null;
		int max = 0;
		
		for(int i = 0;i < size;i++){
			Person boy = boys.get(i);
			int value = getScore(girl,boy);
			if(value > max){
				max = value;
				Maxboy = boy;
			}else if(value == max){
				if(prority(boy,Maxboy)){
					max = value;
					Maxboy = boy;
				}
			}
		}
		return Maxboy;
	}
	@SuppressWarnings("unchecked")
	public static void main(String[] args){
		FileToList fileTo = new FileToList();
		ArrayList male = fileTo.convey(fileTo.malePath);//初始化男士
		ArrayList female = fileTo.convey(fileTo.femalePath);//初始化女士
		ArrayList player = fileTo.convey(fileTo.playerPath);//初始化主角
		
		for(int i = 0;i < 100;i++){
			Person play =  player.get(i);
			match((ArrayList)male.clone(),(ArrayList)female.clone(),play);
		}
	}
}

 

 

你可能感兴趣的:(java基础,java)