I tried to express sadness and joy with the stable marriage problem.

What is the stable marriage problem (from wikipedia)

The stable marriage problem is one of the stable matching problems, which was proposed in 1962 by D. Gale and L. S. Shapley. An example of a stable marriage problem consists of N men and N women, and each individual's wish list. The wish list is a list of all the opposite sexes arranged in total order based on each individual's preference. The solution to the stable marriage problem is stable matching. For the example of the stable marriage problem, matching in which there is no pair (hereinafter referred to as blocking pair) that is more favorite than the partner currently paired with each other is called stable matching. https://ja.wikipedia.org/wiki/%E5%AE%89%E5%AE%9A%E7%B5%90%E5%A9%9A%E5%95%8F%E9%A1%8C

code

Implemented by Gale-Shapley algorithm.

# coding: utf-8
# Here your code !
import random
class Message:
    def __init__(self,message_list):
        self.message_list = message_list
    def get_message(self):
        return self.message_list[random.randint(0,len(self.message_list)-1)]

class ProposalMessage(Message):
    def __init__(self,man_id,woman_id):
        self.man_id = man_id
        self.woman_id = woman_id
        proposal_messages = [
                            'I love you the most in the world!',
                            "I need you! I can't live without you!",
                            "All I need is you!",
                            "Please make me the happinest man in the world!",
                            "It is similar to die if I can't marry you!"
                            ]
        message_list = map(lambda x: "%s %s" % (x, "Will you marry me?"), proposal_messages)
        Message.__init__(self,message_list)
    def get_man_id(self):
        return self.man_id
    def get_woman_id(self):
        return self.woman_id

class RejectMessage(Message):
    def __init__(self):
        reject_messages = [
                            'I like you. However I regard you as a brother.',
                            "However I think we need more time to know each other. It is too early to response to your proposal.",
                            "I'm sorry, I can't consider about marrige for now.",
                            "I'm sorry, I need a bit of time to consider." 
                            ]
        message_list = map(lambda x: "%s %s" % ("I appreciate your proposal.", x), reject_messages)
        Message.__init__(self,message_list)
    def get_conclution(self):
        return False

class AcceptMessage(Message):
    def __init__(self, left_man_id):
        self.left_man_id = left_man_id
        accept_messages = [
                            "I'm happy to hear that. I've just though to marry you too",
                            "I have the happinest day since I was born!",
                            "What great day!",
                            "Sure! All I need is you too!",
                            "I have dreamed today several times."
                            ]
        message_list = map(lambda x: "%s %s" % ("WOW!", x), accept_messages)
        Message.__init__(self,message_list)
    def get_conclution(self):
        return True
    def left_from(self):
        return self.left_man_id 

class MessageAfterRejected(Message):
    def __init__(self):
        messages_after_rejected = [
                            "But I'll never forget her. She gave me a lot of happy memories...",
                            "I need more beer...",
                            "I can't sleep without wine....",
                            "........."
                            ]
        message_list = map(lambda x: "%s %s" % ("...", x), messages_after_rejected)
        Message.__init__(self,message_list)

class MessageAfterRejecting(Message):
    def __init__(self):
        message_list =      [
                            "How terrible day today! The strange man made me irritate.",
                            "I wonder that he has seen a mirror.",
                            "Beautifulness is guilty.",
                            "I was surprised that he thought that there is possibility to marry me.",
                            "What makes him foolish?"
                            ]
        Message.__init__(self,message_list)

class MessageAfterAccepted(Message):
    def __init__(self):
        messages_after_accepted = [
                            "I'm the happinest man in the world!",
                            "I have to work harder after this.",
                            "I wonder I'm dreaming now!"
                            ]
        message_list = map(lambda x: "%s %s" % ("Hooray!", x), messages_after_accepted)
        Message.__init__(self,message_list)
        
class Man:
    def __init__(self,name,man_id,women):
        self.name = name
        self.man_id = man_id
        self.women = women
        self.engaged = -1
    def propose(self):
        return ProposalMessage(self.man_id,self.women.pop(0))
    def is_single(self):
        return self.engaged == -1
    def engage(self,woman_id):
        self.engaged = woman_id
        return MessageAfterAccepted().get_message()
    def broken(self):
        self.engaged = -1
        return MessageAfterRejected().get_message()

class Woman:
    def __init__(self,name,woman_id,men):
        self.name = name
        self.men = men
        self.engaged = -1
    def response(self,message):
        man_id = message.get_man_id()
        if self.engaged == -1:
            self.engaged = man_id
            return AcceptMessage(-1)
        elif self.men.index(man_id) < self.men.index(self.engaged):
            left_man_id = self.engaged
            self.engaged = man_id
            return AcceptMessage(left_man_id)
        else:
            return RejectMessage()
    def after_rejecting(self):
        return MessageAfterRejecting().get_message()

def shuffle(cards,shuffle_count):
    for i in range(shuffle_count):
        card = cards.pop(random.randint(0,len(cards)-1))
        cards += [card]
    return cards

