Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gale-Shapley Implemented in Go with JSON test/example input #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions Algorithms/1.7_Gale-Shapley/galeShapley.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package galeshapley

import (
"strings"
)

type Person struct {
Name string `json:"name"`
Male bool `json:"male"`
Preferences []string `json:"preferences"`

Match *Person
}

func Alg(males, females []Person) {
// by having a nil EngagedTo, a person is free... inital state all
// people are free

// while (∃ free man m who still has a woman w to propose to)
for SinglesExist(males) {
for mi := range males {
if !males[mi].IsSingle() {
continue
}

fi := males[mi].GetPreferedMatch(females)
if females[fi].IsSingle() {
match(&males[mi], &females[fi])
continue
}

if females[fi].CompareToExistingMatch(&males[mi]) {
females[fi].BreakUp()
match(&males[mi], &females[fi])
continue
}
}
}
}

func (person Person) String() string {
str := strings.Title(person.Name)
if person.Match == nil {
str += " (single)"
return str
}
str += " is engaged to "
str += strings.Title(person.Match.Name)
return str
}

func (person Person) IsSingle() bool {
return person.Match == nil
}

func (person *Person) BreakUp() {
person.Match.Match = nil
person.Match = nil
}

// Perfers returns
// n > 1 if person1 a is prefered
// n = 0 if there is no preference
// n < 1 if person2 a is prefered
func (person Person) CompareToExistingMatch(otherPerson *Person) bool {
otherPersonIndex := len(person.Preferences)
engagedToIndex := len(person.Preferences)

for i, prefName := range person.Preferences {
if prefName == otherPerson.Name {
otherPersonIndex = i
}
if prefName == person.Match.Name {
engagedToIndex = i
}
}

return engagedToIndex > otherPersonIndex
}

func (person *Person) GetPreferedMatch(people []Person) int {
preferenceIndex := 0

for preferenceIndex = range people {
if people[preferenceIndex].Name == person.Preferences[0] {
break
}
}

if len(person.Preferences) > 1 {
person.Preferences = person.Preferences[1:] // shortens preference list
}
return preferenceIndex
}

func SinglesExist(people []Person) bool {
for i := range people {
if people[i].Match == nil {
return true
}
}
return false
}

func match(person1, person2 *Person) {
person1.Match, person2.Match = person2, person1
}
72 changes: 72 additions & 0 deletions Algorithms/1.7_Gale-Shapley/galeShapley_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package galeshapley

import (
"testing"
)

func TestAlg00(t *testing.T) {
males, females, err := LoadPeopleFromFile("testdata/people00.json")
if err != nil {
t.Fatal(err)
}

Alg(males, females)

if SinglesExist(males) || SinglesExist(females) {
t.Fail()
}
// for _, person := range males {
// t.Log(person)
// }
}

func TestAlg01(t *testing.T) {
males, females, err := LoadPeopleFromFile("testdata/people01.json")
if err != nil {
t.Fatal(err)
}

Alg(males, females)

if SinglesExist(males) || SinglesExist(females) {
t.Fail()
}
// for _, person := range males {
// t.Log(person)
// }
}

func TestAlg02(t *testing.T) {
males, females, err := LoadPeopleFromFile("testdata/people02.json")
if err != nil {
t.Fatal(err)
}

for _, person := range append(males, females...) {
t.Log(person)
}

Alg(males, females)

if SinglesExist(males) || SinglesExist(females) {
t.Fail()
}
for _, person := range males {
t.Log(person)
}
}

func TestLoadPeopleFromFile(t *testing.T) {
if _, _, err := LoadPeopleFromFile("testdata/badData.json"); err == nil {
t.Error("testdata/badData.json should err")
}
if _, _, err := LoadPeopleFromFile("testdata/badData00.json"); err == nil {
t.Error("testdata/badData00.json should err")
}
if _, _, err := LoadPeopleFromFile("testdata/badData01.json"); err == nil {
t.Error("testdata/badData01.json should err")
}
if _, _, err := LoadPeopleFromFile("testdata/badData02.json"); err == nil {
t.Error("testdata/badData02.json should err")
}
}
52 changes: 52 additions & 0 deletions Algorithms/1.7_Gale-Shapley/people.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package galeshapley

import (
"encoding/json"
"fmt"
"os"
)

