婚姻匹配

题目:建立一个模型,来模拟推导社会男女择偶过程。 为了模型简化,一个人的特性指标有三个,这里假设为财富、样貌、品格,每个指标均可取值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、循环该配对法则,直到最后一对男女配对成功。

问题分析:

1,建立男女属性类,类中有每个人的个人信息。

2,将存储男女信息和主角的文件转化为Arrylist存储每个人的信息。

3,每次加入一个主角,然后匹配,匹配时需注意先让男生选择心动女生,然后在女生反选男生。

4,清楚上次的过程,重复进行。

package tulun0803;

import java.util.ArrayList;

class person{
	public int id;
	/**
	 * 自己的颜值 品格 财富
	 */
	public int apperance;
	public int character;
	public int wealth;
	/**
	 * 期望的颜值 品格 财富
	 */
	public int expectApperance;
	public int expectCharacter;
	public int expectWealth;
	/**
	 * 性别
	 */
	public int sex;
	public person(int id, int apperance, int character, int wealth, int expectApperance, int expectCharacter, int expectWealth,int sex) {
        this.id = id;
        this.apperance = apperance;
        this.character = character;
        this.wealth = wealth;
        this.expectApperance = expectApperance;
        this.expectCharacter = expectCharacter;
        this.expectWealth = expectWealth;
        this.sex = sex;
        this.list = new ArrayList<person>();
    }
	public int getId() {
		return id;
	}
	/**
	 * 链表 用来存储一个女生被多个男生喜欢的男生列表
	 */
	public ArrayList<person> list;
	public void setId(int id) {
		this.id = id;
	}
	public int getApperance() {
		return apperance;
	}
	public void setApperance(int apperance) {
		this.apperance = apperance;
	}
	public int getCharacter() {
		return character;
	}
	public void setCharacter(int character) {
		this.character = character;
	}
	public int getWealth() {
		return wealth;
	}
	public void setWealth(int wealth) {
		this.wealth = wealth;
	}
	public int getExpectApperance() {
		return expectApperance;
	}
	public void setExpectApperance(int expectApperance) {
		this.expectApperance = expectApperance;
	}
	public int getExpectCharacter() {
		return expectCharacter;
	}
	public void setExpectCharacter(int expectCharacter) {
		this.expectCharacter = expectCharacter;
	}
	public int getExpectWealth() {
		return expectWealth;
	}
	public void setExpectWealth(int expectWealth) {
		this.expectWealth = expectWealth;
	}
	public int getSex() {
		return sex;
	}
	public void setSex(int sex) {
		this.sex = sex;
	}
	@Override
	public String toString() {
		return "person [id=" + id + ", apperance=" + apperance + ", character=" + character + ", wealth=" + wealth
				+ ", expectApperance=" + expectApperance + ", expectCharacter=" + expectCharacter + ", expectWealth="
				+ expectWealth + ", sex=" + sex + ", list=" + list + "]";
	}
}
public class Demo1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 
	}

}
package tulun0803;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class TestDemo {

	public static void main(String[] args) {
        Scanner in = null;
        Scanner in1 = null;
        try {
            in = new Scanner(new File("C:\\Users\\吟厄克呦\\Desktop\\Java男女匹配作业1.txt"));
            in1 = new Scanner(new File("C:\\Users\\吟厄克呦\\Desktop\\Java男女匹配作业2.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        int x = 0;
        while (in.hasNextLine()) {
            String i = in.nextLine();
            String i1 = in1.nextLine();
            System.out.println(i+"    "+i1);
            if(!i.equals(i1))
            {
                System.out.println(x);
            }
            x++;
        }
    }

}
package tulun0803;

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

public class User {
	/**
     * 将Person文件转换为一个集合
     * @param filePath  文件路径
     * @param sex          性别
     * @return          ArrayList集合
     */
    public static ArrayList<person> FIleTrasPersonClassList(String filePath,int sex)
    {   Scanner in = null;
        ArrayList<person> people = new ArrayList<person>();
        try {
            in = new Scanner(new File(filePath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        while (in.hasNextLine()) {
            String i = in.nextLine();
            String[] split = i.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
                    ));

        }
        return people;
    }

    /**
     * 将主角文件转化为 ArrayLis
     * @param filePath 文件路径
     * @return ArrayList集合
     */
    public static ArrayList<person> FIleTrasPlayerClassList(String filePath)
    {
        Scanner in = null;
        ArrayList<person> people = new ArrayList<person>();
        try {
            in = new Scanner(new File(filePath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        while (in.hasNextLine()) {
            String i = in.nextLine();
            String[] split = i.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])
            ));

        }
        return people;
    }
}
package tulun0803;

import java.util.ArrayList;
import java.util.HashMap;

public class main {
	 /**
     * 进行计数
     */
    private static int x=1  ;
    private static int couple  = 1000;
    private static  long start = System.currentTimeMillis();
    public static void main(String[] args) throws CloneNotSupportedException {

        //男人的文件路径
        String malePath = "C:\\Users\\14831\\Desktop\\Java男女匹配作业1.txt";
        //进行转换
        ArrayList<person> males = User.FIleTrasPersonClassList(malePath,1);
        //女人的文件路径
        String famalPath="C:\\Users\\14831\\Desktop\\Java男女匹配作业2.txt";
        //进行转换
        ArrayList<person> females = User.FIleTrasPersonClassList(famalPath,0);
        //主角文件路径
        String playerPath="C:\\Users\\14831\\Desktop\\Java男女匹配作业3.txt";
        //转换
        ArrayList<person> players = User.FIleTrasPlayerClassList(playerPath);
       // ArrayList<Person> players = new ArrayList<Person>();
       // players.add(new Person(-1,10,100,73,31,46,23,0));
        //逐个加入主角进行匹配
        int size = players.size();
        //Collections.sort(males);
        //Collections.sort(females);
        System.out.println(males);
        long l = System.currentTimeMillis();
        for (int i = 0; i < size; i++) {
            person player = players.get(i);
            //匹配
            Mate((ArrayList<person> )males.clone(), (ArrayList<person> )females.clone(), player);
        }
        System.out.println(System.currentTimeMillis()-start);
    }
    /**
     * 匹配
     * @param males     男性数组
     * @param females   女性数组
     * @param player    主角
     */
    public static void Mate(ArrayList<person> males,ArrayList<person> females,person player) {
        int coup = males.size();
        int sex = player.getSex();
        //根据性别不同放入到不同的集合中
        if (sex == 0) {
            females.add(player);
        } else {
            males.add(player);
        }
        //进行couple次配对
        int p;
        HashMap<person, ArrayList<person>> femaleScore;
        int maleSize;
        int femaleSize;
        for (p = 0; p < coup; p++) {
            maleSize = males.size();
            femaleSize = females.size();
            //统计每个女生的追求者
            femaleScore = new HashMap<person, ArrayList<person>>(maleSize);
            //让每个男的都选择自己喜欢的女生,并且统计每个女生被喜欢数
            for (int i = 0; i < maleSize; i++) {
                person male = males.get(i);
                int max = -1;
                person Maxfamale = null;
                for (int i1 = 0; i1 < femaleSize; i1++) {
                    person female = females.get(i1);
                    int score = GetScore(male, female);
                    if (score > max||(score==max&&Select(female,Maxfamale))) {
                        max = score;
                        Maxfamale = female;
                    }
                }
                boolean isOne = !femaleScore.containsKey(Maxfamale);
                if (isOne) {
                    ArrayList<person> people = new ArrayList<person>(maleSize);
                    people.add(male);
                    femaleScore.put(Maxfamale, people);
                } else {
                    ArrayList<person> people = femaleScore.get(Maxfamale);
                    people.add(male);
                    femaleScore.put(Maxfamale, people);
                }
            }
            int maxFemaleNum = -1;
            ArrayList<person> maxFemaleList = null;
            person maxFemale = null;
            //找到最受欢迎的女生
            int maxFemaleId = 0;
            for (int i = 0; i < femaleSize; i++) {
                person person = females.get(i);
                //ArrayList<Person> people = femaleScore.get(person);
                ArrayList<person> people = femaleScore.get(person);
                if (people != null) {
                    int peopleSize = people.size();
                    if ((peopleSize > maxFemaleNum)||(people.size() == maxFemaleNum&&Select(person, maxFemale))) {
                        maxFemaleNum = peopleSize;
                        maxFemale = person;
                        maxFemaleList = people;
                        maxFemaleId = i;
                    }
                }
            }
            //最喜欢的女生挑选男生
            if (maxFemaleList != null) {
                int maxFemaleListSize = maxFemaleList.size();
                int maxScore = 0;
                person maxMale = null;
                int maxMaleId = 0;
                for (int i = 0; i < maxFemaleListSize; i++) {
                    person person = maxFemaleList.get(i);
                    int score = GetScore(maxFemale, person);
                    if ((score > maxScore)||(score == maxScore&&Select(person, maxMale))) {
                        maxScore = score;
                        maxMale = person;
                        maxMaleId = i;
                    }
                }
                //根据主角的性别进行判断输出
                if (sex == 0) {
                    if (maxFemale.getId()==-1) {
                        System.out.println("第" + (x++) + "组player加入" + maxMale.getId() + ":" + player.getId()+"   当前已用时间"+(System.currentTimeMillis()-start));
                        break;
                    }
                } else {
                    if(maxMale.getId()==-1) {
                        System.out.println("第" + (x++) + "组player加入" + player.getId() + ":" + maxFemale.getId()+"   当前已用时间"+(System.currentTimeMillis()-start));
                        break;
                    }
                }
                males.remove(maxMaleId);
                females.remove(maxFemaleId);
            }
        }
        if(p==coup)
        {
            System.out.println("第" + (x++) + "组player加入  无结果"+"   当前已用时间"+(System.currentTimeMillis()-start));
        }

    }

    /**
     * One对Two的分数
     * @param  one
     * @param  two
     * @return 分数
     */
    public static int  GetScore(person one ,person two)
    {
        return one.expectApperance*two.apperance+
                one.expectCharacter*two.character+
                one.expectWealth*two.wealth;
    }

    /**
     * one比two优先
     * @param one
     * @param two
     * @return 如果one比two大返回ture
     */
    private static boolean Select(person one, person two)
    {
        int sumOne = one.apperance+one.character+one.wealth;
        int sumTwo = two.apperance+two.character+two.wealth;
        if(sumOne>sumTwo) {
            return true;
        }
        else if(sumOne<sumTwo) {
            return false;
        }
        else {
            return one.id <= two.id;
        }
    }
	

}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值