src/HOL/Tools/Sledgehammer/MaSh/src/mash.py
author blanchet
Sat, 08 Dec 2012 00:48:50 +0100
changeset 50434 960a3429615c
parent 50399 52d9720f7a48
child 50441 1e71f9d3cd57
permissions -rwxr-xr-x
more MaSh tweaking -- in particular, export the same facts in "MaSh_Export" as are later tried in "MaSh_Eval"
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
     1
#!/usr/bin/python
50222
40e3c3be6bca added file headers
blanchet
parents: 50220
diff changeset
     2
#     Title:      HOL/Tools/Sledgehammer/MaSh/src/mash.py
40e3c3be6bca added file headers
blanchet
parents: 50220
diff changeset
     3
#     Author:     Daniel Kuehlwein, ICIS, Radboud University Nijmegen
40e3c3be6bca added file headers
blanchet
parents: 50220
diff changeset
     4
#     Copyright   2012
40e3c3be6bca added file headers
blanchet
parents: 50220
diff changeset
     5
#
40e3c3be6bca added file headers
blanchet
parents: 50220
diff changeset
     6
# Entry point for MaSh (Machine Learning for Sledgehammer).
40e3c3be6bca added file headers
blanchet
parents: 50220
diff changeset
     7
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
     8
'''
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
     9
MaSh - Machine Learning for Sledgehammer
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    10
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    11
MaSh allows to use different machine learning algorithms to predict relevant fact for Sledgehammer.
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    12
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    13
Created on July 12, 2012
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    14
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    15
@author: Daniel Kuehlwein
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    16
'''
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    17
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    18
import logging,datetime,string,os,sys
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    19
from argparse import ArgumentParser,RawDescriptionHelpFormatter
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    20
from time import time
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    21
from stats import Statistics
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    22
from dictionaries import Dictionaries
50399
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
    23
