Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is _wrong_ with Python? a.k.a. PoR probabilities
#10
I need to try to distract myself.

With the Pool class, I shifted TN from PoolRoll to Pool. I'll be editing that in a bit, if I remember.
Code:
Class Pool:
  def __init__(self, oldpool, newdie)
    self.TN = oldpool.TN
    self.rolls = set()
    for oldpoolroll in oldpool.rolls:
      for dieroll in (0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) if dieroll <= newdie:
        newpoolroll = PoolRoll(oldpoolroll, dieroll, self.TN)
        self.rolls.add( newpoolroll )

  def successes(self):
    totals = [0, 0, 0, 0, 0, 0]
    for roll in self.rolls:
      totals[roll.successes] += 1
    return( totals )

class SeedRoll:
  def __init__( self, roll )
    self.roll = [roll]
    self.rolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    self.rolls[roll] += 1
    self.successes = 0

class SeedPool:
  def __init__(self, die, TN):
    self.TN = TN
    self.rolls = set()
    for i in (0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) if i <= die:
      seed = SeedRoll( i )
      self.rolls.add(seed)
      del seed   #not sure if this is necessary, also not sure that I can't do self.rolls.add( SeedRoll( i ) )

And this is a recursive function that will, hopefully, cycle through all combinations (not permutations) of pools in size of 2 to 11 dice where each die is less than or equal to first die in the pool, which is itself limited by the target number. If my understanding is correct, the dictionary pools is passed by pointer, so changes made to it follows through the function but oldpool, and pool are limited by scope. MAX is unchanged throughout so it doesn't matter. I suppose I could just use it as a global, but, you know, can't do it.
Code:
def iterate( pools, oldpool, lastpool, MAX ):
  if len(lastpool) == MAX:
    pools[str(lastpool)] = oldpool
    return(pools)
  else:
    pools[str(lastpool)] = oldpool
    for newdie in (4, 6, 8, 10, 12) if newdie <= lastpool[-1]:
      currentpool = lastpool[:]
      currentpool.append(newdie)
      pool = Pool(oldpool, newdie)
      pools = iterate( pools, pool, currentpool, MAX )
What I think should happen is it'll create dice pools in the following pattern (sent [8] where MAX = 4):
Code:
[8,4], [8,4,4], [8,4,4,4]
[8,6], [8,6,4], [8,6,4,4]
       [8,6,6], [8,6,6,4]
                [8,6,6,6]
[8,8], [8,8,4], [8,8,4,4]
       [8,8,6], [8,8,6,4]
                [8,8,6,6]
       [8,8,8], [8,8,8,4]
                [8,8,8,6]
                [8,8,8,8]

I'm hoping that this is a properly functioning main{}
Code:
MAX = 11    #Maximmum size of a pool
for TN in xrange(5,14):    #target numbers
  limit = ((TN-1)/2)*2      #should return the largest even number <= TN
  for die in (4, 6, 8, 10, 12) if die <= limit:
    currentpool = [die]
    seed = SeedPool(die, pool)
    pool = Pool(seed, die)
    pools = {}
    pools = iterate(pools, pool, currentpool, MAX)
    file = open("iterative additive", "a")
    for key in pools
      file.write("%s %d %s" % (key, pools[key].TN, pools.[key].successes()))
    file.close()
    del pools, seed, pool

Holy crap that's going to suck up RAM. Especially when we get to d12s. But where can we do a dump? When we get to the end condition in iterate()? That would require putting pools into scope. Or maybe not. The last pool can definitely be deleted, the next to last can be deleted if its last die is the same size, and so on. Everything will eventually be deletable, but we want to be sure to get the output first and we don't want to output anything until it's ready to be deleted. This makes things fun because we're tracking the pools in a dictionary, which cannot use lists as keys, which is why I'm converting the lists into strings, and I'm leaving the brackets in for output purposes. So we can slice the string, looking for number characters, but probably creates problems with single and double digit numbers. Converting the string back into a list is doable but still messy. Another method is to store the pool list in pool, something I had removed as duplicate information to keep memory usage down. I'm thinking it's worth putting back in.


Code:
def iterate( pools, oldpool, lastpool, MAX ):
  if len(lastpool) = MAX:
    pools[str(lastpool)] = oldpool
    file = open("iterative additive", "a")
    for key in pools if pools[key].pool[-1] == lastpool[-1]:
      file.write("%s %d %s" % (key, pools[key].TN, pools.[key].successes()))
      del pools[key]
    file.close()
    return(pools)
  else:
    etc.
That should do it. ( [-1] looks at the last entry, -2 the second last, etc.) If my reasoning is correct (again).
Getting me free admission into gaming conventions for a decade
Reply


Messages In This Thread
RE: What is _wrong_ with Python? - by Kersus - 10-31-2016, 01:00 PM
RE: What is _wrong_ with Python? - by Kersus - 11-03-2016, 03:56 PM
RE: What is _wrong_ with Python? - by Oedipussy Rex - 11-10-2016, 03:33 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)