From ae645b27d2828f69dca0d4f5fa491a44fafba477 Mon Sep 17 00:00:00 2001 From: Iason Date: Thu, 18 Feb 2021 12:12:52 +0200 Subject: [PATCH] Refactoring --- src/main.cpp | 237 ++++++++++++++++++---------------- src/reducedmodeloptimizer.cpp | 62 +++++---- 2 files changed, 162 insertions(+), 137 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 54aff52..b4cad26 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,47 +16,51 @@ #include #include -void exportOptimizationResults(const ReducedModelOptimizer::Results& optimizationResults,const std::filesystem::path& optimizationDirectory,const ReducedModelOptimizer::Settings& settings_optimization,csvFile& statistics,const std::pair patternPair) { - std::filesystem::path saveToPath( - std::filesystem::path(optimizationDirectory) - .append(patternPair.first->getLabel() + "@" + - patternPair.second->getLabel())); - std::filesystem::create_directories(std::filesystem::path(saveToPath)); +void exportOptimizationResults( + const ReducedModelOptimizer::Results &optimizationResults, + const std::filesystem::path &optimizationDirectory, + const ReducedModelOptimizer::Settings &settings_optimization, + csvFile &statistics, + const std::pair patternPair) { + std::filesystem::path saveToPath(std::filesystem::path(optimizationDirectory) + .append(patternPair.first->getLabel() + + "@" + + patternPair.second->getLabel())); + std::filesystem::create_directories(std::filesystem::path(saveToPath)); - optimizationResults.save(saveToPath.string()); - statistics << patternPair.first->getLabel() + "@" + - patternPair.second->getLabel(); - statistics << optimizationResults.objectiveValue; - for (const double &optimalX : - optimizationResults.x) { - statistics << optimalX; - } + optimizationResults.save(saveToPath.string()); + statistics << patternPair.first->getLabel() + "@" + + patternPair.second->getLabel(); + statistics << optimizationResults.objectiveValue; + for (const double &optimalX : optimizationResults.x) { + statistics << optimalX; + } - for (int unusedXVarCounter = 0; - unusedXVarCounter < - settings_optimization.xRanges.size() - - optimizationResults.x.size(); - unusedXVarCounter++) { - statistics << "-"; - } - - statistics << optimizationResults.time; - if (optimizationResults.numberOfSimulationCrashes == 0) { - statistics << "No crashes"; - } else { - statistics << optimizationResults.numberOfSimulationCrashes; - } - statistics << endrow; + for (int unusedXVarCounter = 0; + unusedXVarCounter < + settings_optimization.xRanges.size() - optimizationResults.x.size(); + unusedXVarCounter++) { + statistics << "-"; + } + statistics << optimizationResults.time; + if (optimizationResults.numberOfSimulationCrashes == 0) { + statistics << "No crashes"; + } else { + statistics << optimizationResults.numberOfSimulationCrashes; + } + statistics << endrow; } -void optimizeTestSet(const std::vector> patternPairs,const ReducedModelOptimizer::Settings& settings_optimization,const std::string& saveTo) { +void optimizeTestSet( + const std::vector> patternPairs, + const ReducedModelOptimizer::Settings &settings_optimization, + const std::string &saveTo) { const std::string optimizationSettingsString = settings_optimization.toString(); std::filesystem::path thisOptimizationDirectory( - std::filesystem::path(saveTo) - .append(optimizationSettingsString)); - //.append("Results")); + std::filesystem::path(saveTo).append(optimizationSettingsString)); + //.append("Results")); std::filesystem::create_directories(thisOptimizationDirectory); std::cout << optimizationSettingsString << std::endl; csvFile csv_settings(std::filesystem::path(thisOptimizationDirectory) @@ -85,66 +89,72 @@ void optimizeTestSet(const std::vector> std::vector optimizationResults_testSet( patternPairs.size()); auto start = std::chrono::high_resolution_clock::now(); -int pairsOptimized=0; - #pragma omp parallel for + int pairsOptimized = 0; +#pragma omp parallel for for (int patternPairIndex = 0; patternPairIndex < patternPairs.size(); - patternPairIndex++) { - const std::pair& - patternPair = patternPairs[patternPairIndex]; - const std::vector numberOfNodesPerSlot{ 1, 0, 0, 2, 1, 2, 1 }; - ReducedModelOptimizer optimizer(numberOfNodesPerSlot); - optimizer.initializePatterns(*patternPair.first, - *patternPair.second, {}); - ReducedModelOptimizer::Results optimizationResults = - optimizer.optimize(settings_optimization); + patternPairIndex++) { + const std::pair &patternPair = + patternPairs[patternPairIndex]; + std::cout << "Optimizing " << patternPair.first->getLabel() << "@" + << patternPair.second->getLabel() << std::endl; + const std::vector numberOfNodesPerSlot{1, 0, 0, 2, 1, 2, 1}; + ReducedModelOptimizer optimizer(numberOfNodesPerSlot); + optimizer.initializePatterns(*patternPair.first, *patternPair.second, {}); + ReducedModelOptimizer::Results optimizationResults = + optimizer.optimize(settings_optimization); - totalError += optimizationResults.objectiveValue; - optimizationResults_testSet[patternPairIndex] = optimizationResults; - totalNumberOfSimulationCrashes += - optimizationResults.numberOfSimulationCrashes; + totalError += optimizationResults.objectiveValue; + optimizationResults_testSet[patternPairIndex] = optimizationResults; + totalNumberOfSimulationCrashes += + optimizationResults.numberOfSimulationCrashes; - std::cout << "Optimized " << ++pairsOptimized << "/" << patternPairs.size() << std::endl; + std::cout << "Optimized " << ++pairsOptimized << "/" << patternPairs.size() + << std::endl; +#pragma omp critical + { + exportOptimizationResults(optimizationResults, thisOptimizationDirectory, + settings_optimization, statistics, patternPair); + optimizationResults.draw(); + } } - for (int patternPairIndex = 0; patternPairIndex < patternPairs.size(); - patternPairIndex++) { - const std::pair& - patternPair = patternPairs[patternPairIndex]; - const ReducedModelOptimizer::Results optimizationResults=optimizationResults_testSet[patternPairIndex]; - //Save results - std::filesystem::path saveToPath( - std::filesystem::path(thisOptimizationDirectory) - .append(patternPair.first->getLabel() + "@" + - patternPair.second->getLabel())); - std::filesystem::create_directories(std::filesystem::path(saveToPath)); - optimizationResults.save(saveToPath.string()); + // for (int patternPairIndex = 0; patternPairIndex < patternPairs.size(); + // patternPairIndex++) { + // const std::pair &patternPair = + // patternPairs[patternPairIndex]; + // const ReducedModelOptimizer::Results optimizationResults = + // optimizationResults_testSet[patternPairIndex]; + // // Save results + // std::filesystem::path saveToPath( + // std::filesystem::path(thisOptimizationDirectory) + // .append(patternPair.first->getLabel() + "@" + + // patternPair.second->getLabel())); + // std::filesystem::create_directories(std::filesystem::path(saveToPath)); + // optimizationResults.save(saveToPath.string()); - //Save statistics - statistics << patternPair.first->getLabel() + "@" + - patternPair.second->getLabel(); - statistics << optimizationResults.objectiveValue; - for (const double& optimalX : - optimizationResults.x) { - statistics << optimalX; - } + // // Save statistics + // statistics << patternPair.first->getLabel() + "@" + + // patternPair.second->getLabel(); + // statistics << optimizationResults.objectiveValue; + // for (const double &optimalX : optimizationResults.x) { + // statistics << optimalX; + // } - for (int unusedXVarCounter = 0; - unusedXVarCounter < - settings_optimization.xRanges.size() - - optimizationResults.x.size(); - unusedXVarCounter++) { - statistics << "-"; - } + // for (int unusedXVarCounter = 0; + // unusedXVarCounter < + // settings_optimization.xRanges.size() - + // optimizationResults.x.size(); unusedXVarCounter++) { + // statistics << "-"; + // } - statistics << optimizationResults.time; - if (optimizationResults.numberOfSimulationCrashes == 0) { - statistics << "No crashes"; - } - else { - statistics << optimizationResults.numberOfSimulationCrashes; - } - statistics << endrow; - } + // statistics << optimizationResults.time; + // if (optimizationResults.numberOfSimulationCrashes == 0) { + // statistics << "No crashes"; + // } else { + // statistics << optimizationResults.numberOfSimulationCrashes; + // } + // statistics << endrow; + // } } int main(int argc, char *argv[]) { @@ -155,18 +165,20 @@ int main(int argc, char *argv[]) { FlatPattern singleBarReducedModel(numberOfNodesPerSlot, singleBarReducedModelEdges); singleBarReducedModel.setLabel("SingleBar_reduced"); + singleBarReducedModel.savePly(singleBarReducedModel.getLabel()); singleBarReducedModel.scale(0.03); std::vector CWreducedModelEdges{vcg::Point2i(1, 5), - vcg::Point2i(3, 1)}; + vcg::Point2i(3, 5)}; FlatPattern CWReducedModel(numberOfNodesPerSlot, CWreducedModelEdges); - CWReducedModel.setLabel("CCW_reduced"); + CWReducedModel.setLabel("CW_reduced"); CWReducedModel.scale(0.03); std::vector CCWreducedModelEdges{vcg::Point2i(1, 5), - vcg::Point2i(3, 5)}; + vcg::Point2i(3, 1)}; FlatPattern CCWReducedModel(numberOfNodesPerSlot, CCWreducedModelEdges); - CCWReducedModel.setLabel("CW_reduced"); + CCWReducedModel.setLabel("CCW_reduced"); + CCWReducedModel.savePly(CWReducedModel.getLabel()); CCWReducedModel.scale(0.03); std::vector reducedModels{&singleBarReducedModel, @@ -178,7 +190,7 @@ int main(int argc, char *argv[]) { ReducedModelOptimizer::xRange beamE{"E", 0.1, 1.9}; ReducedModelOptimizer::xRange innerHexagonSize{"HS", 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: " @@ -201,17 +213,18 @@ int main(int argc, char *argv[]) { } FlatPattern fullPattern(filepathString); - fullPattern.setLabel(filepath.stem().string()); - fullPattern.scale(0.03); - for (int reducedPatternIndex = 0; - reducedPatternIndex < reducedModels.size(); reducedPatternIndex++) { - FlatPattern *pFullPattern = new FlatPattern(); - pFullPattern->copy(fullPattern); - FlatPattern *pReducedPattern = new FlatPattern(); - pReducedPattern->copy(*reducedModels[reducedPatternIndex]); - //pReducedPattern->copy(*reducedModels[0]); - patternPairs.push_back(std::make_pair(pFullPattern, pReducedPattern)); - } + fullPattern.setLabel(filepath.stem().string()); + fullPattern.scale(0.03); + // for (int reducedPatternIndex = 0; + // reducedPatternIndex < reducedModels.size(); + // reducedPatternIndex++) { + FlatPattern *pFullPattern = new FlatPattern(); + pFullPattern->copy(fullPattern); + FlatPattern *pReducedPattern = new FlatPattern(); + // pReducedPattern->copy(*reducedModels[reducedPatternIndex]); + pReducedPattern->copy(*reducedModels[0]); + patternPairs.push_back(std::make_pair(pFullPattern, pReducedPattern)); + // } } // for (double rangeOffset = 0.15; rangeOffset <= 0.95; rangeOffset += 0.05) @@ -225,19 +238,19 @@ int main(int argc, char *argv[]) { // settings_optimization.numberOfFunctionCalls += 100) { std::string optimiziationResultsDirectory = "OptimizationResults"; if (argc < 2) { - settings_optimization.numberOfFunctionCalls =100; + settings_optimization.numberOfFunctionCalls = 100; + } else if (argc == 2) { + settings_optimization.numberOfFunctionCalls = std::atoi(argv[1]); + } else if (argc == 3) { + settings_optimization.numberOfFunctionCalls = std::atoi(argv[1]); + optimiziationResultsDirectory = argv[2]; } - else if (argc == 2) { - settings_optimization.numberOfFunctionCalls =std::atoi(argv[1]); - } - else if (argc == 3) { - settings_optimization.numberOfFunctionCalls =std::atoi(argv[1]); - optimiziationResultsDirectory = argv[2]; -} - settings_optimization.normalizeObjectiveValue =false; - optimizeTestSet(patternPairs,settings_optimization,optimiziationResultsDirectory); + // settings_optimization.normalizeObjectiveValue = false; + // optimizeTestSet(patternPairs, settings_optimization, + // optimiziationResultsDirectory); settings_optimization.normalizeObjectiveValue = true; - optimizeTestSet(patternPairs,settings_optimization,optimiziationResultsDirectory); + optimizeTestSet(patternPairs, settings_optimization, + optimiziationResultsDirectory); for (auto &patternPair : patternPairs) { delete patternPair.first; diff --git a/src/reducedmodeloptimizer.cpp b/src/reducedmodeloptimizer.cpp index 76fa94e..69c28b5 100644 --- a/src/reducedmodeloptimizer.cpp +++ b/src/reducedmodeloptimizer.cpp @@ -125,7 +125,8 @@ GlobalOptimizationVariables tls[MAX_THREAD] __attribute__((aligned(64))); double ReducedModelOptimizer::computeError( const std::vector &reducedPatternDisplacements, - const std::vector &fullPatternDisplacements,const double& interfaceDisplacementsNormSum, + const std::vector &fullPatternDisplacements, + const double &interfaceDisplacementsNormSum, const std::unordered_map &reducedToFullInterfaceViMap) { auto &global = tls[omp_get_thread_num()]; @@ -156,7 +157,7 @@ double ReducedModelOptimizer::computeError( } if (global.optimizationSettings.normalizeObjectiveValue) { - return error/std::max(interfaceDisplacementsNormSum,0.00003); + return error / std::max(interfaceDisplacementsNormSum, 0.00003); } return error; } @@ -293,8 +294,11 @@ double ReducedModelOptimizer::objective(long n, const double *x) { // simulationSettings); global.numOfSimulationCrashes++; } else { - error += computeError( - reducedModelResults.displacements, global.fullPatternDisplacements[simulationScenarioIndex],global.fullPatternDisplacementNormSum[simulationScenarioIndex], global.reducedToFullInterfaceViMap); + error += computeError( + reducedModelResults.displacements, + global.fullPatternDisplacements[simulationScenarioIndex], + global.fullPatternDisplacementNormSum[simulationScenarioIndex], + global.reducedToFullInterfaceViMap); filename = "/home/iason/Coding/Projects/Approximating shapes with flat " "patterns/RodModelOptimizationForPatterns/build/" "ProblematicSimulationJobs/conv_dimensions.txt"; @@ -327,7 +331,7 @@ double ReducedModelOptimizer::objective(long n, const double *x) { global.minX.assign(x, x + n); } // if (++global.numberOfFunctionCalls %100== 0) { - //std::cout << "Number of function calls:" << global.numberOfFunctionCalls + // std::cout << "Number of function calls:" << global.numberOfFunctionCalls // << std::endl; //} @@ -661,8 +665,8 @@ void ReducedModelOptimizer::computeDesiredReducedModelDisplacements( } } -ReducedModelOptimizer::Results ReducedModelOptimizer::runOptimization( - const Settings &settings) { +ReducedModelOptimizer::Results +ReducedModelOptimizer::runOptimization(const Settings &settings) { auto &global = tls[omp_get_thread_num()]; global.gObjectiveValueHistory.clear(); @@ -1051,20 +1055,23 @@ void ReducedModelOptimizer::visualizeResults( SimulationResults fullModelResults = simulator.executeSimulation(pFullPatternSimulationJob); fullModelResults.registerForDrawing(); - //fullModelResults.saveDeformedModel(); + // fullModelResults.saveDeformedModel(); const std::shared_ptr &pReducedPatternSimulationJob = reducedPatternSimulationJobs[simulationScenarioIndex]; SimulationResults reducedModelResults = simulator.executeSimulation(pReducedPatternSimulationJob); double interfaceDisplacementNormSum = 0; - for (const auto& interfaceViPair : reducedToFullInterfaceViMap) { - const int fullPatternInterfaceIndex = interfaceViPair.second; - Eigen::Vector3d fullPatternDisplacementVector(fullModelResults.displacements[fullPatternInterfaceIndex][0], fullModelResults.displacements[fullPatternInterfaceIndex][1], fullModelResults.displacements[fullPatternInterfaceIndex][2]); - interfaceDisplacementNormSum += fullPatternDisplacementVector.norm(); + for (const auto &interfaceViPair : reducedToFullInterfaceViMap) { + const int fullPatternInterfaceIndex = interfaceViPair.second; + Eigen::Vector3d fullPatternDisplacementVector( + fullModelResults.displacements[fullPatternInterfaceIndex][0], + fullModelResults.displacements[fullPatternInterfaceIndex][1], + fullModelResults.displacements[fullPatternInterfaceIndex][2]); + interfaceDisplacementNormSum += fullPatternDisplacementVector.norm(); } - double error = computeError(reducedModelResults.displacements, - fullModelResults.displacements,interfaceDisplacementNormSum, - reducedToFullInterfaceViMap); + double error = computeError( + reducedModelResults.displacements, fullModelResults.displacements, + interfaceDisplacementNormSum, reducedToFullInterfaceViMap); std::cout << "Error of simulation scenario " << simulationScenarioStrings[simulationScenarioIndex] << " is " << error << std::endl; @@ -1121,17 +1128,22 @@ ReducedModelOptimizer::Results ReducedModelOptimizer::optimize( simulationJobs[simulationScenarioIndex]; SimulationResults fullModelResults = simulator.executeSimulation(pFullPatternSimulationJob, settings); - global.fullPatternDisplacements[simulationScenarioIndex] = fullModelResults.displacements; + global.fullPatternDisplacements[simulationScenarioIndex] = + fullModelResults.displacements; double interfaceDisplacementNormSum = 0; - for (const auto& interfaceViPair : global.reducedToFullInterfaceViMap) { - const int fullPatternInterfaceIndex = interfaceViPair.second; - Eigen::Vector3d fullPatternDisplacementVector(fullModelResults.displacements[fullPatternInterfaceIndex][0], fullModelResults.displacements[fullPatternInterfaceIndex][1], fullModelResults.displacements[fullPatternInterfaceIndex][2]); - interfaceDisplacementNormSum += fullPatternDisplacementVector.norm(); + for (const auto &interfaceViPair : global.reducedToFullInterfaceViMap) { + const int fullPatternInterfaceIndex = interfaceViPair.second; + Eigen::Vector3d fullPatternDisplacementVector( + fullModelResults.displacements[fullPatternInterfaceIndex][0], + fullModelResults.displacements[fullPatternInterfaceIndex][1], + fullModelResults.displacements[fullPatternInterfaceIndex][2]); + interfaceDisplacementNormSum += fullPatternDisplacementVector.norm(); } - global.fullPatternDisplacementNormSum[simulationScenarioIndex] = interfaceDisplacementNormSum; - //global.g_optimalReducedModelDisplacements[simulationScenarioIndex].resize( + global.fullPatternDisplacementNormSum[simulationScenarioIndex] = + interfaceDisplacementNormSum; + // global.g_optimalReducedModelDisplacements[simulationScenarioIndex].resize( // m_pReducedPatternSimulationMesh->VN(), 3); - //computeDesiredReducedModelDisplacements( + // computeDesiredReducedModelDisplacements( // fullModelResults, global.reducedToFullInterfaceViMap, // global.g_optimalReducedModelDisplacements[simulationScenarioIndex]); @@ -1154,7 +1166,7 @@ ReducedModelOptimizer::Results ReducedModelOptimizer::optimize( // optResults.draw(); // visualizeResults(simulationJobs, global.simulationScenarioIndices); - //visualizeResults(simulationJobs, global.reducedPatternSimulationJobs, - // global.simulationScenarioIndices,global.reducedToFullInterfaceViMap); + // visualizeResults(simulationJobs, global.reducedPatternSimulationJobs, + // global.simulationScenarioIndices,global.reducedToFullInterfaceViMap); return optResults; }