ReducedModelOptimization/main.py

133 lines
5.9 KiB
Python

# This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import multiprocessing
import subprocess
import os
from os import listdir
from os.path import isfile, join
import itertools
import shutil
import sys
from datetime import datetime
from subprocess import check_output
from shutil import copyfile
numberOfOptimizedPatterns=0
numberOfSkippedPatterns=0
numberOfFunctionCalls=0
start_time = datetime.now()
def listener(q):
#print("Entered listener")
with open(os.path.join(resultsDir,'results.csv'), 'a') as f:
while 1:
m = q.get()
if m == 'kill':
# f.write('killed')
break
global numberOfOptimizedPatterns
numberOfOptimizedPatterns = numberOfOptimizedPatterns + 1
print("Optimized patterns:" + str(numberOfOptimizedPatterns))
if not m:
global numberOfSkippedPatterns
numberOfSkippedPatterns=numberOfSkippedPatterns+1
continue
f.write(m)
f.flush()
global start_time
#print("Before")
if numberOfSkippedPatterns != numberOfOptimizedPatterns:
averageTimePerPattern_min=(datetime.now()-start_time).total_seconds()/(60*(numberOfOptimizedPatterns-numberOfSkippedPatterns))
print("Average minutes/pattern:"+str(averageTimePerPattern_min))
#print("After")
# global totalNumberOfPatterns
# print(totalNumberOfPatterns)
# global totalNumberOfPatterns
# print(totalNumberOfPatterns)
# completionPercentage=numberOfOptimizedPatterns/totalNumberOfPatterns
# print("Optimized patterns:" + str(completionPercentage))
def optimize(fullPatternFilepath, reducedPatternFilepath,translationalObjectiveWeight):
"""Call run(), catch exceptions."""
dirname = os.path.abspath(os.path.dirname(__file__))
# dirname="/home/iason/Coding/build/ReducedModelOptimization/Release"
executableFilepath = os.path.join(dirname, 'ReducedModelOptimization')
#putResultsTo=os.path.join(resultsDir,str(translationalObjectiveWeight))
#putResultsTo=os.path.join(putResultsTo,os.path.basename(os.path.dirname(os.path.dirname(fullPatternFilepath))))
putResultsTo=os.path.join(resultsDir,os.path.basename(os.path.dirname(os.path.dirname(fullPatternFilepath))))
try:
# global resultsDir
p = args = (executableFilepath,
fullPatternFilepath,
reducedPatternFilepath,
str(numberOfFunctionCalls),str(translationalObjectiveWeight),
#os.path.join(resultsDir,os.path.basename(os.path.dirname(fullPatternFilepath))))
putResultsTo)
patternStartTime=datetime.now()
#print("Optimizing " + fullPatternFilepath+" at "+str(datetime.now()))
popen = subprocess.Popen(args, stdout=subprocess.PIPE,bufsize=1005024)
#popen.wait()
#output = popen.stdout.read()
output = check_output(args).decode("utf-8")
#output,error=popen.communicate()
duration_min=(datetime.now() - patternStartTime).total_seconds()/60
#print("Optimized " + fullPatternFilepath+" in "+str(duration_min)+" minutes")
q.put(output)
except Exception as e:
print("error: %s run(*%r, **%r)" % (e, fullPatternFilepath,reducedPatternFilepath))
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
dirOfThisFile = os.path.abspath(os.path.dirname(__file__))
copyFrom="/home/iason/Coding/build/ReducedModelOptimization/Release/ReducedModelOptimization"
copyfile(copyFrom, os.path.join(dirOfThisFile,"ReducedModelOptimization"))
fullPatternDirectory= "/home/iason/Coding/Projects/Approximating shapes with flat patterns/ReducedModelOptimization/TestSet/FullPatterns/selectionOfPatterns"
numberOfFunctionCalls=100000
optimizationBatchName='variableComparison_AllVars_ensmallen'+'_'+str(int(numberOfFunctionCalls/1000))+'k'
resultsDir=os.path.join(dirOfThisFile,os.path.join('Results/OptimizationResults/',optimizationBatchName))
#print(resultsDir)
if not os.path.exists(resultsDir):
os.makedirs(resultsDir)
#shutil.rmtree(resultsDir)
manager = multiprocessing.Manager()
q = manager.Queue()
reducedPatternFilepath= "TestSet/ReducedPatterns/single_reduced.ply"
fullPatternFilepaths=[]
pool=multiprocessing.Pool(11)
watcher = pool.apply_async(listener, (q,))
for subdir, dirs, files in os.walk(fullPatternDirectory):
#print(subdir)
#print(dirs)
#print(files)
#if !os.path.exists(resultsDir):
# Write results
#fullPatternFilepaths.extend([join(subdir, fullPatternFilepath) for fullPatternFilepath in listdir(subdir) if isfile(join(subdir, fullPatternFilepath))])
fullPatternFilepaths.extend(os.path.join(subdir, fullPatternFile) for fullPatternFile in files if fullPatternFile.endswith(".ply"))
#fullPatternFilepaths.extend([os.path.join(fullPatternDirect])
#print(fullPatternFilepaths)
#print(optimizationPairs)
jobs=[]
translationalObjectiveWeights=[1.2] #[x/10 for x in range(2,18,2)]
jobs.extend(list(itertools.product(fullPatternFilepaths,[reducedPatternFilepath],translationalObjectiveWeights)))
#print(optimizationPairs)
totalNumberOfPatterns=len(jobs)
print("Runnning:",optimizationBatchName)
print("Number of function calls:",numberOfFunctionCalls)
print("Full pattern test set directory:"+fullPatternDirectory)
print("Total number of optimization jobs:"+ str(totalNumberOfPatterns))
print("Results directory:"+str(resultsDir))
print("Start time:", start_time)
pool.starmap(optimize,jobs)
print("Completed")
# f.close()
q.put('kill')
pool.close()
pool.join()