def fc_pattern(d, s): if not intersect(factor(s), factor(s + d)): type = 'third' k = 1 _, q_type = f(s * k + d, s) while type == 'third' or q_type != 'FC': type = fc_type(s * k + d, s) _, q_type = f(s * k + d, s) k += 1 # print(type) denom = str(2 * s) + 'k' numer = str(s) + 'k' if type == 'small': numer += '+' + str(d) if 2 * d > s: denom += '+' + str(2 * s) else: denom += '+' + str(s) elif type == 'large': if 2 * d >= s: denom += '+' + str(s) numer += ('-' if s - d < 0 else '+') + str(s - d) else: numer += '-' + str(d) return numer, denom else: return None, None
def biggrid(maxM, maxS): lines = [] for m in range(1, maxM + 1): values = [] for s in range(1, maxS + 1): if m > s: # (m,s,d,pieces,hasElem) = getRow(m,s) # pieces, d = getPiecesDenom(m,s) # bound = pieces[0]/d # if hasElem: inter = 1 try: inter, _ = fms.f(m, s) except: pass fc = floorciel(m, s) bound = min(inter, fc) if bound == S(1) / 3: values.append(str(bound)) else: values.append(str(0)) else: values.append(str(0)) lines.append(";".join(values)) text = "\n".join(lines) text_file = open("girdy3.csv", "w") text_file.write(text) text_file.close()
def find_exceptions(s, d, stat_map): pattern_found = False start_k = 1 exceptions = {} range_end = int((100 - d) / s) + 1 for k in range(1, range_end): m = s * k + d q, q_type = f(m, s) if q_type == 'FC' and q != Fraction(1, 3): if not pattern_found: pattern_found = True start_k = k elif q_type == 'FC' and q == Fraction(1, 3): exceptions[Fraction(m, s)] = (q, r'FC-exception') stat_map['total'] += 1 stat_map['1/3'] += 1 if math.ceil(2 * m / s) == 3: stat_map['V3'] += 1 elif q_type == 'BM': exceptions[Fraction(m, s)] = (q, 'BM-exception') stat_map['BM'] += 1 else: exceptions[Fraction(m, s)] = (q, 'INT-exception') stat_map['int'] += 1 stat_map['total'] += 1 if q == Fraction(1, 3): stat_map['1/3'] += 1 if math.ceil(2 * m / s) == 3: stat_map['V3'] += 1 if not pattern_found: start_k = k return exceptions, start_k
def doit(m, s): V = interval.findV(m, s) Q, _ = f(m, s) (sV, sVm1) = interval.getShares(m, s, V) intervals = getIntervals(m, s, Q, V) # print (sV, sVm1, V) return intervals
def open_cases(): opens = [(67, 21), (69, 32)] for i, case in enumerate(opens): m = case[0] s = case[1] ub, ans_types = f(m, s) open_prob = '' lb = '' lb_cd = '' ub_cd = '' used_gaps = False if max(Fraction(1, 3), scott.f(m, s, True)) == ub: open_prob = '' else: used_gaps = True lb = Fraction(ub.numerator - 2, ub.denominator) lb = lb if lb > Fraction(1, 3) else Fraction(1, 3) lb, lb_type = closer_bounds(m, s, lb, ub, max_denom=2000) if lb_type == 'Open': open_prob = 'Open' lb_cd, ub_cd = convert_den(lb, ub) else: ans_types = [lb_type] ub = lb lb = '' ans_types_str = functools.reduce(lambda a, b: a + ',' + str(b), ans_types) row = (m, s, ans_types_str, open_prob, str(lb), str(ub), lb_cd, ub_cd) print(row)
def mapunion(s, f): # print(s) ends = list(s.boundary) ends.reverse intervals = [] middle = False if len(ends) % 2 != 0: # this means we have piece left over in middle middle = True if not s.contains(S(1) / 2): print("this should never happen.. something is wrong") ends.remove(S(1) / 2) # this is only way this should ever happen... for i in range(0, len(ends), 2): x, y = ends[i], ends[i + 1] x, y = f(x, y) intervals.append(Interval(x, y)) if middle: intervals.append(Interval(S(1) / 2, S(1) / 2)) return Union(*intervals)
print('\n') if 'BM' in ans_type: V, _, _ = calcSv(m, s) d = m - s k = int(s / (3 * d)) if s % (3 * d) != 0 else int(s / (3 * d)) - 1 a = s - 3 * d * k print('We know that a = %d, and d = %d.' % (a, d)) if __name__ == '__main__': if len(sys.argv) == 3: m = int(sys.argv[1]) s = int(sys.argv[2]) findProc = bool(sys.argv[3]) if len(sys.argv > 3) else False ub, ub_types = f(m, s) if checkProc(findProc, m, s, ub): getProcedures(m, s, ub, True) if findProc else scott.f(m, s) print( '\nFor m = %d and s = %d, f(m,s) has lower bound %s, as there exists a ' 'procedure for cutting m muffins for s students such that the smallest ' 'piece is at least %s.' % (m, s, ub, ub)) print('That procedure is shown above. ^\n\n') print( 'For m = %d and for s = %d, f(m,s) has an upper bound of %s.' % (m, s, ub)) gen_proof(m, s, ub, ub_types)
elif h < Fraction(1, 2): sv_str = '\hbox{%d %d-shs}' % (V * sv, V) sv1_low_str = '\hbox{%d %d-shs}' % ((V - 1) * sv1 - V * sv, V - 1) sv1_high_str = '\hbox{%d %d-shs}' % (V * sv, V - 1) if g == h: frac_strs, univ_lcm = make_common_frac([ans, g, 1 - g, 1 - ans]) else: frac_strs, univ_lcm = make_common_frac([ans, g, h, 1 - h, 1 - g, 1 - ans]) result.append(data_to_latex([sv_str, sv1_low_str, sv1_high_str], frac_strs, univ_lcm)) split_str = '\hbox{%d %d-shs}' % (((V - 1) * sv1 - V * sv) / 2, V - 1) if g == h: frac_strs, univ_lcm = make_common_frac([h, Fraction(1, 2), 1 - g, 1 - ans]) else: frac_strs, univ_lcm = make_common_frac([h, Fraction(1, 2), 1 - h, 1 - g, 1 - ans]) result.append(split_to_latex([split_str, split_str, sv1_high_str], frac_strs, univ_lcm, V - 1, False)) return result if __name__ == '__main__': m = int(sys.argv[1]) s = int(sys.argv[2]) try: ans = fms.f(m, s)[0] if len(sys.argv) == 3 \ else Fraction(int(sys.argv[3].split('/')[0]), int(sys.argv[3].split('/')[1])) [print(diag) for diag in make_diagram(m, s, ans)] except ValueError: print('Incorrect arguments.')
with open(filename, 'w') as csv_file: csv_writer = csv.writer(csv_file) csv_writer.writerow(['m', 's', 'ans', 'method']) for s in range(1, 100): print('----------------> %d' % s) max_range = 4 * s # s * s # if s % 2 == 1: # max_range = int(.65 * (s * s)) # if s % 2 == 0: # max_range = int(.35 * (s * s)) for m in range(s + 1, max_range): if bigrun.relatively_prime(m, s): if m % 100 == 0: print(m, s, file=sys.stdout) ans = scott.f(m, s, True) ub, ub_types = f(m, s) if max(Fraction(1, 3), ans) == ub: ans_types = functools.reduce(lambda a, b: a + ',' + str(b), ub_types) else: try: print(m, s, ans) lb_type = bigrun.verify_gaps(m, s, ans.numerator, ans.denominator) if lb_type == 'Open': ans_types = 'None' csv_writer.writerow([m, s, ans, ans_types]) else: ans_types = lb_type except TimeoutError: ans_types = 'GAPS-TIMEOUT'
pieces, d = intervalToPieces(m, s, interval) procedures = integers.solve(m, s, d=d, pieces=pieces, intervals=Interval(0, 1)) text.append("m, s = " + str((m, s)) + "\ndenominator=" + str(d) + '\n' + procedureToString(next(procedures)) + '\n') return '\n'.join(text) if __name__ == '__main__': if len(sys.argv) >= 3: m = int(sys.argv[1]) s = int(sys.argv[2]) ans, _ = fms.f(m, s) if len(sys.argv) == 4: ans = Fraction(int(sys.argv[3].split('/')[0]), int(sys.argv[3].split('/')[1])) try: if getProcedures(m, s, ans, True): print( '\nFor m = %d and s = %d, there exists a procedure for cutting m muffins for s ' 'students such that the smallest piece is at least %s.' % (m, s, ans)) print('That procedure is shown above. ^') else: print( '\nFor m = %d and s = %d, there does not exists a procedure for cutting m muffins for s '
def write_file(m_l=3, m_u=70, s_l=3, s_u=60, findProc=False): doc = Document('BIGRUN_ALL') doc_one = Document('BIGRUN_ONE') doc_open = Document('Open') doc_non_FC = Document('Non_FC') doc_V3 = Document('V3') doc_gaps = Document('GAPS') table = LongTable('|c|c|c|c|c|c|c|c|') table.add_hline() table.add_row((bold('M'), bold('S'), bold('Method'), bold('Open?'), bold('LB'), bold('UB'), bold('LB-CD'), bold('UB-CD'))) table.add_hline() table.add_hline() table_one = LongTable('|c|c|c|c|c|c|c|c|') table_one.add_hline() table_one.add_row((bold('M'), bold('S'), bold('Method'), bold('Open?'), bold('LB'), bold('UB'), bold('LB-CD'), bold('UB-CD'))) table_one.add_hline() table_one.add_hline() table_open = LongTable('|c|c|c|c|c|c|c|c|') table_open.add_hline() table_open.add_row((bold('M'), bold('S'), bold('Method'), bold('Open?'), bold('LB'), bold('UB'), bold('LB-CD'), bold('UB-CD'))) table_open.add_hline() table_open.add_hline() table_non_FC = LongTable('|c|c|c|c|c|c|c|c|') table_non_FC.add_hline() table_non_FC.add_row((bold('M'), bold('S'), bold('Method'), bold('Open?'), bold('LB'), bold('UB'), bold('LB-CD'), bold('UB-CD'))) table_non_FC.add_hline() table_non_FC.add_hline() table_V3 = LongTable('|c|c|c|c|c|c|c|c|') table_V3.add_hline() table_V3.add_row((bold('M'), bold('S'), bold('Method'), bold('Open?'), bold('LB'), bold('UB'), bold('LB-CD'), bold('UB-CD'))) table_V3.add_hline() table_V3.add_hline() table_gaps = LongTable('|c|c|c|c|c|c|c|c|') table_gaps.add_hline() table_gaps.add_row((bold('M'), bold('S'), bold('Method'), bold('Open?'), bold('LB'), bold('UB'), bold('LB-CD'), bold('UB-CD'))) table_gaps.add_hline() table_gaps.add_hline() bigrun_csv = open('data/bigrun_all.csv', 'w') csv_writer = csv.writer(bigrun_csv) csv_writer.writerow(['M', 'S', 'FC', 'HALF', 'INT', 'MID', 'EBM', 'HBM', 'VHBM']) for s in range(s_l, s_u + 1): m_start = s + 1 if s + 1 > m_l else m_l for m in range(m_start, m_u + 1): if relatively_prime(m, s): ub, ans_types, all_results = f(m, s, bigrun=True) csv_writer.writerow([m, s] + all_results) open_prob = '' lb = '' lb_cd = '' ub_cd = '' used_gaps = False try: if checkProc(findProc, m, s, ub): open_prob = '' else: used_gaps = True lb = Fraction(ub.numerator - 2, ub.denominator) lb = lb if lb > Fraction(1, 3) else Fraction(1, 3) lb, lb_type = closer_bounds(m, s, lb, ub, max_denom=750) if lb_type == 'Open': open_prob = 'Open' lb_cd, ub_cd = convert_den(lb, ub) else: ans_types = [lb_type] ub = lb lb = '' except procedures.TimeoutError: open_prob = 'Timeout' except KeyError: open_prob = 'Timeout' ans_types_str = functools.reduce(lambda a, b: a + ',' + str(b), ans_types) row = (m, s, ans_types_str, open_prob, str(lb), str(ub), lb_cd, ub_cd) table.add_row(row) row_one = (m, s, ans_types[0], open_prob, str(lb), str(ub), lb_cd, ub_cd) table_one.add_row(row_one) print(row_one) table.add_hline() table_one.add_hline() if len(open_prob) > 0: table_open.add_row(row) table_open.add_hline() if len(open_prob) > 0 or 'FC' not in ans_types_str: table_non_FC.add_row(row) table_non_FC.add_hline() V, _, _ = calcSv(m, s) if V == 3: if not used_gaps: _, ans_types_all = f(m, s, bigrun=False) ans_types_all_str = functools.reduce(lambda a, b: a + ',' + str(b), ans_types) row = (m, s, ans_types_all_str, open_prob, str(lb), str(ub), lb_cd, ub_cd) table_V3.add_row(row) table_V3.add_hline() if len(open_prob) == 0 and used_gaps: table_gaps.add_row(row) table_gaps.add_hline() doc.append(table) doc.generate_pdf('bigrun/BIGRUN_all', clean_tex=False) doc_one.append(table_one) doc_one.generate_pdf('bigrun/BIGRUN_one', clean_tex=False) doc_open.append(table_open) doc_open.generate_pdf('bigrun/BIGRUN_opens', clean_tex=False) doc_non_FC.append(table_non_FC) doc_non_FC.generate_pdf('bigrun/BIGRUN_non_FC', clean_tex=False) doc_V3.append(table_V3) doc_V3.generate_pdf('bigrun/BIGRUN_V3', clean_tex=False) doc_gaps.append(table_gaps) doc_gaps.generate_pdf('bigrun/BIGRUN_GAPS', clean_tex=False)