2021-12-15 14:21:21 +01:00
# 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
2021-12-21 20:24:45 +01:00
from shutil import copyfile
2021-12-15 14:21:21 +01:00
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. """
2021-12-21 20:24:45 +01:00
dirname = os . path . abspath ( os . path . dirname ( __file__ ) )
# dirname="/home/iason/Coding/build/ReducedModelOptimization/Release"
2021-12-15 14:21:21 +01:00
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__ ) )
2021-12-21 20:24:45 +01:00
copyFrom = " /home/iason/Coding/build/ReducedModelOptimization/Release/ReducedModelOptimization "
copyfile ( copyFrom , os . path . join ( dirOfThisFile , " ReducedModelOptimization " ) )
2021-12-15 14:21:21 +01:00
fullPatternDirectory = " /home/iason/Coding/Projects/Approximating shapes with flat patterns/ReducedModelOptimization/TestSet/FullPatterns/selectionOfPatterns "
numberOfFunctionCalls = 100000
2021-12-21 20:24:45 +01:00
optimizationBatchName = ' variableComparison_AllVars_ensmallen ' + ' _ ' + str ( int ( numberOfFunctionCalls / 1000 ) ) + ' k '
2021-12-15 14:21:21 +01:00
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 ) )
2021-12-21 20:24:45 +01:00
print ( " Results directory: " + str ( resultsDir ) )
2021-12-15 14:21:21 +01:00
print ( " Start time: " , start_time )
pool . starmap ( optimize , jobs )
print ( " Completed " )
# f.close()
q . put ( ' kill ' )
pool . close ( )
pool . join ( )