男女匹配

package com.tulun;
/**  
* 描述:TODO
*  
* @author ASUS  
* @date 2018年7月21日
*/
public class Person {
	private int id;
    private int apperance;
    private int character;
    private int wealth;
    private int expectApperance;
    private int expectCharacter;
    private int expectWealth;
    private int sex;
		/**
		 * @param id
		 * @param sex
		 * @param wealth
		 * @param character
		 * @param apperance
		 * @param expectWealth
		 * @param expectCharacter
		 * @param expectApperance
		 */
		
   
		/**
		 * @param id
		 * @param apperance
		 * @param character
		 * @param wealth
		 * @param expectApperance
		 * @param expectCharacter
		 * @param expectWealth
		 * @param sex
		 */
		public Person(int id, int apperance, int character, int wealth, int expectApperance, int expectCharacter,
				int expectWealth, int sex) {
			super();
			this.id = id;
			this.apperance = apperance;
			this.character = character;
			this.wealth = wealth;
			this.expectApperance = expectApperance;
			this.expectCharacter = expectCharacter;
			this.expectWealth = expectWealth;
			this.sex = sex;
		}
		 public int getId() {
				return id;
			}
		public void setId(int id) {
			this.id = id;
		}
		public int getSex() {
			return sex;
		}
		public void setSex(int sex) {
			this.sex = sex;
		}
		public int getWealth() {
			return wealth;
		}
		public void setWealth(int wealth) {
			this.wealth = wealth;
		}
		public int getCharacter() {
			return character;
		}
		public void setCharacter(int character) {
			this.character = character;
		}
		public int getApperance() {
			return apperance;
		}
		public void setApperance(int apperance) {
			this.apperance = apperance;
		}
		public int getExpectWealth() {
			return expectWealth;
		}
		public void setExpectWealth(int expectWealth) {
			this.expectWealth = expectWealth;
		}
		public int getExpectCharacter() {
			return expectCharacter;
		}
		public void setExpectCharacter(int expectCharacter) {
			this.expectCharacter = expectCharacter;
		}
		public int getExpectApperance() {
			return expectApperance;
		}
		public void setExpectApperance(int expectApperance) {
			this.expectApperance = expectApperance;
		}
	  
        
	   
}
package com.tulun;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;