#from fullNaiveBayes import NBClassifier
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    24
from naiveBayes import NBClassifier
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    25
from snow import SNoW
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    26
from predefined import Predefined
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    27
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    28
# Set up command-line parser
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    29
parser = ArgumentParser(description='MaSh - Machine Learning for Sledgehammer.  \n\n\
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    30
MaSh allows to use different machine learning algorithms to predict relevant facts for Sledgehammer.\n\n\
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    31
--------------- Example Usage ---------------\n\
50434
960a3429615c more MaSh tweaking -- in particular, export the same facts in "MaSh_Export" as are later tried in "MaSh_Eval"
blanchet
parents: 50399
diff changeset
    32
First initialize:\n./mash.py -l test.log -o ../tmp/ --init --inputDir ../data/Jinja/ \n\
960a3429615c more MaSh tweaking -- in particular, export the same facts in "MaSh_Export" as are later tried in "MaSh_Eval"
blanchet
parents: 50399
diff changeset
    33
Then create predictions:\n./mash.py -i ../data/Jinja/mash_commands -p ../data/Jinja/mash_suggestions -l test.log -o ../tmp/ --statistics\n\
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    34
\n\n\
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    35
Author: Daniel Kuehlwein, July 2012',formatter_class=RawDescriptionHelpFormatter)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    36
parser.add_argument('-i','--inputFile',help='File containing all problems to be solved.')
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    37
parser.add_argument('-o','--outputDir', default='../tmp/',help='Directory where all created files are stored. Default=../tmp/.')
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
    38
parser.add_argument('-p','--predictions',default='../tmp/%s.predictions' % datetime.datetime.now(),
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    39
                    help='File where the predictions stored. Default=../tmp/dateTime.predictions.')
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    40
parser.add_argument('--numberOfPredictions',default=200,help="Number of premises to write in the output. Default=200.",type=int)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    41
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    42
parser.add_argument('--init',default=False,action='store_true',help="Initialize Mash. Requires --inputDir to be defined. Default=False.")
50434
960a3429615c more MaSh tweaking -- in particular, export the same facts in "MaSh_Export" as are later tried in "MaSh_Eval"
blanchet
parents: 50399
diff changeset
    43
parser.add_argument('--inputDir',default='../data/Jinja/',\
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    44
                    help='Directory containing all the input data. MaSh expects the following files: mash_features,mash_dependencies,mash_accessibility')
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    45
parser.add_argument('--depFile', default='mash_dependencies',
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    46
                    help='Name of the file with the premise dependencies. The file must be in inputDir. Default = mash_dependencies')
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    47
parser.add_argument('--saveModel',default=False,action='store_true',help="Stores the learned Model at the end of a prediction run. Default=False.")
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    48
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    49
parser.add_argument('--nb',default=False,action='store_true',help="Use Naive Bayes for learning. This is the default learning method.")
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    50
parser.add_argument('--snow',default=False,action='store_true',help="Use SNoW's naive bayes instead of Naive Bayes for learning.")
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    51
parser.add_argument('--predef',default=False,action='store_true',\
50399
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
    52
                    help="Use predefined predictions. Used only for comparison with the actual learning. Expects mash_mepo_suggestions in inputDir.")
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    53
parser.add_argument('--statistics',default=False,action='store_true',help="Create and show statistics for the top CUTOFF predictions.\
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    54
                    WARNING: This will make the program a lot slower! Default=False.")
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    55
parser.add_argument('--saveStats',default=None,help="If defined, stores the statistics in the filename provided.")
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    56
parser.add_argument('--cutOff',default=500,help="Option for statistics. Only consider the first cutOff predictions. Default=500.",type=int)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    57
parser.add_argument('-l','--log', default='../tmp/%s.log' % datetime.datetime.now(), help='Log file name. Default=../tmp/dateTime.log')
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    58
parser.add_argument('-q','--quiet',default=False,action='store_true',help="If enabled, only print warnings. Default=False.")
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    59
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
    60
def main(argv = sys.argv[1:]):
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    61
    # Initializing command-line arguments
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    62
    args = parser.parse_args(argv)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    63
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
    64
    # Set up logging
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    65
    logging.basicConfig(level=logging.DEBUG,
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    66
                        format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    67
                        datefmt='%d-%m %H:%M:%S',
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    68
                        filename=args.log,
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    69
                        filemode='w')
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    70
    console = logging.StreamHandler(sys.stdout)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    71
    console.setLevel(logging.INFO)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    72
    formatter = logging.Formatter('# %(message)s')
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    73
    console.setFormatter(formatter)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    74
    logging.getLogger('').addHandler(console)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    75
    logger = logging.getLogger('main.py')
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    76
    if args.quiet:
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    77
        logger.setLevel(logging.WARNING)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    78
        console.setLevel(logging.WARNING)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    79
    if not os.path.exists(args.outputDir):
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    80
        os.makedirs(args.outputDir)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    81
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    82
    logger.info('Using the following settings: %s',args)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    83
    # Pick algorithm
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    84
    if args.nb:
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
    85
        logger.info('Using Naive Bayes for learning.')
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
    86
        model = NBClassifier()
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    87
        modelFile = os.path.join(args.outputDir,'NB.pickle')
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    88
    elif args.snow:
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    89
        logger.info('Using naive bayes (SNoW) for learning.')
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    90
        model = SNoW()
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    91
        modelFile = os.path.join(args.outputDir,'SNoW.pickle')
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    92
    elif args.predef:
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    93
        logger.info('Using predefined predictions.')
50399
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
    94
        #predictionFile = os.path.join(args.inputDir,'mash_meng_paulson_suggestions') 
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
    95
        predictionFile = os.path.join(args.inputDir,'mash_mepo_suggestions')
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    96
        model = Predefined(predictionFile)
50399
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
    97
        modelFile = os.path.join(args.outputDir,'mepo.pickle')        
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    98
    else:
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
    99
        logger.info('No algorithm specified. Using Naive Bayes.')
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   100
        model = NBClassifier()
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   101
        modelFile = os.path.join(args.outputDir,'NB.pickle')
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   102
    dictsFile = os.path.join(args.outputDir,'dicts.pickle')
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   103
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   104
    # Initializing model
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   105
    if args.init:
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   106
        logger.info('Initializing Model.')
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   107
        startTime = time()
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   108
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   109
        # Load all data
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   110
        dicts = Dictionaries()
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   111
        dicts.init_all(args.inputDir,depFileName=args.depFile)
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   112
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   113
        # Create Model
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   114
        trainData = dicts.featureDict.keys()
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   115
        if args.predef:
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   116
            dicts = model.initializeModel(trainData,dicts)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   117
        else:
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   118
            model.initializeModel(trainData,dicts)
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   119
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   120
        model.save(modelFile)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   121
        dicts.save(dictsFile)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   122
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   123
        logger.info('All Done. %s seconds needed.',round(time()-startTime,2))
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   124
        return 0
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   125
    # Create predictions and/or update model
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   126
    else:
50399
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   127
        lineCounter = 1
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   128
        statementCounter = 1
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   129
        computeStats = False
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   130
        dicts = Dictionaries()
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   131
        # Load Files
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   132
        if os.path.isfile(dictsFile):
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   133
            dicts.load(dictsFile)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   134
        if os.path.isfile(modelFile):
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   135
            model.load(modelFile)
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   136
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   137
        # IO Streams
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   138
        OS = open(args.predictions,'a')
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   139
        IS = open(args.inputFile,'r')
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   140
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   141
        # Statistics
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   142
        if args.statistics:
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   143
            stats = Statistics(args.cutOff)
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   144
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   145
        predictions = None
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   146
        #Reading Input File
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   147
        for line in IS:
50399
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   148
#           try:
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   149
            if True:
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   150
                if line.startswith('!'):
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   151
                    problemId = dicts.parse_fact(line)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   152
                    # Statistics
50399
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   153
                    if args.statistics and computeStats:
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   154
                        computeStats = False
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   155
                        acc = dicts.accessibleDict[problemId]
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   156
                        if args.predef:
50399
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   157
                            predictions = model.predict(problemId)
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   158
                        else:
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   159
                            predictions,_predictionsValues = model.predict(dicts.featureDict[problemId],dicts.expand_accessibles(acc))
50399
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   160
                        stats.update(predictions,dicts.dependenciesDict[problemId],statementCounter)
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   161
                        if not stats.badPreds == []:
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   162
                            bp = string.join([str(dicts.idNameDict[x]) for x in stats.badPreds], ',')
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   163
                            logger.debug('Bad predictions: %s',bp)
50399
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   164
                    statementCounter += 1
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   165
                    # Update Dependencies, p proves p
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   166
                    dicts.dependenciesDict[problemId] = [problemId]+dicts.dependenciesDict[problemId]
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   167
                    model.update(problemId,dicts.featureDict[problemId],dicts.dependenciesDict[problemId])
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   168
                elif line.startswith('p'):
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   169
                    # Overwrite old proof.
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   170
                    problemId,newDependencies = dicts.parse_overwrite(line)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   171
                    newDependencies = [problemId]+newDependencies
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   172
                    model.overwrite(problemId,newDependencies,dicts)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   173
                    dicts.dependenciesDict[problemId] = newDependencies
50399
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   174
                elif line.startswith('?'):                    
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   175
                    startTime = time()
50399
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   176
                    computeStats = True
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   177
                    if args.predef:
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   178
                        continue
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   179
                    name,features,accessibles = dicts.parse_problem(line)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   180
                    # Create predictions
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   181
                    logger.info('Starting computation for problem on line %s',lineCounter)
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   182
                    predictions,predictionValues = model.predict(features,accessibles)
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   183
                    assert len(predictions) == len(predictionValues)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   184
                    logger.info('Done. %s seconds needed.',round(time()-startTime,2))
50399
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   185
                    # Output        
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   186
                    predictionNames = [str(dicts.idNameDict[p]) for p in predictions[:args.numberOfPredictions]]
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   187
                    predictionValues = [str(x) for x in predictionValues[:args.numberOfPredictions]]
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   188
                    predictionsStringList = ['%s=%s' % (predictionNames[i],predictionValues[i]) for i in range(len(predictionNames))]
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   189
                    predictionsString = string.join(predictionsStringList,' ')
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   190
                    outString = '%s: %s' % (name,predictionsString)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   191
                    OS.write('%s\n' % outString)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   192
                else:
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   193
                    logger.warning('Unspecified input format: \n%s',line)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   194
                    sys.exit(-1)
50399
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   195
                lineCounter += 1
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   196
            """
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   197
            except:
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   198
                logger.warning('An error occurred on line %s .',line)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   199
                lineCounter += 1
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   200
                continue
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   201
            """
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   202
        OS.close()
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   203
        IS.close()
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   204
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   205
        # Statistics
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   206
        if args.statistics:
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   207
            stats.printAvg()
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   208
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   209
        # Save
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   210
        if args.saveModel:
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   211
            model.save(modelFile)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   212
        dicts.save(dictsFile)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   213
        if not args.saveStats == None:
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   214
            statsFile = os.path.join(args.outputDir,args.saveStats)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   215
            stats.save(statsFile)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   216
    return 0
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   217
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   218
if __name__ == '__main__':
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   219
    # Example:
50434
960a3429615c more MaSh tweaking -- in particular, export the same facts in "MaSh_Export" as are later tried in "MaSh_Eval"
blanchet
parents: 50399
diff changeset
   220
    # Jinja
960a3429615c more MaSh tweaking -- in particular, export the same facts in "MaSh_Export" as are later tried in "MaSh_Eval"
blanchet
parents: 50399
diff changeset
   221
    #args = ['-l','testIsabelle.log','-o','../tmp/','--statistics','--init','--inputDir','../data/Jinja/','--predef']
960a3429615c more MaSh tweaking -- in particular, export the same facts in "MaSh_Export" as are later tried in "MaSh_Eval"
blanchet
parents: 50399
diff changeset
   222
    #args = ['-i', '../data/Jinja/mash_commands','-p','../tmp/testIsabelle.pred','-l','testIsabelle.log','--predef','-o','../tmp/','--statistics','--saveStats','../tmp/natATPMP.stats']
960a3429615c more MaSh tweaking -- in particular, export the same facts in "MaSh_Export" as are later tried in "MaSh_Eval"
blanchet
parents: 50399
diff changeset
   223
    #args = ['-l','testNB.log','-o','../tmp/','--statistics','--init','--inputDir','../data/Jinja/']
960a3429615c more MaSh tweaking -- in particular, export the same facts in "MaSh_Export" as are later tried in "MaSh_Eval"
blanchet
parents: 50399
diff changeset
   224
    #args = ['-i', '../data/Jinja/mash_commands','-p','../tmp/testNB.pred','-l','../tmp/testNB.log','--nb','-o','../tmp/','--statistics','--saveStats','../tmp/natATPNB.stats','--cutOff','500']
50399
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   225
    # List
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   226
    #args = ['-l','testIsabelle.log','-o','../tmp/','--statistics','--init','--inputDir','../data/List/','--isabelle']
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   227
    #args = ['-i', '../data/List/mash_commands','-p','../tmp/testIsabelle.pred','-l','testIsabelle.log','--isabelle','-o','../tmp/','--statistics']
50399
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   228
    # Huffmann
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   229
    #args = ['-l','testNB.log','-o','../tmp/','--statistics','--init','--inputDir','../data/Huffman/','--depFile','mash_atp_dependencies']
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   230
    #args = ['-l','testNB.log','-o','../tmp/','--statistics','--init','--inputDir','../data/Huffman/']
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   231
    #args = ['-i', '../data/Huffman/mash_commands','-p','../tmp/testNB.pred','-l','testNB.log','--nb','-o','../tmp/','--statistics']
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   232
    # Arrow
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   233
    #args = ['-l','testNB.log','-o','../tmp/','--statistics','--init','--inputDir','../data/Arrow_Order/']    
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   234
    #args = ['-i', '../data/Arrow_Order/mash_commands','-p','../tmp/testNB.pred','-l','../tmp/testNB.log','--nb','-o','../tmp/','--statistics','--saveStats','../tmp/arrowIsarNB.stats','--cutOff','500']
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   235
    # Fundamental_Theorem_Algebra
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   236
    #args = ['-l','testNB.log','-o','../tmp/','--statistics','--init','--inputDir','../data/Fundamental_Theorem_Algebra/']    
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   237
    #args = ['-i', '../data/Fundamental_Theorem_Algebra/mash_commands','-p','../tmp/testNB.pred','-l','../tmp/testNB.log','--nb','-o','../tmp/','--statistics','--saveStats','../tmp/Fundamental_Theorem_AlgebraIsarNB.stats','--cutOff','500']
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   238
    #args = ['-l','testIsabelle.log','-o','../tmp/','--statistics','--init','--inputDir','../data/Fundamental_Theorem_Algebra/','--predef']
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   239
    #args = ['-i', '../data/Fundamental_Theorem_Algebra/mash_commands','-p','../tmp/Fundamental_Theorem_AlgebraMePo.pred','-l','testIsabelle.log','--predef','-o','../tmp/','--statistics','--saveStats','../tmp/Fundamental_Theorem_AlgebraMePo.stats']
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   240
    # Jinja
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   241
    #args = ['-l','testNB.log','-o','../tmp/','--statistics','--init','--inputDir','../data/Jinja/']    
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   242
    #args = ['-i', '../data/Jinja/mash_commands','-p','../tmp/testNB.pred','-l','../tmp/testNB.log','--nb','-o','../tmp/','--statistics','--saveStats','../tmp/JinjaIsarNB.stats','--cutOff','500']
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   243
52d9720f7a48 made Python code compile again (by Daniel K.)
blanchet
parents: 50388
diff changeset
   244
    
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   245
    #startTime = time()
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   246
    #sys.exit(main(args))
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   247
    #print 'New ' + str(round(time()-startTime,2))
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   248
    sys.exit(main())