func LoadPeopleFromFile(filename string) ([]Person, []Person, error) {
file := struct {
People []Person `json:"people"`
}{}

var (
males, females []Person
)

f, err := os.Open(filename)
if err != nil {
return males, females, err
}

err = json.NewDecoder(f).Decode(&file)
if err != nil {
return males, females, err
}

for personIndex, person := range file.People {
nextPreference:
for preferenceIndex, preference := range person.Preferences {
if person.Name == preference {
return males, females, fmt.Errorf("person %s cannot prefer themeselves (preferences: %d)", person.Name, preferenceIndex)
}

for i, p := range file.People {
if i != personIndex && p.Name == preference {
continue nextPreference
}
}
return males, females, fmt.Errorf("%s has an unknown person named %s in their preference list", person.Name, preference)
}
}

for _, person := range file.People {
if person.Male {
males = append(males, person)
} else {
females = append(females, person)
}
}
return males, females, nil
}
3 changes: 3 additions & 0 deletions Algorithms/1.7_Gale-Shapley/testdata/badData00.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"foo": bad json
}
22 changes: 22 additions & 0 deletions Algorithms/1.7_Gale-Shapley/testdata/badData01.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"people": [
{
"name": "Am",
"male": true,
"preferences": ["Am", "Bf"]
}, {
"name": "Bm",
"male": true,
"preferences": ["Bf", "Af"]
},
{
"name": "Af",
"male": false,
"preferences": ["Am", "Bm"]
}, {
"name": "Bf",
"male": false,
"preferences": ["Bm", "Am"]
}
]
}
22 changes: 22 additions & 0 deletions Algorithms/1.7_Gale-Shapley/testdata/badData02.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"people": [
{
"name": "Am",
"male": true,
"preferences": ["XX", "Bf"]
}, {
"name": "Bm",
"male": true,
"preferences": ["Bf", "Af"]
},
{
"name": "Af",
"male": false,
"preferences": ["Am", "Bm"]
}, {
"name": "Bf",
"male": false,
"preferences": ["Bm", "Am"]
}
]
}
22 changes: 22 additions & 0 deletions Algorithms/1.7_Gale-Shapley/testdata/people00.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"people": [
{
"name": "Am",
"male": true,
"preferences": ["Af", "Bf"]
}, {
"name": "Bm",
"male": true,
"preferences": ["Bf", "Af"]
},
{
"name": "Af",
"male": false,
"preferences": ["Am", "Bm"]
}, {
"name": "Bf",
"male": false,
"preferences": ["Bm", "Am"]
}
]
}
30 changes: 30 additions & 0 deletions Algorithms/1.7_Gale-Shapley/testdata/people01.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"people": [
{
"name": "Am",
"male": true,
"preferences": ["Af", "Bf", "Cf"]
}, {
"name": "Bm",
"male": true,
"preferences": ["Bf", "Cf", "Af"]
}, {
"name": "Cm",
"male": true,
"preferences": ["Bf", "Af", "Cf"]
},
{
"name": "Af",
"male": false,
"preferences": [ "Cm", "Am", "Bm"]
}, {
"name": "Bf",
"male": false,
"preferences": ["Bm", "Cm", "Am"]
}, {
"name": "Cf",
"male": false,
"preferences": ["Bm", "Cm","Am"]
}
]
}
45 changes: 45 additions & 0 deletions Algorithms/1.7_Gale-Shapley/testdata/people02.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"people": [
{
"name": "John",
"male": true,
"preferences": ["Stephanie", "Roxanne", "Jenny", "Maria", "Sarah"]
}, {
"name": "Michael",
"male": true,
"preferences": ["Stephanie", "Jenny", "Roxanne", "Maria", "Sarah"]
}, {
"name": "Matthew",
"male": true,
"preferences": ["Maria", "Stephanie", "Jenny", "Sarah", "Roxanne"]
}, {
"name": "Ryan",
"male": true,
"preferences": ["Maria", "Jenny", "Stephanie", "Sarah", "Roxanne"]
}, {
"name": "Andrew",
"male": true,
"preferences": ["Roxanne", "Maria", "Jenny", "Stephanie", "Sarah"]
}, {
"name": "Stephanie",
"male": false,
"preferences": ["Michael", "John", "Matthew", "Ryan", "Andrew"]
}, {
"name": "Roxanne",
"male": false,
"preferences": ["Michael", "John", "Matthew", "Ryan", "Andrew"]
}, {
"name": "Jenny",
"male": false,
"preferences": ["Michael", "Matthew", "John", "Ryan", "Andrew"]
}, {
"name": "Maria",
"male": false,
"preferences": ["Michael", "Ryan", "Matthew", "John", "Andrew"]
}, {
"name": "Sarah",
"male": false,
"preferences": ["Matthew", "Michael", "Ryan", "John", "Andrew"]
}
]
}