/**  
* 描述:TODO
*  
* @author ASUS  
* @date 2018年7月21日
*/
public class Unit {
	public static ArrayList<Person>personFIleTrasArrayList(String filePath,int sex) throws IOException  {
       
        ArrayList<Person> people = new ArrayList<Person>();
      
        try {
        	//创建并读取指定路径的文件读取
            FileReader in =  new FileReader(filePath);
            //带有缓冲区的字符流
            BufferedReader buffer = new BufferedReader(in);
           String line;
			while ((line = buffer.readLine())!= null) {
			   
			    String[] split = line.split(",");
			    people.add(new Person(Integer.parseInt(split[0]),
			            Integer.parseInt(split[1]),
			            Integer.parseInt(split[2]),
			            Integer.parseInt(split[3]),
			            Integer.parseInt(split[4]),
			            Integer.parseInt(split[5]),
			            Integer.parseInt(split[6]),
			            sex
			            ));

			}
			buffer.close();
			in.close();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return people;
    }

    /**
     * 将主角文件转化为 ArrayList
     * @param filePath 文件路径
     * @return ArrayList集合
     * @throws FileNotFoundException 
     */
    public static ArrayList<Person> playerFIleTrasArrayList(String filePath) throws FileNotFoundException  {
    	ArrayList<Person> people = new ArrayList<Person>();
        
       
        try {
        	 FileReader in =  new FileReader(filePath);
             BufferedReader buffer = new BufferedReader(in);
            String line;
			while ((line = buffer.readLine())!= null) {
			    
			    String[] split = line.split(",");
			    people.add(new Person(-1,
			            Integer.parseInt(split[1]),
			            Integer.parseInt(split[2]),
			            Integer.parseInt(split[3]),
			            Integer.parseInt(split[4]),
			            Integer.parseInt(split[5]),
			            Integer.parseInt(split[6]),
			            Integer.parseInt(split[0])
			    ));

			}
			buffer.close();
			in.close();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return people;
    }
}
package com.tulun;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

/**  
* 描述:TODO
*  
* @author ASUS  
* @date 2018年7月21日
*/
public class Mapping {

    /**
     * 匹配次数
     */
    public static final int NUMBER = 100;
    /**
     * 组别统计,从第一组开始
     */
    public static int group = 1;

    public static void maping(ArrayList<Person> male, ArrayList<Person> female, Person player) {

        // 配对好的男女统计
        HashMap<Person, Person> maleFemale = new HashMap<>();
        HashMap<Person, Person> femaleMale = new HashMap<>();

        // 主角加入配对
        int playerSex = player.getSex();
        if (playerSex == 1) {
            male.add(player);
        } else {
            female.add(player);
        }

        //进行100位男生对女生发出的邀请
        for (int c = 0; c < NUMBER; c++) {

            // 统计女生的追求者
            HashMap<Person, ArrayList<Person>> maleToFemale= new HashMap<>(female.size());
            
            for (int i = 0; i < male.size(); i++) {
                // 男生对女生的满意度
                int maxFemaleDegree = 0;

                // 男生的心仪女生
                Person loveFemale = null;

                // 在所有女生中寻找男生的心动女生
                for (int j = 0; j < female.size(); j++) {
                    int degree = GetScore(male.get(i), female.get(j));
                    if (degree == maxFemaleDegree) {
                        // 满意度相同的处理
                        if (Select(female.get(j), loveFemale)) {
                            maxFemaleDegree = degree;
                            loveFemale = female.get(j);
                        }
                    }
                    if (degree > maxFemaleDegree) {
                        maxFemaleDegree = degree;
                        loveFemale = female.get(j);
                    }

                }

                /*
                 *如果女生只有一个男生喜欢,将他俩的组合记录
                 *如果一个女生被多个男生喜欢,将该男生记录到喜欢该女生的男生队列下
                 */
                if (!maleToFemale.containsKey(loveFemale)) {
                    ArrayList<Person> arrayList = new ArrayList<>();
                    arrayList.add(male.get(i));
                    maleToFemale.put(loveFemale, arrayList);
                } else {
                    ArrayList<Person> arrayList = maleToFemale.get(loveFemale);
                    arrayList.add(male.get(i));
                    maleToFemale.put(loveFemale, arrayList);
                }
            }

            // 喜欢某女生的男生集合
            ArrayList<Person> arrayList = null;

            // 最受欢迎的女生
            int maxLength = 0;

            // 最受男生欢迎的女生
            Person maxLoveFemale = null;

            // 寻找最受欢迎的女生
            for (int i = 0; i < female.size(); i++) {
                Person p = female.get(i);
                if (maleToFemale.get(p) != null) {
                    if (maleToFemale.get(p).size() == maxLength) {
                        // 有两个女生追求的男生数量相同
                        if (Select(p, maxLoveFemale)) {
                            maxLoveFemale = p;
                            maxLength = maleToFemale.get(p).size();
                            arrayList = maleToFemale.get(p);
                        }
                    }
                   // 最受欢迎的女生从她喜欢的男生中选出最喜欢的
                    if (maleToFemale.get(p).size() > maxLength) {
                        maxLoveFemale = p;
                        maxLength = maleToFemale.get(p).size();
                        arrayList = maleToFemale.get(p);
                    }
                }
            }

            // 
            if (arrayList != null) {
                int maxMaleDegree = 0;
                Person maxLoveMale = null;
                for (int i = 0; i < arrayList.size(); i++) {
                    int degree = GetScore(maxLoveFemale, arrayList.get(i));
                    if (degree == maxMaleDegree) {
                        // 满意度相同处理
                        if (Select(arrayList.get(i), maxLoveMale)) {
                            maxMaleDegree = degree;
                            maxLoveMale = arrayList.get(i);
                        }
                    }
                    if (degree > maxMaleDegree) {
                        maxMaleDegree = degree;
                        maxLoveMale = arrayList.get(i);
                    }
                }

                // 记录匹配成功该对男女
                maleFemale.put(maxLoveMale, maxLoveFemale);
                femaleMale.put(maxLoveFemale, maxLoveMale);
                //删除该对男女
                male.remove(maxLoveMale);
                female.remove(maxLoveFemale);
            }
        }

        if (playerSex == 0) {
            if (femaleMale.get(player) != null) {
                Person person = femaleMale.get(player);
                System.out.println("第" + (group++) + "组player加入" + person.getId() + ":" + player.getId());
            } else {
            	System.out.println("第" + (group++) + "组player加入  无结果");
            }
        } else {
            if (maleFemale.get(player) != null) {
                Person person = maleFemale.get(player);
                System.out.println("第" + (group++) + "组player加入" + player.getId() + ":" + person.getId());
            } else {
            	System.out.println("第" + (group++) + "组player加入  无结果");
            }
        }
    }

	

    /**
     * a对b的分数
     * @param  a
     * @param  b
     * @return 分数
     */
    public static int  GetScore(Person a ,Person b)
    {
        return a.getExpectApperance()*b.getApperance()+
                a.getExpectCharacter()*b.getCharacter()+
                a.getExpectWealth()*b.getWealth();
    }

    /**
     * 分数一样时,比较对自身的评价。如果还一致,就根据id的排序。
     * @param a
     * @param b
     * @return 如果a比b大返回ture
     */
    private static boolean Select(Person a, Person b)
    {
        int sumOne = a.getApperance()+a.getCharacter()+a.getWealth();
        int sumTwo = b.getApperance()+b.getCharacter()+b.getWealth();
        if(sumOne>sumTwo) {
            return true;
        }
        else if(sumOne<sumTwo) {
            return false;
        } else {
            return a.getId() <= b.getId();
        }
    }

	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		//传入男人的文件
        String malePath = "C:/Users/ASUS/Desktop/1000组待匹配数据/male.txt";
        //文件转换成集合
        ArrayList<Person> males = Unit.personFIleTrasArrayList(malePath,1);
        //传入女人的文件
        String famalPath="C:/Users/ASUS/Desktop/1000组待匹配数据/female.txt";
        //文件转换成集合
        ArrayList<Person> females = Unit.personFIleTrasArrayList(famalPath,0);
        //传入主角文件
        String playerPath="C:/Users/ASUS/Desktop/1000组待匹配数据/players.txt";
        //文件转换成集合
        ArrayList<Person> players = Unit.playerFIleTrasArrayList(playerPath);
        //逐个加入主角进行匹配
        for (int i = 0; i < players.size() ; i++) {
            //匹配
        	maping((ArrayList<Person>)males.clone(),(ArrayList<Person>) females.clone(),players.get(i));
        }

    }
	

}

猜你喜欢

转载自blog.csdn.net/qq_41974391/article/details/81161128