evaluate.py 3.06 KB
Newer Older
Prasanna's avatar
Prasanna committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from string import Template
import re
import os
import sys
import time
import json
import math
import os
import subprocess
import csv

def readResults(fname, evalnum):
    pattern1 = re.compile("START TIME:", re.IGNORECASE)
    pattern2 = re.compile("OUTPUT:", re.IGNORECASE)
    pattern3 = re.compile("END TIME:", re.IGNORECASE)
    pattern4 = re.compile("INPUT:", re.IGNORECASE)
    resDict = {}
    resDict['evalnum'] = evalnum
    resDict['startTime'] = -1
    resDict['endTime'] = -1
    resDict['cost'] = sys.float_info.max
    resDict['x'] = None
    try:
        while True:
            with open(fname, 'rt') as in_file:
                for linenum, line in enumerate(in_file):
                    if pattern1.search(line) is not None:
                        print(line)
                        str1 = line.rstrip('\n')
                        res = re.findall('START TIME:(.*)', str1)
                        resDict['startTime'] = int(res[0])
                    elif pattern2.search(line) is not None:
                        print(line)
                        str1 = line.rstrip('\n')
                        res = re.findall('OUTPUT:(.*)', str1)
                        rv = float(res[0])
                        if math.isnan(rv):
                            rv = sys.float_info.max
                        resDict['cost'] = rv
                    elif pattern3.search(line) is not None:
                        print(line)
                        str1 = line.rstrip('\n')
                        res = re.findall('END TIME:(.*)', str1)
                        resDict['endTime'] = int(res[0])
                    elif pattern4.search(line) is not None:
                        print(line)
                        str1 = line.rstrip('\n')
                        res = re.findall('INPUT:(.*)', str1)
                        resDict['x'] = eval(res[0])
                if len(resDict.keys()) == 5:
                    key = os.path.basename(fname)
                    resDict['key'] = key
                    resDict['status'] = 0
            if 'endTime' in resDict.keys():
                    break
            time.sleep(5)
    except Exception:
        print('Unexpected error:', sys.exc_info()[0])
    print(resDict)
    return(resDict)

def commandLine(x, params):
    cmd = ''
    hlist = []
    for p, v in zip(params, x):
        cmd = cmd + ('--%s %s ') % (p, str(v))
    print(cmd)
    return(cmd)

def evaluate(x, evalCounter, params, prob_dir, job_dir, result_dir):
    cmd = commandLine(x, params)
    jobfile = job_dir+'/%05d.job' % evalCounter
    outputfile = result_dir+'/%05d.dat' % evalCounter
    filein = open(prob_dir+'/job.tmpl')
    src = Template(filein.read())
    inpstr = str(x)
    d = {'outputfile': outputfile, 'inpstr': inpstr, 'cmd': cmd, 'ompn':x[0]}
    result = src.substitute(d)
    with open(jobfile, "w") as jobFile:
        jobFile.write(result)
    status = subprocess.check_output('chmod +x %s' % jobfile, shell=True)
    status = subprocess.call(' sh %s ' % jobfile, shell=True)
    resDict = readResults(outputfile, evalCounter)
    print(resDict)

    return(resDict)