From 8e57909f3d2e649a2b0907428b697203a2f8bb39 Mon Sep 17 00:00:00 2001 From: iasonmanolas Date: Fri, 12 Feb 2021 18:09:56 +0200 Subject: [PATCH] Refactoring" --- src/main.cpp | 25 +++++++++++++++---------- src/reducedmodeloptimizer.cpp | 12 +++++------- src/reducedmodeloptimizer.hpp | 16 ++++++++-------- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 1eac1ab..ccbf59d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,7 +17,6 @@ #include int main(int argc, char *argv[]) { - // Create reduced models // FormFinder::runUnitTests(); const std::vector numberOfNodesPerSlot{1, 0, 0, 2, 1, 2, 1}; @@ -48,7 +47,7 @@ int main(int argc, char *argv[]) { ReducedModelOptimizer::xRange beamE{"E", 0.1, 1.9}; ReducedModelOptimizer::xRange innerHexagonSize{"HexagonSize", 0.1, 0.9}; // Test set of full patterns - std::string fullPatternsTestSetDirectory = "../TestSet"; + std::string fullPatternsTestSetDirectory = "TestSet"; if (!std::filesystem::exists( std::filesystem::path(fullPatternsTestSetDirectory))) { std::cerr << "Full pattern directory does not exist: " @@ -70,14 +69,16 @@ int main(int argc, char *argv[]) { continue; } + FlatPattern fullPattern(filepathString); + fullPattern.setLabel(filepath.stem().string()); + fullPattern.scale(0.03); for (int reducedPatternIndex = 0; reducedPatternIndex < reducedModels.size(); reducedPatternIndex++) { - FlatPattern *pFullPattern = new FlatPattern(filepathString); - pFullPattern->setLabel(filepath.stem().string()); - pFullPattern->scale(0.03); + FlatPattern *pFullPattern = new FlatPattern(); + pFullPattern->copy(fullPattern); FlatPattern *pReducedPattern = new FlatPattern(); pReducedPattern->copy(*reducedModels[reducedPatternIndex]); - // pReducedPattern->copy(*reducedModels[2]); + //pReducedPattern->copy(*reducedModels[0]); patternPairs.push_back(std::make_pair(pFullPattern, pReducedPattern)); } } @@ -94,8 +95,12 @@ int main(int argc, char *argv[]) { settings_optimization.numberOfFunctionCalls = 10; const std::string optimizationSettingsString = settings_optimization.toString(); + std::string optimiziationResultsDirectory = "../OptimizationResults"; +// if (argc == 1) { +// optimiziationResultsDirectory = argv[0]; +//} std::filesystem::path thisOptimizationDirectory( - std::filesystem::path("../OptimizationResults") + std::filesystem::path(optimiziationResultsDirectory) .append(optimizationSettingsString)); std::filesystem::create_directories(thisOptimizationDirectory); std::cout << optimizationSettingsString << std::endl; @@ -111,7 +116,7 @@ int main(int argc, char *argv[]) { patternPairs.size()); auto start = std::chrono::high_resolution_clock::now(); - //#pragma omp parallel for + #pragma omp parallel for for (int patternPairIndex = 0; patternPairIndex < patternPairs.size(); patternPairIndex++) { const std::vector numberOfNodesPerSlot{1, 0, 0, 2, 1, 2, 1}; @@ -138,8 +143,8 @@ int main(int argc, char *argv[]) { patternPairs[patternPairIndex].second->getLabel())); std::filesystem::create_directories(std::filesystem::path(saveToPath)); - optimizationResults_testSet[patternPairIndex].save(saveToPath); - // optimizationResults_testSet[patternPairIndex].draw(); + optimizationResults_testSet[patternPairIndex].save(saveToPath.string()); + optimizationResults_testSet[patternPairIndex].draw(); } csvFile statistics(std::filesystem::path(thisOptimizationDirectory) .append("statistics.csv") diff --git a/src/reducedmodeloptimizer.cpp b/src/reducedmodeloptimizer.cpp index b81fb03..8cfb7fd 100644 --- a/src/reducedmodeloptimizer.cpp +++ b/src/reducedmodeloptimizer.cpp @@ -351,11 +351,10 @@ double ReducedModelOptimizer::objective(long n, const double *x) { global.minY = error; global.minX.assign(x, x + n); } - global.numberOfFunctionCalls++; - // if (++global.numberOfFunctionCalls % 50 == 0) { + if (++global.numberOfFunctionCalls % 100 == 0) { std::cout << "Number of function calls:" << global.numberOfFunctionCalls << std::endl; - //} + } // compute error and return it global.gObjectiveValueHistory.push_back(error); @@ -688,8 +687,7 @@ void ReducedModelOptimizer::computeDesiredReducedModelDisplacements( } ReducedModelOptimizer::Results ReducedModelOptimizer::runOptimization( - const Settings &settings, - double (*pObjectiveFunction)(long, const double *)) { + const Settings &settings) { auto &global = tls[omp_get_thread_num()]; global.gObjectiveValueHistory.clear(); @@ -740,7 +738,6 @@ ReducedModelOptimizer::Results ReducedModelOptimizer::runOptimization( xMax(i) = settings.xRanges[i].max; } - global.numberOfFunctionCalls = 0; auto start = std::chrono::system_clock::now(); dlib::function_evaluation result; if (global.optimizeInnerHexagonSize) { @@ -1130,6 +1127,7 @@ ReducedModelOptimizer::Results ReducedModelOptimizer::optimize( global.g_firstRoundIterationIndex = 0; global.minY = std::numeric_limits::max(); global.numOfSimulationCrashes = 0; + global.numberOfFunctionCalls = 0; // polyscope::removeAllStructures(); FormFinder::Settings settings; @@ -1152,7 +1150,7 @@ ReducedModelOptimizer::Results ReducedModelOptimizer::optimize( global.reducedPatternSimulationJobs[simulationScenarioIndex] = std::make_shared(reducedPatternSimulationJob); } - Results optResults = runOptimization(xRanges, &objective); + Results optResults = runOptimization(xRanges); for (int simulationScenarioIndex : global.simulationScenarioIndices) { optResults.fullPatternSimulationJobs.push_back( simulationJobs[simulationScenarioIndex]); diff --git a/src/reducedmodeloptimizer.hpp b/src/reducedmodeloptimizer.hpp index f2ceaef..b404592 100644 --- a/src/reducedmodeloptimizer.hpp +++ b/src/reducedmodeloptimizer.hpp @@ -160,9 +160,7 @@ private: const SimulationResults &fullModelResults, const std::unordered_map &displacementsReducedToFullMap, Eigen::MatrixX3d &optimalDisplacementsOfReducedModel); - static Results runOptimization(const Settings &settings, - double (*pObjectiveFunction)(long, - const double *)); + static Results runOptimization(const Settings &settings); std::vector> createScenarios(const std::shared_ptr &pMesh); void computeMaps(FlatPattern &fullModel, FlatPattern &reducedPattern, @@ -243,15 +241,17 @@ struct ReducedModelOptimizer::Results { .append(pFullPatternSimulationJob->label)); std::filesystem::create_directory(simulationJobFolderPath); const auto fullPatternDirectoryPath = - std::filesystem::path(simulationJobFolderPath).append("FullPattern"); + std::filesystem::path(simulationJobFolderPath).append("Full"); std::filesystem::create_directory(fullPatternDirectoryPath); pFullPatternSimulationJob->save(fullPatternDirectoryPath.string()); const std::shared_ptr &pReducedPatternSimulationJob = reducedPatternSimulationJobs[simulationJobIndex]; const auto reducedPatternDirectoryPath = std::filesystem::path(simulationJobFolderPath) - .append("ReducedPattern"); - std::filesystem::create_directory(reducedPatternDirectoryPath); + .append("Reduced"); + if (!std::filesystem::exists(reducedPatternDirectoryPath)) { + std::filesystem::create_directory(reducedPatternDirectoryPath); + } pReducedPatternSimulationJob->save(reducedPatternDirectoryPath.string()); } } @@ -264,7 +264,7 @@ struct ReducedModelOptimizer::Results { // Load reduced pattern files for (const auto &fileEntry : filesystem::directory_iterator( std::filesystem::path(simulationScenarioPath) - .append("FullPattern"))) { + .append("Full"))) { const auto filepath = fileEntry.path(); if (filepath.extension() == ".json") { SimulationJob job; @@ -277,7 +277,7 @@ struct ReducedModelOptimizer::Results { // Load full pattern files for (const auto &fileEntry : filesystem::directory_iterator( std::filesystem::path(simulationScenarioPath) - .append("ReducedPattern"))) { + .append("Reduced"))) { const auto filepath = fileEntry.path(); if (filepath.extension() == ".json") { SimulationJob job;