if __name__ == '__main__':
    men_name = shuffle(['Alex','Bob','Chris','Daniel','Edgar','Frank','Geoff'],100)
    women_name = shuffle(['Alice','Barbara','Charlotte','Dolly','Elisabeth','Flora','Gloriana'],100)
    N = len(men_name)
    men_list, women_list = [], []
    
    for i, man in enumerate(men_name):
        men_list.append(Man(man,i,shuffle(range(N),100)))
    for i, woman in enumerate(women_name):
        women_list.append(Woman(woman,i,shuffle(range(N),100)))

    cotinue_flag = True
    while cotinue_flag:
        cotinue_flag = False
        for man in men_list:
            if man.is_single():
                print "#" * 60
                cotinue_flag = True
                message = man.propose()
                print "%s said '%s'" % (man.name, message.get_message())
                woman = women_list[message.get_woman_id()]
                response = woman.response(message)
                print "%s responsed '%s'" % (woman.name, response.get_message()) 
                if response.get_conclution():
                    print "%s got exciting and said '%s'" % (man.name, man.engage(message.get_woman_id()))
                    if response.left_from() != -1:
                        left_man = men_list[response.left_from()]
                        print "%s got shoked and said '%s'" % ( left_man.name, left_man.broken())
                else:
                    print "%s got shoked and said '%s'" % ( man.name, man.broken())
                    print "%s twittered '%s'" % (woman.name, woman.after_rejecting())
    print "#" * 60
    for man in men_list:
        print "%s engaged with %s" % ( man.name, women_list[man.engaged].name )

Execution result

I felt sadness.

############################################################
Daniel said 'I love you the most in the world! Will you marry me?'
Elisabeth responsed 'WOW! I have dreamed today several times.'
Daniel got exciting and said 'Hooray! I have to work harder after this.'
############################################################
Alex said 'It is similar to die if I can't marry you! Will you marry me?'
Elisabeth responsed 'WOW! I have the happinest day since I was born!'
Alex got exciting and said 'Hooray! I'm the happinest man in the world!'
Daniel got shoked and said '... I can't sleep without wine....'
############################################################
Edgar said 'All I need is you! Will you marry me?'
Charlotte responsed 'WOW! Sure! All I need is you too!'
Edgar got exciting and said 'Hooray! I'm the happinest man in the world!'
############################################################
Geoff said 'Please make me the happinest man in the world! Will you marry me?'
Gloriana responsed 'WOW! I have the happinest day since I was born!'
Geoff got exciting and said 'Hooray! I'm the happinest man in the world!'
############################################################
Chris said 'I love you the most in the world! Will you marry me?'
Gloriana responsed 'WOW! What great day!'
Chris got exciting and said 'Hooray! I wonder I'm dreaming now!'
Geoff got shoked and said '... .........'
############################################################
Frank said 'I love you the most in the world! Will you marry me?'
Alice responsed 'WOW! I have dreamed today several times.'
Frank got exciting and said 'Hooray! I'm the happinest man in the world!'
############################################################
Bob said 'I need you! I can't live without you! Will you marry me?'
Alice responsed 'WOW! Sure! All I need is you too!'
Bob got exciting and said 'Hooray! I wonder I'm dreaming now!'
Frank got shoked and said '... I need more beer...'
############################################################
Daniel said 'It is similar to die if I can't marry you! Will you marry me?'
Alice responsed 'WOW! Sure! All I need is you too!'
Daniel got exciting and said 'Hooray! I'm the happinest man in the world!'
Bob got shoked and said '... I can't sleep without wine....'
############################################################
Geoff said 'I love you the most in the world! Will you marry me?'
Elisabeth responsed 'I appreciate your proposal. However I think we need more time to know each other. It is too early to response to your proposal.'
Geoff got shoked and said '... But I'll never forget her. She gave me a lot of happy memories...'
Elisabeth twittered 'I wonder that he has seen a mirror.'
############################################################
Frank said 'I need you! I can't live without you! Will you marry me?'
Gloriana responsed 'I appreciate your proposal. I like you. However I regard you as a brother.'
Frank got shoked and said '... I need more beer...'
Gloriana twittered 'How terrible day today! The strange man made me irritate.'
############################################################
Bob said 'It is similar to die if I can't marry you! Will you marry me?'
Elisabeth responsed 'WOW! What great day!'
Bob got exciting and said 'Hooray! I have to work harder after this.'
Alex got shoked and said '... But I'll never forget her. She gave me a lot of happy memories...'
############################################################
Alex said 'All I need is you! Will you marry me?'
Gloriana responsed 'WOW! I have dreamed today several times.'
Alex got exciting and said 'Hooray! I'm the happinest man in the world!'
Chris got shoked and said '... I need more beer...'
############################################################
Geoff said 'I love you the most in the world! Will you marry me?'
Barbara responsed 'WOW! Sure! All I need is you too!'
Geoff got exciting and said 'Hooray! I wonder I'm dreaming now!'
############################################################
Chris said 'I need you! I can't live without you! Will you marry me?'
Elisabeth responsed 'I appreciate your proposal. However I think we need more time to know each other. It is too early to response to your proposal.'
Chris got shoked and said '... .........'
Elisabeth twittered 'I wonder that he has seen a mirror.'
############################################################
Frank said 'I love you the most in the world! Will you marry me?'
Elisabeth responsed 'I appreciate your proposal. However I think we need more time to know each other. It is too early to response to your proposal.'
Frank got shoked and said '... I can't sleep without wine....'
Elisabeth twittered 'I wonder that he has seen a mirror.'
############################################################
Chris said 'I need you! I can't live without you! Will you marry me?'
Charlotte responsed 'WOW! What great day!'
Chris got exciting and said 'Hooray! I'm the happinest man in the world!'
Edgar got shoked and said '... I can't sleep without wine....'
############################################################
Frank said 'Please make me the happinest man in the world! Will you marry me?'
Barbara responsed 'I appreciate your proposal. I like you. However I regard you as a brother.'
Frank got shoked and said '... I need more beer...'
Barbara twittered 'Beautifulness is guilty.'
############################################################
Edgar said 'I need you! I can't live without you! Will you marry me?'
Flora responsed 'WOW! I'm happy to hear that. I've just though to marry you too'
Edgar got exciting and said 'Hooray! I have to work harder after this.'
############################################################
Frank said 'I need you! I can't live without you! Will you marry me?'
Dolly responsed 'WOW! I have dreamed today several times.'
Frank got exciting and said 'Hooray! I wonder I'm dreaming now!'
############################################################
Daniel engaged with Alice
Alex engaged with Gloriana
Edgar engaged with Flora
Geoff engaged with Barbara
Chris engaged with Charlotte
Frank engaged with Dolly
Bob engaged with Elisabeth

