#OK to post homework #Yuxuan Yang, Feb 24th, Assignment 11 with(combinat): #2 GaleShTw:=proc(M,W) inv(GaleShT(W,M)[1]),GaleShT(W,M)[2]: end: #3 CheckOpt:=proc(M,W) local pi1,pi2,i: pi1:=GaleShT(M,W)[1]: pi2:=inv(GaleShT(W,M)[1]): for i from 1 to nops(pi1) do if M[i][pi1[i]]>M[i][pi2[i]] then RETURN(FAIL): fi: if W[i][inv(pi1)[i]]{} do #i is the current proposer i:=SinM[1]: #Mr. i proposes to the best remaining woman j:=Mrank[i][1]: #Mr. i1 is the current husband of Ms. j i1:=CurH[j]: co:=co+1: print(`I am Mr.`, i, `Ms. `, j, `would you marry me?`): if i1=0 then print(`Sure, you are my`, W[j][i], `-th choice, but since I am single it does not matter, so sure`): SinM:=SinM minus {i}: SinW:=SinW minus {j}: CurW[i]:=j: CurH[j]:=i: elif W[j][i1]{} do #i is the current proposer i:=SinM[1]: #Mr. i proposes to the best remaining woman j:=Mrank[i][1]: #Mr. i1 is the current husband of Ms. j i1:=CurH[j]: co:=co+1: if i1=0 then SinM:=SinM minus {i}: SinW:=SinW minus {j}: CurW[i]:=j: CurH[j]:=i: elif W[j][i1]i2 then if not IsStable1(M,W,pi,i1,i2) then num:=num+1: fi: fi: od: od: num: end: GFstab:=proc(n,K,x) local i,pi,f,rt: f:=0: for i from 1 to K do pi:=randperm(n): rt:=RT(n): f:=f+x^(CountInstablities(rt[1],rt[2],pi)): od: f: end: #c10.txt Help10:=proc():print(` RT(n), IsStable1(M,W,pi,i1,i2), IsStable(M,W,pi), FindStable(M,W,K) `):end: with(combinat): #RT(n): Generates a pair of random preferance M,W, two lists-of-lists #M[i][j]:= The ranking of Ms. j in the eyes of Mr. i #W[j][i]:= The ranking of Mr. i in the eyes of Ms. j RT:=proc(n) local i,j: [seq(randperm(n),i=1..n)],[seq(randperm(n),j=1..n)]: end: # i1 is married to j1 #i2 is married to j2 #i1 would rather be married to j2 and j2 would rather be married to i1 IsStable1:=proc(M,W,pi,i1,i2) local j1,j2: j1:=pi[i1]: j2:=pi[i2]: not(M[i1][j2]i2 then if not IsStable1(M,W,pi,i1,i2) then RETURN(false, [i1,i2]): fi: fi: od: od: true,true: end: #FindStable(M,W,K): Given the Men's and Women's preferance tables, starts with a random matching #and keeps "fixing it" until hopefully you get a stable matching, we limit the number of #iterations to K, if none found it will return FAIL. Otherwise it will return a stable #mathing, followed by the number if iterations it took FindStable:=proc(M,W,K) local n,pi,co,a,i1,i2,j1,j2: print(`Warning: This is a stupid program that usually FAILS, for larger n`): n:=nops(M): pi:=randperm(n): for co from 1 to K do a:=IsStable(M,W,pi): if a[1] then RETURN(pi,co): else i1:=a[2][1]: i2:=a[2][2]: j1:=pi[i1]: j2:=pi[i2]: if i1