def runs2k11(nRuns): ''' Generate as many distinct runs possible (within nRuns). The distinctness is obtained by modifying A's hands such that it has not occurred in the first announcement. ''' uL = ut.allHands(4, range(10)) # Returns set of all hands in sorted order remSeq = [len(uL)] # Sequence of remainder hands (after each run) runSeq = [] handL = [] firstAnn = ann1 currDeal = deal i = 0 while len(uL) > 0 and i < nRuns: secondAnn = SecondAnn(currDeal, firstAnn) thirdAnn = ('c', [currDeal['c']]) ann1L = firstAnn[1] for hand in ann1L: if hand in uL: uL.remove(hand) currRun = [firstAnn, secondAnn, thirdAnn] runSeq.append(currRun) remSeq.append(len(uL)) handL.append(currDeal[a]) maxId = len(uL) - 1 idx = rand.randint(0, maxId) nextHand = uL[idx] currDeal = minDeal(nextHand) firstAnn = genPermAnn(nextHand, ann1)[0] i = i + 1 return (runSeq, handL, remSeq)
def getStrategy1(deal, agt): ''' Return a possible announcement sequence for agt at deal. Since announcements are independent of actual history, we can generate the strategy apriori. ''' hand = deal[agt] rest = [] for agt1 in deal.keys(): if agt1 != agt: rest = rest + deal[agt1] X = getCards(rest, 2) annL1 = ut.allHands(len(hand), hand + X) if rand.randint(0,1) == 0: X.pop() # drop an element of X annL2 = ut.allHands(len(hand), hand + X) return [annL1, annL2]
def maxInfFor(hand): ''' returns the announcement of maximum size that is informative to the agent 'b' for every deal that agent 'a' thinks possible initially. ''' cLst = range(10) nH = nonHand(hand) # All hands that do not contain any of a's cards. nilHand = ut.allHands(4, nH) # Convert the cards into a list of lists oneCard = ut.allHands(1, hand) # Set of all hands of size 3 not containing any of a's cards threeCH = ut.allHands(3, nH) # Set of all hands containing exactly one card from hand oneCHL = ut.crossProd(oneCard, threeCH) maxInfAnn = [hand] + oneCHL + nilHand maxInfAnn = ut.annSort(maxInfAnn) return maxInfAnn
def genX(deal, n): ''' Return the set of all n cards that an agent doesn't have. (assume that n <= len(deck \ deal[agt]) for each agent). ''' agts = ['a', 'b', 'c'] possX = {} for ag in agts: hand = deal[ag] rest = [] for ag1 in agts: if ag1 != ag : rest = rest + deal[ag1] # rest initialized. possX[ag] = ut.allHands(n, rest) return possX
def extendedAnn1(s0, deal, agt, k): ''' Compute firstAnn and extend by k random non hands of agt. ''' ann1, xCard = firstAnn(s0, deal, agt) hL = ut.allHands(len(deal[agt]), s0.cards) hLx = filterHands(hL, [xCard]) # Now to genuinely increas the ann length. nonAnn1L = [] for h in hLx: if not h in ann1[1]: nonAnn1L.append(h) idL = range(len(nonAnn1L)) resHands = ann1[1] choiceLst = [] for x in idL: choiceLst.append(x) for i in range(k): idx = rand.randint(0, len(choiceLst) - 1) dId = choiceLst.pop(idx) resHands.append(nonAnn1L[dId]) return (resHands, nonAnn1L, xCard)
def allHandsRuns2k11(): ''' Generate distinct runs for each hand of A (at a particular deal). The distinctness is obtained by modifying A's hands such that it has not occurred in the first announcement previously. ''' uL = ut.allHands(4, range(10)) runL = [] hL = [] while len(uL) > 0: hL.append(uL[0]) currHand = uL[0] currDeal = minDeal(currHand) currRun = getRun2k11(currDeal, currHand) runL.append(currRun) ann1 = currRun[0] ann1L = ann1[1] for disj in ann1L: disj.sort() if disj in uL: uL.remove(disj) return (runL, hL)
import cpState as cps import cpUtil as ut; deal = {'a' : [0, 1, 2, 3], 'b':[4, 5, 6, 7], 'c':[8,9] }; infAgts, eaves = ['a', 'b'],'c'; rus442 = cps.cpState([4, 4, 2], ['a','b', 'c'], deal, infAgts, eaves); aCards = [[0], [1], [2], [3]]; A0 = ut.allHands(4, range(4,10)); A1 = ut.allHands(3, range(4,10)); aL = ut.crossProduct(aCards, A1, ''); aL = [[0,1,2,3]] + aL + A0; annA = ('a', aL); run = [annA]; s0 = rus442.updateAnn(annA); altC = s0.getAgtDeals('c'); altC = ut.sortDeals(altC); aH = ut.allHands(4, range(10)) def nonHand(hand): ''' All cards not in hand ''' cLst = range(10) nH = [] for c in cLst: if not c in hand: nH.append(c) return nH
def getAnn1ForX(deal, agt, X): ''' ''' hand = deal[agt] annL1 = ut.allHands(len(hand), hand + X[agt]) return (agt, annL1)
resDecl = resLst[i] resName = resDecl.name() resIdx = int(resName.split('__')[1]) if currIdx < resIdx: resLst[i] = currDecl currDecl = resDecl resLst.append(currDecl) return resLst z3 = cps.z3 And = z3.And Implies = z3.Implies Not = z3.Not Or = z3.Or cL = ut.allHands(4, range(12)) cL5 = filterHands(cL, [5]) hLst = [[0, 1, 2, 3]] + cL5 boolHands = z3.BoolVector('h', len(hLst)) bH = boolHands a, b, c, e = 'a', 'b', 'c', 'e' dL, agtL, cards = [4, 4, 4, 0], [a, b, c, e], range(12) \ deal = ut.minDeal(dL, agtL, cards) st = cps.cpState s0 = st(dL, agtL, deal, [a, b, c], e) remLst = [] for h in hLst: cS = set(cards) remCards = list(cS.difference(set(h)))
def initHands(self): self.initializedHands = True hSize = self.hSize self.handList = ut.allHands(hSize, self.cards)