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

import krylov
import splitting

import lcp
import numpy as np
import math
import metric
import accel

def parse_args():
    import argparse

    parser = argparse.ArgumentParser(description='benchmark LCP solvers.')
    parser.add_argument('file', help='filename for LCP data')
    parser.add_argument('--iter', type=int, default=100,
                        help='iteration count')

    parser.add_argument('--eps', type=float, default=1e-8,
                   help='precision')

    return parser.parse_args()



# some adapters
def pgs(x, (M, q)):
    """PGS"""
    return splitting.pgs(x, M, -q)

def pjacobi(x, (M, q)):
    """PJacobi"""
    return splitting.pjacobi(x, M, -q)


args = parse_args()

(M, q) = lcp.load( args.file )
iterations = args.iter
precision = args.eps

# solver list
solvers = [pgs, pjacobi,
           accel.nlnscg(pgs),
           accel.nlnscg(pgs, metric = np.diag(M) ),
           accel.nlnscg(pjacobi),
           accel.nlnscg(pjacobi, metric = np.diag(M) ),
]

# error metric 
error = metric.lcp_merit( (M, q) )


from matplotlib import pyplot as plt

print 'file:', args.file

for s in solvers:

    name = s.__doc__

    run = lcp.bench( (M, q), s,
                     iterations = iterations,
                     precision = precision,
                     metric = error )
    
    print 'running {}...'.format(name), 
    data = [e for k, e in run]
    print 'ok'
    
    plt.plot( data, label = name )


plt.yscale('log')
plt.legend()
plt.title( error.__doc__.title() )
plt.show()
    
                          <commit_msg>multiplots yay<commit_after>#!/usr/bin/env python

import krylov
import splitting

import lcp
import numpy as np
import math
import metric
import accel

def parse_args():
    import argparse

    parser = argparse.ArgumentParser(description='benchmark LCP solvers.')
    parser.add_argument('file', nargs='+', help='filename for LCP data')
    parser.add_argument('--iter', type=int, default=100,
                        help='iteration count')
    parser.add_argument('--fontsize', type=int, default=8,
                        help='font size in plot')
    
    parser.add_argument('--nolegend', action='store_true',
                        help='disable legend in plots')
    
    parser.add_argument('--eps', type=float, default=1e-8,
                   help='precision')

    return parser.parse_args()



# some adapters
def pgs(x, (M, q)):
    """PGS"""
    return splitting.pgs(x, M, -q)

def pjacobi(x, (M, q)):
    """PJacobi"""
    return splitting.pjacobi(x, M, -q)

args = parse_args()

# params
iterations = args.iter
precision = args.eps

cols = min(3, len(args.file) )
rows = len(args.file) / cols 


import matplotlib
from matplotlib import pyplot as plt

for param in [ 'axes.titlesize',
               'axes.labelsize',
               'xtick.labelsize',
               'ytick.labelsize',
               'legend.fontsize' ]:
    matplotlib.rcParams[param] = args.fontsize

_, plots = plt.subplots(rows, cols)

for i, f in enumerate(args.file):
    
    (M, q) = lcp.load( f )
    
    # solver list
    solvers = [pgs, pjacobi,
               accel.nlnscg(pgs),
               accel.nlnscg(pgs, metric = np.diag(M) ),
               accel.nlnscg(pjacobi),
               accel.nlnscg(pjacobi, metric = np.diag(M) ),
    ]

    # error metric 
    error = metric.lcp_merit( (M, q) )

    print 'file:', f
    print 'dim:', q.size
    print 'metric:', error.__doc__

    p = plots[ i / cols, i % cols ] if rows > 1 else plots[ i ] if cols > 1 else plots
    
    for s in solvers:

        name = s.__doc__

        run = lcp.bench( (M, q), s,
                         iterations = iterations,
                         precision = precision,
                         metric = error )

        print '\t{}...'.format(name), 
        data = [e for k, e in run]
        print '\tit: {} \t eps: {:.2e}'.format(len(data), data[-1])

        p.plot( data, label = name )
        p.set_title('{} (n = {})'.format(f, q.size))
        p.set_yscale('log')
        if not args.nolegend: p.legend()

plt.show()


{'fraction_non_alphanumeric': 0.13883495145631067, 'fraction_numerical': 0.003883495145631068, 'mean_word_length': 2.1172465960665656, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '6727527', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1379, 'n_words': 349}