<commit_before>#!/usr/bin/env python3

# https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.optimize.linprog.html
from scipy.optimize import linprog
from timfuz import Benchmark, load_sub, A_ub_np2d, acorner2csv, corner_s2i
import numpy as np
import glob
import json
import math
import sys
import os
import time
import timfuz_solve

def save(outfn, res, names, corner):
    # ballpark minimum actual observed delay is around 7 (carry chain)
    # anything less than one is probably a solver artifact
    delta = 0.5
    corneri = corner_s2i[corner]

    print('Writing resutls')
    zeros = 0
    with open(outfn, 'w') as fout:
        # write as one variable per line
        # this natively forms a bound if fed into linprog solver
        fout.write('ico,fast_max fast_min slow_max slow_min,rows...\n')
        for xval, name in zip(res.x, names):
            row_ico = 1

            # FIXME: only report for the given corner?
            # also review ceil vs floor choice for min vs max
            # lets be more conservative for now
            if xval < delta:
                print('WARNING: near 0 delay on %s: %0.6f' % (name, xval))
                zeros += 1
                #continue
            items = [str(row_ico), acorner2csv(math.ceil(xval), corneri)]
            items.append('%u %s' % (1, name))
            fout.write(','.join(items) + '\n')
    print('Wrote: zeros %u => %u / %u constrained delays' % (zeros, len(names) - zeros, len(names)))

def run_corner(Anp, b, names, corner, verbose=False, opts={}, meta={}, outfn=None):
    # Given timing scores for above delays (-ps)
    assert type(Anp[0]) is np.ndarray, type(Anp[0])
    assert type(b) is np.ndarray, type(b)

    #check_feasible(Anp, b)

    '''
    Be mindful of signs
    Have something like
    timing1/timing 2 are constants
    delay1 +   delay2 +               delay4     >= timing1
               delay2 +   delay3                 >= timing2

    But need it in compliant form:
    -delay1 +   -delay2 +               -delay4     <= -timing1
                -delay2 +   -delay3                 <= -timing2
    '''
    rows = len(Anp)
    cols = len(Anp[0])
    print('Scaling to solution form...')
    b_ub = -1.0 * b
    #A_ub = -1.0 * Anp
    A_ub = [-1.0 * x for x in Anp]

    print('Creating misc constants...')
    # Minimization function scalars
    # Treat all logic elements as equally important
    c = [1 for _i in range(len(names))]
    # Delays cannot be negative
    # (this is also the default constraint)
    #bounds =  [(0, None) for _i in range(len(names))]
    # Also you can provide one to apply to all
    bounds = (0, None)

    # Seems to take about rows + 3 iterations
    # Give some margin<commit_msg>timfuz: corner FIXME assert<commit_after>#!/usr/bin/env python3

# https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.optimize.linprog.html
from scipy.optimize import linprog
from timfuz import Benchmark, load_sub, A_ub_np2d, acorner2csv, corner_s2i
import numpy as np
import glob
import json
import math
import sys
import os
import time
import timfuz_solve

def save(outfn, res, names, corner):
    # ballpark minimum actual observed delay is around 7 (carry chain)
    # anything less than one is probably a solver artifact
    delta = 0.5
    corneri = corner_s2i[corner]

    print('Writing resutls')
    zeros = 0
    with open(outfn, 'w') as fout:
        # write as one variable per line
        # this natively forms a bound if fed into linprog solver
        fout.write('ico,fast_max fast_min slow_max slow_min,rows...\n')
        for xval, name in zip(res.x, names):
            row_ico = 1

            # FIXME: only report for the given corner?
            # also review ceil vs floor choice for min vs max
            # lets be more conservative for now
            if xval < delta:
                print('WARNING: near 0 delay on %s: %0.6f' % (name, xval))
                zeros += 1
                #continue
            items = [str(row_ico), acorner2csv(math.ceil(xval), corneri)]
            items.append('%u %s' % (1, name))
            fout.write(','.join(items) + '\n')
    print('Wrote: zeros %u => %u / %u constrained delays' % (zeros, len(names) - zeros, len(names)))

def run_corner(Anp, b, names, corner, verbose=False, opts={}, meta={}, outfn=None):
    # Given timing scores for above delays (-ps)
    assert type(Anp[0]) is np.ndarray, type(Anp[0])
    assert type(b) is np.ndarray, type(b)

    #check_feasible(Anp, b)

    '''
    Be mindful of signs
    Have something like
    timing1/timing 2 are constants
    delay1 +   delay2 +               delay4     >= timing1
               delay2 +   delay3                 >= timing2

    But need it in compliant form:
    -delay1 +   -delay2 +               -delay4     <= -timing1
                -delay2 +   -delay3                 <= -timing2
    '''
    assert 'max' in corner, 'FIXME: support min corner math'

    rows = len(Anp)
    cols = len(Anp[0])
    print('Scaling to solution form...')
    b_ub = -1.0 * b
    #A_ub = -1.0 * Anp
    A_ub = [-1.0 * x for x in Anp]

    print('Creating misc constants...')
    # Minimization function scalars
    # Treat all logic elements as equally important
    c = [1 for _i in range(len(names))]
    # Delays cannot be negative
    # (this is also the default constraint)
    #bounds =  [(0, None) for _i in range(len(names))]
    # Also you can provide one to apply to all
    bounds = (0, None)

    # Seems to take about rows + 3 iterations
    # Give some margin
{'fraction_non_alphanumeric': 0.10800576992426975, 'fraction_numerical': 0.017670393076090876, 'mean_word_length': 2.240070093457944, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '505580', 'n_tokens_mistral': 1856, 'n_tokens_neox': 1761, 'n_words': 603}