Mercurial > hg > config
changeset 776:67fa26b40dc6
add example program for time-spacing
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 03 Jul 2016 18:33:09 -0700 |
parents | 13dd0ce4fa9f |
children | ef6731a4ad86 |
files | python/example/possibilities.py |
diffstat | 1 files changed, 104 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/python/example/possibilities.py Sun Jul 03 18:33:09 2016 -0700 @@ -0,0 +1,104 @@ +#!/usr/bin/env python + +# imports +import argparse +import sys +import time +from pprint import pprint + +costs = {1:1, + 7:3, + 30:25} + +A = [1, 2, 4, 5, 7, 29, 30] + +#A = [3, 7, 10] + +#A=[1,2,3] + +def possibilities(A): + possibilities = [[(1, date) for date in A]] + for_consideration = [0] + while True: + if not for_consideration: + break + _for_consideration = [] + for item in for_consideration: + basis = possibilities[item][:] + tickets = [i[0] for i in basis] + if set(tickets) == set([7]): + continue + index = 0 + potential = [] + while True: + try: + pos = tickets.find(1, index) + except ValueError: + break + potential = basis[:] + potential[pos] = (7, basis[pos][-1]) + + try: + while A[index] < date+6: + index += 1 + except IndexError: + pass + + return possibilities + +def fill_space(A, spans): + """ + A -- the space to be filled + spans -- the discrete length of each space + """ + if not A: + return [] + + A = sorted(A) + + possibilities = [] + + # [(ticket_value, index_into_A)] + working_set = [[(value, 0)] for value in spans] + # print "working set" + # pprint(working_set) + while working_set: + item = working_set.pop() + last_span, index = item[-1] + last_date = A[index] + e = None + index += 1 + try: + while last_date + last_span > A[index]: + print ('index: {}'.format(index)) + index += 1 + except IndexError as e: + possibilities.append(item) + continue + + # print "item:" + # pprint(item) + extension = [item[:] + [(span, index)] + for span in spans] + + working_set.extend(extension) + + # fill possibility indices with dates + retval = [[(A[index], span) for span, index in possibility] + for possibility in possibilities] + return retval + + +def cost(A, spans=(30,)): + + # determine possibility spaces + spaces = fill_space(A, spans=spans) + + # determine costs + +if __name__ == '__main__': + + parser = argparse + spans = costs.keys() + + pprint (fill_space(A, spans))