Added ensmallen,armadillo. Parametrized optimization with choice of optimization parameters
This commit is contained in:
parent
6c92b291da
commit
535dd8a44d
|
@ -25,15 +25,48 @@ endif()
|
|||
##Create directory for the external libraries
|
||||
file(MAKE_DIRECTORY ${EXTERNAL_DEPS_DIR})
|
||||
|
||||
|
||||
#Add the project sources
|
||||
file(GLOB SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.hpp ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
|
||||
|
||||
if(MSVC)
|
||||
add_compile_definitions(_HAS_STD_BYTE=0)
|
||||
endif(MSVC)
|
||||
|
||||
add_executable(${PROJECT_NAME} ${SOURCES})
|
||||
|
||||
if(${CMAKE_BUILD_TYPE} STREQUAL "Release")
|
||||
set(USE_POLYSCOPE FALSE)
|
||||
set(MYSOURCES_STATIC_LINK TRUE)
|
||||
set(USE_POLYSCOPE false)
|
||||
set(MYSOURCES_STATIC_LINK true)
|
||||
set(USE_ENSMALLEN false)
|
||||
else()
|
||||
set(USE_POLYSCOPE TRUE)
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES POSITION_INDEPENDENT_CODE TRUE)
|
||||
set(USE_POLYSCOPE true)
|
||||
add_compile_definitions(POLYSCOPE_DEFINED)
|
||||
set(MYSOURCES_STATIC_LINK FALSE)
|
||||
set(MYSOURCES_STATIC_LINK false)
|
||||
set(USE_ENSMALLEN true)
|
||||
if(${USE_ENSMALLEN})
|
||||
add_compile_definitions(USE_ENSMALLEN)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
#dlib
|
||||
set(DLIB_BIN_DIR ${CMAKE_CURRENT_BINARY_DIR}/dlib_bin)
|
||||
file(MAKE_DIRECTORY ${DLIB_BIN_DIR})
|
||||
download_project(PROJ DLIB
|
||||
GIT_REPOSITORY https://github.com/davisking/dlib.git
|
||||
GIT_TAG master
|
||||
BINARY_DIR ${DLIB_BIN_DIR}
|
||||
PREFIX ${EXTERNAL_DEPS_DIR}
|
||||
${UPDATE_DISCONNECTED_IF_AVAILABLE}
|
||||
)
|
||||
add_subdirectory(${DLIB_SOURCE_DIR} ${DLIB_BINARY_DIR})
|
||||
if(${MYSOURCES_STATIC_LINK})
|
||||
target_link_libraries(${PROJECT_NAME} "-static" dlib::dlib)
|
||||
else()
|
||||
target_link_libraries(${PROJECT_NAME} dlib::dlib)
|
||||
endif()
|
||||
add_compile_definitions(DLIB_DEFINED)
|
||||
|
||||
set(MYSOURCES_SOURCE_DIR "/home/iason/Coding/Libraries/MySources")
|
||||
if (EXISTS ${MYSOURCES_SOURCE_DIR})
|
||||
|
@ -48,36 +81,16 @@ download_project(PROJ MYSOURCES
|
|||
endif()
|
||||
add_subdirectory(${MYSOURCES_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}/MySourcesBinDir)
|
||||
|
||||
#dlib
|
||||
set(DLIB_BIN_DIR ${CMAKE_CURRENT_BINARY_DIR}/dlib_bin)
|
||||
file(MAKE_DIRECTORY ${DLIB_BIN_DIR})
|
||||
download_project(PROJ DLIB
|
||||
GIT_REPOSITORY https://github.com/davisking/dlib.git
|
||||
GIT_TAG master
|
||||
BINARY_DIR ${DLIB_BIN_DIR}
|
||||
PREFIX ${EXTERNAL_DEPS_DIR}
|
||||
${UPDATE_DISCONNECTED_IF_AVAILABLE}
|
||||
)
|
||||
add_subdirectory(${DLIB_SOURCE_DIR} ${DLIB_BINARY_DIR})
|
||||
|
||||
##Eigen 3 NOTE: Eigen is required on the system the code is ran
|
||||
find_package(Eigen3 3.3 REQUIRED)
|
||||
|
||||
#Add the project sources
|
||||
file(GLOB SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.hpp ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
|
||||
|
||||
if(MSVC)
|
||||
add_compile_definitions(_HAS_STD_BYTE=0)
|
||||
endif(MSVC)
|
||||
|
||||
add_executable(${PROJECT_NAME} ${SOURCES})
|
||||
|
||||
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20)
|
||||
target_include_directories(${PROJECT_NAME}
|
||||
PUBLIC ${MYSOURCES_SOURCE_DIR}
|
||||
)
|
||||
if(${MYSOURCES_STATIC_LINK})
|
||||
target_link_libraries(${PROJECT_NAME} -static Eigen3::Eigen dlib::dlib MySources)
|
||||
target_link_libraries(${PROJECT_NAME} -static Eigen3::Eigen MySources)
|
||||
else()
|
||||
target_link_libraries(${PROJECT_NAME} Eigen3::Eigen dlib::dlib MySources)
|
||||
target_link_libraries(${PROJECT_NAME} Eigen3::Eigen MySources)
|
||||
endif()
|
||||
|
||||
##Eigen 3 NOTE: Eigen is required on the system the code is ran
|
||||
find_package(Eigen3 3.3 REQUIRED)
|
||||
|
||||
|
|
|
@ -0,0 +1,130 @@
|
|||
# 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
|
||||
|
||||
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__))
|
||||
fullPatternDirectory= "/home/iason/Coding/Projects/Approximating shapes with flat patterns/ReducedModelOptimization/TestSet/FullPatterns/selectionOfPatterns"
|
||||
#resultsDir=os.path.join(dirOfThisFile,os.path.join('Results/OptimizationResults/',os.path.basename(fullPatternDirectory)))
|
||||
numberOfFunctionCalls=100000
|
||||
optimizationBatchName='variableComparison_allVars_joint_forConfirmation'+'_'+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("Start time:", start_time)
|
||||
pool.starmap(optimize,jobs)
|
||||
print("Completed")
|
||||
# f.close()
|
||||
q.put('kill')
|
||||
pool.close()
|
||||
pool.join()
|
||||
|
33
src/main.cpp
33
src/main.cpp
|
@ -126,26 +126,27 @@ int main(int argc, char *argv[]) {
|
|||
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
optimizationResults.time = elapsed.count() / 1000.0;
|
||||
|
||||
if (optimizationResults.wasSuccessful) {
|
||||
resultsOutputDir = convergedJobsDirPath.string();
|
||||
csvFile csv_resultsLocalFile(std::filesystem::path(resultsOutputDir).append("results.csv"),
|
||||
true);
|
||||
csvFile csv_results({}, false);
|
||||
std::vector<csvFile *> csvVector{&csv_resultsLocalFile, &csv_results};
|
||||
csv_results << "Name";
|
||||
csv_resultsLocalFile << "Name";
|
||||
optimizationResults.writeHeaderTo(csvVector);
|
||||
settings_optimization.writeHeaderTo(csv_results);
|
||||
csv_results << endrow;
|
||||
csv_results << std::to_string(fullPattern.EN()) + "#" + pairName;
|
||||
optimizationResults.writeResultsTo(csvVector);
|
||||
settings_optimization.writeSettingsTo(csv_results);
|
||||
csv_results << endrow;
|
||||
} else {
|
||||
if (!optimizationResults.wasSuccessful) {
|
||||
// resultsOutputDir = crashedJobsDirPath.string();
|
||||
return 1;
|
||||
}
|
||||
resultsOutputDir = convergedJobsDirPath.string();
|
||||
optimizationResults.save(resultsOutputDir, true);
|
||||
csvFile csv_resultsLocalFile(std::filesystem::path(resultsOutputDir).append("results.csv"),
|
||||
true);
|
||||
csvFile csv_results({}, false);
|
||||
std::vector<csvFile *> csvVector{&csv_resultsLocalFile, &csv_results};
|
||||
csv_results << "Name";
|
||||
csv_resultsLocalFile << "Name";
|
||||
optimizationResults.writeHeaderTo(csvVector);
|
||||
settings_optimization.writeHeaderTo(csv_results);
|
||||
csv_results << endrow;
|
||||
csv_resultsLocalFile << endrow;
|
||||
csv_results << std::to_string(fullPattern.EN()) + "#" + pairName;
|
||||
optimizationResults.writeResultsTo(csvVector);
|
||||
settings_optimization.writeSettingsTo(csv_results);
|
||||
csv_results << endrow;
|
||||
csv_resultsLocalFile << endrow;
|
||||
}
|
||||
|
||||
#ifdef POLYSCOPE_DEFINED
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -9,8 +9,10 @@
|
|||
#include "reducedmodeloptimizer_structs.hpp"
|
||||
#include "simulationmesh.hpp"
|
||||
#include <Eigen/Dense>
|
||||
#ifdef DLIB_DEFINED
|
||||
#include <dlib/global_optimization.h>
|
||||
#include <dlib/optimization.h>
|
||||
#endif
|
||||
|
||||
#ifdef POLYSCOPE_DEFINED
|
||||
#include "polyscope/color_management.h"
|
||||
|
@ -39,7 +41,16 @@ class ReducedModelOptimizer
|
|||
constexpr static double youngsModulus{1 * 1e9};
|
||||
|
||||
public:
|
||||
constexpr static struct ParameterLabels
|
||||
struct FunctionEvaluation
|
||||
{
|
||||
FunctionEvaluation() = default;
|
||||
FunctionEvaluation(const std::vector<double> &x, double y) : x(x), y(y) {}
|
||||
|
||||
std::vector<double> x;
|
||||
double y = std::numeric_limits<double>::quiet_NaN();
|
||||
};
|
||||
|
||||
struct ParameterLabels
|
||||
{
|
||||
inline const static std::string E = {"E"};
|
||||
inline const static std::string A = {"A"};
|
||||
|
@ -48,11 +59,13 @@ public:
|
|||
inline const static std::string J = {"J"};
|
||||
inline const static std::string theta = {"Theta"};
|
||||
inline const static std::string R = {"R"};
|
||||
} parameterLabels;
|
||||
};
|
||||
inline constexpr static ParameterLabels parameterLabels();
|
||||
|
||||
enum OptimizationParameterIndex { E, A, I2, I3, J, R, Theta, NumberOfOptimizationParameters };
|
||||
|
||||
constexpr static std::array<int, 5> simulationScenariosResolution = {11, 11, 20, 20, 20};
|
||||
constexpr static std::array<int, 5> baseScenarioWeights = {1, 1, 5, 5, 5};
|
||||
// constexpr static std::array<int, 5> simulationScenariosResolution = {3, 3, 3, 3, 3};
|
||||
inline static int totalNumberOfSimulationScenarios
|
||||
= std::accumulate(simulationScenariosResolution.begin(),
|
||||
|
@ -183,12 +196,9 @@ public:
|
|||
const std::vector<std::pair<FullPatternVertexIndex, FullPatternVertexIndex>>
|
||||
&oppositeInterfaceViPairs,
|
||||
SimulationJob &job);
|
||||
static std::function<void(const dlib::matrix<double, 0, 1> &x,
|
||||
static std::function<void(const std::vector<double> &x,
|
||||
std::shared_ptr<SimulationMesh> &pReducedPatternSimulationMesh)>
|
||||
function_updateReducedPattern;
|
||||
static std::function<void(const dlib::matrix<double, 0, 1> &x,
|
||||
std::shared_ptr<SimulationMesh> &pReducedPatternSimulationMesh)>
|
||||
function_updateReducedPattern_material;
|
||||
static std::function<void(const double &newE,
|
||||
std::shared_ptr<SimulationMesh> &pReducedPatternSimulationMesh)>
|
||||
function_updateReducedPattern_material_E;
|
||||
|
@ -207,9 +217,9 @@ public:
|
|||
static std::function<void(const double &newJ,
|
||||
std::shared_ptr<SimulationMesh> &pReducedPatternSimulationMesh)>
|
||||
function_updateReducedPattern_material_J;
|
||||
static std::function<void(const dlib::matrix<double, 0, 1> &x,
|
||||
std::shared_ptr<SimulationMesh> &pReducedPatternSimulationMesh)>
|
||||
function_updateReducedPattern_geometry;
|
||||
static double objective(const std::vector<double> &x);
|
||||
static void initializeUpdateReducedPatternFunctions();
|
||||
static double objective(const double &xValue);
|
||||
|
||||
private:
|
||||
static void computeDesiredReducedModelDisplacements(
|
||||
|
@ -230,7 +240,8 @@ private:
|
|||
|
||||
DRMSimulationModel simulator;
|
||||
void computeObjectiveValueNormalizationFactors();
|
||||
static void getResults(const dlib::function_evaluation &optimizationResult_dlib,
|
||||
|
||||
static void getResults(const FunctionEvaluation &optimalObjective,
|
||||
const ReducedPatternOptimization::Settings &settings,
|
||||
ReducedPatternOptimization::Results &results);
|
||||
double computeFullPatternMaxSimulationForce(
|
||||
|
@ -244,12 +255,11 @@ private:
|
|||
getFullPatternMaxSimulationForces(
|
||||
const std::vector<ReducedPatternOptimization::BaseSimulationScenario>
|
||||
&desiredBaseSimulationScenarioIndices);
|
||||
|
||||
#ifdef DLIB_DEFINED
|
||||
static double objective(const dlib::matrix<double, 0, 1> &x);
|
||||
static void initializeUpdateReducedPatternFunctions();
|
||||
static double objective(const double &xValue);
|
||||
#endif
|
||||
};
|
||||
inline std::function<void(const dlib::matrix<double, 0, 1> &x, std::shared_ptr<SimulationMesh> &m)>
|
||||
ReducedModelOptimizer::function_updateReducedPattern_material;
|
||||
inline std::function<void(const double &newE,
|
||||
std::shared_ptr<SimulationMesh> &pReducedPatternSimulationMesh)>
|
||||
ReducedModelOptimizer::function_updateReducedPattern_material_E;
|
||||
|
@ -268,10 +278,7 @@ inline std::function<void(const double &newI3,
|
|||
inline std::function<void(const double &newJ,
|
||||
std::shared_ptr<SimulationMesh> &pReducedPatternSimulationMesh)>
|
||||
ReducedModelOptimizer::function_updateReducedPattern_material_J;
|
||||
inline std::function<void(const dlib::matrix<double, 0, 1> &x, std::shared_ptr<SimulationMesh> &m)>
|
||||
ReducedModelOptimizer::function_updateReducedPattern_geometry;
|
||||
inline std::function<void(const dlib::matrix<double, 0, 1> &x,
|
||||
std::shared_ptr<SimulationMesh> &m)>
|
||||
inline std::function<void(const std::vector<double> &x, std::shared_ptr<SimulationMesh> &m)>
|
||||
ReducedModelOptimizer::function_updateReducedPattern;
|
||||
|
||||
#endif // REDUCEDMODELOPTIMIZER_HPP
|
||||
|
|
Loading…
Reference in New Issue