"Algorithmic" Game Theory in Love (stable matching) (with source)

Source: Internet
Author: User

The idea comes from knowing: Are there games in love? , mostly the answer of Nick Yu. thank him. Then some of the original text is not clear, I directly follow my understanding to add up.


Note: This article uses C # to implement


First of all, assume a virtual world, the world has n boys, n Girls, boys and girls, the number is exactly the same, boys and girls have a favorite object list, different people's favorite object list is random, in the Favorite object list the more front, the more he (she) like. Boys can tell the girls, girls can not tell the boys, girls to accept or reject the boy's confession.


Boys will first to his favorite girl, and then love, and then go on to vindicate. And the girls through the confession of the boys to judge, if he is the list of his favorite ranked first, well, it is him, he is the most suitable for me. But if not, the girl will say, sorry, you are a good person, and then rejected the boys. Then the girl will think, is not I ask too high, that ranked second can also. Then the girls relaxed the ranking to second place. But accordingly, the second place is not the girl's best companion, if she accepts the second invitation, it means that her happiness is not the highest. Of course, if the second to her confession, the first to her confession, she will mercilessly shake off the second, and then the first place.


OK, based on the above requirements, then first of all to design a human, human needs to have the following properties, are numbered (easy to understand and sort), gender, the object list of objects, expected objects, partners. Then this class must be able to confess, to accept the confession. There are so many main functions. The following is the design of the class

    Enum Sex    {man        ,        Woman    }    class Human    {        int _index = 0;                   Index public        int Index {get {return _index;}}        Sex _sex = Sex.man;               Sex public Sex sex        {get {return _sex;}}        list

The first is the most important confession procedure, the first to get to the right object list, and the object of the right to confess, if the object accepted, that OK, love it; If the object does not accept, well, sad, continue and next confession. hahaha. The following is the implementation of the confession function

        Confession public        bool Confession ()        {            if (_lovelist.count >= getloveindex ())            {for                (int i = 0; i < Get Loveindex (); i++)                {                    form. Addlist (String. Format ("{0} # to {1}", index, _lovelist[i].index));                    if (_lovelist[i]. Accept (this))      //vindicate                    {                        //Success                        //Set companion                        _partner = _lovelist[i];                        return true;                    }                    else                    {                        //fail/                        Decrease Happiness                        addloveindex ();            }}} return false;        }

Confess the sister paper to be sure to accept or not, first sister paper to see this person himself like not like, if not like, directly refused to discuss, if you like, and then see if you have a boyfriend, have a boyfriend, see which is better, if the boyfriend is poor, then directly dumped the boyfriend. Sister paper is Jiangzi Qaq

Consider whether to accept public bool Accept (Human Askman) {if (_lovelist.count >= getloveindex ()) {var list = _lovelist.take (Getloveindex ()).                ToList (); var man = list.                FirstOrDefault (x = X._index = = Askman._index);                         if (man! = null) {//Object single if (_partner! = null) {                        _partner.partner = null; _partner.                        Addloveindex (); Form. Addlist (String.                    Format ("{0} was lovelorn, his partner {1} accepted the declaration of {2}", _partner.index, Index, askman.index));                    } _partner = Askman;                return true;                    } else {//female survivors are single when Addloveindex ();                return false;        }} return false; }


Then is the virtual world of the confession algorithm, the virtual world rules: 1. Only men can say 2. Only a single man can confess.

        Start confession        void Startconfession ()        {            addlist ("Start Confession");            foreach (Var p in people)            {                if (p.sex = = Sex.man && P.partner = = null)                {                    bool result = P.confessio n ();                    if (result = = True)                    {                        //Love                        string msg = string. Format ("{0} number with {1} number in Love", P.index, P.partner.index);                        Addlist (msg);                    }                    else                    {                        addlist (string. Format ("{0} failed to vindicate", P.index));}}}        

Then the result is that women's happiness is lower than men's, and I set up 1000 sets of data to see who has a higher sense of well-being (note that the higher the value of happiness in the program means the lower the happiness, see the function Addloveindex for more details) (PS: This is just the result of running once, Run multiple results may vary)



SOURCE Click Open link


"Algorithmic" Game Theory in Love (stable matching) (with source)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.