+ Reply to Thread
Results 1 to 19 of 19

Gale Shapley matching (Stable Marriage problem)

  1. #1
    Registered User
    Join Date
    07-26-2013
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2010
    Posts
    20

    Gale Shapley matching (Stable Marriage problem)

    I am trying to create a function that performs a special type of preference-matching of certain items with one another.

    This special-matching function is essentially just the "Gale–Shapley algorithm" that is best known and expressed (in logic / mathematics) as the "Stable Marriage problem".

    So far, I have been completely unsuccessful in finding any sort of online resources that would enable such an algorithm to work in Excel, much less able to find any sources that even talk about it in regards to Excel. I'm starting to think now that, perhaps, this particular algorithm might just be too sophisticated for Excel and/or VBA to manage.

    If anyone can please shed some light and offer assistance, I would greatly appreciate it. Is this function even possible in Excel?

    In case you are not familiar with the "Stable Marriage problem" and its solution with the Gale-Shapley algorithm, here is a quick video that explains it. Otherwise, you can just google it for a basic Wiki read.

    https://www.youtube.com/watch?v=Qcv1IqHWAzg

    https://www.youtube.com/watch?v=FIS7hfPfZOE


    NOTE: Here is a link to Rosettacode.org, which contains several different language codes to build this Gale-Shapley algorithm.

    https://rosettacode.org/wiki/Stable_...ge_problem#Lua


    Thank you.

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Gale Shapley matching (Stable Marriage problem)

    Fascinating little problem! Thanks for posting it, I've enjoyed spending the last couple of hours working through this logic!

    See attachment for a worked example. I used the example dataset from Rosetta Code, stored in a couple of listobject tables.

    The code reads in the arrays of Men and Women with their preferences, then loops through, according to the algorithm:
    Quote Originally Posted by Wikipedia
    Please Login or Register  to view this content.
    You could easily tweak the input source(s). The output is stored in the respective arrays, with a text log written to the log sheet. You could do whatever you like with the output.

    Here's my code:
    Please Login or Register  to view this content.
    I enjoyed that!
    Attached Files Attached Files
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Registered User
    Join Date
    07-26-2013
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Gale Shapley matching (Stable Marriage problem)

    This is great! Thank you so much!

    Just two questions

    1. If we wanted to insert an additional qualifier which restricts the matching process even further by adding another (secondary) preference rule, where exactly in the coding would we do that?

    For example, let's say we wanted to add another preference rule that selects for 'age'. Assuming we have already established the default preferences for everyone (which we have), we start assigning age preferences to everyone on the list. Abi's first age preference would be anyone in the age-rage of 21 - 28. Her second age-range preference would extend to anyone aged 18 - 35. Finally, her last preference is to accept anyone of any age. Bob, for example, will ONLY accept anyone in the age-range of 24 - 28. Dan has NO age-range preference at all, and is willing to accept anyone of any age.

    If this additional layer of complexity is too complex to answer and develop, then I completely understand and I am still very satisfied with what you have provided so far.

    2. Which language-source code did you use here from Rosetta Code? If you already stated that; then sorry, I didn't catch that part.

    Again, thank you very much for helping!

    - Taylor

  4. #4
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Gale Shapley matching (Stable Marriage problem)

    I'll have a think about adding additional criteria, and how that might work.


    Quote Originally Posted by tshobe View Post
    2. Which language-source code did you use here from Rosetta Code? If you already stated that; then sorry, I didn't catch that part.
    I didn't use any code from Rosetta. I got the Gale Shapley algorithm from Wikipedia (quoted in my first post), and wrote the VA code based on that.
    I used the sample data on the Rosetta Code page:

    Quote Originally Posted by Rosetta Code - Stable Marriage
    Please Login or Register  to view this content.

    I've now uploaded a (non-Excel specific) version of my VBA code to the Rosetta Code page.
    Last edited by Olly; 08-09-2016 at 03:12 AM.

  5. #5
    Registered User
    Join Date
    07-26-2013
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Gale Shapley matching (Stable Marriage problem)

    Oh, I see. Fantastic, thanks!

    I'll be on stand-by if you can think of any way to accommodate additional criteria.

    Thanks!

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Gale Shapley matching (Stable Marriage problem)

    I think this code might suffice (based on Olly's file)

    Please Login or Register  to view this content.
    An Excel independent variant:

    Please Login or Register  to view this content.
    Last edited by snb; 08-10-2016 at 11:28 AM.



  7. #7
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Gale Shapley matching (Stable Marriage problem)

    Ouch @snb. I always loved your code!!!
    Be fore warned, I regularly post drunk. So don't take offence (too much) to what I say.
    I am the real 'Napster'
    The Grid. A digital frontier. I tried to picture clusters of information as they moved through the computer. What did they look like? Ships? motorcycles? Were the circuits like freeways? I kept dreaming of a world I thought I'd never see. And then, one day...

    If you receive help please give thanks. Click the * in the bottom left hand corner.

    snb's VBA Help Files

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Gale Shapley matching (Stable Marriage problem)

    now more dictionary oriented (this is exemplary why/when you should use dictionaries)

    Please Login or Register  to view this content.
    Last edited by snb; 08-12-2016 at 05:08 AM.

  9. #9
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Gale Shapley matching (Stable Marriage problem)

    @tshobe

    You are not introducing additional criteria to this, you are only specifying the original data (i.e the preferences in both sets).

  10. #10
    Registered User
    Join Date
    07-26-2013
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Gale Shapley matching (Stable Marriage problem)

    @snb

    Thank you very much for assisting and providing your code.

    In regards to terminology being used; Olly and I were phrasing it that way referring to non-coding terminology before getting into programming / coding / data analytics jargon.

    Couple of questions, if you don't mind helping me with this, SNB;

    1) What is an "Excel independent variant"? and what is its use here?

    2) The more "dictionary-oriented" version is just supposed to be a non-coding (non-VBA?) friendlier version, right? Just easier to read? I do not have access to excel at the moment now.

    BTW, I am new to this and know very little about VBA, much less any coding in general.

    Thank you.
    Last edited by tshobe; 08-14-2016 at 01:12 AM.

  11. #11
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Gale Shapley matching (Stable Marriage problem)

    VBA consists of several libraries.
    There's a special VBA library that contains methods etc. that can only be applied to Excel.
    If you only use VBA code that doesn't need any 'Excel-VBA' it is Excel independent.

    The Dictionary is also part of a VBA library.
    So the 'Dictionary oriented' variant is VBA.

    See also:

    https://rosettacode.org/wiki/Stable_...ge_problem#VBA

  12. #12
    Registered User
    Join Date
    07-26-2013
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Gale Shapley matching (Stable Marriage problem)

    Alright.

    Thank you, snb.

  13. #13
    Registered User
    Join Date
    09-20-2016
    Location
    Washingtion, D.C.
    MS-Off Ver
    2010
    Posts
    2

    Re: Gale Shapley matching (Stable Marriage problem)

    You guys are king. Thank you so much @Olly et al.

    Now, can you make it so that all the couples "divorce" and remarry someone else, ensuring that they don't remarry their ex in MS Excel?

  14. #14
    Registered User
    Join Date
    04-01-2019
    Location
    NC, USA
    MS-Off Ver
    2016
    Posts
    1

    Question Re: Gale Shapley matching (Stable Marriage problem)

    Hi! It's been quite a long time since the original posts ... hope y'all are still around!

    Can you modify the code to handle cases where a 1:1 match is not possible? Specifically, when the the group sizes are different. For example, if there are only 8 men vs. 10 women?

  15. #15
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,926

    Re: Gale Shapley matching (Stable Marriage problem)

    rprasadusa welcome to the forum

    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original. Please start a new thread - See Forum rule #4

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  16. #16
    Registered User
    Join Date
    09-20-2016
    Location
    Washingtion, D.C.
    MS-Off Ver
    2010
    Posts
    2
    Can you modify the code to handle cases where a 1:1 match is not possible? Specifically, when the the group sizes are different. For example, if there are only 8 men vs. 10 women?

    You might not need to, just tailor inputs. Depending on your scenario. But yes, something similar but modified is used to match doctors with residencies.

    If you're considering hanging two women out to dry you can just incorporate two low ranked men and the women who match with them get passed on. Otherwise, if you're considering polygamy, there are a few paths forward: one of which is to input the 2 highest ranked men again as the lowest ranked men.

  17. #17
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,926

    Re: Gale Shapley matching (Stable Marriage problem)

    Quote Originally Posted by KButler9 View Post
    Can you modify the code to handle cases where a 1:1 match is not possible? Specifically, when the the group sizes are different. For example, if there are only 8 men vs. 10 women?

    You might not need to, just tailor inputs. Depending on your scenario. But yes, something similar but modified is used to match doctors with residencies.

    If you're considering hanging two women out to dry you can just incorporate two low ranked men and the women who match with them get passed on. Otherwise, if you're considering polygamy, there are a few paths forward: one of which is to input the 2 highest ranked men again as the lowest ranked men.
    Administrative Note:

    Welcome to the forum.

    Sorry, but your post does not comply with Rule #6 of our Forum RULES.: please do not ignore requests by Administrators, Moderators and senior forum members regarding forum rules.

    If you are unclear about the request or instruction, then send a private message to them asking for clarification.

    All Participants:

    Please do not post a reply in a thread where a Moderator or Administrator has requested an action that has not yet been complied with (e.g. title change, code tags requested, etc.). Thanks.

  18. #18
    Registered User
    Join Date
    05-12-2019
    Location
    Windsor
    MS-Off Ver
    10
    Posts
    1

    Re: Gale Shapley matching (Stable Marriage problem)

    Varying group sizes is an extension or variation of the algorithm that is hard to do but there are some applications widely in use. Notably, x schools to y students is just one that has been implemented. I have not yet figured out how to do this in VBA.

  19. #19
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Gale Shapley matching (Stable Marriage problem)

    ...and as an aside reflecting on the WaterCooler thread on the Views:Replies ratio this one is currently 4574:17 (actually one more now that I've submitted this presumably).

    Is it the Stable Marriage thread title that's resulted in this
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Weirdest problem ever - excel not matching matching text
    By andre_as in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 05-19-2015, 03:33 AM
  2. 40 years of marriage...
    By TMS in forum The Water Cooler
    Replies: 21
    Last Post: 04-08-2014, 01:56 AM
  3. Formula to compute oceanic Shapley value for largest player.
    By alexpsyched in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-15-2013, 03:58 AM
  4. [SOLVED] Stable formula
    By Syahira in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 07-28-2006, 03:10 PM
  5. [SOLVED] Stable chart
    By How too create a stable chart? in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 01-24-2006, 05:40 AM
  6. Replies: 1
    Last Post: 10-03-2005, 08:05 AM
  7. Marriage of two formulas
    By emerald_dragonfly in forum Excel General
    Replies: 2
    Last Post: 07-04-2005, 10:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1