Recommended Posts

I tried to express sadness and joy with the stable marriage problem.
I tried to solve the problem with Python Vol.1
I tried to save the data with discord
I tried to learn the angle from sin and cos with chainer
I tried to implement the traveling salesman problem
I tried to control the network bandwidth and delay with the tc command
[Introduction to AWS] I tried porting the conversation app and playing with text2speech @ AWS ♪
I tried to learn the sin function with chainer
I tried to read and save automatically with VOICEROID2 2
I tried to implement and learn DCGAN with PyTorch
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
I tried to automatically read and save with VOICEROID2
I tried to implement Grad-CAM with keras and tensorflow
I tried to solve the virtual machine placement optimization problem (simple version) with blueqat
I tried to automate the article update of Livedoor blog with Python and selenium.
I tried to compare the processing speed with dplyr of R and pandas of Python
The 15th offline real-time I tried to solve the problem of how to write with python
I tried to predict and submit Titanic survivors with Kaggle
I tried to find the entropy of the image with python
I tried to simulate how the infection spreads with Python
I tried to analyze the whole novel "Weathering with You" ☔️
I tried to find the average of the sequence with TensorFlow
I tried to notify the train delay information with LINE Notify
I tried to solve a combination optimization problem with Qiskit
I tried to illustrate the time and time in C language
I tried to display the time and today's weather w
I tried to enumerate the differences between java and python
I tried to make GUI tic-tac-toe with Python and Tkinter
I tried to divide the file into folders with Python
I tried to automatically post to ChatWork at the time of deployment with fabric and ChatWork Api
How to write offline real time I tried to solve the problem of F02 with Python
I tried to get the number of days of the month holidays (Saturdays, Sundays, and holidays) with python
I also tried to imitate the function monad and State monad with a generator in Python
I tried to move the ball
I tried to estimate the interval.
I tried to describe the traffic in real time with WebSocket
I tried to solve the ant book beginner's edition with python
I tried to automate the watering of the planter with Raspberry Pi
I tried to visualize bookmarks flying to Slack with Doc2Vec and PCA
I tried to process the image in "sketch style" with OpenCV
I tried to make a periodical process with Selenium and Python
I tried to get started with Bitcoin Systre on the weekend
I wanted to solve the ABC164 A ~ D problem with Python
I tried to create Bulls and Cows with a shell program
I tried to process the image in "pencil style" with OpenCV
I tried to expand the size of the logical volume with LVM
I tried to easily detect facial landmarks with python and dlib
I tried to solve the shift scheduling problem by various methods
I tried to improve the efficiency of daily work with Python
I tried to create serverless batch processing for the first time with DynamoDB and Step Functions
I tried to implement Autoencoder with TensorFlow
I tried to summarize the umask command
I tried to visualize AutoEncoder with TensorFlow
I tried to recognize the wake word
I tried to summarize the graphical modeling.
I tried to estimate the pi stochastically
I tried to touch the COTOHA API
Python: I tried the traveling salesman problem
I tried to implement CVAE with PyTorch
I tried playing with the image with Pillow